|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (c) 2020 Insolita <webmaster100500@ya.ru> and contributors |
| 5 | + * @license https://github.com/insolita/yii2-fractal/blob/master/LICENSE |
| 6 | + */ |
| 7 | + |
| 8 | +namespace insolita\fractal\actions; |
| 9 | + |
| 10 | +use insolita\fractal\exceptions\ValidationException; |
| 11 | +use insolita\fractal\providers\CursorActiveDataProvider; |
| 12 | +use insolita\fractal\providers\JsonApiActiveDataProvider; |
| 13 | +use Yii; |
| 14 | +use yii\base\InvalidConfigException; |
| 15 | +use yii\db\ActiveQueryInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Handler for list actions with parent id equals current user identity |
| 19 | + * @example |
| 20 | + * show user posts |
| 21 | + * $dataProvider by query Post::find()->where(['author_id' => Yii::$app->user->id])->... + filter conditions |
| 22 | + * 'my-posts' => [ |
| 23 | + * 'class' => ListForIdentityAction::class, |
| 24 | + * 'userIdAttribute' => 'author_id', |
| 25 | + * 'modelClass' => Post::class, |
| 26 | + * 'transformer' => PostTransformer::class |
| 27 | + * ], |
| 28 | +**/ |
| 29 | +class ListForIdentityAction extends JsonApiAction |
| 30 | +{ |
| 31 | + use HasResourceTransformer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + * user foreign key attribute |
| 36 | + */ |
| 37 | + public $userIdAttribute = 'user_id'; |
| 38 | + /** |
| 39 | + * @var callable |
| 40 | + * @example |
| 41 | + * 'prepareDataProvider' => function(ListAction $action, \yii\data\DataProviderInterface $dataProvider) { |
| 42 | + * Modify $dataProvider |
| 43 | + * or return completely configured dataProvider (JsonApiActiveDataProvider|CursorActiveDataProvider) |
| 44 | + * } |
| 45 | + */ |
| 46 | + public $prepareDataProvider; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \yii\data\DataFilter |
| 50 | + */ |
| 51 | + public $dataFilter; |
| 52 | + |
| 53 | + /** |
| 54 | + * Provide custom configured dataProvider object (JsonApiActiveDataProvider|CursorActiveDataProvider) |
| 55 | + * You can set 'pagination' => false for disable pagination |
| 56 | + * @var array |
| 57 | + */ |
| 58 | + public $dataProvider = [ |
| 59 | + 'class' => JsonApiActiveDataProvider::class, |
| 60 | + 'pagination'=>['defaultPageSize' => 20] |
| 61 | + ]; |
| 62 | + |
| 63 | + /** |
| 64 | + * @throws \yii\base\InvalidConfigException |
| 65 | + */ |
| 66 | + public function init():void |
| 67 | + { |
| 68 | + parent::init(); |
| 69 | + $this->initResourceTransformer(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return \insolita\fractal\providers\JsonApiActiveDataProvider|object |
| 74 | + * @throws \insolita\fractal\exceptions\ValidationException |
| 75 | + * @throws \yii\base\InvalidConfigException |
| 76 | + */ |
| 77 | + public function run() |
| 78 | + { |
| 79 | + if ($this->checkAccess) { |
| 80 | + call_user_func($this->checkAccess, $this->id); |
| 81 | + } |
| 82 | + |
| 83 | + return $this->makeDataProvider(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param $requestParams |
| 88 | + * @return array|bool|mixed|null |
| 89 | + * @throws \insolita\fractal\exceptions\ValidationException |
| 90 | + * @throws \yii\base\InvalidConfigException |
| 91 | + */ |
| 92 | + protected function prepareDataFilter($requestParams) |
| 93 | + { |
| 94 | + if ($this->dataFilter === null) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + $this->dataFilter = Yii::createObject($this->dataFilter); |
| 98 | + if ($this->dataFilter->load($requestParams)) { |
| 99 | + $filter = $this->dataFilter->build(); |
| 100 | + if ($filter === false) { |
| 101 | + throw new ValidationException($this->dataFilter->getErrors()); |
| 102 | + } |
| 103 | + return $filter; |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Add condition for parent model restriction if needed |
| 110 | + * @param \yii\db\ActiveQueryInterface $query |
| 111 | + * @return \yii\db\ActiveQueryInterface |
| 112 | + */ |
| 113 | + protected function prepareParentQuery(ActiveQueryInterface $query):ActiveQueryInterface |
| 114 | + { |
| 115 | + $userId = Yii::$app->user->id; |
| 116 | + $condition[$this->modelTable().'.'.$this->userIdAttribute] = $userId; |
| 117 | + $query->where($condition); |
| 118 | + return $query; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /** |
| 123 | + * @return JsonApiActiveDataProvider|object |
| 124 | + * @throws \insolita\fractal\exceptions\ValidationException |
| 125 | + * @throws \yii\base\InvalidConfigException |
| 126 | + */ |
| 127 | + protected function makeDataProvider() |
| 128 | + { |
| 129 | + $requestParams = Yii::$app->getRequest()->getBodyParams(); |
| 130 | + if (empty($requestParams)) { |
| 131 | + $requestParams = Yii::$app->getRequest()->getQueryParams(); |
| 132 | + } |
| 133 | + $filter = $this->prepareDataFilter($requestParams); |
| 134 | + |
| 135 | + /* @var $modelClass \yii\db\BaseActiveRecord */ |
| 136 | + $modelClass = $this->modelClass; |
| 137 | + $query = $this->prepareParentQuery($modelClass::find()); |
| 138 | + $query = $this->prepareIncludeQuery($query); |
| 139 | + |
| 140 | + |
| 141 | + if (!empty($filter)) { |
| 142 | + $query->andWhere($filter); |
| 143 | + } |
| 144 | + |
| 145 | + $dataProvider = Yii::createObject($this->dataProvider); |
| 146 | + if (!$dataProvider instanceof JsonApiActiveDataProvider && !$dataProvider instanceof CursorActiveDataProvider) { |
| 147 | + throw new InvalidConfigException('Invalid dataProvider configuration'); |
| 148 | + } |
| 149 | + $dataProvider->query = $query; |
| 150 | + $dataProvider->resourceKey = $this->resourceKey; |
| 151 | + $dataProvider->transformer = $this->transformer; |
| 152 | + $dataProvider->setSort(['params' => $requestParams]); |
| 153 | + |
| 154 | + if ($this->prepareDataProvider !== null) { |
| 155 | + return call_user_func($this->prepareDataProvider, $this, $dataProvider); |
| 156 | + } |
| 157 | + |
| 158 | + return $dataProvider; |
| 159 | + } |
| 160 | +} |
0 commit comments