| Total Complexity | 89 |
| Total Lines | 690 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| Bugs | 1 | Features | 0 |
Complex classes like app_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use app_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) |
||
| 383 | } |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * @return Widget_Action |
||
| 388 | */ |
||
| 389 | protected function createActionForAddon($idx, $actionParams) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 737 | } |
||
| 738 | } |
||
| 739 |