ElggObject.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Elgg Object
  4. *
  5. * Elgg objects are the most common means of storing information in the database.
  6. * They are a child class of \ElggEntity, so receive all the benefits of the Entities,
  7. * but also include a title and description field.
  8. *
  9. * An \ElggObject represents a row from the objects_entity table, as well
  10. * as the related row in the entities table as represented by the parent
  11. * \ElggEntity object.
  12. *
  13. * @note Internal: Title and description are stored in the objects_entity table.
  14. *
  15. * @package Elgg.Core
  16. * @subpackage DataModel.Object
  17. *
  18. * @property string $title The title, name, or summary of this object
  19. * @property string $description The body, description, or content of the object
  20. * @property array $tags Tags that describe the object (metadata)
  21. */
  22. class ElggObject extends \ElggEntity {
  23. /**
  24. * Initialize the attributes array to include the type,
  25. * title, and description.
  26. *
  27. * @return void
  28. */
  29. protected function initializeAttributes() {
  30. parent::initializeAttributes();
  31. $this->attributes['type'] = "object";
  32. $this->attributes += self::getExternalAttributes();
  33. $this->tables_split = 2;
  34. }
  35. /**
  36. * Get default values for attributes stored in a separate table
  37. *
  38. * @return array
  39. * @access private
  40. *
  41. * @see \Elgg\Database\EntityTable::getEntities
  42. */
  43. final public static function getExternalAttributes() {
  44. return [
  45. 'title' => null,
  46. 'description' => null,
  47. ];
  48. }
  49. /**
  50. * Create a new \ElggObject.
  51. *
  52. * Plugin developers should only use the constructor to create a new entity.
  53. * To retrieve entities, use get_entity() and the elgg_get_entities* functions.
  54. *
  55. * If no arguments are passed, it creates a new entity.
  56. * If a database result is passed as a \stdClass instance, it instantiates
  57. * that entity.
  58. *
  59. * @param \stdClass $row Database row result. Default is null to create a new object.
  60. *
  61. * @throws IOException If cannot load remaining data from db
  62. * @throws InvalidParameterException If not passed a db row result
  63. */
  64. public function __construct($row = null) {
  65. $this->initializeAttributes();
  66. // compatibility for 1.7 api.
  67. $this->initialise_attributes(false);
  68. if (!empty($row)) {
  69. // Is $row is a DB row from the entity table
  70. if ($row instanceof \stdClass) {
  71. // Load the rest
  72. if (!$this->load($row)) {
  73. $msg = "Failed to load new " . get_class() . " for GUID: " . $row->guid;
  74. throw new \IOException($msg);
  75. }
  76. } else if ($row instanceof \ElggObject) {
  77. // $row is an \ElggObject so this is a copy constructor
  78. elgg_deprecated_notice('This type of usage of the \ElggObject constructor was deprecated. Please use the clone method.', 1.7);
  79. foreach ($row->attributes as $key => $value) {
  80. $this->attributes[$key] = $value;
  81. }
  82. } else if (is_numeric($row)) {
  83. // $row is a GUID so load
  84. elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
  85. if (!$this->load($row)) {
  86. throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
  87. }
  88. } else {
  89. throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
  90. }
  91. }
  92. }
  93. /**
  94. * Loads the full \ElggObject when given a guid.
  95. *
  96. * @param mixed $guid GUID of an \ElggObject or the \stdClass object from entities table
  97. *
  98. * @return bool
  99. * @throws InvalidClassException
  100. */
  101. protected function load($guid) {
  102. $attr_loader = new \Elgg\AttributeLoader(get_class(), 'object', $this->attributes);
  103. $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
  104. $attr_loader->secondary_loader = 'get_object_entity_as_row';
  105. $attrs = $attr_loader->getRequiredAttributes($guid);
  106. if (!$attrs) {
  107. return false;
  108. }
  109. $this->attributes = $attrs;
  110. $this->tables_loaded = 2;
  111. $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
  112. _elgg_cache_entity($this);
  113. return true;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. protected function create() {
  119. global $CONFIG;
  120. $guid = parent::create();
  121. if (!$guid) {
  122. // @todo this probably means permission to create entity was denied
  123. // Is returning false the correct thing to do
  124. return false;
  125. }
  126. $title = sanitize_string($this->title);
  127. $description = sanitize_string($this->description);
  128. $query = "INSERT into {$CONFIG->dbprefix}objects_entity
  129. (guid, title, description) values ($guid, '$title', '$description')";
  130. $result = $this->getDatabase()->insertData($query);
  131. if ($result === false) {
  132. // TODO(evan): Throw an exception here?
  133. return false;
  134. }
  135. return $guid;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. protected function update() {
  141. global $CONFIG;
  142. if (!parent::update()) {
  143. return false;
  144. }
  145. $guid = (int)$this->guid;
  146. $title = sanitize_string($this->title);
  147. $description = sanitize_string($this->description);
  148. $query = "UPDATE {$CONFIG->dbprefix}objects_entity
  149. set title='$title', description='$description' where guid=$guid";
  150. return $this->getDatabase()->updateData($query) !== false;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getDisplayName() {
  156. return $this->title;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function setDisplayName($displayName) {
  162. $this->title = $displayName;
  163. }
  164. /**
  165. * Return sites that this object is a member of
  166. *
  167. * Site membership is determined by relationships and not site_guid.
  168. *
  169. * @todo Moved to \ElggEntity so remove this in 2.0
  170. *
  171. * @param array $options Options array. Used to be $subtype
  172. * @param int $limit The number of results to return (deprecated)
  173. * @param int $offset Any indexing offset (deprecated)
  174. *
  175. * @return array
  176. */
  177. public function getSites($options = "", $limit = 10, $offset = 0) {
  178. if (is_string($options)) {
  179. elgg_deprecated_notice('\ElggObject::getSites() takes an options array', 1.9);
  180. return get_site_objects($this->getGUID(), $options, $limit, $offset);
  181. }
  182. return parent::getSites();
  183. }
  184. /**
  185. * Add this object to a site
  186. *
  187. * @param \ElggSite $site The site to add this object to. This used to be the
  188. * the site guid (still supported by deprecated)
  189. * @return bool
  190. */
  191. public function addToSite($site) {
  192. if (is_numeric($site)) {
  193. elgg_deprecated_notice('\ElggObject::addToSite() takes a site entity', 1.9);
  194. return add_site_object($site, $this->getGUID());
  195. }
  196. return parent::addToSite($site);
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. protected function prepareObject($object) {
  202. $object = parent::prepareObject($object);
  203. $object->title = $this->getDisplayName();
  204. $object->description = $this->description;
  205. $object->tags = $this->tags ? $this->tags : array();
  206. return $object;
  207. }
  208. /*
  209. * EXPORTABLE INTERFACE
  210. */
  211. /**
  212. * Return an array of fields which can be exported.
  213. *
  214. * @return array
  215. * @deprecated 1.9 Use toObject()
  216. */
  217. public function getExportableValues() {
  218. return array_merge(parent::getExportableValues(), array(
  219. 'title',
  220. 'description',
  221. ));
  222. }
  223. /**
  224. * Can a user comment on this object?
  225. *
  226. * @see \ElggEntity::canComment()
  227. *
  228. * @param int $user_guid User guid (default is logged in user)
  229. * @return bool
  230. * @since 1.8.0
  231. */
  232. public function canComment($user_guid = 0) {
  233. $result = parent::canComment($user_guid);
  234. if ($result !== null) {
  235. return $result;
  236. }
  237. if ($user_guid == 0) {
  238. $user_guid = _elgg_services()->session->getLoggedInUserGuid();
  239. }
  240. // must be logged in to comment
  241. if (!$user_guid) {
  242. return false;
  243. }
  244. // must be member of group
  245. if (elgg_instanceof($this->getContainerEntity(), 'group')) {
  246. if (!$this->getContainerEntity()->canWriteToContainer($user_guid)) {
  247. return false;
  248. }
  249. }
  250. // no checks on read access since a user cannot see entities outside his access
  251. return true;
  252. }
  253. }