Beispiel #1
0
 /**
  * Store result in singleton to prevent multiple db requests with multiple calls
  *
  * @param bool $fromSingleton
  *
  * @return static
  */
 public static function getCurrentUser($fromSingleton = true)
 {
     if (!$fromSingleton) {
         return static::findOne(Yii::$app->user->id);
     }
     $user = Singleton::getData('__currentUser');
     if (!$user) {
         $user = static::findOne(Yii::$app->user->id);
         Singleton::setData('__currentUser', $user);
     }
     return $user;
 }
 /**
  * Render widgets in active template
  * Singleton::getData('content_template_id') is set in ContentUrlRule
  *
  * @param string $position
  *
  * @return string
  */
 public static function renderWidgets($position)
 {
     $templateId = Singleton::getData('content_template_id');
     if (!$templateId) {
         return '';
     }
     $result = '';
     $templateHasWidgets = ContentTemplateHasWidget::getDb()->cache(function () use($position, $templateId) {
         return ContentTemplateHasWidget::find()->joinWith('contentTemplateWidget')->andWhere(['content_template_widget.active' => 1])->andWhere(['content_template_has_widget.content_template_id' => $templateId, 'content_template_has_widget.position' => $position, 'content_template_widget.active' => 1])->orderBy('content_template_has_widget.sorter ASC')->all();
     }, ContentModule::CACHE_TIME, new TagDependency(['tags' => ContentModule::CACHE_TAG]));
     foreach ($templateHasWidgets as $templateHasWidget) {
         $widgetClass = $templateHasWidget->contentTemplateWidget->widget_class;
         $result .= "<div class='layout-widget layout-widget-{$position}'>";
         $result .= $widgetClass::widget(@unserialize($templateHasWidget->contentTemplateWidget->widget_options));
         $result .= "</div>";
     }
     return $result;
 }
 /**
  * Parses the given request and returns the corresponding route and parameters.
  * @param UrlManager $manager the URL manager
  * @param Request $request the request component
  * @return array|boolean the parsing result. The route and the parameters are returned as an array.
  * If false, it means this rule cannot be used to parse this path info.
  */
 public function parseRequest($manager, $request)
 {
     // If it's base url - call main page
     if ($request->getPathInfo() === '') {
         $mainPage = $this->getMainPage();
         if ($mainPage !== false) {
             Singleton::setData('content_template_id', $mainPage['content_template_id']);
             if ($mainPage['type'] == ContentPage::TYPE_TEXT) {
                 return ['/content/default/view', ['slug' => $mainPage['slug']]];
             } elseif ($mainPage['type'] == ContentPage::TYPE_INTERNAL_LINK) {
                 return ['/' . ltrim($mainPage['slug'], '/'), []];
             }
         }
     }
     $path = rtrim($request->getPathInfo(), '/');
     $parts = explode('/', $path);
     $languagePart = null;
     // Multilingual support - remove language from parts
     if (isset(Yii::$app->params['mlConfig']['languages'])) {
         if (array_key_exists($parts[0], Yii::$app->params['mlConfig']['languages'])) {
             $languagePart = array_shift($parts);
         }
     }
     $slug = count($parts) == 1 && $parts[0] != '' ? $parts[0] : end($parts);
     if (isset($this->getAllPages()[$slug])) {
         if (isset($this->getAllPages()[$slug])) {
             $page = $this->getAllPages()[$slug];
             Singleton::setData('content_template_id', $page['content_template_id']);
             return ['content/default/view', ['slug' => $slug, '_language' => $languagePart]];
         }
     } elseif (count($parts) > 1) {
         $slug = implode('/', $parts);
         if (isset($this->getAllPages()[$slug])) {
             $page = $this->getAllPages()[$slug];
             Singleton::setData('content_template_id', $page['content_template_id']);
         }
     }
     return false;
     // this rule does not apply
 }
 /**
  * @param string $slug
  *
  * @throws \yii\web\NotFoundHttpException
  * @return string
  */
 public function actionView($slug)
 {
     $contentPage = ContentPage::getDb()->cache(function () use($slug) {
         return ContentPage::find()->andWhere(['active' => 1, 'slug' => $slug])->one();
     }, ContentModule::CACHE_TIME, new TagDependency(['tags' => ContentModule::CACHE_TAG]));
     if (!$contentPage) {
         throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
     }
     // content_template_id is set in ContentUrlRule
     $templateId = Singleton::getData('content_template_id');
     if ($templateId && $this->module->enableTemplates) {
         $template = ContentTemplate::getDb()->cache(function () use($templateId) {
             return ContentTemplate::find()->andWhere(['id' => $templateId, 'active' => 1])->one();
         }, ContentModule::CACHE_TIME, new TagDependency(['tags' => ContentModule::CACHE_TAG]));
         if ($template && is_file(Yii::getAlias('@app/templates/') . $template->layout . '/layout.php')) {
             $this->layout = '@app/templates/' . $template->layout . '/layout.php';
         }
     } else {
         $this->layout = $this->module->defaultLayout;
     }
     $breadcrumbs[] = ['label' => $contentPage->name];
     //		$this->prepareBreadcrumbs($contentPage, $breadcrumbs);
     return $this->renderIsAjax('view', compact('contentPage', 'breadcrumbs'));
 }
 /**
  * @return array
  */
 private function _getI18NAttributes()
 {
     $singletonKey = '_mlAttributes_' . get_class($this);
     $mlAttributes = Singleton::getData($singletonKey);
     if ($mlAttributes === false) {
         $mlAttributes = [];
         $languages = Yii::$app->params['mlConfig']['languages'];
         //			unset($languages[Yii::$app->params['mlConfig']['default_language']]);
         foreach ($languages as $languageCode => $languageName) {
             foreach ($this->_i18n_attributes as $attribute) {
                 $mlAttributes[] = $attribute . '_' . $languageCode;
             }
         }
         Singleton::setData($singletonKey, $mlAttributes);
     }
     return $mlAttributes;
 }
 /**
  * @return array
  */
 public function mlGetAttributes()
 {
     $singletonKey = '_mlAttributes_' . get_class($this->owner);
     $mlAttributes = Singleton::getData($singletonKey);
     if ($mlAttributes === false) {
         $mlAttributes = [];
         $languages = $this->mlConfig['languages'];
         unset($languages[$this->mlConfig['default_language']]);
         foreach ($languages as $languageCode => $languageName) {
             foreach ($this->mlConfig['attributes'] as $attribute) {
                 $mlAttributes[] = $attribute . '_' . $languageCode;
             }
         }
         Singleton::setData($singletonKey, $mlAttributes);
     }
     return $mlAttributes;
 }