Blame view

backend/modules/user/controllers/ProfileController.php 2.85 KB
d1f8bd40   Alexey Boroda   first 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 backend\modules\user\controllers;
  
  use Yii;
  use yii\web\NotFoundHttpException;
  use yii\filters\AccessControl;
  //
  use thread\app\base\controllers\BackendController;
  use thread\actions\Update;
  use thread\actions\fileapi\{
      DeleteAction, UploadAction
  };
  //
  use backend\modules\user\models\{
      Profile, User
  };
  
  /**
   * @author FilamentV <vortex.filament@gmail.com>
   * @copyright (c), Thread
   */
  class ProfileController extends BackendController
  {
      public $name = 'profile';
      public $title = "Profile";
      protected $model = Profile::class;
      public $defaultAction = 'update';
      public $actionListLinkStatus = ['/user/user/list'];
      protected $user_id = null;
  
      /**
       * @return array
       */
      public function behaviors()
      {
          return [
              'AccessControl' => [
                  'class' => AccessControl::class,
                  'rules' => [
                      [
                          'allow' => true,
                          'actions' => ['request-reset', 'reset'],
                          'roles' => ['?'],
                      ],
                      [
                          'allow' => true,
                          'actions' => ['validation', 'change'],
                          'roles' => ['admin']
                      ],
                      [
                          'allow' => false,
                      ],
                  ],
              ],
          ];
      }
  
      /**
       *
       * @return array
       */
      public function actions()
      {
          /**
           * @var $module \backend\modules\user\User
           */
          $model = Profile::find()->byId(Yii::$app->getRequest()->get('id', 0))->one();
          $user = $model->user;
          $module = $this->module;
  
          return [
              'update' => [
                  'class' => Update::class,
                  'modelClass' => $this->model,
                  'redirect' => function () {
                      return $_POST['save_and_exit'] ? ['/user/user/list'] : ['update', 'id' => $this->action->getModel()->id];
                  }
              ],
              'fileupload' => [
                  'class' => UploadAction::class,
                  'path' => $module->getAvatarUploadPath($user)
              ],
              'filedelete' => [
                  'class' => DeleteAction::class,
                  'path' => $module->getAvatarUploadPath($user)
              ],
          ];
      }
  
      /**
       * @param $action
       * @return bool
       * @throws NotFoundHttpException
       */
      public function beforeAction($action)
      {
  
          $actionName = $this->action->id;
          if (in_array($actionName, ['fileupload', 'filedelete'])) {
              $model = Profile::find()->byId(Yii::$app->getRequest()->get('id', 0))->one();
              if ($model === null) {
                  throw new NotFoundHttpException;
              }
              $this->user_id = $model['user_id'];
          }
          return true;
      }
  }