Пример #1
0
 /**
  * function ::create ($data)
  */
 public static function create($data)
 {
     $now = strtotime('now');
     $username = Yii::$app->user->identity->username;
     $model = new Discovery();
     if ($model->load($data)) {
         if ($log = new UserLog()) {
             $log->username = $username;
             $log->action = "Create";
             $log->object_class = "Discovery";
             $log->created_at = $now;
             $log->is_success = 0;
             $log->save();
         }
         $model->created_at = $now;
         $model->created_by = $username;
         do {
             $path = FileUtils::generatePath($now);
         } while (file_exists(Yii::$app->params['images_folder'] . $path));
         $model->image_path = $path;
         $targetFolder = Yii::$app->params['images_folder'] . $model->image_path;
         $targetUrl = Yii::$app->params['images_url'] . $model->image_path;
         if (!empty($data['discovery-image'])) {
             $copyResult = FileUtils::copyImage(['imageName' => $model->image, 'fromFolder' => Yii::$app->params['uploads_folder'], 'toFolder' => $targetFolder, 'resize' => array_values(Discovery::$image_resizes), 'removeInputImage' => true]);
             if ($copyResult['success']) {
                 $model->image = $copyResult['imageName'];
             }
         }
         if (!empty($data['discovery-banner'])) {
             $copyResult = FileUtils::copyImage(['imageName' => $model->banner, 'fromFolder' => Yii::$app->params['uploads_folder'], 'toFolder' => $targetFolder, 'resize' => array_values(Discovery::$banner_resizes), 'removeInputImage' => true]);
             if ($copyResult['success']) {
                 $model->banner = $copyResult['imageName'];
             }
         }
         if ($model->save()) {
             if ($log) {
                 $log->object_pk = $model->id;
                 $log->is_success = 1;
                 $log->save();
             }
             return $model;
         }
         $model->getErrors();
         return $model;
     }
     return false;
 }
Пример #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Discovery::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'is_active' => $this->is_active, 'position' => $this->position, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'old_slugs', $this->old_slugs])->andFilterWhere(['like', 'embedded_video', $this->embedded_video])->andFilterWhere(['like', 'embedded_image', $this->embedded_image])->andFilterWhere(['like', 'link', $this->link])->andFilterWhere(['like', 'image_path', $this->image_path])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'banner', $this->banner])->andFilterWhere(['like', 'created_by', $this->created_by])->andFilterWhere(['like', 'updated_by', $this->updated_by]);
     return $dataProvider;
 }
Пример #3
0
 /**
  * Finds the Discovery model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Discovery the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Discovery::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Пример #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDiscovery()
 {
     return $this->hasOne(Discovery::className(), ['id' => 'discovery_id']);
 }