123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- class ElggObject extends \ElggEntity {
-
- protected function initializeAttributes() {
- parent::initializeAttributes();
- $this->attributes['type'] = "object";
- $this->attributes += self::getExternalAttributes();
- $this->tables_split = 2;
- }
-
- final public static function getExternalAttributes() {
- return [
- 'title' => null,
- 'description' => null,
- ];
- }
-
- public function __construct($row = null) {
- $this->initializeAttributes();
-
- $this->initialise_attributes(false);
- if (!empty($row)) {
-
- if ($row instanceof \stdClass) {
-
- if (!$this->load($row)) {
- $msg = "Failed to load new " . get_class() . " for GUID: " . $row->guid;
- throw new \IOException($msg);
- }
- } else if ($row instanceof \ElggObject) {
-
- elgg_deprecated_notice('This type of usage of the \ElggObject constructor was deprecated. Please use the clone method.', 1.7);
- foreach ($row->attributes as $key => $value) {
- $this->attributes[$key] = $value;
- }
- } else if (is_numeric($row)) {
-
- elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
- if (!$this->load($row)) {
- throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
- }
- } else {
- throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
- }
- }
- }
-
- protected function load($guid) {
- $attr_loader = new \Elgg\AttributeLoader(get_class(), 'object', $this->attributes);
- $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
- $attr_loader->secondary_loader = 'get_object_entity_as_row';
- $attrs = $attr_loader->getRequiredAttributes($guid);
- if (!$attrs) {
- return false;
- }
- $this->attributes = $attrs;
- $this->tables_loaded = 2;
- $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
- _elgg_cache_entity($this);
- return true;
- }
-
- protected function create() {
- global $CONFIG;
- $guid = parent::create();
- if (!$guid) {
-
-
- return false;
- }
- $title = sanitize_string($this->title);
- $description = sanitize_string($this->description);
-
- $query = "INSERT into {$CONFIG->dbprefix}objects_entity
- (guid, title, description) values ($guid, '$title', '$description')";
- $result = $this->getDatabase()->insertData($query);
- if ($result === false) {
-
- return false;
- }
-
- return $guid;
- }
-
- protected function update() {
- global $CONFIG;
- if (!parent::update()) {
- return false;
- }
-
- $guid = (int)$this->guid;
- $title = sanitize_string($this->title);
- $description = sanitize_string($this->description);
- $query = "UPDATE {$CONFIG->dbprefix}objects_entity
- set title='$title', description='$description' where guid=$guid";
- return $this->getDatabase()->updateData($query) !== false;
- }
-
- public function getDisplayName() {
- return $this->title;
- }
-
- public function setDisplayName($displayName) {
- $this->title = $displayName;
- }
-
- public function getSites($options = "", $limit = 10, $offset = 0) {
- if (is_string($options)) {
- elgg_deprecated_notice('\ElggObject::getSites() takes an options array', 1.9);
- return get_site_objects($this->getGUID(), $options, $limit, $offset);
- }
- return parent::getSites();
- }
-
- public function addToSite($site) {
- if (is_numeric($site)) {
- elgg_deprecated_notice('\ElggObject::addToSite() takes a site entity', 1.9);
- return add_site_object($site, $this->getGUID());
- }
- return parent::addToSite($site);
- }
-
- protected function prepareObject($object) {
- $object = parent::prepareObject($object);
- $object->title = $this->getDisplayName();
- $object->description = $this->description;
- $object->tags = $this->tags ? $this->tags : array();
- return $object;
- }
-
-
- public function getExportableValues() {
- return array_merge(parent::getExportableValues(), array(
- 'title',
- 'description',
- ));
- }
-
- public function canComment($user_guid = 0) {
- $result = parent::canComment($user_guid);
- if ($result !== null) {
- return $result;
- }
- if ($user_guid == 0) {
- $user_guid = _elgg_services()->session->getLoggedInUserGuid();
- }
-
- if (!$user_guid) {
- return false;
- }
-
- if (elgg_instanceof($this->getContainerEntity(), 'group')) {
- if (!$this->getContainerEntity()->canWriteToContainer($user_guid)) {
- return false;
- }
- }
-
- return true;
- }
- }
|