root/elgg/branches/elgg1.5-sql-fixes/engine/lib/annotations.php

Revision 378, 27.5 kB (checked in by bettse, 5 months ago)

corrections to group by and order by statements

Line 
1 <?php
2     /**
3      * Elgg annotations
4      * Functions to manage object annotations.
5      *
6      * @package Elgg
7      * @subpackage Core
8      * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
9      * @author Curverider Ltd <info@elgg.com>
10      * @copyright Curverider Ltd 2008-2009
11      * @link http://elgg.org/
12      */
13
14     /**
15      * Include the ElggExtender superclass
16      *
17      */
18     require_once('extender.php');
19
20     /**
21      * ElggAnnotation
22      *
23      * An annotation is similar to metadata each entity can contain more than one of each annotation.
24      *
25      * @package Elgg
26      * @subpackage Core
27      * @author Curverider Ltd <info@elgg.com>
28      */
29     class ElggAnnotation extends ElggExtender
30     {
31         
32         /**
33          * Construct a new site object, optionally from a given id value or db row.
34          *
35          * @param mixed $id
36          */
37         function __construct($id = null)
38         {
39             $this->attributes = array();
40             
41             if (!empty($id)) {
42                 if ($id instanceof stdClass)
43                     $annotation = $id;
44                 else
45                     $annotation = get_annotation($id);
46                 
47                 if ($annotation) {
48                     $objarray = (array) $annotation;
49                     foreach($objarray as $key => $value) {
50                         $this->attributes[$key] = $value;
51                     }
52                     $this->attributes['type'] = "annotation";
53                 }
54             }
55         }
56         
57         /**
58          * Class member get overloading
59          *
60          * @param string $name
61          * @return mixed
62          */
63         function __get($name) {
64             return $this->get($name);
65         }
66         
67         /**
68          * Class member set overloading
69          *
70          * @param string $name
71          * @param mixed $value
72          * @return void
73          */
74         function __set($name, $value) {
75             return $this->set($name, $value);
76         }       
77         
78         /**
79          * Save this instance
80          *
81          * @return int an object id
82          */
83         function save()
84         {
85             if ($this->id > 0)
86                 return update_annotation($this->id, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
87             else
88             {
89                 $this->id = create_annotation($this->entity_guid, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
90                 if (!$this->id) throw new IOException(sprintf(elgg_new('IOException:UnableToSaveNew'), get_class()));
91                 return $this->id;
92             }
93         }
94         
95         /**
96          * Delete a given site.
97          */
98         function delete()
99         {
100             return delete_annotation($this->id);
101         }
102         
103         /**
104          * Get a url for this annotation.
105          *
106          * @return string
107          */
108         public function getURL() { return get_annotation_url($this->id); }
109     
110         // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
111
112         /**
113          * For a given ID, return the object associated with it.
114          * This is used by the river functionality primarily.
115          * This is useful for checking access permissions etc on objects.
116          */
117         public function getObjectFromID($id) { return get_annotation($id); }
118     }
119     
120     /**
121      * Convert a database row to a new ElggAnnotation
122      *
123      * @param stdClass $row
124      * @return stdClass or ElggAnnotation
125      */
126     function row_to_elggannotation($row)
127     {
128         if (!($row instanceof stdClass))
129             return $row;
130             
131         return new ElggAnnotation($row);
132     }
133     
134     /**
135      * Get a specific annotation.
136      *
137      * @param int $annotation_id
138      */
139     function get_annotation($annotation_id)
140     {
141         global $CONFIG;
142         
143         $annotation_id = (int) $annotation_id;
144         $access = get_access_sql_suffix("a");
145         
146         return row_to_elggannotation(get_data_row("SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id JOIN {$CONFIG->dbprefix}metastrings v on a.value_id = v.id where a.id=$annotation_id and $access"));           
147     }
148     
149     /**
150      * Create a new annotation.
151      *
152      * @param int $entity_guid
153      * @param string $name
154      * @param string $value
155      * @param string $value_type
156      * @param int $owner_guid
157      * @param int $access_id
158      */
159     function create_annotation($entity_guid, $name, $value, $value_type, $owner_guid, $access_id = ACCESS_PRIVATE)
160     {
161         global $CONFIG;
162
163         $result = false;
164         
165         $entity_guid = (int)$entity_guid;
166         //$name = sanitise_string(trim($name));
167         //$value = sanitise_string(trim($value));
168         $value_type = detect_extender_valuetype($value, sanitise_string(trim($value_type)));
169         
170         $owner_guid = (int)$owner_guid;
171         if ($owner_guid==0) $owner_guid = get_loggedin_userid();
172         
173         $access_id = (int)$access_id;
174         
175         $time = time();
176         
177         // Add the metastring
178         $value = add_metastring($value);
179         if (!$value) return false;
180         
181         $name = add_metastring($name);
182         if (!$name) return false;
183         
184         $entity = get_entity($entity_guid);
185         
186         if (trigger_elgg_event('annotate',$entity->type,$entity)) {
187             system_log($entity, 'annotate');
188             
189             // If ok then add it
190             $result = insert_data("INSERT into {$CONFIG->dbprefix}annotations (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES ($entity_guid,'$name',$value,'$value_type', $owner_guid, $time, $access_id)");
191             if ($result!==false) {
192                 $obj = get_annotation($result);
193                 if (trigger_elgg_event('create', 'annotation', $obj)) {
194                     return true;
195                 } else {
196                     delete_annotation($result);
197                 }
198             }
199         }
200         
201         return $result;
202     }
203     
204     /**
205      * Update an annotation.
206      *
207      * @param int $annotation_id
208      * @param string $name
209      * @param string $value
210      * @param string $value_type
211      * @param int $owner_guid
212      * @param int $access_id
213      */
214     function update_annotation($annotation_id, $name, $value, $value_type, $owner_guid, $access_id)
215     {
216         global $CONFIG;
217
218         $annotation_id = (int)$annotation_id;
219         $name = (trim($name));
220         $value = (trim($value));
221         $value_type = detect_extender_valuetype($value, sanitise_string(trim($value_type)));
222         
223         $owner_guid = (int)$owner_guid;
224         if ($owner_guid==0) $owner_guid = get_loggedin_userid();
225         
226         $access_id = (int)$access_id;
227         
228         $access = get_access_sql_suffix();
229         
230         // Add the metastring
231         $value = add_metastring($value);
232         if (!$value) return false;
233         
234         $name = add_metastring($name);
235         if (!$name) return false;
236         
237         // If ok then add it       
238         $result = update_data("UPDATE {$CONFIG->dbprefix}annotations set value_id='$value', value_type='$value_type', access_id=$access_id, owner_guid=$owner_guid where id=$annotation_id and name_id='$name' and $access");
239         if ($result!==false) {
240             $obj = get_annotation($annotation_id);
241             if (trigger_elgg_event('update', 'annotation', $obj)) {
242                 return true;
243             } else {
244                 delete_annotation($annotation_id);
245             }
246         }
247         
248         return $result;
249     }
250     
251     /**
252      * Get a list of annotations for a given object/user/annotation type.
253      *
254      * @param int|array $entity_guid
255      * @param string $entity_type
256      * @param string $entity_subtype
257      * @param string $name
258      * @param mixed $value
259      * @param int|array $owner_guid
260      * @param int $limit
261      * @param int $offset
262      * @param string $order_by
263      */
264     function get_annotations($entity_guid = 0, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $owner_guid = 0, $limit = 10, $offset = 0, $order_by = "asc")
265     {
266         global $CONFIG;
267         
268         if (is_array($entity_guid)) {
269             if (sizeof($entity_guid) > 0) {
270                 foreach($entity_guid as $key => $val) {
271                     $entity_guid[$key] = (int) $val;           
272                 }
273             } else {
274                 $entity_guid = 0;
275             }
276         } else {
277             $entity_guid = (int)$entity_guid;
278         }
279         $entity_type = sanitise_string($entity_type);
280         $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
281         if ($name)
282         {
283             $name = get_metastring_id($name);
284         
285             if ($name === false)
286                 $name = 0;
287         }
288         if ($value != "") $value = get_metastring_id($value);
289         if (is_array($owner_guid)) {
290             if (sizeof($owner_guid) > 0) {
291                 foreach($owner_guid as $key => $val) {
292                     $owner_guid[$key] = (int) $val;
293                 }
294             } else {
295                 $owner_guid = 0;
296             }
297         } else {
298             $owner_guid = (int)$owner_guid;
299         }
300         $limit = (int)$limit;
301         $offset = (int)$offset;
302         if($order_by == 'asc')
303             $order_by = "a.time_created asc";
304             
305         if($order_by == 'desc')
306             $order_by = "a.time_created desc";
307         
308         $where = array();
309         
310         if ($entity_guid != 0 && !is_array($entity_guid)) {
311             $where[] = "a.entity_guid=$entity_guid";
312         } else if (is_array($entity_guid)) {
313             $where[] = "a.entity_guid in (". implode(",",$entity_guid) . ")";
314         }
315             
316         if ($entity_type != "")
317             $where[] = "e.type='$entity_type'";
318             
319         if ($entity_subtype != "")
320             $where[] = "e.subtype='$entity_subtype'";
321         
322         if ($owner_guid != 0 && !is_array($owner_guid)) {
323             $where[] = "a.owner_guid=$owner_guid";
324         } else {
325             if (is_array($owner_guid))
326                 $where[] = "a.owner_guid in (" . implode(",",$owner_guid) . ")";
327         }
328             
329         if ($name !== "")
330             $where[] = "a.name_id='$name'";
331             
332         if ($value != "")
333             $where[] = "a.value_id='$value'";
334         
335         $query = "SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id where ";
336         foreach ($where as $w)
337             $query .= " $w and ";
338         $query .= get_access_sql_suffix("a"); // Add access controls
339         $query .= " order by $order_by limit $limit offset $offset"; // Add order and limit
340         return get_data($query, "row_to_elggannotation");
341         
342     }
343     
344     /**
345      * Return a list of entities which are annotated with a specific annotation.
346      * These can be ordered by when the annotation was created/updated.
347      *
348      * @param string $entity_type Type of entity.
349      * @param string $entity_subtype Subtype of entity.
350      * @param string $name Name of annotation.
351      * @param string $value Value of annotation.
352      * @param int $owner_guid Owner.
353      * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
354      * @param int $limit Maximum number of results to return.
355      * @param int $offset Place to start.
356      * @param string $order_by How to order results.
357      * @param boolean $count Whether to count entities rather than return them
358      */
359     function get_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $owner_guid = 0, $group_guid = 0, $limit = 10, $offset = 0, $order_by = "asc", $count = false)
360     {
361         global $CONFIG;
362         
363         $entity_type = sanitise_string($entity_type);
364         $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
365         if ($name)
366         {
367             $name = get_metastring_id($name);
368         
369             if ($name === false)
370                 $name = 0;
371         }
372         if ($value != "") $value = get_metastring_id($value);
373         if (is_array($owner_guid)) {
374             if (sizeof($owner_guid) > 0) {
375                 foreach($owner_guid as $key => $val) {
376                     $owner_guid[$key] = (int) $val;
377                 }
378             } else {
379                 $owner_guid = 0;
380             }
381         } else {
382             $owner_guid = (int)$owner_guid;
383         }
384         $group_guid = (int)$group_guid;
385         
386         $limit = (int)$limit;
387         $offset = (int)$offset;
388         if($order_by == 'asc')
389             $order_by = "maxtime asc";
390             
391         if($order_by == 'desc')
392             $order_by = "maxtime desc";
393         
394         $where = array();
395         
396         if ($entity_type != "")
397             $where[] = "e.type='$entity_type'";
398             
399         if ($entity_subtype != "")
400             $where[] = "e.subtype='$entity_subtype'";
401         
402         if ($owner_guid != 0 && !is_array($owner_guid)) {
403             $where[] = "a.owner_guid=$owner_guid";
404         } else {
405             if (is_array($owner_guid))
406                 $where[] = "a.owner_guid in (" . implode(",",$owner_guid) . ")";
407         }
408         
409         if (($group_guid != 0) && ($entity_type=='object'))
410             $where[] = "e.container_guid = $group_guid";
411             
412         if ($name !== "")
413             $where[] = "a.name_id='$name'";
414             
415         if ($value != "")
416             $where[] = "a.value_id='$value'";
417             
418         
419         if ($count) {
420             $query = "SELECT count(distinct e.guid) as total ";
421         } else {
422             $query = "SELECT e.*, max(a.time_created) as maxtime ";           
423         }
424         $query .= "from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on e.guid = a.entity_guid ";
425         if ($value != "")
426             $query .= " JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id";
427         
428         if (($group_guid != 0) && ($entity_type=='object')) $query .= "JOIN {$CONFIG->dbprefix}objects_entity o on o.guid = e.guid";
429         $query .= " where";
430             
431         foreach ($where as $w)
432             $query .= " $w and ";
433         $query .= get_access_sql_suffix("a"); // Add access controls
434         $query .= ' and ' . get_access_sql_suffix("e"); // Add access controls
435         
436         if ($count) {
437             $row = get_data_row($query);
438             return $row->total;
439         } else {
440             $query .= " group by a.entity_guid,e.guid,e.type,e.subtype,e.owner_guid,e.site_guid,e.container_guid,e.access_id,e.time_created,e.time_updated,e.enabled order by $order_by limit $limit offset $offset"; // Add order and limit, also postgres requires all fields enumerated when using group by
441             return get_data($query, "entity_row_to_elggstar");
442         }   
443     }
444     
445     /**
446      * Lists entities
447      *
448      * @see elgg_view_entity_list
449      *
450      * @param string $entity_type Type of entity.
451      * @param string $entity_subtype Subtype of entity.
452      * @param string $name Name of annotation.
453      * @param string $value Value of annotation.
454      * @param int $limit Maximum number of results to return.
455      * @param int $owner_guid Owner.
456      * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
457      * @param boolean $asc Whether to list in ascending or descending order (default: desc)
458      * @param boolean $fullview Whether to display the entities in full
459      * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)
460      * @return string Formatted entity list
461      */
462     function list_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false) {
463         
464         if ($asc) {
465             $asc = "asc";
466         } else {
467             $asc = "desc";
468         }
469         $count = get_entities_from_annotations($entity_type, $entity_subtype, $name, $value, $owner_guid, $group_guid, null, null, $asc, true);
470         $offset = (int) get_input("offset",0);
471         $entities = get_entities_from_annotations($entity_type, $entity_subtype, $name, $value, $owner_guid, $group_guid, $limit, $offset, $asc);
472
473         return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle);
474         
475     }
476     
477     /**
478      * Returns a human-readable list of annotations on a particular entity.
479      *
480      * @param int $entity_guid The entity GUID
481      * @param string $name The name of the kind of annotation
482      * @param int $limit The number of annotations to display at once
483      * @param true|false $asc Whether or not the annotations are displayed in ascending order. (Default: true)
484      * @return string HTML (etc) version of the annotation list
485      */
486     function list_annotations($entity_guid, $name = "", $limit = 25, $asc = true) {
487         
488         if ($asc) {
489             $asc = "asc";
490         } else {
491             $asc = "desc";
492         }
493         $count = count_annotations($entity_guid, "", "", $name);
494         $offset = (int) get_input("annoff",0);
495         $annotations = get_annotations($entity_guid, "", "", $name, "", "", $limit, $offset, $asc);
496         
497         return elgg_view_annotation_list($annotations, $count, $offset, $limit);
498         
499     }
500
501     /**
502      * Return the sum of a given integer annotation.
503      *
504      * @param $entity_guid int
505      * @param $entity_type string
506      * @param $entity_subtype string
507      * @param $name string
508      */
509     function get_annotations_sum($entity_guid, $entity_type = "", $entity_subtype = "", $name = "")
510     {
511         return __get_annotations_calculate_x("sum", $entity_guid, $entity_type, $entity_subtype, $name);
512     }
513     
514     /**
515      * Return the max of a given integer annotation.
516      *
517      * @param $entity_guid int
518      * @param $entity_type string
519      * @param $entity_subtype string
520      * @param $name string
521      */
522     function get_annotations_max($entity_guid, $entity_type = "", $entity_subtype = "", $name = "")
523     {
524         return __get_annotations_calculate_x("max", $entity_guid, $entity_type, $entity_subtype, $name);
525     }
526     
527     /**
528      * Return the minumum of a given integer annotation.
529      *
530      * @param $entity_guid int
531      * @param $entity_type string
532      * @param $entity_subtype string
533      * @param $name string
534      */
535     function get_annotations_min($entity_guid, $entity_type = "", $entity_subtype = "", $name = "")
536     {
537         return __get_annotations_calculate_x("min", $entity_guid, $entity_type, $entity_subtype, $name);
538     }
539     
540     /**
541      * Return the average of a given integer annotation.
542      *
543      * @param $entity_guid int
544      * @param $entity_type string
545      * @param $entity_subtype string
546      * @param $name string
547      */
548     function get_annotations_avg($entity_guid, $entity_type = "", $entity_subtype = "", $name = "")
549     {
550         return __get_annotations_calculate_x("avg", $entity_guid, $entity_type, $entity_subtype, $name);
551     }
552     
553     /**
554      * Count the number of annotations based on search parameters
555      *
556      * @param int $entity_guid
557      * @param string $entity_type
558      * @param string $entity_subtype
559      * @param string $name
560      */
561     function count_annotations($entity_guid = 0, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)
562     {
563         return __get_annotations_calculate_x("count", $entity_guid, $entity_type, $entity_subtype, $name);
564     }
565     
566     /**
567      * Perform a mathmatical calculation on integer annotations.
568      *
569      * @param $sum string
570      * @param $entity_id int
571      * @param $entity_type string
572      * @param $entity_subtype string
573      * @param $name string
574      */
575     function __get_annotations_calculate_x($sum = "avg", $entity_guid, $entity_type = "", $entity_subtype = "", $name = "")
576     {
577         global $CONFIG;
578         
579         $sum = sanitise_string($sum);
580         $entity_guid = (int)$entity_guid;
581         $entity_type = sanitise_string($entity_type);
582         $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
583         if ($name != '') $name = get_metastring_id($name);
584         
585         // if (empty($name)) return 0;
586         
587         $where = array();
588         
589         if ($entity_guid)
590             $where[] = "e.guid=$entity_guid";
591         if ($entity_type!="")
592             $where[] = "e.type='$entity_type'";
593         if ($entity_subtype)
594             $where[] = "e.subtype=$entity_subtype";
595         if ($name!="")
596             $where[] = "a.name_id='$name'";
597             
598         if ($sum != "count")
599             $where[] = "a.value_type='integer'"; // Limit on integer types
600         
601         $query = "SELECT $sum(ms.string) as sum from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id WHERE ";
602         foreach ($where as $w)
603             $query .= " $w and ";
604         $query .= get_access_sql_suffix("a"); // now add access
605         $query .= ' and ' . get_access_sql_suffix("e"); // now add access
606         
607         $row = get_data_row($query);
608         if ($row)
609             return $row->sum;
610             
611         return false;
612     }
613
614     /**
615      * Get entities ordered by a mathematical calculation
616      *
617      * @param $sum string
618      * @param $entity_type string
619      * @param $entity_subtype string
620      * @param $name string
621      * @param $mdname string
622      * @param $mdvalue string
623      * @param $limit int
624      * @param string $orderdir Default: asc - the sort order
625      * @return unknown
626      */
627     function __get_entities_from_annotations_calculate_x($sum = "sum", $entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false)
628     {
629         global $CONFIG;
630         
631         $sum = sanitise_string($sum);
632         $entity_type = sanitise_string($entity_type);
633         $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
634         $name = get_metastring_id($name);
635         $limit = (int) $limit;
636         $offset = (int) $offset;
637         $owner_guid = (int) $owner_guid;
638         if (!empty($mdname) && !empty($mdvalue)) {
639             $meta_n = get_metastring_id($mdname);
640             $meta_v = get_metastring_id($mdvalue);
641         }
642         
643         if (empty($name)) return 0;
644         
645         $where = array();
646         
647         if ($entity_type!="")
648             $where[] = "e.type='$entity_type'";
649         if ($owner_guid > 0)
650             $where[] = "e.container_guid = $owner_guid";
651         if ($entity_subtype)
652             $where[] = "e.subtype=$entity_subtype";
653         if ($name!="")
654             $where[] = "a.name_id='$name'";
655             
656         if (!empty($mdname) && !empty($mdvalue)) {
657             if ($mdname!="")
658                 $where[] = "m.name_id='$meta_n'";
659             if ($mdvalue!="")
660                 $where[] = "m.value_id='$meta_v'";
661         }
662             
663         if ($sum != "count")
664             $where[] = "a.value_type='integer'"; // Limit on integer types
665
666         if (!$count) {
667             $query = "SELECT distinct e.*, $sum(ms.string) as sum ";
668         } else {
669             $query = "SELECT count(distinct e.guid) as num, $sum(ms.string) as sum ";
670         }
671         $query .= " from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}annotations a on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id ";
672         
673         if (!empty($mdname) && !empty($mdvalue)) {
674             $query .= " JOIN {$CONFIG->dbprefix}metadata m on m.entity_guid = e.guid ";
675         }
676         
677         $query .= " WHERE ";
678         foreach ($where as $w)
679             $query .= " $w and ";
680         $query .= get_access_sql_suffix("a"); // now add access
681         $query .= ' and ' . get_access_sql_suffix("e"); // now add access
682         if (!$count) $query .= ' group by e.guid, e.type, e.subtype, e.owner_guid, e.site_guid, e.container_guid, e.access_id, e.time_created, e.time_modified, e.enabled ';
683         
684         if (!$count) {
685             $query .= ' order by count(msvalue.id) ' . $orderdir;
686             $query .= ' limit ' . $limit . ' offset ' . $offset;
687             return get_data($query, "entity_row_to_elggstar");
688         } else {
689             if ($row = get_data_row($query)) {
690                 return $row->num;
691             }
692         }
693         return false;
694     }
695
696     /**
697      * Returns entities ordered by the sum of an annotation
698      *
699      * @param unknown_type $entity_type
700      * @param unknown_type $entity_subtype
701      * @param unknown_type $name
702      * @param string $mdname
703      * @param string $mdvalue
704      * @param unknown_type $owner_guid
705      * @param int $limit
706      * @param int $offset
707      * @param true|false $count
708      * @return unknown
709      */
710     function get_entities_from_annotation_count($entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false) {
711         return __get_entities_from_annotations_calculate_x('sum',$entity_type,$entity_subtype,$name,$mdname, $mdvalue, $owner_guid,$limit, $offset, $orderdir, $count);
712     }
713
714     /**
715      * Lists entities by the totals of a particular kind of annotation
716      *
717      * @param string $entity_type Type of entity.
718      * @param string $entity_subtype Subtype of entity.
719      * @param string $name Name of annotation.
720      * @param int $limit Maximum number of results to return.
721      * @param int $owner_guid Owner.
722      * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
723      * @param boolean $asc Whether to list in ascending or descending order (default: desc)
724      * @param boolean $fullview Whether to display the entities in full
725      * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)
726      * @return string Formatted entity list
727      */
728     function list_entities_from_annotation_count($entity_type = "", $entity_subtype = "", $name = "", $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false, $pagination = true, $orderdir = 'desc') {
729         
730         if ($asc) {
731             $asc = "asc";
732         } else {
733             $asc = "desc";
734         }
735         
736         $offset = (int) get_input("offset",0);
737         $count = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, '', '', $owner_guid, $limit, $offset, $orderdir, true);
738         $entities = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, '', '', $owner_guid, $limit, $offset, $orderdir, false);
739
740         return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
741         
742     }
743     
744     /**
745      * Lists entities by the totals of a particular kind of annotation AND the value of a piece of metadata
746      *
747      * @param string $entity_type Type of entity.
748      * @param string $entity_subtype Subtype of entity.
749      * @param string $name Name of annotation.
750      * @param string $mdname Metadata name
751      * @param string $mdvalue Metadata value
752      * @param int $limit Maximum number of results to return.
753      * @param int $owner_guid Owner.
754      * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
755      * @param boolean $asc Whether to list in ascending or descending order (default: desc)
756      * @param boolean $fullview Whether to display the entities in full
757      * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)
758      * @return string Formatted entity list
759      */
760     function list_entities_from_annotation_count_by_metadata($entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false, $pagination = true, $orderdir = 'desc') {
761         
762         if ($asc) {
763             $asc = "asc";
764         } else {
765             $asc = "desc";
766         }
767         
768         $offset = (int) get_input("offset",0);
769         $count = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, $mdname, $mdvalue, $owner_guid, $limit, $offset, $orderdir, true);
770         $entities = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, $mdname, $mdvalue, $owner_guid, $limit, $offset, $orderdir, false);
771
772         return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
773         
774     }
775     
776     /**
777      * Delete a given annotation.
778      *
779      * @param $id int The id
780      */
781     function delete_annotation($id)
782     {
783         global $CONFIG;
784
785         $id = (int)$id;
786         
787         $access = get_access_sql_suffix();
788         
789         return delete_data("DELETE from {$CONFIG->dbprefix}annotations  where id=$id and $access");
790     }
791     
792     /**
793      * Clear all the annotations for a given entity, assuming you have access to that metadata.
794      *
795      * @param int $guid
796      */
797     function clear_annotations($guid, $name = "")
798     {
799         global $CONFIG;
800         
801         $guid = (int)$guid;
802         
803         if (!empty($name))
804             $name = get_metastring_id($name);
805         
806         $entity_guid = (int) $guid;
807         if ($entity = get_entity($entity_guid)) {
808             if ($entity->canEdit()) {
809         
810                 $where = array();
811                 
812                 if ($name != "")
813                     $where[] = " name_id='$name'";
814                 
815                 $query = "DELETE from {$CONFIG->dbprefix}annotations where entity_guid=$guid ";
816                 foreach ($where as $w)
817                     $query .= " and $w";
818                 
819                 return delete_data($query);
820         
821             }
822         }
823     }
824     
825     /**
826      * Handler called by trigger_plugin_hook on the "export" event.
827      */
828     function export_annotation_plugin_hook($hook, $entity_type, $returnvalue, $params)
829     {
830         // Sanity check values
831         if ((!is_array($params)) && (!isset($params['guid'])))
832             throw new InvalidParameterException(elgg_echo('InvalidParameterException:GUIDNotForExport'));
833             
834         if (!is_array($returnvalue))
835             throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonArrayReturnValue'));
836         
837         $guid = (int)$params['guid'];
838         $name = $params['name'];   
839         
840         $result = get_annotations($guid);
841                 
842         if ($result)
843         {
844             foreach ($result as $r)
845                 $returnvalue[] = $r->export();
846         }
847         
848         return $returnvalue;
849     }
850     
851     /**
852      * Get the URL for this item of metadata, by default this links to the export handler in the current view.
853      *
854      * @param int $id
855      */
856     function get_annotation_url($id)
857     {
858         $id = (int)$id;
859         
860         if ($extender = get_annotation($id)) {
861             return get_extender_url($extender);   
862         }
863         return false;
864     }
865     
866     
867     /**
868      * Register an annotation url handler.
869      *
870      * @param string $function_name The function.
871      * @param string $extender_name The name, default 'all'.
872      */
873     function register_annotation_url_handler($function_name, $extender_name = "all") {
874         return register_extender_url_handler($function_name, 'annotation', $extender_name);
875     }
876     
877     /** Register the hook */
878     register_plugin_hook("export", "all", "export_annotation_plugin_hook", 2);
879 ?>
880
Note: See TracBrowser for help on using the browser.