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