/** * 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 int $id ContentTemplate ID * @param string $position * * @return string * @throws \yii\web\BadRequestHttpException */ public function actionExistingWidgets($id, $position) { if (!Yii::$app->request->isAjax) { throw new BadRequestHttpException('AJAX only'); } $widgets = []; $templateHasWidgets = ContentTemplateHasWidget::find()->joinWith('contentTemplateWidget')->andWhere(['content_template_widget.active' => 1])->andWhere(['content_template_has_widget.content_template_id' => $id, 'content_template_has_widget.position' => $position, 'content_template_widget.active' => 1])->orderBy('content_template_has_widget.sorter ASC')->all(); foreach ($templateHasWidgets as $templateHasWidget) { $widgets[] = $templateHasWidget->contentTemplateWidget; } return $this->renderAjax('existingWidgets', compact('widgets', 'position')); }