Elgg  Version master
ElggExtender.php
Go to the documentation of this file.
1 <?php
2 
4 
27 abstract class ElggExtender extends \ElggData {
28 
32  protected const INTEGER_ATTR_NAMES = [
33  'id',
34  'entity_guid',
35  'owner_guid',
36  'access_id',
37  'time_created',
38  ];
39 
43  protected function initializeAttributes() {
44  parent::initializeAttributes();
45 
46  $this->attributes['type'] = null;
47  $this->attributes['id'] = null;
48  $this->attributes['entity_guid'] = null;
49  $this->attributes['owner_guid'] = null;
50  $this->attributes['access_id'] = ACCESS_PRIVATE;
51  $this->attributes['enabled'] = 'yes';
52  }
53 
61  public function __set($name, $value) {
62  if ($name === 'access_id' && $this instanceof ElggMetadata) {
64  }
65 
66  if (isset($value) && in_array($name, static::INTEGER_ATTR_NAMES)) {
67  $value = (int) $value;
68  }
69 
70  $this->attributes[$name] = $value;
71  if ($name == 'value') {
72  $this->attributes['value_type'] = self::detectValueType($value);
73  }
74  }
75 
84  public function setValue($value, string $value_type = ''): void {
85  $this->attributes['value'] = $value;
86  $this->attributes['value_type'] = self::detectValueType($value, $value_type);
87  }
88 
97  public function __get($name) {
98  if (array_key_exists($name, $this->attributes)) {
99  if ($name == 'value') {
100  switch ($this->attributes['value_type']) {
101  case 'bool':
102  return (bool) $this->attributes['value'];
103  case 'integer':
104  return (int) $this->attributes['value'];
105  case 'text':
106  return $this->attributes['value'];
107  default:
108  $msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
109  throw new ElggUnexpectedValueException($msg);
110  }
111  }
112 
113  if ($name === 'access_id' && $this instanceof ElggMetadata) {
114  return ACCESS_PUBLIC;
115  }
116 
117  return $this->attributes[$name];
118  }
119 
120  return null;
121  }
122 
128  public function getOwnerGUID(): int {
129  return $this->owner_guid;
130  }
131 
137  public function getOwnerEntity(): ?\ElggEntity {
138  return $this->owner_guid ? get_entity($this->owner_guid) : null;
139  }
140 
146  public function getEntity(): ?\ElggEntity {
147  return $this->entity_guid ? get_entity($this->entity_guid) : null;
148  }
149 
158  abstract public function canEdit(int $user_guid = 0): bool;
159 
163  public function toObject(array $params = []) {
164 
165  $object = new \Elgg\Export\Extender();
166  $object->id = $this->id;
167  $object->entity_guid = $this->entity_guid;
168  $object->owner_guid = $this->owner_guid;
169  $object->name = $this->name;
170  $object->value = $this->value;
171  $object->time_created = date('c', $this->getTimeCreated());
172  $object->read_access = $this->access_id;
173 
174  $params[$this->getType()] = $this;
175 
176  return _elgg_services()->events->triggerResults('to:object', $this->getType(), $params, $object);
177  }
178 
179  /*
180  * SYSTEM LOG INTERFACE
181  */
182 
186  public function getSystemLogID(): int {
187  return (int) $this->id;
188  }
189 
195  public function getType(): string {
196  return $this->type;
197  }
198 
205  public function getSubtype(): string {
206  return $this->name;
207  }
208 
217  public function getURL(): string {
218 
219  $params = ['extender' => $this];
220  $url = _elgg_services()->events->triggerResults('extender:url', $this->getType(), $params, '');
221 
222  return elgg_normalize_url($url);
223  }
224 
234  public static function detectValueType($value, string $value_type = ''): string {
235  if (in_array($value_type, ['integer', 'text', 'bool'])) {
236  return $value_type;
237  }
238 
239  if (is_int($value)) {
240  return 'integer';
241  } elseif (is_bool($value)) {
242  return 'bool';
243  }
244 
245  return 'text';
246  }
247 }
getType()
Return a type of extension.
$user_guid
Definition: login_as.php:10
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:99
initializeAttributes()
{}
$params
Saves global plugin settings.
Definition: save.php:13
getEntity()
Get the entity this describes.
__get($name)
Gets an attribute.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
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
const INTEGER_ATTR_NAMES
$type
Definition: delete.php:21
__set($name, $value)
Set an attribute.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
setValue($value, string $value_type= '')
Set the value of the extender.
$value
Definition: generic.php:51
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
$owner_guid
canEdit(int $user_guid=0)
Returns if a user can edit this entity extender.
const ACCESS_PRIVATE
Definition: constants.php:10
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
static detectValueType($value, string $value_type= '')
Detect the value_type for a value to be stored as metadata or an annotation.
ElggMetadata.
toObject(array $params=[])
{}
A generic class that contains shared code among , , and .
Definition: ElggData.php:10
getOwnerEntity()
Get the entity that owns this extender.
getOwnerGUID()
Get the GUID of the extender&#39;s owner entity.
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
$access_id
Definition: access.php:10
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
const ACCESS_PUBLIC
Definition: constants.php:12
elgg_normalize_url(string $url)
Definition: output.php:163
$id
Generic annotation delete action.
Definition: delete.php:6
getSubtype()
Return a subtype.
getURL()
Get a url for this extender.