public function actionActiveWidgets($id) { $variation = $this->loadModel(ThemeVariation::className(), $id); if (isset($_POST['addWidget'], $_POST['partId'])) { $part = ThemeParts::findById($_POST['partId']); if ($part === null) { throw new NotFoundHttpException(); } $widget = ThemeWidgets::findById($_POST['addWidget']); if ($widget === null) { throw new NotFoundHttpException(); } $binding = new ThemeActiveWidgets(); $binding->part_id = $part->id; $binding->variation_id = $variation->id; $binding->widget_id = $widget->id; $binding->save(); } $models = ThemeActiveWidgets::find()->where(['variation_id' => $variation->id])->orderBy(['part_id' => SORT_ASC, 'sort_order' => SORT_ASC])->all(); // Warning! Element of $allParts is not a model! It's an array of db rows $allParts = ThemeParts::getAllParts(); $availableWidgets = ThemeWidgets::find()->joinWith('applying')->all(); return $this->render('active-widgets', ['variation' => $variation, 'models' => $models, 'availableWidgets' => $availableWidgets, 'allParts' => $allParts]); }
/** * @inheritdoc */ public function beforeDelete() { if (!parent::beforeDelete()) { return false; } $parts = ThemeWidgetApplying::find()->where(['widget_id' => $this->id])->all(); foreach ($parts as $part) { $part->delete(); } $activeWidgets = ThemeActiveWidgets::find()->where(['widget_id' => $this->id])->all(); foreach ($activeWidgets as $widget) { $widget->delete(); } return true; }
/** * Returns active widgets for current request * @return ThemeActiveWidgets[] */ public static function getActiveWidgets() { if (static::$activeWidgets === null) { Yii::beginProfile('Get active widgets'); $variationIds = ArrayHelper::getColumn(ThemeVariation::getMatchedVariations(), 'id'); if (count($variationIds) === 0) { Yii::trace("Warning! No active widgets because of no matched variations! Check your variations configuration."); Yii::endProfile('Get active widgets'); return []; } $cacheKey = 'ActiveWidgets:' . implode(',', $variationIds); static::$activeWidgets = Yii::$app->cache->get($cacheKey); if (static::$activeWidgets === false) { static::$activeWidgets = ThemeActiveWidgets::find()->where(['in', 'variation_id', $variationIds])->with('widget')->orderBy(['part_id' => SORT_ASC, 'sort_order' => SORT_ASC])->all(); Yii::$app->cache->set($cacheKey, static::$activeWidgets, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(ThemeActiveWidgets::className()), ActiveRecordHelper::getCommonTag(ThemeWidgets::className())]])); } Yii::endProfile('Get active widgets'); } return static::$activeWidgets; }