Conditions | 24 |
Paths | 11522 |
Total Lines | 140 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 3 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | public function autocomplete(HTTPRequest $request): HTTPResponse |
||
73 | { |
||
74 | if ($this->isDisabled() || $this->isReadonly()) { |
||
75 | return $this->httpError(403); |
||
76 | } |
||
77 | |||
78 | // CSRF check |
||
79 | $token = $this->getForm()->getSecurityToken(); |
||
80 | if (!$token->checkRequest($request)) { |
||
81 | return $this->httpError(400, "Invalid token"); |
||
82 | } |
||
83 | |||
84 | $name = $this->getName(); |
||
85 | |||
86 | $vars = $this->getServerVars(); |
||
87 | |||
88 | // Don't use by default % at the start of term as it prevents use of indexes |
||
89 | $term = $request->getVar($vars['queryParam']) . '%'; |
||
90 | $term = str_replace(' ', '%', $term); |
||
91 | if ($this->ajaxFullSearch) { |
||
92 | $term = "%" . $term; |
||
93 | } |
||
94 | |||
95 | $class = $this->ajaxClass; |
||
96 | |||
97 | $sng = $class::singleton(); |
||
98 | $baseTable = $sng->baseTable(); |
||
99 | |||
100 | $searchField = ''; |
||
101 | $searchCandidates = [ |
||
102 | 'Title', |
||
103 | 'Name', |
||
104 | 'Surname', |
||
105 | 'Email', |
||
106 | 'ID' |
||
107 | ]; |
||
108 | |||
109 | // Ensure field exists, this is really rudimentary |
||
110 | $db = $class::config()->db; |
||
111 | foreach ($searchCandidates as $searchCandidate) { |
||
112 | if ($searchField) { |
||
113 | continue; |
||
114 | } |
||
115 | if (isset($db[$searchCandidate])) { |
||
116 | $searchField = $searchCandidate; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | $searchCols = [$searchField]; |
||
121 | |||
122 | // For Surname, do something better |
||
123 | if ($searchField == "Surname") { |
||
124 | // Show first name, surname |
||
125 | if (isset($db['FirstName'])) { |
||
126 | $searchField = ['FirstName', 'Surname']; |
||
127 | $searchCols = ['FirstName', 'Surname']; |
||
128 | } |
||
129 | // Also search email |
||
130 | if (isset($db['Email'])) { |
||
131 | $searchCols = ['FirstName', 'Surname', 'Email']; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | |||
136 | if ($this->customSearchField) { |
||
137 | $searchField = $this->customSearchField; |
||
138 | } |
||
139 | if ($this->customSearchCols) { |
||
140 | $searchCols = $this->customSearchCols; |
||
141 | } |
||
142 | |||
143 | /** @var DataList $list */ |
||
144 | $list = $sng::get(); |
||
145 | |||
146 | // Make sure at least one field is not null... |
||
147 | $where = []; |
||
148 | foreach ($searchCols as $searchCol) { |
||
149 | $where[] = $searchCol . ' IS NOT NULL'; |
||
150 | } |
||
151 | $list = $list->whereAny($where); |
||
152 | // ... and matches search term ... |
||
153 | $where = []; |
||
154 | foreach ($searchCols as $searchCol) { |
||
155 | $where[$searchCol . ' LIKE ?'] = $term; |
||
156 | } |
||
157 | $list = $list->whereAny($where); |
||
158 | // ... and any user set requirements |
||
159 | $filters = $this->ajaxFilters; |
||
160 | if (!empty($filters)) { |
||
161 | $list = $list->filter($filters); |
||
162 | } |
||
163 | $where = $this->ajaxWhere; |
||
164 | if (!empty($where)) { |
||
165 | // Deal with in clause |
||
166 | if (is_string($where)) { |
||
167 | $customWhere = $where; |
||
168 | } else { |
||
169 | $customWhere = []; |
||
170 | foreach ($where as $col => $param) { |
||
171 | // For array, we need a IN statement with a ? for each value |
||
172 | if (is_array($param)) { |
||
173 | $prepValue = []; |
||
174 | $params = []; |
||
175 | foreach ($param as $paramValue) { |
||
176 | $params[] = $paramValue; |
||
177 | $prepValue[] = "?"; |
||
178 | } |
||
179 | $customWhere["$col IN (" . implode(',', $prepValue) . ")"] = $params; |
||
180 | } else { |
||
181 | $customWhere["$col = ?"] = $param; |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | $list = $list->where($customWhere); |
||
186 | } |
||
187 | |||
188 | $list = $list->limit($this->recordsLimit); |
||
189 | |||
190 | $results = iterator_to_array($list); |
||
191 | $data = []; |
||
192 | foreach ($results as $record) { |
||
193 | if (is_array($searchField)) { |
||
194 | $labelParts = []; |
||
195 | foreach ($searchField as $sf) { |
||
196 | $labelParts[] = $record->$sf ?? ""; |
||
197 | } |
||
198 | $label = implode(" ", $labelParts); |
||
199 | } else { |
||
200 | $label = $record->$searchField ?? ""; |
||
201 | } |
||
202 | $data[] = [ |
||
203 | $vars['valueField'] => $record->ID, |
||
204 | $vars['labelField'] => $label ?? "(no label)", |
||
205 | ]; |
||
206 | } |
||
207 | $body = json_encode([$vars['dataKey'] => $data]); |
||
208 | |||
209 | $response = new HTTPResponse($body); |
||
210 | $response->addHeader('Content-Type', 'application/json'); |
||
211 | return $response; |
||
212 | } |
||
472 |