public function updateImages($event) { $post = \Yii::$app->request->post(); $model = $this->owner; $form_name = $model->formName(); if (isset($post[$form_name][static::UPDATE_IMAGES_FIELD])) { $images = $post[$form_name][static::UPDATE_IMAGES_FIELD]; if (!empty($images['default'])) { $image = Image::findOne($images['default']); $image->default = true; $image->save(); } if (!empty($images['delete'])) { foreach ($images['delete'] as $id => $_v) { $image = Image::findOne($id); $model->removeImage($image); } } } $this->attachImages($event); }
/** * Finds the Image model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Image the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Image::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * * @return boolean the status of deletion */ public function afterDelete() { $images = $this->getImage(); if ($images) { foreach ($images as $image) { $modelImage = Image::findOne($image->id); $path = implode("/", [Yii::$app->params['uploadStoragesPath'], $modelImage->getFullPath()]); if ($modelImage->delete()) { unlink($path); } } } return parent::afterDelete(); }
private function isResized($rawFilePath) { $image = \app\models\Image::findOne(['rawFilePath' => $rawFilePath]); return $image !== null; }