/**
  * @return null|string
  */
 public function run()
 {
     $model = $this->findModel();
     if (isset($model)) {
         return $this->render($this->detailTemplate, ['model' => $model, 'class' => $this->class]);
     } else {
         $models = GalleryModel::find()->where(['active' => 1])->orderby(['position' => SORT_ASC])->all();
         return $this->render($this->template, ['models' => $models, 'class' => $this->class]);
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Gallery::find()->joinWith(['translations'])->where(['language' => Yii::$app->language]);
     $query->orderBy('position');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false, 'pagination' => ['pageSize' => 50]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     // Convert date to Unix timestamp
     if (!empty($params[StringHelper::basename(self::className())]['date'])) {
         $query->andFilterWhere(['date' => strtotime($params[StringHelper::basename(self::className())]['date'])]);
     }
     $query->andFilterWhere(['id' => $this->id, 'active' => $this->active]);
     $query->andFilterWhere(['like', 'translations.name', $this->name]);
     return $dataProvider;
 }
 /**
  * Set as main image
  * @param string $id
  * @return mixed
  */
 public function actionMain()
 {
     // Reset previous main image
     Image::updateAll(['isMain' => 0], ['isMain' => 1, 'itemId' => Yii::$app->session->get('gallery.gallery-id'), 'modelName' => StringHelper::basename(Gallery::className())]);
     // Set new main image
     $model = $this->findModel(Yii::$app->request->post('id'));
     $model->isMain = 1;
     $model->active = 1;
     return $model->save();
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getGallery()
 {
     return $this->hasOne(Gallery::className(), ['id' => 'gallery_id']);
 }
 /**
  * Finds the Gallery model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Gallery the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Gallery::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }