/
controller.php
149 lines (127 loc) · 4.34 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Concrete\Block\ExternalForm;
use Concrete\Core\Block\BlockController;
class Controller extends BlockController
{
public $helpers = ['file', 'form'];
protected $btTable = 'btExternalForm';
protected $btInterfaceWidth = 420;
protected $btInterfaceHeight = 175;
protected $btCacheBlockRecord = true;
protected $btWrapperClass = 'ccm-ui';
/**
* Used for localization. If we want to localize the name/description we have to include this.
*/
public function getBlockTypeDescription()
{
return t('Include external forms in the filesystem and place them on pages.');
}
public function getBlockTypeName()
{
return t('External Form');
}
public function getJavaScriptStrings()
{
return ['form-required' => t('You must select a form.')];
}
public function getFilename()
{
return $this->filename;
}
public function getExternalFormFilenamePath()
{
if ($this->filename) {
if (file_exists(DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL . '/' . $this->filename)) {
$filename = DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL . '/' . $this->filename;
} else {
if (file_exists(DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL_CORE . '/' . $this->filename)) {
$filename = DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL_CORE . '/' . $this->filename;
}
}
}
if ($filename) {
return $filename;
}
}
public function isValidControllerTask($method, $parameters = [])
{
$controller = $this->getController();
if ($controller) {
if (method_exists($controller, $method)) {
return true;
}
return parent::isValidControllerTask($method, $parameters);
}
}
public function validate($args)
{
$e = $this->app->make('helper/validation/error');
if (!$args['filename']) {
$e->add(t('You must specify an external form.'));
} else {
$filename = $args['filename'];
if (substr($filename, -4) === '.php') {
// Let's run our regular expression check on everything BEFORE ".php"
$filenameToCheck = substr($filename, 0, -4);
} else {
$filenameToCheck = $filename; // We just check the entirety of what's passed in.
}
if (!preg_match('/^[A-Za-z0-9_-]+$/i', $filenameToCheck)) {
$e->add(
t('External forms may only contain letters, numbers, dashes and underscores.')
);
}
}
return $e;
}
protected function getController()
{
try {
$class = camelcase(substr($this->filename, 0, strrpos($this->filename, '.php')));
$cl = $this->app->make(
overrideable_core_class(
'Block\\ExternalForm\\Form\\Controller\\' . $class,
DIRNAME_BLOCKS . '/external_form/form/controller/' . $this->filename
)
);
$cl->bID = $this->bID;
return $cl;
} catch (\Exception $e) {
}
}
public function runAction($method, $parameters = [])
{
$controller = $this->getController();
if ($controller) {
$controller->runAction($method, $parameters);
foreach ($controller->getSets() as $key => $value) {
$this->set($key, $value);
}
}
parent::runAction($method, $parameters);
}
public function add()
{
$this->set('filenames', $this->getFormList());
}
public function getFormList()
{
$forms = [];
$fh = $this->app->make('helper/file');
if (file_exists(DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL)) {
$forms = array_merge(
$forms,
$fh->getDirectoryContents(DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL, ['controller']));
}
if (file_exists(DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL_CORE)) {
$forms = array_merge(
$forms,
$fh->getDirectoryContents(DIR_FILES_BLOCK_TYPES_FORMS_EXTERNAL_CORE, ['controller']));
}
return $forms;
}
public function edit()
{
$this->set('filenames', $this->getFormList());
}
}