Elgg  Version 6.1
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 array $orig_attributes = [];
40 
46  public function __construct(\stdClass $row = null) {
47  $this->initializeAttributes();
48 
49  if (!empty($row)) {
50  foreach ((array) $row as $key => $value) {
51  if (!in_array($key, static::PRIMARY_ATTR_NAMES)) {
52  // don't set arbitrary attributes that aren't supported
53  continue;
54  }
55 
56  if (in_array($key, static::INTEGER_ATTR_NAMES)) {
57  $value = (int) $value;
58  }
59 
60  $this->attributes[$key] = $value;
61  }
62  }
63  }
64 
68  protected function initializeAttributes() {
69  parent::initializeAttributes();
70 
71  $this->attributes['id'] = null;
72  $this->attributes['guid_one'] = null;
73  $this->attributes['relationship'] = null;
74  $this->attributes['guid_two'] = null;
75  }
76 
85  public function __set(string $name, mixed $value): void {
86  if (in_array($name, static::INTEGER_ATTR_NAMES) && isset($value) && !is_int($value)) {
87  // make sure the new value is an int for the int columns
88  $value = (int) $value;
89  }
90 
91  if ($this->$name === $value) {
92  // nothing changed
93  return;
94  }
95 
96  if (!array_key_exists($name, $this->attributes)) {
97  // only support setting attributes
98  return;
99  }
100 
101  if (in_array($name, ['id', 'time_created'])) {
102  // these attributes can't be changed by the user
103  return;
104  }
105 
106  if ($this->id > 0 && !array_key_exists($name, $this->orig_attributes)) {
107  // store original attribute
108  $this->orig_attributes[$name] = $this->attributes[$name];
109  }
110 
111  $this->attributes[$name] = $value;
112  }
113 
121  public function __get(string $name): mixed {
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 ($this->id > 0 && 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($this, true);
143  if ($id === false) {
144  return false;
145  }
146 
147  $this->attributes['id'] = $id;
148  $this->attributes['time_created'] = _elgg_services()->relationshipsTable->getCurrentTime()->getTimestamp();
149 
150  return true;
151  }
152 
158  public function delete(): bool {
159  return _elgg_services()->relationshipsTable->delete($this->id);
160  }
161 
170  public function getURL(): string {
171  $url = _elgg_services()->events->triggerResults('relationship:url', $this->getType(), ['relationship' => $this], '');
172 
173  return elgg_normalize_url($url);
174  }
175 
179  public function toObject(array $params = []) {
180  $object = new \Elgg\Export\Relationship();
181  $object->id = $this->id;
182  $object->subject_guid = $this->guid_one;
183  $object->relationship = $this->relationship;
184  $object->object_guid = $this->guid_two;
185  $object->time_created = date('c', $this->getTimeCreated());
186 
187  $params['relationship'] = $this;
188 
189  return _elgg_services()->events->triggerResults('to:object', 'relationship', $params, $object);
190  }
191 
192  // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
193 
197  public function getSystemLogID(): int {
198  return (int) $this->id;
199  }
200 
210  public function getObjectFromID(int $id) {
211  return _elgg_services()->relationshipsTable->get($id);
212  }
213 
219  public function getType(): string {
220  return 'relationship';
221  }
222 
229  public function getSubtype(): string {
230  return $this->relationship;
231  }
232 
238  public function getOriginalAttributes(): array {
239  return $this->orig_attributes;
240  }
241 }
toObject(array $params=[])
{}
__set(string $name, mixed $value)
Set an attribute of the relationship.
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:99
$params
Saves global plugin settings.
Definition: save.php:13
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getObjectFromID(int $id)
For a given ID, return the object associated with it.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
$relationship
Elgg default relationship view.
Definition: default.php:10
$value
Definition: generic.php:51
getType()
Return a type of the object - eg.
getOriginalAttributes()
Get the original values of attribute(s) that have been modified since the relationship was persisted...
__get(string $name)
Get an attribute of the relationship.
A generic class that contains shared code among , , and .
Definition: ElggData.php:10
__construct(\stdClass $row=null)
Create a relationship object.
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
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_normalize_url(string $url)
Definition: output.php:163
$id
Generic annotation delete action.
Definition: delete.php:6
getURL()
Get a URL for this relationship.
getSubtype()
Return a subtype.