/** * 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; }
/** * @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; }