-
Notifications
You must be signed in to change notification settings - Fork 4
/
item.php
904 lines (701 loc) · 25.7 KB
/
item.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
<?php
/**
* @package com_zoo
* @author YOOtheme http://www.yootheme.com
* @copyright Copyright (C) YOOtheme GmbH
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
/*
Class: ItemTable
The table class for items.
*/
class ItemTable extends AppTable {
public function __construct($app) {
parent::__construct($app, ZOO_TABLE_ITEM);
}
protected function _initObject($object) {
parent::_initObject($object);
// workaround for php bug, which calls constructor before filling values
if (is_string($object->params) || is_null($object->params)) {
// decorate data as object
$object->params = $this->app->parameter->create($object->params);
}
if (is_string($object->elements) || is_null($object->elements)) {
// decorate data as object
$object->elements = $this->app->data->create($object->elements);
}
// add to cache
$key_name = $this->key;
if ($object->$key_name && !key_exists($object->$key_name, $this->_objects)) {
$this->_objects[$object->$key_name] = $object;
}
// trigger init event
$this->app->event->dispatcher->notify($this->app->event->create($object, 'item:init'));
return $object;
}
/*
Function: save
Override. Save object to database table.
Returns:
Boolean.
*/
public function save($object) {
if (!($application = $object->getApplication())) {
throw new ItemTableException('Invalid application id');
}
if (!is_string($object->type) || empty($object->type)) {
throw new ItemTableException('Invalid type id');
}
if ($object->name == '') {
throw new ItemTableException('Invalid name');
}
if ($object->alias == '' || $object->alias != $this->app->string->sluggify($object->alias)) {
throw new ItemTableException('Invalid slug');
}
if ($this->app->alias->item->checkAliasExists($object->alias, $object->id)) {
throw new ItemTableException('Alias already exists, please choose a unique alias');
}
$new = !(bool) $object->id;
// trigger save event
$this->app->event->dispatcher->notify($this->app->event->create($object, 'item:save', compact('new')));
$result = parent::save($object);
// init vars
$db = $this->database;
$search_data = array();
foreach ($object->getElements() as $id => $element) {
// get search data
if ($data = $element->getSearchData()) {
$search_data[] = "(".$object->id.", ".$db->quote($id).", ".$db->quote($data).")";
}
}
// delete old search data
$query = "DELETE FROM ".ZOO_TABLE_SEARCH
." WHERE item_id = ".(int) $object->id;
$db->query($query);
// insert new search data
if (count($search_data)) {
$query = "INSERT INTO ".ZOO_TABLE_SEARCH
." VALUES ".implode(", ", $search_data);
$db->query($query);
}
// save tags
$this->app->table->tag->save($object);
// trigger saved event
$this->app->event->dispatcher->notify($this->app->event->create($object, 'item:saved', compact('new')));
return $result;
}
/*
Function: delete
Override. Delete object from database table.
Returns:
Boolean.
*/
public function delete($object) {
// get database
$db = $this->database;
// delete item to category relations
$query = "DELETE FROM ".ZOO_TABLE_CATEGORY_ITEM
." WHERE item_id = ".(int) $object->id;
$db->query($query);
// delete related comments
$query = "DELETE FROM ".ZOO_TABLE_COMMENT
." WHERE item_id = ".(int) $object->id;
$db->query($query);
// delete related search data rows
$query = "DELETE FROM ".ZOO_TABLE_SEARCH
." WHERE item_id = ". (int) $object->id;
$db->query($query);
// delete related rating data rows
$query = "DELETE FROM ".ZOO_TABLE_RATING
." WHERE item_id = ". (int) $object->id;
$db->query($query);
// delete related tag data rows
$query = "DELETE FROM ".ZOO_TABLE_TAG
." WHERE item_id = ". (int) $object->id;
$db->query($query);
$result = parent::delete($object);
// trigger deleted event
$this->app->event->dispatcher->notify($this->app->event->create($object, 'item:deleted'));
return $result;
}
/*
Function: hit
Increment item hits.
Returns:
Boolean.
*/
public function hit($object) {
// zoo_hack_start
return true;
// zoo_hack_end
}
/*
Function: getApplicationItemCount
Method to get application related item count.
Parameters:
$application_id - Application id
Returns:
Int
*/
public function getApplicationItemCount($application_id) {
$query = "SELECT count(a.id) AS item_count"
." FROM ".$this->name." AS a"
." WHERE a.application_id = ".(int) $application_id;
return (int) $this->_queryResult($query);
}
/*
Function: getTypeItemCount
Method to get types related item count.
Parameters:
$type - Type
Returns:
Int
*/
public function getTypeItemCount($type){
// get database
$db = $this->database;
$group = $type->getApplication()->getGroup();
$query = "SELECT count(a.id) AS item_count"
." FROM ".$this->name." AS a"
." JOIN ".ZOO_TABLE_APPLICATION." AS b ON a.application_id = b.id"
." WHERE a.type = ".$db->Quote($type->id)
." AND b.application_group = ".$db->Quote($group);
return (int) $this->_queryResult($query);
}
/*
@deprecated version 2.5 beta
*/
public function findAll($application_id = false, $published = false, $user = null, $options = array()) {
// get database
$db = $this->database;
// get date
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// set query options
$conditions =
($application_id !== false ? "application_id = ".(int) $application_id : "")
// zoo_hack_start
//." AND ".$this->app->user->getDBAccessString($user)
. ($published == true ? " AND state = 1" : "");
//." AND (publish_up = ".$null." OR publish_up <= ".$now.")"
//." AND (publish_down = ".$null." OR publish_down >= ".$now.")": "")
// zoo_hack_end
return $this->all(array_merge(compact('conditions'), $options));
}
/*
Function: getByCharacter
Method to retrieve all items starting with a certain character.
Parameters:
$application_id - Application id
$char - Character(s)
$not_in - Use not in for matching multiple characters
Returns:
Array - Array of items
*/
public function getByIds($ids, $published = false, $user = null, $orderby = '', $ignore_order_priority = false){
$ids = (array) $ids;
if (empty($ids)) {
return array();
}
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// get item ordering
list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
$query = "SELECT a.*"
." FROM ".$this->name." AS a"
.($join ? $join : "")
." WHERE a.id IN (".implode(",", $ids).")"
// zoo_hack_start
// ." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
.($order ? " ORDER BY " . $order : "");
return $this->_queryObjectList($query);
}
/*
Function: getByCharacter
Method to retrieve all items starting with a certain character.
Parameters:
$application_id - Application id
$char - Character(s)
$not_in - Use not in for matching multiple characters
$published
$user
$orderby
$offset
$limit
Returns:
Array - Array of items
*/
public function getByCharacter($application_id, $char, $not_in = false, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0, $ignore_order_priority = false){
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// escape and quote character array
if (is_array($char)) {
foreach ($char as $key => $val) {
$char[$key] = "'".$db->escape($val)."'";
}
}
// get item ordering
list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
$query = "SELECT a.*"
." FROM ".ZOO_TABLE_CATEGORY_ITEM." AS ci"
." JOIN ".$this->name." AS a ON a.id = ci.item_id"
.($join ? $join : "")
." WHERE a.application_id = ".(int) $application_id
// zoo_hack_start
//." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." AND BINARY LOWER(LEFT(a.name, 1)) ".(is_array($char) ? ($not_in ? "NOT" : null)." IN (".implode(",", $char).")" : " = '".$db->escape($char)."'")
.($order ? " ORDER BY " . $order : "")
.($limit ? " LIMIT ".(int) $offset.",".(int) $limit : "");
return $this->_queryObjectList($query);
}
/*
Function: getByTag
Method to retrieve all items matching a certain tag.
Parameters:
$tag - Tag name
$application_id - Application id
Returns:
Array - Array of items
*/
public function getByTag($application_id, $tag, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0, $ignore_order_priority = false){
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// get item ordering
list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
$query = "SELECT a.*"
." FROM ".$this->name." AS a "
." LEFT JOIN ".ZOO_TABLE_TAG." AS b ON a.id = b.item_id"
.($join ? $join : "")
." WHERE a.application_id = ".(int) $application_id
." AND b.name = '".$db->escape($tag)."'"
// zoo_hack_start
//." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." GROUP BY a.id"
.($order ? " ORDER BY " . $order : "")
.($limit ? " LIMIT ".(int) $offset.",".(int) $limit : "");
return $this->_queryObjectList($query);
}
/*
Function: getByType
Method to get types related items.
Parameters:
$type_id - Type
Returns:
Array - Items
*/
public function getByType($type_id, $application_id = false, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0, $ignore_order_priority = false){
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// get item ordering
list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
$query = "SELECT a.*"
." FROM ".$this->name." AS a"
.($join ? $join : "")
." WHERE a.type = ".$db->Quote($type_id)
.($application_id !== false ? " AND a.application_id = ".(int) $application_id : "")
// zoo_hack_start
//." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." GROUP BY a.id"
.($order ? " ORDER BY " . $order : "")
.($limit ? " LIMIT ".(int) $offset.",".(int) $limit : "");
return $this->_queryObjectList($query);
}
/*
Function: getByCategory
Method to retrieve all items of a category.
Parameters:
$category_id - Category id(s)
Returns:
Array - Array of items
*/
public function getByCategory($application_id, $category_id, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0, $ignore_order_priority = false) {
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// get item ordering
list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
$query = "SELECT a.id"
." FROM ".$this->name." AS a"
." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
.($join ? $join : "")
." WHERE a.application_id = ".(int) $application_id
// zoo_hack_start
// ." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
." GROUP BY a.id"
.($order ? " ORDER BY " . $order : "")
.($limit ? " LIMIT ".(int) $offset.",".(int) $limit : "");
$list = $this->app->database->queryAssocList($query);
$itemOrig = array_reduce($list, function($acc, $item){
$acc[] = $item['id'];
return $acc;
}, []);
$list = JBModelItem::model()->getZooItemsByIds($itemOrig);
$list = $this->app->jbarray->sortByArray($list, $itemOrig);
return $list;
}
/**
* Gets adjacent items
*
* @return array first value is previous item, second value is next item
*/
public function getPrevNext($application_id, $category_id, $item_id, $published = false, $user = null, $orderby = "", $ignore_order_priority = false) {
// init vars
$prev = null;
$next = null;
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// get item ordering
list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
$query = "SELECT ROWNUM"
." FROM ("
."SELECT @rownum:= @rownum+1 ROWNUM, id"
." FROM"
." (SELECT @rownum:=0) r,"
." (SELECT id"
." FROM ".$this->name." AS a"
." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
.($join ? $join : "")
." WHERE a.application_id = ".(int) $application_id
." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
// zoo_hack_start
//." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." GROUP BY a.id"
.($order ? " ORDER BY " . $order : "")
.") t1"
.") t2"
." WHERE id = ".(int) $item_id;
if ($row = $this->_queryResult($query)) {
$query = "SELECT ROWNUM, id"
." FROM ("
."SELECT @rownum:= @rownum+1 ROWNUM, id"
." FROM"
." (SELECT @rownum:=0) r,"
." (SELECT id"
." FROM ".$this->name." AS a"
." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
.($join ? $join : "")
." WHERE a.application_id = ".(int) $application_id
." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
// zoo_hack_start
//." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." GROUP BY a.id"
.($order ? " ORDER BY " . $order : "")
.") t1"
.") t2"
." WHERE ROWNUM = ".((int) $row-1) ." OR ROWNUM = ".((int) $row+1);
if ($objects = $db->queryObjectList($query)) {
foreach ($objects as $object) {
if ($object->ROWNUM > $row) {
$next = $this->get($object->id);
} else {
$prev = $this->get($object->id);
}
}
}
}
return array($prev, $next);
}
public function getByUser($application_id, $user_id, $type = null, $search = null, $order = null, $offset = null, $limit = null, $published = false) {
$options = $this->_getByUserOptions($application_id, $user_id, $type, $search, $published);
// Order
$orders = array(
'date' => 'a.created ASC',
'rdate' => 'a.created DESC',
'alpha' => 'a.name ASC',
'ralpha' => 'a.name DESC',
'hits' => 'a.hits DESC',
'rhits' => 'a.hits ASC');
$options['order'] = ($order && isset($orders[$order])) ? $orders[$order] : $orders['rdate'];
$options = $limit ? array_merge($options, array('offset' => $offset, 'limit' => $limit)) : $options;
return $this->all($options);
}
public function getItemCountByUser($application_id, $user_id, $type = null, $search = null, $published = false) {
return $this->count(array_merge($this->_getByUserOptions($application_id, $user_id, $type, $search, $published), array('select' => 'a.id')));
}
protected function _getByUserOptions($application_id, $user_id, $type = null, $search = null, $published = false) {
// select
$select = 'a.*';
// get from
$from = $this->name.' AS a';
// get data from the table
$where = array();
// application filter
$where[] = 'a.application_id = ' . (int) $application_id;
// author filter
$where[] = 'a.created_by = ' . (int) $user_id;
// user rights
// zoo_hack_start
//$where[] = 'a.'.$this->app->user->getDBAccessString($this->app->user->get((int) $user_id));
// zoo_hack_end
// published
if ($published) {
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
// Add filters for publishing
$where[] = 'a.state = 1';
// zoo_hack_start
//$where[] = "(a.publish_up = ".$null." OR a.publish_up <= ".$now.")";
//$where[] = "(a.publish_down = ".$null." OR a.publish_down >= ".$now.")";
// zoo_hack_end
}
// Type
if ($type) {
if (is_array($type)) {
$where[] = 'a.type IN ("' . implode('", "', array_keys($type)) . '")';
} else {
$where[] = 'a.type = "' . (string) $type . '"';
}
}
// Search
if ($search) {
$from .= ' LEFT JOIN '.ZOO_TABLE_TAG.' AS t ON a.id = t.item_id';
$where[] = '(LOWER(a.name) LIKE '.$this->app->database->Quote('%'.$this->app->database->escape($search, true).'%', false)
. ' OR LOWER(t.name) LIKE '.$this->app->database->Quote('%'.$this->app->database->escape($search, true).'%', false).')';
}
// conditions
$conditions = array(implode(' AND ', $where));
return compact('select', 'from', 'conditions');
}
/*
@deprecated ZOO version 2.5 beta, use getByCategory instead
*/
public function getFromCategory($application_id, $category_id, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0) {
return $this->getByCategory($application_id, $category_id, $published, $user, $orderby, $offset, $limit);
}
/*
Function: getItemCountFromCategory
Method to retrieve items count of a category.
Parameters:
$category_id - Category id(s)
Returns:
Array - Array of items
*/
public function getItemCountFromCategory($application_id, $category_id, $published = false, $user = null){
// get database
$db = $this->database;
// get dates
$date = $this->app->date->create();
$now = $db->Quote($date->toSQL());
$null = $db->Quote($db->getNullDate());
$query = "SELECT a.id"
." FROM ".$this->name." AS a"
." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
." WHERE a.application_id = ".(int) $application_id
." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
// zoo_hack_start
//." AND a.".$this->app->user->getDBAccessString($user)
.($published == true ? " AND a.state = 1" : "")
//." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
//." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
// zoo_hack_end
." GROUP BY a.id";
$db->query($query);
return $db->getNumRows();
}
/*
Function: search
Method to retrieve all items matching search data.
Parameters:
$search_string - the search string
$app_id - specify an application id to limit the search scope
Returns:
Array - Array of items
*/
public function search($search_string, $app_id = 0) {
// get database
$db = $this->database;
$db_search = $db->Quote('%'.$db->escape( $search_string, true ).'%', false);
$query = "SELECT a.*"
." FROM ".$this->name." AS a"
." LEFT JOIN ".ZOO_TABLE_SEARCH." AS b ON a.id = b.item_id"
." WHERE (LOWER(b.value) LIKE LOWER(" . $db_search . ")"
." OR LOWER(a.name) LIKE LOWER(" . $db_search . "))"
. (empty($app_id) ? "" : " AND application_id = " . $app_id)
." AND a.searchable=1"
." GROUP BY a.id";
return $this->_queryObjectList($query);
}
/*
Function: searchElements
Method to retrieve all items matching search data.
Parameters:
$elements_array - key = element_name, value = search string
$app_id - specify an application id to limit the search scope
Returns:
Array - Array of items
*/
public function searchElements($elements_array, $app_id = 0) {
// get database
$db = $this->database;
$i = 0;
$join = array();
$where = array();
foreach ($elements_array as $name => $search_string) {
$as = "table" . $i;
$db_name = $db->Quote($db->escape( $name, true ), false);
$db_search = $db->Quote('%'.$db->escape( $search_string, true ).'%', false);
$join[] = " LEFT JOIN ".ZOO_TABLE_SEARCH." AS " . $as . " ON a.id = " . $as . ".item_id";
$where[] = $as.".element_id = ".$db_name." AND LOWER(".$as.".value) LIKE LOWER(".$db_search.")";
$i++;
}
$query = "SELECT a.*"
." FROM ".$this->name." AS a "
. implode(" ", $join)
." WHERE "
. implode(" AND ", $where)
. (empty($app_id) ? "" : " AND application_id = " . $app_id)
." AND a.searchable=1"
." GROUP BY a.id";
return $this->_queryObjectList($query);
}
/*
Function: getUsers
Method to get users of items
Parameters:
$app_id - Application id
Returns:
Array - Array of items
*/
public function getUsers($app_id) {
$query = 'SELECT DISTINCT u.id, u.name'
.' FROM '.$this->name.' AS i'
.' JOIN #__users AS u ON i.created_by = u.id'
. ((empty($app_id)) ? "" : " WHERE i.application_id = ".$app_id);
return $this->database->queryObjectList($query, 'id');
}
/*
@deprecated version 2.5.5, use parents has() function
*/
public function isInitialized($key) {
return $this->has($key);
}
protected function _getItemOrder($order, $ignore_order_priority = false) {
// if string, try to convert ordering
if (is_string($order)) {
$order = $this->app->itemorder->convert($order);
}
$result = array(null, null);
$order = (array) $order;
if (in_array('_ignore_priority', $order)) {
$ignore_order_priority = true;
unset($order['_ignore_priority']);
}
// trigger order event
$this->app->event->dispatcher->notify($this->app->event->create($order, 'item:changeorder'));
// remove empty and duplicate values
$order = array_unique(array_filter($order));
// if random return immediately
if (in_array('_random', $order)) {
$result[1] = 'RAND()';
return $result;
}
// get order dir
if (($index = array_search('_reversed', $order)) !== false) {
$reversed = 'DESC';
unset($order[$index]);
} else {
$reversed = 'ASC';
}
// get ordering type
$alphanumeric = false;
if (($index = array_search('_alphanumeric', $order)) !== false) {
$alphanumeric = true;
unset($order[$index]);
}
// set default ordering attribute
if (empty($order)) {
$order[] = '_itemname';
}
// if there is a none core element present, ordering will only take place for those elements
if (count($order) > 1) {
$order = array_filter($order, create_function('$a', 'return strpos($a, "_item") === false;'));
}
// order by core attribute
foreach ($order as $element) {
if (strpos($element, '_item') === 0) {
$var = str_replace('_item', '', $element);
if ($alphanumeric) {
$result[1] = $reversed == 'ASC' ? "a.$var+0<>0 DESC, a.$var+0, a.$var" : "a.$var+0<>0, a.$var+0 DESC, a.$var DESC";
} else {
$result[1] = $reversed == 'ASC' ? "a.$var" : "a.$var DESC";
}
}
}
// else order by elements
if (!isset($result[1])) {
$result[0] = " LEFT JOIN ".ZOO_TABLE_SEARCH." AS s ON a.id = s.item_id AND s.element_id IN ('".implode("', '", $order)."')";
if ($alphanumeric) {
$result[1] = $reversed == 'ASC' ? "ISNULL(s.value), s.value+0<>0 DESC, s.value+0, s.value" : "s.value+0<>0, s.value+0 DESC, s.value DESC";
} else {
$result[1] = $reversed == 'ASC' ? "s.value" : "s.value DESC";
}
}
// If there wasn't _ignore_priority in the order array, prefix priority
if (!$ignore_order_priority) {
$result[1] = $result[1] ? 'a.priority DESC, ' . $result[1] : 'a.priority DESC';
}
// trigger init event
$this->app->event->dispatcher->notify($this->app->event->create($order, 'item:orderquery', array('result' => &$result)));
return $result;
}
}
/*
Class: ItemTableException
*/
class ItemTableException extends AppException {}