Elgg  Version master
ElggRiverItem.php
Go to the documentation of this file.
1 <?php
2 
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 }
getType()
Get the type of the object.
$user_guid
Definition: login_as.php:10
$params
Saves global plugin settings.
Definition: save.php:13
initializeAttributes()
Initialize the attributes array.
$posted
Definition: sidebar.php:21
toObject()
Get a plain old object copy for public consumption.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getSubjectEntity()
Get the subject of this river item.
const INTEGER_ATTR_NAMES
updateLastAction(int $last_action=null)
Update the last_action column in the river table.
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
Entity Annotation.
$target_guid
Definition: groups.php:21
getTimePosted()
Get the time this activity was posted.
__get($name)
{}
$value
Definition: generic.php:51
getSubtype()
Get the subtype of the object.
save()
Save the river item to the database.
canDelete(int $user_guid=0)
Can a user delete this river item?
__isset($name)
{}
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
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
getAnnotation()
Get the Annotation for this river item.
__unset($name)
{}
__construct(\stdClass $row=null)
Construct a river item object.
__set(string $name, $value)
{}
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
elgg_get_annotation_from_id(int $id)
Elgg annotations Functions to manage object annotations.
Definition: annotations.php:16
getView()
Get the view used to display this river item.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
$id
Generic annotation delete action.
Definition: delete.php:6
getTargetEntity()
Get the target of this river item.
getObjectEntity()
Get the object of this river item.