1: <?php
2: /**
3: * ClipIt - JuxtaLearn Web Space
4: * PHP version: >= 5.2
5: * Creation date: 2013-10-10
6: * Last update: $Date$
7: * @author Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
8: * @version $Version$
9: * @link http://www.juxtalearn.eu
10: * @license GNU Affero General Public License v3
11: * @package ClipIt
12: * @subpackage urjc_backend
13: */
14:
15: /**
16: * <Class Description>
17: */
18: class UBItem {
19: /**
20: * @const string Elgg entity TYPE for this class
21: */
22: const TYPE = "object";
23: /**
24: * @const string Elgg entity SUBTYPE for this class
25: */
26: const SUBTYPE = "UBItem";
27: /**
28: * @const string Clone-Parent relationship name
29: */
30: const REL_PARENT_CLONE = "parent-clone";
31: /**
32: * @var int Unique Id of this instance
33: */
34: public $id = 0;
35: /**
36: * @var string Name of this instance
37: */
38: public $name = "";
39: /**
40: * @var string Description of this instance
41: */
42: public $description = "";
43: /**
44: * @var string URL of the instance
45: */
46: public $url = "";
47: /**
48: * @var int Unique Id of the owner/creator of this instance
49: */
50: public $owner_id = 0;
51: /**
52: * @var int Timestamp when this Item was created
53: */
54: public $time_created = 0;
55: /**
56: * @var int Origin object id, in case this object was cloned. If not = 0.
57: */
58: public $cloned_from = 0;
59: /**
60: * @var array Object clone ids.
61: */
62: public $clone_array = array();
63:
64: /**
65: * Constructor
66: *
67: * @param int $id If !null, load instance.
68: * @param ElggObject|ElggEntity $elgg_object Object to load instance from (optional)
69: *
70: * @throws APIException
71: */
72: function __construct($id = null, $elgg_object = null) {
73: if(!empty($id)) {
74: if(empty($elgg_object)){
75: if(!$elgg_object = new ElggObject($id)) {
76: throw new APIException("ERROR: Failed to load " . get_called_class() ." object with ID '" . $id . "'.");
77: }
78: }
79: $elgg_type = $elgg_object->type;
80: $elgg_subtype = $elgg_object->getSubtype();
81: if(($elgg_type != static::TYPE) || ($elgg_subtype != static::SUBTYPE)) {
82: throw new APIException(
83: "ERROR: ID '" . $id . "' does not correspond to a " . get_called_class() . " object."
84: );
85: }
86: $this->copy_from_elgg($elgg_object);
87: }
88: }
89:
90: /**
91: * Loads object parameters stored in Elgg
92: *
93: * @param ElggEntity $elgg_entity Elgg Object to load parameters from.
94: */
95: protected function copy_from_elgg($elgg_entity)
96: {
97: $this->id = (int)$elgg_entity->get("guid");
98: $this->name = (string)$elgg_entity->get("name");
99: $this->description = (string)$elgg_entity->get("description");
100: $this->url = (string)$elgg_entity->get("url");
101: $this->owner_id = (int)$elgg_entity->getOwnerGUID();
102: $this->time_created = (int)$elgg_entity->getTimeCreated();
103: $this->cloned_from = (int)static::get_cloned_from($this->id);
104: $this->clone_array = (array)static::get_clones($this->id);
105: }
106:
107: /**
108: * Get the parent Item ID for an Item.
109: *
110: * @param int $id Item from which to return parent
111: * @param bool $recursive Whether to look for parent recursively
112: *
113: * @return int Parent ID
114: */
115: static function get_cloned_from($id, $recursive = false)
116: {
117: $parent = UBCollection::get_items($id, static::REL_PARENT_CLONE, true);
118: if (empty($parent)) {
119: return 0;
120: }
121: $parent = (int)array_pop($parent);
122: if ($recursive) {
123: $new_parent = static::get_cloned_from($parent, true);
124: while(!empty($new_parent)){
125: $parent = $new_parent;
126: $new_parent = static::get_cloned_from($parent, true);
127: }
128: }
129: return $parent;
130: }
131:
132: /**
133: * Get all parent "master" items (which are not cloned)
134: *
135: * @param string $order_by forwarded to get_all (see get_all)
136: * @param bool $ascending forwarded to get_all (see get_all)
137: * @param bool|false $id_only Whether to return only IDs
138: * @return array[static]|array[int] Array of parent objects from class
139: */
140: static function get_all_parents($order_by = "", $ascending = true, $id_only = false){
141: $all_items = static::get_all(0, 0, $order_by, $ascending, $id_only);
142: $parent_array = array();
143: foreach($all_items as $item){
144: $cloned_from = static::get_cloned_from($id_only ? $item : $item->id);
145: if(empty($cloned_from)){
146: $parent_array[] = $item;
147: }
148: }
149: return $parent_array;
150: }
151:
152: /**
153: * Get an ID array of all cloned Items from a specified one.
154: *
155: * @param int $id Item from which to return clones
156: * @param bool $recursive Whether to look for clones recursively
157: *
158: * @return int[] Array of Item IDs
159: */
160: static function get_clones($id, $recursive = false)
161: {
162: $item_clones = array_reverse(UBCollection::get_items($id, static::REL_PARENT_CLONE));
163: if ($recursive) {
164: $clone_array = array();
165: foreach ($item_clones as $clone) {
166: $clone_array[] = $clone;
167: $clone_children = static::get_clones($clone, true);
168: if(!empty($clone_children)){
169: $clone_array[] = $clone_children;
170: }
171: }
172: return $clone_array;
173: }
174: return $item_clones;
175: }
176:
177: /* Static Functions */
178:
179: /**
180: * Create a new instance of this class, and assign values to its properties.
181: *
182: * @param array $prop_value_array Array of [property]=>value pairs to set into the new instance
183: *
184: * @return int|bool Returns instance Id if correct, or false if error
185: */
186: static function create($prop_value_array)
187: {
188: return static::set_properties(null, $prop_value_array);
189: }
190:
191: /**
192: * Sets values to specified properties of an Item
193: *
194: * @param int $id Id of Item to set property values
195: * @param array $prop_value_array Array of property=>value pairs to set into the Item
196: *
197: * @return int|bool Returns Id of Item if correct, or false if error
198: * @throws InvalidParameterException
199: */
200: static function set_properties($id, $prop_value_array)
201: {
202: if(!$item = new static($id)) {
203: return false;
204: }
205: $class_properties = (array)static::list_properties();
206: foreach ($prop_value_array as $prop => $value) {
207: if (!array_key_exists($prop, $class_properties)) {
208: throw new InvalidParameterException("ERROR: One or more property names do not exist.");
209: }
210: if ($prop == "id") {
211: continue; // cannot set an item's ID manually.
212: }
213: $item->$prop = $value;
214: }
215: if (array_key_exists("time_created", $prop_value_array)) {
216: return $item->save(true);
217: } else {
218: return $item->save(false);
219: }
220: }
221:
222: /**
223: * Lists the properties contained in this object
224: * @return array Array of properties with type and default value
225: */
226: static function list_properties()
227: {
228: return get_class_vars(get_called_class());
229: }
230:
231: /**
232: * Saves this instance to the system.
233: * @param bool $double_save if double_save is true, this object is saved twice to ensure that all properties are
234: * updated properly. E.g. the time_created property can only beset on ElggObjects during an update. Defaults to false!
235: * @return bool|int Returns id of saved instance, or false if error.
236: */
237: protected function save($double_save = false)
238: {
239: if (!empty($this->id)) {
240: if (!$elgg_object = new ElggObject($this->id)) {
241: return false;
242: }
243: } else {
244: $elgg_object = new ElggObject();
245: $elgg_object->type = static::TYPE;
246: $elgg_object->subtype = static::SUBTYPE;
247: }
248: $this->copy_to_elgg($elgg_object);
249: $elgg_object->save();
250: if ($double_save) {
251: // Only updates are saving time_created, thus first save for creation, second save for updating to
252: // proper creation time if given
253: $elgg_object->save();
254: }
255: return $this->id = $elgg_object->get("guid");
256: }
257:
258: /**
259: * Copy $this object parameters into an Elgg entity.
260: *
261: * @param ElggEntity $elgg_entity Elgg object instance to save $this to
262: */
263: protected function copy_to_elgg($elgg_entity)
264: {
265: $elgg_entity->set("name", (string)$this->name);
266: $elgg_entity->set("description", (string)$this->description);
267: $elgg_entity->set("url", (string)$this->url);
268: if (!empty($this->owner_id)) {
269: $elgg_entity->set("owner_guid", (int)$this->owner_id);
270: }
271: $elgg_entity->set("time_created", (int)$this->time_created);
272: $elgg_entity->set("access_id", ACCESS_PUBLIC);
273: }
274:
275: /**
276: * Clone the specified Item, including all of its properties.
277: *
278: * @param int $id Item id from which to create a clone.
279: * @param bool $linked Selects whether the clone will be linked to the parent object.
280: * @param bool $keep_owner Selects whether the clone will keep the parent item's owner (default: no)
281: *
282: * @return bool|int Id of the new clone Item, false in case of error.
283: */
284: static function create_clone($id, $linked = true, $keep_owner = false) {
285: $prop_value_array = static::get_properties($id);
286: if($keep_owner === false){
287: $prop_value_array["owner_id"] = elgg_get_logged_in_user_guid();
288: }
289: $clone_id = static::set_properties(null, $prop_value_array);
290: if($linked){
291: static::link_parent_clone($id, $clone_id);
292: }
293: return $clone_id;
294: }
295:
296: /**
297: * Get specified property values for an Item
298: *
299: * @param int $id Id of instance to get properties from
300: * @param array $prop_array Array of property names to get values from
301: *
302: * @return array|bool Returns an array of property=>value pairs, or false if error
303: * @throws InvalidParameterException
304: */
305: static function get_properties($id, $prop_array = null)
306: {
307: if (!$item = new static($id)) {
308: return null;
309: }
310: $prop_value_array = array();
311: if (!empty($prop_array)) {
312: foreach ($prop_array as $prop) {
313: if (array_key_exists($prop, static::list_properties())) {
314: $prop_value_array[$prop] = $item->$prop;
315: } else {
316: throw new InvalidParameterException("ERROR: One or more property names do not exist.");
317: }
318: }
319: } else {
320: $prop_array = static::list_properties();
321: do {
322: $prop = key($prop_array);
323: $prop_value_array[$prop] = $item->$prop;
324: next($prop_array);
325: } while (key($prop_array) !== null);
326: }
327: return $prop_value_array;
328: }
329:
330: /**
331: * Links two entities as parent and clone
332: *
333: * @param int $id_parent ID of parent entity
334: * @param int $id_clone IF of clone entity
335: * @return bool true if OK
336: */
337: static function link_parent_clone($id_parent, $id_clone)
338: {
339: return UBCollection::add_items($id_parent, array($id_clone), static::REL_PARENT_CLONE, true);
340: }
341:
342: /**
343: * Unlinks an entity from its parent
344: *
345: * @param int $id ID of entity
346: * @return bool returns true if OK
347: */
348: static function unlink_from_parent($id)
349: {
350: $parent = static::get_cloned_from($id);
351: return UBCollection::remove_items($parent, array($id), static::REL_PARENT_CLONE);
352: }
353:
354: /**
355: * Unlinks an entity from its clones
356: *
357: * @param int $id ID of entity
358: * @return bool returns true if OK]
359: */
360: static function unlink_from_clones($id)
361: {
362: $clones = static::get_clones($id);
363: return UBCollection::remove_items($id, $clones, static::REL_PARENT_CLONE);
364: }
365:
366: /**
367: * Get an array with the full clone list of the clone tree an item belongs to
368: * @param int|null $id ID of Item (if non set, return all trees)
369: *
370: * @return int[] Array of item IDs
371: * @throws InvalidParameterException
372: */
373: static function get_clone_tree($id = null){
374: if(empty($id)){
375: $all_items = array_reverse(static::get_all(0, 0, "", true, false));
376: $clone_tree = array();
377: foreach($all_items as $item_id){
378: if(array_search($item_id, array_flatten($clone_tree)) === false){
379: $clone_tree[] = static::get_clone_tree($item_id);
380: }
381: }
382: } else {
383: // Find top parent in clone tree
384: $top_parent = static::get_cloned_from($id, true);
385: if(empty($top_parent)){
386: $top_parent = $id;
387: }
388: $clone_tree = array();
389: $clone_tree[] = $top_parent;
390: $clones = static::get_clones($top_parent, true);
391: if(!empty($clones)){
392: $clone_tree[] = $clones;
393: }
394: }
395: return $clone_tree;
396: }
397:
398: /**
399: * Delete All Items for this class.
400: * @return bool Returns true if correct, or false if error
401: */
402: static function delete_all()
403: {
404: $items = static::get_all(0, 0, "", true, true);
405: if (!empty($items)) {
406: static::delete_by_id($items);
407: }
408: return true;
409: }
410:
411: static function count_all(){
412: return elgg_get_entities(array("limit" => 0, "count" => true));
413: }
414:
415: /**
416: * Get all Object instances of this TYPE and SUBTYPE from the system, optionally only a specified property.
417: *
418: * @param int $limit Number of results to show, default= 0 [no limit] (optional)
419: * @param int $offset Offset from where to show results, default=0 [from the begining] (optional)
420: * @param string $order_by Default = "" == time_created desc (newest first)
421: * @param bool $ascending Default = true (ascending order)
422: * @param bool $id_only Whether to only return IDs, or return whole objects (default: false)
423: * will be done if it is set to true.
424: *
425: * @return static[]|int[] Returns an array of Objects, or Object IDs if id_only = true
426: */
427: static function get_all($limit = 0, $offset = 0, $order_by = "", $ascending = true, $id_only = false)
428: {
429: $return_array = array();
430: if(!empty($order_by)){
431: $options = array(
432: 'type' => static::TYPE,
433: 'subtype' => static::SUBTYPE,
434: 'limit' => $limit,
435: 'offset' => $offset,
436: 'order_by_metadata' =>
437: array("name" => $order_by ,
438: "direction" => ($ascending ? "ASC":"DESC"))
439: );
440: }else{
441: $options = array(
442: 'type' => static::TYPE,
443: 'subtype' => static::SUBTYPE,
444: 'limit' => $limit,
445: 'offset' => $offset,
446: 'sort_by' => "e.time_created desc"
447: );
448: }
449: $elgg_entity_array = elgg_get_entities_from_metadata($options);
450: if (!$elgg_entity_array) {
451: return $return_array;
452: }
453: if ($id_only) {
454: foreach ($elgg_entity_array as $elgg_entity) {
455: $return_array[] = $elgg_entity->guid;
456: }
457: return $return_array;
458: }
459: foreach ($elgg_entity_array as $elgg_entity) {
460: $return_array[(int)$elgg_entity->guid] = new static((int)$elgg_entity->guid, $elgg_entity);
461: }
462: // if (!empty($order_by)) {
463: // $args = array("order_by" => $order_by, "ascending" => $ascending);
464: // uasort($return_array,
465: // function ($i1, $i2) use ($args) {
466: // if (!$i1 && !$i2) {
467: // return 0;
468: // }
469: // if ($i1->$args["order_by"] == $i2->$args["order_by"]) {
470: // return 0;
471: // }
472: // if ((bool)$args["ascending"]) {
473: // if (!$i1) {
474: // return 1;
475: // }
476: // if (!$i2) {
477: // return -1;
478: // }
479: // return (strtolower($i1->$args["order_by"]) < strtolower($i2->$args["order_by"]) ? -1 : 1);
480: // //return strcmp($i1->$args["order_by"], $i2->$args["order_by"]);
481: // } else {
482: // if (!$i1) {
483: // return -1;
484: // }
485: // if (!$i2) {
486: // return 1;
487: // }
488: // return (strtolower($i2->$args["order_by"]) < strtolower($i1->$args["order_by"]) ? -1 : 1);
489: // //return strcmp($i2->$args["order_by"], $i1->$args["order_by"]);
490: // }
491: // });
492: // }
493: return $return_array;
494: }
495:
496: /**
497: * Delete Items given their Id.
498: *
499: * @param array $id_array List of Item Ids to delete
500: *
501: * @return bool Returns true if correct, or false if error
502: */
503: static function delete_by_id($id_array)
504: {
505: if (empty($id_array)) {
506: return true;
507: }
508: foreach ($id_array as $id) {
509: // Check if ID is of same subtype as class
510: $lookup_array = ClipitSite::lookup($id);
511: if($lookup_array["subtype"] != static::SUBTYPE){
512: continue;
513: }
514: // Don't allow to delete the Site ID
515: $site = elgg_get_site_entity();
516: if ($id == $site->guid) {
517: continue;
518: }
519: if (delete_entity((int)$id) === false) {
520: return false;
521: }
522: }
523: return true;
524: }
525:
526: /**
527: * Get Objects with id contained in a given list.
528: *
529: * @param array $id_array Array of item IDs to get
530: * @param int $limit (optional) limit of items to get
531: * @param int $offset (optional) offset of items to get
532: * @param string $order_by (optional) order by a certain property
533: * @param bool $ascending (optional) order by ascending (default) or descending
534: * @return static[] array of UBItems filtered by the parameters given
535: */
536: static function get_by_id($id_array, $limit = 0, $offset = 0, $order_by = "", $ascending = true) {
537: $item_array = array();
538: if(empty($id_array)){
539: return $item_array;
540: }
541: foreach($id_array as $id) {
542: if(empty($id)){
543: continue;
544: }
545: $item_array[(int)$id] = new static((int)$id);
546: }
547: if(!empty($item_array) && !empty($order_by)){
548: $args = array("order_by" => $order_by, "ascending" => $ascending);
549: uasort($item_array, function($i1, $i2) use($args){
550: if (!$i1 && !$i2) {
551: return 0;
552: }
553: if($i1->$args["order_by"] == $i2->$args["order_by"]){
554: return 0;
555: }
556: if((bool)$args["ascending"]) {
557: if (!$i1) {
558: return 1;
559: }
560: if (!$i2) {
561: return -1;
562: }
563: return (strtolower($i1->$args["order_by"]) < strtolower($i2->$args["order_by"]) ? -1 : 1);
564: } else {
565: if (!$i1) {
566: return -1;
567: }
568: if (!$i2) {
569: return 1;
570: }
571: return (strtolower($i2->$args["order_by"]) < strtolower($i1->$args["order_by"]) ? -1 : 1);
572: }
573: });
574: }
575: if(empty($limit)){
576: $limit = null;
577: }
578: return array_slice($item_array, (int)$offset, $limit, true);
579: }
580:
581: /**
582: * Get Items with Owner Id contained in a given list.
583: *
584: * @param array $owner_array Array of Owner Ids
585: * @param int $limit (optional) Number of Items to return, default 0 = all
586: * @param int $offset (optional) offset of items to get
587: * @param string $order_by (optional) order by a certain property
588: * @param bool $ascending (optional) order by ascending (default) or descending
589: *
590: * @return static[] Returns an array of Items
591: */
592: static function get_by_owner($owner_array = null, $limit = 0, $offset = 0, $order_by = "", $ascending = true)
593: {
594: $object_array = array();
595: if (empty($owner_array)) {
596: $item_array = static::get_all();
597: $return_array = array();
598: foreach ($item_array as $item) {
599: if (!isset($return_array[$item->owner_id])) {
600: $return_array[$item->owner_id] = array();
601: }
602: $return_array[$item->owner_id][] = $item;
603: }
604: if (!empty($order_by)) {
605: foreach ($return_array as $owner_items) {
606: $args = array("order_by" => $order_by, "ascending" => $ascending);
607: uasort($owner_items,
608: function ($i1, $i2) use ($args) {
609: if (!$i1 && !$i2) {
610: return 0;
611: }
612: if ($i1->$args["order_by"] == $i2->$args["order_by"]) {
613: return 0;
614: }
615: if ((bool)$args["ascending"]) {
616: if (!$i1) {
617: return 1;
618: }
619: if (!$i2) {
620: return -1;
621: }
622: return (strtolower($i1->$args["order_by"]) < strtolower($i2->$args["order_by"]) ? -1 : 1);
623: //return strcmp($i1->$args["order_by"], $i2->$args["order_by"]);
624: } else {
625: if (!$i1) {
626: return -1;
627: }
628: if (!$i2) {
629: return 1;
630: }
631: return (strtolower($i2->$args["order_by"]) < strtolower($i1->$args["order_by"]) ? -1 : 1);
632: //return strcmp($i2->$args["order_by"], $i1->$args["order_by"]);
633: }
634: });
635: }
636: }
637: return $return_array;
638: }
639: // else if !empty($owner_array)
640: foreach($owner_array as $owner_id) {
641: $elgg_object_array = elgg_get_entities(
642: array(
643: "type" => static::TYPE,
644: "subtype" => static::SUBTYPE,
645: "owner_guid" => (int)$owner_id,
646: "limit" => $limit,
647: "offset" => $offset,
648: "sort_by" => "e.time_created"
649: )
650: );
651: if(!empty($elgg_object_array)) {
652: $temp_array = array();
653: foreach($elgg_object_array as $elgg_object) {
654: $temp_array[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
655: }
656: if(!empty($temp_array)) {
657: if (!empty($order_by)) {
658: $args = array("order_by" => $order_by, "ascending" => $ascending);
659: uasort($temp_array,
660: function ($i1, $i2) use ($args) {
661: if (!$i1 && !$i2) {
662: return 0;
663: }
664: if ($i1->$args["order_by"] == $i2->$args["order_by"]) {
665: return 0;
666: }
667: if ((bool)$args["ascending"]) {
668: if (!$i1) {
669: return 1;
670: }
671: if (!$i2) {
672: return -1;
673: }
674: return (strtolower($i1->$args["order_by"]) < strtolower($i2->$args["order_by"]) ? -1 : 1);
675: //return strcmp($i1->$args["order_by"], $i2->$args["order_by"]);
676: } else {
677: if (!$i1) {
678: return -1;
679: }
680: if (!$i2) {
681: return 1;
682: }
683: return (strtolower($i2->$args["order_by"]) < strtolower($i1->$args["order_by"]) ? -1 : 1);
684: //return strcmp($i2->$args["order_by"], $i1->$args["order_by"]);
685: }
686: });
687: }
688: $object_array[(int)$owner_id] = $temp_array;
689: } else {
690: $object_array[(int)$owner_id] = null;
691: }
692: } else{
693: $object_array[(int)$owner_id] = null;
694: }
695: }
696: return $object_array;
697: }
698:
699: /**
700: * Get all system events filtered by the class TYPE and SUBTYPE.
701: *
702: * @param int $offset Skip the first $offset events
703: * @param int $limit Return at most $limit events
704: *
705: * @return array Array of system events
706: */
707: static function get_events($offset = 0, $limit = 10) {
708: return get_system_log(
709: null, // $by_user = ""
710: null, // $event = ""
711: null, // $class = ""
712: static::TYPE, // $type = ""
713: static::SUBTYPE, // $subtype = ""
714: $limit, // $limit = 10
715: $offset, // $offset = 0
716: null, // $count = false
717: null, // $timebefore = 0
718: null, // $timeafter = 0
719: null, // $object_id = 0
720: null
721: ); // $ip_address = ""
722: }
723:
724: /**
725: * Get all objects which match a $search_string
726: *
727: * @param string $search_string String for searching matching objects
728: * @param bool $name_only Whether to look only in the name property, default false.
729: * @param bool $strict Whether to match the $search_string exactly, including case, or only partially.
730: * @param int $offset The offset of the returned array
731: * @param int $limit The limit of the returned array
732: *
733: * @return static[] Returns an array of matched objects
734: */
735: static function get_from_search($search_string, $name_only = false, $strict = false, $limit = 0, $offset = 0) {
736: $search_result = array();
737: if(!$strict) {
738: $search_string = strtolower($search_string);
739: // get the full array of entities
740: $elgg_object_array = elgg_get_entities(
741: array('type' => static::TYPE, 'subtype' => static::SUBTYPE, 'limit' => 0)
742: );
743: $search_result = array();
744: foreach($elgg_object_array as $elgg_object) {
745: // search for string in name
746: if(strpos(strtolower($elgg_object->name), $search_string) !== false) {
747: $search_result[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
748: continue;
749: }
750: // if not in name, search in description
751: if($name_only === false) {
752: if(strpos(strtolower($elgg_object->description), $search_string) !== false) {
753: $search_result[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
754: }
755: }
756: }
757: } else { // $strict == true
758: // directly retrieve entities with name = $search_string
759: $elgg_object_array = elgg_get_entities_from_metadata(
760: array(
761: 'type' => static::TYPE,
762: 'subtype' => static::SUBTYPE,
763: 'metadata_names' => array("name"),
764: 'metadata_values' => array($search_string),
765: 'limit' => 0
766: )
767: );
768: if(!empty($elgg_object_array)) {
769: foreach($elgg_object_array as $elgg_object) {
770: $search_result[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
771: }
772: }
773: }
774: if(empty($limit)){
775: return array_slice($search_result, (int)$offset, count($search_result), true);
776: } else {
777: return array_slice($search_result, (int)$offset, (int)$limit, true);
778: }
779: //return $search_result;
780: }
781:
782: /**
783: * Sort by Date, oldest to newest.
784: *
785: * @param static $i1
786: * @param static $i2
787: *
788: * @return int Returns 0 if equal, -1 if i1 before i2, 1 if i1 after i2.
789: */
790: static function sort_by_date($i1, $i2) {
791: if(!$i1 && !$i2){
792: return 0;
793: }elseif(!$i1){
794: return 1;
795: }elseif(!$i2){
796: return -1;
797: }
798: if((int)$i1->time_created == (int)$i2->time_created) {
799: return 0;
800: }
801: return ((int)$i1->time_created < (int)$i2->time_created) ? - 1 : 1;
802: }
803:
804: /**
805: * Sort by Date Inverse order, newest to oldest.
806: *
807: * @param static $i1
808: * @param static $i2
809: *
810: * @return int Returns 0 if equal, -1 if i1 before i2, 1 if i1 after i2.
811: */
812: static function sort_by_date_inv($i1, $i2) {
813: if(!$i1 && !$i2){
814: return 0;
815: }elseif(!$i1){
816: return 1;
817: }elseif(!$i2){
818: return -1;
819: }
820: if((int)$i1->time_created == (int)$i2->time_created) {
821: return 0;
822: }
823: return ((int)$i1->time_created > (int)$i2->time_created) ? - 1 : 1;
824: }
825:
826: /**
827: * Sort by Name, in alphabetical order.
828: *
829: * @param static $i1
830: * @param static $i2
831: *
832: * @return int Returns 0 if equal, -1 if i1 before i2, 1 if i1 after i2.
833: */
834: static function sort_by_name($i1, $i2) {
835: if(!$i1 && !$i2){
836: return 0;
837: }elseif(!$i1){
838: return 1;
839: }elseif(!$i2){
840: return -1;
841: }
842: return strcmp($i1->name, $i2->name);
843: }
844:
845: /**
846: * Sort by Name, in inverse alphabetical order.
847: *
848: * @param static $i1
849: * @param static $i2
850: *
851: * @return int Returns 0 if equal, -1 if i1 before i2, 1 if i1 after i2.
852: */
853: static function sort_by_name_inv($i1, $i2) {
854: if(!$i1 && !$i2){
855: return 0;
856: }elseif(!$i1){
857: return 1;
858: }elseif(!$i2){
859: return -1;
860: }
861: return strcmp($i2->name, $i1->name);
862: }
863:
864: /**
865: * Sort numbers, in increasing order.
866: *
867: * @param float $i1
868: * @param float $i2
869: *
870: * @return int Returns 0 if equal, -1 if i1 before i2, 1 if i1 after i2.
871: */
872: static function sort_numbers($i1, $i2) {
873: if(!$i1 && !$i2){
874: return 0;
875: }elseif(!$i1){
876: return 1;
877: }elseif(!$i2){
878: return -1;
879: }
880: if((int)$i1 == (int)$i2) {
881: return 0;
882: }
883: return ((int)$i1 < (int)$i2) ? - 1 : 1;
884: }
885:
886: /**
887: * Sort numbers, in decreasing order.
888: *
889: * @param float $i1
890: * @param float $i2
891: *
892: * @return int Returns 0 if equal, -1 if i1 before i2, 1 if i1 after i2.
893: */
894: static function sort_numbers_inv($i1, $i2) {
895: if(!$i1 && !$i2){
896: return 0;
897: }elseif(!$i1){
898: return 1;
899: }elseif(!$i2){
900: return -1;
901: }
902: if((int)$i1 == (int)$i2) {
903: return 0;
904: }
905: return ((int)$i1 > (int)$i2) ? - 1 : 1;
906: }
907: }