Elgg  Version 1.11
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);
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 
59  protected function initializeAttributes() {
60  parent::initializeAttributes();
61 
62  $this->attributes['id'] = null;
63  $this->attributes['guid_one'] = null;
64  $this->attributes['relationship'] = null;
65  $this->attributes['guid_two'] = null;
66  }
67 
75  public function __set($name, $value) {
76  $this->attributes[$name] = $value;
77  }
78 
87  public function set($name, $value) {
88  elgg_deprecated_notice("Use -> instead of set()", 1.9);
89  $this->__set($name, $value);
90  return true;
91  }
92 
99  public function __get($name) {
100  if (array_key_exists($name, $this->attributes)) {
101  return $this->attributes[$name];
102  }
103 
104  return null;
105  }
106 
114  public function get($name) {
115  elgg_deprecated_notice("Use -> instead of get()", 1.9);
116  return $this->__get($name);
117  }
118 
125  public function save() {
126  if ($this->id > 0) {
127  delete_relationship($this->id);
128  }
129 
130  $this->id = add_entity_relationship($this->guid_one, $this->relationship, $this->guid_two);
131  if (!$this->id) {
132  throw new \IOException("Unable to save new " . get_class());
133  }
134 
135  return $this->id;
136  }
137 
143  public function delete() {
144  return delete_relationship($this->id);
145  }
146 
155  public function getURL() {
156  $url = '';
157  // @todo remove when elgg_register_relationship_url_handler() has been removed
158  if ($this->id) {
159  global $CONFIG;
160 
161  $subtype = $this->getSubtype();
162 
163  $function = "";
164  if (isset($CONFIG->relationship_url_handler[$subtype])) {
165  $function = $CONFIG->relationship_url_handler[$subtype];
166  }
167  if (isset($CONFIG->relationship_url_handler['all'])) {
168  $function = $CONFIG->relationship_url_handler['all'];
169  }
170 
171  if (is_callable($function)) {
172  $url = call_user_func($function, $this);
173  }
174 
175  if ($url) {
177  }
178  }
179 
180  $type = $this->getType();
181  $params = array('relationship' => $this);
182  $url = _elgg_services()->hooks->trigger('relationship:url', $type, $params, $url);
183 
184  return elgg_normalize_url($url);
185  }
186 
190  public function toObject() {
191  $object = new \stdClass();
192  $object->id = $this->id;
193  $object->subject_guid = $this->guid_one;
194  $object->relationship = $this->relationship;
195  $object->object_guid = $this->guid_two;
196  $object->time_created = date('c', $this->getTimeCreated());
197  $params = array('relationship' => $this);
198  return _elgg_services()->hooks->trigger('to:object', 'relationship', $params, $object);
199  }
200 
201  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
202 
209  public function getExportableValues() {
210  elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
211  return array(
212  'id',
213  'guid_one',
214  'relationship',
215  'guid_two'
216  );
217  }
218 
225  public function export() {
226  elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
227  $uuid = get_uuid_from_object($this);
228  $relationship = new ODDRelationship(
229  guid_to_uuid($this->guid_one),
230  $this->relationship,
231  guid_to_uuid($this->guid_two)
232  );
233 
234  $relationship->setAttribute('uuid', $uuid);
235 
236  return $relationship;
237  }
238 
239  // IMPORTABLE INTERFACE ////////////////////////////////////////////////////////////
240 
250  public function import(ODD $data) {
251  elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
252  if (!($data instanceof ODDRelationship)) {
253  throw new \InvalidParameterException("import() passed an unexpected ODD class");
254  }
255 
256  $uuid_one = $data->getAttribute('uuid1');
257  $uuid_two = $data->getAttribute('uuid2');
258 
259  // See if this entity has already been imported, if so then we need to link to it
260  $entity1 = get_entity_from_uuid($uuid_one);
261  $entity2 = get_entity_from_uuid($uuid_two);
262  if (($entity1) && ($entity2)) {
263  // Set the item ID
264  $this->attributes['guid_one'] = $entity1->getGUID();
265  $this->attributes['guid_two'] = $entity2->getGUID();
266 
267  // Map verb to relationship
268  //$verb = $data->getAttribute('verb');
269  //$relationship = get_relationship_from_verb($verb);
270  $relationship = $data->getAttribute('type');
271 
272  if ($relationship) {
273  $this->attributes['relationship'] = $relationship;
274  // save
275  $result = $this->save();
276  if (!$result) {
277  throw new \ImportException("There was a problem saving " . get_class());
278  }
279 
280  return true;
281  }
282  }
283 
284  return false;
285  }
286 
287  // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
288 
295  public function getSystemLogID() {
296  return $this->id;
297  }
298 
308  public function getObjectFromID($id) {
309  return get_relationship($id);
310  }
311 
317  public function getType() {
318  return 'relationship';
319  }
320 
327  public function getSubtype() {
328  return $this->relationship;
329  }
330 }
export()
Export this relationship.
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:131
get_relationship($id)
Get a relationship by its ID.
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
elgg_normalize_url($url)
Definition: output.php:311
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
$object
Definition: upgrade.php:12
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
$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
_elgg_services()
Definition: autoloader.php:14
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:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
delete_relationship($id)
Delete a relationship by its ID.
__construct($row=null)
Create a relationship object.
guid_to_uuid($guid)
Generate a UUID from a given GUID.
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
_elgg_get_relationship_row($id)
Get a database row from the relationship table.
save()
Save the relationship.
$subtype
Definition: river.php:12
if(!$collection_name) $id
Definition: add.php:17
getURL()
Get a URL for this relationship.
getSubtype()
Return a subtype.