Total Complexity | 222 |
Total Lines | 1110 |
Duplicated Lines | 0 % |
Changes | 35 | ||
Bugs | 20 | Features | 0 |
Complex classes like modResource 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 modResource, and based on these observations, apply Extract Interface, too.
1 | <<<<<<< HEAD |
||
7 | */ |
||
8 | class modResource extends MODxAPI |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $mode = 'new'; |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $default_field = array( |
||
18 | 'type' => 'document', |
||
19 | 'contentType' => 'text/html', |
||
20 | 'pagetitle' => 'New document', |
||
21 | 'longtitle' => '', |
||
22 | 'description' => '', |
||
23 | 'alias' => '', |
||
24 | 'link_attributes' => '', |
||
25 | 'published' => 1, |
||
26 | 'pub_date' => 0, |
||
27 | 'unpub_date' => 0, |
||
28 | 'parent' => 0, |
||
29 | 'isfolder' => 0, |
||
30 | 'introtext' => '', |
||
31 | 'content' => '', |
||
32 | 'richtext' => 1, |
||
33 | 'template' => 0, |
||
34 | 'menuindex' => 0, |
||
35 | 'searchable' => 1, |
||
36 | 'cacheable' => 1, |
||
37 | 'createdon' => 0, |
||
38 | 'createdby' => 0, |
||
39 | 'editedon' => 0, |
||
40 | 'editedby' => 0, |
||
41 | 'deleted' => 0, |
||
42 | 'deletedon' => 0, |
||
43 | 'deletedby' => 0, |
||
44 | 'publishedon' => 0, |
||
45 | 'publishedby' => 0, |
||
46 | 'menutitle' => '', |
||
47 | 'donthit' => 0, |
||
48 | 'privateweb' => 0, |
||
49 | 'privatemgr' => 0, |
||
50 | 'content_dispo' => 0, |
||
51 | 'hidemenu' => 0, |
||
52 | 'alias_visible' => 1 |
||
53 | ); |
||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $table = array( |
||
58 | '"' => '_', |
||
59 | "'" => '_', |
||
60 | ' ' => '_', |
||
61 | '.' => '_', |
||
62 | ',' => '_', |
||
63 | 'а' => 'a', |
||
64 | 'б' => 'b', |
||
65 | 'в' => 'v', |
||
66 | 'г' => 'g', |
||
67 | 'д' => 'd', |
||
68 | 'е' => 'e', |
||
69 | 'ё' => 'e', |
||
70 | 'ж' => 'zh', |
||
71 | 'з' => 'z', |
||
72 | 'и' => 'i', |
||
73 | 'й' => 'y', |
||
74 | 'к' => 'k', |
||
75 | 'л' => 'l', |
||
76 | 'м' => 'm', |
||
77 | 'н' => 'n', |
||
78 | 'о' => 'o', |
||
79 | 'п' => 'p', |
||
80 | 'р' => 'r', |
||
81 | 'с' => 's', |
||
82 | 'т' => 't', |
||
83 | 'у' => 'u', |
||
84 | 'ф' => 'f', |
||
85 | 'х' => 'h', |
||
86 | 'ц' => 'c', |
||
87 | 'ч' => 'ch', |
||
88 | 'ш' => 'sh', |
||
89 | 'щ' => 'sch', |
||
90 | 'ь' => '', |
||
91 | 'ы' => 'y', |
||
92 | 'ъ' => '', |
||
93 | 'э' => 'e', |
||
94 | 'ю' => 'yu', |
||
95 | 'я' => 'ya', |
||
96 | 'А' => 'A', |
||
97 | 'Б' => 'B', |
||
98 | 'В' => 'V', |
||
99 | 'Г' => 'G', |
||
100 | 'Д' => 'D', |
||
101 | 'Е' => 'E', |
||
102 | 'Ё' => 'E', |
||
103 | 'Ж' => 'Zh', |
||
104 | 'З' => 'Z', |
||
105 | 'И' => 'I', |
||
106 | 'Й' => 'Y', |
||
107 | 'К' => 'K', |
||
108 | 'Л' => 'L', |
||
109 | 'М' => 'M', |
||
110 | 'Н' => 'N', |
||
111 | 'О' => 'O', |
||
112 | 'П' => 'P', |
||
113 | 'Р' => 'R', |
||
114 | 'С' => 'S', |
||
115 | 'Т' => 'T', |
||
116 | 'У' => 'U', |
||
117 | 'Ф' => 'F', |
||
118 | 'Х' => 'H', |
||
119 | 'Ц' => 'C', |
||
120 | 'Ч' => 'Ch', |
||
121 | 'Ш' => 'Sh', |
||
122 | 'Щ' => 'Sch', |
||
123 | 'Ь' => '', |
||
124 | 'Ы' => 'Y', |
||
125 | 'Ъ' => '', |
||
126 | 'Э' => 'E', |
||
127 | 'Ю' => 'Yu', |
||
128 | 'Я' => 'Ya', |
||
129 | ); |
||
130 | /** |
||
131 | * @var array массив ТВшек где name это ключ массива, а ID это значение |
||
132 | */ |
||
133 | private $tv = array(); |
||
134 | /** |
||
135 | * @var array массив ТВшек где ID это ключ массива, а name это значение |
||
136 | */ |
||
137 | private $tvid = array(); |
||
138 | /** |
||
139 | * @var array значения по умолчанию для ТВ параметров |
||
140 | */ |
||
141 | private $tvd = array(); |
||
142 | |||
143 | /** @var array связи ТВ и шаблонов */ |
||
144 | private $tvTpl = array(); |
||
145 | |||
146 | /** @var array параметры ТВ с массивами */ |
||
147 | protected $tvaFields = array(); |
||
148 | |||
149 | /** |
||
150 | * Массив администраторов |
||
151 | * @var DLCollection |
||
152 | */ |
||
153 | private $managerUsers = null; |
||
154 | /** @var array группы документов */ |
||
155 | protected $groupIds = array(); |
||
156 | |||
157 | /** |
||
158 | * modResource constructor. |
||
159 | * @param DocumentParser $modx |
||
160 | * @param bool $debug |
||
161 | */ |
||
162 | public function __construct($modx, $debug = false) |
||
163 | { |
||
164 | parent::__construct($modx, $debug); |
||
165 | $this->get_TV(); |
||
166 | $uTable = $this->makeTable("manager_users"); |
||
167 | $aTable = $this->makeTable("user_attributes"); |
||
168 | $query = "SELECT `u`.`id`, `a`.`email`, `u`.`username` FROM " . $aTable . " as `a` LEFT JOIN " . $uTable . " as `u` ON `u`.`id`=`a`.`internalKey`"; |
||
169 | $query = $this->query($query); |
||
170 | $this->managerUsers = new DLCollection($modx, empty($query) ? array() : $query); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return array |
||
175 | */ |
||
176 | public function toArrayMain() |
||
177 | { |
||
178 | $out = array_intersect_key(parent::toArray(), $this->default_field); |
||
179 | |||
180 | return $out; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param bool $render |
||
185 | * @return array |
||
186 | */ |
||
187 | public function toArrayTV($render = false) |
||
188 | { |
||
189 | $out = array_diff_key(parent::toArray(), $this->default_field); |
||
190 | $tpl = $this->get('template'); |
||
191 | $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array()); |
||
192 | foreach ($tvTPL as $item) { |
||
193 | if (isset($this->tvid[$item]) && !array_key_exists($this->tvid[$item], $out)) { |
||
194 | $value = $this->get($this->tvid[$item]); |
||
195 | $out[$this->tvid[$item]] = empty($value) ? $this->tvd[$this->tvid[$item]] : $value; |
||
196 | } |
||
197 | |||
198 | } |
||
199 | if ($render) { |
||
200 | foreach ($out as $key => $val) { |
||
201 | $out[$key] = $this->renderTV($key); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return $out; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param string $prefix |
||
210 | * @param string $suffix |
||
211 | * @param string $sep |
||
212 | * @param bool $render |
||
213 | * @return array |
||
214 | */ |
||
215 | public function toArray($prefix = '', $suffix = '', $sep = '_', $render = true) |
||
216 | { |
||
217 | $out = array_merge( |
||
218 | $this->toArrayMain(), |
||
219 | $this->toArrayTV($render), |
||
220 | array($this->fieldPKName() => $this->getID()) |
||
221 | ); |
||
222 | |||
223 | return \APIhelpers::renameKeyArr($out, $prefix, $suffix, $sep); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return null|string |
||
228 | */ |
||
229 | public function getUrl() |
||
230 | { |
||
231 | $out = null; |
||
232 | $id = (int)$this->getID(); |
||
233 | if (!empty($id)) { |
||
234 | $out = $this->modx->makeUrl($id); |
||
235 | } |
||
236 | |||
237 | return $out; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $main |
||
242 | * @param string $second |
||
243 | * @return mixed |
||
244 | */ |
||
245 | public function getTitle($main = 'menutitle', $second = 'pagetitle') |
||
246 | { |
||
247 | $title = $this->get($main); |
||
248 | if (empty($title) && $title !== '0') { |
||
249 | $title = $this->get($second); |
||
250 | } |
||
251 | |||
252 | return $title; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function isWebShow() |
||
259 | { |
||
260 | $pub = ($this->get('publishedon') < time() && $this->get('published')); |
||
261 | $unpub = ($this->get('unpub_date') == 0 || $this->get('unpub_date') > time()); |
||
262 | $del = ($this->get('deleted') == 0 && ($this->get('deletedon') == 0 || $this->get('deletedon') > time())); |
||
263 | |||
264 | return ($pub && $unpub && $del); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @return $this |
||
269 | */ |
||
270 | public function touch() |
||
271 | { |
||
272 | $this->set('editedon', time()); |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param $tvname |
||
279 | * @return null|string |
||
280 | */ |
||
281 | public function renderTV($tvname) |
||
282 | { |
||
283 | $out = null; |
||
284 | if ($this->getID() > 0) { |
||
285 | include_once MODX_MANAGER_PATH . "includes/tmplvars.format.inc.php"; |
||
286 | include_once MODX_MANAGER_PATH . "includes/tmplvars.commands.inc.php"; |
||
287 | $tvval = $this->get($tvname); |
||
288 | if ($this->isTVarrayField($tvname) && is_array($tvval)) { |
||
289 | $tvval = implode('||', $tvval); |
||
290 | } |
||
291 | $param = APIHelpers::getkey($this->tvd, $tvname, array()); |
||
292 | $display = APIHelpers::getkey($param, 'display', ''); |
||
293 | $display_params = APIHelpers::getkey($param, 'display_params', ''); |
||
294 | $type = APIHelpers::getkey($param, 'type', ''); |
||
295 | $out = getTVDisplayFormat($tvname, $tvval, $display, $display_params, $type, $this->getID(), ''); |
||
296 | } |
||
297 | |||
298 | return $out; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param $key |
||
303 | * @return mixed |
||
304 | */ |
||
305 | public function get($key) |
||
306 | { |
||
307 | $out = parent::get($key); |
||
308 | if (isset($this->tv[$key])) { |
||
309 | $tpl = $this->get('template'); |
||
310 | $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array()); |
||
311 | $tvID = APIHelpers::getkey($this->tv, $key, 0); |
||
312 | if (in_array($tvID, $tvTPL) && is_null($out)) { |
||
313 | $out = APIHelpers::getkey($this->tvd, $key, null); |
||
314 | } |
||
315 | } |
||
316 | |||
317 | return $out; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @param $key |
||
322 | * @param $value |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function set($key, $value) |
||
326 | { |
||
327 | if ((is_scalar($value) || $this->isTVarrayField($key) || $this->isJsonField($key)) && is_scalar($key) && !empty($key)) { |
||
328 | switch ($key) { |
||
329 | case 'parent': |
||
330 | $value = (int)$value; |
||
331 | break; |
||
332 | case 'template': |
||
333 | $value = trim($value); |
||
334 | $value = $this->setTemplate($value); |
||
335 | break; |
||
336 | case 'published': |
||
337 | $value = (int)((bool)$value); |
||
338 | if ($value) { |
||
339 | $this->field['publishedon'] = time() + $this->modxConfig('server_offset_time'); |
||
340 | } |
||
341 | break; |
||
342 | case 'pub_date': |
||
343 | $value = $this->getTime($value); |
||
344 | if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) { |
||
345 | $this->field['published'] = 1; |
||
346 | $this->field['publishedon'] = $value; |
||
347 | } |
||
348 | break; |
||
349 | case 'unpub_date': |
||
350 | $value = $this->getTime($value); |
||
351 | if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) { |
||
352 | $this->field['published'] = 0; |
||
353 | $this->field['publishedon'] = 0; |
||
354 | } |
||
355 | break; |
||
356 | case 'deleted': |
||
357 | $value = (int)((bool)$value); |
||
358 | if ($value) { |
||
359 | $this->field['deletedon'] = time() + $this->modxConfig('server_offset_time'); |
||
360 | } else { |
||
361 | $this->field['deletedon'] = 0; |
||
362 | } |
||
363 | break; |
||
364 | case 'deletedon': |
||
365 | $value = $this->getTime($value); |
||
366 | if ($value > 0 && time() + $this->modxConfig('server_offset_time') < $value) { |
||
367 | $value = 0; |
||
368 | } |
||
369 | if ($value) { |
||
370 | $this->field['deleted'] = 1; |
||
371 | } |
||
372 | break; |
||
373 | case 'editedon': |
||
374 | case 'createdon': |
||
375 | case 'publishedon': |
||
376 | $value = $this->getTime($value); |
||
377 | break; |
||
378 | case 'publishedby': |
||
379 | case 'editedby': |
||
380 | case 'createdby': |
||
381 | case 'deletedby': |
||
382 | $value = $this->getUser($value, $this->default_field[$key]); |
||
383 | break; |
||
384 | } |
||
385 | $this->field[$key] = $value; |
||
386 | } |
||
387 | |||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @param $value |
||
393 | * @param int $default |
||
394 | * @return int|mixed |
||
395 | */ |
||
396 | protected function getUser($value, $default = 0) |
||
397 | { |
||
398 | $currentAdmin = APIHelpers::getkey($_SESSION, 'mgrInternalKey', 0); |
||
399 | $value = (int)$value; |
||
400 | if (!empty($value)) { |
||
401 | $by = $this->findUserBy($value); |
||
402 | $exists = $this->managerUsers->exists(function ($key, Helpers\Collection $val) use ($by, $value) { |
||
403 | return ($val->containsKey($by) && $val->get($by) === (string)$value); |
||
404 | }); |
||
405 | if (!$exists) { |
||
406 | $value = 0; |
||
407 | } |
||
408 | } |
||
409 | if (empty($value)) { |
||
410 | $value = empty($currentAdmin) ? $default : $currentAdmin; |
||
411 | } |
||
412 | |||
413 | return $value; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param $data |
||
418 | * @return bool|string |
||
419 | */ |
||
420 | protected function findUserBy($data) |
||
421 | { |
||
422 | switch (true) { |
||
423 | case (is_int($data) || ((int)$data > 0 && (string)intval($data) === $data)): |
||
424 | $find = 'id'; |
||
425 | break; |
||
426 | case filter_var($data, FILTER_VALIDATE_EMAIL): |
||
427 | $find = 'email'; |
||
428 | break; |
||
429 | case is_scalar($data): |
||
430 | $find = 'username'; |
||
431 | break; |
||
432 | default: |
||
433 | $find = false; |
||
434 | } |
||
435 | |||
436 | return $find; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @param array $data |
||
441 | * @return $this |
||
442 | */ |
||
443 | public function create($data = array()) |
||
444 | { |
||
445 | $this->close(); |
||
446 | $fld = array(); |
||
447 | foreach ($this->tvd as $name => $tv) { |
||
448 | $fld[$name] = $tv; |
||
449 | }; |
||
450 | $this->store($fld); |
||
451 | |||
452 | $this->fromArray(array_merge($fld, $data)); |
||
453 | $this->set('createdby', null) |
||
454 | ->set('editedby', null) |
||
455 | ->set('createdon', time()) |
||
456 | ->touch(); |
||
457 | |||
458 | return $this; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @param $id |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function edit($id) |
||
466 | { |
||
467 | $id = is_scalar($id) ? trim($id) : ''; |
||
468 | if ($this->getID() != $id) { |
||
469 | $this->close(); |
||
470 | $this->markAllEncode(); |
||
471 | $this->newDoc = false; |
||
472 | |||
473 | $result = $this->query("SELECT * from {$this->makeTable('site_content')} where `id`=" . (int)$id); |
||
474 | $this->fromArray($this->modx->db->getRow($result)); |
||
475 | $result = $this->query("SELECT * from {$this->makeTable('site_tmplvar_contentvalues')} where `contentid`=" . (int)$id); |
||
476 | while ($row = $this->modx->db->getRow($result)) { |
||
477 | $this->field[$this->tvid[$row['tmplvarid']]] = $row['value']; |
||
478 | } |
||
479 | if (empty($this->field['id'])) { |
||
480 | $this->id = null; |
||
481 | } else { |
||
482 | $this->id = $this->field['id']; |
||
483 | $this->set('editedby', null)->touch(); |
||
484 | $this->decodeFields(); |
||
485 | } |
||
486 | $this->store($this->toArray(null, null, null, false)); |
||
487 | unset($this->field['id']); |
||
488 | } |
||
489 | |||
490 | return $this; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * @param bool $fire_events |
||
495 | * @param bool $clearCache |
||
496 | * @return bool|null |
||
497 | */ |
||
498 | public function save($fire_events = false, $clearCache = false) |
||
499 | { |
||
500 | $parent = null; |
||
501 | if ($this->field['pagetitle'] == '') { |
||
502 | $this->log['emptyPagetitle'] = 'Pagetitle is empty in <pre>' . print_r($this->field, true) . '</pre>'; |
||
503 | |||
504 | return false; |
||
505 | } |
||
506 | |||
507 | $uid = $this->modx->getLoginUserID('mgr'); |
||
508 | |||
509 | if ( |
||
510 | $this->field['parent'] == 0 && |
||
511 | !$this->modxConfig('udperms_allowroot') && |
||
512 | !($uid && isset($_SESSION['mgrRole']) && $_SESSION['mgrRole'] == 1) |
||
513 | ) { |
||
514 | $this->log['rootForbidden'] = 'Only Administrators can create documents in the root folder because udperms_allowroot setting is off'; |
||
515 | |||
516 | return false; |
||
517 | } |
||
518 | |||
519 | $this->set('alias', $this->getAlias()); |
||
520 | |||
521 | $this->invokeEvent('OnBeforeDocFormSave', array( |
||
522 | 'mode' => $this->newDoc ? "new" : "upd", |
||
523 | 'id' => isset($this->id) ? $this->id : '', |
||
524 | 'doc' => $this->toArray(), |
||
525 | 'docObj' => $this |
||
526 | ), $fire_events); |
||
527 | |||
528 | $fld = $this->encodeFields()->toArray(null, null, null, false); |
||
529 | foreach ($this->default_field as $key => $value) { |
||
530 | $tmp = $this->get($key); |
||
531 | if ($this->newDoc && (!is_int($tmp) && $tmp == '')) { |
||
532 | if ($tmp == $value) { |
||
533 | switch ($key) { |
||
534 | case 'cacheable': |
||
535 | $value = $this->modxConfig('cache_default'); |
||
536 | break; |
||
537 | case 'template': |
||
538 | $value = $value = $this->modxConfig('default_template'); |
||
539 | break; |
||
540 | case 'published': |
||
541 | $value = $this->modxConfig('publish_default'); |
||
542 | break; |
||
543 | case 'searchable': |
||
544 | $value = $this->modxConfig('search_default'); |
||
545 | break; |
||
546 | case 'donthit': |
||
547 | $value = $this->modxConfig('track_visitors'); |
||
548 | break; |
||
549 | } |
||
550 | } |
||
551 | $this->field[$key] = $value; |
||
552 | } |
||
553 | switch (true) { |
||
554 | case $key == 'parent': |
||
555 | $parent = (int)$this->get($key); |
||
556 | $q = $this->query("SELECT count(`id`) FROM {$this->makeTable('site_content')} WHERE `id`='{$parent}'"); |
||
557 | if ($this->modx->db->getValue($q) != 1) { |
||
558 | $parent = 0; |
||
559 | } |
||
560 | $this->field[$key] = $parent; |
||
561 | $this->Uset($key); |
||
562 | break; |
||
563 | case ($key == 'alias_visible' && !$this->checkVersion('1.0.10', true)): |
||
564 | $this->eraseField('alias_visible'); |
||
565 | break; |
||
566 | default: |
||
567 | $this->Uset($key); |
||
568 | } |
||
569 | unset($fld[$key]); |
||
570 | } |
||
571 | |||
572 | if (!empty($this->set)) { |
||
573 | if ($this->newDoc) { |
||
574 | $SQL = "INSERT into {$this->makeTable('site_content')} SET " . implode(', ', $this->set); |
||
575 | } else { |
||
576 | $SQL = "UPDATE {$this->makeTable('site_content')} SET " . implode(', ', |
||
577 | $this->set) . " WHERE `id` = " . $this->id; |
||
578 | } |
||
579 | $this->query($SQL); |
||
580 | |||
581 | if ($this->newDoc) { |
||
582 | $this->id = $this->modx->db->getInsertId(); |
||
583 | } |
||
584 | |||
585 | if ($parent > 0) { |
||
586 | $this->query("UPDATE {$this->makeTable('site_content')} SET `isfolder`='1' WHERE `id`='{$parent}'"); |
||
587 | } |
||
588 | } |
||
589 | |||
590 | $_deleteTVs = $_insertTVs = array(); |
||
591 | foreach ($fld as $key => $value) { |
||
592 | if (empty($this->tv[$key]) || !$this->isChanged($key) || !$this->belongsToTemplate($this->tv[$key])) { |
||
593 | continue; |
||
594 | } elseif ($value === '') { |
||
595 | $_deleteTVs[] = $this->tv[$key]; |
||
596 | } else { |
||
597 | $_insertTVs[$this->tv[$key]] = $this->escape($value); |
||
598 | } |
||
599 | } |
||
600 | |||
601 | if (!empty($_insertTVs)) { |
||
602 | $values = array(); |
||
603 | foreach ($_insertTVs as $id => $value) { |
||
604 | $values[] = "({$this->id}, {$id}, '{$value}')"; |
||
605 | } |
||
606 | $values = implode(',', $values); |
||
607 | $this->query("INSERT INTO {$this->makeTable('site_tmplvar_contentvalues')} (`contentid`,`tmplvarid`,`value`) VALUES {$values} ON DUPLICATE KEY UPDATE |
||
608 | `value` = VALUES(`value`)"); |
||
609 | } |
||
610 | |||
611 | if (!empty($_deleteTVs)) { |
||
612 | $ids = implode(',', $_deleteTVs); |
||
613 | $this->query("DELETE FROM {$this->makeTable('site_tmplvar_contentvalues')} WHERE `contentid` = '{$this->id}' AND `tmplvarid` IN ({$ids})"); |
||
614 | } |
||
615 | |||
616 | if (!isset($this->mode)) { |
||
617 | $this->mode = $this->newDoc ? "new" : "upd"; |
||
618 | $this->newDoc = false; |
||
619 | } |
||
620 | |||
621 | if (!empty($this->groupIds)) { |
||
622 | $this->setDocumentGroups($this->id, $this->groupIds); |
||
623 | } |
||
624 | $this->invokeEvent('OnDocFormSave', array( |
||
625 | 'mode' => $this->mode, |
||
626 | 'id' => isset($this->id) ? $this->id : '', |
||
627 | 'doc' => $this->toArray(), |
||
628 | 'docObj' => $this |
||
629 | ), $fire_events); |
||
630 | |||
631 | if ($clearCache) { |
||
632 | $this->clearCache($fire_events); |
||
633 | } |
||
634 | $this->decodeFields(); |
||
635 | |||
636 | return $this->id; |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * @param $tvId |
||
641 | * @return bool |
||
642 | */ |
||
643 | protected function belongsToTemplate($tvId) |
||
644 | { |
||
645 | $template = $this->get('template'); |
||
646 | |||
647 | return isset($this->tvTpl[$template]) && in_array($tvId, $this->tvTpl[$template]); |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * @param $ids |
||
652 | * @return $this |
||
653 | * @throws Exception |
||
654 | */ |
||
655 | public function toTrash($ids) |
||
656 | { |
||
657 | $ignore = $this->systemID(); |
||
658 | $_ids = $this->cleanIDs($ids, ',', $ignore); |
||
659 | if (is_array($_ids) && $_ids != array()) { |
||
660 | $id = $this->sanitarIn($_ids); |
||
661 | $uid = (int)$this->modx->getLoginUserId(); |
||
662 | $deletedon = time() + $this->modxConfig('server_offset_time'); |
||
663 | $this->query("UPDATE {$this->makeTable('site_content')} SET `deleted`=1, `deletedby`={$uid}, `deletedon`={$deletedon} WHERE `id` IN ({$id})"); |
||
664 | } else { |
||
665 | throw new Exception('Invalid IDs list for mark trash: <pre>' . print_r($ids, |
||
666 | 1) . '</pre> please, check ignore list: <pre>' . print_r($ignore, 1) . '</pre>'); |
||
667 | } |
||
668 | |||
669 | return $this; |
||
670 | } |
||
671 | |||
672 | /** |
||
673 | * @param bool $fire_events |
||
674 | * @return $this |
||
675 | */ |
||
676 | public function clearTrash($fire_events = false) |
||
677 | { |
||
678 | $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `deleted`='1'"); |
||
679 | $_ids = $this->modx->db->getColumn('id', $q); |
||
680 | if (is_array($_ids) && $_ids != array()) { |
||
681 | $this->invokeEvent('OnBeforeEmptyTrash', array( |
||
682 | "ids" => $_ids |
||
683 | ), $fire_events); |
||
684 | |||
685 | $id = $this->sanitarIn($_ids); |
||
686 | $this->query("DELETE from {$this->makeTable('site_content')} where `id` IN ({$id})"); |
||
687 | $this->query("DELETE from {$this->makeTable('site_tmplvar_contentvalues')} where `contentid` IN ({$id})"); |
||
688 | |||
689 | $this->invokeEvent('OnEmptyTrash', array( |
||
690 | "ids" => $_ids |
||
691 | ), $fire_events); |
||
692 | } |
||
693 | |||
694 | return $this; |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * @param $ids |
||
699 | * @param int|bool $depth |
||
700 | * @return array |
||
701 | */ |
||
702 | public function children($ids, $depth) |
||
703 | { |
||
704 | $_ids = $this->cleanIDs($ids, ','); |
||
705 | if (is_array($_ids) && $_ids != array()) { |
||
706 | $id = $this->sanitarIn($_ids); |
||
707 | if (!empty($id)) { |
||
708 | $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} where `parent` IN ({$id})"); |
||
709 | $id = $this->modx->db->getColumn('id', $q); |
||
710 | if ($depth > 0 || $depth === true) { |
||
711 | $id = $this->children($id, is_bool($depth) ? $depth : ($depth - 1)); |
||
712 | } |
||
713 | $_ids = array_merge($_ids, $id); |
||
714 | } |
||
715 | } |
||
716 | |||
717 | return $_ids; |
||
718 | } |
||
719 | |||
720 | /** |
||
721 | * @param string|array $ids |
||
722 | * @param bool $fire_events |
||
723 | * @return $this |
||
724 | * @throws Exception |
||
725 | */ |
||
726 | public function delete($ids, $fire_events = false) |
||
727 | { |
||
728 | $ids = $this->children($ids, true); |
||
729 | $_ids = $this->cleanIDs($ids, ',', $this->systemID()); |
||
730 | $this->invokeEvent('OnBeforeDocFormDelete', array( |
||
731 | 'ids' => $_ids |
||
732 | ), $fire_events); |
||
733 | $this->toTrash($_ids); |
||
734 | $this->invokeEvent('OnDocFormDelete', array( |
||
735 | 'ids' => $_ids |
||
736 | ), $fire_events); |
||
737 | |||
738 | return $this; |
||
739 | } |
||
740 | |||
741 | /** |
||
742 | * @return array |
||
743 | */ |
||
744 | private function systemID() |
||
745 | { |
||
746 | $ignore = array( |
||
747 | 0, //empty document |
||
748 | (int)$this->modxConfig('site_start'), |
||
749 | (int)$this->modxConfig('error_page'), |
||
750 | (int)$this->modxConfig('unauthorized_page'), |
||
751 | (int)$this->modxConfig('site_unavailable_page') |
||
752 | ); |
||
753 | $data = $this->query("SELECT DISTINCT setting_value FROM {$this->makeTable('web_user_settings')} WHERE `setting_name`='login_home' AND `setting_value`!=''"); |
||
754 | $data = $this->modx->db->makeArray($data); |
||
755 | foreach ($data as $item) { |
||
756 | $ignore[] = (int)$item['setting_value']; |
||
757 | } |
||
758 | |||
759 | return array_unique($ignore); |
||
760 | |||
761 | } |
||
762 | |||
763 | /** |
||
764 | * @param $alias |
||
765 | * @return string |
||
766 | */ |
||
767 | protected function checkAlias($alias) |
||
768 | { |
||
769 | $alias = strtolower($alias); |
||
770 | if ($this->modxConfig('friendly_urls')) { |
||
771 | $_alias = $this->escape($alias); |
||
772 | if ((!$this->modxConfig('allow_duplicate_alias') && !$this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) { |
||
773 | $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' AND `parent`={$this->get('parent')} LIMIT 1")); |
||
774 | } else { |
||
775 | $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' LIMIT 1")); |
||
776 | } |
||
777 | if (($flag && $this->newDoc) || (!$this->newDoc && $flag && $this->id != $flag)) { |
||
778 | $suffix = substr($alias, -2); |
||
779 | if (preg_match('/-(\d+)/', $suffix, $tmp) && isset($tmp[1]) && (int)$tmp[1] > 1) { |
||
780 | $suffix = (int)$tmp[1] + 1; |
||
781 | $alias = substr($alias, 0, -2) . '-' . $suffix; |
||
782 | } else { |
||
783 | $alias .= '-2'; |
||
784 | } |
||
785 | $alias = $this->checkAlias($alias); |
||
786 | } |
||
787 | } |
||
788 | |||
789 | return $alias; |
||
790 | } |
||
791 | |||
792 | /** |
||
793 | * @param $key |
||
794 | * @return bool |
||
795 | */ |
||
796 | public function issetField($key) |
||
797 | { |
||
798 | return (array_key_exists($key, $this->default_field) || array_key_exists($key, $this->tv)); |
||
799 | } |
||
800 | |||
801 | /** |
||
802 | * @param bool $reload |
||
803 | * @return $this |
||
804 | */ |
||
805 | protected function get_TV($reload = false) |
||
806 | { |
||
807 | $this->modx->_TVnames = $this->loadFromCache('_TVnames'); |
||
808 | if ($this->modx->_TVnames === false || empty($this->modx->_TVnames) || $reload) { |
||
809 | $this->modx->_TVnames = array(); |
||
810 | $result = $this->query('SELECT `id`,`name`,`default_text`,`type` FROM ' . $this->makeTable('site_tmplvars')); |
||
811 | while ($row = $this->modx->db->GetRow($result)) { |
||
812 | $this->modx->_TVnames[$row['name']] = array( |
||
813 | "id" => $row['id'], |
||
814 | "type" => $row['type'], |
||
815 | "default" => $row['default_text'] |
||
816 | ); |
||
817 | } |
||
818 | $this->saveToCache($this->modx->_TVnames, '_TVnames'); |
||
819 | } |
||
820 | $arrayTypes = array('checkbox', 'listbox-multiple'); |
||
821 | $arrayTVs = array(); |
||
822 | foreach ($this->modx->_TVnames as $name => $data) { |
||
823 | $this->tvid[$data['id']] = $name; |
||
824 | $this->tv[$name] = $data['id']; |
||
825 | if (in_array($data['type'], $arrayTypes)) { |
||
826 | $arrayTVs[] = $name; |
||
827 | } |
||
828 | } |
||
829 | if (empty($this->tvaFields)) { |
||
830 | $this->tvaFields = $arrayTVs; |
||
831 | } |
||
832 | $this->loadTVTemplate()->loadTVDefault(array_values($this->tv)); |
||
833 | |||
834 | return $this; |
||
835 | } |
||
836 | |||
837 | /** |
||
838 | * @return $this |
||
839 | */ |
||
840 | protected function loadTVTemplate() |
||
841 | { |
||
842 | $this->tvTpl = $this->loadFromCache('_tvTpl'); |
||
843 | if ($this->tvTpl === false) { |
||
844 | $q = $this->query("SELECT `tmplvarid`, `templateid` FROM " . $this->makeTable('site_tmplvar_templates')); |
||
845 | $this->tvTpl = array(); |
||
846 | while ($item = $this->modx->db->getRow($q)) { |
||
847 | $this->tvTpl[$item['templateid']][] = $item['tmplvarid']; |
||
848 | } |
||
849 | $this->saveToCache($this->tvTpl, '_tvTpl'); |
||
850 | } |
||
851 | |||
852 | return $this; |
||
853 | } |
||
854 | |||
855 | /** |
||
856 | * @param array $tvId |
||
857 | * @return $this |
||
858 | */ |
||
859 | protected function loadTVDefault(array $tvId = array()) |
||
860 | { |
||
861 | if (is_array($tvId) && !empty($tvId)) { |
||
862 | $this->tvd = array(); |
||
863 | foreach ($tvId as $id) { |
||
864 | $name = $this->tvid[$id]; |
||
865 | $this->tvd[$name] = $this->modx->_TVnames[$name]['default']; |
||
866 | } |
||
867 | } |
||
868 | |||
869 | return $this; |
||
870 | } |
||
871 | |||
872 | /** |
||
873 | * @param $tpl |
||
874 | * @return int |
||
875 | * @throws Exception |
||
876 | */ |
||
877 | public function setTemplate($tpl) |
||
878 | { |
||
879 | if (!is_numeric($tpl) || $tpl != (int)$tpl) { |
||
880 | if (is_scalar($tpl)) { |
||
881 | $sql = "SELECT `id` FROM {$this->makeTable('site_templates')} WHERE `templatename` = '" . $this->escape($tpl) . "'"; |
||
882 | $rs = $this->query($sql); |
||
883 | if (!$rs || $this->modx->db->getRecordCount($rs) <= 0) { |
||
884 | throw new Exception("Template {$tpl} is not exists"); |
||
885 | } |
||
886 | $tpl = $this->modx->db->getValue($rs); |
||
887 | } else { |
||
888 | throw new Exception("Invalid template name: " . print_r($tpl, 1)); |
||
889 | } |
||
890 | } |
||
891 | |||
892 | return (int)$tpl; |
||
893 | } |
||
894 | |||
895 | /** |
||
896 | * @return string |
||
897 | */ |
||
898 | protected function getAlias() |
||
899 | { |
||
900 | if ($this->modxConfig('friendly_urls') && $this->modxConfig('automatic_alias') && $this->get('alias') == '') { |
||
901 | $alias = strtr($this->get('pagetitle'), $this->table); |
||
902 | } else { |
||
903 | if ($this->get('alias') != '') { |
||
904 | $alias = $this->get('alias'); |
||
905 | } else { |
||
906 | $alias = ''; |
||
907 | } |
||
908 | } |
||
909 | $alias = $this->modx->stripAlias($alias); |
||
910 | |||
911 | return $this->checkAlias($alias); |
||
912 | } |
||
913 | |||
914 | /** |
||
915 | * @param int $parent |
||
916 | * @param string $criteria |
||
917 | * @param string $dir |
||
918 | * @return $this |
||
919 | * |
||
920 | * Пересчет menuindex по полю таблицы site_content |
||
921 | */ |
||
922 | public function updateMenuindex($parent, $criteria = 'id', $dir = 'asc') |
||
923 | { |
||
924 | $dir = strtolower($dir) == 'desc' ? 'desc' : 'asc'; |
||
925 | if (is_integer($parent) && $criteria !== '') { |
||
926 | $this->query("SET @index := 0"); |
||
927 | $this->query("UPDATE {$this->makeTable('site_content')} SET `menuindex` = (@index := @index + 1) WHERE `parent`={$parent} ORDER BY {$criteria} {$dir}"); |
||
928 | } |
||
929 | |||
930 | return $this; |
||
931 | } |
||
932 | |||
933 | /** |
||
934 | * Устанавливает значение шаблона согласно системной настройке |
||
935 | * |
||
936 | * @return $this |
||
937 | */ |
||
938 | public function setDefaultTemplate() |
||
939 | { |
||
940 | $parent = $this->get('parent'); |
||
941 | $template = $this->modxConfig('default_template'); |
||
942 | switch ($this->modxConfig('auto_template_logic')) { |
||
943 | case 'sibling': |
||
944 | if (!$parent) { |
||
945 | $site_start = $this->modxConfig('site_start'); |
||
946 | $where = "sc.isfolder=0 AND sc.id!={$site_start}"; |
||
947 | $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', $where, 'menuindex', 'ASC', 1); |
||
948 | if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') { |
||
949 | $template = $sibl[0]['template']; |
||
950 | } |
||
951 | } else { |
||
952 | $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', 'isfolder=0', 'menuindex', |
||
953 | 'ASC', 1); |
||
954 | if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') { |
||
955 | $template = $sibl[0]['template']; |
||
956 | } else { |
||
957 | $sibl = $this->modx->getDocumentChildren($parent, 0, 0, 'template', 'isfolder=0', 'menuindex', |
||
958 | 'ASC', 1); |
||
959 | if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') { |
||
960 | $template = $sibl[0]['template']; |
||
961 | } |
||
962 | } |
||
963 | } |
||
964 | break; |
||
965 | case 'parent': |
||
966 | if ($parent) { |
||
967 | $_parent = $this->modx->getPageInfo($parent, 0, 'template'); |
||
968 | if (isset($_parent['template'])) { |
||
969 | $template = $_parent['template']; |
||
970 | } |
||
971 | } |
||
972 | break; |
||
973 | } |
||
974 | $this->set('template', $template); |
||
975 | |||
976 | return $this; |
||
977 | } |
||
978 | |||
979 | /** |
||
980 | * Декодирует конкретное поле |
||
981 | * @param string $field Имя поля |
||
982 | * @param bool $store обновить распакованное поле |
||
983 | * @return array ассоциативный массив с данными из json строки |
||
984 | */ |
||
985 | public function decodeField($field, $store = false) |
||
986 | { |
||
987 | $out = array(); |
||
988 | if ($this->isDecodableField($field)) { |
||
989 | $data = $this->get($field); |
||
990 | if ($this->isTVarrayField($field)) { |
||
991 | $out = explode('||', $data); |
||
992 | } else { |
||
993 | $out = jsonHelper::jsonDecode($data, array('assoc' => true), true); |
||
994 | } |
||
995 | } |
||
996 | if ($store) { |
||
997 | $this->field[$field] = $out; |
||
998 | $this->markAsDecode($field); |
||
999 | } |
||
1000 | |||
1001 | return $out; |
||
1002 | } |
||
1003 | |||
1004 | /** |
||
1005 | * Запаковывает конкретное поле в JSON |
||
1006 | * @param string $field Имя поля |
||
1007 | * @param bool $store обновить запакованное поле |
||
1008 | * @return string|null json строка |
||
1009 | */ |
||
1010 | public function encodeField($field, $store = false) |
||
1011 | { |
||
1012 | $out = null; |
||
1013 | if ($this->isEncodableField($field)) { |
||
1014 | $data = $this->get($field); |
||
1015 | if ($this->isTVarrayField($field)) { |
||
1016 | $out = is_array($data) ? implode('||', $data) : (string)$data; |
||
1017 | } else { |
||
1018 | $out = json_encode($data); |
||
1019 | } |
||
1020 | } |
||
1021 | if ($store) { |
||
1022 | $this->field[$field] = $out; |
||
1023 | $this->markAsEncode($field); |
||
1024 | } |
||
1025 | |||
1026 | return $out; |
||
1027 | } |
||
1028 | |||
1029 | /** |
||
1030 | * Может ли содержать данное поле json массив |
||
1031 | * @param string $field имя поля |
||
1032 | * @return boolean |
||
1033 | */ |
||
1034 | public function isTVarrayField($field) |
||
1035 | { |
||
1036 | return (is_scalar($field) && in_array($field, $this->tvaFields)); |
||
1037 | } |
||
1038 | |||
1039 | /** |
||
1040 | * Пометить все поля как запакованные |
||
1041 | * @return $this |
||
1042 | */ |
||
1043 | public function markAllEncode() |
||
1044 | { |
||
1045 | parent::markAllEncode(); |
||
1046 | foreach ($this->tvaFields as $field) { |
||
1047 | $this->markAsEncode($field); |
||
1048 | } |
||
1049 | |||
1050 | return $this; |
||
1051 | } |
||
1052 | |||
1053 | /** |
||
1054 | * Пометить все поля как распакованные |
||
1055 | * @return $this |
||
1056 | */ |
||
1057 | public function markAllDecode() |
||
1058 | { |
||
1059 | parent::markAllDecode(); |
||
1060 | foreach ($this->tvaFields as $field) { |
||
1061 | $this->markAsDecode($field); |
||
1062 | } |
||
1063 | |||
1064 | return $this; |
||
1065 | } |
||
1066 | |||
1067 | /** |
||
1068 | * @param int $docId |
||
1069 | */ |
||
1070 | public function getDocumentGroups($docId = 0) |
||
1071 | { |
||
1072 | $out = array(); |
||
1073 | $doc = $this->switchObject($docId); |
||
1074 | if (null !== $doc->getID()) { |
||
1075 | $doc_groups = $this->makeTable('document_groups'); |
||
1076 | $docgroup_names = $this->makeTable('documentgroup_names'); |
||
1077 | |||
1078 | $rs = $this->query("SELECT `dg`.`document_group`, `dgn`.`name` FROM {$doc_groups} as `dg` INNER JOIN {$docgroup_names} as `dgn` ON `dgn`.`id`=`dg`.`document_group` |
||
1079 | WHERE `dg`.`document` = " . $doc->getID()); |
||
1080 | while ($row = $this->modx->db->getRow($rs)) { |
||
1081 | $out[$row['document_group']] = $row['name']; |
||
1082 | } |
||
1083 | |||
1084 | } |
||
1085 | unset($doc); |
||
1086 | |||
1087 | return $out; |
||
1088 | } |
||
1089 | |||
1090 | /** |
||
1091 | * @param int $docId |
||
1092 | * @param array $groupIds |
||
1093 | * @return $this |
||
1094 | */ |
||
1095 | public function setDocumentGroups($docId = 0, $groupIds = array()) |
||
1096 | { |
||
1097 | if (!is_array($groupIds)) { |
||
1098 | return $this; |
||
1099 | } |
||
1100 | if ($this->newDoc && $docId == 0) { |
||
1101 | $this->groupIds = $groupIds; |
||
1102 | } else { |
||
1103 | $doc = $this->switchObject($docId); |
||
1104 | if ($id = $doc->getID()) { |
||
1105 | foreach ($groupIds as $gid) { |
||
1106 | $this->query("REPLACE INTO {$this->makeTable('document_groups')} (`document_group`, `document`) VALUES ('{$gid}', '{$id}')"); |
||
1107 | } |
||
1108 | if (!$this->newDoc) { |
||
1109 | $groupIds = empty($groupIds) ? '0' : implode(',', $groupIds); |
||
1110 | $this->query("DELETE FROM {$this->makeTable('document_groups')} WHERE `document`={$id} AND `document_group` NOT IN ({$groupIds})"); |
||
1111 | } |
||
1112 | } |
||
1113 | unset($doc); |
||
1114 | $this->groupIds = array(); |
||
1115 | } |
||
1116 | |||
1117 | return $this; |
||
2240 |