forked from sitemason/sm6-devlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMTool.php
executable file
·1384 lines (1075 loc) · 33.6 KB
/
SMTool.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
/*------------------------------------------------------------------------------------------
File: SMTool.php
Summary: Object for all things Sitemason® Tool (tool/page) related.
Version: 6.0
Converts Sitemason data into an SMTool object. This code also parses the items, sending
that data to SMItem to instantiate the SMItem objects, then assinging those Items to this
Tool.
Most of the Item data is parsed by the SM4/5 list view, but we also support cal_grid for
the month and week (translating that format into standard Items) - the strategy being,
eventually cal_grid and cal_list can be removed for straight-up list view.
Copyright (C) 2013 Sitemason, Inc. All Rights Reserved.
------------------------------------------------------------------------------------------*/
class SMTool extends SMObject {
private $customFields;
private $folder; // the folder containing this tool
private $foxyCartAPIKey;
private $foxyCartSubdomain;
private $includeInNavigation;
private $includeInSiteMap;
private $items;
private $layout;
private $metaDescription;
private $metaKeywords;
private $numberOfItems; // since, due to URL manipulation, we may only be seeing a few of the items, we need this number from Sitemason.
private $offset;
private $path;
private $redirectURL;
private $request;
private $slug;
private $set;
private $span;
private $tagGroups;
private $toolType;
private $URL;
private $view;
private $windowTitle;
// calendar-specific properties
private $nextPageURL;
private $previousPageURL;
private $displayedDate;
private $debug = 0;
/**
Create an SMTool object from SM5 JSON
$requestData = request data (array) - send to SMRequest to get JSON back from Sitemason
$responseData = existing data obtained elsewhere (simply to save an additional call to the app layer)
*/
function __construct($requestData = null, $responseData = null) {
global $smConsoleDebug;
if ($this->debug) { echo 'SMTool construct()..<br>'; }
$debugStart = microtime();
// if we got responseData, set up a SMRequest to store it
if ($responseData) {
$this->request = new SMRequest($requestData, $responseData);
}
// find our data if it doesn't already exist!
else {
if ($this->debug) { echo 'No responseData given. instantiating new SMRequest for requestData: <br>'; print_r($requestData); echo '<br />';}
$this->request = new SMRequest($requestData);
$responseCode = $this->request->getResponseCode();
$responseData = $this->request->getResponseData();
if ($this->debug) { echo 'SMTool constructor. $responseCode: '. $responseCode .'<br>'; }
}
//
// Create this SMTool from data (responseData array)
//
// ?sitejson or ?json
if ($responseData['content']['element']) {
$data = $responseData['content']['element'];
}
// ?tooljson
else if ($responseData['element']) {
$data = $responseData['element'];
}
// other (used for navigation in SMFolder)
else if ($responseData) {
$data = $responseData;
}
//
// ID
//
$this->id = (int)$data['id'];
// URL
$this->URL = $data['base_url'];
$this->layout = $data['settings']['layout'];
// Timestamps
$item = (array)$data['item'];
$itemData = $item[0];
$creationTime = new SMTime(array('startTime' => (string)$data['settings']['creation_timestamp'], 'timezone' => (string)$itemData['time_zone']));
$this->creationTime = $creationTime;
$lastModifiedTime = new SMTime(array('startTime' => (string)$data['settings']['modified_timestamp'], 'timezone' => (string)$itemData['time_zone']));
$this->lastModifiedTime = $lastModifiedTime;
//
// PATH
//
// called "path" in navjson
if ($data['path']) {
$this->path = (string)$data['path'];
}
// folder path + tool path
else {
if ($data['settings']['site']['path']) {
$path .= '/'. $data['settings']['site']['path'];
}
if ($data['settings']['path']) {
$path .= '/'. $data['settings']['path'];
}
$this->path = $path;
}
//
// SEO
//
if ($data['settings']['meta_description']) {
$this->metaDescription = $data['settings']['meta_description'];
}
if ($data['settings']['meta_keywords']) {
$this->metaKeywords = $data['settings']['meta_keywords'];
}
if ($data['settings']['page_title']) {
$this->windowTitle = $data['settings']['page_title'];
}
//
// Instance Custom Fields
//
// convert SM4/5 style custom fields into SM6 style (these are legacy settings and are not even used in SM6)
$customFields = array(
'cf1' => $data['settings']['instance_custom_field_1'],
'cf2' => $data['settings']['instance_custom_field_2'],
'cf3' => $data['settings']['instance_custom_field_3'],
'cf4' => $data['settings']['instance_custom_field_4'],
'cf5' => $data['settings']['instance_custom_field_5'],
'cf6' => $data['settings']['instance_custom_field_6'],
'cf7' => $data['settings']['instance_custom_field_7'],
'cf8' => $data['settings']['instance_custom_field_8']
);
// handle new-style custom_field_json
if ($data['settings']['custom_field_json']) {
$customFieldsJson = (string)$data['settings']['custom_field_json'];
$customFieldsJson = preg_replace("/\t\n/", '', $customFieldsJson);
$customFieldsJson = json_decode($customFieldsJson, true);
// Merge the two arrays, convert to (back into) JSON, then set customFields
$customFields = array_merge($customFields,$customFieldsJson);
}
$this->customFields = json_encode($customFields);
//
// TOOL TYPE
//
// used in navjson
if ($data['toolType']) {
$this->toolType = (string)$data['toolType'];
}
// in the SM4 data, this is called "type" in Page
// (and "app" in XML navigation block, but that doesn't matter since we're using navjson now)
else if ($data['type']) {
$this->toolType = (string)$data['type'];
}
// in the SM4 data, form case
else if ($responseData['current_nav']['app']) {
// convert form > customForm
if ((string)$responseData['current_nav']['app'] == 'form') {
$this->toolType = 'customForm';
}
// other (unknown) case
else {
$this->toolType = (string)$responseData['current_nav']['app'];
}
}
//
// Site map / Navigation
//
// used in navjson
if ($data['includeInNavigation']) {
$this->includeInNavigation = (bool)$data['includeInNavigation'];
}
// navjson
if ($data['includeInSiteMap']) {
$this->includeInSiteMap = (bool)$data['includeInSiteMap'];
}
// sitejson
else if ($data['settings']['include_in_sitemap']) {
$this->includeInSiteMap = (bool)$data['settings']['include_in_sitemap'];
}
//
// Slug
//
// navjson
if ($data['slug']) {
$this->slug = (string)$data['slug'];
}
// sitejson
else if ($data['settings']['slug']) {
$this->slug = (string)$data['settings']['slug'];
}
//
// Title
//
// if the tool is being instantiated normally, then the title = settings.name
if ($data['settings']['name']) {
$this->title = (string)$data['settings']['name'];
#$this->title = (string)$data['title']; // this is always set to the same as "window title" in this case, which is wrong.
}
// if we're creating a partial SMTool from the navigation data, then the name will be in the root of the data
else if ($data['title']) {
$this->title = (string)$data['title'];
}
// if all else fails, try to pull it from current_nav, which will only work under scenario 1
else {
$this->title = (string)$responseData['current_nav']['title'];
}
// Update title for site search, otherwise it's just the website URL
if ($this->getToolType() == 'site_search') {
$this->title = 'Search Results';
}
//
// element-specific properties
//
$this->span = (string)$data['span'];
$this->set = (int)$data['set'];
$this->offset = (int)$data['offset'];
$this->view = (string)$data['view'];
$this->numberOfItems = (int)$data['total_items'];
#$this->encodedId = (string)$data['id_enc'];
#$this->navEncodedId = (string)$data['navEncodedId']; // set by SMFolder when generating a navigation item.
#$this->secureURL = (string)$data['settings']['secure_url'];
//
// Tag Groups
//
$this->tagGroups = array();
if ($data['included_tag_groups']) {
$tagGroups = array();
foreach ($data['included_tag_groups']['tag_group'] as $tagGroup) {
$tagGroups[] = new SMTagGroup($tagGroup);
}
$this->tagGroups = $tagGroups;
}
// make Items now
$this->items = array();
// list view
if ($data['item']) {
$newItems = array();
foreach ($data['item'] as $item) {
$smItem = new SMItem($item, $this);
$smItem->setTool($this);
$newItems[] = $smItem;
}
$this->items = $newItems;
}
// month (cal_list & cal_grid)
else if ($data['month']['day']) {
$newItems = array();
foreach ($data['month']['day'] as $day) {
if ($day['item']) {
foreach ((array)$day['item'] as $item) {
$item['url'] = $this->getURL() . $item['url'];
$smItem = new SMItem($item);
$smItem->setTool($this);
$newItems[] = $smItem;
}
}
}
$this->items = $newItems;
}
// week (cal_list & cal_grid)
else if ($data['week']['day']) {
$newItems = array();
// day
foreach ((array)$data['week']['day'] as $day) {
// cal_grid: organized by hour
if ($day['hour']) {
foreach ((array)$day['hour'] as $hour) {
if ($hour['item']) {
foreach ((array)$hour['item'] as $item) {
// we need to modify item here, since the URL is a path relative to the tool
$item['url'] = $this->getURL() . $item['url'];
$smItem = new SMItem($item);
$smItem->setTool($this);
$newItems[] = $smItem;
}
}
}
}
// cal_list: organized by item
else {
foreach ((array)$day['item'] as $item) {
// we need to modify item here, since the URL is a path relative to the tool
$item['url'] = $this->getURL() . $item['url'];
$smItem = new SMItem($item);
$smItem->setTool($this);
$newItems[] = $smItem;
}
}
}
$this->items = $newItems;
}
// day (cal_list & cal_grid)
else if ($data['day']) {
$newItems = array();
// cal_grid: day:hour:item
if ($data['day']['hour']) {
foreach ((array)$data['day']['hour'] as $hour) {
if ($hour['item']) {
foreach ((array)$hour['item'] as $item) {
// we need to modify item here, since the URL is a path relative to the tool
$item['url'] = $this->getURL() . $item['url'];
$smItem = new SMItem($item);
$smItem->setTool($this);
$newItems[] = $smItem;
}
}
}
}
// cal_list: day:item
else {
foreach ((array)$data['day']['item'] as $item) {
// we need to modify item here, since the URL is a path relative to the tool
$item['url'] = $this->getURL() . $item['url'];
$smItem = new SMItem($item);
$smItem->setTool($this);
$newItems[] = $smItem;
}
}
$this->items = $newItems;
}
//
// Links (calendar-specific, for now, but may not be soon)
//
if ($data['links']) {
$nextPageURL = $this->getPath() . $data['links']['next'];
$previousPageURL = $this->getPath() . $data['links']['previous'];
// if there is a query string, remove it and replace it with this one.
if ($_SERVER['QUERY_STRING']) {
$nextPageURL .= '?'. $_SERVER['QUERY_STRING'];
$previousPageURL .= '?'. $_SERVER['QUERY_STRING'];
}
$this->nextPageURL = $nextPageURL;
$this->previousPageURL = $previousPageURL;
}
//
// Calendar-specific properties
//
if ($data['date_display']) {
$this->displayedDate = $data['date_display'];
}
//
// Store-specific settings
//
$this->foxyCartAPIKey = $data['settings']['store_api_key'];
$this->foxyCartSubdomain = $data['settings']['store_name'];
if (smShouldDebugApiRequests > 8) {
$smConsoleDebug .= ' console.info("instantiated new SMTool: '. $this->getTitle() .', URL: '. $this->getURL() .', Path: '. $this->getPath() .'");'."\n";
}
if ($this->debug) { echo 'Finished with SMTool constructor. Tool has '. count($this->items) .' items<br>'; }
$debugStop = microtime();
$debugDuration = $debugStop - $debugStart;
if (smShouldDebugTiming) {
$smConsoleDebug .= 'console.info("TIMING: instantiate SMTool: '. $this->getTitle() .': '. $debugDuration .'s");'."\n";
}
}
//!
//! Basic get/set methods
//!------------------------------
public function getFolder() {
return $this->folder;
}
public function setFolder(SMFolder $folder) {
$this->folder = $folder;
}
public function getFoxyCartAPIKey() {
return $this->foxyCartAPIKey;
}
public function getFoxyCartSubdomain() {
return $this->foxyCartSubdomain;
}
public function getLayout() {
return $this->layout;
}
/**
Returns the Items belonging to this SMTool. Prior to 6.0.10 on the detail view,
it would simply return the one Item. As of 6.0.10, if the visitor is on a detail
view, it does some trickery to find all Items belonging to the Tool.
$shouldForceCurrentFeed forces the method to use the current Sitemason feed. This
is called from getItem().
*/
public function getItems($shouldForceCurrentFeed = false) {
global $smCurrentSite;
$toolType = $this->getToolType();
// detail view: get ALL of the Items (instead of just one)
if ($this->getView() == 'detail' && $toolType != 'page' && !$shouldForceCurrentFeed) {
$thisTool = new SMTool(array('url' => $this->getURL()));
$items = $thisTool->getItems();
}
// any other view: just return what is here
else {
$items = $this->items;
}
return $items;
}
public function setItems($items) {
$this->items = $items;
}
public function getMetaDescription() {
return $this->metaDescription;
}
public function getMetaKeywords() {
return $this->metaKeywords;
}
public function getNextPageURL() {
return $this->nextPageURL;
}
public function getNumberOfItems() {
return $this->numberOfItems;
}
public function getOffset() {
return $this->offset;
}
public function getPath() {
return $this->path;
}
public function getPreviousPageURL() {
return $this->previousPageURL;
}
public function getRedirectURL() {
return $this->redirectURL;
}
public function setRedirectURL($redirectURL) {
$this->redirectURL = $redirectURL;
}
public function getRequest() {
return $this->request;
}
public function getSet() {
return $this->set;
}
public function getSlug() {
return $this->slug;
}
public function getSpan() {
return $this->span;
}
public function getTagGroups() {
return $this->tagGroups;
}
/**
does nothing - simply here in case someone calls it
while doing navigation functions
*/
public function getTools() {
return null;
}
public function getToolType() {
return $this->toolType;
}
public function getView() {
return $this->view;
}
/**
Thought about adding this to convert list to cal_grid, but cal_grid
needs other things from Sitemason in that view, so it doesn't really work
as intended.
*/
/*
public function setView($view) {
$this->view = $view;
}
*/
public function getWindowTitle() {
return $this->windowTitle;
}
//!
//! Calendar-specific methods
//!------------------------------
public function getDisplayedDate() {
return $this->displayedDate;
}
//!
//! PRIVATE get/set methods
//!------------------------------
/**
returns a JSON string of this SMItem's custom fields
*/
private function getCustomFields() {
return $this->customFields;
}
//!
//! Other Methods
//!------------------------------
/**
Creates a cumulative window title from the Tool's level and up
*/
public function getCumulativeWindowTitle($delimiter = '|') {
// if this tool is on the detail view and it's not a Page tool, return the Item's window title
if ($this->getView() == 'detail' && $this->getToolType() != 'page') {
$item = $this->getItem();
$itemTitle = $item->getTitle();
}
// Get the tool's window title or use the tool title if no window title was defined
$toolTitle = $this->getWindowTitle();
if (!$toolTitle) {
$toolTitle = $this->getTitle();
}
// Get the Folder's window title
$folder = $this->getFolder();
if ($folder) {
$folderTitle = $folder->getCumulativeWindowTitle();
}
$windowTitle = null;
$x = 0;
if ($itemTitle) {
$windowTitle .= $itemTitle;
$x++;
}
if ($toolTitle) {
if ($x > 0) { $windowTitle .= ' '. $delimiter .' '; }
$windowTitle .= $toolTitle;
$x++;
}
if ($folderTitle) {
// check for a duplicate...
#preg_replace('/'. $toolTitle .' '. $delimiter .' /','',$folderTitle);
if ($x > 0) { $windowTitle .= ' '. $delimiter .' '; }
$windowTitle .= $folderTitle;
$x++;
}
return $windowTitle;
}
/**
Access the tool's custom fields (instance custom fields)
*/
public function getCustomFieldWithKey($key) {
$customFields = json_decode($this->getCustomFields(), true);
// if $key = 1-8, add the "cf" prefix!
if ($key > 0 && $key < 8) {
$key = 'cf'. $key;
}
return $customFields[$key];
}
/**
Returns one Item. Prior to 6.0.10, it simply returned the first item
in the array. As of 6.0.10, it does the same if called from the list
view, but if called from the detail view, it returns the item being examined.
*/
public function getItem() {
// detail view: we just want what the current feed has (one Item)
if ($this->getView() == 'detail') {
// call getItems and force it to use the current feed
$items = $this->getItems(true);
}
// any other view: look up all of the Items and return the first one.
else {
$items = $this->getItems();
}
return $items[0];
}
public function getImportantItems() {
$items = $this->getItemsWithOptions(array('isImportant' => true));
return $items;
}
/**
returns the subset of items with an SMTime equal to the given date
Date format: YYYY-MM-DD
*/
public function getItemsWithDate($date) {
$filteredItems = array();
$items = $this->getItems();
foreach ($items as $item) {
if ($item->getStartDate() == $date) {
$filteredItems[] = $item;
}
}
return $filteredItems;
}
/**
returns a subset of the items, based on the given limit and offset
*/
public function getItemsWithLimitAndOffset($limit = 25, $offset = 0) {
return array_slice($this->getItems(), $offset, $limit);
}
/**
PRELIMINARY - wait until all options are fully tested before making "live"
Returns Items with the given options. This is a bit of a hack for the
time being, but will be simplified when Sitemason 7's API becomes available.
Valid options are:
- isImportant
*/
public function getItemsWithOptions(array $options) {
$items = $this->getItems();
// isImportant
if ($options['isImportant']) {
$newItems = array();
foreach ($items as $item) {
if ($item->isImportant()) {
$newItems[] = $item;
}
}
$items = $newItems;
}
return $items;
}
/**
Returns an array of SMItems that contain the given Tag key
*/
public function getItemsWithTagWithKey($tagKey) {
$items = $this->getItems();
$returnItems = array();
foreach ($items as $item) {
$tags = $item->getTags();
foreach ($tags as $tag) {
if ($tag->getKey() == $tagKey) {
$returnItems[] = $item;
continue;
}
}
}
return $returnItems;
}
/**
Returns an array of SMItems that contain the given Tag Title
*/
public function getItemsWithTagWithTitle($tagTitle) {
$items = $this->getItems();
$returnItems = array();
foreach ($items as $item) {
$tags = $item->getTags();
foreach ($tags as $tag) {
if ($tag->getTitle() == $tagTitle) {
$returnItems[] = $item;
continue;
}
}
}
return $returnItems;
}
/**
Returns all of the Items in this tool that have been tagged with a
Tag contained in the Tag Group with the given title (Tag Group's title).
*/
public function getItemsWithTagInTagGroupWithTitle($tagGroupTitle) {
$items = array();
$itemIds = array();
$tags = $this->getTagsInTagGroupWithTitle($tagGroupTitle);
foreach ($tags as $tag) {
$tagKey = $tag->getKey();
$newItems = $this->getItemsWithTagWithKey($tagKey);
// we need to make sure the items are unique
foreach ($newItems as $item) {
$itemId = $item->getID();
if (!in_array($itemId, $itemIds)) {
$itemIds[] = $itemId;
$items[] = $item;
}
}
}
return $items;
}
/**
Returns an empty array, since Tools cannot have navigation Tools
This is meant as a convenience when working with navigation structures
*/
public function getNavigationTools() {
$returnVal = array();
return $returnVal;
}
/**
Returns false, since Tools cannot have navigation Tools
This is meant as a convenience when working with navigation structures
*/
public function hasNavigationTools() {
return false;
}
/**
Returns a random item from within the items array
*/
public function getRandomItem() {
$items = $this->getItems();
$lastItem = count($items) - 1;
$random = rand(0, $lastItem);
return $items[$random];
}
/**
Returns the given number of random items from within the items array
*/
public function getRandomItems($number = 2) {
// ERROR: no items in the tool
$items = $this->getItems();
$returnItems = array(); // the SMItems to return
$shouldContinue = true;
// the requested number of items to return cannot be larger than the number of Items!
if ($number > count($items)) {
$number = count($items);
}
while ($shouldContinue) {
$lastItem = count($items) - 1;
$random = rand(0, $lastItem);
$item = $items[$random];
// add it to the array
$returnItems[] = $item;
// we have the requested number of items
if (count($returnItems) == $number) {
$shouldContinue = false;
}
// else remove this item from $items
else {
array_splice($items, $random, 1);
}
}
return $returnItems;
}
/**
Returns an array of all Tags (from all Items) in this Tool.
Since we don't have a concise list from the API, we're doing
this the hard way for now...
*/
public function getTags() {
$returnTags = array();
$tagIds = array();
$items = $this->getItems();
foreach ($items as $item) {
$tags = $item->getTags();
foreach ($tags as $tag) {
if (!in_array($tag->getID(), $tagIds)) {
$tagIds[] = $tag->getID();
$returnTags[] = $tag;
}
}
}
return $returnTags;
}
public function getTagGroupWithTitle($tagGroupTitle) {
$tagGroups = $this->getTagGroups();
$returnVal = null;
foreach ($tagGroups as $tagGroup) {
if ($tagGroup->getTitle() == $tagGroupTitle) {
$returnVal = $tagGroup;
break;
}
}
return $returnVal;
}
/**
Returns an array of SMTags contained in the Tag Group with the given name
*/
public function getTagsInTagGroupWithTitle($tagGroupTitle) {
$tags = array();
$tagGroups = $this->getTagGroups();
foreach ($tagGroups as $tagGroup) {
if ($tagGroup->getTitle() == $tagGroupTitle) {
$tags = $tagGroup->getTags();
break;
}
}
return $tags;
}
/**
Returns the titles of the subset of SMTags belonging to this SMTool where
those SMTags are members of the SMTagGroup with the given title
*/
public function getTitlesOfTagsInTagGroupWithTitle($tagGroupTitle) {
$tags = $this->getTagsInTagGroupWithTitle($tagGroupTitle);
$tagTitles = array();
foreach ($tags as $tag) {
$tagTitles[] = $tag->getTitle();
}
return $tagTitles;
}
/**
Specilized getView called by the Tool Template index script.
Needed in order to do some conversions for Calendar tool types...
*/
public function getToolView() {
$toolView = $this->getView();
// if the view is a cal_grid, change that to the span (month, week, day)
if ($toolView == 'cal_grid') {
$toolView = $this->getSpan();
}