Blame view

common/modules/fileloader/controllers/FileloaderController.php 3.85 KB
14a09168   Alex Savenko   init commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  <?php

      namespace common\modules\fileloader\controllers;

  

      use common\modules\fileloader\models\Fileloader;

      use yii\filters\AccessControl;

      use yii\helpers\Html;

      use yii\web\ForbiddenHttpException;

      use yii\web\UploadedFile;

  

      class FileloaderController extends \yii\web\Controller

      {

  

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

  //                'access' => [

  //                    'class' => AccessControl::className(),

  //                    'rules' => [

  //                        [

  //                            'allow' => true,

  //                            'roles' => [ '@' ],

  //                        ],

  //                    ],

  //                ],

                  'verbs' => [

                      'class'   => \yii\filters\VerbFilter::className(),

                      'actions' => [

                          '*' => [ 'post' ],

                      ],

                  ],

              ];

          }

  

          /**

           * Handle ajax file uploading

           *

           * @return array

           */

          public function actionUpload()

          {

              $request = \Yii::$app->request;

              $response = \Yii::$app->response;

              $response->format = $response::FORMAT_JSON;

              $model = new Fileloader();

              $model->files = UploadedFile::getInstance($model, 'files');

              if(!empty( $model->files )) {

                  $file_id = $model->saveFile($model->files);

                  if(!empty( $file_id )) {

                      $child_model = $request->post('model', $model->className());

                      $child_model = new $child_model([ 'file' => $file_id ]);

                      $input = Html::activeHiddenInput($child_model, 'fileloader[]', [

                          'value' => $file_id,

                          'class' => 'fileloader-item-input',

                      ]);

                      return [

                          'result' => [

                              'file_id' => $file_id,

                              'file_name' => $model->name,

                              'file_href' => $model->dir,

                              'input' => $input,

                              'id' => $request->post('id'),

                          ],

                      ];

                  } else {

                      return [ 'error' => 'Ошибка сохранения файла' ];

                  }

              }

              return [ 'error' => 'Ошибка загрузки' ];

          }

  

          /**

           * Handle ajax file deleting

           *

           * @return array

           */

          public function actionDelete()

          {

              /**

               * @var Fileloader $model

               */

              if(empty(\Yii::$app->user->identity)) {

                  throw new ForbiddenHttpException();

              }

              $request = \Yii::$app->request;

              $response = \Yii::$app->response;

              $response->format = $response::FORMAT_JSON;

              if(empty( $request->post('id') )) {

                  return [ 'error' => 'Не указан id файла' ];

              }

              $model = Fileloader::find()

                                 ->where([

                                     'file_id' => $request->post('id'),

                                     'user_id' => \Yii::$app->user->getId(),

                                 ])

                                 ->one();

              if(empty( $model )) {

                  return [ 'error' => 'Файл не найден' ];

              }

              if($model->delete()) {

                  return [ 'result' => [ 'message' => 'Файл успешно удален' ] ];

              } else {

                  return [ 'error' => 'Ошибка удаления файла' ];

              }

          }

          

      }