Exemplo n.º 1
0
 /**
  * Deletes an existing Page model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $fid
  * @return mixed
  */
 public function actionDelete($fid)
 {
     if (File::fileDelete($fid)) {
         $result = [];
     } else {
         $result = ['error' => 'Ошибка'];
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     return $result;
 }
Exemplo n.º 2
0
 public function actionUpdate($id)
 {
     $this->layout = '@app/views/layouts/admin';
     $modelSlider = $this->findSlider($id);
     $post = Yii::$app->request->post();
     if ($modelSlider->load($post) && $modelSlider->save()) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $i = 0;
             foreach ($post['SliderItem'] as $item) {
                 $err = $_FILES['slide-' . $i]['error'];
                 if ($item['id'] > 0 || $err == 0 || $item['title'] !== '' || $item['body'] !== '') {
                     if ($item['id'] > 0) {
                         $sliderItem = SliderItem::findOne($item['id']);
                     } else {
                         $sliderItem = new SliderItem();
                     }
                     $sliderItem->title = $item['title'];
                     $sliderItem->body = $item['body'];
                     $sliderItem->slider_id = $modelSlider->id;
                     $sliderItem->sort_order = $i;
                     if ($_FILES['slide-' . $i]['error'] == 0) {
                         if ($sliderItem->fid > 0) {
                             File::fileUnlink(['fid' => $sliderItem->fid]);
                         }
                         $file = UploadedFile::getInstanceByName('slide-' . $i);
                         $saveFile = new File();
                         $sliderItem->fid = $saveFile->uploadFile($file, 'SliderItem', $sliderItem->id, 'slider');
                     }
                     $sliderItem->save();
                 }
                 $i++;
             }
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
         }
         return $this->redirect(['/slider/node/view', 'slug' => $modelSlider->name]);
     } else {
         $items = $modelSlider->items;
         if (!count($items)) {
             $items[0] = new SliderItem();
         }
         return $this->render('update', ['modelSlider' => $modelSlider, 'modelSliderItem' => $items]);
     }
 }
Exemplo n.º 3
0
 public function getFile()
 {
     return $this->hasOne(File::className(), ['fid' => 'fid']);
 }
Exemplo n.º 4
0
 public function saveUploadedFile($dir)
 {
     $result = [];
     $year = date('Y', time());
     $month = date('m', time());
     $path = '@webroot/' . $dir . '/' . $year . '/' . $month . '/';
     $path = Yii::getAlias($path);
     if (!file_exists($path)) {
         mkdir($path, 0777, true);
     }
     if (count($this->files) > 0) {
         $key = 0;
         foreach ($this->files as $file) {
             $this->slugName = Inflector::slug($file->baseName);
             $uri = $this->path . $this->slugName . '.' . $file->extension;
             $iteration = 0;
             while (file_exists(Yii::getAlias($uri))) {
                 $uri = $this->path . $this->slugName . '_' . $iteration . '.' . $file->extension;
                 $iteration++;
             }
             if ($file->error == 0) {
                 list($width, $height) = getimagesize($file->tempName);
                 if ($width > 0 && $height > 0) {
                     $image = Image::thumbnail($file->tempName, $width, $height);
                     $deg = 0;
                     $file_exif = exif_read_data($file->tempName);
                     if (isset($file_exif['Orientation'])) {
                         switch ($file_exif['Orientation']) {
                             case 3:
                                 $deg = 180;
                                 break;
                             case 6:
                                 $deg = 90;
                                 break;
                             case 8:
                                 $deg = 270;
                                 break;
                             default:
                                 $deg = 0;
                         }
                     }
                     if ($width > $this->maxWidth) {
                         $ratio = $width / $height;
                         $width = $this->maxWidth;
                         $height = round($width / $ratio);
                         $image = Image::thumbnail($file->tempName, $width, $height);
                     }
                     $image->rotate($deg)->save(Yii::getAlias($uri), ['quality' => $this->quality]);
                 } else {
                     $file->saveAs(Yii::getAlias($uri));
                 }
                 $model = new File();
                 $model->file = $file;
                 $model->uri = $uri;
                 $model->key = $key;
                 $key++;
                 $result[] = $model->saveFile();
             }
         }
     }
     return $result;
 }
Exemplo n.º 5
0
 public function getPluginOptions($attribute)
 {
     $this->fileToken();
     $files = File::getAttach($this->model_name, $this->model_id, $attribute);
     $initialPreview = [];
     $initialPreviewConfig = [];
     foreach ($files as $file) {
         $initialPreview[] = stristr($file->uri, '/');
         $initialPreviewConfig[] = ['caption' => $file->filename, 'size' => $file->file_size, 'url' => Url::to(['/files/' . $file->fid . '/delete']), 'key' => $file->key, 'extra' => ['fid' => $file->fid]];
     }
     $pluginOptions = ['initialPreview' => $initialPreview, 'initialPreviewConfig' => $initialPreviewConfig, 'showCaption' => false, 'showUpload' => false, 'showRemove' => false, 'overwriteInitial' => false, 'browseIcon' => '<i class="material-icons">file_download</i>', 'removeIcon' => '<i class="material-icons">delete</i>'];
     return $pluginOptions;
 }