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