ElggExtender.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * The base class for \ElggEntity extenders.
  4. *
  5. * Extenders allow you to attach extended information to an
  6. * \ElggEntity. Core supports two: \ElggAnnotation and \ElggMetadata.
  7. *
  8. * Saving the extender data to database is handled by the child class.
  9. *
  10. * @package Elgg.Core
  11. * @subpackage DataModel.Extender
  12. * @see \ElggAnnotation
  13. * @see \ElggMetadata
  14. *
  15. * @property string $type annotation or metadata (read-only after save)
  16. * @property int $id The unique identifier (read-only)
  17. * @property int $entity_guid The GUID of the entity that this extender describes
  18. * @property int $owner_guid The GUID of the owner of this extender
  19. * @property int $access_id Specifies the visibility level of this extender
  20. * @property string $name The name of this extender
  21. * @property mixed $value The value of the extender (int or string)
  22. * @property int $time_created A UNIX timestamp of when the extender was created (read-only, set on first save)
  23. * @property string $value_type 'integer' or 'text'
  24. * @property string $enabled Is this extender enabled ('yes' or 'no')
  25. */
  26. abstract class ElggExtender extends \ElggData {
  27. /**
  28. * (non-PHPdoc)
  29. *
  30. * @see \ElggData::initializeAttributes()
  31. *
  32. * @return void
  33. */
  34. protected function initializeAttributes() {
  35. parent::initializeAttributes();
  36. $this->attributes['type'] = null;
  37. $this->attributes['id'] = null;
  38. $this->attributes['entity_guid'] = null;
  39. $this->attributes['owner_guid'] = null;
  40. $this->attributes['access_id'] = ACCESS_PRIVATE;
  41. $this->attributes['enabled'] = 'yes';
  42. }
  43. /**
  44. * Set an attribute
  45. *
  46. * @param string $name Name
  47. * @param mixed $value Value
  48. * @return void
  49. */
  50. public function __set($name, $value) {
  51. $this->attributes[$name] = $value;
  52. if ($name == 'value') {
  53. $this->attributes['value_type'] = detect_extender_valuetype($value);
  54. }
  55. }
  56. /**
  57. * Set the value of the extender
  58. *
  59. * @param mixed $value The value being set
  60. * @param string $value_type The type of the : 'integer' or 'text'
  61. * @return void
  62. * @since 1.9
  63. */
  64. public function setValue($value, $value_type = '') {
  65. $this->attributes['value'] = $value;
  66. $this->attributes['value_type'] = detect_extender_valuetype($value, $value_type);
  67. }
  68. /**
  69. * Set an attribute
  70. *
  71. * @param string $name Name
  72. * @param mixed $value Value
  73. * @param string $value_type Value type
  74. *
  75. * @return boolean
  76. * @deprecated 1.9
  77. */
  78. protected function set($name, $value, $value_type = '') {
  79. elgg_deprecated_notice("Use -> instead of set()", 1.9);
  80. if ($name == 'value') {
  81. $this->setValue($value, $value_type);
  82. } else {
  83. $this->__set($name, $value);
  84. }
  85. return true;
  86. }
  87. /**
  88. * Gets an attribute
  89. *
  90. * @param string $name Name
  91. * @return mixed
  92. */
  93. public function __get($name) {
  94. if (array_key_exists($name, $this->attributes)) {
  95. if ($name == 'value') {
  96. switch ($this->attributes['value_type']) {
  97. case 'integer' :
  98. return (int)$this->attributes['value'];
  99. break;
  100. case 'text' :
  101. return $this->attributes['value'];
  102. break;
  103. default :
  104. $msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
  105. throw new \UnexpectedValueException($msg);
  106. break;
  107. }
  108. }
  109. return $this->attributes[$name];
  110. }
  111. return null;
  112. }
  113. /**
  114. * Returns an attribute
  115. *
  116. * @param string $name Name
  117. * @return mixed
  118. * @deprecated 1.9
  119. */
  120. protected function get($name) {
  121. elgg_deprecated_notice("Use -> instead of get()", 1.9);
  122. return $this->__get($name);
  123. }
  124. /**
  125. * Get the GUID of the extender's owner entity.
  126. *
  127. * @return int The owner GUID
  128. */
  129. public function getOwnerGUID() {
  130. return $this->owner_guid;
  131. }
  132. /**
  133. * Return the guid of the entity's owner.
  134. *
  135. * @return int The owner GUID
  136. * @deprecated 1.8 Use getOwnerGUID
  137. */
  138. public function getOwner() {
  139. elgg_deprecated_notice("\ElggExtender::getOwner deprecated for \ElggExtender::getOwnerGUID", 1.8);
  140. return $this->getOwnerGUID();
  141. }
  142. /**
  143. * Get the entity that owns this extender
  144. *
  145. * @return \ElggEntity
  146. */
  147. public function getOwnerEntity() {
  148. return get_entity($this->owner_guid);
  149. }
  150. /**
  151. * Get the entity this describes.
  152. *
  153. * @return \ElggEntity The entity
  154. */
  155. public function getEntity() {
  156. return get_entity($this->entity_guid);
  157. }
  158. /**
  159. * Returns if a user can edit this entity extender.
  160. *
  161. * @param int $user_guid The GUID of the user doing the editing
  162. * (defaults to currently logged in user)
  163. *
  164. * @return bool
  165. * @see elgg_set_ignore_access()
  166. */
  167. abstract public function canEdit($user_guid = 0);
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function toObject() {
  172. $object = new \stdClass();
  173. $object->id = $this->id;
  174. $object->entity_guid = $this->entity_guid;
  175. $object->owner_guid = $this->owner_guid;
  176. $object->name = $this->name;
  177. $object->value = $this->value;
  178. $object->time_created = date('c', $this->getTimeCreated());
  179. $object->read_access = $this->access_id;
  180. $params = array($this->getSubtype() => $this);
  181. return _elgg_services()->hooks->trigger('to:object', $this->getSubtype(), $params, $object);
  182. }
  183. /*
  184. * EXPORTABLE INTERFACE
  185. */
  186. /**
  187. * Return an array of fields which can be exported.
  188. *
  189. * @return array
  190. * @deprecated 1.9 Use toObject()
  191. */
  192. public function getExportableValues() {
  193. elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
  194. return array(
  195. 'id',
  196. 'entity_guid',
  197. 'name',
  198. 'value',
  199. 'value_type',
  200. 'owner_guid',
  201. 'type',
  202. );
  203. }
  204. /**
  205. * Export this object
  206. *
  207. * @return array
  208. * @deprecated 1.9 Use toObject()
  209. */
  210. public function export() {
  211. elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
  212. $uuid = get_uuid_from_object($this);
  213. $meta = new ODDMetaData($uuid, guid_to_uuid($this->entity_guid), $this->attributes['name'],
  214. $this->attributes['value'], $this->attributes['type'], guid_to_uuid($this->owner_guid));
  215. $meta->setAttribute('published', date("r", $this->time_created));
  216. return $meta;
  217. }
  218. /*
  219. * SYSTEM LOG INTERFACE
  220. */
  221. /**
  222. * Return an identification for the object for storage in the system log.
  223. * This id must be an integer.
  224. *
  225. * @return int
  226. */
  227. public function getSystemLogID() {
  228. return $this->id;
  229. }
  230. /**
  231. * Return a type of extension.
  232. *
  233. * @return string
  234. */
  235. public function getType() {
  236. return $this->type;
  237. }
  238. /**
  239. * Return a subtype. For metadata & annotations this is the 'name' and
  240. * for relationship this is the relationship type.
  241. *
  242. * @return string
  243. */
  244. public function getSubtype() {
  245. return $this->name;
  246. }
  247. /**
  248. * Get a url for this extender.
  249. *
  250. * Plugins can register for the 'extender:url', <type> plugin hook to
  251. * customize the url for an annotation or metadata.
  252. *
  253. * @return string
  254. */
  255. public function getURL() {
  256. $url = "";
  257. $type = $this->getType();
  258. $subtype = $this->getSubtype();
  259. // @todo remove when elgg_register_extender_url_handler() has been removed
  260. if ($this->id) {
  261. global $CONFIG;
  262. $function = "";
  263. if (isset($CONFIG->extender_url_handler[$type][$subtype])) {
  264. $function = $CONFIG->extender_url_handler[$type][$subtype];
  265. }
  266. if (isset($CONFIG->extender_url_handler[$type]['all'])) {
  267. $function = $CONFIG->extender_url_handler[$type]['all'];
  268. }
  269. if (isset($CONFIG->extender_url_handler['all']['all'])) {
  270. $function = $CONFIG->extender_url_handler['all']['all'];
  271. }
  272. if (is_callable($function)) {
  273. $url = call_user_func($function, $this);
  274. }
  275. if ($url) {
  276. $url = elgg_normalize_url($url);
  277. }
  278. }
  279. $params = array('extender' => $this);
  280. $url = _elgg_services()->hooks->trigger('extender:url', $type, $params, $url);
  281. return elgg_normalize_url($url);
  282. }
  283. }