Elgg  Version 4.x
ElggRelationship.php
Go to the documentation of this file.
1 <?php
2 
12 class ElggRelationship extends \ElggData {
13 
17  public const PRIMARY_ATTR_NAMES = [
18  'id',
19  'guid_one',
20  'relationship',
21  'guid_two',
22  'time_created',
23  ];
24 
28  protected const INTEGER_ATTR_NAMES = [
29  'guid_one',
30  'guid_two',
31  'time_created',
32  'id',
33  ];
34 
39  protected $orig_attributes = [];
40 
46  public function __construct(\stdClass $row) {
47  $this->initializeAttributes();
48 
49  foreach ((array) $row as $key => $value) {
50  if (!in_array($key, static::PRIMARY_ATTR_NAMES)) {
51  // don't set arbitrary attributes that aren't supported
52  continue;
53  }
54 
55  if (in_array($key, static::INTEGER_ATTR_NAMES)) {
56  $value = (int) $value;
57  }
58 
59  $this->attributes[$key] = $value;
60  }
61  }
62 
70  protected function initializeAttributes() {
71  parent::initializeAttributes();
72 
73  $this->attributes['id'] = null;
74  $this->attributes['guid_one'] = null;
75  $this->attributes['relationship'] = null;
76  $this->attributes['guid_two'] = null;
77  }
78 
86  public function __set($name, $value) {
87  if (in_array($name, static::INTEGER_ATTR_NAMES) && isset($value) && !is_int($value)) {
88  // make sure the new value is an int for the int columns
89  $value = (int) $value;
90  }
91 
92  if ($this->$name === $value) {
93  // nothing changed
94  return;
95  }
96 
97  if (!array_key_exists($name, $this->attributes)) {
98  // only support setting attributes
99  return;
100  }
101 
102  if (in_array($name, ['id', 'time_created'])) {
103  // these attributes can't be changed by the user
104  return;
105  }
106 
107  if ($this->id > 0 && !array_key_exists($name, $this->orig_attributes)) {
108  // store original attribute
109  $this->orig_attributes[$name] = $this->attributes[$name];
110  }
111 
112  $this->attributes[$name] = $value;
113  }
114 
121  public function __get($name) {
122  if (array_key_exists($name, $this->attributes)) {
123  return $this->attributes[$name];
124  }
125 
126  return null;
127  }
128 
132  public function save() : bool {
133  if (empty($this->orig_attributes)) {
134  // nothing has changed
135  return true;
136  }
137 
138  if ($this->id > 0) {
139  _elgg_services()->relationshipsTable->delete($this->id);
140  }
141 
142  $id = _elgg_services()->relationshipsTable->add(
143  $this->guid_one,
144  $this->relationship,
145  $this->guid_two,
146  true
147  );
148 
149  if ($id === false) {
150  return false;
151  }
152 
153  $this->attributes['id'] = $id;
154 
155  return true;
156  }
157 
163  public function delete() {
164  return _elgg_services()->relationshipsTable->delete($this->id);
165  }
166 
175  public function getURL() {
176  $url = _elgg_services()->hooks->trigger('relationship:url', $this->getType(), ['relationship' => $this], '');
177 
178  return elgg_normalize_url($url);
179  }
180 
184  public function toObject(array $params = []) {
185  $object = new \Elgg\Export\Relationship();
186  $object->id = $this->id;
187  $object->subject_guid = $this->guid_one;
188  $object->relationship = $this->relationship;
189  $object->object_guid = $this->guid_two;
190  $object->time_created = date('c', $this->getTimeCreated());
191 
192  $params['relationship'] = $this;
193 
194  return _elgg_services()->hooks->trigger('to:object', 'relationship', $params, $object);
195  }
196 
197  // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
198 
205  public function getSystemLogID() {
206  return $this->id;
207  }
208 
218  public function getObjectFromID($id) {
219  return _elgg_services()->relationshipsTable->get($id);
220  }
221 
227  public function getType() {
228  return 'relationship';
229  }
230 
237  public function getSubtype() {
238  return $this->relationship;
239  }
240 
246  public function getOriginalAttributes() {
247  return $this->orig_attributes;
248  }
249 }
toObject(array $params=[])
{}
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:100
$params
Saves global plugin settings.
Definition: save.php:13
elgg_normalize_url($url)
Definition: output.php:152
getSystemLogID()
Return an identification for the object for storage in the system log.
__set($name, $value)
Set an attribute of the relationship.
$relationship
Elgg default relationship view.
Definition: default.php:10
$value
Definition: generic.php:51
__get($name)
Get an attribute of the relationship.
getType()
Return a type of the object - eg.
initializeAttributes()
(non-PHPdoc)
getOriginalAttributes()
Get the original values of attribute(s) that have been modified since the relationship was persisted...
A generic class that contains shared code among , , and .
Definition: ElggData.php:10
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
getObjectFromID($id)
For a given ID, return the object associated with it.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
$id
Generic annotation delete action.
Definition: delete.php:6
__construct(\stdClass $row)
Create a relationship object.
getURL()
Get a URL for this relationship.
getSubtype()
Return a subtype.