Advertisement
NFL

Untitled

NFL
Dec 9th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.63 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author S.Serdyuk
  5.  * @copyright (c) 2013, WebAkula
  6.  *
  7.  */
  8. class Core_Controller_Action extends Core_Controller {
  9.  
  10.     const ROLE_GUEST = 2; //Хард
  11.  
  12.     //
  13.     public function dispatch($action) {
  14.         // exit(var_dump($action));
  15.         $time = microtime(true);
  16.         $this->_helper->notifyPreDispatch();
  17.  
  18.         $this->preDispatch();
  19.         $this->init();
  20.         if ($this->getRequest()->isDispatched()) {
  21.             if (null === $this->_classMethods) {
  22.                 $this->_classMethods = get_class_methods($this);
  23.             }
  24.             if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
  25.                 if ($this->getInvokeArg('useCaseSensitiveActions')) {
  26.                     trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
  27.                 }
  28.                 if (in_array($action, $this->_classMethods)) {
  29.                     $this->_callAction($action);
  30.                 }
  31.             } else {
  32.                 $class = get_class($this);
  33.                 //var_dump($class);
  34.                 $parts = explode('_', $class);
  35.                 if (count($parts) === 1) {
  36.                     $module = 'default';
  37.                     $controller = mb_strtolower(str_ireplace('Controller', '', $parts[0]));
  38.                 } else {
  39.                     $modle = mb_strtolower($parts[0]);
  40.                     $controller = mb_strtolower(str_ireplace('Controller', '', $parts[1]));
  41.                 }
  42.                 $path = APPLICATION_PATH
  43.                         . DIRECTORY_SEPARATOR . 'custom'
  44.                         . DIRECTORY_SEPARATOR . $module
  45.                         . DIRECTORY_SEPARATOR . 'controllers'
  46.                         . DIRECTORY_SEPARATOR . $controller
  47.                         . DIRECTORY_SEPARATOR . $action . '.php';
  48.  
  49.                 if (file_exists($path)) {
  50.                     include_once($path);
  51.                     $class = new $action;
  52.                     call_user_func_array([$class, $action], []);
  53.                 } else {
  54.                     $this->__call($action, array());
  55.                 }
  56.             }
  57.             $this->postDispatch();
  58.         }
  59.  
  60.         $this->_helper->notifyPostDispatch();
  61.         //var_dump(microtime(true) - $time);
  62.     }
  63.  
  64.     protected function _callAction($action) {
  65.         $reflection = Zend_Server_Reflection::reflectClass($this);
  66.         $methods = $reflection->getMethods();
  67.         $mtd = null;
  68.         foreach ($methods as $m) {
  69.             if ($m->getName() == $action)
  70.                 $mtd = $m;
  71.         }
  72.         if (!$mtd)
  73.             throw new RuntimeException('Method "' . $action . '" not found');
  74.         //exit(var_dump($action));
  75.         $protos = $mtd->getPrototypes();
  76.         $basicTypes = array('int', 'float', 'string', 'bool');
  77.         foreach ($protos as $proto) {
  78.             $parameters = array();
  79.             $args = $proto->getParameters();
  80.  
  81.             foreach ($args as $arg) {
  82.                 $name = $arg->getName();
  83.                 $param = $this->getRequest()->getParam($name, null);
  84.                 $type = $arg->getType();
  85.  
  86.                 if ($arg->isOptional() && $param === null) {
  87.  
  88.                     $param = $arg->getDefaultValue();
  89.                 } elseif ($param === null) {
  90.                     throw new RuntimeException("Parameter '$name' does not exist");
  91.                 }
  92.  
  93.                 if (in_array($type, $basicTypes)) {
  94.                     settype($param, $type);
  95.                 }
  96.  
  97.                 $parameters[] = $param;
  98.             }
  99.         }
  100.         call_user_func_array(array($this, $action), $parameters);
  101.     }
  102.  
  103.     public function preDispatch() {
  104.  
  105.  
  106.         $auth = Zend_Auth::getInstance();
  107.         $auth->setStorage(new Zend_Auth_Storage_Session('UserArea'));
  108.         $this->auth = $auth;
  109.         $this->view->auth = $auth;
  110.         $moduleName = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
  111.         $this->view->addScriptPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $moduleName);
  112.         $this->view->addHelperPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'viewhelpers' . DIRECTORY_SEPARATOR . $moduleName, ucfirst($moduleName) . '_View_Helper_');
  113.         //exit('foo');
  114.         $session = Zend_Session::getId();
  115.         $request = new Zend_Controller_Request_Http();
  116.         if (APP_MULTILINGUAL) {
  117.             $session = new Zend_Session_Namespace('Language');
  118.             $this->_languageId = isset($session->current) ? $session->current : DEFAULT_LANGUAGE;
  119.  
  120.             $model = new Core_Model_Languages();
  121.             $this->_languageId = $model->getLanguageId($this->_languageId); //exit($this->_languageId);
  122.         }
  123.  
  124.  
  125. //        $role = $this->auth->hasIdentity() ? $this->auth->getIdentity()->role_id : self::ROLE_GUEST;
  126. //
  127. //        $request = Zend_Controller_Front::getInstance()->getRequest();
  128. //        $module = $request->getModuleName();
  129. //        $controller = $request->getControllerName();
  130. //        $action = $request->getActionName();
  131. //        Application_Plugin_AccessCheck::isAllowed($role, $module, $controller, $action);
  132. //        //exit(var_dump($role, $module, $controller, $action, Application_Plugin_AccessCheck::isAllowed($role, $module, $controller, $action)));
  133. //        if (!Application_Plugin_AccessCheck::isAllowed($role, $module, $controller, $action) && $module != 'default') {
  134. //            $this->_redirect('/');
  135. //        }
  136. //parent::preDispatch();
  137.     }
  138.  
  139.     public function __get($name) {
  140.         //if()
  141.         exit(var_dump(get_object_vars($this)));
  142.         switch ($name) {
  143.             case 'auth':
  144.                 exit('foo');
  145.                 return $this->auth->getIdentity()->{$name};
  146.                 break;
  147.             default:
  148.                 throw new Exception("Invalid type '$name' provided for __get() :" . __METHOD__);
  149.                 break;
  150.         }
  151.     }
  152.  
  153.     public function init() {
  154.  
  155.         parent::init();
  156.  
  157.  
  158.  
  159.         //$this->_breadcrumbs->setHomeTitle('Интернет-маркет');
  160.  
  161.         $host = $_SERVER['HTTP_HOST'];
  162.  
  163.         $domains = new Sloboda_Model_Domains;
  164.         $domain = $domains->getDomainInfo();
  165.         //exit(var_dump($domain));
  166.  
  167.         $session = $this->getTmpTradeType();
  168.         $currTradeTypeWhole = ($session->current == SLOBODA_TRADETYPE_WHOLESALE);
  169.         $tradeType = null;
  170.         $auth = Zend_Auth::getInstance();
  171.         $auth->setStorage(new Zend_Auth_Storage_Session('UserArea'));
  172.         $this->auth = $auth;
  173.         if ($currTradeTypeWhole) {
  174.             if ($auth->hasIdentity()) {
  175.                 $tradeType = $auth->getIdentity()->TradeType;
  176.             } else {
  177.                 $tradeType = SLOBODA_TRADETYPE_RETAIL;
  178.             }
  179.         }
  180.         if ($currTradeTypeWhole && $tradeType == SLOBODA_TRADETYPE_RETAIL) {
  181.             if (!$this->_isUrlAllowed()) {
  182.                 $this->_redirect('/users/auth/auth');
  183.             }
  184.         }
  185.  
  186.         if (Zend_Auth::getInstance()->hasIdentity()) {
  187.             $model = new Sloboda_Model_Regions;
  188.  
  189.             $regions = $model->getFirstLevel($this->_lang);
  190.             //Zend_Debug::dump($this->_lang);
  191.             $this->view->regions = $regions;
  192.  
  193.             $this->domain = $domain;
  194.             $this->domainId = $domain['id'];
  195.             $this->view->domain = $domain;
  196.  
  197.             $session = new Zend_Session_Namespace('Intro');
  198.             $session->domainInfo = $domain;
  199.             $this->view->layout()->tradeType = $auth->getIdentity()->TradeType;
  200.         } else {
  201.             if ($domain['rozn_enabled']) {
  202.  
  203.                 $model = new Sloboda_Model_Regions;
  204.  
  205.                 $regions = $model->getFirstLevel($this->_lang);
  206.                 //Zend_Debug::dump($this->_lang);
  207.                 $this->view->regions = $regions;
  208.  
  209.                 $this->domain = $domain;
  210.                 $this->domainId = $domain['id'];
  211.                 $this->view->domain = $domain;
  212.  
  213.                 $session = new Zend_Session_Namespace('Intro');
  214.                 $session->domainInfo = $domain;
  215.                 $this->view->layout()->tradeType = SLOBODA_TRADETYPE_RETAIL;
  216.             } else {
  217.                 Zend_Auth::getInstance()->clearIdentity();
  218.                 //Zend_Session::destroy();
  219.                 $default = $domains->getDefault();
  220.                 if (!$this->_isUrlAllowed()) {
  221.                     $this->_redirect('/users/auth/auth');
  222.                 }
  223.             }
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Точка входа для управления настройками модуля
  229.      */
  230.     public function settings() {
  231.        
  232.     }
  233.  
  234.     public function meta() {
  235.        
  236.     }
  237.  
  238.     public function getTmpTradeType() {
  239.         $session = new Zend_Session_Namespace($this->_tmpTradetype);
  240.         return $session;
  241.     }
  242.  
  243.     protected function _isUrlAllowed() {
  244.         foreach ($this->_allowedUrls as $v) {
  245.             if (strpos($_SERVER['REQUEST_URI'], $v) !== false) {
  246.                 return true;
  247.             }
  248.         }
  249.         return false;
  250.     }
  251.  
  252.     protected function _setDefaultMeta(array $optional = array()) {
  253.  
  254.         $module = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
  255.         $model = new Admin_Model_Core_Admin_Modules();
  256.         $module = $model->get($module);
  257.  
  258.         $meta = new Core_Model_Meta;
  259.         $module = $meta->getModuleDefaults($module['id'], $this->_languageId);
  260.  
  261.         $this->view->headMeta()->appendName('keywords', $module['meta_keywords'])->appendName('description', $module['meta_description']);
  262.         $this->view->title($module['client_title']);
  263.     }
  264.  
  265.     public function setTitle($title) {
  266.        
  267.     }
  268.  
  269.     public function setKeywords() {
  270.        
  271.     }
  272.  
  273.     public function setDescription() {
  274.        
  275.     }
  276.  
  277.     public function setTemplatedModuleMeta(array $data) {
  278.         Zend_Debug::dump($data);
  279.     }
  280.  
  281.     public function setMeta(array $data, array $titleParts = array()) {
  282. //exit;
  283. //Zend_Debug::dump($data, 'setMeta');
  284. //exit(var_dump((bool)mb_strlen($data['meta_title'])));//
  285.  
  286.         $view = Zend_Layout::getMvcInstance()->getView();
  287.         //exit(Zend_Debug::dump($data));;
  288.         if (count($titleParts)) {
  289.  
  290.             if (mb_strlen($data['meta_title']) > 0) {
  291.                 $view->title($data['meta_title'], $titleParts);
  292.             } else {
  293.                 $view->title($data['title'], $titleParts);
  294.             }
  295.         } else {
  296.             if (mb_strlen($data['meta_title']) > 0) {
  297.                 $view->headTitle($data['meta_title']);
  298.             } else {
  299.                 $view->headTitle($data['title']);
  300.             }
  301.         }
  302.         unset($data['title'], $data['meta_title']);
  303.  
  304.         foreach ($data as $k => $v) {
  305.             if (strpos($k, 'meta_') !== false) {
  306.                 $view->headMeta()->appendName(str_replace('meta_', '', $k), $v);
  307.             }
  308.         }
  309.     }
  310.  
  311.     public function __set($name, $value) {
  312.         switch ($name) {
  313.             case 'route':
  314.                 $this->view->route = $value;
  315. //Установка роута для урл хелпера мультиязычника                
  316.                 break;
  317.         }
  318.     }
  319.  
  320.     public function setRoute(array $data) {
  321.        
  322.     }
  323.  
  324.     /**
  325.      *
  326.      * @param type $module - имя модуля (news, catalog, etc)
  327.      * @param type $route - имя роута (по умолчанию default)
  328.      */
  329.     public function defaultLangSelector($module = null, $route = 'default') {
  330.         if (APP_MULTILINGUAL) {
  331. //Генерируем список URL для переключателя языков
  332.             if (is_null($module)) {
  333.                 $module = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
  334.             }
  335.             $model = new Core_Model_Languages();
  336.             $tr = $model->getList();
  337.             foreach ($tr as $i => $t) {
  338.                 $this->view->langSelector()->buildFromArray(array('module' => $module), $i, $route);
  339.             }
  340.         }
  341.     }
  342.  
  343.     public function setIndexMeta($data = null) {
  344.         $view = Zend_Layout::getMvcInstance()->getView();
  345.         if (is_null($data)) {
  346.             $model = new Core_Model_Seo;
  347.             $data = $model->getIndexMeta();
  348.         }
  349.         if (mb_strlen($data['meta_title']) > 0) {
  350.             $view->headTitle($data['meta_title']);
  351.         } else {
  352.             $view->headTitle($data['title']);
  353.         }
  354.         unset($data['title'], $data['meta_title']);
  355.         foreach ($data as $k => $v) {
  356.             if (strpos($k, 'meta_') !== false) {
  357.                 $view->headMeta()->appendName(str_replace('meta_', '', $k), $v);
  358.             }
  359.         }
  360.     }
  361.  
  362.     public function disableLayout() {
  363.         $this->_helper->layout()->disableLayout();
  364.     }
  365.  
  366.     public function postDispatch() {
  367.         if ($this->_helper->layout()->getLayout() != 'index-intro') {
  368.             $this->view->headScript()->appendFile('/js/sloboda/regions-selector.js');
  369.         }
  370.         parent::postDispatch();
  371.     }
  372.  
  373.     public function err($code = 404) {
  374.         //Zend_Controller_Front::getRequest()->setModuleName('default')->setControllerName('error')->setActionName('error');
  375.         http_response_code($code);
  376.         $this->_forward('error', 'error', 'default');
  377.     }
  378.  
  379.     public function getTradetype() {
  380.         return $this->auth->hasIdentity() ? $this->auth->getIdentity()->TradeType : SLOBODA_TRADETYPE_RETAIL;
  381.     }
  382.  
  383.     public function getRegion() {
  384.         return $this->auth->hasIdentity() ? $this->auth->getIdentity()->IDRegion : $this->domain['region'];
  385.     }
  386.  
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement