Elgg  Version 3.0
ElggRelationship.php
Go to the documentation of this file.
1 <?php
14 class ElggRelationship extends \ElggData {
15  // database column limit
16  const RELATIONSHIP_LIMIT = 50;
17 
24  public function __construct(\stdClass $row) {
25  $this->initializeAttributes();
26 
27  foreach ((array) $row as $key => $value) {
28  $this->attributes[$key] = $value;
29  }
30 
31  $this->attributes['id'] = (int) $this->attributes['id'];
32  }
33 
41  protected function initializeAttributes() {
42  parent::initializeAttributes();
43 
44  $this->attributes['id'] = null;
45  $this->attributes['guid_one'] = null;
46  $this->attributes['relationship'] = null;
47  $this->attributes['guid_two'] = null;
48  }
49 
57  public function __set($name, $value) {
58  $this->attributes[$name] = $value;
59  }
60 
67  public function __get($name) {
68  if (array_key_exists($name, $this->attributes)) {
69  return $this->attributes[$name];
70  }
71 
72  return null;
73  }
74 
81  public function save() {
82  if ($this->id > 0) {
83  delete_relationship($this->id);
84  }
85 
86  $id = _elgg_services()->relationshipsTable->add(
87  $this->guid_one,
88  $this->relationship,
89  $this->guid_two,
90  true
91  );
92  if ($id === false) {
93  throw new \IOException("Unable to save new " . get_class());
94  }
95 
96  $this->id = $id;
97 
98  return $this->id;
99  }
100 
106  public function delete() {
107  return delete_relationship($this->id);
108  }
109 
118  public function getURL() {
119  $url = '';
120  // @todo remove when elgg_register_relationship_url_handler() has been removed
121  if ($this->id) {
122  $subtype = $this->getSubtype();
123 
124  $function = "";
125  $handlers = _elgg_config()->relationship_url_handler;
126 
127  if (isset($handlers[$subtype])) {
128  $function = $handlers[$subtype];
129  }
130  if (isset($handlers['all'])) {
131  $function = $handlers['all'];
132  }
133 
134  if (is_callable($function)) {
135  $url = call_user_func($function, $this);
136  }
137 
138  if ($url) {
140  }
141  }
142 
143  $type = $this->getType();
144  $params = ['relationship' => $this];
145  $url = _elgg_services()->hooks->trigger('relationship:url', $type, $params, $url);
146 
147  return elgg_normalize_url($url);
148  }
149 
153  public function toObject(array $params = []) {
154  $object = new \Elgg\Export\Relationship();
155  $object->id = $this->id;
156  $object->subject_guid = $this->guid_one;
157  $object->relationship = $this->relationship;
158  $object->object_guid = $this->guid_two;
159  $object->time_created = date('c', $this->getTimeCreated());
160 
161  $params['relationship'] = $this;
162 
163  return _elgg_services()->hooks->trigger('to:object', 'relationship', $params, $object);
164  }
165 
166  // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
167 
174  public function getSystemLogID() {
175  return $this->id;
176  }
177 
187  public function getObjectFromID($id) {
188  return get_relationship($id);
189  }
190 
196  public function getType() {
197  return 'relationship';
198  }
199 
206  public function getSubtype() {
207  return $this->relationship;
208  }
209 }
toObject(array $params=[])
{}
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:106
get_relationship($id)
Get a relationship by its ID.
$params
Saves global plugin settings.
Definition: save.php:13
elgg_normalize_url($url)
Definition: output.php:186
getSystemLogID()
Return an identification for the object for storage in the system log.
__set($name, $value)
Set an attribute of the relationship.
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
__get($name)
Get an attribute of the relationship.
getType()
Return a type of the object - eg.
initializeAttributes()
(non-PHPdoc)
$id
River item delete action.
Definition: delete.php:6
$url
Definition: default.php:33
delete_relationship($id)
Delete a relationship by its ID.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
_elgg_config()
Get the Elgg config service.
getObjectFromID($id)
For a given ID, return the object associated with it.
$value
Definition: debugging.php:7
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
save()
Save the relationship.
__construct(\stdClass $row)
Create a relationship object.
getURL()
Get a URL for this relationship.
getSubtype()
Return a subtype.