public function actions() { $formSettings = array('redirect' => $this->createUrl('admin'), 'forms' => array('id' => 'mainForm', 'varName' => 'cmsAlias', 'models' => 'CmsAlias', 'onAfterSave' => function ($event) { $model = $event->params['model']; if ($model->location != 'nochange') { $decode = CJSON::decode($_POST['CmsAlias']['location']); $to = CmsAlias::model()->findByPk((int) $decode['to']); $action = $decode['action']; switch ($action) { case 'child': $model->moveAsLast($to); break; case 'before': if ($to->isRoot()) { $model->moveAsRoot(); } else { $model->moveBefore($to); } break; case 'after': if ($to->isRoot()) { $model->moveAsRoot(); } else { $model->moveAfter($to); } break; } } }, 'forms' => array('id' => 'routesForm', 'models' => 'CmsAliasRoute', 'varName' => 'cmsAliasRoutes', 'parentIdAttribute' => 'alias_id'))); return array('create' => array('class' => 'application.components.actions.Create', 'formSettings' => $formSettings), 'update' => array('class' => 'application.components.actions.Update', 'formSettings' => $formSettings), 'delete' => array('class' => 'application.components.actions.Delete', 'modelClass' => 'CmsAlias'), 'admin' => array('class' => 'application.components.actions.Admin', 'modelClass' => 'CmsAlias')); }
public function afterFind() { foreach (Yii::app()->languageManager->languages as $language => $fullLanguage) { if ($language === Yii::app()->sourceLanguage) { $suffix = ''; } else { $suffix = '_' . $language; } $alias = CmsAlias::model()->multilang()->findByPk($this->alias->id); $this->{'title' . $suffix} = $alias->{'title' . $suffix}; if ($language == Yii::app()->language) { $this->title = $alias->{'title' . $suffix}; } } return parent::afterFind(); }
/** * Returns a path type string from an alias id * * @param int $id the alias id * * @return string the path */ public static function pathFromId($id) { $paths = array(); while (true) { $alias = CmsAlias::model()->findByPk($id); $paths[] = $alias->alias; $id = $alias->parent_id; if ($alias->parent_id == 0) { $paths = array_reverse($paths); $pathStr = ''; foreach ($paths as $path) { $pathStr .= $path . '/'; } //$pathStr = trim($pathStr, '/'); return $pathStr; } } }
/** * Cms parseUrl * * See comments in code to understand the procedure. * * You can cancel alias resolution by setting Cms::cancelAliasResultion to true. * * @see CUrlManager::parseUrl(). */ public function parseUrl($request) { // Install and admin bypass normal execution because we don't want the language or aliases. if ($request->getPathInfo() == 'install') { return 'install'; } else { if ($request->getPathInfo() == 'admin') { return 'admin'; } else { if ($this->getUrlFormat() === self::PATH_FORMAT) { $pathInfo = $request->getPathInfo(); $pathInfo = preg_replace('/@@@/', '', $pathInfo); // Set language. if (Yii::app()->languageManager->multilang && preg_match('/^(' . implode('|', array_keys(Yii::app()->languageManager->languages)) . ')(\\/(.*))?$/', $pathInfo, $matches)) { $_GET['language'] = $matches[1]; $pathInfo = isset($matches[3]) ? $matches[3] : ''; } Yii::app()->languageManager->setLanguage(); // Replacing the alias in the pathInfo with the proper route. if (!$this->cancelAliasResolution) { $originalRoute = $pathInfo; $route = $pathInfo; if (!(($route = trim($route, '/')) === '')) { $route = $route . '/'; $lft = null; $rgt = null; $noAliasCondition = ''; $noAliasParams = array(); $aliasModelFound = null; // Looping through the url backwards until we find (or not) the alias in the cache $routeBackwards = '/' . rtrim($route, '/'); while (($pos = strrpos($routeBackwards, '/')) !== false) { if (($aliasId = Yii::app()->cache->get('parseUrl_/' . Yii::app()->language . $routeBackwards)) === false) { $routeBackwards = substr($routeBackwards, 0, $pos); } else { Yii::app()->cms->currentAlias = CmsAlias::model()->with('routes')->findByPk($aliasId); Yii::app()->cms->currentSectionId = Yii::app()->cms->currentAlias->section_id; $route = substr($route, strlen($routeBackwards)); // We'll need the GET part of the route in $route later if ($route === false) { $route = ''; } break; } } // If not found in cache if (!isset(Yii::app()->cms->currentAlias)) { // Looping from beginning to end of url as long as it finds a corresponding alias. $posTotal = 0; while (($pos = strpos($route, '/')) !== false) { $id = substr($route, 0, $pos); $route = (string) substr($route, $pos + 1); $params = array(':id' => $id, ':lang' => Yii::app()->language); if ($lft !== null) { $params[':lft'] = $lft; $params[':rgt'] = $rgt; } if ($noAliasCondition == '' && ($aliasModel = CmsAlias::model()->multilang()->find('i18nCmsAlias.l_alias=:id AND i18nCmsAlias.lang_id=:lang' . ($lft === null ? ' AND t.level = 2' : ' AND t.lft > :lft AND t.rgt < :rgt'), $params))) { $lft = $aliasModel->lft; $rgt = $aliasModel->rgt; $aliasModelFound = $aliasModel; $posTotal += $posTotal == 0 ? $pos : $pos + 1; } else { if ($aliasModelFound == null) { $noAliasParamsCount = count($noAliasParams); $path = $noAliasCondition == '' ? $id : $noAliasParams['path' . ($noAliasParamsCount - 1)] . '/' . $id; $noAliasCondition .= 't.route = :path' . $noAliasParamsCount . ' OR '; $noAliasParams['path' . $noAliasParamsCount] = $path; } else { break; } } } // If alias found if ($aliasModelFound != null) { Yii::app()->cms->currentAlias = CmsAlias::model()->with('routes')->findByPk($aliasModelFound->primaryKey); // Must get the localized version (non multilang()). Yii::app()->cms->currentSectionId = Yii::app()->cms->currentAlias->section_id; Yii::app()->languageManager->checkAliasLanguageRestrictions(); Yii::app()->cache->set('parseUrl_/' . Yii::app()->language . substr('/' . trim($originalRoute, '/') . '/', 0, $posTotal + 1), Yii::app()->cms->currentAlias->id); // Adding route to cache // Checking to see if ancestors of alias are valid. $route = $originalRoute; $aliasAncestors = Yii::app()->cms->currentAlias->ancestors()->findAll(); if (!empty($aliasAncestors)) { for ($i = 1; $i < count($aliasAncestors); $i++) { $aliasAncestor = $aliasAncestors[$i]; $pos = strpos($route, '/'); $id = substr($route, 0, $pos); $route = (string) substr($route, $pos + 1); if ($id != $aliasAncestor->alias) { throw new CHttpException(404, Yii::t('yii', 'Unable to resolve the request "{route}".', array('{route}' => $originalRoute))); } } } if ($pos = strpos($route, '/')) { $route = substr($route, $pos + 1); } else { $route = ''; } } // If no alias was found, checking to see if any alias exists for this route. if ($noAliasCondition != '') { if (CmsAliasRoute::model()->count(substr($noAliasCondition, 0, -4), $noAliasParams) > 0) { $this->_aliasNotUsed = true; } } } } } // Setting $pathInfo if (isset(Yii::app()->cms->currentAlias)) { if ($pathRoute = Yii::app()->cms->currentAlias->routes) { $pathInfo = $pathRoute[0]->route . ($route !== '' ? '/' . trim($route, '/') : ''); } else { $pathInfo = '404'; } // Page without content (serves as parent to other pages only) if we land here then we need to throw and error. } // Now that we have the proper route, we can roll through the modules url rewrite classes. foreach ($this->_moduleUrlRuleClasses as $moduleUrlRuleClass) { $moduleRule = Yii::createComponent($moduleUrlRuleClass); if (($moduleRoute = $moduleRule->parseUrl($this, $request, $pathInfo, $pathInfo)) !== false) { return $moduleRoute; } } // Now that we have the proper route, we can roll through the modules url rewrite rules. foreach ($this->_rules as $i => $rule) { if (is_array($rule)) { $this->_rules[$i] = $rule = Yii::createComponent($rule); } if (($r = $rule->parseUrl($this, $request, $pathInfo, $pathInfo)) !== false) { return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r; } } // Strict parsing requires that a rewrite rule be used. If this point was reached we throw an exception, otherwise return the pathInfo. if ($this->useStrictParsing) { throw new CHttpException(404, Yii::t('yii', 'Unable to resolve the request "{route}".', array('{route}' => $pathInfo))); } else { return $pathInfo; } } else { if (isset($_GET[$this->routeVar])) { return $_GET[$this->routeVar]; } else { if (isset($_POST[$this->routeVar])) { return $_POST[$this->routeVar]; } else { return ''; } } } } } }
/** * Generate breadcrumbs from CMS alias. Uses cache. * * @param bool $linkLast wether to link the last item or not. * @param int $aliasId the alias id. Defaults to the current alias. * * @return array the breadcrumbs. */ public static function breadcrumbsFromAlias($linkLast = false, $aliasId = null) { $controller = Yii::app()->controller; $breadcrumbs = array(); if ($aliasId === null) { $currentAlias = true; $aliasId = Yii::app()->cms->currentAlias->primaryKey; } else { $currentAlias = false; } if (($serializedBreadcrumb = Yii::app()->cache->get('aliasBreadcrumb_' . $aliasId)) === false) { if ($currentAlias) { $aliasModel = Yii::app()->cms->currentAlias; } else { $aliasModel = CmsAlias::model()->findByPk($aliasId); } $ancestors = $aliasModel->ancestors()->findAll(); foreach ($ancestors as $ancestor) { if ($ancestor->level != 1) { $routes = $ancestor->routes; if (count($routes) == 0) { $breadcrumbs[] = $ancestor->title; } elseif (is_null($ancestor->section)) { $breadcrumbs[] = $ancestor->title; } else { $aliasUrl = $controller->createUrl('/', array('keyword' => $ancestor->keyword)); $breadcrumbs[$ancestor->title] = $aliasUrl; } } } $routes = $aliasModel->routes; if (!$linkLast || count($routes) == 0 || is_null($aliasModel->section)) { $breadcrumbs[] = $aliasModel->title; } else { $aliasUrl = $controller->createUrl('/', array('keyword' => $aliasModel->keyword)); $breadcrumbs[$aliasModel->title] = $aliasUrl; } Yii::app()->cache->set('aliasBreadcrumb_' . $aliasId, serialize($breadcrumbs)); } else { $breadcrumbs = unserialize($serializedBreadcrumb); } return $breadcrumbs; }
//================================================================================ // Add a "active" class to menu items //================================================================================ <?php // For keyword type links $currentAliasKeyword = isset(Yii::app()->cms->currentAlias) ? Yii::app()->cms->currentAlias->keyword : ''; // For whole modules $currentModuleId = isset($this->module->id) ? $this->module->id : ""; ?> <ul> <!-- To set active if it's a child of a certain parent --> <li<?php echo isset(Yii::app()->cms->currentAlias) ? Yii::app()->cms->currentAlias->isDescendantOf(CmsAlias::model()->findByAttributes(array('keyword' => 'parent-keyword'))) : false ? " class='active'" : ""; ?> ><a href="#">Child</a></li> <!-- For keyword type links --> <li<?php echo $currentAliasKeyword == 'keyword' ? " class='active'" : ""; ?> ><a href="#">Page</a></li> <!-- To set active if in module --> <li<?php echo $currentModuleId == "name-module" ? " class='active'" : ""; ?> ><a href="#">Module</a></li>
public function actions() { $formSettings = array('redirect' => $this->createUrl('admin'), 'forms' => array('id' => 'mainForm', 'varName' => 'contentPage', 'models' => 'ContentPage', 'forms' => array('id' => 'blocsForm', 'blocs' => 'content', 'varName' => 'blocs'), 'onAfterSetAttributes' => function ($event) { if (Yii::app()->user->id != 'Admin-1' && isset($this->module->layouts)) { $model = $event->params['model']; if ($model->layout == '') { foreach ($this->module->layouts as $layoutName => $layoutTitle) { $model->layout = $layoutName; break; } } } }, 'onBeforeSave' => function ($event) { $model = $event->params['model']; if ($model->isNewRecord) { $alias = new CmsAlias(); foreach (Yii::app()->languageManager->languages as $language => $fullLanguage) { if ($language === Yii::app()->sourceLanguage) { $suffix = ''; } else { $suffix = '_' . $language; } $alias->{'alias' . $suffix} = AdminHelper::generateUrlStr($model->{'title' . $suffix}, $alias, 'alias', null, $language); $alias->{'title' . $suffix} = $model->{'title' . $suffix}; } $alias->section_id = CmsSection::model()->findByAttributes(array('module' => 'content'))->id; $alias->allow_children = 1; $alias->attributes = $_POST['CmsAlias']; $root = CmsAlias::model()->roots()->find(); $alias->appendTo($root); $aliasRoute = new CmsAliasRoute(); $aliasRoute->route = 'content'; $aliasRoute->alias_id = $alias->primaryKey; $aliasRoute->save(); $model->alias_id = $alias->primaryKey; } else { $alias = CmsAlias::model()->multilang()->findByPk($model->alias->id); $alias->attributes = $_POST['CmsAlias']; foreach (Yii::app()->languageManager->languages as $language => $fullLanguage) { if ($language === Yii::app()->sourceLanguage) { $suffix = ''; } else { $suffix = '_' . $language; } $alias->{'alias' . $suffix} = AdminHelper::generateUrlStr($model->{'title' . $suffix}, $alias, 'alias', $alias->id, $language); $alias->{'title' . $suffix} = $model->{'title' . $suffix}; } } if ($alias->location != 'nochange') { $decode = CJSON::decode($_POST['CmsAlias']['location']); $to = CmsAlias::model()->findByPk((int) $decode['to']); $action = $decode['action']; switch ($action) { case 'child': $alias->moveAsLast($to); break; case 'before': if ($to->isRoot()) { $alias->moveAsRoot(); } else { $alias->moveBefore($to); } break; case 'after': if ($to->isRoot()) { $alias->moveAsRoot(); } else { $alias->moveAfter($to); } break; } } else { $alias->saveNode(); } })); return array('create' => array('class' => 'application.components.actions.Create', 'formSettings' => $formSettings), 'update' => array('class' => 'application.components.actions.Update', 'formSettings' => $formSettings), 'delete' => array('class' => 'application.components.actions.Delete', 'modelClass' => 'ContentPage'), 'admin' => array('class' => 'application.components.actions.Admin', 'modelClass' => 'ContentPage')); }