Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
ElggRelationship.php
Go to the documentation of this file.
1 <?php
14 class ElggRelationship extends \ElggData implements
15  Importable // deprecated
16 {
17  // database column limit
18  const RELATIONSHIP_LIMIT = 50;
19 
26  public function __construct($row = null) {
27  $this->initializeAttributes();
28 
29  if ($row === null) {
30  elgg_deprecated_notice('Passing null to constructor is deprecated. Use add_entity_relationship()', 1.9);
31  return;
32  }
33 
34  if (!($row instanceof \stdClass)) {
35  if (!is_numeric($row)) {
36  throw new \InvalidArgumentException("Constructor accepts only a \stdClass or null.");
37  }
38 
39  $id = (int)$row;
40  elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use get_relationship()', 1.9);
41  $row = _elgg_services()->relationshipsTable->getRow($id);
42  if (!$row) {
43  throw new \InvalidArgumentException("Relationship not found with ID $id");
44  }
45  }
46 
47  foreach ((array)$row as $key => $value) {
48  $this->attributes[$key] = $value;
49  }
50 
51  $this->attributes['id'] = (int)$this->attributes['id'];
52  }
53 
61  protected function initializeAttributes() {
62  parent::initializeAttributes();
63 
64  $this->attributes['id'] = null;
65  $this->attributes['guid_one'] = null;
66  $this->attributes['relationship'] = null;
67  $this->attributes['guid_two'] = null;
68  }
69 
77  public function __set($name, $value) {
78  $this->attributes[$name] = $value;
79  }
80 
89  public function set($name, $value) {
90  elgg_deprecated_notice("Use -> instead of set()", 1.9);
91  $this->__set($name, $value);
92  return true;
93  }
94 
101  public function __get($name) {
102  if (array_key_exists($name, $this->attributes)) {
103  return $this->attributes[$name];
104  }
105 
106  return null;
107  }
108 
116  public function get($name) {
117  elgg_deprecated_notice("Use -> instead of get()", 1.9);
118  return $this->__get($name);
119  }
120 
127  public function save() {
128  if ($this->id > 0) {
129  delete_relationship($this->id);
130  }
131 
132  $this->id = _elgg_services()->relationshipsTable->add(
133  $this->guid_one,
134  $this->relationship,
135  $this->guid_two,
136  true
137  );
138  if (!$this->id) {
139  throw new \IOException("Unable to save new " . get_class());
140  }
141 
142  return $this->id;
143  }
144 
150  public function delete() {
151  return delete_relationship($this->id);
152  }
153 
162  public function getURL() {
163  $url = '';
164  // @todo remove when elgg_register_relationship_url_handler() has been removed
165  if ($this->id) {
166  global $CONFIG;
167 
168  $subtype = $this->getSubtype();
169 
170  $function = "";
171  if (isset($CONFIG->relationship_url_handler[$subtype])) {
172  $function = $CONFIG->relationship_url_handler[$subtype];
173  }
174  if (isset($CONFIG->relationship_url_handler['all'])) {
175  $function = $CONFIG->relationship_url_handler['all'];
176  }
177 
178  if (is_callable($function)) {
179  $url = call_user_func($function, $this);
180  }
181 
182  if ($url) {
184  }
185  }
186 
187  $type = $this->getType();
188  $params = array('relationship' => $this);
189  $url = _elgg_services()->hooks->trigger('relationship:url', $type, $params, $url);
190 
191  return elgg_normalize_url($url);
192  }
193 
197  public function toObject() {
198  $object = new \stdClass();
199  $object->id = $this->id;
200  $object->subject_guid = $this->guid_one;
201  $object->relationship = $this->relationship;
202  $object->object_guid = $this->guid_two;
203  $object->time_created = date('c', $this->getTimeCreated());
204  $params = array('relationship' => $this);
205  return _elgg_services()->hooks->trigger('to:object', 'relationship', $params, $object);
206  }
207 
208  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
209 
216  public function getExportableValues() {
217  elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
218  return array(
219  'id',
220  'guid_one',
221  'relationship',
222  'guid_two'
223  );
224  }
225 
232  public function export() {
233  elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
234  $uuid = get_uuid_from_object($this);
235  $relationship = new ODDRelationship(
236  guid_to_uuid($this->guid_one),
237  $this->relationship,
238  guid_to_uuid($this->guid_two)
239  );
240 
241  $relationship->setAttribute('uuid', $uuid);
242 
243  return $relationship;
244  }
245 
246  // IMPORTABLE INTERFACE ////////////////////////////////////////////////////////////
247 
257  public function import(ODD $data) {
258  elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
259  if (!($data instanceof ODDRelationship)) {
260  throw new \InvalidParameterException("import() passed an unexpected ODD class");
261  }
262 
263  $uuid_one = $data->getAttribute('uuid1');
264  $uuid_two = $data->getAttribute('uuid2');
265 
266  // See if this entity has already been imported, if so then we need to link to it
267  $entity1 = get_entity_from_uuid($uuid_one);
268  $entity2 = get_entity_from_uuid($uuid_two);
269  if (($entity1) && ($entity2)) {
270  // Set the item ID
271  $this->attributes['guid_one'] = $entity1->getGUID();
272  $this->attributes['guid_two'] = $entity2->getGUID();
273 
274  // Map verb to relationship
275  //$verb = $data->getAttribute('verb');
276  //$relationship = get_relationship_from_verb($verb);
277  $relationship = $data->getAttribute('type');
278 
279  if ($relationship) {
280  $this->attributes['relationship'] = $relationship;
281  // save
282  $result = $this->save();
283  if (!$result) {
284  throw new \ImportException("There was a problem saving " . get_class());
285  }
286 
287  return true;
288  }
289  }
290 
291  return false;
292  }
293 
294  // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
295 
302  public function getSystemLogID() {
303  return $this->id;
304  }
305 
315  public function getObjectFromID($id) {
316  return get_relationship($id);
317  }
318 
324  public function getType() {
325  return 'relationship';
326  }
327 
334  public function getSubtype() {
335  return $this->relationship;
336  }
337 }
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
export()
Export this relationship.
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:110
get_relationship($id)
Get a relationship by its ID.
elgg_normalize_url($url)
Definition: output.php:290
get_uuid_from_object($object)
Get a UUID from a given object.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
getSystemLogID()
Return an identification for the object for storage in the system log.
get_entity_from_uuid($uuid)
This function attempts to retrieve a previously imported entity via its UUID.
$data
Definition: opendd.php:13
__set($name, $value)
Set an attribute of the relationship.
$value
Definition: longtext.php:26
$subtype
Definition: delete.php:28
$url
Definition: exceptions.php:24
__get($name)
Get an attribute of the relationship.
getType()
Return a type of the object - eg.
$params
Definition: login.php:72
initializeAttributes()
(non-PHPdoc)
$key
Definition: summary.php:34
global $CONFIG
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
delete_relationship($id)
Delete a relationship by its ID.
__construct($row=null)
Create a relationship object.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
guid_to_uuid($guid)
Generate a UUID from a given GUID.
set($name, $value)
Class member set overloading.
getObjectFromID($id)
For a given ID, return the object associated with it.
Definition: ODD.php:9
getExportableValues()
Return an array of fields which can be exported.
$row
save()
Save the relationship.
if(!$collection_name) $id
Definition: add.php:17
getURL()
Get a URL for this relationship.
getSubtype()
Return a subtype.
if(!$display_name) $type
Definition: delete.php:27