Elgg  Version 6.2
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 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
$id
Generic annotation delete action.
Definition: delete.php:6
$params
Saves global plugin settings.
Definition: save.php:13
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
A generic class that contains shared code among \ElggExtender, \ElggEntity, and \ElggRelationship.
Definition: ElggData.php:12
__get(string $name)
Get an attribute of the relationship.
getURL()
Get a URL for this relationship.
__set(string $name, mixed $value)
Set an attribute of the relationship.
__construct(?\stdClass $row=null)
Create a relationship object.
getObjectFromID(int $id)
For a given ID, return the object associated with it.
getSubtype()
Return a subtype.
getSystemLogID()
{Return an identification for the object for storage in the system log.This id must be an integer....
initializeAttributes()
{Initialize the attributes array.This is vital to distinguish between metadata and base parameters....
getOriginalAttributes()
Get the original values of attribute(s) that have been modified since the relationship was persisted.
save()
{Save this data to the appropriate database table.bool}
getType()
Return a type of the object - eg.
toObject(array $params=[])
{Get a plain old object copy for public consumption.Export parameters\Elgg\Export\Data}
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
if($email instanceof \Elgg\Email) $object
Definition: body.php:24
$relationship
Elgg default relationship view.
Definition: default.php:10
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
$value
Definition: generic.php:51
elgg_normalize_url(string $url)
Definition: output.php:163
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10