Completed
Push — master ( 0a9665...84b8bb )
by
unknown
02:37
created
assets/lib/MODxAPI/modResource.php 2 patches
Spacing   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
         $tpl = $this->get('template');
187 187
         $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array());
188 188
         foreach ($tvTPL as $item) {
189
-            if (isset($this->tvid[$item]) && !array_key_exists($this->tvid[$item], $out)) {
189
+            if (isset($this->tvid[$item]) && ! array_key_exists($this->tvid[$item], $out)) {
190 190
                 $out[$this->tvid[$item]] = $this->get($this->tvid[$item]);
191 191
             }
192 192
         }
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
     {
225 225
         $out = null;
226 226
         $id = (int)$this->getID();
227
-        if (!empty($id)) {
227
+        if ( ! empty($id)) {
228 228
             $out = $this->modx->makeUrl($id);
229 229
         }
230 230
 
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
      */
316 316
     public function set($key, $value)
317 317
     {
318
-        if (is_scalar($value) && is_scalar($key) && !empty($key)) {
318
+        if (is_scalar($value) && is_scalar($key) && ! empty($key)) {
319 319
             switch ($key) {
320 320
                 case 'parent':
321 321
                     $value = (int)$value;
@@ -388,12 +388,12 @@  discard block
 block discarded – undo
388 388
     {
389 389
         $currentAdmin = APIHelpers::getkey($_SESSION, 'mgrInternalKey', 0);
390 390
         $value = (int)$value;
391
-        if (!empty($value)) {
391
+        if ( ! empty($value)) {
392 392
             $by = $this->findUserBy($value);
393
-            $exists = $this->managerUsers->exists(function ($key, Helpers\Collection $val) use ($by, $value) {
393
+            $exists = $this->managerUsers->exists(function($key, Helpers\Collection $val) use ($by, $value) {
394 394
                 return ($val->containsKey($by) && $val->get($by) === (string)$value);
395 395
             });
396
-            if (!$exists) {
396
+            if ( ! $exists) {
397 397
                 $value = 0;
398 398
             }
399 399
         }
@@ -434,11 +434,11 @@  discard block
 block discarded – undo
434 434
     protected function getTime($value)
435 435
     {
436 436
         $value = trim($value);
437
-        if (!empty($value)) {
438
-            if (!is_numeric($value)) {
437
+        if ( ! empty($value)) {
438
+            if ( ! is_numeric($value)) {
439 439
                 $value = (int)strtotime($value);
440 440
             }
441
-            if (!empty($value)) {
441
+            if ( ! empty($value)) {
442 442
                 $value += $this->modxConfig('server_offset_time');
443 443
             }
444 444
         }
@@ -454,12 +454,12 @@  discard block
 block discarded – undo
454 454
     {
455 455
         $this->close();
456 456
         $fld = array();
457
-        foreach($this->tvd as $name => $tv) {
457
+        foreach ($this->tvd as $name => $tv) {
458 458
             $fld[$name] = $tv['value'];
459 459
         };
460 460
         $this->store($fld);
461 461
 
462
-        $this->fromArray(array_merge($fld,$data));
462
+        $this->fromArray(array_merge($fld, $data));
463 463
         $this->set('createdby', null)
464 464
             ->set('editedby', null)
465 465
             ->set('createdon', time())
@@ -516,8 +516,8 @@  discard block
 block discarded – undo
516 516
 
517 517
         if (
518 518
             $this->field['parent'] == 0 &&
519
-            !$this->modxConfig('udperms_allowroot') &&
520
-            !($uid && isset($_SESSION['mgrRole']) && $_SESSION['mgrRole'] == 1)
519
+            ! $this->modxConfig('udperms_allowroot') &&
520
+            ! ($uid && isset($_SESSION['mgrRole']) && $_SESSION['mgrRole'] == 1)
521 521
         ) {
522 522
             $this->log['rootForbidden'] = 'Only Administrators can create documents in the root folder because udperms_allowroot setting is off';
523 523
 
@@ -536,7 +536,7 @@  discard block
 block discarded – undo
536 536
         $fld = $this->toArray(null, null, null, false);
537 537
         foreach ($this->default_field as $key => $value) {
538 538
             $tmp = $this->get($key);
539
-            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
539
+            if ($this->newDoc && ( ! is_int($tmp) && $tmp == '')) {
540 540
                 if ($tmp == $value) {
541 541
                     switch ($key) {
542 542
                         case 'cacheable':
@@ -568,7 +568,7 @@  discard block
 block discarded – undo
568 568
                     $this->field[$key] = $parent;
569 569
                     $this->Uset($key);
570 570
                     break;
571
-                case ($key == 'alias_visible' && !$this->checkVersion('1.0.10', true)):
571
+                case ($key == 'alias_visible' && ! $this->checkVersion('1.0.10', true)):
572 572
                     $this->eraseField('alias_visible');
573 573
                     break;
574 574
                 default:
@@ -577,7 +577,7 @@  discard block
 block discarded – undo
577 577
             unset($fld[$key]);
578 578
         }
579 579
 
580
-        if (!empty($this->set)) {
580
+        if ( ! empty($this->set)) {
581 581
             if ($this->newDoc) {
582 582
                 $SQL = "INSERT into {$this->makeTable('site_content')} SET " . implode(', ', $this->set);
583 583
             } else {
@@ -597,7 +597,7 @@  discard block
 block discarded – undo
597 597
 
598 598
         $_deleteTVs = $_updateTVs = $_insertTVs = array();
599 599
         foreach ($fld as $key => $value) {
600
-            if (empty($this->tv[$key]) || !$this->isChanged($key)) {
600
+            if (empty($this->tv[$key]) || ! $this->isChanged($key)) {
601 601
                 continue;
602 602
             } elseif ($value === '') {
603 603
                 $_deleteTVs[] = $this->tv[$key];
@@ -606,37 +606,37 @@  discard block
 block discarded – undo
606 606
             }
607 607
         }
608 608
 
609
-        if (!$this->newDoc && !empty($_insertTVs)) {
610
-            $ids = implode(',',array_keys($_insertTVs));
609
+        if ( ! $this->newDoc && ! empty($_insertTVs)) {
610
+            $ids = implode(',', array_keys($_insertTVs));
611 611
             $result = $this->query("SELECT `tmplvarid` FROM {$this->makeTable('site_tmplvar_contentvalues')} WHERE `contentid`={$this->id} AND `tmplvarid` IN ({$ids})");
612
-            $existedTVs = $this->modx->db->getColumn('tmplvarid',$result);
612
+            $existedTVs = $this->modx->db->getColumn('tmplvarid', $result);
613 613
             foreach ($existedTVs as $id) {
614 614
                 $_updateTVs[$id] = $_insertTVs[$id];
615 615
                 unset($_insertTVs[$id]);
616 616
             }
617 617
         }
618 618
 
619
-        if (!empty($_updateTVs)) {
620
-            foreach($_updateTVs as $id => $value) {
619
+        if ( ! empty($_updateTVs)) {
620
+            foreach ($_updateTVs as $id => $value) {
621 621
                 $this->query("UPDATE {$this->makeTable('site_tmplvar_contentvalues')} SET `value` = '{$value}' WHERE `contentid` = {$this->id} AND `tmplvarid` = {$id}");
622 622
             }
623 623
         }
624 624
 
625
-        if (!empty($_insertTVs)) {
625
+        if ( ! empty($_insertTVs)) {
626 626
             $values = array();
627 627
             foreach ($_insertTVs as $id => $value) {
628 628
                 $values[] = "({$this->id}, {$id}, '{$value}')";
629 629
             }
630
-            $values = implode(',',$values);
630
+            $values = implode(',', $values);
631 631
             $this->query("INSERT into {$this->makeTable('site_tmplvar_contentvalues')} (`contentid`,`tmplvarid`,`value`) VALUES {$values}");
632 632
         }
633 633
 
634
-        if (!empty($_deleteTVs)) {
635
-            $ids = implode(',',$_deleteTVs);
634
+        if ( ! empty($_deleteTVs)) {
635
+            $ids = implode(',', $_deleteTVs);
636 636
             $this->query("DELETE FROM {$this->makeTable('site_tmplvar_contentvalues')} WHERE `contentid` = '{$this->id}' AND `tmplvarid` IN ({$ids})");
637 637
         }
638 638
 
639
-        if (!isset($this->mode)) {
639
+        if ( ! isset($this->mode)) {
640 640
             $this->mode = $this->newDoc ? "new" : "upd";
641 641
             $this->newDoc = false;
642 642
         }
@@ -711,7 +711,7 @@  discard block
 block discarded – undo
711 711
         $_ids = $this->cleanIDs($ids, ',');
712 712
         if (is_array($_ids) && $_ids != array()) {
713 713
             $id = $this->sanitarIn($_ids);
714
-            if (!empty($id)) {
714
+            if ( ! empty($id)) {
715 715
                 $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} where `parent` IN ({$id})");
716 716
                 $id = $this->modx->db->getColumn('id', $q);
717 717
                 if ($depth > 0 || $depth === true) {
@@ -776,12 +776,12 @@  discard block
 block discarded – undo
776 776
         $alias = strtolower($alias);
777 777
         if ($this->modxConfig('friendly_urls')) {
778 778
             $_alias = $this->escape($alias);
779
-            if ((!$this->modxConfig('allow_duplicate_alias') && !$this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) {
779
+            if (( ! $this->modxConfig('allow_duplicate_alias') && ! $this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) {
780 780
                 $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' AND `parent`={$this->get('parent')} LIMIT 1"));
781 781
             } else {
782 782
                 $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' LIMIT 1"));
783 783
             }
784
-            if (($flag && $this->newDoc) || (!$this->newDoc && $flag && $this->id != $flag)) {
784
+            if (($flag && $this->newDoc) || ( ! $this->newDoc && $flag && $this->id != $flag)) {
785 785
                 $suffix = substr($alias, -2);
786 786
                 if (preg_match('/-(\d+)/', $suffix, $tmp) && isset($tmp[1]) && (int)$tmp[1] > 1) {
787 787
                     $suffix = (int)$tmp[1] + 1;
@@ -847,7 +847,7 @@  discard block
 block discarded – undo
847 847
      */
848 848
     protected function loadTVDefault(array $tvId = array())
849 849
     {
850
-        if (is_array($tvId) && !empty($tvId)) {
850
+        if (is_array($tvId) && ! empty($tvId)) {
851 851
             $tbl_site_tmplvars = $this->makeTable('site_tmplvars');
852 852
             $fields = 'id,name,default_text as value,display,display_params,type';
853 853
             $implodeTvId = implode(',', $tvId);
@@ -869,11 +869,11 @@  discard block
 block discarded – undo
869 869
      */
870 870
     public function setTemplate($tpl)
871 871
     {
872
-        if (!is_numeric($tpl) || $tpl != (int)$tpl) {
872
+        if ( ! is_numeric($tpl) || $tpl != (int)$tpl) {
873 873
             if (is_scalar($tpl)) {
874 874
                 $sql = "SELECT `id` FROM {$this->makeTable('site_templates')} WHERE `templatename` = '" . $this->escape($tpl) . "'";
875 875
                 $rs = $this->query($sql);
876
-                if (!$rs || $this->modx->db->getRecordCount($rs) <= 0) {
876
+                if ( ! $rs || $this->modx->db->getRecordCount($rs) <= 0) {
877 877
                     throw new Exception("Template {$tpl} is not exists");
878 878
                 }
879 879
                 $tpl = $this->modx->db->getValue($rs);
@@ -934,7 +934,7 @@  discard block
 block discarded – undo
934 934
         $template = $this->modxConfig('default_template');
935 935
         switch ($this->modxConfig('auto_template_logic')) {
936 936
             case 'sibling':
937
-                if (!$parent) {
937
+                if ( ! $parent) {
938 938
                     $site_start = $this->modxConfig('site_start');
939 939
                     $where = "sc.isfolder=0 AND sc.id!={$site_start}";
940 940
                     $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', $where, 'menuindex', 'ASC', 1);
Please login to merge, or discard this patch.
Braces   +158 added lines, -158 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
      */
@@ -155,8 +155,8 @@  discard block
 block discarded – undo
155 155
      * @param DocumentParser $modx
156 156
      * @param bool $debug
157 157
      */
158
-    public function __construct($modx, $debug = false)
159
-    {
158
+    public function __construct($modx, $debug = false)
159
+    {
160 160
         parent::__construct($modx, $debug);
161 161
         $this->get_TV();
162 162
         $uTable = $this->makeTable("manager_users");
@@ -169,8 +169,8 @@  discard block
 block discarded – undo
169 169
     /**
170 170
      * @return array
171 171
      */
172
-    public function toArrayMain()
173
-    {
172
+    public function toArrayMain()
173
+    {
174 174
         $out = array_intersect_key(parent::toArray(), $this->default_field);
175 175
 
176 176
         return $out;
@@ -180,18 +180,18 @@  discard block
 block discarded – undo
180 180
      * @param bool $render
181 181
      * @return array
182 182
      */
183
-    public function toArrayTV($render = false)
184
-    {
183
+    public function toArrayTV($render = false)
184
+    {
185 185
         $out = array_diff_key(parent::toArray(), $this->default_field);
186 186
         $tpl = $this->get('template');
187 187
         $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array());
188
-        foreach ($tvTPL as $item) {
189
-            if (isset($this->tvid[$item]) && !array_key_exists($this->tvid[$item], $out)) {
188
+        foreach ($tvTPL as $item) {
189
+            if (isset($this->tvid[$item]) && !array_key_exists($this->tvid[$item], $out)) {
190 190
                 $out[$this->tvid[$item]] = $this->get($this->tvid[$item]);
191 191
             }
192 192
         }
193
-        if ($render) {
194
-            foreach ($out as $key => $val) {
193
+        if ($render) {
194
+            foreach ($out as $key => $val) {
195 195
                 $out[$key] = $this->renderTV($key);
196 196
             }
197 197
         }
@@ -206,8 +206,8 @@  discard block
 block discarded – undo
206 206
      * @param bool $render
207 207
      * @return array
208 208
      */
209
-    public function toArray($prefix = '', $suffix = '', $sep = '_', $render = true)
210
-    {
209
+    public function toArray($prefix = '', $suffix = '', $sep = '_', $render = true)
210
+    {
211 211
         $out = array_merge(
212 212
             $this->toArrayMain(),
213 213
             $this->toArrayTV($render),
@@ -220,11 +220,11 @@  discard block
 block discarded – undo
220 220
     /**
221 221
      * @return null|string
222 222
      */
223
-    public function getUrl()
224
-    {
223
+    public function getUrl()
224
+    {
225 225
         $out = null;
226 226
         $id = (int)$this->getID();
227
-        if (!empty($id)) {
227
+        if (!empty($id)) {
228 228
             $out = $this->modx->makeUrl($id);
229 229
         }
230 230
 
@@ -236,10 +236,10 @@  discard block
 block discarded – undo
236 236
      * @param string $second
237 237
      * @return mixed
238 238
      */
239
-    public function getTitle($main = 'menutitle', $second = 'pagetitle')
240
-    {
239
+    public function getTitle($main = 'menutitle', $second = 'pagetitle')
240
+    {
241 241
         $title = $this->get($main);
242
-        if (empty($title) && $title !== '0') {
242
+        if (empty($title) && $title !== '0') {
243 243
             $title = $this->get($second);
244 244
         }
245 245
 
@@ -249,8 +249,8 @@  discard block
 block discarded – undo
249 249
     /**
250 250
      * @return bool
251 251
      */
252
-    public function isWebShow()
253
-    {
252
+    public function isWebShow()
253
+    {
254 254
         $pub = ($this->get('publishedon') < time() && $this->get('published'));
255 255
         $unpub = ($this->get('unpub_date') == 0 || $this->get('unpub_date') > time());
256 256
         $del = ($this->get('deleted') == 0 && ($this->get('deletedon') == 0 || $this->get('deletedon') > time()));
@@ -261,8 +261,8 @@  discard block
 block discarded – undo
261 261
     /**
262 262
      * @return $this
263 263
      */
264
-    public function touch()
265
-    {
264
+    public function touch()
265
+    {
266 266
         $this->set('editedon', time());
267 267
 
268 268
         return $this;
@@ -272,10 +272,10 @@  discard block
 block discarded – undo
272 272
      * @param $tvname
273 273
      * @return null|string
274 274
      */
275
-    public function renderTV($tvname)
276
-    {
275
+    public function renderTV($tvname)
276
+    {
277 277
         $out = null;
278
-        if ($this->getID() > 0) {
278
+        if ($this->getID() > 0) {
279 279
             include_once MODX_MANAGER_PATH . "includes/tmplvars.format.inc.php";
280 280
             include_once MODX_MANAGER_PATH . "includes/tmplvars.commands.inc.php";
281 281
             $tvval = $this->get($tvname);
@@ -293,14 +293,14 @@  discard block
 block discarded – undo
293 293
      * @param $key
294 294
      * @return mixed
295 295
      */
296
-    public function get($key)
297
-    {
296
+    public function get($key)
297
+    {
298 298
         $out = parent::get($key);
299
-        if (isset($this->tv[$key])) {
299
+        if (isset($this->tv[$key])) {
300 300
             $tpl = $this->get('template');
301 301
             $tvTPL = APIHelpers::getkey($this->tvTpl, $tpl, array());
302 302
             $tvID = APIHelpers::getkey($this->tv, $key, 0);
303
-            if (in_array($tvID, $tvTPL) && is_null($out)) {
303
+            if (in_array($tvID, $tvTPL) && is_null($out)) {
304 304
                 $out = APIHelpers::getkey($this->tvd[$key], 'value', null);
305 305
             }
306 306
         }
@@ -313,10 +313,10 @@  discard block
 block discarded – undo
313 313
      * @param $value
314 314
      * @return $this
315 315
      */
316
-    public function set($key, $value)
317
-    {
318
-        if (is_scalar($value) && is_scalar($key) && !empty($key)) {
319
-            switch ($key) {
316
+    public function set($key, $value)
317
+    {
318
+        if (is_scalar($value) && is_scalar($key) && !empty($key)) {
319
+            switch ($key) {
320 320
                 case 'parent':
321 321
                     $value = (int)$value;
322 322
                     break;
@@ -326,38 +326,38 @@  discard block
 block discarded – undo
326 326
                     break;
327 327
                 case 'published':
328 328
                     $value = (int)((bool)$value);
329
-                    if ($value) {
329
+                    if ($value) {
330 330
                         $this->field['publishedon'] = time() + $this->modxConfig('server_offset_time');
331 331
                     }
332 332
                     break;
333 333
                 case 'pub_date':
334 334
                     $value = $this->getTime($value);
335
-                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) {
335
+                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') > $value) {
336 336
                         $this->field['published'] = 1;
337 337
                         $this->field['publishedon'] = $value;
338 338
                     }
339 339
                     break;
340 340
                 case 'unpub_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'] = 0;
344 344
                         $this->field['publishedon'] = 0;
345 345
                     }
346 346
                     break;
347 347
                 case 'deleted':
348 348
                     $value = (int)((bool)$value);
349
-                    if ($value) {
349
+                    if ($value) {
350 350
                         $this->field['deletedon'] = time() + $this->modxConfig('server_offset_time');
351
-                    } else {
351
+                    } else {
352 352
                         $this->field['deletedon'] = 0;
353 353
                     }
354 354
                     break;
355 355
                 case 'deletedon':
356 356
                     $value = $this->getTime($value);
357
-                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') < $value) {
357
+                    if ($value > 0 && time() + $this->modxConfig('server_offset_time') < $value) {
358 358
                         $value = 0;
359 359
                     }
360
-                    if ($value) {
360
+                    if ($value) {
361 361
                         $this->field['deleted'] = 1;
362 362
                     }
363 363
                     break;
@@ -384,20 +384,20 @@  discard block
 block discarded – undo
384 384
      * @param int $default
385 385
      * @return int|mixed
386 386
      */
387
-    protected function getUser($value, $default = 0)
388
-    {
387
+    protected function getUser($value, $default = 0)
388
+    {
389 389
         $currentAdmin = APIHelpers::getkey($_SESSION, 'mgrInternalKey', 0);
390 390
         $value = (int)$value;
391
-        if (!empty($value)) {
391
+        if (!empty($value)) {
392 392
             $by = $this->findUserBy($value);
393
-            $exists = $this->managerUsers->exists(function ($key, Helpers\Collection $val) use ($by, $value) {
393
+            $exists = $this->managerUsers->exists(function ($key, Helpers\Collection $val) use ($by, $value) {
394 394
                 return ($val->containsKey($by) && $val->get($by) === (string)$value);
395 395
             });
396
-            if (!$exists) {
396
+            if (!$exists) {
397 397
                 $value = 0;
398 398
             }
399 399
         }
400
-        if (empty($value)) {
400
+        if (empty($value)) {
401 401
             $value = empty($currentAdmin) ? $default : $currentAdmin;
402 402
         }
403 403
 
@@ -408,9 +408,9 @@  discard block
 block discarded – undo
408 408
      * @param $data
409 409
      * @return bool|string
410 410
      */
411
-    protected function findUserBy($data)
412
-    {
413
-        switch (true) {
411
+    protected function findUserBy($data)
412
+    {
413
+        switch (true) {
414 414
             case (is_int($data) || ((int)$data > 0 && (string)intval($data) === $data)):
415 415
                 $find = 'id';
416 416
                 break;
@@ -431,14 +431,14 @@  discard block
 block discarded – undo
431 431
      * @param $value
432 432
      * @return int|mixed|string
433 433
      */
434
-    protected function getTime($value)
435
-    {
434
+    protected function getTime($value)
435
+    {
436 436
         $value = trim($value);
437
-        if (!empty($value)) {
438
-            if (!is_numeric($value)) {
437
+        if (!empty($value)) {
438
+            if (!is_numeric($value)) {
439 439
                 $value = (int)strtotime($value);
440 440
             }
441
-            if (!empty($value)) {
441
+            if (!empty($value)) {
442 442
                 $value += $this->modxConfig('server_offset_time');
443 443
             }
444 444
         }
@@ -450,11 +450,11 @@  discard block
 block discarded – undo
450 450
      * @param array $data
451 451
      * @return $this
452 452
      */
453
-    public function create($data = array())
454
-    {
453
+    public function create($data = array())
454
+    {
455 455
         $this->close();
456 456
         $fld = array();
457
-        foreach($this->tvd as $name => $tv) {
457
+        foreach($this->tvd as $name => $tv) {
458 458
             $fld[$name] = $tv['value'];
459 459
         };
460 460
         $this->store($fld);
@@ -472,22 +472,22 @@  discard block
 block discarded – undo
472 472
      * @param $id
473 473
      * @return $this
474 474
      */
475
-    public function edit($id)
476
-    {
475
+    public function edit($id)
476
+    {
477 477
         $id = is_scalar($id) ? trim($id) : '';
478
-        if ($this->getID() != $id) {
478
+        if ($this->getID() != $id) {
479 479
             $this->close();
480 480
             $this->newDoc = false;
481 481
 
482 482
             $result = $this->query("SELECT * from {$this->makeTable('site_content')} where `id`=" . (int)$id);
483 483
             $this->fromArray($this->modx->db->getRow($result));
484 484
             $result = $this->query("SELECT * from {$this->makeTable('site_tmplvar_contentvalues')} where `contentid`=" . (int)$id);
485
-            while ($row = $this->modx->db->getRow($result)) {
485
+            while ($row = $this->modx->db->getRow($result)) {
486 486
                 $this->field[$this->tvid[$row['tmplvarid']]] = $row['value'];
487 487
             }
488
-            if (empty($this->field['id'])) {
488
+            if (empty($this->field['id'])) {
489 489
                 $this->id = null;
490
-            } else {
490
+            } else {
491 491
                 $this->id = $this->field['id'];
492 492
                 $this->set('editedby', null)->touch();
493 493
             }
@@ -503,10 +503,10 @@  discard block
 block discarded – undo
503 503
      * @param bool $clearCache
504 504
      * @return bool|null|void
505 505
      */
506
-    public function save($fire_events = false, $clearCache = false)
507
-    {
506
+    public function save($fire_events = false, $clearCache = false)
507
+    {
508 508
         $parent = null;
509
-        if ($this->field['pagetitle'] == '') {
509
+        if ($this->field['pagetitle'] == '') {
510 510
             $this->log['emptyPagetitle'] = 'Pagetitle is empty in <pre>' . print_r($this->field, true) . '</pre>';
511 511
 
512 512
             return false;
@@ -518,7 +518,7 @@  discard block
 block discarded – undo
518 518
             $this->field['parent'] == 0 &&
519 519
             !$this->modxConfig('udperms_allowroot') &&
520 520
             !($uid && isset($_SESSION['mgrRole']) && $_SESSION['mgrRole'] == 1)
521
-        ) {
521
+        ) {
522 522
             $this->log['rootForbidden'] = 'Only Administrators can create documents in the root folder because udperms_allowroot setting is off';
523 523
 
524 524
             return false;
@@ -534,11 +534,11 @@  discard block
 block discarded – undo
534 534
         ), $fire_events);
535 535
 
536 536
         $fld = $this->toArray(null, null, null, false);
537
-        foreach ($this->default_field as $key => $value) {
537
+        foreach ($this->default_field as $key => $value) {
538 538
             $tmp = $this->get($key);
539
-            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
540
-                if ($tmp == $value) {
541
-                    switch ($key) {
539
+            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
540
+                if ($tmp == $value) {
541
+                    switch ($key) {
542 542
                         case 'cacheable':
543 543
                             $value = $this->modxConfig('cache_default');
544 544
                             break;
@@ -558,11 +558,11 @@  discard block
 block discarded – undo
558 558
                 }
559 559
                 $this->field[$key] = $value;
560 560
             }
561
-            switch (true) {
561
+            switch (true) {
562 562
                 case $key == 'parent':
563 563
                     $parent = (int)$this->get($key);
564 564
                     $q = $this->query("SELECT count(`id`) FROM {$this->makeTable('site_content')} WHERE `id`='{$parent}'");
565
-                    if ($this->modx->db->getValue($q) != 1) {
565
+                    if ($this->modx->db->getValue($q) != 1) {
566 566
                         $parent = 0;
567 567
                     }
568 568
                     $this->field[$key] = $parent;
@@ -577,66 +577,66 @@  discard block
 block discarded – undo
577 577
             unset($fld[$key]);
578 578
         }
579 579
 
580
-        if (!empty($this->set)) {
581
-            if ($this->newDoc) {
580
+        if (!empty($this->set)) {
581
+            if ($this->newDoc) {
582 582
                 $SQL = "INSERT into {$this->makeTable('site_content')} SET " . implode(', ', $this->set);
583
-            } else {
583
+            } else {
584 584
                 $SQL = "UPDATE {$this->makeTable('site_content')} SET " . implode(', ',
585 585
                         $this->set) . " WHERE `id` = " . $this->id;
586 586
             }
587 587
             $this->query($SQL);
588 588
 
589
-            if ($this->newDoc) {
589
+            if ($this->newDoc) {
590 590
                 $this->id = $this->modx->db->getInsertId();
591 591
             }
592 592
 
593
-            if ($parent > 0) {
593
+            if ($parent > 0) {
594 594
                 $this->query("UPDATE {$this->makeTable('site_content')} SET `isfolder`='1' WHERE `id`='{$parent}'");
595 595
             }
596 596
         }
597 597
 
598 598
         $_deleteTVs = $_updateTVs = $_insertTVs = array();
599
-        foreach ($fld as $key => $value) {
600
-            if (empty($this->tv[$key]) || !$this->isChanged($key)) {
599
+        foreach ($fld as $key => $value) {
600
+            if (empty($this->tv[$key]) || !$this->isChanged($key)) {
601 601
                 continue;
602
-            } elseif ($value === '') {
602
+            } elseif ($value === '') {
603 603
                 $_deleteTVs[] = $this->tv[$key];
604
-            } else {
604
+            } else {
605 605
                 $_insertTVs[$this->tv[$key]] = $this->escape($value);
606 606
             }
607 607
         }
608 608
 
609
-        if (!$this->newDoc && !empty($_insertTVs)) {
609
+        if (!$this->newDoc && !empty($_insertTVs)) {
610 610
             $ids = implode(',',array_keys($_insertTVs));
611 611
             $result = $this->query("SELECT `tmplvarid` FROM {$this->makeTable('site_tmplvar_contentvalues')} WHERE `contentid`={$this->id} AND `tmplvarid` IN ({$ids})");
612 612
             $existedTVs = $this->modx->db->getColumn('tmplvarid',$result);
613
-            foreach ($existedTVs as $id) {
613
+            foreach ($existedTVs as $id) {
614 614
                 $_updateTVs[$id] = $_insertTVs[$id];
615 615
                 unset($_insertTVs[$id]);
616 616
             }
617 617
         }
618 618
 
619
-        if (!empty($_updateTVs)) {
620
-            foreach($_updateTVs as $id => $value) {
619
+        if (!empty($_updateTVs)) {
620
+            foreach($_updateTVs as $id => $value) {
621 621
                 $this->query("UPDATE {$this->makeTable('site_tmplvar_contentvalues')} SET `value` = '{$value}' WHERE `contentid` = {$this->id} AND `tmplvarid` = {$id}");
622 622
             }
623 623
         }
624 624
 
625
-        if (!empty($_insertTVs)) {
625
+        if (!empty($_insertTVs)) {
626 626
             $values = array();
627
-            foreach ($_insertTVs as $id => $value) {
627
+            foreach ($_insertTVs as $id => $value) {
628 628
                 $values[] = "({$this->id}, {$id}, '{$value}')";
629 629
             }
630 630
             $values = implode(',',$values);
631 631
             $this->query("INSERT into {$this->makeTable('site_tmplvar_contentvalues')} (`contentid`,`tmplvarid`,`value`) VALUES {$values}");
632 632
         }
633 633
 
634
-        if (!empty($_deleteTVs)) {
634
+        if (!empty($_deleteTVs)) {
635 635
             $ids = implode(',',$_deleteTVs);
636 636
             $this->query("DELETE FROM {$this->makeTable('site_tmplvar_contentvalues')} WHERE `contentid` = '{$this->id}' AND `tmplvarid` IN ({$ids})");
637 637
         }
638 638
 
639
-        if (!isset($this->mode)) {
639
+        if (!isset($this->mode)) {
640 640
             $this->mode = $this->newDoc ? "new" : "upd";
641 641
             $this->newDoc = false;
642 642
         }
@@ -647,7 +647,7 @@  discard block
 block discarded – undo
647 647
             'docObj' => $this
648 648
         ), $fire_events);
649 649
 
650
-        if ($clearCache) {
650
+        if ($clearCache) {
651 651
             $this->clearCache($fire_events);
652 652
         }
653 653
 
@@ -659,16 +659,16 @@  discard block
 block discarded – undo
659 659
      * @return $this
660 660
      * @throws Exception
661 661
      */
662
-    public function toTrash($ids)
663
-    {
662
+    public function toTrash($ids)
663
+    {
664 664
         $ignore = $this->systemID();
665 665
         $_ids = $this->cleanIDs($ids, ',', $ignore);
666
-        if (is_array($_ids) && $_ids != array()) {
666
+        if (is_array($_ids) && $_ids != array()) {
667 667
             $id = $this->sanitarIn($_ids);
668 668
             $uid = (int)$this->modx->getLoginUserId();
669 669
             $deletedon = time() + $this->modxConfig('server_offset_time');
670 670
             $this->query("UPDATE {$this->makeTable('site_content')} SET `deleted`=1, `deletedby`={$uid}, `deletedon`={$deletedon} WHERE `id` IN ({$id})");
671
-        } else {
671
+        } else {
672 672
             throw new Exception('Invalid IDs list for mark trash: <pre>' . print_r($ids,
673 673
                     1) . '</pre> please, check ignore list: <pre>' . print_r($ignore, 1) . '</pre>');
674 674
         }
@@ -680,11 +680,11 @@  discard block
 block discarded – undo
680 680
      * @param bool $fire_events
681 681
      * @return $this
682 682
      */
683
-    public function clearTrash($fire_events = false)
684
-    {
683
+    public function clearTrash($fire_events = false)
684
+    {
685 685
         $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `deleted`='1'");
686 686
         $_ids = $this->modx->db->getColumn('id', $q);
687
-        if (is_array($_ids) && $_ids != array()) {
687
+        if (is_array($_ids) && $_ids != array()) {
688 688
             $this->invokeEvent('OnBeforeEmptyTrash', array(
689 689
                 "ids" => $_ids
690 690
             ), $fire_events);
@@ -706,15 +706,15 @@  discard block
 block discarded – undo
706 706
      * @param int|bool $depth
707 707
      * @return array
708 708
      */
709
-    public function children($ids, $depth)
710
-    {
709
+    public function children($ids, $depth)
710
+    {
711 711
         $_ids = $this->cleanIDs($ids, ',');
712
-        if (is_array($_ids) && $_ids != array()) {
712
+        if (is_array($_ids) && $_ids != array()) {
713 713
             $id = $this->sanitarIn($_ids);
714
-            if (!empty($id)) {
714
+            if (!empty($id)) {
715 715
                 $q = $this->query("SELECT `id` FROM {$this->makeTable('site_content')} where `parent` IN ({$id})");
716 716
                 $id = $this->modx->db->getColumn('id', $q);
717
-                if ($depth > 0 || $depth === true) {
717
+                if ($depth > 0 || $depth === true) {
718 718
                     $id = $this->children($id, is_bool($depth) ? $depth : ($depth - 1));
719 719
                 }
720 720
                 $_ids = array_merge($_ids, $id);
@@ -730,8 +730,8 @@  discard block
 block discarded – undo
730 730
      * @return $this
731 731
      * @throws Exception
732 732
      */
733
-    public function delete($ids, $fire_events = false)
734
-    {
733
+    public function delete($ids, $fire_events = false)
734
+    {
735 735
         $ids = $this->children($ids, true);
736 736
         $_ids = $this->cleanIDs($ids, ',', $this->systemID());
737 737
         $this->invokeEvent('OnBeforeDocFormDelete', array(
@@ -748,8 +748,8 @@  discard block
 block discarded – undo
748 748
     /**
749 749
      * @return array
750 750
      */
751
-    private function systemID()
752
-    {
751
+    private function systemID()
752
+    {
753 753
         $ignore = array(
754 754
             0, //empty document
755 755
             (int)$this->modxConfig('site_start'),
@@ -759,7 +759,7 @@  discard block
 block discarded – undo
759 759
         );
760 760
         $data = $this->query("SELECT DISTINCT setting_value FROM {$this->makeTable('web_user_settings')} WHERE `setting_name`='login_home' AND `setting_value`!=''");
761 761
         $data = $this->modx->db->makeArray($data);
762
-        foreach ($data as $item) {
762
+        foreach ($data as $item) {
763 763
             $ignore[] = (int)$item['setting_value'];
764 764
         }
765 765
 
@@ -771,22 +771,22 @@  discard block
 block discarded – undo
771 771
      * @param $alias
772 772
      * @return string
773 773
      */
774
-    protected function checkAlias($alias)
775
-    {
774
+    protected function checkAlias($alias)
775
+    {
776 776
         $alias = strtolower($alias);
777
-        if ($this->modxConfig('friendly_urls')) {
777
+        if ($this->modxConfig('friendly_urls')) {
778 778
             $_alias = $this->escape($alias);
779
-            if ((!$this->modxConfig('allow_duplicate_alias') && !$this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) {
779
+            if ((!$this->modxConfig('allow_duplicate_alias') && !$this->modxConfig('use_alias_path')) || ($this->modxConfig('allow_duplicate_alias') && $this->modxConfig('use_alias_path'))) {
780 780
                 $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' AND `parent`={$this->get('parent')} LIMIT 1"));
781
-            } else {
781
+            } else {
782 782
                 $flag = $this->modx->db->getValue($this->query("SELECT `id` FROM {$this->makeTable('site_content')} WHERE `alias`='{$_alias}' LIMIT 1"));
783 783
             }
784
-            if (($flag && $this->newDoc) || (!$this->newDoc && $flag && $this->id != $flag)) {
784
+            if (($flag && $this->newDoc) || (!$this->newDoc && $flag && $this->id != $flag)) {
785 785
                 $suffix = substr($alias, -2);
786
-                if (preg_match('/-(\d+)/', $suffix, $tmp) && isset($tmp[1]) && (int)$tmp[1] > 1) {
786
+                if (preg_match('/-(\d+)/', $suffix, $tmp) && isset($tmp[1]) && (int)$tmp[1] > 1) {
787 787
                     $suffix = (int)$tmp[1] + 1;
788 788
                     $alias = substr($alias, 0, -2) . '-' . $suffix;
789
-                } else {
789
+                } else {
790 790
                     $alias .= '-2';
791 791
                 }
792 792
                 $alias = $this->checkAlias($alias);
@@ -800,8 +800,8 @@  discard block
 block discarded – undo
800 800
      * @param $key
801 801
      * @return bool
802 802
      */
803
-    public function issetField($key)
804
-    {
803
+    public function issetField($key)
804
+    {
805 805
         return (array_key_exists($key, $this->default_field) || array_key_exists($key, $this->tv));
806 806
     }
807 807
 
@@ -809,15 +809,15 @@  discard block
 block discarded – undo
809 809
      * @param bool $reload
810 810
      * @return $this
811 811
      */
812
-    protected function get_TV($reload = false)
813
-    {
814
-        if (empty($this->modx->_TVnames) || $reload) {
812
+    protected function get_TV($reload = false)
813
+    {
814
+        if (empty($this->modx->_TVnames) || $reload) {
815 815
             $result = $this->query('SELECT `id`,`name` FROM ' . $this->makeTable('site_tmplvars'));
816
-            while ($row = $this->modx->db->GetRow($result)) {
816
+            while ($row = $this->modx->db->GetRow($result)) {
817 817
                 $this->modx->_TVnames[$row['name']] = $row['id'];
818 818
             }
819 819
         }
820
-        foreach ($this->modx->_TVnames as $name => $id) {
820
+        foreach ($this->modx->_TVnames as $name => $id) {
821 821
             $this->tvid[$id] = $name;
822 822
             $this->tv[$name] = $id;
823 823
         }
@@ -829,12 +829,12 @@  discard block
 block discarded – undo
829 829
     /**
830 830
      * @return $this
831 831
      */
832
-    protected function loadTVTemplate()
833
-    {
832
+    protected function loadTVTemplate()
833
+    {
834 834
         $q = $this->query("SELECT `tmplvarid`, `templateid` FROM " . $this->makeTable('site_tmplvar_templates'));
835 835
         $q = $this->modx->db->makeArray($q);
836 836
         $this->tvTpl = array();
837
-        foreach ($q as $item) {
837
+        foreach ($q as $item) {
838 838
             $this->tvTpl[$item['templateid']][] = $item['tmplvarid'];
839 839
         }
840 840
 
@@ -845,16 +845,16 @@  discard block
 block discarded – undo
845 845
      * @param array $tvId
846 846
      * @return $this
847 847
      */
848
-    protected function loadTVDefault(array $tvId = array())
849
-    {
850
-        if (is_array($tvId) && !empty($tvId)) {
848
+    protected function loadTVDefault(array $tvId = array())
849
+    {
850
+        if (is_array($tvId) && !empty($tvId)) {
851 851
             $tbl_site_tmplvars = $this->makeTable('site_tmplvars');
852 852
             $fields = 'id,name,default_text as value,display,display_params,type';
853 853
             $implodeTvId = implode(',', $tvId);
854 854
             $rs = $this->query("SELECT {$fields} FROM {$tbl_site_tmplvars} WHERE id IN({$implodeTvId})");
855 855
             $rows = $this->modx->db->makeArray($rs);
856 856
             $this->tvd = array();
857
-            foreach ($rows as $item) {
857
+            foreach ($rows as $item) {
858 858
                 $this->tvd[$item['name']] = $item;
859 859
             }
860 860
         }
@@ -867,17 +867,17 @@  discard block
 block discarded – undo
867 867
      * @return int
868 868
      * @throws Exception
869 869
      */
870
-    public function setTemplate($tpl)
871
-    {
872
-        if (!is_numeric($tpl) || $tpl != (int)$tpl) {
873
-            if (is_scalar($tpl)) {
870
+    public function setTemplate($tpl)
871
+    {
872
+        if (!is_numeric($tpl) || $tpl != (int)$tpl) {
873
+            if (is_scalar($tpl)) {
874 874
                 $sql = "SELECT `id` FROM {$this->makeTable('site_templates')} WHERE `templatename` = '" . $this->escape($tpl) . "'";
875 875
                 $rs = $this->query($sql);
876
-                if (!$rs || $this->modx->db->getRecordCount($rs) <= 0) {
876
+                if (!$rs || $this->modx->db->getRecordCount($rs) <= 0) {
877 877
                     throw new Exception("Template {$tpl} is not exists");
878 878
                 }
879 879
                 $tpl = $this->modx->db->getValue($rs);
880
-            } else {
880
+            } else {
881 881
                 throw new Exception("Invalid template name: " . print_r($tpl, 1));
882 882
             }
883 883
         }
@@ -888,14 +888,14 @@  discard block
 block discarded – undo
888 888
     /**
889 889
      * @return string
890 890
      */
891
-    protected function getAlias()
892
-    {
893
-        if ($this->modxConfig('friendly_urls') && $this->modxConfig('automatic_alias') && $this->get('alias') == '') {
891
+    protected function getAlias()
892
+    {
893
+        if ($this->modxConfig('friendly_urls') && $this->modxConfig('automatic_alias') && $this->get('alias') == '') {
894 894
             $alias = strtr($this->get('pagetitle'), $this->table);
895
-        } else {
896
-            if ($this->get('alias') != '') {
895
+        } else {
896
+            if ($this->get('alias') != '') {
897 897
                 $alias = $this->get('alias');
898
-            } else {
898
+            } else {
899 899
                 $alias = '';
900 900
             }
901 901
         }
@@ -912,10 +912,10 @@  discard block
 block discarded – undo
912 912
      *
913 913
      * Пересчет menuindex по полю таблицы site_content
914 914
      */
915
-    public function updateMenuindex($parent, $criteria = 'id', $dir = 'asc')
916
-    {
915
+    public function updateMenuindex($parent, $criteria = 'id', $dir = 'asc')
916
+    {
917 917
         $dir = strtolower($dir) == 'desc' ? 'desc' : 'asc';
918
-        if (is_integer($parent) && $criteria !== '') {
918
+        if (is_integer($parent) && $criteria !== '') {
919 919
             $this->query("SET @index := 0");
920 920
             $this->query("UPDATE {$this->makeTable('site_content')} SET `menuindex` = (@index := @index + 1) WHERE `parent`={$parent} ORDER BY {$criteria} {$dir}");
921 921
         }
@@ -928,37 +928,37 @@  discard block
 block discarded – undo
928 928
      *
929 929
      * @return $this
930 930
      */
931
-    public function setDefaultTemplate()
932
-    {
931
+    public function setDefaultTemplate()
932
+    {
933 933
         $parent = $this->get('parent');
934 934
         $template = $this->modxConfig('default_template');
935
-        switch ($this->modxConfig('auto_template_logic')) {
935
+        switch ($this->modxConfig('auto_template_logic')) {
936 936
             case 'sibling':
937
-                if (!$parent) {
937
+                if (!$parent) {
938 938
                     $site_start = $this->modxConfig('site_start');
939 939
                     $where = "sc.isfolder=0 AND sc.id!={$site_start}";
940 940
                     $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', $where, 'menuindex', 'ASC', 1);
941
-                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
941
+                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
942 942
                         $template = $sibl[0]['template'];
943 943
                     }
944
-                } else {
944
+                } else {
945 945
                     $sibl = $this->modx->getDocumentChildren($parent, 1, 0, 'template', 'isfolder=0', 'menuindex',
946 946
                         'ASC', 1);
947
-                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
947
+                    if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
948 948
                         $template = $sibl[0]['template'];
949
-                    } else {
949
+                    } else {
950 950
                         $sibl = $this->modx->getDocumentChildren($parent, 0, 0, 'template', 'isfolder=0', 'menuindex',
951 951
                             'ASC', 1);
952
-                        if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
952
+                        if (isset($sibl[0]['template']) && $sibl[0]['template'] !== '') {
953 953
                             $template = $sibl[0]['template'];
954 954
                         }
955 955
                     }
956 956
                 }
957 957
                 break;
958 958
             case 'parent':
959
-                if ($parent) {
959
+                if ($parent) {
960 960
                     $_parent = $this->modx->getPageInfo($parent, 0, 'template');
961
-                    if (isset($_parent['template'])) {
961
+                    if (isset($_parent['template'])) {
962 962
                         $template = $_parent['template'];
963 963
                     }
964 964
                 }
Please login to merge, or discard this patch.