Elgg  Version 1.11
ElggObject.php
Go to the documentation of this file.
1 <?php
22 class ElggObject extends \ElggEntity {
23 
30  protected function initializeAttributes() {
31  parent::initializeAttributes();
32 
33  $this->attributes['type'] = "object";
34  $this->attributes += self::getExternalAttributes();
35  $this->tables_split = 2;
36  }
37 
46  final public static function getExternalAttributes() {
47  return [
48  'title' => null,
49  'description' => null,
50  ];
51  }
52 
68  public function __construct($row = null) {
69  $this->initializeAttributes();
70 
71  // compatibility for 1.7 api.
72  $this->initialise_attributes(false);
73 
74  if (!empty($row)) {
75  // Is $row is a DB row from the entity table
76  if ($row instanceof \stdClass) {
77  // Load the rest
78  if (!$this->load($row)) {
79  $msg = "Failed to load new " . get_class() . " for GUID: " . $row->guid;
80  throw new \IOException($msg);
81  }
82  } else if ($row instanceof \ElggObject) {
83  // $row is an \ElggObject so this is a copy constructor
84  elgg_deprecated_notice('This type of usage of the \ElggObject constructor was deprecated. Please use the clone method.', 1.7);
85  foreach ($row->attributes as $key => $value) {
86  $this->attributes[$key] = $value;
87  }
88  } else if (is_numeric($row)) {
89  // $row is a GUID so load
90  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
91  if (!$this->load($row)) {
92  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
93  }
94  } else {
95  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
96  }
97  }
98  }
99 
108  protected function load($guid) {
109  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'object', $this->attributes);
110  $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
111  $attr_loader->secondary_loader = 'get_object_entity_as_row';
112 
113  $attrs = $attr_loader->getRequiredAttributes($guid);
114  if (!$attrs) {
115  return false;
116  }
117 
118  $this->attributes = $attrs;
119  $this->tables_loaded = 2;
120  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
121  _elgg_cache_entity($this);
122 
123  return true;
124  }
125 
129  protected function create() {
130  global $CONFIG;
131 
132  $guid = parent::create();
133  if (!$guid) {
134  // @todo this probably means permission to create entity was denied
135  // Is returning false the correct thing to do
136  return false;
137  }
138  $title = sanitize_string($this->title);
140 
141  $query = "INSERT into {$CONFIG->dbprefix}objects_entity
142  (guid, title, description) values ($guid, '$title', '$description')";
143 
144  $result = $this->getDatabase()->insertData($query);
145  if ($result === false) {
146  // TODO(evan): Throw an exception here?
147  return false;
148  }
149 
150  return $guid;
151  }
152 
156  protected function update() {
157  global $CONFIG;
158 
159  if (!parent::update()) {
160  return false;
161  }
162 
163  $guid = (int)$this->guid;
164  $title = sanitize_string($this->title);
166 
167  $query = "UPDATE {$CONFIG->dbprefix}objects_entity
168  set title='$title', description='$description' where guid=$guid";
169 
170  return $this->getDatabase()->updateData($query) !== false;
171  }
172 
176  public function getDisplayName() {
177  return $this->title;
178  }
179 
183  public function setDisplayName($displayName) {
184  $this->title = $displayName;
185  }
186 
200  public function getSites($options = "", $limit = 10, $offset = 0) {
201  if (is_string($options)) {
202  elgg_deprecated_notice('\ElggObject::getSites() takes an options array', 1.9);
203  return get_site_objects($this->getGUID(), $options, $limit, $offset);
204  }
205 
206  return parent::getSites();
207  }
208 
216  public function addToSite($site) {
217  if (is_numeric($site)) {
218  elgg_deprecated_notice('\ElggObject::addToSite() takes a site entity', 1.9);
219  return add_site_object($site, $this->getGUID());
220  }
221 
222  return parent::addToSite($site);
223  }
224 
228  protected function prepareObject($object) {
229  $object = parent::prepareObject($object);
230  $object->title = $this->getDisplayName();
231  $object->description = $this->description;
232  $object->tags = $this->tags ? $this->tags : array();
233  return $object;
234  }
235 
236  /*
237  * EXPORTABLE INTERFACE
238  */
239 
246  public function getExportableValues() {
247  return array_merge(parent::getExportableValues(), array(
248  'title',
249  'description',
250  ));
251  }
252 
262  public function canComment($user_guid = 0) {
263  $result = parent::canComment($user_guid);
264  if ($result !== null) {
265  return $result;
266  }
267 
268  if ($user_guid == 0) {
269  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
270  }
271 
272  // must be logged in to comment
273  if (!$user_guid) {
274  return false;
275  }
276 
277  // must be member of group
278  if (elgg_instanceof($this->getContainerEntity(), 'group')) {
280  return false;
281  }
282  }
283 
284  // no checks on read access since a user cannot see entities outside his access
285  return true;
286  }
287 }
addToSite($site)
Add this object to a site.
Definition: ElggObject.php:216
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggObject.php:46
ui datepicker title
Definition: admin.php:616
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:67
get_site_objects($site_guid, $subtype="", $limit=10, $offset=0)
Get the objects belonging to a site.
loadAdditionalSelectValues(array $data)
Stores non-attributes from the loading of the entity as volatile data.
$object
Definition: upgrade.php:12
canWriteToContainer($user_guid=0, $type= 'all', $subtype= 'all')
Can a user add an entity to this container.
getSites($options="", $limit=10, $offset=0)
Return sites that this object is a member of.
Definition: ElggObject.php:200
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
Definition: entities.php:92
load($guid)
Loads the full when given a guid.
Definition: ElggObject.php:108
$value
Definition: longtext.php:26
add_site_object($site_guid, $object_guid)
Add an object to a site.
if($screenshots) $description
Definition: full.php:173
if(!$count) $offset
Definition: pagination.php:25
getGUID()
Returns the guid.
initializeAttributes()
Initialize the attributes array to include the type, title, and description.
Definition: ElggObject.php:30
$guid
Removes an admin notice.
elgg input elgg input tags
Definition: admin.php:490
$title
Definition: save.php:24
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
$options
Definition: index.php:14
if(!$site) if(!($site instanceof ElggSite)) $site description
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:922
$limit
Definition: userpicker.php:31
$key
Definition: summary.php:34
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
prepareObject($object)
{}
Definition: ElggObject.php:228
__construct($row=null)
Create a new .
Definition: ElggObject.php:68
$attrs
Definition: ajax_loader.php:30
setDisplayName($displayName)
{}
Definition: ElggObject.php:183
getDisplayName()
{}
Definition: ElggObject.php:176
canComment($user_guid=0)
Can a user comment on this object?
Definition: ElggObject.php:262
$row
getContainerEntity()
Get the container entity for this object.
$user_guid
Avatar remove action.
Definition: remove.php:6
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggObject.php:246