Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author S.Serdyuk
- * @copyright (c) 2013, WebAkula
- *
- */
- class Core_Controller_Action extends Core_Controller {
- const ROLE_GUEST = 2; //Хард
- //
- public function dispatch($action) {
- // exit(var_dump($action));
- $time = microtime(true);
- $this->_helper->notifyPreDispatch();
- $this->preDispatch();
- $this->init();
- if ($this->getRequest()->isDispatched()) {
- if (null === $this->_classMethods) {
- $this->_classMethods = get_class_methods($this);
- }
- if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
- if ($this->getInvokeArg('useCaseSensitiveActions')) {
- trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
- }
- if (in_array($action, $this->_classMethods)) {
- $this->_callAction($action);
- }
- } else {
- $class = get_class($this);
- //var_dump($class);
- $parts = explode('_', $class);
- if (count($parts) === 1) {
- $module = 'default';
- $controller = mb_strtolower(str_ireplace('Controller', '', $parts[0]));
- } else {
- $modle = mb_strtolower($parts[0]);
- $controller = mb_strtolower(str_ireplace('Controller', '', $parts[1]));
- }
- $path = APPLICATION_PATH
- . DIRECTORY_SEPARATOR . 'custom'
- . DIRECTORY_SEPARATOR . $module
- . DIRECTORY_SEPARATOR . 'controllers'
- . DIRECTORY_SEPARATOR . $controller
- . DIRECTORY_SEPARATOR . $action . '.php';
- if (file_exists($path)) {
- include_once($path);
- $class = new $action;
- call_user_func_array([$class, $action], []);
- } else {
- $this->__call($action, array());
- }
- }
- $this->postDispatch();
- }
- $this->_helper->notifyPostDispatch();
- //var_dump(microtime(true) - $time);
- }
- protected function _callAction($action) {
- $reflection = Zend_Server_Reflection::reflectClass($this);
- $methods = $reflection->getMethods();
- $mtd = null;
- foreach ($methods as $m) {
- if ($m->getName() == $action)
- $mtd = $m;
- }
- if (!$mtd)
- throw new RuntimeException('Method "' . $action . '" not found');
- //exit(var_dump($action));
- $protos = $mtd->getPrototypes();
- $basicTypes = array('int', 'float', 'string', 'bool');
- foreach ($protos as $proto) {
- $parameters = array();
- $args = $proto->getParameters();
- foreach ($args as $arg) {
- $name = $arg->getName();
- $param = $this->getRequest()->getParam($name, null);
- $type = $arg->getType();
- if ($arg->isOptional() && $param === null) {
- $param = $arg->getDefaultValue();
- } elseif ($param === null) {
- throw new RuntimeException("Parameter '$name' does not exist");
- }
- if (in_array($type, $basicTypes)) {
- settype($param, $type);
- }
- $parameters[] = $param;
- }
- }
- call_user_func_array(array($this, $action), $parameters);
- }
- public function preDispatch() {
- $auth = Zend_Auth::getInstance();
- $auth->setStorage(new Zend_Auth_Storage_Session('UserArea'));
- $this->auth = $auth;
- $this->view->auth = $auth;
- $moduleName = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
- $this->view->addScriptPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $moduleName);
- $this->view->addHelperPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'viewhelpers' . DIRECTORY_SEPARATOR . $moduleName, ucfirst($moduleName) . '_View_Helper_');
- //exit('foo');
- $session = Zend_Session::getId();
- $request = new Zend_Controller_Request_Http();
- if (APP_MULTILINGUAL) {
- $session = new Zend_Session_Namespace('Language');
- $this->_languageId = isset($session->current) ? $session->current : DEFAULT_LANGUAGE;
- $model = new Core_Model_Languages();
- $this->_languageId = $model->getLanguageId($this->_languageId); //exit($this->_languageId);
- }
- // $role = $this->auth->hasIdentity() ? $this->auth->getIdentity()->role_id : self::ROLE_GUEST;
- //
- // $request = Zend_Controller_Front::getInstance()->getRequest();
- // $module = $request->getModuleName();
- // $controller = $request->getControllerName();
- // $action = $request->getActionName();
- // Application_Plugin_AccessCheck::isAllowed($role, $module, $controller, $action);
- // //exit(var_dump($role, $module, $controller, $action, Application_Plugin_AccessCheck::isAllowed($role, $module, $controller, $action)));
- // if (!Application_Plugin_AccessCheck::isAllowed($role, $module, $controller, $action) && $module != 'default') {
- // $this->_redirect('/');
- // }
- //parent::preDispatch();
- }
- public function __get($name) {
- //if()
- exit(var_dump(get_object_vars($this)));
- switch ($name) {
- case 'auth':
- exit('foo');
- return $this->auth->getIdentity()->{$name};
- break;
- default:
- throw new Exception("Invalid type '$name' provided for __get() :" . __METHOD__);
- break;
- }
- }
- public function init() {
- parent::init();
- //$this->_breadcrumbs->setHomeTitle('Интернет-маркет');
- $host = $_SERVER['HTTP_HOST'];
- $domains = new Sloboda_Model_Domains;
- $domain = $domains->getDomainInfo();
- //exit(var_dump($domain));
- $session = $this->getTmpTradeType();
- $currTradeTypeWhole = ($session->current == SLOBODA_TRADETYPE_WHOLESALE);
- $tradeType = null;
- $auth = Zend_Auth::getInstance();
- $auth->setStorage(new Zend_Auth_Storage_Session('UserArea'));
- $this->auth = $auth;
- if ($currTradeTypeWhole) {
- if ($auth->hasIdentity()) {
- $tradeType = $auth->getIdentity()->TradeType;
- } else {
- $tradeType = SLOBODA_TRADETYPE_RETAIL;
- }
- }
- if ($currTradeTypeWhole && $tradeType == SLOBODA_TRADETYPE_RETAIL) {
- if (!$this->_isUrlAllowed()) {
- $this->_redirect('/users/auth/auth');
- }
- }
- if (Zend_Auth::getInstance()->hasIdentity()) {
- $model = new Sloboda_Model_Regions;
- $regions = $model->getFirstLevel($this->_lang);
- //Zend_Debug::dump($this->_lang);
- $this->view->regions = $regions;
- $this->domain = $domain;
- $this->domainId = $domain['id'];
- $this->view->domain = $domain;
- $session = new Zend_Session_Namespace('Intro');
- $session->domainInfo = $domain;
- $this->view->layout()->tradeType = $auth->getIdentity()->TradeType;
- } else {
- if ($domain['rozn_enabled']) {
- $model = new Sloboda_Model_Regions;
- $regions = $model->getFirstLevel($this->_lang);
- //Zend_Debug::dump($this->_lang);
- $this->view->regions = $regions;
- $this->domain = $domain;
- $this->domainId = $domain['id'];
- $this->view->domain = $domain;
- $session = new Zend_Session_Namespace('Intro');
- $session->domainInfo = $domain;
- $this->view->layout()->tradeType = SLOBODA_TRADETYPE_RETAIL;
- } else {
- Zend_Auth::getInstance()->clearIdentity();
- //Zend_Session::destroy();
- $default = $domains->getDefault();
- if (!$this->_isUrlAllowed()) {
- $this->_redirect('/users/auth/auth');
- }
- }
- }
- }
- /**
- * Точка входа для управления настройками модуля
- */
- public function settings() {
- }
- public function meta() {
- }
- public function getTmpTradeType() {
- $session = new Zend_Session_Namespace($this->_tmpTradetype);
- return $session;
- }
- protected function _isUrlAllowed() {
- foreach ($this->_allowedUrls as $v) {
- if (strpos($_SERVER['REQUEST_URI'], $v) !== false) {
- return true;
- }
- }
- return false;
- }
- protected function _setDefaultMeta(array $optional = array()) {
- $module = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
- $model = new Admin_Model_Core_Admin_Modules();
- $module = $model->get($module);
- $meta = new Core_Model_Meta;
- $module = $meta->getModuleDefaults($module['id'], $this->_languageId);
- $this->view->headMeta()->appendName('keywords', $module['meta_keywords'])->appendName('description', $module['meta_description']);
- $this->view->title($module['client_title']);
- }
- public function setTitle($title) {
- }
- public function setKeywords() {
- }
- public function setDescription() {
- }
- public function setTemplatedModuleMeta(array $data) {
- Zend_Debug::dump($data);
- }
- public function setMeta(array $data, array $titleParts = array()) {
- //exit;
- //Zend_Debug::dump($data, 'setMeta');
- //exit(var_dump((bool)mb_strlen($data['meta_title'])));//
- $view = Zend_Layout::getMvcInstance()->getView();
- //exit(Zend_Debug::dump($data));;
- if (count($titleParts)) {
- if (mb_strlen($data['meta_title']) > 0) {
- $view->title($data['meta_title'], $titleParts);
- } else {
- $view->title($data['title'], $titleParts);
- }
- } else {
- if (mb_strlen($data['meta_title']) > 0) {
- $view->headTitle($data['meta_title']);
- } else {
- $view->headTitle($data['title']);
- }
- }
- unset($data['title'], $data['meta_title']);
- foreach ($data as $k => $v) {
- if (strpos($k, 'meta_') !== false) {
- $view->headMeta()->appendName(str_replace('meta_', '', $k), $v);
- }
- }
- }
- public function __set($name, $value) {
- switch ($name) {
- case 'route':
- $this->view->route = $value;
- //Установка роута для урл хелпера мультиязычника
- break;
- }
- }
- public function setRoute(array $data) {
- }
- /**
- *
- * @param type $module - имя модуля (news, catalog, etc)
- * @param type $route - имя роута (по умолчанию default)
- */
- public function defaultLangSelector($module = null, $route = 'default') {
- if (APP_MULTILINGUAL) {
- //Генерируем список URL для переключателя языков
- if (is_null($module)) {
- $module = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
- }
- $model = new Core_Model_Languages();
- $tr = $model->getList();
- foreach ($tr as $i => $t) {
- $this->view->langSelector()->buildFromArray(array('module' => $module), $i, $route);
- }
- }
- }
- public function setIndexMeta($data = null) {
- $view = Zend_Layout::getMvcInstance()->getView();
- if (is_null($data)) {
- $model = new Core_Model_Seo;
- $data = $model->getIndexMeta();
- }
- if (mb_strlen($data['meta_title']) > 0) {
- $view->headTitle($data['meta_title']);
- } else {
- $view->headTitle($data['title']);
- }
- unset($data['title'], $data['meta_title']);
- foreach ($data as $k => $v) {
- if (strpos($k, 'meta_') !== false) {
- $view->headMeta()->appendName(str_replace('meta_', '', $k), $v);
- }
- }
- }
- public function disableLayout() {
- $this->_helper->layout()->disableLayout();
- }
- public function postDispatch() {
- if ($this->_helper->layout()->getLayout() != 'index-intro') {
- $this->view->headScript()->appendFile('/js/sloboda/regions-selector.js');
- }
- parent::postDispatch();
- }
- public function err($code = 404) {
- //Zend_Controller_Front::getRequest()->setModuleName('default')->setControllerName('error')->setActionName('error');
- http_response_code($code);
- $this->_forward('error', 'error', 'default');
- }
- public function getTradetype() {
- return $this->auth->hasIdentity() ? $this->auth->getIdentity()->TradeType : SLOBODA_TRADETYPE_RETAIL;
- }
- public function getRegion() {
- return $this->auth->hasIdentity() ? $this->auth->getIdentity()->IDRegion : $this->domain['region'];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement