This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
categories.php
1000 lines (886 loc) · 19.6 KB
/
categories.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Joomla.Legacy
* @subpackage Categories
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* JCategories Class.
*
* @package Joomla.Legacy
* @subpackage Categories
* @since 11.1
*/
class JCategories
{
/**
* Array to hold the object instances
*
* @var array
* @since 11.1
*/
public static $instances = array();
/**
* Array of category nodes
*
* @var mixed
* @since 11.1
*/
protected $_nodes;
/**
* Array of checked categories -- used to save values when _nodes are null
*
* @var array
* @since 11.1
*/
protected $_checkedCategories;
/**
* Name of the extension the categories belong to
*
* @var string
* @since 11.1
*/
protected $_extension = null;
/**
* Name of the linked content table to get category content count
*
* @var string
* @since 11.1
*/
protected $_table = null;
/**
* Name of the category field
*
* @var string
* @since 11.1
*/
protected $_field = null;
/**
* Name of the key field
*
* @var string
* @since 11.1
*/
protected $_key = null;
/**
* Name of the items state field
*
* @var string
* @since 11.1
*/
protected $_statefield = null;
/**
* Array of options
*
* @var array
* @since 11.1
*/
protected $_options = null;
/**
* Class constructor
*
* @param array $options Array of options
*
* @since 11.1
*/
public function __construct($options)
{
$this->_extension = $options['extension'];
$this->_table = $options['table'];
$this->_field = (isset($options['field']) && $options['field']) ? $options['field'] : 'catid';
$this->_key = (isset($options['key']) && $options['key']) ? $options['key'] : 'id';
$this->_statefield = (isset($options['statefield'])) ? $options['statefield'] : 'state';
$options['access'] = (isset($options['access'])) ? $options['access'] : 'true';
$options['published'] = (isset($options['published'])) ? $options['published'] : 1;
$this->_options = $options;
return true;
}
/**
* Returns a reference to a JCategories object
*
* @param string $extension Name of the categories extension
* @param array $options An array of options
*
* @return JCategories JCategories object
*
* @since 11.1
*/
public static function getInstance($extension, $options = array())
{
$hash = md5($extension . serialize($options));
if (isset(self::$instances[$hash]))
{
return self::$instances[$hash];
}
$parts = explode('.', $extension);
$component = 'com_' . strtolower($parts[0]);
$section = count($parts) > 1 ? $parts[1] : '';
$classname = ucfirst(substr($component, 4)) . ucfirst($section) . 'Categories';
if (!class_exists($classname))
{
$path = JPATH_SITE . '/components/' . $component . '/helpers/category.php';
if (is_file($path))
{
include_once $path;
}
else
{
return false;
}
}
self::$instances[$hash] = new $classname($options);
return self::$instances[$hash];
}
/**
* Loads a specific category and all its children in a JCategoryNode object
*
* @param mixed $id an optional id integer or equal to 'root'
* @param boolean $forceload True to force the _load method to execute
*
* @return mixed JCategoryNode object or null if $id is not valid
*
* @since 11.1
*/
public function get($id = 'root', $forceload = false)
{
if ($id !== 'root')
{
$id = (int) $id;
if ($id == 0)
{
$id = 'root';
}
}
// If this $id has not been processed yet, execute the _load method
if ((!isset($this->_nodes[$id]) && !isset($this->_checkedCategories[$id])) || $forceload)
{
$this->_load($id);
}
// If we already have a value in _nodes for this $id, then use it.
if (isset($this->_nodes[$id]))
{
return $this->_nodes[$id];
}
// If we processed this $id already and it was not valid, then return null.
elseif (isset($this->_checkedCategories[$id]))
{
return null;
}
return false;
}
/**
* Load method
*
* @param integer $id Id of category to load
*
* @return void
*
* @since 11.1
*/
protected function _load($id)
{
$db = JFactory::getDbo();
$app = JFactory::getApplication();
$user = JFactory::getUser();
$extension = $this->_extension;
// Record that has this $id has been checked
$this->_checkedCategories[$id] = true;
$query = $db->getQuery(true);
// Right join with c for category
$query->select('c.*');
$case_when = ' CASE WHEN ';
$case_when .= $query->charLength('c.alias') . '!=0';
$case_when .= ' THEN ';
$c_id = $query->castAsChar('c.id');
$case_when .= $query->concatenate(array($c_id, 'c.alias'), ':');
$case_when .= ' ELSE ';
$case_when .= $c_id . ' END as slug';
$query->select($case_when);
$query->from('#__categories as c');
$query->where('(c.extension=' . $db->Quote($extension) . ' OR c.extension=' . $db->Quote('system') . ')');
if ($this->_options['access'])
{
$query->where('c.access IN (' . implode(',', $user->getAuthorisedViewLevels()) . ')');
}
if ($this->_options['published'] == 1)
{
$query->where('c.published = 1');
}
$query->order('c.lft');
// Note: s for selected id
if ($id != 'root')
{
// Get the selected category
$query->leftJoin('#__categories AS s ON (s.lft <= c.lft AND s.rgt >= c.rgt) OR (s.lft > c.lft AND s.rgt < c.rgt)');
$query->where('s.id=' . (int) $id);
}
$subQuery = ' (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ' .
'ON cat.lft BETWEEN parent.lft AND parent.rgt WHERE parent.extension = ' . $db->quote($extension) .
' AND parent.published != 1 GROUP BY cat.id) ';
$query->leftJoin($subQuery . 'AS badcats ON badcats.id = c.id');
$query->where('badcats.id is null');
// Note: i for item
if (isset($this->_options['countItems']) && $this->_options['countItems'] == 1)
{
if ($this->_options['published'] == 1)
{
$query->leftJoin(
$db->quoteName($this->_table) . ' AS i ON i.' . $db->quoteName($this->_field) . ' = c.id AND i.' . $this->_statefield . ' = 1'
);
}
else
{
$query->leftJoin($db->quoteName($this->_table) . ' AS i ON i.' . $db->quoteName($this->_field) . ' = c.id');
}
$query->select('COUNT(i.' . $db->quoteName($this->_key) . ') AS numitems');
}
// Group by
$query->group('c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
c.path, c.published, c.rgt, c.title, c.modified_user_id');
// Filter by language
if ($app->isSite() && $app->getLanguageFilter())
{
$query->where(
'(' . ($id != 'root' ? 'c.id=s.id OR ' : '') . 'c.language in (' . $db->Quote(JFactory::getLanguage()->getTag()) . ',' .
$db->Quote('*') . '))'
);
}
// Get the results
$db->setQuery($query);
$results = $db->loadObjectList('id');
$childrenLoaded = false;
if (count($results))
{
// Foreach categories
foreach ($results as $result)
{
// Deal with root category
if ($result->id == 1)
{
$result->id = 'root';
}
// Deal with parent_id
if ($result->parent_id == 1)
{
$result->parent_id = 'root';
}
// Create the node
if (!isset($this->_nodes[$result->id]))
{
// Create the JCategoryNode and add to _nodes
$this->_nodes[$result->id] = new JCategoryNode($result, $this);
// If this is not root and if the current node's parent is in the list or the current node parent is 0
if ($result->id != 'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id == 1))
{
// Compute relationship between node and its parent - set the parent in the _nodes field
$this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
}
// If the node's parent id is not in the _nodes list and the node is not root (doesn't have parent_id == 0),
// then remove the node from the list
if (!(isset($this->_nodes[$result->parent_id]) || $result->parent_id == 0))
{
unset($this->_nodes[$result->id]);
continue;
}
if ($result->id == $id || $childrenLoaded)
{
$this->_nodes[$result->id]->setAllLoaded();
$childrenLoaded = true;
}
}
elseif ($result->id == $id || $childrenLoaded)
{
// Create the JCategoryNode
$this->_nodes[$result->id] = new JCategoryNode($result, $this);
if ($result->id != 'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id))
{
// Compute relationship between node and its parent
$this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
}
if (!isset($this->_nodes[$result->parent_id]))
{
unset($this->_nodes[$result->id]);
continue;
}
if ($result->id == $id || $childrenLoaded)
{
$this->_nodes[$result->id]->setAllLoaded();
$childrenLoaded = true;
}
}
}
}
else
{
$this->_nodes[$id] = null;
}
}
}
/**
* Helper class to load Categorytree
*
* @package Joomla.Legacy
* @subpackage Categories
* @since 11.1
*/
class JCategoryNode extends JObject
{
/**
* Primary key
*
* @var integer
* @since 11.1
*/
public $id = null;
/**
* The id of the category in the asset table
*
* @var integer
* @since 11.1
*/
public $asset_id = null;
/**
* The id of the parent of category in the asset table, 0 for category root
*
* @var integer
* @since 11.1
*/
public $parent_id = null;
/**
* The lft value for this category in the category tree
*
* @var integer
* @since 11.1
*/
public $lft = null;
/**
* The rgt value for this category in the category tree
*
* @var integer
* @since 11.1
*/
public $rgt = null;
/**
* The depth of this category's position in the category tree
*
* @var integer
* @since 11.1
*/
public $level = null;
/**
* The extension this category is associated with
*
* @var integer
* @since 11.1
*/
public $extension = null;
/**
* The menu title for the category (a short name)
*
* @var string
* @since 11.1
*/
public $title = null;
/**
* The the alias for the category
*
* @var string
* @since 11.1
*/
public $alias = null;
/**
* Description of the category.
*
* @var string
* @since 11.1
*/
public $description = null;
/**
* The publication status of the category
*
* @var boolean
* @since 11.1
*/
public $published = null;
/**
* Whether the category is or is not checked out
*
* @var boolean
* @since 11.1
*/
public $checked_out = 0;
/**
* The time at which the category was checked out
*
* @var string
* @since 11.1
*/
public $checked_out_time = 0;
/**
* Access level for the category
*
* @var integer
* @since 11.1
*/
public $access = null;
/**
* JSON string of parameters
*
* @var string
* @since 11.1
*/
public $params = null;
/**
* Metadata description
*
* @var string
* @since 11.1
*/
public $metadesc = null;
/**
* Key words for meta data
*
* @var string
* @since 11.1
*/
public $metakey = null;
/**
* JSON string of other meta data
*
* @var string
* @since 11.1
*/
public $metadata = null;
/**
* The ID of the user who created the category
*
* @var integer
* @since 11.1
*/
public $created_user_id = null;
/**
* The time at which the category was created
*
* @var string
* @since 11.1
*/
public $created_time = null;
/**
* The ID of the user who last modified the category
*
* @var integer
* @since 11.1
*/
public $modified_user_id = null;
/**
* The time at which the category was modified
*
* @var string
* @since 11.1
*/
public $modified_time = null;
/**
* Nmber of times the category has been viewed
*
* @var integer
* @since 11.1
*/
public $hits = null;
/**
* The language for the category in xx-XX format
*
* @var string
* @since 11.1
*/
public $language = null;
/**
* Number of items in this category or descendants of this category
*
* @var integer
* @since 11.1
*/
public $numitems = null;
/**
* Number of children items
*
* @var integer
* @since 11.1
*/
public $childrennumitems = null;
/**
* Slug fo the category (used in URL)
*
* @var string
* @since 11.1
*/
public $slug = null;
/**
* Array of assets
*
* @var array
* @since 11.1
*/
public $assets = null;
/**
* Parent Category object
*
* @var object
* @since 11.1
*/
protected $_parent = null;
/**
* @var Array of Children
* @since 11.1
*/
protected $_children = array();
/**
* Path from root to this category
*
* @var array
* @since 11.1
*/
protected $_path = array();
/**
* Category left of this one
*
* @var integer
* @since 11.1
*/
protected $_leftSibling = null;
/**
* Category right of this one
*
* @var
* @since 11.1
*/
protected $_rightSibling = null;
/**
* true if all children have been loaded
*
* @var boolean
* @since 11.1
*/
protected $_allChildrenloaded = false;
/**
* Constructor of this tree
*
* @var
* @since 11.1
*/
protected $_constructor = null;
/**
* Class constructor
*
* @param array $category The category data.
* @param JCategoryNode $constructor The tree constructor.
*
* @since 11.1
*/
public function __construct($category = null, $constructor = null)
{
if ($category)
{
$this->setProperties($category);
if ($constructor)
{
$this->_constructor = $constructor;
}
return true;
}
return false;
}
/**
* Set the parent of this category
*
* If the category already has a parent, the link is unset
*
* @param mixed $parent JCategoryNode for the parent to be set or null
*
* @return void
*
* @since 11.1
*/
public function setParent($parent)
{
if ($parent instanceof JCategoryNode || is_null($parent))
{
if (!is_null($this->_parent))
{
$key = array_search($this, $this->_parent->_children);
unset($this->_parent->_children[$key]);
}
if (!is_null($parent))
{
$parent->_children[] = & $this;
}
$this->_parent = $parent;
if ($this->id != 'root')
{
if ($this->parent_id != 1)
{
$this->_path = $parent->getPath();
}
$this->_path[] = $this->id . ':' . $this->alias;
}
if (count($parent->_children) > 1)
{
end($parent->_children);
$this->_leftSibling = prev($parent->_children);
$this->_leftSibling->_rightsibling = &$this;
}
}
}
/**
* Add child to this node
*
* If the child already has a parent, the link is unset
*
* @param JCategoryNode $child The child to be added.
*
* @return void
*
* @since 11.1
*/
public function addChild($child)
{
if ($child instanceof JCategoryNode)
{
$child->setParent($this);
}
}
/**
* Remove a specific child
*
* @param integer $id ID of a category
*
* @return void
*
* @since 11.1
*/
public function removeChild($id)
{
$key = array_search($this, $this->_parent->_children);
unset($this->_parent->_children[$key]);
}
/**
* Get the children of this node
*
* @param boolean $recursive False by default
*
* @return array The children
*
* @since 11.1
*/
public function &getChildren($recursive = false)
{
if (!$this->_allChildrenloaded)
{
$temp = $this->_constructor->get($this->id, true);
if ($temp)
{
$this->_children = $temp->getChildren();
$this->_leftSibling = $temp->getSibling(false);
$this->_rightSibling = $temp->getSibling(true);
$this->setAllLoaded();
}
}
if ($recursive)
{
$items = array();
foreach ($this->_children as $child)
{
$items[] = $child;
$items = array_merge($items, $child->getChildren(true));
}
return $items;
}
return $this->_children;
}
/**
* Get the parent of this node
*
* @return mixed JCategoryNode or null
*
* @since 11.1
*/
public function getParent()
{
return $this->_parent;
}
/**
* Test if this node has children
*
* @return boolean True if there is a child
*
* @since 11.1
*/
public function hasChildren()
{
return count($this->_children);
}
/**
* Test if this node has a parent
*
* @return boolean True if there is a parent
*
* @since 11.1
*/
public function hasParent()
{
return $this->getParent() != null;
}
/**
* Function to set the left or right sibling of a category
*
* @param JCategoryNode $sibling JCategoryNode object for the sibling
* @param boolean $right If set to false, the sibling is the left one
*
* @return void
*
* @since 11.1
*/
public function setSibling($sibling, $right = true)
{
if ($right)
{
$this->_rightSibling = $sibling;
}
else
{
$this->_leftSibling = $sibling;
}
}
/**
* Returns the right or left sibling of a category
*
* @param boolean $right If set to false, returns the left sibling
*
* @return mixed JCategoryNode object with the sibling information or
* NULL if there is no sibling on that side.
*
* @since 11.1
*/
public function getSibling($right = true)
{
if (!$this->_allChildrenloaded)
{
$temp = $this->_constructor->get($this->id, true);
$this->_children = $temp->getChildren();
$this->_leftSibling = $temp->getSibling(false);
$this->_rightSibling = $temp->getSibling(true);
$this->setAllLoaded();
}
if ($right)
{
return $this->_rightSibling;
}
else
{
return $this->_leftSibling;
}
}
/**
* Returns the category parameters
*
* @return JRegistry
*
* @since 11.1
*/
public function getParams()
{
if (!($this->params instanceof JRegistry))
{
$temp = new JRegistry;
$temp->loadString($this->params);
$this->params = $temp;
}
return $this->params;
}
/**
* Returns the category metadata
*
* @return JRegistry A JRegistry object containing the metadata
*
* @since 11.1
*/
public function getMetadata()
{
if (!($this->metadata instanceof JRegistry))
{
$temp = new JRegistry;
$temp->loadString($this->metadata);
$this->metadata = $temp;
}
return $this->metadata;
}
/**
* Returns the category path to the root category
*
* @return array
*
* @since 11.1
*/
public function getPath()
{
return $this->_path;
}
/**
* Returns the user that created the category
*
* @param boolean $modified_user Returns the modified_user when set to true
*
* @return JUser A JUser object containing a userid
*
* @since 11.1
*/
public function getAuthor($modified_user = false)
{
if ($modified_user)
{
return JFactory::getUser($this->modified_user_id);
}
return JFactory::getUser($this->created_user_id);
}
/**
* Set to load all children
*
* @return void
*
* @since 11.1
*/
public function setAllLoaded()
{
$this->_allChildrenloaded = true;
foreach ($this->_children as $child)
{
$child->setAllLoaded();
}
}
/**
* Returns the number of items.
*
* @param boolean $recursive If false number of children, if true number of descendants
*
* @return integer Number of children or descendants
*
* @since 11.1
*/
public function getNumItems($recursive = false)
{
if ($recursive)
{
$count = $this->numitems;
foreach ($this->getChildren() as $child)
{
$count = $count + $child->getNumItems(true);
}
return $count;
}
return $this->numitems;
}
}