public static function deleteDependentFiles($adverId, $fileName) { $attachmentPath = Attachment::getAttachmentPath($adverId) . DIRECTORY_SEPARATOR . $fileName; if (file_exists($attachmentPath)) { @unlink($attachmentPath); } }
public function run($id) { $id = (int) $id; if (!($model = Attachment::findOne($id))) { throw new \yii\web\NotFoundHttpException(Yii::t('app', 'The requested file does not exist.')); } $path = $model->getAttachment(); if (!$path) { throw new \yii\web\NotFoundHttpException(Yii::t('app', 'The requested file does not exist.')); } Yii::$app->getResponse()->sendFile($path); }
public function run($id) { $id = (int) $id; $userId = Adver::getUserIdFromAdver($id); $output = []; if ($userId != Yii::$app->getUser()->id) { $output = ['error' => true, 'message' => '<div class="alert alert-danger">' . Yii::t('app', 'The requested page does not exist.') . '</div>']; } if (empty($output) && !Attachment::checkLimitation($id)) { $output = ['error' => true, 'message' => '<div class="alert alert-danger">' . Yii::t('app', 'Attachment limitation per advertisement reached!') . '</div>']; } if (empty($output)) { $model = new Attachment(['scenario' => 'new']); if ($model->load(Yii::$app->request->post())) { $model->attachment = UploadedFile::getInstance($model, 'attachment'); if ($model->attachment) { $model->name = Yii::$app->helper->safeFile($model->attachment->baseName) . '-' . Yii::$app->getSecurity()->generateRandomString(6) . '-' . time() . '.' . $model->attachment->extension; $path = $model->getAttachmentPath($id) . DIRECTORY_SEPARATOR . $model->name; $path = FileHelper::normalizePath($path); $model->adver_id = $id; if ($model->validate() && $model->attachment->saveAs($path, false)) { if ($model->save()) { $output = ['error' => false, 'message' => '<div class="alert alert-success">' . Yii::t('app', 'Attachment saved!') . '</div>']; } else { $output = ['error' => true, 'message' => '<div class="alert alert-danger">' . Yii::t('app', 'Error on saving attachment.') . '</div>']; } } } } //print_r($model); if (empty($output)) { $output = ['error' => true, 'message' => \yii\helpers\Html::errorSummary($model, ["class" => "alert alert-danger"])]; } } return \yii\helpers\Json::encode($output); }
public function run($id) { $id = (int) $id; $output = []; if (($model = Attachment::findOne($id)) !== null) { if (Yii::$app->getUser()->can('DeleteOwnAttachment', ['attachment' => $model])) { if ($model->delete()) { $output = ['error' => false, 'message' => Yii::t('app', 'Successfully deleted!')]; } } } if (empty($output)) { $output = ['error' => true, 'message' => Yii::t('app', 'The requested page does not exist.')]; } return \yii\helpers\Json::encode($output); }
public function search($params, $adverId) { $query = Attachment::find(); $query->andWhere(['adver_id' => $adverId]); if (!Yii::$app->getUser()->can('GalleryList')) { $query->andWhere(['user_id' => Yii::$app->getUser()->id]); } $dataProvider = new ActiveDataProvider(['query' => $query]); //$dataProvider->sort->defaultOrder = ['updated_at' => SORT_DESC]; // load the seach form data and validate if (!($this->load($params) && $this->validate())) { return $dataProvider; } // adjust the query by adding the filters $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
public static function deleteDependentFiles($adverId) { $galleryPath = Gallery::getImagePath($adverId); $attachmentPath = Attachment::getAttachmentPath($adverId); if (is_dir($galleryPath)) { FileHelper::removeDirectory($galleryPath); } if (is_dir($attachmentPath)) { FileHelper::removeDirectory($attachmentPath); } }