Пример #1
0
 /**
  * Performs an action
  * 
  * @param string $module Name of the action
  * @param string $method Name of the action method to run
  */
 public static function performAction($module, $method)
 {
     // Set content variable
     $content = null;
     // Set the template to be used when rendering the html (or other) output
     $template_path = CASPAR_MODULES_PATH . $module . DS . 'templates' . DS;
     // Construct the action class and method name, including any pre- action(s)
     $actionClassName = "\\application\\modules\\{$module}\\Actions";
     $actionToRunName = 'run' . ucfirst($method);
     $preActionToRunName = 'pre' . ucfirst($method);
     // Set up the response object, responsible for controlling any output
     self::getResponse()->setPage(self::getRouting()->getCurrentRouteName());
     self::getResponse()->setTemplate(mb_strtolower($method) . '.' . self::getRequest()->getRequestedFormat() . '.php');
     self::getResponse()->setupResponseContentType(self::getRequest()->getRequestedFormat());
     // Set up the action object
     $actionObject = new $actionClassName();
     // Run the specified action method set if it exists
     if (method_exists($actionObject, $actionToRunName)) {
         // Turning on output buffering
         ob_start('mb_output_handler');
         ob_implicit_flush(0);
         if (self::getRouting()->isCurrentRouteCSRFenabled()) {
             self::checkCSRFtoken(true);
         }
         if (self::$_debug_mode) {
             $time = explode(' ', microtime());
             $pretime = $time[1] + $time[0];
         }
         if ($content === null) {
             Logging::log('Running main pre-execute action');
             // Running any overridden preExecute() method defined for that module
             // or the default empty one provided by Actions
             if ($pre_action_retval = $actionObject->preExecute(self::getRequest(), $method)) {
                 $content = ob_get_clean();
                 Logging::log('preexecute method returned something, skipping further action');
                 if (self::$_debug_mode) {
                     $visited_templatename = "{$actionClassName}::preExecute()";
                 }
             }
         }
         if ($content === null) {
             $action_retval = null;
             if (self::getResponse()->getHttpStatus() == 200) {
                 // Checking for and running action-specific preExecute() function if
                 // it exists
                 if (method_exists($actionObject, $preActionToRunName)) {
                     Logging::log('Running custom pre-execute action');
                     $actionObject->{$preActionToRunName}(self::getRequest(), $method);
                 }
                 // Running main route action
                 Logging::log('Running route action ' . $actionToRunName . '()');
                 if (self::$_debug_mode) {
                     $time = explode(' ', microtime());
                     $action_pretime = $time[1] + $time[0];
                 }
                 $action_retval = $actionObject->{$actionToRunName}(self::getRequest());
                 if (self::$_debug_mode) {
                     $time = explode(' ', microtime());
                     $action_posttime = $time[1] + $time[0];
                     self::visitPartial("{$actionClassName}::{$actionToRunName}", $action_posttime - $action_pretime);
                 }
             }
             if (self::getResponse()->getHttpStatus() == 200 && $action_retval) {
                 // If the action returns *any* output, we're done, and collect the
                 // output to a variable to be outputted in context later
                 $content = ob_get_clean();
                 Logging::log('...done');
             } elseif (!$action_retval) {
                 // If the action doesn't return any output (which it usually doesn't)
                 // we continue on to rendering the template file for that specific action
                 Logging::log('...done');
                 Logging::log('Displaying template');
                 // Check to see if we have a translated version of the template
                 if (!self::getI18n() instanceof I18n || ($templateName = self::getI18n()->hasTranslatedTemplate(self::getResponse()->getTemplate())) === false) {
                     // Check to see if the template has been changed, and whether it's in a
                     // different module, specified by "module/templatename"
                     if (mb_strpos(self::getResponse()->getTemplate(), '/')) {
                         $newPath = explode('/', self::getResponse()->getTemplate());
                         $templateName = CASPAR_MODULES_PATH . $newPath[0] . DS . 'templates' . DS . $newPath[1] . '.' . self::getRequest()->getRequestedFormat() . '.php';
                     } else {
                         $templateName = $template_path . self::getResponse()->getTemplate();
                     }
                 }
                 // Check to see if the template exists and throw an \Exception otherwise
                 if (!file_exists($templateName)) {
                     Logging::log('The template file for the ' . $method . ' action ("' . self::getResponse()->getTemplate() . '") does not exist', 'core', Logging::LEVEL_FATAL);
                     throw new TemplateNotFoundException('The template file for the ' . $method . ' action ("' . self::getResponse()->getTemplate() . '") does not exist');
                 }
                 self::loadLibrary('common');
                 // Present template for current action
                 Components::presentTemplate($templateName, $actionObject->getParameterHolder());
                 $content = ob_get_clean();
                 Logging::log('...completed');
             }
         } elseif (self::$_debug_mode) {
             $time = explode(' ', microtime());
             $posttime = $time[1] + $time[0];
             self::visitPartial($visited_templatename, $posttime - $pretime);
         }
         if (!isset($csp_response)) {
             /**
              * @global Request The request object
              */
             $csp_request = self::getRequest();
             /**
              * @global User The user object
              */
             $csp_user = self::getUser();
             /**
              * @global Response The response object
              */
             $csp_response = self::getResponse();
             /**
              * @global Routing The routing object
              */
             $csp_routing = self::getRouting();
             // Load the "ui" library, since this is used a lot
             self::loadLibrary('ui');
         }
         self::loadLibrary('common');
         Logging::log('rendering content');
         if (self::isMaintenanceModeEnabled() && !mb_strstr(self::getRouting()->getCurrentRouteName(), 'configure')) {
             if (!file_exists(CASPAR_APPLICATION_PATH . 'templates/offline.inc.php')) {
                 throw new TemplateNotFoundException('Can not find offline mode template');
             }
             ob_start('mb_output_handler');
             ob_implicit_flush(0);
             require CASPAR_APPLICATION_PATH . 'templates/offline.inc.php';
             $content = ob_get_clean();
         }
         // Render output in correct order
         self::getResponse()->renderHeaders();
         if (self::getResponse()->getDecoration() == Response::DECORATE_DEFAULT) {
             require \CASPAR_APPLICATION_PATH . 'templates/layout.php';
         } else {
             // Render header template if any, and store the output in a variable
             if (!self::getRequest()->isAjaxCall() && self::getResponse()->doDecorateHeader()) {
                 Logging::log('decorating with header');
                 if (!file_exists(self::getResponse()->getHeaderDecoration())) {
                     throw new TemplateNotFoundException('Can not find header decoration: ' . self::getResponse()->getHeaderDecoration());
                 }
                 require self::getResponse()->getHeaderDecoration();
             }
             echo $content;
             Logging::log('...done (rendering content)');
             // Render footer template if any
             if (!self::getRequest()->isAjaxCall() && self::getResponse()->doDecorateFooter()) {
                 Logging::log('decorating with footer');
                 if (!file_exists(self::getResponse()->getFooterDecoration())) {
                     throw new TemplateNotFoundException('Can not find footer decoration: ' . self::getResponse()->getFooterDecoration());
                 }
                 require self::getResponse()->getFooterDecoration();
             }
             Logging::log('...done');
         }
         if (self::isDebugMode()) {
             self::getI18n()->addMissingStringsToStringsFile();
         }
         return true;
     } else {
         Logging::log("Cannot find the method {$actionToRunName}() in class {$actionClassName}.", 'core', Logging::LEVEL_FATAL);
         throw new ActionNotFoundException("Cannot find the method {$actionToRunName}() in class {$actionClassName}. Make sure the method exists.");
     }
 }