Example #1
0
 /**
  * Main server procedure. index.php in the system's root calls this method by default.
  */
 public function run()
 {
     $db = Config::main()->getDbo();
     // Prepare necessities.
     $this->user = new ModelUser($db);
     if ($this->user->isLoggedIn()) {
         $this->user->restoreSession()->refreshSession();
     }
     Cache::init($db);
     Profiler::startSection('DiamondMVC');
     Profiler::startSection('Initiating i18n');
     logMsg('DiamondMVC: using compressed language file: ' . (Config::main()->get('DEBUG_MODE') ? 'yes' : 'no'), 1, false);
     i18n::init(Config::main()->get('DEBUG_MODE'));
     Profiler::endSection();
     // Load plugins
     Profiler::startSection('Load plugins');
     $this->plugins = Plugin::getPlugins();
     Profiler::endSection();
     Profiler::startSection('Load permissions');
     Permissions::init($db);
     Profiler::endSection();
     // Register field classes
     Profiler::startSection('Register fields');
     FormBuilder::registerFields();
     Profiler::endSection();
     $evt = new SystemBeforeRouteEvent();
     $this->trigger($evt);
     if (!$evt->isDefaultPrevented()) {
         Profiler::startSection('Routing');
         // Route requested controller
         if (isset($_REQUEST['control']) and !empty($_REQUEST['control'])) {
             $control = 'Controller' . str_replace('-', '_', $_REQUEST['control']);
             if (!class_exists($control)) {
                 $control = Config::main()->get('DEFAULT_CONTROLLER');
             }
         } else {
             $control = Config::main()->get('DEFAULT_CONTROLLER');
         }
         logMsg("DiamondMVC: routed controller to " . $control, 1, false);
         // Detect requested action, by default "main"
         if (isset($_REQUEST['action'])) {
             $action = $_REQUEST['action'];
         } else {
             $action = 'main';
         }
         $action = preg_replace('/[^A-z_]+/', '_', $action);
         logMsg("DiamondMVC: action is " . $action, 1, false);
         // Detect type of action. Currently possible: HTML, AJAX, JSON
         if (isset($_REQUEST['type'])) {
             $type = strToLower($_REQUEST['type']);
         } else {
             $type = 'html';
         }
         logMsg("DiamondMVC: request type is " . $type, 1, false);
         // Detect requested template. Ignored in JSON requests
         if (isset($_REQUEST['tpl'])) {
             $_REQUEST['tpl'] = trim($_REQUEST['tpl']);
             if (!empty($_REQUEST['tpl'])) {
                 $tpl = $_REQUEST['tpl'];
             } else {
                 $tpl = 'default';
             }
         } else {
             $tpl = 'default';
         }
         if (!file_exists(jailpath(DIAMONDMVC_ROOT . '/templates', $tpl))) {
             $tpl = 'default';
         }
         logMsg("DiamondMVC: jailed template is " . $tpl, 1, false);
         // Detect requested view
         if (isset($_REQUEST['view'])) {
             $view = $_REQUEST['view'];
         } else {
             $view = '';
         }
         logMsg("DiamondMVC: view is " . $view, 1, false);
         // The controller is the heart of the MVC system.
         logMsg("DiamondMVC: constructing controller", 1, false);
         $controller = new $control();
         // Before actually performing the action, we'll give plugins the chance to change the controller, action, view and template.
         logMsg("DiamondMVC: triggering pre-action event", 1, false);
         $this->controller = $controller;
         $this->action = $action;
         $this->view = $view;
         $this->tpl = $tpl;
         $evt = new SystemActionEvent($controller, $action, $view, $tpl);
         $this->trigger($evt);
         $contrller = $this->controller = $evt->controller;
         $action = $this->action = $evt->action;
         $view = $this->view = $evt->view;
         $tpl = $this->tpl = $evt->template;
         Profiler::endSection();
         logMsg("DiamondMVC: new controller is: Controller" . $controller->getName() . '; new action is: ' . $action . '; new view is: ' . $view . '; new type is: ' . $type, 1, false);
         Profiler::startSection('Perform action');
         $controller->action($action);
         Profiler::endSection();
         Profiler::startSection('Output');
         switch ($type) {
             // Not specially treated view type
             default:
                 // Does the view type exist in the controller?
                 if ($controller->hasView($view, $type)) {
                     logMsg("DiamondMVC: typed view {$view} ({$type}) found", 1, false);
                     $controller->getView($view, $type)->render();
                     break;
                 } else {
                     logMsg("DiamondMVC: no specific typed view found, defaulting to HTML view", 1, false);
                 }
                 // The entire website is built and sent to the client.
             // The entire website is built and sent to the client.
             case 'html':
                 logMsg("DiamondMVC: rendering HTML view", 1, false);
                 (new Template($controller, $tpl))->title(Config::main()->get('WEBSITE_TITLE'))->render($view);
                 break;
                 // TODO: AJAX request differ from JSON requests in that they make use of the client side engine to update particular parts of the web page (content, header, etc.) and request missing css and js.
             // TODO: AJAX request differ from JSON requests in that they make use of the client side engine to update particular parts of the web page (content, header, etc.) and request missing css and js.
             case 'ajax':
                 logMsg("DiamondMVC: AJAX view still needs implementation!", 1, false);
                 break;
                 // Send JSON formatted raw data. By default the result of the controller is simply encoded, but a specialized JSON view can manipulate and format data before sending it out. This is
                 // useful to remove circular references and PHP objects.
             // Send JSON formatted raw data. By default the result of the controller is simply encoded, but a specialized JSON view can manipulate and format data before sending it out. This is
             // useful to remove circular references and PHP objects.
             case 'json':
                 logMsg('DiamondMVC: hasView - ' . ($controller->hasView($view, 'json') ? 'yes' : 'no') . ' | hasTemplate - ' . ($controller->hasTemplate($view, 'json') ? 'yes' : 'no'), 1, false);
                 if ($controller->hasView($view, 'json') or $controller->hasTemplate($view, 'json')) {
                     logMsg("DiamondMVC: JSON view for " . (empty($view) ? 'default' : $view) . " found", 1, false);
                     $controller->getView($view, 'json')->read()->render();
                 } else {
                     logMsg("DiamondMVC: using generic JSON stringification on controller result", 1, false);
                     echo json_encode($controller->getResult());
                 }
                 break;
         }
         Profiler::endSection();
     } else {
         logMsg('DiamondMVC: Skipping routing', 1, false);
     }
     Profiler::endSection();
 }