1 | <?php |
||
2 | /** |
||
3 | * BEdita, API-first content management framework |
||
4 | * Copyright 2021 ChannelWeb Srl, Chialab Srl |
||
5 | * |
||
6 | * This file is part of BEdita: you can redistribute it and/or modify |
||
7 | * it under the terms of the GNU Lesser General Public License as published |
||
8 | * by the Free Software Foundation, either version 3 of the License, or |
||
9 | * (at your option) any later version. |
||
10 | * |
||
11 | * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
||
12 | */ |
||
13 | namespace App\Controller\Component; |
||
14 | |||
15 | use Cake\Controller\Component; |
||
16 | use Cake\Core\Configure; |
||
17 | use Cake\Utility\Hash; |
||
18 | |||
19 | /** |
||
20 | * Handles query strings. |
||
21 | */ |
||
22 | class QueryComponent extends Component |
||
23 | { |
||
24 | /** |
||
25 | * Retrieve `index` module query string array |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | public function index(): array |
||
30 | { |
||
31 | $query = $this->getController()->getRequest()->getQueryParams(); |
||
32 | if (array_key_exists('sort', $query)) { |
||
33 | $sort = (string)Hash::get($query, 'sort'); |
||
34 | $this->handleSort($sort, $query); |
||
35 | } |
||
36 | $query = $this->handleInclude($query); |
||
37 | |||
38 | // make sure `filter[history_editor]` is empty in order to use logged user id |
||
39 | if (isset($query['filter']['history_editor'])) { |
||
40 | $query['filter']['history_editor'] = ''; |
||
41 | } |
||
42 | |||
43 | // return URL query string if `filter`, `sort`, or `q` are set |
||
44 | if (!empty(array_intersect_key($query, array_flip(['filter', 'sort', 'q'])))) { |
||
45 | return $query; |
||
46 | } |
||
47 | |||
48 | // set sort order: use `currentModule.sort` or default '-id' |
||
49 | $module = (array)$this->getController()->viewBuilder()->getVar('currentModule'); |
||
50 | $sort = (string)Hash::get($module, 'sort'); |
||
51 | $this->handleSort($sort, $query); |
||
52 | |||
53 | return $query; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Handle include parameter |
||
58 | * |
||
59 | * @param array $query Query string |
||
60 | * @return array |
||
61 | */ |
||
62 | protected function handleInclude(array $query): array |
||
63 | { |
||
64 | $decoded = []; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
65 | $include = array_filter( |
||
66 | (array)Configure::read( |
||
67 | sprintf( |
||
68 | 'Properties.%s.index', |
||
69 | $this->getController()->getRequest()->getParam('object_type') |
||
70 | ) |
||
71 | ), |
||
72 | function ($value) { |
||
73 | return is_array($value); |
||
74 | } |
||
75 | ); |
||
76 | $decoded = empty($include) ? $include : array_keys(reset($include)); |
||
77 | if ($this->getConfig('include') != null) { |
||
78 | $decoded = array_unique( |
||
79 | array_merge( |
||
80 | $decoded, |
||
81 | explode(',', (string)$this->getConfig('include')) |
||
82 | ) |
||
83 | ); |
||
84 | } |
||
85 | if (!empty($decoded)) { |
||
86 | $query['include'] = implode(',', $decoded); |
||
87 | } |
||
88 | |||
89 | return $query; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Handle sort order |
||
94 | * |
||
95 | * @param string $sort Sort order |
||
96 | * @param array $query Query string |
||
97 | * @return void |
||
98 | */ |
||
99 | protected function handleSort(string $sort, array &$query): void |
||
100 | { |
||
101 | // remove sort from query if `q` search is set: order is done by search engine |
||
102 | if (!empty($query['q'])) { |
||
103 | unset($query['sort']); |
||
104 | |||
105 | return; |
||
106 | } |
||
107 | // set sort order from query string or default '-id' |
||
108 | $query['sort'] = !empty($sort) ? $sort : '-id'; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Prepare query string to make BE4 API call |
||
113 | * |
||
114 | * @param array $query Input query string |
||
115 | * @return array |
||
116 | */ |
||
117 | public function prepare(array $query): array |
||
118 | { |
||
119 | // cleanup `filter`, remove empty keys |
||
120 | $filter = array_filter((array)Hash::get($query, 'filter')); |
||
121 | $remove = array_flip(['count', 'page_items', 'page_count', 'filter']); |
||
122 | $query = array_diff_key($query, $remove); |
||
123 | if (!empty($filter)) { |
||
124 | $query += compact('filter'); |
||
125 | } |
||
126 | |||
127 | return $query; |
||
128 | } |
||
129 | } |
||
130 |