Elgg  Version 4.x
ElggExtender.php
Go to the documentation of this file.
1 <?php
24 abstract class ElggExtender extends \ElggData {
25 
29  protected const INTEGER_ATTR_NAMES = [
30  'id',
31  'entity_guid',
32  'owner_guid',
33  'access_id',
34  'time_created',
35  ];
36 
40  protected function initializeAttributes() {
41  parent::initializeAttributes();
42 
43  $this->attributes['type'] = null;
44  $this->attributes['id'] = null;
45  $this->attributes['entity_guid'] = null;
46  $this->attributes['owner_guid'] = null;
47  $this->attributes['access_id'] = ACCESS_PRIVATE;
48  $this->attributes['enabled'] = 'yes';
49  }
50 
58  public function __set($name, $value) {
59  if ($name === 'access_id' && $this instanceof ElggMetadata) {
61  }
62  if (isset($value) && in_array($name, static::INTEGER_ATTR_NAMES)) {
63  $value = (int) $value;
64  }
65  $this->attributes[$name] = $value;
66  if ($name == 'value') {
67  $this->attributes['value_type'] = self::detectValueType($value);
68  }
69  }
70 
79  public function setValue($value, $value_type = '') {
80  $this->attributes['value'] = $value;
81  $this->attributes['value_type'] = self::detectValueType($value, $value_type);
82  }
83 
90  public function __get($name) {
91  if (array_key_exists($name, $this->attributes)) {
92  if ($name == 'value') {
93  switch ($this->attributes['value_type']) {
94  case 'bool' :
95  return (bool) $this->attributes['value'];
96  case 'integer' :
97  return (int) $this->attributes['value'];
98  case 'text' :
99  return $this->attributes['value'];
100  default :
101  $msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
102  throw new \UnexpectedValueException($msg);
103  break;
104  }
105  }
106 
107  if ($name === 'access_id' && $this instanceof ElggMetadata) {
108  return ACCESS_PUBLIC;
109  }
110 
111  return $this->attributes[$name];
112  }
113 
114  return null;
115  }
116 
122  public function getOwnerGUID() {
123  return $this->owner_guid;
124  }
125 
131  public function getOwnerEntity() {
132  return get_entity($this->owner_guid);
133  }
134 
140  public function getEntity() {
141  return get_entity($this->entity_guid);
142  }
143 
152  abstract public function canEdit($user_guid = 0);
153 
157  public function toObject(array $params = []) {
158 
159  $object = new \Elgg\Export\Extender();
160  $object->id = $this->id;
161  $object->entity_guid = $this->entity_guid;
162  $object->owner_guid = $this->owner_guid;
163  $object->name = $this->name;
164  $object->value = $this->value;
165  $object->time_created = date('c', $this->getTimeCreated());
166  $object->read_access = $this->access_id;
167 
168  $params[$this->getType()] = $this;
169 
170  return _elgg_services()->hooks->trigger('to:object', $this->getType(), $params, $object);
171  }
172 
173  /*
174  * SYSTEM LOG INTERFACE
175  */
176 
183  public function getSystemLogID() {
184  return $this->id;
185  }
186 
192  public function getType() {
193  return $this->type;
194  }
195 
202  public function getSubtype() {
203  return $this->name;
204  }
205 
214  public function getURL() {
215 
216  $params = ['extender' => $this];
217  $url = _elgg_services()->hooks->trigger('extender:url', $this->getType(), $params, '');
218 
219  return elgg_normalize_url($url);
220  }
221 
231  public static function detectValueType($value, string $value_type = ''): string {
232  if (in_array($value_type, ['integer', 'text', 'bool'])) {
233  return $value_type;
234  }
235 
236  if (is_int($value)) {
237  return 'integer';
238  } elseif (is_bool($value)) {
239  return 'bool';
240  }
241 
242  return 'text';
243  }
244 }
getType()
Return a type of extension.
$user_guid
Definition: login_as.php:10
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:100
initializeAttributes()
{}
$params
Saves global plugin settings.
Definition: save.php:13
getEntity()
Get the entity this describes.
elgg_normalize_url($url)
Definition: output.php:152
__get($name)
Gets an attribute.
canEdit($user_guid=0)
Returns if a user can edit this entity extender.
$access_id
Definition: access.php:11
const INTEGER_ATTR_NAMES
$type
Definition: delete.php:21
__set($name, $value)
Set an attribute.
$value
Definition: generic.php:51
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
$owner_guid
const ACCESS_PRIVATE
Definition: constants.php:12
static detectValueType($value, string $value_type= '')
Detect the value_type for a value to be stored as metadata or an annotation.
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
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
getSystemLogID()
Return an identification for the object for storage in the system log.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
const ACCESS_PUBLIC
Definition: constants.php:14
$id
Generic annotation delete action.
Definition: delete.php:6
getSubtype()
Return a subtype.
setValue($value, $value_type= '')
Set the value of the extender.
getURL()
Get a url for this extender.
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:69