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

Revision 364, 27.0 kB (checked in by bettse, 5 months ago)

sql fixes from r358,r360,r361,r362,r263. I merged them in and didn't realize they don't get commited automatically. In the future I'll commit them one by one

Line 
1 <?php
2     /**
3      * Elgg relationships.
4      * Stub containing relationship functions, making import and export easier.
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      * Relationship class.
16      *
17      * @author Curverider Ltd
18      * @package Elgg
19      * @subpackage Core
20      */
21     class ElggRelationship implements
22         Importable,
23         Exportable,
24         Loggable,    // Can events related to this object class be logged
25         Iterator,    // Override foreach behaviour
26         ArrayAccess // Override for array access
27     {
28         /**
29          * This contains the site's main properties (id, etc)
30          * @var array
31          */
32         protected $attributes;
33         
34         /**
35          * Construct a new site object, optionally from a given id value or row.
36          *
37          * @param mixed $id
38          */
39         function __construct($id = null)
40         {
41             $this->attributes = array();
42             
43             if (!empty($id)) {
44                 
45                 if ($id instanceof stdClass)
46                     $relationship = $id; // Create from db row
47                 else
48                     $relationship = get_relationship($id);   
49                 
50                 if ($relationship) {
51                     $objarray = (array) $relationship;
52                     foreach($objarray as $key => $value) {
53                         $this->attributes[$key] = $value;
54                     }
55                 }
56             }
57         }
58         
59         /**
60          * Class member get overloading
61          *
62          * @param string $name
63          * @return mixed
64          */
65         protected function __get($name) {
66             if (isset($this->attributes[$name]))
67                 return $this->attributes[$name];
68     
69             return null;
70         }
71         
72         /**
73          * Class member set overloading
74          *
75          * @param string $name
76          * @param mixed $value
77          * @return mixed
78          */
79         protected function __set($name, $value) {
80             $this->attributes[$name] = $value;
81             return true;
82         }
83
84         /**
85          * Save the relationship
86          *
87          * @return int the relationship id
88          */
89         public function save()
90         {
91             if ($this->id > 0)
92             {
93                 delete_relationship($this->id);
94             }
95
96             $this->id = add_entity_relationship($this->guid_one, $this->relationship, $this->guid_two);
97             if (!$this->id) throw new IOException(sprintf(elgg_new('IOException:UnableToSaveNew'), get_class()));
98
99             return $this->id;
100             
101         }
102         
103         /**
104          * Delete a given relationship.
105          */
106         public function delete()
107         {
108             return delete_relationship($this->id);
109         }
110         
111         /**
112          * Get a URL for this relationship.
113          *
114          * @return string
115          */
116         public function getURL()
117         {
118             return get_relationship_url($this->id);
119         }
120     
121         // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
122         
123         /**
124          * Return an array of fields which can be exported.
125          */
126         public function getExportableValues()
127         {
128             return array(
129                 'id',
130                 'guid_one',
131                 'relationship',
132                 'guid_two'
133             );
134         }
135         
136         /**
137          * Export this relationship
138          *
139          * @return array
140          */
141         public function export()
142         {       
143             $uuid = get_uuid_from_object($this);
144             $relationship = new ODDRelationship(
145                 guid_to_uuid($this->guid_one),
146                 $this->relationship,
147                 guid_to_uuid($this->guid_two)
148             );
149             
150             $relationship->setAttribute('uuid', $uuid);
151             
152             return $relationship;
153         }
154         
155         // IMPORTABLE INTERFACE ////////////////////////////////////////////////////////////
156         
157         /**
158          * Import a relationship
159          *
160          * @param array $data
161          * @param int $version
162          * @return ElggRelationship
163          * @throws ImportException
164          */
165         public function import(ODD $data)
166         {
167             if (!($element instanceof ODDRelationship))
168                 throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnexpectedODDClass'));
169             
170             $uuid_one = $data->getAttribute('uuid1');
171             $uuid_two = $data->getAttribute('uuid2');     
172                 
173             // See if this entity has already been imported, if so then we need to link to it
174             $entity1 = get_entity_from_uuid($uuid_one);
175             $entity2 = get_entity_from_uuid($uuid_two);
176             if (($entity1) && ($entity2))
177             {
178                 // Set the item ID
179                 $this->attributes['guid_one'] = $entity1->getGUID();
180                 $this->attributes['guid_two'] = $entity2->getGUID();
181                 
182                 // Map verb to relationship
183                 //$verb = $data->getAttribute('verb');
184                 //$relationship = get_relationship_from_verb($verb);
185                 $relationship = $data->getAttribute('type');
186                 
187                 if ($relationship)
188                 {   
189                     $this->attributes['relationship'] = $relationship;
190                     // save
191                     $result = $this->save();
192                     if (!$result)
193                         throw new ImportException(sprintf(elgg_echo('ImportException:ProblemSaving'), get_class()));
194                     
195                     return $this;
196                 }
197             }
198         }
199         
200         // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
201         
202         /**
203          * Return an identification for the object for storage in the system log.
204          * This id must be an integer.
205          *
206          * @return int
207          */
208         public function getSystemLogID() { return $this->id;    }
209         
210         /**
211          * Return the class name of the object.
212          */
213         public function getClassName() { return get_class($this); }
214         
215         /**
216          * For a given ID, return the object associated with it.
217          * This is used by the river functionality primarily.
218          * This is useful for checking access permissions etc on objects.
219          */
220         public function getObjectFromID($id) { return get_relationship($id); }
221         
222         /**
223          * Return the GUID of the owner of this object.
224          */
225         public function getObjectOwnerGUID() { return $this->owner_guid; }
226         
227         /**
228          * Return a type of the object - eg. object, group, user, relationship, metadata, annotation etc
229          */
230         public function getType() { return 'relationship'; }
231         
232         /**
233          * Return a subtype. For metadata & annotations this is the 'name' and for relationship this is the relationship type.
234          */
235         public function getSubtype() { return $this->relationship; }
236         
237         // ITERATOR INTERFACE //////////////////////////////////////////////////////////////
238         /*
239          * This lets an entity's attributes be displayed using foreach as a normal array.
240          * Example: http://www.sitepoint.com/print/php5-standard-library
241          */
242         
243         private $valid = FALSE;
244         
245            function rewind()
246            {
247                $this->valid = (FALSE !== reset($this->attributes)); 
248            }
249   
250            function current()
251            {
252                return current($this->attributes);
253            }
254         
255            function key()
256            {
257                return key($this->attributes);
258            }
259         
260            function next()
261            {
262                $this->valid = (FALSE !== next($this->attributes)); 
263            }
264           
265            function valid()
266            {
267                return $this->valid
268            }
269     
270            // ARRAY ACCESS INTERFACE //////////////////////////////////////////////////////////
271         /*
272          * This lets an entity's attributes be accessed like an associative array.
273          * Example: http://www.sitepoint.com/print/php5-standard-library
274          */
275
276         function offsetSet($key, $value)
277         {
278                if ( array_key_exists($key, $this->attributes) ) {
279                  $this->attributes[$key] = $value;
280                }
281          }
282         
283          function offsetGet($key)
284          {
285                if ( array_key_exists($key, $this->attributes) ) {
286                  return $this->attributes[$key];
287                }
288          }
289         
290          function offsetUnset($key)
291          {
292                if ( array_key_exists($key, $this->attributes) ) {
293                  $this->attributes[$key] = ""; // Full unsetting is dangerious for our objects
294                }
295          }
296         
297          function offsetExists($offset)
298          {
299                return array_key_exists($offset, $this->attributes);
300          }
301     }
302     
303     
304     /**
305      * Convert a database row to a new ElggRelationship
306      *
307      * @param stdClass $row
308      * @return stdClass or ElggMetadata
309      */
310     function row_to_elggrelationship($row)
311     {
312         if (!($row instanceof stdClass))
313             return $row;
314             
315         return new ElggRelationship($row);
316     }
317     
318     /**
319      * Return a relationship.
320      *
321      * @param int $id
322      */
323     function get_relationship($id)
324     {
325         global $CONFIG;
326         
327         $id = (int)$id;
328         
329         return row_to_elggrelationship(get_data_row("SELECT * from {$CONFIG->dbprefix}entity_relationships where id=$id"));
330     }
331     
332     /**
333      * Delete a specific relationship.
334      *
335      * @param int $id
336      */
337     function delete_relationship($id)
338     {
339         global $CONFIG;
340         
341         $id = (int)$id;
342         
343         $result = delete_data("delete from {$CONFIG->dbprefix}entity_relationships where id=$id");
344         
345         return $result;
346     }
347     
348     /**
349      * Define an arbitrary relationship between two entities.
350      * This relationship could be a friendship, a group membership or a site membership.
351      *
352      * This function lets you make the statement "$guid_one has $relationship with $guid_two".
353      *
354      * @param int $guid_one
355      * @param string $relationship
356      * @param int $guid_two
357      */
358     function add_entity_relationship($guid_one, $relationship, $guid_two)
359     {
360         global $CONFIG;
361         
362         $guid_one = (int)$guid_one;
363         $relationship = sanitise_string($relationship);
364         $guid_two = (int)$guid_two;
365             
366         // Check for duplicates
367         if (check_entity_relationship($guid_one, $relationship, $guid_two))
368             return false;
369         
370         $result = insert_data("INSERT into {$CONFIG->dbprefix}entity_relationships (guid_one, relationship, guid_two) values ($guid_one, '$relationship', $guid_two)");
371         
372         if ($result!==false) {
373             $obj = get_relationship($result);
374             if (trigger_elgg_event('create', $relationship, $obj)) {
375                 return true;
376             } else {
377                 delete_relationship($result);
378             }
379         }
380                 
381         return false;
382     }
383     
384     /**
385      * Determine whether or not a relationship between two entities exists and returns the relationship object if it does
386      *
387      * @param int $guid_one The GUID of the entity "owning" the relationship
388      * @param string $relationship The type of relationship
389      * @param int $guid_two The GUID of the entity the relationship is with
390      * @return object|false Depending on success
391      */
392     function check_entity_relationship($guid_one, $relationship, $guid_two)
393     {
394         global $CONFIG;
395         
396         $guid_one = (int)$guid_one;
397         $relationship = sanitise_string($relationship);
398         $guid_two = (int)$guid_two;
399             
400         if ($row = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entity_relationships WHERE guid_one=$guid_one AND relationship='$relationship' AND guid_two=$guid_two limit 1")) {
401             return $row;
402         }
403         return false;
404     }
405
406     /**
407      * Remove an arbitrary relationship between two entities.
408      *
409      * @param int $guid_one
410      * @param string $relationship
411      * @param int $guid_two
412      */
413     function remove_entity_relationship($guid_one, $relationship, $guid_two)
414     {
415         global $CONFIG;
416         
417         $guid_one = (int)$guid_one;
418         $relationship = sanitise_string($relationship);
419         $guid_two = (int)$guid_two;
420         
421         $obj = check_entity_relationship($guid_one, $relationship, $guid_two);
422         if ($obj == false) return false;
423         
424         if (trigger_elgg_event('delete', $relationship, $obj)) {
425             return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one and relationship='$relationship' and guid_two=$guid_two");
426         } else {
427             return false;
428         }
429     }
430
431     /**
432      * Removes all arbitrary relationships originating from a particular entity
433      *
434      * @param int $guid_one The GUID of the entity
435      * @param string $relationship The name of the relationship (optionally)
436      * @param true|false $inverse Whether we're deleting inverse relationships (default false)
437      * @param string $type The type of entity to limit this relationship delete to (defaults to all)
438      * @return true|false Depending on success
439      */
440     function remove_entity_relationships($guid_one, $relationship = "", $inverse = false, $type = '') {
441         
442         global $CONFIG;
443         
444         $guid_one = (int) $guid_one;
445         
446         if (!empty($relationship)) {
447             $relationship = sanitise_string($relationship);
448             $where = "and er.relationship='$relationship'";
449         } else {
450             $where = "";
451         }
452         
453         if (!empty($type)) {
454             $type = sanitise_string($type);
455             if (!$inverse) {
456                 $join = " join {$CONFIG->dbprefix}entities e on e.guid = er.guid_two ";
457             } else {
458                 $join = " join {$CONFIG->dbprefix}entities e on e.guid = er.guid_one ";
459                 $where .= " and ";
460             }
461             $where .= " and e.type = '{$type}' ";
462         } else {
463             $join = "";
464         }
465         
466         if (!$inverse) {
467             $sql = "DELETE from {$CONFIG->dbprefix}entity_relationships er {$join} where er.guid_one={$guid_one} {$where}";
468             return delete_data($sql);
469         } else {
470             $sql = "DELETE from {$CONFIG->dbprefix}entity_relationships er {$join} where er.guid_two={$guid_one} {$where}";
471             return delete_data($sql);
472         }
473         
474     }
475
476     /**
477      * Get all the relationships for a given guid.
478      *
479      * @param int $guid
480      */
481     function get_entity_relationships($guid)
482     {
483         global $CONFIG;
484         
485         $guid = (int)$guid;
486         
487         $query = "SELECT * from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid";
488         
489         return get_data($query, "row_to_elggrelationship");
490     }
491     
492     /**
493      * Return entities matching a given query joining against a relationship.
494      *
495      * @param string $relationship The relationship eg "friends_of"
496      * @param int $relationship_guid The guid of the entity to use query
497      * @param bool $inverse_relationship Reverse the normal function of the query to instead say "give me all entities for whome $relationship_guid is a $relationship of"
498      * @param string $type
499      * @param string $subtype
500      * @param int $owner_guid
501      * @param string $order_by
502      * @param int $limit
503      * @param int $offset
504      * @param boolean $count Set to true if you want to count the number of entities instead (default false)
505      * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
506      * @return array|int|false An array of entities, or the number of entities, or false on failure
507      */
508     function get_entities_from_relationship($relationship, $relationship_guid, $inverse_relationship = false, $type = "", $subtype = "", $owner_guid = 0, $order_by = "", $limit = 10, $offset = 0, $count = false, $site_guid = 0)
509     {
510         global $CONFIG;
511         
512         $relationship = sanitise_string($relationship);
513         $relationship_guid = (int)$relationship_guid;
514         $inverse_relationship = (bool)$inverse_relationship;
515         $type = sanitise_string($type);
516         $subtype = get_subtype_id($type, $subtype);
517         $owner_guid = (int)$owner_guid;
518         if ($order_by == "") $order_by = "time_created desc";
519         $order_by = sanitise_string($order_by);
520         $limit = (int)$limit;
521         $offset = (int)$offset;
522         $site_guid = (int) $site_guid;
523         if ($site_guid == 0)
524             $site_guid = $CONFIG->site_guid;
525         
526         //$access = get_access_list();
527         
528         $where = array();
529         
530         if ($relationship!="")
531             $where[] = "r.relationship='$relationship'";
532         if ($relationship_guid)
533             $where[] = ($inverse_relationship ? "r.guid_two='$relationship_guid'" : "r.guid_one='$relationship_guid'");
534         if ($type != "")
535             $where[] = "e.type='$type'";
536         if ($subtype)
537             $where[] = "e.subtype=$subtype";
538         if ($owner_guid != "")
539             $where[] = "e.container_guid='$owner_guid'";
540         if ($site_guid > 0)
541             $where[] = "e.site_guid = {$site_guid}";
542         
543         // Select what we're joining based on the options
544         $joinon = "e.guid = r.guid_one";
545         if (!$inverse_relationship)
546             $joinon = "e.guid = r.guid_two";   
547             
548         if ($count) {
549             $query = "SELECT count(distinct e.guid) as total ";
550         } else {
551             $query = "SELECT distinct e.* ";
552         }
553         $query .= " from {$CONFIG->dbprefix}entity_relationships r JOIN {$CONFIG->dbprefix}entities e on $joinon where ";
554         foreach ($where as $w)
555             $query .= " $w and ";
556         $query .= get_access_sql_suffix("e"); // Add access controls
557         if (!$count) {
558             $query .= " order by $order_by limit $limit offset $offset"; // Add order and limit
559             return get_data($query, "entity_row_to_elggstar");
560         } else {
561             if ($count = get_data_row($query)) {
562                 return $count->total;
563             }
564         }
565         return false;
566         
567     }
568
569     /**
570      * Returns a viewable list of entities by relationship
571      *
572      * @see elgg_view_entity_list
573      *
574      * @param string $relationship The relationship eg "friends_of"
575      * @param int $relationship_guid The guid of the entity to use query
576      * @param bool $inverse_relationship Reverse the normal function of the query to instead say "give me all entities for whome $relationship_guid is a $relationship of"
577      * @param string $type The type of entity (eg 'object')
578      * @param string $subtype The entity subtype
579      * @param int $owner_guid The owner (default: all)
580      * @param int $limit The number of entities to display on a page
581      * @param true|false $fullview Whether or not to display the full view (default: true)
582      * @param true|false $viewtypetoggle Whether or not to allow gallery view
583      * @param true|false $pagination Whether to display pagination (default: true)
584      * @return string The viewable list of entities
585      */
586     function list_entities_from_relationship($relationship, $relationship_guid, $inverse_relationship = false, $type = "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true, $viewtypetoggle = false, $pagination = true) {
587         
588         $limit = (int) $limit;
589         $offset = (int) get_input('offset');
590         $count = get_entities_from_relationship($relationship, $relationship_guid, $inverse_relationship, $type, $subtype, $owner_guid, "", $limit, $offset, true);
591         $entities = get_entities_from_relationship($relationship, $relationship_guid, $inverse_relationship, $type, $subtype, $owner_guid, "", $limit, $offset);
592
593         return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
594         
595     }
596
597     /**
598      * Gets the number of entities by a the number of entities related to them in a particular way.
599      * This is a good way to get out the users with the most friends, or the groups with the most members.
600      *
601      * @param string $relationship The relationship eg "friends_of"
602      * @param bool $inverse_relationship Reverse the normal function of the query to instead say "give me all entities for whome $relationship_guid is a $relationship of" (default: true)
603      * @param string $type The type of entity (default: all)
604      * @param string $subtype The entity subtype (default: all)
605      * @param int $owner_guid The owner of the entities (default: none)
606      * @param int $limit
607      * @param int $offset
608      * @param boolean $count Set to true if you want to count the number of entities instead (default false)
609      * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
610      * @return array|int|false An array of entities, or the number of entities, or false on failure
611      */
612     
613     function get_entities_by_relationship_count($relationship, $inverse_relationship = true, $type = "", $subtype = "", $owner_guid = 0, $limit = 10, $offset = 0, $count = false, $site_guid = 0) {
614         
615         global $CONFIG;
616         
617         $relationship = sanitise_string($relationship);
618         $inverse_relationship = (bool)$inverse_relationship;
619         $type = sanitise_string($type);
620         $subtype = get_subtype_id($type, $subtype);
621         $owner_guid = (int)$owner_guid;
622         $order_by = sanitise_string($order_by);
623         $limit = (int)$limit;
624         $offset = (int)$offset;
625         $site_guid = (int) $site_guid;
626         if ($site_guid == 0)
627             $site_guid = $CONFIG->site_guid;
628         
629         //$access = get_access_list();
630         
631         $where = array();
632         
633         if ($relationship!="")
634             $where[] = "r.relationship='$relationship'";
635         if ($inverse_relationship) {
636             $on = 'e.guid = r.guid_two';
637         } else {
638             $on = 'e.guid = r.guid_one';
639         }
640         if ($type != "")
641             $where[] = "e.type='$type'";
642         if ($subtype)
643             $where[] = "e.subtype=$subtype";
644         if ($owner_guid != "")
645             $where[] = "e.container_guid='$owner_guid'";
646         if ($site_guid > 0)
647             $where[] = "e.site_guid = {$site_guid}";
648         
649         if ($count) {
650             $query = "SELECT count(distinct e.guid) as total ";
651         } else {
652             $query = "SELECT e.*, count(e.guid) as total ";
653         }
654         
655         $query .= " from {$CONFIG->dbprefix}entity_relationships r JOIN {$CONFIG->dbprefix}entities e on {$on} where ";
656         
657         if (!empty($where))
658         foreach ($where as $w)
659             $query .= " $w and ";
660         $query .= get_access_sql_suffix("e"); // Add access controls
661         
662         if (!$count) {
663             $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_updated,e.enabled ";//postgres requires all fields being selected to be listed in the group by clause. 
664             $query .= " order by total desc limit {$limit} offset {$offset}"; // Add order and limit
665             return get_data($query, "entity_row_to_elggstar");
666         } else {
667             if ($count = get_data_row($query)) {
668                 return $count->total;
669             }
670         }
671         
672         return false;
673             
674     }
675     
676     /**
677      * Displays a human-readable list of entities
678      *
679      * @param string $relationship The relationship eg "friends_of"
680      * @param bool $inverse_relationship Reverse the normal function of the query to instead say "give me all entities for whome $relationship_guid is a $relationship of" (default: true)
681      * @param string $type The type of entity (eg 'object')
682      * @param string $subtype The entity subtype
683      * @param int $owner_guid The owner (default: all)
684      * @param int $limit The number of entities to display on a page
685      * @param true|false $fullview Whether or not to display the full view (default: true)
686      * @param true|false $viewtypetoggle Whether or not to allow gallery view
687      * @param true|false $pagination Whether to display pagination (default: true)
688      * @return string The viewable list of entities
689      */
690     
691     function list_entities_by_relationship_count($relationship, $inverse_relationship = true, $type = "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true, $viewtypetoggle = false, $pagination = true) {
692         
693         $limit = (int) $limit;
694         $offset = (int) get_input('offset');
695         $count = get_entities_by_relationship_count($relationship,$inverse_relationship,$type,$subtype,$owner_guid,0,0,true);
696         $entities = get_entities_by_relationship_count($relationship,$inverse_relationship,$type,$subtype,$owner_guid,$limit,$offset);
697
698         return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
699         
700     }
701     
702     /**
703      * Sets the URL handler for a particular relationship type
704      *
705      * @param string $function_name The function to register
706      * @param string $relationship_type The relationship type.
707      * @return true|false Depending on success
708      */
709     function register_relationship_url_handler($function_name, $relationship_type = "all") {
710         global $CONFIG;
711         
712         if (!is_callable($function_name)) return false;
713         
714         if (!isset($CONFIG->relationship_url_handler)) {
715             $CONFIG->relationship_url_handler = array();
716         }
717         
718         $CONFIG->relationship_url_handler[$relationship_type] = $function_name;
719         
720         return true;
721         
722     }
723     
724     /**
725      * Get the url for a given relationship.
726      *
727      * @param unknown_type $id
728      * @return unknown
729      */
730     function get_relationship_url($id)
731     {
732         global $CONFIG;
733         
734         $id = (int)$id;
735         
736         if ($relationship = get_relationship($id))
737         {
738             $view = elgg_get_viewtype();
739                 
740             $guid = $relationship->guid_one;
741             $type = $relationship->relationship;
742             
743             $url = "";
744             
745             $function = "";
746             if (isset($CONFIG->relationship_url_handler[$type]))
747                 $function = $CONFIG->relationship_url_handler[$type];
748             if (isset($CONFIG->relationship_url_handler['all']))
749                 $function = $CONFIG->relationship_url_handler['all'];
750                 
751             if (is_callable($function)) {
752                 $url = $function($relationship);
753             }
754             
755             if ($url == "") {
756                 
757                 $nameid = $relationship->id;
758                 
759                 $url = $CONFIG->wwwroot  . "export/$view/$guid/relationship/$nameid/";
760             }
761             
762             return $url;
763             
764         }
765         
766         return false;
767     }
768     
769     /**** HELPER FUNCTIONS FOR RELATIONSHIPS OF TYPE 'ATTACHED' ****/
770     
771      /**
772      * Function to determine if the object trying to attach to other, has already done so
773      * @param int $guid_one This is the target object
774      * @param int $guid_two This is the object trying to attach to $guid_one
775      * @return true | false
776      **/
777     
778      function already_attached($guid_one, $guid_two){
779     
780          if($attached = check_entity_relationship($guid_one, "attached", $guid_two)){
781              return true;
782          }else{
783              return false;
784          }
785      }
786     
787      /**
788      * Function to get all objects attached to a particular object
789      * @param int $guid
790      * @param string $type - the type of object to return e.g. 'file', 'friend_of' etc
791      * @return an array of objects
792     **/
793         
794         function get_attachments($guid, $type=""){
795             
796             $attached = get_entities_from_relationship("attached", $guid, $inverse_relationship = false, $type, $subtype = "", $owner_guid = 0, $order_by = "time_created desc", $limit = 10, $offset = 0, $count = false, $site_guid = 0);
797             return $attached;
798           
799         }
800         
801      /**
802      * Function to remove a particular attachment between two objects
803      * @param int $guid_one This is the target object
804      * @param int $guid_two This is the object to remove from $guid_one
805      * @return a view
806     **/
807         
808         function remove_attachment($guid_one, $guid_two){
809             
810             if(already_attached($guid_one, $guid_two))
811                 remove_entity_relationship($guid_one, "attached", $guid_two);
812           
813         }
814         
815         
816     
817      /**
818      * Function to start the process of attaching one object to another
819      * @param int $guid_one This is the target object
820      * @param int $guid_two This is the object trying to attach to $guid_one
821      * @return a view
822     **/
823         
824         function make_attachment($guid_one, $guid_two){
825             
826             if(!(already_attached($guid_one, $guid_two)))
827                 if(add_entity_relationship($guid_one, "attached", $guid_two))
828                     return true;
829           
830         }
831     
832     /**
833      *  Handler called by trigger_plugin_hook on the "import" event.
834      */
835     function import_relationship_plugin_hook($hook, $entity_type, $returnvalue, $params)
836     {
837         $element = $params['element'];
838         
839         $tmp = NULL;
840         
841         if ($element instanceof ODDRelationship)
842         {
843             $tmp = new ElggRelationship();
844             $tmp->import($element);
845             
846             return $tmp;
847         }
848     }
849     
850     /**
851      *  Handler called by trigger_plugin_hook on the "export" event.
852      */
853     function export_relationship_plugin_hook($hook, $entity_type, $returnvalue, $params)
854     {
855         global $CONFIG;
856         
857         // Sanity check values
858         if ((!is_array($params)) && (!isset($params['guid'])))
859             throw new InvalidParameterException(elgg_echo('InvalidParameterException:GUIDNotForExport'));
860             
861         if (!is_array($returnvalue))
862             throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonArrayReturnValue'));
863             
864         $guid = (int)$params['guid'];
865         
866         $result = get_entity_relationships($guid);
867         
868         if ($result)
869         {
870             foreach ($result as $r)
871                 $returnvalue[] = $r->export();
872         }
873         
874         return $returnvalue;
875     }
876
877     /**
878      * An event listener which will notify users based on certain events.
879      *
880      * @param unknown_type $event
881      * @param unknown_type $object_type
882      * @param unknown_type $object
883      */
884     function relationship_notification_hook($event, $object_type, $object)
885     {
886         global $CONFIG;
887         
888         if (
889             ($object instanceof ElggRelationship) &&
890             ($event == 'create') &&
891             ($object_type == 'friend')
892         )
893         {
894             $user_one = get_entity($object->guid_one);
895             $user_two = get_entity($object->guid_two);
896             
897             // Notify target user
898             return notify_user($object->guid_two, $object->guid_one, sprintf(elgg_echo('friend:newfriend:subject'), $user_one->name),
899                 sprintf(elgg_echo("friend:newfriend:body"), $user_one->name, $CONFIG->site->url . "pg/profile/" . $user_one->username)
900             );
901         }
902     }
903     
904     /** Register the import hook */
905     register_plugin_hook("import", "all", "import_relationship_plugin_hook", 3);
906     
907     /** Register the hook, ensuring entities are serialised first */
908     register_plugin_hook("export", "all", "export_relationship_plugin_hook", 3);
909     
910     /** Register event to listen to some events **/
911     register_elgg_event_handler('create','friend','relationship_notification_hook');
912 ?>
913
Note: See TracBrowser for help on using the browser.