Elgg  Version master
ElggRiverItem.php
Go to the documentation of this file.
1 <?php
2 
3 use Elgg\Exceptions\RuntimeException as ElggRuntimeException;
4 
19 
23  protected const INTEGER_ATTR_NAMES = [
24  'id',
25  'subject_guid',
26  'object_guid',
27  'target_guid',
28  'annotation_id',
29  'access_id',
30  'posted',
31  'last_action',
32  ];
33 
34  protected array $attributes = [];
35 
41  public function __construct(?\stdClass $row = null) {
42  $this->initializeAttributes();
43 
44  if (empty($row)) {
45  return;
46  }
47 
48  // build from database
49  foreach ($row as $key => $value) {
50  if (!array_key_exists($key, $this->attributes)) {
51  continue;
52  }
53 
54  if (in_array($key, static::INTEGER_ATTR_NAMES)) {
55  $value = (int) $value;
56  }
57 
58  $this->attributes[$key] = $value;
59  }
60  }
61 
67  public function __set(string $name, $value) {
68  if (!array_key_exists($name, $this->attributes)) {
69  throw new ElggRuntimeException("It's not allowed to set {$name} on " . get_class($this));
70  }
71 
72  if (in_array($name, static::INTEGER_ATTR_NAMES)) {
73  $value = (int) $value;
74  }
75 
76  $this->attributes[$name] = $value;
77  }
78 
82  public function __get($name) {
83  switch ($name) {
84  case 'type':
85  case 'subtype':
86  $object = $this->getObjectEntity();
87  if ($object) {
88  return $object->$name;
89  }
90  break;
91  default:
92  if (array_key_exists($name, $this->attributes)) {
93  return $this->attributes[$name];
94  }
95  break;
96  }
97  }
98 
102  public function __isset($name) : bool {
103  return isset($this->attributes[$name]);
104  }
105 
109  public function __unset($name): void {
110  if (!array_key_exists($name, $this->attributes)) {
111  return;
112  }
113 
114  $this->attributes[$name] = null;
115  }
116 
122  protected function initializeAttributes(): void {
123  $this->attributes['id'] = null;
124  $this->attributes['action_type'] = null;
125  $this->attributes['view'] = null;
126  $this->attributes['subject_guid'] = null;
127  $this->attributes['object_guid'] = null;
128  $this->attributes['target_guid'] = null;
129  $this->attributes['annotation_id'] = null;
130  $this->attributes['posted'] = null;
131  $this->attributes['last_action'] = null;
132  }
133 
139  public function getSubjectEntity(): ?\ElggEntity {
140  return $this->subject_guid ? get_entity($this->subject_guid) : null;
141  }
142 
148  public function getObjectEntity(): ?\ElggEntity {
149  return $this->object_guid ? get_entity($this->object_guid) : null;
150  }
151 
157  public function getTargetEntity(): ?\ElggEntity {
158  return $this->target_guid ? get_entity($this->target_guid) : null;
159  }
160 
166  public function getAnnotation(): ?\ElggAnnotation {
167  return $this->annotation_id ? elgg_get_annotation_from_id($this->annotation_id) : null;
168  }
169 
175  public function getView(): string {
176  return (string) $this->view;
177  }
178 
184  public function getTimePosted(): int {
185  return (int) $this->posted;
186  }
187 
195  public function updateLastAction(?int $last_action = null): int {
196  $this->last_action = _elgg_services()->riverTable->updateLastAction($this, $last_action);
197 
198  return $this->last_action;
199  }
200 
209  public function getType(): string {
210  return 'river';
211  }
212 
220  public function getSubtype(): string {
221  return 'item';
222  }
223 
234  public function canDelete(int $user_guid = 0): bool {
235  return _elgg_services()->userCapabilities->canDeleteRiverItem($this, $user_guid);
236  }
237 
244  public function delete(): bool {
245  if (!$this->canDelete()) {
246  return false;
247  }
248 
249  return _elgg_services()->riverTable->delete($this);
250  }
251 
257  public function toObject(): \stdClass {
258  $object = new \stdClass();
259  $object->id = $this->id;
260  $object->subject_guid = $this->subject_guid;
261  $object->target_guid = $this->target_guid;
262  $object->object_guid = $this->object_guid;
263  $object->annotation_id = $this->annotation_id;
264  $object->action = $this->action_type;
265  $object->view = $this->view;
266  $object->time_posted = date('c', $this->getTimePosted());
267  $object->last_action = date('c', $this->last_action);
268 
269  $params = ['item' => $this];
270  return _elgg_services()->events->triggerResults('to:object', 'river_item', $params, $object);
271  }
272 
278  public function save(): bool {
279  if ($this->id) {
280  // update (not supported)
281  return true;
282  }
283 
284  return (bool) _elgg_services()->riverTable->create($this);
285  }
286 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
$id
Generic annotation delete action.
Definition: delete.php:6
if(!empty($avatar) &&! $avatar->isValid()) elseif(empty($avatar)) if(! $owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
Definition: upload.php:39
$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
elgg_get_annotation_from_id(int $id)
Elgg annotations Functions to manage object annotations.
Definition: annotations.php:16
Entity Annotation.
getType()
Get the type of the object.
getTimePosted()
Get the time this activity was posted.
initializeAttributes()
Initialize the attributes array.
__unset($name)
{}
getSubjectEntity()
Get the subject of this river item.
getSubtype()
Get the subtype of the object.
getTargetEntity()
Get the target of this river item.
__isset($name)
{}
canDelete(int $user_guid=0)
Can a user delete this river item?
save()
Save the river item to the database.
__set(string $name, $value)
{}
getView()
Get the view used to display this river item.
const INTEGER_ATTR_NAMES
toObject()
Get a plain old object copy for public consumption.
__construct(?\stdClass $row=null)
Construct a river item object.
getAnnotation()
Get the Annotation for this river item.
getObjectEntity()
Get the object of this river item.
__get($name)
{}
updateLastAction(?int $last_action=null)
Update the last_action column in the river table.
Exception thrown if an error which can only be found on runtime occurs.
if($email instanceof \Elgg\Email) $object
Definition: body.php:24
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
$value
Definition: generic.php:51
$target_guid
Definition: groups.php:21
$user_guid
Definition: login_as.php:10
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