Elgg  Version 2.3
ElggExtender.php
Go to the documentation of this file.
1 <?php
26 abstract class ElggExtender extends \ElggData {
27 
35  protected function initializeAttributes() {
36  parent::initializeAttributes();
37 
38  $this->attributes['type'] = null;
39  $this->attributes['id'] = null;
40  $this->attributes['entity_guid'] = null;
41  $this->attributes['owner_guid'] = null;
42  $this->attributes['access_id'] = ACCESS_PRIVATE;
43  $this->attributes['enabled'] = 'yes';
44  }
45 
53  public function __set($name, $value) {
54  if ($name === 'access_id' && $this instanceof ElggMetadata && $value != ACCESS_PUBLIC) {
55  elgg_deprecated_notice('Setting ->access_id to a value other than ACCESS_PUBLIC is deprecated. '
56  . 'All metadata will be public in 3.0.', '2.3');
57  }
58 
59  $this->attributes[$name] = $value;
60  if ($name == 'value') {
61  $this->attributes['value_type'] = self::detectValueType($value);
62  }
63  }
64 
73  public function setValue($value, $value_type = '') {
74  $this->attributes['value'] = $value;
75  $this->attributes['value_type'] = self::detectValueType($value, $value_type);
76  }
77 
88  protected function set($name, $value, $value_type = '') {
89  elgg_deprecated_notice("Use -> instead of set()", 1.9);
90  if ($name == 'value') {
91  $this->setValue($value, $value_type);
92  } else {
93  $this->__set($name, $value);
94  }
95 
96  return true;
97  }
98 
105  public function __get($name) {
106  if (array_key_exists($name, $this->attributes)) {
107  if ($name == 'value') {
108  switch ($this->attributes['value_type']) {
109  case 'integer' :
110  return (int)$this->attributes['value'];
111  break;
112  case 'text' :
113  return $this->attributes['value'];
114  break;
115  default :
116  $msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
117  throw new \UnexpectedValueException($msg);
118  break;
119  }
120  }
121 
122  return $this->attributes[$name];
123  }
124 
125  return null;
126  }
127 
135  protected function get($name) {
136  elgg_deprecated_notice("Use -> instead of get()", 1.9);
137  return $this->__get($name);
138  }
139 
145  public function getOwnerGUID() {
146  return $this->owner_guid;
147  }
148 
154  public function getOwnerEntity() {
155  return get_entity($this->owner_guid);
156  }
157 
163  public function getEntity() {
164  return get_entity($this->entity_guid);
165  }
166 
176  abstract public function canEdit($user_guid = 0);
177 
181  public function toObject() {
182  $object = new \stdClass();
183  $object->id = $this->id;
184  $object->entity_guid = $this->entity_guid;
185  $object->owner_guid = $this->owner_guid;
186  $object->name = $this->name;
187  $object->value = $this->value;
188  $object->time_created = date('c', $this->getTimeCreated());
189  $object->read_access = $this->access_id;
190  $params = array(
191  $this->getSubtype() => $this, // deprecated use
192  $this->getType() => $this,
193  );
194  if (_elgg_services()->hooks->hasHandler('to:object', $this->getSubtype())) {
195  _elgg_services()->deprecation->sendNotice("Triggering 'to:object' hook by extender name '{$this->getSubtype()}' has been deprecated. "
196  . "Use the generic 'to:object','{$this->getType()}' hook instead.", '2.3');
197  $object = _elgg_services()->hooks->trigger('to:object', $this->getSubtype(), $params, $object);
198  }
199  return _elgg_services()->hooks->trigger('to:object', $this->getType(), $params, $object);
200  }
201 
202  /*
203  * EXPORTABLE INTERFACE
204  */
205 
212  public function getExportableValues() {
213  elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
214  return array(
215  'id',
216  'entity_guid',
217  'name',
218  'value',
219  'value_type',
220  'owner_guid',
221  'type',
222  );
223  }
224 
231  public function export() {
232  elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
233  $uuid = get_uuid_from_object($this);
234 
235  $meta = new ODDMetaData($uuid, guid_to_uuid($this->entity_guid), $this->attributes['name'],
236  $this->attributes['value'], $this->attributes['type'], guid_to_uuid($this->owner_guid));
237  $meta->setAttribute('published', date("r", $this->time_created));
238 
239  return $meta;
240  }
241 
242  /*
243  * SYSTEM LOG INTERFACE
244  */
245 
252  public function getSystemLogID() {
253  return $this->id;
254  }
255 
261  public function getType() {
262  return $this->type;
263  }
264 
271  public function getSubtype() {
272  return $this->name;
273  }
274 
283  public function getURL() {
284 
285  $url = "";
286  $type = $this->getType();
287  $subtype = $this->getSubtype();
288 
289  // @todo remove when elgg_register_extender_url_handler() has been removed
290  if ($this->id) {
291  global $CONFIG;
292 
293  $function = "";
294  if (isset($CONFIG->extender_url_handler[$type][$subtype])) {
295  $function = $CONFIG->extender_url_handler[$type][$subtype];
296  }
297  if (isset($CONFIG->extender_url_handler[$type]['all'])) {
298  $function = $CONFIG->extender_url_handler[$type]['all'];
299  }
300  if (isset($CONFIG->extender_url_handler['all']['all'])) {
301  $function = $CONFIG->extender_url_handler['all']['all'];
302  }
303  if (is_callable($function)) {
304  $url = call_user_func($function, $this);
305  }
306 
307  if ($url) {
309  }
310  }
311 
312  $params = array('extender' => $this);
313  $url = _elgg_services()->hooks->trigger('extender:url', $type, $params, $url);
314 
315  return elgg_normalize_url($url);
316  }
317 
328  public static function detectValueType($value, $value_type = "") {
329  if ($value_type === 'integer' || $value_type === 'text') {
330  return $value_type;
331  }
332 
333  return is_int($value) ? 'integer' : 'text';
334  }
335 }
getType()
Return a type of extension.
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:112
initializeAttributes()
(non-PHPdoc)
getEntity()
Get the entity this describes.
elgg_normalize_url($url)
Definition: output.php:280
get_uuid_from_object($object)
Get a UUID from a given object.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
__get($name)
Gets an attribute.
canEdit($user_guid=0)
Returns if a user can edit this entity extender.
$value
Definition: longtext.php:42
$subtype
Definition: delete.php:28
static detectValueType($value, $value_type="")
Detect the value_type for a value to be stored as metadata or an annotation.
__set($name, $value)
Set an attribute.
getExportableValues()
Return an array of fields which can be exported.
$url
Definition: exceptions.php:24
$params
Definition: login.php:72
$entity_guid
Definition: save.php:9
export()
Export this object.
$owner_guid
global $CONFIG
const ACCESS_PRIVATE
Definition: elgglib.php:2082
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
elgg global
Pointer to the global context.
Definition: elgglib.js:12
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
getOwnerEntity()
Get the entity that owns this extender.
guid_to_uuid($guid)
Generate a UUID from a given GUID.
getOwnerGUID()
Get the GUID of the extender&#39;s owner entity.
const ACCESS_PUBLIC
Definition: elgglib.php:2084
getSystemLogID()
Return an identification for the object for storage in the system log.
$user_guid
Avatar remove action.
Definition: remove.php:6
if(!$collection_name) $id
Definition: add.php:17
getSubtype()
Return a subtype.
setValue($value, $value_type= '')
Set the value of the extender.
getURL()
Get a url for this extender.
$comment owner_guid
Definition: save.php:58
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
if(!$display_name) $type
Definition: delete.php:27