1
|
|
|
<?php |
2
|
|
|
//------------------------------------------------------------------------- |
3
|
|
|
// OVIDENTIA http://www.ovidentia.org |
4
|
|
|
// Ovidentia is free software; you can redistribute it and/or modify |
5
|
|
|
// it under the terms of the GNU General Public License as published by |
6
|
|
|
// the Free Software Foundation; either version 2, or (at your option) |
7
|
|
|
// any later version. |
8
|
|
|
// |
9
|
|
|
// This program is distributed in the hope that it will be useful, but |
10
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12
|
|
|
// See the GNU General Public License for more details. |
13
|
|
|
// |
14
|
|
|
// You should have received a copy of the GNU General Public License |
15
|
|
|
// along with this program; if not, write to the Free Software |
16
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
17
|
|
|
// USA. |
18
|
|
|
//------------------------------------------------------------------------- |
19
|
|
|
/** |
20
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL) |
21
|
|
|
* @copyright Copyright (c) 2008 by CANTICO ({@link http://www.cantico.fr}) |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
require_once $GLOBALS['babInstallPath'] . 'utilit/controller.class.php'; |
25
|
|
|
require_once $GLOBALS['babInstallPath'] . 'utilit/json.php'; |
26
|
|
|
|
27
|
|
|
require_once dirname(__FILE__). '/functions.php'; |
28
|
|
|
|
29
|
|
|
$App = app_App(); |
30
|
|
|
if ($App) { |
|
|
|
|
31
|
|
|
$App->includeBase(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class app_UnknownActionException extends Exception |
36
|
|
|
{ |
37
|
|
|
public function __construct($action, $code = 0) |
38
|
|
|
{ |
39
|
|
|
$message = 'Unknown method "' . $action->getController() . '::' . $action->getMethod() . '"'; |
40
|
|
|
parent::__construct($message, $code); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
class app_Controller extends bab_Controller implements app_Object_Interface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var Func_App |
51
|
|
|
*/ |
52
|
|
|
protected $app = null; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string[] |
58
|
|
|
*/ |
59
|
|
|
protected $reloadSelectors = array(); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $reloadSelector |
65
|
|
|
*/ |
66
|
|
|
public function addReloadSelector($reloadSelector) |
67
|
|
|
{ |
68
|
|
|
$this->reloadSelectors[$reloadSelector] = $reloadSelector; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
|
|
public function getReloadSelectors() |
76
|
|
|
{ |
77
|
|
|
return $this->reloadSelectors; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Can be used in a controller to check if the current request was made by ajax. |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
protected $isAjaxRequest = null; |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Name of method to use to create action |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
public $createActionMethod = 'createActionForTg'; |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Func_App $app |
98
|
|
|
*/ |
99
|
|
|
public function __construct(Func_App $app = null) |
100
|
|
|
{ |
101
|
|
|
$this->setApp($app); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @param mixed $arguments |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function __call($name, $arguments = array()) |
111
|
|
|
{ |
112
|
|
|
switch (true) { |
113
|
|
|
case ($component = $this->App()->getComponentByName(ucfirst($name))) != false: |
114
|
|
|
return call_user_func_array(array($component, 'controller'), $arguments); |
115
|
|
|
default: |
116
|
|
|
throw new \app_Exception(sprintf($this->App()->translate('Unknown action %s'), $name)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
* @see app_Object::setApp() |
124
|
|
|
*/ |
125
|
|
|
public function setApp(Func_App $app = null) |
126
|
|
|
{ |
127
|
|
|
$this->app = $app; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
* @see app_Object::App() |
134
|
|
|
*/ |
135
|
|
|
public function App() |
136
|
|
|
{ |
137
|
|
|
return $this->app; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function getControllerTg() |
145
|
|
|
{ |
146
|
|
|
return $this->App()->controllerTg; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Can be used in a controller to check if the current request was made by ajax. |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function isAjaxRequest() |
155
|
|
|
{ |
156
|
|
|
if (!isset($this->isAjaxRequest)) { |
157
|
|
|
$this->isAjaxRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); |
158
|
|
|
} |
159
|
|
|
return $this->isAjaxRequest; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Ensures that the user is logged in. |
165
|
|
|
* |
166
|
|
|
* Tries to use the appropriate authentication type depending on the situation. |
167
|
|
|
*/ |
168
|
|
|
public function requireCredential($message = null) |
169
|
|
|
{ |
170
|
|
|
if ($this->isAjaxRequest()) { |
171
|
|
|
$authType = 'Basic'; |
172
|
|
|
} else { |
173
|
|
|
$authType = ''; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (!isset($message)) { |
177
|
|
|
$message = $this->App()->translate('You must be logged in to access this page.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
bab_requireCredential($message, $authType); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns an instance of the specific controller class. |
188
|
|
|
* If $proxy is true, a proxy to this controller is returned instead. |
189
|
|
|
* |
190
|
|
|
* @deprecated use $App->ControllerProxy() instead |
191
|
|
|
* |
192
|
|
|
* @param string $classname |
193
|
|
|
* @param bool $proxy |
194
|
|
|
* @return app_Controller |
195
|
|
|
*/ |
196
|
|
|
public static function getInstance($classname, $proxy = true) |
197
|
|
|
{ |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
// If the app object was not specified (through the setApp() method) |
201
|
|
|
// we try to select one according to the classname prefix. |
202
|
|
|
list($prefix) = explode('_', __CLASS__); |
203
|
|
|
$functionalityName = ucwords($prefix); |
204
|
|
|
$App = @bab_functionality::get('App/' . $functionalityName); |
205
|
|
|
|
206
|
|
|
if (!$App) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
throw new app_Exception('Faild to autodetect functionality App/' . $functionalityName.', the getInstance method is deprecated, use $App->ControllerProxy() instead'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$controller = $App->ControllerProxy($classname, $proxy); |
|
|
|
|
212
|
|
|
|
213
|
|
|
return $controller; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Dynamically creates a proxy class for this controller with all public, non-final and non-static functions |
219
|
|
|
* overriden so that they return an action (Widget_Action) corresponding to each of them. |
220
|
|
|
* |
221
|
|
|
* @param string $classname |
222
|
|
|
* @return app_Controller |
223
|
|
|
*/ |
224
|
|
|
static function getProxyInstance(Func_App $App, $classname) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$class = new ReflectionClass($classname); |
227
|
|
|
$proxyClassname = $class->getShortName() . self::PROXY_CLASS_SUFFIX; |
228
|
|
|
if (!class_exists($proxyClassname)) { |
229
|
|
|
$classStr = 'class ' . $proxyClassname . ' extends ' . $class->name . ' {' . "\n"; |
230
|
|
|
$methods = $class->getMethods(); |
231
|
|
|
|
232
|
|
|
$classStr .= ' public function __construct(Func_App $App) { |
233
|
|
|
$this->setApp($App); |
234
|
|
|
}' . "\n"; |
235
|
|
|
|
236
|
|
|
foreach ($methods as $method) { |
237
|
|
|
if ($method->name === '__construct' || !$method->isPublic() || $method->isStatic() || $method->isFinal() || $method->name === 'setApp' || $method->name === 'App') { |
238
|
|
|
continue; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
$classStr .= ' public function ' . $method->name . '('; |
243
|
|
|
$parameters = $method->getParameters(); |
244
|
|
|
$parametersStr = array(); |
245
|
|
|
foreach ($parameters as $parameter) { |
246
|
|
|
|
247
|
|
|
if ($parameter->isDefaultValueAvailable()) { |
248
|
|
|
$parametersStr[] = '$' . $parameter->name . ' = ' . var_export($parameter->getDefaultValue(), true); |
249
|
|
|
} else { |
250
|
|
|
$parametersStr[] = '$' . $parameter->name; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
$classStr .= implode(', ', $parametersStr); |
254
|
|
|
$classStr .= ') {' . "\n"; |
255
|
|
|
$classStr .= ' $args = func_get_args();' . "\n"; |
256
|
|
|
$classStr .= ' return $this->getMethodAction(__FUNCTION__, $args);' . "\n"; |
257
|
|
|
$classStr .= ' }' . "\n"; |
258
|
|
|
} |
259
|
|
|
$classStr .= '}' . "\n"; |
260
|
|
|
|
261
|
|
|
// We define the proxy class |
262
|
|
|
eval($classStr); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$proxy = new $proxyClassname($App); |
266
|
|
|
|
267
|
|
|
//$proxy = bab_getInstance($proxyClassname); |
268
|
|
|
return $proxy; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
|
|
public function proxy() |
276
|
|
|
{ |
277
|
|
|
return self::getProxyInstance($this->App(), get_class($this)); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get proxy class with action from the new addon controller |
282
|
|
|
* @return self |
283
|
|
|
*/ |
284
|
|
|
protected function quickProxy() |
285
|
|
|
{ |
286
|
|
|
$proxy = $this->proxy(); |
287
|
|
|
$proxy->createActionMethod = 'createActionForAddon'; |
288
|
|
|
return $proxy; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritDoc} |
295
|
|
|
* @see bab_Controller::getObjectName() |
296
|
|
|
*/ |
297
|
|
|
protected function getObjectName($classname) |
298
|
|
|
{ |
299
|
|
|
list($objectname) = explode('Controller', $classname); |
300
|
|
|
if($this->App()->getComponentByName($objectname)){ |
301
|
|
|
return strtolower($objectname); |
302
|
|
|
} |
303
|
|
|
list(, $objectname) = explode('_Ctrl', $classname); |
304
|
|
|
return strtolower($objectname); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Returns the action object corresponding to the current object method $methodName |
309
|
|
|
* with the parameters $args. |
310
|
|
|
* |
311
|
|
|
* @param string $methodName |
312
|
|
|
* @param array $args |
313
|
|
|
* @return Widget_Action Or null on error. |
314
|
|
|
*/ |
315
|
|
|
protected function getMethodAction($methodName, $args) |
316
|
|
|
{ |
317
|
|
|
$fullClassName = substr(get_class($this), 0, -strlen(self::PROXY_CLASS_SUFFIX)); |
318
|
|
|
$className = join('', array_slice(explode('\\', $fullClassName), -1)); |
319
|
|
|
|
320
|
|
|
$reflector = new ReflectionClass(get_class($this)); |
321
|
|
|
$parent = $reflector->getParentClass(); |
322
|
|
|
if($parent){ |
323
|
|
|
$parentMethod = $parent->getMethod('__construct'); |
324
|
|
|
$docComment = $parentMethod->getDocComment(); |
325
|
|
|
if (strpos($docComment, '@isComponentController') !== false) { |
326
|
|
|
$fullClassName = $parent->getName(); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$rc = new ReflectionClass($fullClassName); |
331
|
|
|
|
332
|
|
|
if (!$rc->hasMethod($methodName)) { |
333
|
|
|
throw new bab_InvalidActionException($fullClassName . '::' . $methodName); |
334
|
|
|
} |
335
|
|
|
$method = new ReflectionMethod($fullClassName, $methodName); |
336
|
|
|
|
337
|
|
|
$objectName = $this->getObjectName($className); |
338
|
|
|
|
339
|
|
|
$parameters = $method->getParameters(); |
340
|
|
|
$actionParams = array(); |
341
|
|
|
$argNumber = 0; |
342
|
|
|
foreach ($parameters as $parameter) { |
343
|
|
|
$parameterName = $parameter->getName(); |
344
|
|
|
if (isset($args[$argNumber])) { |
345
|
|
|
$actionParams[$parameterName] = $args[$argNumber]; |
346
|
|
|
} elseif ($parameter->isDefaultValueAvailable()) { |
347
|
|
|
$actionParams[$parameterName] = $parameter->getDefaultValue(); |
348
|
|
|
} else { |
349
|
|
|
$actionParams[$parameterName] = null; |
350
|
|
|
} |
351
|
|
|
$argNumber++; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$action = new Widget_Action(); |
355
|
|
|
|
356
|
|
|
$action->setMethod($this->getControllerTg(), $objectName . '.' . $methodName, $actionParams); |
357
|
|
|
|
358
|
|
|
$docComment = $method->getDocComment(); |
359
|
|
|
if (strpos($docComment, '@ajax') !== false) { |
360
|
|
|
$action->setAjax(true); |
361
|
|
|
} |
362
|
|
|
if (strpos($docComment, '@requireSaveMethod') !== false && method_exists($action, 'setRequireSaveMethod')) { |
363
|
|
|
$action->setRequireSaveMethod(true); |
364
|
|
|
} |
365
|
|
|
if (strpos($docComment, '@requireDeleteMethod') !== false && method_exists($action, 'setRequireDeleteMethod')) { |
366
|
|
|
$action->setRequireDeleteMethod(true); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
return $action; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return Widget_Action |
375
|
|
|
*/ |
376
|
|
|
protected function createActionForTg($idx, $actionParams) |
377
|
|
|
{ |
378
|
|
|
$action = new Widget_Action(); |
379
|
|
|
|
380
|
|
|
$action->setMethod($this->App()->controllerTg, $idx, $actionParams); |
381
|
|
|
|
382
|
|
|
return $action; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @return Widget_Action |
388
|
|
|
*/ |
389
|
|
|
protected function createActionForAddon($idx, $actionParams) |
390
|
|
|
{ |
391
|
|
|
$App = $this->App(); |
392
|
|
|
$action = new Widget_Action(); |
393
|
|
|
|
394
|
|
|
$action->setParameters($actionParams); |
395
|
|
|
|
396
|
|
|
|
397
|
|
|
list(,,$file) = explode('/', $App->controllerTg); |
398
|
|
|
|
399
|
|
|
$action->setParameter('addon', $App->getAddonName().'.'.$file); |
400
|
|
|
$action->setParameter('idx', $idx); |
401
|
|
|
|
402
|
|
|
return $action; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Tries to dispatch the action to the correct sub-controller. |
409
|
|
|
* |
410
|
|
|
* @param Widget_Action $action |
411
|
|
|
* @return mixed |
412
|
|
|
*/ |
413
|
|
|
final public function execute(Widget_Action $action) |
414
|
|
|
{ |
415
|
|
|
$this->app->setCurrentComponent(null); |
416
|
|
|
|
417
|
|
|
$method = $action->getMethod(); |
418
|
|
|
|
419
|
|
|
if (!isset($method) || '' === $method) { |
420
|
|
|
return false; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
list($objectName, ) = explode('.', $method); |
424
|
|
|
|
425
|
|
|
if (!method_exists($this, $objectName)) { |
426
|
|
|
/* @var $component app_Component */ |
427
|
|
|
$component = $this->app->getComponentByName(ucfirst($objectName)); |
428
|
|
|
if($component){ |
|
|
|
|
429
|
|
|
$this->app->setCurrentComponent($component); |
430
|
|
|
$objectController = $component->controller(false); |
431
|
|
|
} |
432
|
|
|
else{ |
433
|
|
|
header('HTTP/1.0 400 Bad Request'); |
434
|
|
|
throw new app_UnknownActionException($action); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
if(!isset($component)){ |
439
|
|
|
$objectController = $this->{$objectName}(false); |
440
|
|
|
if ( ! ($objectController instanceof app_Controller)) { |
441
|
|
|
return false; |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
try { |
446
|
|
|
$returnedValue = $objectController->execAction($action); |
|
|
|
|
447
|
|
|
} catch (app_AccessException $e) { |
448
|
|
|
|
449
|
|
|
if (!bab_isUserLogged() && $e->requireCredential) { |
450
|
|
|
bab_requireCredential($e->getMessage()); |
451
|
|
|
} else { |
452
|
|
|
if ($this->isAjaxRequest()) { |
453
|
|
|
|
454
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403); |
455
|
|
|
|
456
|
|
|
die( |
457
|
|
|
bab_json_encode( |
458
|
|
|
array( |
459
|
|
|
'exception' => 'app_AccessException', |
460
|
|
|
'message' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
461
|
|
|
'errorMessage' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
462
|
|
|
) |
463
|
|
|
) |
464
|
|
|
); |
465
|
|
|
} |
466
|
|
|
app_addPageInfo($e->getMessage()); |
467
|
|
|
|
468
|
|
|
$returnedValue = $this->App()->Ui()->Page(); |
469
|
|
|
// $returnedValue->addError($e->getMessage()); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
} catch (app_SaveException $e) { |
473
|
|
|
|
474
|
|
|
if ($this->isAjaxRequest()) { |
475
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403); |
476
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
477
|
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
478
|
|
|
header('Content-type: application/json'); |
479
|
|
|
|
480
|
|
|
die( |
481
|
|
|
bab_json_encode( |
482
|
|
|
array( |
483
|
|
|
'exception' => 'app_SaveException', |
484
|
|
|
'message' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
485
|
|
|
'errorMessage' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
486
|
|
|
) |
487
|
|
|
) |
488
|
|
|
); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
} catch (app_DeletedRecordException $e) { |
492
|
|
|
$this->deletedItemPage($action, $e); |
493
|
|
|
|
494
|
|
|
} catch (app_NotFoundException $e) { |
495
|
|
|
$this->notFoundPage($action, $e); |
496
|
|
|
|
497
|
|
|
} catch (ORM_Exception $e) { |
498
|
|
|
$this->errorPage($e); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
$W = bab_Widgets(); |
502
|
|
|
|
503
|
|
|
if ($returnedValue instanceof Widget_Displayable_Interface) { |
504
|
|
|
|
505
|
|
|
if ($returnedValue instanceof Widget_BabPage && !$this->isAjaxRequest()) { |
506
|
|
|
|
507
|
|
|
// If the action returned a page, we display it. |
508
|
|
|
$returnedValue->displayHtml(); |
509
|
|
|
|
510
|
|
|
} else { |
511
|
|
|
|
512
|
|
|
$htmlCanvas = $W->HtmlCanvas(); |
513
|
|
|
if (self::$acceptJson) { |
514
|
|
|
$itemId = $returnedValue->getId(); |
515
|
|
|
$returnArray = array($itemId => $returnedValue->display($htmlCanvas)); |
516
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
517
|
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
518
|
|
|
header('Content-type: application/json'); |
519
|
|
|
die(bab_json_encode($returnArray)); |
520
|
|
|
} else { |
521
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
522
|
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
523
|
|
|
header('Content-type: text/html'); |
524
|
|
|
if ($returnedValue instanceof Widget_Page && method_exists($returnedValue, 'getPageTitle')) { |
525
|
|
|
$pageTitle = $returnedValue->getPageTitle(); |
526
|
|
|
|
527
|
|
|
if (method_exists($htmlCanvas, 'sendPageTitle')) { |
528
|
|
|
$htmlCanvas->sendPageTitle($pageTitle); |
529
|
|
|
} else { |
530
|
|
|
header('X-Cto-PageTitle: ' . $pageTitle); |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
$html = $returnedValue->display($htmlCanvas); |
534
|
|
|
die($html); |
|
|
|
|
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
} elseif (is_array($returnedValue)) { |
539
|
|
|
|
540
|
|
|
$htmlCanvas = $W->HtmlCanvas(); |
541
|
|
|
$returnedArray = array(); |
542
|
|
|
foreach ($returnedValue as $key => &$item) { |
543
|
|
|
if ($item instanceof Widget_Displayable_Interface) { |
544
|
|
|
$returnedArray[$item->getId()] = $item->display($htmlCanvas); |
545
|
|
|
} else { |
546
|
|
|
$returnedArray[$key] = $item; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
} |
550
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
551
|
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
552
|
|
|
header('Content-type: application/json'); |
553
|
|
|
die(bab_convertStringFromDatabase(bab_json_encode($returnedArray), bab_charset::UTF_8)); |
554
|
|
|
|
555
|
|
|
} elseif (true === $returnedValue || is_string($returnedValue)) { |
556
|
|
|
|
557
|
|
|
if ($this->isAjaxRequest()) { |
558
|
|
|
$body = bab_getBody(); |
559
|
|
|
$json = array(); |
560
|
|
|
$json['messages'] = array(); |
561
|
|
|
foreach ($body->messages as $message) { |
562
|
|
|
$json['messages'][] = array( |
563
|
|
|
'level' => 'info', |
564
|
|
|
'content' => $message, |
565
|
|
|
'time' => 4000 |
566
|
|
|
); |
567
|
|
|
} |
568
|
|
|
foreach ($body->errors as $message) { |
569
|
|
|
$json['messages'][] = array( |
570
|
|
|
'level' => 'danger', |
571
|
|
|
'content' => $message |
572
|
|
|
); |
573
|
|
|
} |
574
|
|
|
if ($objectController->getReloadSelectors()) { |
575
|
|
|
$json['reloadSelector'] = implode(',', $objectController->getReloadSelectors()); |
576
|
|
|
} |
577
|
|
|
echo bab_json_encode($json); |
578
|
|
|
die; |
|
|
|
|
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
return $returnedValue; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
private function deletedItemPage(Widget_Action $action, app_DeletedRecordException $e) |
587
|
|
|
{ |
588
|
|
|
if ($this->isAjaxRequest()) { |
589
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403); |
590
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
591
|
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
592
|
|
|
header('Content-type: application/json'); |
593
|
|
|
|
594
|
|
|
die( |
595
|
|
|
app_json_encode( |
|
|
|
|
596
|
|
|
|
597
|
|
|
array( |
598
|
|
|
'exception' => 'app_SaveException', |
599
|
|
|
'message' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
600
|
|
|
'errorMessage' => bab_convertStringFromDatabase($e->getMessage(), 'UTF-8'), |
601
|
|
|
) |
602
|
|
|
) |
603
|
|
|
); |
604
|
|
|
|
605
|
|
|
} |
606
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404); |
607
|
|
|
|
608
|
|
|
$App = $this->App(); |
609
|
|
|
$W = bab_Widgets(); |
610
|
|
|
|
611
|
|
|
$page = $App->Ui()->Page(); |
612
|
|
|
|
613
|
|
|
$dialog = $W->Frame(null, $W->VBoxLayout()->setVerticalSpacing(2, 'em')) |
614
|
|
|
->addClass(Func_Icons::ICON_LEFT_16); |
615
|
|
|
|
616
|
|
|
$page->addItem($dialog); |
617
|
|
|
|
618
|
|
|
//TRANSLATORS: %s can be task, contact, organization ... |
619
|
|
|
$dialog->addItem($W->Title(sprintf(app_translate('This %s has been deleted'), $e->getObjectTitle()), 1)); |
620
|
|
|
|
621
|
|
|
if ($e instanceof app_DeletedRecordException) { |
|
|
|
|
622
|
|
|
$dialog->addItem( |
623
|
|
|
$W->VBoxItems( |
624
|
|
|
$e->getDeletedBy(), |
625
|
|
|
$e->getDeletedOn() |
626
|
|
|
) |
627
|
|
|
); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
$page->displayHtml(); |
631
|
|
|
|
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
|
635
|
|
|
|
636
|
|
|
|
637
|
|
|
private function notFoundPage(Widget_Action $action, app_NotFoundException $e) |
638
|
|
|
{ |
639
|
|
|
if ($this->isAjaxRequest()) { |
640
|
|
|
$message = sprintf(app_translate('This %s does not exists'), $e->getObjectTitle() . ' (' . $e->getId() . ')'); |
641
|
|
|
$json = array( |
642
|
|
|
'messages' => array( |
643
|
|
|
array( |
644
|
|
|
'level' => 'danger', |
645
|
|
|
'content' => $message |
646
|
|
|
) |
647
|
|
|
) |
648
|
|
|
); |
649
|
|
|
echo bab_json_encode($json); |
650
|
|
|
die; |
|
|
|
|
651
|
|
|
} |
652
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404); |
653
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
654
|
|
|
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
655
|
|
|
|
656
|
|
|
$App = $this->App(); |
657
|
|
|
$W = bab_Widgets(); |
658
|
|
|
|
659
|
|
|
$page = $App->Ui()->Page(); |
660
|
|
|
$page->addClass(Func_Icons::ICON_LEFT_16); |
661
|
|
|
|
662
|
|
|
//TRANSLATORS: %s can be task, contact, organization ... |
663
|
|
|
$page->addItem($W->Title(sprintf(app_translate('This %s does not exists'), $e->getObjectTitle()), 1)); |
664
|
|
|
$page->displayHtml(); |
665
|
|
|
|
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
|
669
|
|
|
|
670
|
|
|
private function errorPage(Exception $e) |
671
|
|
|
{ |
672
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal server error', true, 500); |
673
|
|
|
|
674
|
|
|
require_once $GLOBALS['babInstallPath'] . 'utilit/uiutil.php'; |
675
|
|
|
$popup = new babBodyPopup(); |
|
|
|
|
676
|
|
|
$popup->babEcho( |
677
|
|
|
'<h1>' . $this->App()->translate('There was a problem when trying to perform this operation') . '</h1>' |
678
|
|
|
. '<h2 class="error">' . $this->App()->translate('Additional debugging information:') . '</h2>' |
679
|
|
|
. '<p class="error">' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getTraceAsString() . '</p>' |
680
|
|
|
); |
681
|
|
|
|
682
|
|
|
|
683
|
|
|
die($popup->printout()); |
|
|
|
|
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
|
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Method to call before saving |
690
|
|
|
* @since 1.0.22 |
691
|
|
|
* @return self |
692
|
|
|
*/ |
693
|
|
|
public function requireSaveMethod() |
694
|
|
|
{ |
695
|
|
|
if ('GET' === $_SERVER['REQUEST_METHOD']) { |
696
|
|
|
throw new app_Exception('Method not allowed'); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
return $this; |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* Method to call before deleting |
705
|
|
|
* @since 1.0.22 |
706
|
|
|
* @return self |
707
|
|
|
*/ |
708
|
|
|
public function requireDeleteMethod() |
709
|
|
|
{ |
710
|
|
|
if ('GET' === $_SERVER['REQUEST_METHOD']) { |
711
|
|
|
throw new app_Exception('Method not allowed'); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
return $this; |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Custom fields |
720
|
|
|
* @return app_CtrlCustomField |
721
|
|
|
*/ |
722
|
|
|
public function CustomField($proxy = true) |
723
|
|
|
{ |
724
|
|
|
require_once APP_CTRL_PATH . 'customfield.ctrl.php'; |
725
|
|
|
return $this->App()->ControllerProxy('app_CtrlCustomField', $proxy); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Custom sections |
731
|
|
|
* @return app_CtrlCustomSection |
732
|
|
|
*/ |
733
|
|
|
public function CustomSection($proxy = true) |
734
|
|
|
{ |
735
|
|
|
require_once APP_CTRL_PATH . 'customsection.ctrl.php'; |
736
|
|
|
return $this->App()->ControllerProxy('app_CtrlCustomSection', $proxy); |
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
|