Advertisement
Deedlit

controller.php

Jul 7th, 2021
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2.  
  3. class Controller
  4. {
  5.  
  6.     const HAS_MODEL = 1;
  7.     const HAS_VIEW = 2;
  8.     const USE_HELPERS = 3;
  9.  
  10.     protected $_defaultAction = 'index';
  11.  
  12.     public function setDefaultAction($action = 'index')
  13.     {
  14.         $this->_defaultAction = $action;
  15.     }
  16.  
  17.     public function getDefaultAction()
  18.     {
  19.         return $this->_defaultAction;
  20.     }
  21.  
  22.     protected $_model;
  23.     protected $_controller;
  24.     protected $_action;
  25.     protected $_view;
  26.     protected $_modelBaseName;
  27.     protected static $_opt;
  28.     protected $_options;
  29.     protected static $_siteDataModel = null;
  30.     protected $_helpersCatName = null;
  31.  
  32.     public function __construct($model = null, $action = null, $options = array(Controller::HAS_MODEL, Controller::HAS_VIEW, Controller::USE_HELPERS))
  33.     {
  34.         // wywołanie z bootstrap.php, by pobrać nazwę domyślnej akcji
  35.         if ($model == null && $action == null)
  36.         {
  37.             $this->setDefaultAction();
  38.             return;
  39.         }
  40.  
  41.         if (self::$_siteDataModel == null)
  42.         {
  43.             self::$_siteDataModel = Model::getInstance('SiteData'); //new SiteDataModel();
  44.         }
  45.  
  46.         $this->_options = $options;
  47.         $this->_controller = ucwords(__CLASS__);
  48.         $this->_action = $action;
  49.         $this->_modelBaseName = $model;
  50.  
  51.         if (in_array(Controller::USE_HELPERS, $options))
  52.             $this->_helpersCatName = $model . '-' . $action;
  53.  
  54.         if (in_array(Controller::HAS_MODEL, $options))
  55.             $this->_setModel($model);
  56.  
  57.         if (in_array(Controller::HAS_VIEW, $options))
  58.         {
  59.             $this->_view = new View(HOME . DS . 'views' . DS . strtolower($this->_modelBaseName) . DS . $action . '.php');
  60.             $this->_view->set('app_model', strtolower($this->_modelBaseName));
  61.             $this->_view->set('app_action', strtolower($this->_action));
  62.         }
  63.     }
  64.  
  65.     public static function setOpt($opt)
  66.     {
  67.         self::$_opt = $opt;
  68.     }
  69.  
  70.     public static function getOpt()
  71.     {
  72.         return self::$_opt;
  73.     }
  74.  
  75.     protected function initHelpers($opt)
  76.     {
  77.         if ($this->_helpersCatName != null)
  78.         {
  79. //            $n = $this->_helpersCatName.($opt != null ? '-'.$opt : '');
  80. //            $this->_helpersCatName = $n;
  81.             return Helpers::init($this->_helpersCatName);
  82.         }
  83.     }
  84.  
  85.     protected function _setModel($modelName)
  86.     {
  87. //      $modelName .= 'Model';
  88.         $this->_model = Model::getInstance($modelName);//new $modelName();
  89.     }
  90.  
  91.     protected function _setView($viewName)
  92.     {
  93.         $this->_view = new View(HOME . DS . 'views' . DS . strtolower($this->_modelBaseName) . DS . $viewName . '.php');
  94.     }
  95.  
  96.     public static final function getSiteDataModel()
  97.     {
  98.         return self::$_siteDataModel;
  99.     }
  100.  
  101.     public static final function getDefaultUserRoute()
  102.     {
  103.         return MSession::getUserDefaultRoute();
  104.     }
  105.  
  106.     public static final function isLoggedIn()
  107.     {
  108.         $logged = MSession::getUser();
  109.         if ($logged !== NULL && ($logged->canLogin() || $logged->canLoinToForm()) && $logged->getIsOnline() != 0)
  110.         {
  111.             if (defined('_AJAX_APP') && _AJAX_APP)
  112.                 return true;
  113.             $logged->_updateInternalVars(false, false);
  114.             //MSession::set("logged_user", new privs($logged->getUserLogin()));
  115.             return true;
  116.         }
  117.         return false;
  118.     }
  119.  
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement