|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (c) 2020 Insolita <[email protected]> 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\JsonApiActiveDataProvider; |
| 12 | +use Yii; |
| 13 | +use yii\db\ActiveQueryInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Provide ability for count resource items without data loading related to current user identity |
| 17 | + * (with filters support) |
| 18 | + * Return header X-Pagination-Total-Count with count value (Use with HEAD request) |
| 19 | + * @example |
| 20 | + * count posts |
| 21 | + * Post::find()->where(['author_id' => Yii::$app->userId])->andWhere([...filter condition])->count(); |
| 22 | + * 'count' => [ |
| 23 | + * 'class' => CountAction::class, |
| 24 | + * 'modelClass' => Post::class, |
| 25 | + * 'dataFilter' => PostDataFilter::class, |
| 26 | + * 'userIdAttribute' => 'author_id' |
| 27 | + * ], |
| 28 | +**/ |
| 29 | +class CountForIdentityAction extends JsonApiAction |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + * user foreign key attribute |
| 34 | + */ |
| 35 | + public $userIdAttribute = 'user_id'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var callable |
| 39 | + * @example |
| 40 | + * 'queryWrapper' => function(CountAction $action, ActiveQuery Query) { |
| 41 | + * Modify $query |
| 42 | + * or return own ActiveQuery |
| 43 | + * } |
| 44 | + */ |
| 45 | + public $queryWrapper; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var \yii\data\DataFilter |
| 49 | + */ |
| 50 | + public $dataFilter; |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * @return \insolita\fractal\providers\JsonApiActiveDataProvider|object |
| 55 | + * @throws \insolita\fractal\exceptions\ValidationException |
| 56 | + * @throws \yii\base\InvalidConfigException |
| 57 | + */ |
| 58 | + public function run() |
| 59 | + { |
| 60 | + if ($this->checkAccess) { |
| 61 | + call_user_func($this->checkAccess, $this->id); |
| 62 | + } |
| 63 | + |
| 64 | + $query = $this->makeQuery(); |
| 65 | + if ($this->queryWrapper !== null) { |
| 66 | + $query = \call_user_func($this->queryWrapper, $this, $query); |
| 67 | + } |
| 68 | + $count = $query->count(); |
| 69 | + Yii::$app->response->headers->set('X-Pagination-Total-Count', $count); |
| 70 | + Yii::$app->response->setStatusCode(204); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param $requestParams |
| 75 | + * @return array|bool|mixed|null |
| 76 | + * @throws \insolita\fractal\exceptions\ValidationException |
| 77 | + * @throws \yii\base\InvalidConfigException |
| 78 | + */ |
| 79 | + protected function prepareDataFilter($requestParams) |
| 80 | + { |
| 81 | + if ($this->dataFilter === null) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + $this->dataFilter = Yii::createObject($this->dataFilter); |
| 85 | + if ($this->dataFilter->load($requestParams)) { |
| 86 | + $filter = $this->dataFilter->build(); |
| 87 | + if ($filter === false) { |
| 88 | + throw new ValidationException($this->dataFilter->getErrors()); |
| 89 | + } |
| 90 | + return $filter; |
| 91 | + } |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Add condition for parent model restriction if needed |
| 97 | + * @param \yii\db\ActiveQueryInterface $query |
| 98 | + * @return \yii\db\ActiveQueryInterface |
| 99 | + */ |
| 100 | + protected function prepareParentQuery(ActiveQueryInterface $query):ActiveQueryInterface |
| 101 | + { |
| 102 | + $condition[$this->modelTable().'.'.$this->userIdAttribute] = Yii::$app->user->id; |
| 103 | + $query->where($condition); |
| 104 | + return $query; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * @return JsonApiActiveDataProvider|object |
| 110 | + * @throws \insolita\fractal\exceptions\ValidationException |
| 111 | + * @throws \yii\base\InvalidConfigException |
| 112 | + */ |
| 113 | + protected function makeQuery() |
| 114 | + { |
| 115 | + $requestParams = Yii::$app->getRequest()->getBodyParams(); |
| 116 | + if (empty($requestParams)) { |
| 117 | + $requestParams = Yii::$app->getRequest()->getQueryParams(); |
| 118 | + } |
| 119 | + $filter = $this->prepareDataFilter($requestParams); |
| 120 | + |
| 121 | + /* @var $modelClass \yii\db\BaseActiveRecord */ |
| 122 | + $modelClass = $this->modelClass; |
| 123 | + $query = $this->prepareParentQuery($modelClass::find()); |
| 124 | + |
| 125 | + if (!empty($filter)) { |
| 126 | + $query->andWhere($filter); |
| 127 | + } |
| 128 | + return $query; |
| 129 | + } |
| 130 | +} |
0 commit comments