Example #1
0
 /**
  * Runs the requested controller action
  *
  * This is THE run method for each controller in each app. When used to resolve
  * requests, no action or parameter need to be set. Both will be autodiscovered
  * by requesting the needed data from the requesthandler. The method can also be
  * used to get a partial result when using a controller from within a controller.
  * In this case you should provide the action an parameters needed to get the
  * wanted result.
  *
  * @throws ControllerException
  *
  * @return boolean bool|string
  */
 public function run()
 {
     // If accesscheck failed => stop here and return false!
     if ($this->checkControllerAccess() === false) {
         $this->app->core->logger->warning(sprintf('Missing permission for ressource %s.%s.%s', $this->app->getName(), $this->getName(), $this->action));
         throw new ControllerException($this->app->language->get('access.missing_userrights'));
         return false;
     }
     // Init return var with boolean false as default value. This default
     // prevents from running the views render() method when the controller
     // action is stopped manually by using return.
     $return = false;
     // a little bit of reflection magic to pass request param into controller func
     $return = $this->di->invokeMethod($this, $this->action, $this->params);
     // Redirect initiated from within the called action?
     if (!empty($this->redirect)) {
         // Clean post data wanted?
         if ($this->redirect[2] == true) {
             $this->app->post->clean();
         }
         // Target is an array and will be analyzed and used as app, controller and actionnames
         if (is_array($this->redirect[0])) {
             // Make sure we have all essential data before calling ACA
             $require = ['app', 'controller', 'action'];
             foreach ($require as $check) {
                 if (empty($this->redirect[0][$check])) {
                     throw new ControllerException(sprintf('Redirects by arrayed $target need a set "%s" element.', $check));
                 }
             }
             $app = $this->app->handler->getAppInstance($this->redirect[0]['app']);
             $controller = $app->getController($this->redirect[0]['controller']);
             $controller->setAction($this->redirect[0]['action']);
             $controller->setParams($this->redirect[1]);
             $return = $controller->run();
             // Prevent rendering of this controllers view because rendering results
             // are coming from the redirected ACA
             $this->render = false;
             // Reset redirection settings
             $this->redirect = [];
         } else {
             // It is important to clean the redirection property before calling the redirection action in this
             // controller. Without cleaning it an endless redirect recursion occurs.
             $this->action = $this->redirect[0];
             $this->params = $this->redirect[1];
             $this->redirect = [];
             $return = $this->run();
         }
     }
     // Do we have a result?
     if (isset($return)) {
         // Boolean false result means to stop work for controller
         if ($return == false) {
             return false;
         }
     }
     // Render the view and return the result
     if ($this->render === true) {
         // Create view instance if not alredy done
         if (!$this->view instanceof View) {
             $this->view = $this->app->getView($this->name);
         }
         $this->view->setAction($this->action);
         $this->view->setParams($this->params);
         $this->view->setVars($this->vars);
         if (isset($this->logger)) {
             $this->view->setLogger($this->logger);
         }
         // Check if there still no view object
         if (empty($this->view)) {
             throw new ControllerException(sprintf('A result has to be rendered but "%sView" does not exist.', $this->name));
         }
         // Render
         $content = $this->view->render();
         // Run possible onEmpty event of app on no render result
         if (empty($content) && method_exists($this->app, 'onEmpty')) {
             $content = $this->app->onEmpty();
         }
         return $content;
     } else {
         // Without view rendering we return the return value send from called controller action
         return $return;
     }
 }