Completed
Push — master ( c5f771...a84702 )
by
unknown
02:16
created
assets/lib/MODxAPI/modResource.php 1 patch
Braces   +193 added lines, -185 removed lines patch added patch discarded remove patch
@@ -4,8 +4,8 @@  discard block
 block discarded – undo
4 4
 /**
5 5
  * Class modResource
6 6
  */
7
-class modResource extends MODxAPI
8
-{
7
+class modResource extends MODxAPI
8
+{
9 9
     /**
10 10
      * @var string
11 11
      */
@@ -157,8 +157,8 @@  discard block
 block discarded – undo
157 157
      * @param DocumentParser $modx
158 158
      * @param bool $debug
159 159
      */
160
-    public function __construct($modx, $debug = false)
161
-    {
160
+    public function __construct($modx, $debug = false)
161
+    {
162 162
         parent::__construct($modx, $debug);
163 163
         $this->get_TV();
164 164
         $uTable = $this->makeTable("manager_users");
@@ -171,8 +171,8 @@  discard block
 block discarded – undo
171 171
     /**
172 172
      * @return array
173 173
      */
174
-    public function toArrayMain()
175
-    {
174
+    public function toArrayMain()
175
+    {
176 176
         $out = array_intersect_key(parent::toArray(), $this->default_field);
177 177
 
178 178
         return $out;
@@ -182,18 +182,18 @@  discard block
 block discarded – undo
182 182
      * @param bool $render
183 183
      * @return array
184 184
      */
185
-    public function toArrayTV($render = false)
186
-    {
185
+    public function toArrayTV($render = false)
186
+    {
187 187
         $out = array_diff_key(parent::toArray(), $this->default_field);
188 188
         $tpl = $this->get('template');
189 189
         $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array());
190
-        foreach ($tvTPL as $item) {
191
-            if (isset($this->tvid[$item]) && !array_key_exists($this->tvid[$item], $out)) {
190
+        foreach ($tvTPL as $item) {
191
+            if (isset($this->tvid[$item]) && !array_key_exists($this->tvid[$item], $out)) {
192 192
                 $out[$this->tvid[$item]] = $this->get($this->tvid[$item]);
193 193
             }
194 194
         }
195
-        if ($render) {
196
-            foreach ($out as $key => $val) {
195
+        if ($render) {
196
+            foreach ($out as $key => $val) {
197 197
                 $out[$key] = $this->renderTV($key);
198 198
             }
199 199
         }
@@ -208,8 +208,8 @@  discard block
 block discarded – undo
208 208
      * @param bool $render
209 209
      * @return array
210 210
      */
211
-    public function toArray($prefix = '', $suffix = '', $sep = '_', $render = true)
212
-    {
211
+    public function toArray($prefix = '', $suffix = '', $sep = '_', $render = true)
212
+    {
213 213
         $out = array_merge(
214 214
             $this->toArrayMain(),
215 215
             $this->toArrayTV($render),
@@ -222,11 +222,11 @@  discard block
 block discarded – undo
222 222
     /**
223 223
      * @return null|string
224 224
      */
225
-    public function getUrl()
226
-    {
225
+    public function getUrl()
226
+    {
227 227
         $out = null;
228 228
         $id = (int)$this->getID();
229
-        if (!empty($id)) {
229
+        if (!empty($id)) {
230 230
             $out = $this->modx->makeUrl($id);
231 231
         }
232 232
 
@@ -238,10 +238,10 @@  discard block
 block discarded – undo
238 238
      * @param string $second
239 239
      * @return mixed
240 240
      */
241
-    public function getTitle($main = 'menutitle', $second = 'pagetitle')
242
-    {
241
+    public function getTitle($main = 'menutitle', $second = 'pagetitle')
242
+    {
243 243
         $title = $this->get($main);
244
-        if (empty($title) && $title !== '0') {
244
+        if (empty($title) && $title !== '0') {
245 245
             $title = $this->get($second);
246 246
         }
247 247
 
@@ -251,8 +251,8 @@  discard block
 block discarded – undo
251 251
     /**
252 252
      * @return bool
253 253
      */
254
-    public function isWebShow()
255
-    {
254
+    public function isWebShow()
255
+    {
256 256
         $pub = ($this->get('publishedon') < time() && $this->get('published'));
257 257
         $unpub = ($this->get('unpub_date') == 0 || $this->get('unpub_date') > time());
258 258
         $del = ($this->get('deleted') == 0 && ($this->get('deletedon') == 0 || $this->get('deletedon') > time()));
@@ -263,8 +263,8 @@  discard block
 block discarded – undo
263 263
     /**
264 264
      * @return $this
265 265
      */
266
-    public function touch()
267
-    {
266
+    public function touch()
267
+    {
268 268
         $this->set('editedon', time());
269 269
 
270 270
         return $this;
@@ -274,14 +274,14 @@  discard block
 block discarded – undo
274 274
      * @param $tvname
275 275
      * @return null|string
276 276
      */
277
-    public function renderTV($tvname)
278
-    {
277
+    public function renderTV($tvname)
278
+    {
279 279
         $out = null;
280
-        if ($this->getID() > 0) {
280
+        if ($this->getID() > 0) {
281 281
             include_once MODX_MANAGER_PATH . "includes/tmplvars.format.inc.php";
282 282
             include_once MODX_MANAGER_PATH . "includes/tmplvars.commands.inc.php";
283 283
             $tvval = $this->get($tvname);
284
-            if ($this->isTVarrayField($tvname) && is_array($tvval)) {
284
+            if ($this->isTVarrayField($tvname) && is_array($tvval)) {
285 285
                 $tvval = implode('||', $tvval);
286 286
             }
287 287
             $param = APIHelpers::getkey($this->tvd, $tvname, array());
@@ -298,14 +298,14 @@  discard block
 block discarded – undo
298 298
      * @param $key
299 299
      * @return mixed
300 300
      */
301
-    public function get($key)
302
-    {
301
+    public function get($key)
302
+    {
303 303
         $out = parent::get($key);
304
-        if (isset($this->tv[$key])) {
304
+        if (isset($this->tv[$key])) {
305 305
             $tpl = $this->get('template');
306 306
             $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array());
307 307
             $tvID = APIHelpers::getkey($this->tv, $key, 0);
308
-            if (in_array($tvID, $tvTPL) && is_null($out)) {
308
+            if (in_array($tvID, $tvTPL) && is_null($out)) {
309 309
                 $out = APIHelpers::getkey($this->tvd[$key], 'value', null);
310 310
             }
311 311
         }
@@ -318,10 +318,10 @@  discard block
 block discarded – undo
318 318
      * @param $value
319 319
      * @return $this
320 320
      */
321
-    public function set($key, $value)
322
-    {
323
-        if ((is_scalar($value) || $this->isTVarrayField($key) || $this->isJsonField($key)) && is_scalar($key) && !empty($key)) {
324
-            switch ($key) {
321
+    public function set($key, $value)
322
+    {
323
+        if ((is_scalar($value) || $this->isTVarrayField($key) || $this->isJsonField($key)) && is_scalar($key) && !empty($key)) {
324
+            switch ($key) {
325 325
                 case 'parent':
326 326
                     $value = (int)$value;
327 327
                     break;
@@ -331,38 +331,38 @@  discard block
 block discarded – undo
331 331
                     break;
332 332
                 case 'published':
333 333
                     $value = (int)((bool)$value);
334
-                    if ($value) {
334
+                    if ($value) {
335 335
                         $this->field['publishedon'] = time() + $this->modxConfig('server_offset_time');
336 336
                     }
337 337
                     break;
338 338
                 case 'pub_date':
339 339
                     $value = $this->getTime($value);
340
-                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) {
340
+                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) {
341 341
                         $this->field['published'] = 1;
342 342
                         $this->field['publishedon'] = $value;
343 343
                     }
344 344
                     break;
345 345
                 case 'unpub_date':
346 346
                     $value = $this->getTime($value);
347
-                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) {
347
+                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) {
348 348
                         $this->field['published'] = 0;
349 349
                         $this->field['publishedon'] = 0;
350 350
                     }
351 351
                     break;
352 352
                 case 'deleted':
353 353
                     $value = (int)((bool)$value);
354
-                    if ($value) {
354
+                    if ($value) {
355 355
                         $this->field['deletedon'] = time() + $this->modxConfig('server_offset_time');
356
-                    } else {
356
+                    } else {
357 357
                         $this->field['deletedon'] = 0;
358 358
                     }
359 359
                     break;
360 360
                 case 'deletedon':
361 361
                     $value = $this->getTime($value);
362
-                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') < $value) {
362
+                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') < $value) {
363 363
                         $value = 0;
364 364
                     }
365
-                    if ($value) {
365
+                    if ($value) {
366 366
                         $this->field['deleted'] = 1;
367 367
                     }
368 368
                     break;
@@ -389,20 +389,20 @@  discard block
 block discarded – undo
389 389
      * @param int $default
390 390
      * @return int|mixed
391 391
      */
392
-    protected function getUser($value, $default = 0)
393
-    {
392
+    protected function getUser($value, $default = 0)
393
+    {
394 394
         $currentAdmin = APIHelpers::getkey($_SESSION, 'mgrInternalKey', 0);
395 395
         $value = (int)$value;
396
-        if (!empty($value)) {
396
+        if (!empty($value)) {
397 397
             $by = $this->findUserBy($value);
398
-            $exists = $this->managerUsers->exists(function ($key, Helpers\Collection $val) use ($by, $value) {
398
+            $exists = $this->managerUsers->exists(function ($key, Helpers\Collection $val) use ($by, $value) {
399 399
                 return ($val->containsKey($by) && $val->get($by) === (string)$value);
400 400
             });
401
-            if (!$exists) {
401
+            if (!$exists) {
402 402
                 $value = 0;
403 403
             }
404 404
         }
405
-        if (empty($value)) {
405
+        if (empty($value)) {
406 406
             $value = empty($currentAdmin) ? $default : $currentAdmin;
407 407
         }
408 408
 
@@ -413,9 +413,9 @@  discard block
 block discarded – undo
413 413
      * @param $data
414 414
      * @return bool|string
415 415
      */
416
-    protected function findUserBy($data)
417
-    {
418
-        switch (true) {
416
+    protected function findUserBy($data)
417
+    {
418
+        switch (true) {
419 419
             case (is_int($data) || ((int)$data > 0 && (string)intval($data) === $data)):
420 420
                 $find = 'id';
421 421
                 break;
@@ -436,11 +436,11 @@  discard block
 block discarded – undo
436 436
      * @param array $data
437 437
      * @return $this
438 438
      */
439
-    public function create($data = array())
440
-    {
439
+    public function create($data = array())
440
+    {
441 441
         $this->close();
442 442
         $fld = array();
443
-        foreach ($this->tvd as $name => $tv) {
443
+        foreach ($this->tvd as $name => $tv) {
444 444
             $fld[$name] = $tv['value'];
445 445
         };
446 446
         $this->store($fld);
@@ -458,10 +458,10 @@  discard block
 block discarded – undo
458 458
      * @param $id
459 459
      * @return $this
460 460
      */
461
-    public function edit($id)
462
-    {
461
+    public function edit($id)
462
+    {
463 463
         $id = is_scalar($id) ? trim($id) : '';
464
-        if ($this->getID() != $id) {
464
+        if ($this->getID() != $id) {
465 465
             $this->close();
466 466
             $this->markAllEncode();
467 467
             $this->newDoc = false;
@@ -469,12 +469,12 @@  discard block
 block discarded – undo
469 469
             $result = $this->query("SELECT * from {$this->makeTable('site_content')} where `id`=" . (int)$id);
470 470
             $this->fromArray($this->modx->db->getRow($result));
471 471
             $result = $this->query("SELECT * from {$this->makeTable('site_tmplvar_contentvalues')} where `contentid`=" . (int)$id);
472
-            while ($row = $this->modx->db->getRow($result)) {
472
+            while ($row = $this->modx->db->getRow($result)) {
473 473
                 $this->field[$this->tvid[$row['tmplvarid']]] = $row['value'];
474 474
             }
475
-            if (empty($this->field['id'])) {
475
+            if (empty($this->field['id'])) {
476 476
                 $this->id = null;
477
-            } else {
477
+            } else {
478 478
                 $this->id = $this->field['id'];
479 479
                 $this->set('editedby', null)->touch();
480 480
                 $this->decodeFields();
@@ -491,10 +491,10 @@  discard block
 block discarded – undo
491 491
      * @param bool $clearCache
492 492
      * @return bool|null
493 493
      */
494
-    public function save($fire_events = false, $clearCache = false)
495
-    {
494
+    public function save($fire_events = false, $clearCache = false)
495
+    {
496 496
         $parent = null;
497
-        if ($this->field['pagetitle'] == '') {
497
+        if ($this->field['pagetitle'] == '') {
498 498
             $this->log['emptyPagetitle'] = 'Pagetitle is empty in <pre>' . print_r($this->field, true) . '</pre>';
499 499
 
500 500
             return false;
@@ -506,7 +506,7 @@  discard block
 block discarded – undo
506 506
             $this->field['parent'] == 0 &&
507 507
             !$this->modxConfig('udperms_allowroot') &&
508 508
             !($uid && isset($_SESSION['mgrRole']) && $_SESSION['mgrRole'] == 1)
509
-        ) {
509
+        ) {
510 510
             $this->log['rootForbidden'] = 'Only Administrators can create documents in the root folder because udperms_allowroot setting is off';
511 511
 
512 512
             return false;
@@ -522,11 +522,11 @@  discard block
 block discarded – undo
522 522
         ), $fire_events);
523 523
 
524 524
         $fld = $this->encodeFields()->toArray(null, null, null, false);
525
-        foreach ($this->default_field as $key => $value) {
525
+        foreach ($this->default_field as $key => $value) {
526 526
             $tmp = $this->get($key);
527
-            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
528
-                if ($tmp == $value) {
529
-                    switch ($key) {
527
+            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
528
+                if ($tmp == $value) {
529
+                    switch ($key) {
530 530
                         case 'cacheable':
531 531
                             $value = $this->modxConfig('cache_default');
532 532
                             break;
@@ -546,11 +546,11 @@  discard block
 block discarded – undo
546 546
                 }
547 547
                 $this->field[$key] = $value;
548 548
             }
549
-            switch (true) {
549
+            switch (true) {
550 550
                 case $key == 'parent':
551 551
                     $parent = (int)$this->get($key);
552 552
                     $q = $this->query("SELECT count(`id`) FROM {$this->makeTable('site_content')} WHERE `id`='{$parent}'");
553
-                    if ($this->modx->db->getValue($q) != 1) {
553
+                    if ($this->modx->db->getValue($q) != 1) {
554 554
                         $parent = 0;
555 555
                     }
556 556
                     $this->field[$key] = $parent;
@@ -565,38 +565,38 @@  discard block
 block discarded – undo
565 565
             unset($fld[$key]);
566 566
         }
567 567
 
568
-        if (!empty($this->set)) {
569
-            if ($this->newDoc) {
568
+        if (!empty($this->set)) {
569
+            if ($this->newDoc) {
570 570
                 $SQL = "INSERT into {$this->makeTable('site_content')} SET " . implode(', ', $this->set);
571
-            } else {
571
+            } else {
572 572
                 $SQL = "UPDATE {$this->makeTable('site_content')} SET " . implode(', ',
573 573
                         $this->set) . " WHERE `id` = " . $this->id;
574 574
             }
575 575
             $this->query($SQL);
576 576
 
577
-            if ($this->newDoc) {
577
+            if ($this->newDoc) {
578 578
                 $this->id = $this->modx->db->getInsertId();
579 579
             }
580 580
 
581
-            if ($parent > 0) {
581
+            if ($parent > 0) {
582 582
                 $this->query("UPDATE {$this->makeTable('site_content')} SET `isfolder`='1' WHERE `id`='{$parent}'");
583 583
             }
584 584
         }
585 585
 
586 586
         $_deleteTVs = $_insertTVs = array();
587
-        foreach ($fld as $key => $value) {
588
-            if (empty($this->tv[$key]) || !$this->isChanged($key) || !$this->belongsToTemplate($this->tv[$key])) {
587
+        foreach ($fld as $key => $value) {
588
+            if (empty($this->tv[$key]) || !$this->isChanged($key) || !$this->belongsToTemplate($this->tv[$key])) {
589 589
                 continue;
590
-            } elseif ($value === '') {
590
+            } elseif ($value === '') {
591 591
                 $_deleteTVs[] = $this->tv[$key];
592
-            } else {
592
+            } else {
593 593
                 $_insertTVs[$this->tv[$key]] = $this->escape($value);
594 594
             }
595 595
         }
596 596
 
597
-        if (!empty($_insertTVs)) {
597
+        if (!empty($_insertTVs)) {
598 598
             $values = array();
599
-            foreach ($_insertTVs as $id => $value) {
599
+            foreach ($_insertTVs as $id => $value) {
600 600
                 $values[] = "({$this->id}, {$id}, '{$value}')";
601 601
             }
602 602
             $values = implode(',', $values);
@@ -604,17 +604,19 @@  discard block
 block discarded – undo
604 604
     `value` = VALUES(`value`)");
605 605
         }
606 606
 
607
-        if (!empty($_deleteTVs)) {
607
+        if (!empty($_deleteTVs)) {
608 608
             $ids = implode(',', $_deleteTVs);
609 609
             $this->query("DELETE FROM {$this->makeTable('site_tmplvar_contentvalues')} WHERE `contentid` = '{$this->id}' AND `tmplvarid` IN ({$ids})");
610 610
         }
611 611
 
612
-        if (!isset($this->mode)) {
612
+        if (!isset($this->mode)) {
613 613
             $this->mode = $this->newDoc ? "new" : "upd";
614 614
             $this->newDoc = false;
615 615
         }
616 616
 
617
-        if ($this->groupIds) $this->setDocumentGroups($this->id, $this->groupIds);
617
+        if ($this->groupIds) {
618
+            $this->setDocumentGroups($this->id, $this->groupIds);
619
+        }
618 620
         $this->invokeEvent('OnDocFormSave', array(
619 621
             'mode'   => $this->mode,
620 622
             'id'     => $this->id,
@@ -622,7 +624,7 @@  discard block
 block discarded – undo
622 624
             'docObj' => $this
623 625
         ), $fire_events);
624 626
 
625
-        if ($clearCache) {
627
+        if ($clearCache) {
626 628
             $this->clearCache($fire_events);
627 629
         }
628 630
         $this->decodeFields();
@@ -634,7 +636,8 @@  discard block
 block discarded – undo
634 636
      * @param $tvId
635 637
      * @return bool
636 638
      */
637
-    protected function belongsToTemplate($tvId) {
639
+    protected function belongsToTemplate($tvId)
640
+    {
638 641
         $template = $this->get('template');
639 642
 
640 643
         return isset($this->tvTpl[$template]) && in_array($tvId, $this->tvTpl[$template]);
@@ -645,16 +648,16 @@  discard block
 block discarded – undo
645 648
      * @return $this
646 649
      * @throws Exception
647 650
      */
648
-    public function toTrash($ids)
649
-    {
651
+    public function toTrash($ids)
652
+    {
650 653
         $ignore = $this->systemID();
651 654
         $_ids = $this->cleanIDs($ids, ',', $ignore);
652
-        if (is_array($_ids) && $_ids != array()) {
655
+        if (is_array($_ids) && $_ids != array()) {
653 656
             $id = $this->sanitarIn($_ids);
654 657
             $uid = (int)$this->modx->getLoginUserId();
655 658
             $deletedon = time() + $this->modxConfig('server_offset_time');
656 659
             $this->query("UPDATE {$this->makeTable('site_content')} SET `deleted`=1, `deletedby`={$uid}, `deletedon`={$deletedon} WHERE `id` IN ({$id})");
657
-        } else {
660
+        } else {
658 661
             throw new Exception('Invalid IDs list for mark trash: <pre>' . print_r($ids,
659 662
                     1) . '</pre> please, check ignore list: <pre>' . print_r($ignore, 1) . '</pre>');
660 663
         }
@@ -666,11 +669,11 @@  discard block
 block discarded – undo
666 669
      * @param bool $fire_events
667 670
      * @return $this
668 671
      */
669
-    public function clearTrash($fire_events = false)
670
-    {
672
+    public function clearTrash($fire_events = false)
673
+    {
671 674
         $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `deleted`='1'");
672 675
         $_ids = $this->modx->db->getColumn('id', $q);
673
-        if (is_array($_ids) && $_ids != array()) {
676
+        if (is_array($_ids) && $_ids != array()) {
674 677
             $this->invokeEvent('OnBeforeEmptyTrash', array(
675 678
                 "ids" => $_ids
676 679
             ), $fire_events);
@@ -692,15 +695,15 @@  discard block
 block discarded – undo
692 695
      * @param int|bool $depth
693 696
      * @return array
694 697
      */
695
-    public function children($ids, $depth)
696
-    {
698
+    public function children($ids, $depth)
699
+    {
697 700
         $_ids = $this->cleanIDs($ids, ',');
698
-        if (is_array($_ids) && $_ids != array()) {
701
+        if (is_array($_ids) && $_ids != array()) {
699 702
             $id = $this->sanitarIn($_ids);
700
-            if (!empty($id)) {
703
+            if (!empty($id)) {
701 704
                 $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} where `parent` IN ({$id})");
702 705
                 $id = $this->modx->db->getColumn('id', $q);
703
-                if ($depth > 0 || $depth === true) {
706
+                if ($depth > 0 || $depth === true) {
704 707
                     $id = $this->children($id, is_bool($depth) ? $depth : ($depth - 1));
705 708
                 }
706 709
                 $_ids = array_merge($_ids, $id);
@@ -716,8 +719,8 @@  discard block
 block discarded – undo
716 719
      * @return $this
717 720
      * @throws Exception
718 721
      */
719
-    public function delete($ids, $fire_events = false)
720
-    {
722
+    public function delete($ids, $fire_events = false)
723
+    {
721 724
         $ids = $this->children($ids, true);
722 725
         $_ids = $this->cleanIDs($ids, ',', $this->systemID());
723 726
         $this->invokeEvent('OnBeforeDocFormDelete', array(
@@ -734,8 +737,8 @@  discard block
 block discarded – undo
734 737
     /**
735 738
      * @return array
736 739
      */
737
-    private function systemID()
738
-    {
740
+    private function systemID()
741
+    {
739 742
         $ignore = array(
740 743
             0, //empty document
741 744
             (int)$this->modxConfig('site_start'),
@@ -745,7 +748,7 @@  discard block
 block discarded – undo
745 748
         );
746 749
         $data = $this->query("SELECT DISTINCT setting_value FROM {$this->makeTable('web_user_settings')} WHERE `setting_name`='login_home' AND `setting_value`!=''");
747 750
         $data = $this->modx->db->makeArray($data);
748
-        foreach ($data as $item) {
751
+        foreach ($data as $item) {
749 752
             $ignore[] = (int)$item['setting_value'];
750 753
         }
751 754
 
@@ -757,22 +760,22 @@  discard block
 block discarded – undo
757 760
      * @param $alias
758 761
      * @return string
759 762
      */
760
-    protected function checkAlias($alias)
761
-    {
763
+    protected function checkAlias($alias)
764
+    {
762 765
         $alias = strtolower($alias);
763
-        if ($this->modxConfig('friendly_urls')) {
766
+        if ($this->modxConfig('friendly_urls')) {
764 767
             $_alias = $this->escape($alias);
765
-            if ((!$this->modxConfig('allow_duplicate_alias') && !$this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) {
768
+            if ((!$this->modxConfig('allow_duplicate_alias') && !$this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) {
766 769
                 $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' AND `parent`={$this->get('parent')} LIMIT 1"));
767
-            } else {
770
+            } else {
768 771
                 $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' LIMIT 1"));
769 772
             }
770
-            if (($flag && $this->newDoc) || (!$this->newDoc && $flag && $this->id != $flag)) {
773
+            if (($flag && $this->newDoc) || (!$this->newDoc && $flag && $this->id != $flag)) {
771 774
                 $suffix = substr($alias, -2);
772
-                if (preg_match('/-(\d+)/', $suffix, $tmp) && isset($tmp[1]) && (int)$tmp[1] > 1) {
775
+                if (preg_match('/-(\d+)/', $suffix, $tmp) && isset($tmp[1]) && (int)$tmp[1] > 1) {
773 776
                     $suffix = (int)$tmp[1] + 1;
774 777
                     $alias = substr($alias, 0, -2) . '-' . $suffix;
775
-                } else {
778
+                } else {
776 779
                     $alias .= '-2';
777 780
                 }
778 781
                 $alias = $this->checkAlias($alias);
@@ -786,8 +789,8 @@  discard block
 block discarded – undo
786 789
      * @param $key
787 790
      * @return bool
788 791
      */
789
-    public function issetField($key)
790
-    {
792
+    public function issetField($key)
793
+    {
791 794
         return (array_key_exists($key, $this->default_field) || array_key_exists($key, $this->tv));
792 795
     }
793 796
 
@@ -795,12 +798,12 @@  discard block
 block discarded – undo
795 798
      * @param bool $reload
796 799
      * @return $this
797 800
      */
798
-    protected function get_TV($reload = false)
799
-    {
801
+    protected function get_TV($reload = false)
802
+    {
800 803
         $this->modx->_TVnames = array();
801
-        if (empty($this->modx->_TVnames) || $reload) {
804
+        if (empty($this->modx->_TVnames) || $reload) {
802 805
             $result = $this->query('SELECT `id`,`name`,`type` FROM ' . $this->makeTable('site_tmplvars'));
803
-            while ($row = $this->modx->db->GetRow($result)) {
806
+            while ($row = $this->modx->db->GetRow($result)) {
804 807
                 $this->modx->_TVnames[$row['name']] = array(
805 808
                     "id"   => $row['id'],
806 809
                     "type" => $row['type']
@@ -809,14 +812,16 @@  discard block
 block discarded – undo
809 812
         }
810 813
         $arrayTypes = array('checkbox', 'listbox-multiple');
811 814
         $arrayTVs = array();
812
-        foreach ($this->modx->_TVnames as $name => $data) {
815
+        foreach ($this->modx->_TVnames as $name => $data) {
813 816
             $this->tvid[$data['id']] = $name;
814 817
             $this->tv[$name] = $data['id'];
815
-            if (in_array($data['type'], $arrayTypes)) {
818
+            if (in_array($data['type'], $arrayTypes)) {
816 819
                 $arrayTVs[] = $name;
817 820
             }
818 821
         }
819
-        if (empty($this->tvaFields)) $this->tvaFields = $arrayTVs;
822
+        if (empty($this->tvaFields)) {
823
+            $this->tvaFields = $arrayTVs;
824
+        }
820 825
         $this->loadTVTemplate()->loadTVDefault(array_values($this->tv));
821 826
 
822 827
         return $this;
@@ -825,12 +830,12 @@  discard block
 block discarded – undo
825 830
     /**
826 831
      * @return $this
827 832
      */
828
-    protected function loadTVTemplate()
829
-    {
833
+    protected function loadTVTemplate()
834
+    {
830 835
         $q = $this->query("SELECT `tmplvarid`, `templateid` FROM " . $this->makeTable('site_tmplvar_templates'));
831 836
         $q = $this->modx->db->makeArray($q);
832 837
         $this->tvTpl = array();
833
-        foreach ($q as $item) {
838
+        foreach ($q as $item) {
834 839
             $this->tvTpl[$item['templateid']][] = $item['tmplvarid'];
835 840
         }
836 841
 
@@ -841,16 +846,16 @@  discard block
 block discarded – undo
841 846
      * @param array $tvId
842 847
      * @return $this
843 848
      */
844
-    protected function loadTVDefault(array $tvId = array())
845
-    {
846
-        if (is_array($tvId) && !empty($tvId)) {
849
+    protected function loadTVDefault(array $tvId = array())
850
+    {
851
+        if (is_array($tvId) && !empty($tvId)) {
847 852
             $tbl_site_tmplvars = $this->makeTable('site_tmplvars');
848 853
             $fields = 'id,name,default_text as value,display,display_params,type';
849 854
             $implodeTvId = implode(',', $tvId);
850 855
             $rs = $this->query("SELECT {$fields} FROM {$tbl_site_tmplvars} WHERE id IN({$implodeTvId})");
851 856
             $rows = $this->modx->db->makeArray($rs);
852 857
             $this->tvd = array();
853
-            foreach ($rows as $item) {
858
+            foreach ($rows as $item) {
854 859
                 $this->tvd[$item['name']] = $item;
855 860
             }
856 861
         }
@@ -863,17 +868,17 @@  discard block
 block discarded – undo
863 868
      * @return int
864 869
      * @throws Exception
865 870
      */
866
-    public function setTemplate($tpl)
867
-    {
868
-        if (!is_numeric($tpl) || $tpl != (int)$tpl) {
869
-            if (is_scalar($tpl)) {
871
+    public function setTemplate($tpl)
872
+    {
873
+        if (!is_numeric($tpl) || $tpl != (int)$tpl) {
874
+            if (is_scalar($tpl)) {
870 875
                 $sql = "SELECT `id` FROM {$this->makeTable('site_templates')} WHERE `templatename` = '" . $this->escape($tpl) . "'";
871 876
                 $rs = $this->query($sql);
872
-                if (!$rs || $this->modx->db->getRecordCount($rs) <= 0) {
877
+                if (!$rs || $this->modx->db->getRecordCount($rs) <= 0) {
873 878
                     throw new Exception("Template {$tpl} is not exists");
874 879
                 }
875 880
                 $tpl = $this->modx->db->getValue($rs);
876
-            } else {
881
+            } else {
877 882
                 throw new Exception("Invalid template name: " . print_r($tpl, 1));
878 883
             }
879 884
         }
@@ -884,14 +889,14 @@  discard block
 block discarded – undo
884 889
     /**
885 890
      * @return string
886 891
      */
887
-    protected function getAlias()
888
-    {
889
-        if ($this->modxConfig('friendly_urls') && $this->modxConfig('automatic_alias') && $this->get('alias') == '') {
892
+    protected function getAlias()
893
+    {
894
+        if ($this->modxConfig('friendly_urls') && $this->modxConfig('automatic_alias') && $this->get('alias') == '') {
890 895
             $alias = strtr($this->get('pagetitle'), $this->table);
891
-        } else {
892
-            if ($this->get('alias') != '') {
896
+        } else {
897
+            if ($this->get('alias') != '') {
893 898
                 $alias = $this->get('alias');
894
-            } else {
899
+            } else {
895 900
                 $alias = '';
896 901
             }
897 902
         }
@@ -908,10 +913,10 @@  discard block
 block discarded – undo
908 913
      *
909 914
      * Пересчет menuindex по полю таблицы site_content
910 915
      */
911
-    public function updateMenuindex($parent, $criteria = 'id', $dir = 'asc')
912
-    {
916
+    public function updateMenuindex($parent, $criteria = 'id', $dir = 'asc')
917
+    {
913 918
         $dir = strtolower($dir) == 'desc' ? 'desc' : 'asc';
914
-        if (is_integer($parent) && $criteria !== '') {
919
+        if (is_integer($parent) && $criteria !== '') {
915 920
             $this->query("SET @index := 0");
916 921
             $this->query("UPDATE {$this->makeTable('site_content')} SET `menuindex` = (@index := @index + 1) WHERE `parent`={$parent} ORDER BY {$criteria} {$dir}");
917 922
         }
@@ -924,37 +929,37 @@  discard block
 block discarded – undo
924 929
      *
925 930
      * @return $this
926 931
      */
927
-    public function setDefaultTemplate()
928
-    {
932
+    public function setDefaultTemplate()
933
+    {
929 934
         $parent = $this->get('parent');
930 935
         $template = $this->modxConfig('default_template');
931
-        switch ($this->modxConfig('auto_template_logic')) {
936
+        switch ($this->modxConfig('auto_template_logic')) {
932 937
             case 'sibling':
933
-                if (!$parent) {
938
+                if (!$parent) {
934 939
                     $site_start = $this->modxConfig('site_start');
935 940
                     $where = "sc.isfolder=0 AND sc.id!={$site_start}";
936 941
                     $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', $where, 'menuindex', 'ASC', 1);
937
-                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
942
+                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
938 943
                         $template = $sibl[0]['template'];
939 944
                     }
940
-                } else {
945
+                } else {
941 946
                     $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', 'isfolder=0', 'menuindex',
942 947
                         'ASC', 1);
943
-                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
948
+                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
944 949
                         $template = $sibl[0]['template'];
945
-                    } else {
950
+                    } else {
946 951
                         $sibl = $this->modx->getDocumentChildren($parent, 0, 0, 'template', 'isfolder=0', 'menuindex',
947 952
                             'ASC', 1);
948
-                        if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
953
+                        if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
949 954
                             $template = $sibl[0]['template'];
950 955
                         }
951 956
                     }
952 957
                 }
953 958
                 break;
954 959
             case 'parent':
955
-                if ($parent) {
960
+                if ($parent) {
956 961
                     $_parent = $this->modx->getPageInfo($parent, 0, 'template');
957
-                    if (isset($_parent['template'])) {
962
+                    if (isset($_parent['template'])) {
958 963
                         $template = $_parent['template'];
959 964
                     }
960 965
                 }
@@ -971,18 +976,18 @@  discard block
 block discarded – undo
971 976
      * @param  bool $store обновить распакованное поле
972 977
      * @return array ассоциативный массив с данными из json строки
973 978
      */
974
-    public function decodeField($field, $store = false)
975
-    {
979
+    public function decodeField($field, $store = false)
980
+    {
976 981
         $out = array();
977
-        if ($this->isDecodableField($field)) {
982
+        if ($this->isDecodableField($field)) {
978 983
             $data = $this->get($field);
979
-            if ($this->isTVarrayField($field)) {
984
+            if ($this->isTVarrayField($field)) {
980 985
                 $out = explode('||', $data);
981
-            } else {
986
+            } else {
982 987
                 $out = jsonHelper::jsonDecode($data, array('assoc' => true), true);
983 988
             }
984 989
         }
985
-        if ($store) {
990
+        if ($store) {
986 991
             $this->field[$field] = $out;
987 992
             $this->markAsDecode($field);
988 993
         }
@@ -996,18 +1001,18 @@  discard block
 block discarded – undo
996 1001
      * @param  bool $store обновить запакованное поле
997 1002
      * @return string|null json строка
998 1003
      */
999
-    public function encodeField($field, $store = false)
1000
-    {
1004
+    public function encodeField($field, $store = false)
1005
+    {
1001 1006
         $out = null;
1002
-        if ($this->isEncodableField($field)) {
1007
+        if ($this->isEncodableField($field)) {
1003 1008
             $data = $this->get($field);
1004
-            if ($this->isTVarrayField($field)) {
1009
+            if ($this->isTVarrayField($field)) {
1005 1010
                 $out = is_array($data) ? implode('||', $data) : $data;
1006
-            } else {
1011
+            } else {
1007 1012
                 $out = json_encode($data);
1008 1013
             }
1009 1014
         }
1010
-        if ($store) {
1015
+        if ($store) {
1011 1016
             $this->field[$field] = $out;
1012 1017
             $this->markAsEncode($field);
1013 1018
         }
@@ -1020,8 +1025,8 @@  discard block
 block discarded – undo
1020 1025
      * @param  string $field имя поля
1021 1026
      * @return boolean
1022 1027
      */
1023
-    public function isTVarrayField($field)
1024
-    {
1028
+    public function isTVarrayField($field)
1029
+    {
1025 1030
         return (is_scalar($field) && in_array($field, $this->tvaFields));
1026 1031
     }
1027 1032
 
@@ -1029,10 +1034,10 @@  discard block
 block discarded – undo
1029 1034
      * Пометить все поля как запакованные
1030 1035
      * @return $this
1031 1036
      */
1032
-    public function markAllEncode()
1033
-    {
1037
+    public function markAllEncode()
1038
+    {
1034 1039
         parent::markAllEncode();
1035
-        foreach ($this->tvaFields as $field) {
1040
+        foreach ($this->tvaFields as $field) {
1036 1041
             $this->markAsEncode($field);
1037 1042
         }
1038 1043
 
@@ -1043,10 +1048,10 @@  discard block
 block discarded – undo
1043 1048
      * Пометить все поля как распакованные
1044 1049
      * @return $this
1045 1050
      */
1046
-    public function markAllDecode()
1047
-    {
1051
+    public function markAllDecode()
1052
+    {
1048 1053
         parent::markAllDecode();
1049
-        foreach ($this->tvaFields as $field) {
1054
+        foreach ($this->tvaFields as $field) {
1050 1055
             $this->markAsDecode($field);
1051 1056
         }
1052 1057
 
@@ -1056,16 +1061,17 @@  discard block
 block discarded – undo
1056 1061
     /**
1057 1062
      * @param int $docId
1058 1063
      */
1059
-    public function getDocumentGroups($docId = 0) {
1064
+    public function getDocumentGroups($docId = 0)
1065
+    {
1060 1066
         $out = array();
1061 1067
         $doc = $this->switchObject($docId);
1062
-        if (null !== $doc->getID()) {
1068
+        if (null !== $doc->getID()) {
1063 1069
             $doc_groups = $this->makeTable('document_groups');
1064 1070
             $docgroup_names = $this->makeTable('documentgroup_names');
1065 1071
 
1066 1072
             $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`
1067 1073
                 WHERE `dg`.`document` = " . $doc->getID());
1068
-            while ($row = $this->modx->db->getRow($rs)) {
1074
+            while ($row = $this->modx->db->getRow($rs)) {
1069 1075
                 $out[$row['document_group']] = $row['name'];
1070 1076
             }
1071 1077
 
@@ -1080,18 +1086,20 @@  discard block
 block discarded – undo
1080 1086
      * @param array $groupIds
1081 1087
      * @return $this
1082 1088
      */
1083
-    public function setDocumentGroups($docId = 0, $groupIds = array())
1084
-    {
1085
-        if (!is_array($groupIds)) return $this;
1086
-        if ($this->newDoc && $docId == 0) {
1089
+    public function setDocumentGroups($docId = 0, $groupIds = array())
1090
+    {
1091
+        if (!is_array($groupIds)) {
1092
+            return $this;
1093
+        }
1094
+        if ($this->newDoc && $docId == 0) {
1087 1095
             $this->groupIds = $groupIds;
1088
-        } else {
1096
+        } else {
1089 1097
             $doc = $this->switchObject($docId);
1090
-            if ($id = $doc->getID()) {
1091
-                foreach ($groupIds as $gid) {
1098
+            if ($id = $doc->getID()) {
1099
+                foreach ($groupIds as $gid) {
1092 1100
                     $this->query("REPLACE INTO {$this->makeTable('document_groups')} (`document_group`, `document`) VALUES ('{$gid}', '{$id}')");
1093 1101
                 }
1094
-                if (!$this->newDoc) {
1102
+                if (!$this->newDoc) {
1095 1103
                     $groupIds = empty($groupIds) ? '0' : implode(',', $groupIds);
1096 1104
                     $this->query("DELETE FROM {$this->makeTable('document_groups')} WHERE `document`={$id} AND `document_group` NOT IN ({$groupIds})");
1097 1105
                 }
Please login to merge, or discard this patch.