Total Complexity | 83 |
Total Lines | 399 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like onetableDocLister often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use onetableDocLister, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class onetableDocLister extends DocLister |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $table = ''; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $idField = 'id'; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $parentField = 'parent'; |
||
28 | |||
29 | /** |
||
30 | * @absctract |
||
31 | */ |
||
32 | public function getDocs($tvlist = '') |
||
33 | { |
||
34 | if ($this->checkExtender('paginate')) { |
||
35 | $this->extender['paginate']->init($this); |
||
36 | } |
||
37 | $type = $this->getCFGDef('idType', 'parents'); |
||
38 | $this->_docs = ($type == 'parents') ? $this->getChildrenList() : $this->getDocList(); |
||
39 | |||
40 | return $this->_docs; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $tpl |
||
45 | * @return string |
||
46 | */ |
||
47 | public function _render($tpl = '') |
||
48 | { |
||
49 | $out = ''; |
||
50 | $separator = $this->getCFGDef('outputSeparator', ''); |
||
51 | if ($tpl == '') { |
||
52 | $tpl = $this->getCFGDef('tpl', ''); |
||
53 | } |
||
54 | if ($tpl != '') { |
||
55 | $this->toPlaceholders(count($this->_docs), 1, "display"); // [+display+] - сколько показано на странице. |
||
56 | |||
57 | $i = 1; |
||
58 | $sysPlh = $this->renameKeyArr($this->_plh, $this->getCFGDef("sysKey", "dl")); |
||
59 | $noneTPL = $this->getCFGDef("noneTPL", ""); |
||
60 | if (count($this->_docs) == 0 && $noneTPL != '') { |
||
61 | $out = $this->parseChunk($noneTPL, $sysPlh); |
||
62 | } else { |
||
63 | /** |
||
64 | * @var $extUser user_DL_Extender |
||
65 | */ |
||
66 | if ($extUser = $this->getExtender('user')) { |
||
67 | $extUser->init($this, array('fields' => $this->getCFGDef("userFields", ""))); |
||
|
|||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @var $extSummary summary_DL_Extender |
||
72 | */ |
||
73 | $extSummary = $this->getExtender('summary'); |
||
74 | |||
75 | /** |
||
76 | * @var $extPrepare prepare_DL_Extender |
||
77 | */ |
||
78 | $extPrepare = $this->getExtender('prepare'); |
||
79 | $this->skippedDocs = 0; |
||
80 | foreach ($this->_docs as $item) { |
||
81 | $this->renderTPL = $tpl; |
||
82 | if ($extUser) { |
||
83 | $item = $extUser->setUserData($item); //[+user.id.createdby+], [+user.fullname.publishedby+], [+dl.user.publishedby+].... |
||
84 | } |
||
85 | |||
86 | $item[$this->getCFGDef("sysKey", "dl") . '.summary'] = $extSummary ? $this->getSummary($item, |
||
87 | $extSummary) : ''; |
||
88 | |||
89 | $item = array_merge($item, |
||
90 | $sysPlh); //inside the chunks available all placeholders set via $modx->toPlaceholders with prefix id, and with prefix sysKey |
||
91 | $item[$this->getCFGDef("sysKey", |
||
92 | "dl") . '.iteration'] = $i; //[+iteration+] - Number element. Starting from zero |
||
93 | |||
94 | $date = $this->getCFGDef('dateSource', 'pub_date'); |
||
95 | if (isset($item[$date])) { |
||
96 | $_date = is_numeric($item[$date]) && $item[$date] == (int)$item[$date] ? $item[$date] : strtotime($item[$date]); |
||
97 | if ($_date !== false) { |
||
98 | $_date = $_date + $this->modx->config['server_offset_time']; |
||
99 | $dateFormat = $this->getCFGDef('dateFormat', '%d.%b.%y %H:%M'); |
||
100 | if ($dateFormat) { |
||
101 | $item['date'] = strftime($dateFormat, $_date); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | $findTpl = $this->renderTPL; |
||
107 | $tmp = $this->uniformPrepare($item, $i); |
||
108 | extract($tmp, EXTR_SKIP); |
||
109 | if ($this->renderTPL == '') { |
||
110 | $this->renderTPL = $findTpl; |
||
111 | } |
||
112 | |||
113 | if ($extPrepare) { |
||
114 | $item = $extPrepare->init($this, array( |
||
115 | 'data' => $item, |
||
116 | 'nameParam' => 'prepare' |
||
117 | )); |
||
118 | if (is_bool($item) && $item === false) { |
||
119 | $this->skippedDocs++; |
||
120 | continue; |
||
121 | } |
||
122 | } |
||
123 | $tmp = $this->parseChunk($this->renderTPL, $item); |
||
124 | if ($this->getCFGDef('contentPlaceholder', 0) !== 0) { |
||
125 | $this->toPlaceholders($tmp, 1, |
||
126 | "item[" . $i . "]"); // [+item[x]+] – individual placeholder for each iteration documents on this page |
||
127 | } |
||
128 | $out .= $tmp; |
||
129 | if (next($this->_docs) !== false) { |
||
130 | $out .= $separator; |
||
131 | } |
||
132 | $i++; |
||
133 | } |
||
134 | } |
||
135 | $out = $this->renderWrap($out); |
||
136 | } |
||
137 | |||
138 | return $this->toPlaceholders($out); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param array $data |
||
143 | * @param mixed $fields |
||
144 | * @param array $array |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getJSON($data, $fields, $array = array()) |
||
148 | { |
||
149 | $out = array(); |
||
150 | $fields = is_array($fields) ? $fields : explode(",", $fields); |
||
151 | $date = $this->getCFGDef('dateSource', 'pub_date'); |
||
152 | |||
153 | /** |
||
154 | * @var $extSummary summary_DL_Extender |
||
155 | */ |
||
156 | $extSummary = $this->getExtender('summary'); |
||
157 | |||
158 | /** |
||
159 | * @var $extPrepare prepare_DL_Extender |
||
160 | */ |
||
161 | $extPrepare = $this->getExtender('prepare'); |
||
162 | |||
163 | /** |
||
164 | * @var $extE e_DL_Extender |
||
165 | */ |
||
166 | $extE = $this->getExtender('e', true, true); |
||
167 | foreach ($data as $num => $row) { |
||
168 | |||
169 | switch (true) { |
||
170 | case ((array('1') == $fields || in_array('summary', $fields)) && $extSummary): |
||
171 | $row['summary'] = $this->getSummary($row, $extSummary, 'introtext'); |
||
172 | //without break |
||
173 | case ((array('1') == $fields || in_array('date', $fields)) && $date != 'date'): |
||
174 | if (isset($row[$date])) { |
||
175 | $_date = is_numeric($row[$date]) && $row[$date] == (int)$row[$date] ? $row[$date] : strtotime($row[$date]); |
||
176 | if ($_date !== false) { |
||
177 | $_date = $_date + $this->modx->config['server_offset_time']; |
||
178 | $dateFormat = $this->getCFGDef('dateFormat', '%d.%b.%y %H:%M'); |
||
179 | if ($dateFormat) { |
||
180 | $row['date'] = strftime($dateFormat, $_date); |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | //nobreak |
||
185 | } |
||
186 | |||
187 | if ($extE && $tmp = $extE->init($this, array('data' => $row))) { |
||
188 | if (is_array($tmp)) { |
||
189 | $row = $tmp; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | if ($extPrepare) { |
||
194 | $row = $extPrepare->init($this, array('data' => $row)); |
||
195 | if (is_bool($row) && $row === false) { |
||
196 | continue; |
||
197 | } |
||
198 | } |
||
199 | $out[$num] = $row; |
||
200 | } |
||
201 | |||
202 | return parent::getJSON($out, $fields, $out); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return array |
||
207 | */ |
||
208 | protected function getDocList() |
||
209 | { |
||
210 | $out = array(); |
||
211 | $sanitarInIDs = $this->sanitarIn($this->IDs); |
||
212 | if ($sanitarInIDs != "''" || $this->getCFGDef('ignoreEmpty', '0')) { |
||
213 | $from = $this->table . " " . $this->_filters['join']; |
||
214 | $where = $this->getCFGDef('addWhereList', ''); |
||
215 | |||
216 | //====== block added by Dreamer to enable filters ====== |
||
217 | $where = ($where ? $where . ' AND ' : '') . $this->_filters['where']; |
||
218 | $where = sqlHelper::trimLogicalOp($where); |
||
219 | //------- end of block ------- |
||
220 | |||
221 | |||
222 | if ($where != '') { |
||
223 | $where = array($where); |
||
224 | } else { |
||
225 | $where = array(); |
||
226 | } |
||
227 | if ($sanitarInIDs != "''") { |
||
228 | $where[] = "{$this->getPK()} IN ({$sanitarInIDs})"; |
||
229 | } |
||
230 | |||
231 | if (!empty($where)) { |
||
232 | $where = "WHERE " . implode(" AND ", $where); |
||
233 | } else { |
||
234 | $where = ''; |
||
235 | } |
||
236 | |||
237 | $limit = $this->LimitSQL($this->getCFGDef('queryLimit', 0)); |
||
238 | $fields = $this->getCFGDef('selectFields', '*'); |
||
239 | $group = $this->getGroupSQL($this->getCFGDef('groupBy', '')); |
||
240 | $sort = $this->SortOrderSQL($this->getPK()); |
||
241 | $rs = $this->dbQuery("SELECT {$fields} FROM {$from} {$where} {$group} {$sort} {$limit}"); |
||
242 | |||
243 | $pk = $this->getPK(false); |
||
244 | while ($item = $this->modx->db->getRow($rs)) { |
||
245 | $out[$item[$pk]] = $item; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | return $out; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function getChildrenList() |
||
256 | { |
||
257 | $where = array(); |
||
258 | $out = array(); |
||
259 | $from = $this->table . " " . $this->_filters['join']; |
||
260 | $tmpWhere = $this->getCFGDef('addWhereList', ''); |
||
261 | $tmpWhere = sqlHelper::trimLogicalOp($tmpWhere); |
||
262 | |||
263 | //====== block added by Dreamer to enable filters ====== |
||
264 | $tmpWhere = ($tmpWhere ? $tmpWhere . ' AND ' : '') . $this->_filters['where']; |
||
265 | $tmpWhere = sqlHelper::trimLogicalOp($tmpWhere); |
||
266 | //------- end of block ------- |
||
267 | |||
268 | |||
269 | if (!empty($tmpWhere)) { |
||
270 | $where[] = $tmpWhere; |
||
271 | } |
||
272 | $sanitarInIDs = $this->sanitarIn($this->IDs); |
||
273 | |||
274 | $tmpWhere = null; |
||
275 | if ($sanitarInIDs != "''") { |
||
276 | $tmpWhere = "({$this->getParentField()} IN (" . $sanitarInIDs . ")"; |
||
277 | switch ($this->getCFGDef('showParent', '0')) { |
||
278 | case -1: |
||
279 | $tmpWhere .= ")"; |
||
280 | break; |
||
281 | case 0: |
||
282 | $tmpWhere .= " AND {$this->getPK()} NOT IN(" . $sanitarInIDs . "))"; |
||
283 | break; |
||
284 | case 1: |
||
285 | default: |
||
286 | $tmpWhere .= " OR {$this->getPK()} IN({$sanitarInIDs}))"; |
||
287 | break; |
||
288 | } |
||
289 | } |
||
290 | if (($addDocs = $this->getCFGDef('documents', '')) != '') { |
||
291 | $addDocs = $this->sanitarIn($this->cleanIDs($addDocs)); |
||
292 | if (empty($tmpWhere)) { |
||
293 | $tmpWhere = $this->getPK() . " IN({$addDocs})"; |
||
294 | } else { |
||
295 | $tmpWhere = "((" . $tmpWhere . ") OR {$this->getPK()} IN({$addDocs}))"; |
||
296 | } |
||
297 | } |
||
298 | if (!empty($tmpWhere)) { |
||
299 | $where[] = $tmpWhere; |
||
300 | } |
||
301 | if (!empty($where)) { |
||
302 | $where = "WHERE " . implode(" AND ", $where); |
||
303 | } else { |
||
304 | $where = ''; |
||
305 | } |
||
306 | $fields = $this->getCFGDef('selectFields', '*'); |
||
307 | $group = $this->getGroupSQL($this->getCFGDef('groupBy', '')); |
||
308 | $sort = $this->SortOrderSQL($this->getPK()); |
||
309 | $limit = $this->LimitSQL($this->getCFGDef('queryLimit', 0)); |
||
310 | if ($sanitarInIDs != "''" || $this->getCFGDef('ignoreEmpty', '0')) { |
||
311 | $rs = $this->dbQuery("SELECT {$fields} FROM {$from} {$where} {$group} {$sort} {$limit}"); |
||
312 | |||
313 | $pk = $this->getPK(false); |
||
314 | |||
315 | while ($item = $this->modx->db->getRow($rs)) { |
||
316 | $out[$item[$pk]] = $item; |
||
317 | } |
||
318 | |||
319 | } |
||
320 | |||
321 | return $out; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @absctract |
||
326 | */ |
||
327 | public function getChildrenCount() |
||
328 | { |
||
329 | $out = 0; |
||
330 | $sanitarInIDs = $this->sanitarIn($this->IDs); |
||
331 | if ($sanitarInIDs != "''" || $this->getCFGDef('ignoreEmpty', '0')) { |
||
332 | $from = $this->table . " " . $this->_filters['join']; |
||
333 | $where = $this->getCFGDef('addWhereList', ''); |
||
334 | |||
335 | //====== block added by Dreamer ====== |
||
336 | $where = ($where ? $where . ' AND ' : '') . $this->_filters['where']; |
||
337 | $where = sqlHelper::trimLogicalOp($where); |
||
338 | //------- end of block ------- |
||
339 | |||
340 | if ($where != '') { |
||
341 | $where = array($where); |
||
342 | } else { |
||
343 | $where = array(); |
||
344 | } |
||
345 | if ($sanitarInIDs != "''") { |
||
346 | if ($sanitarInIDs != "''") { |
||
347 | switch ($this->getCFGDef('idType', 'parents')) { |
||
348 | case 'parents': |
||
349 | switch ($this->getCFGDef('showParent', '0')) { |
||
350 | case '-1': |
||
351 | $tmpWhere = "{$this->getParentField()} IN ({$sanitarInIDs})"; |
||
352 | break; |
||
353 | case 0: |
||
354 | $tmpWhere = "{$this->getParentField()} IN ({$sanitarInIDs}) AND {$this->getPK()} NOT IN({$sanitarInIDs})"; |
||
355 | break; |
||
356 | case 1: |
||
357 | default: |
||
358 | $tmpWhere = "({$this->getParentField()} IN ({$sanitarInIDs}) OR {$this->getPK()} IN({$sanitarInIDs}))"; |
||
359 | break; |
||
360 | } |
||
361 | if (($addDocs = $this->getCFGDef('documents', '')) != '') { |
||
362 | $addDocs = $this->sanitarIn($this->cleanIDs($addDocs)); |
||
363 | $where[] = "((" . $tmpWhere . ") OR {$this->getPK()} IN({$addDocs}))"; |
||
364 | } else { |
||
365 | $where[] = $tmpWhere; |
||
366 | } |
||
367 | break; |
||
368 | case 'documents': |
||
369 | $where[] = "{$this->getPK()} IN({$sanitarInIDs})"; |
||
370 | break; |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | if (!empty($where)) { |
||
375 | $where = "WHERE " . implode(" AND ", $where); |
||
376 | } else { |
||
377 | $where = ''; |
||
378 | } |
||
379 | |||
380 | $group = $this->getGroupSQL($this->getCFGDef('groupBy', $this->getPK())); |
||
381 | $maxDocs = $this->getCFGDef('maxDocs', 0); |
||
382 | $limit = $maxDocs > 0 ? $this->LimitSQL($this->getCFGDef('maxDocs', 0)) : ''; |
||
383 | $rs = ("SELECT count(*) FROM (SELECT count(*) FROM {$from} {$where} {$group} {$limit}) as `tmp`"); |
||
384 | $out = $this->modx->db->getValue($rs); |
||
385 | } |
||
386 | |||
387 | return $out; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param string|array $id |
||
392 | * @return array |
||
393 | */ |
||
394 | public function getChildrenFolder($id) |
||
412 | } |
||
413 | } |
||
414 |