/** * Finds the WidgetCarousel model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return WidgetCarousel the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = WidgetCarousel::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = WidgetCarousel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); $query->andFilterWhere(['like', 'key', $this->key]); return $dataProvider; }
/** * Creates a new WidgetCarouselItem model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($carousel_id) { $model = new WidgetCarouselItem(); $carousel = WidgetCarousel::findOne($carousel_id); if (!$carousel) { throw new HttpException(400); } $model->carousel_id = $carousel->id; if ($model->load(Yii::$app->request->post())) { if ($model->save()) { Yii::$app->getSession()->setFlash('alert', ['options' => ['class' => 'alert-success'], 'body' => Yii::t('backend', 'Carousel slide was successfully saved')]); return $this->redirect(['/widget-carousel/update', 'id' => $model->carousel_id]); } } return $this->render('create', ['model' => $model, 'carousel' => $carousel]); }
/** * @return \yii\db\ActiveQuery */ public function getCarousel() { return $this->hasOne(WidgetCarousel::className(), ['id' => 'carousel_id']); }
/** * @throws InvalidConfigException */ public function init() { if (!$this->key) { throw new InvalidConfigException(); } $cacheKey = [WidgetCarousel::className(), $this->key]; $items = \Yii::$app->cache->get($cacheKey); if ($items === false) { $items = []; $query = WidgetCarouselItem::find()->joinWith('carousel')->where(['{{%widget_carousel_item}}.status' => 1, '{{%widget_carousel}}.status' => WidgetCarousel::STATUS_ACTIVE, '{{%widget_carousel}}.key' => $this->key])->orderBy(['order' => SORT_ASC]); foreach ($query->all() as $k => $item) { /** @var $item \common\models\WidgetCarouselItem */ if ($item->path) { $items[$k]['content'] = Html::img($item->getImageUrl()); } if ($item->url) { $items[$k]['content'] = Html::a($items[$k]['content'], $item->url, ['target' => '_blank']); } if ($item->caption) { $items[$k]['caption'] = $item->caption; } } \Yii::$app->cache->set($cacheKey, $items, 60 * 60 * 24 * 365); } $this->items = $items; parent::init(); }