Elgg  Version 1.9
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['title'] = null;
35  $this->attributes['description'] = null;
36  $this->tables_split = 2;
37  }
38 
54  public function __construct($row = null) {
55  $this->initializeAttributes();
56 
57  // compatibility for 1.7 api.
58  $this->initialise_attributes(false);
59 
60  if (!empty($row)) {
61  // Is $row is a DB row from the entity table
62  if ($row instanceof stdClass) {
63  // Load the rest
64  if (!$this->load($row)) {
65  $msg = "Failed to load new " . get_class() . " for GUID: " . $row->guid;
66  throw new IOException($msg);
67  }
68  } else if ($row instanceof ElggObject) {
69  // $row is an ElggObject so this is a copy constructor
70  elgg_deprecated_notice('This type of usage of the ElggObject constructor was deprecated. Please use the clone method.', 1.7);
71  foreach ($row->attributes as $key => $value) {
72  $this->attributes[$key] = $value;
73  }
74  } else if (is_numeric($row)) {
75  // $row is a GUID so load
76  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
77  if (!$this->load($row)) {
78  throw new IOException("Failed to load new " . get_class() . " from GUID:" . $row);
79  }
80  } else {
81  throw new InvalidParameterException("Unrecognized value passed to constuctor.");
82  }
83  }
84  }
85 
94  protected function load($guid) {
95  $attr_loader = new Elgg_AttributeLoader(get_class(), 'object', $this->attributes);
96  $attr_loader->requires_access_control = !($this instanceof ElggPlugin);
97  $attr_loader->secondary_loader = 'get_object_entity_as_row';
98 
99  $attrs = $attr_loader->getRequiredAttributes($guid);
100  if (!$attrs) {
101  return false;
102  }
103 
104  $this->attributes = $attrs;
105  $this->tables_loaded = 2;
106  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
107  _elgg_cache_entity($this);
108 
109  return true;
110  }
111 
115  protected function create() {
116  global $CONFIG;
117 
118  $guid = parent::create();
119  if (!$guid) {
120  // @todo this probably means permission to create entity was denied
121  // Is returning false the correct thing to do
122  return false;
123  }
124  $title = sanitize_string($this->title);
126 
127  $query = "INSERT into {$CONFIG->dbprefix}objects_entity
128  (guid, title, description) values ($guid, '$title', '$description')";
129 
130  $result = $this->getDatabase()->insertData($query);
131  if ($result === false) {
132  // TODO(evan): Throw an exception here?
133  return false;
134  }
135 
136  return $guid;
137  }
138 
142  protected function update() {
143  global $CONFIG;
144 
145  if (!parent::update()) {
146  return false;
147  }
148 
149  $guid = (int)$this->guid;
150  $title = sanitize_string($this->title);
152 
153  $query = "UPDATE {$CONFIG->dbprefix}objects_entity
154  set title='$title', description='$description' where guid=$guid";
155 
156  return $this->getDatabase()->updateData($query) !== false;
157  }
158 
162  public function getDisplayName() {
163  return $this->title;
164  }
165 
169  public function setDisplayName($displayName) {
170  $this->title = $displayName;
171  }
172 
186  public function getSites($options = "", $limit = 10, $offset = 0) {
187  if (is_string($options)) {
188  elgg_deprecated_notice('ElggObject::getSites() takes an options array', 1.9);
189  return get_site_objects($this->getGUID(), $options, $limit, $offset);
190  }
191 
192  return parent::getSites();
193  }
194 
202  public function addToSite($site) {
203  if (is_numeric($site)) {
204  elgg_deprecated_notice('ElggObject::addToSite() takes a site entity', 1.9);
205  return add_site_object($site, $this->getGUID());
206  }
207 
208  return parent::addToSite($site);
209  }
210 
214  protected function prepareObject($object) {
215  $object = parent::prepareObject($object);
216  $object->title = $this->getDisplayName();
217  $object->description = $this->description;
218  $object->tags = $this->tags ? $this->tags : array();
219  return $object;
220  }
221 
222  /*
223  * EXPORTABLE INTERFACE
224  */
225 
232  public function getExportableValues() {
233  return array_merge(parent::getExportableValues(), array(
234  'title',
235  'description',
236  ));
237  }
238 
248  public function canComment($user_guid = 0) {
249  $result = parent::canComment($user_guid);
250  if ($result !== null) {
251  return $result;
252  }
253 
254  if ($user_guid == 0) {
256  }
257 
258  // must be logged in to comment
259  if (!$user_guid) {
260  return false;
261  }
262 
263  // must be member of group
264  if (elgg_instanceof($this->getContainerEntity(), 'group')) {
266  return false;
267  }
268  }
269 
270  // no checks on read access since a user cannot see entities outside his access
271  return true;
272  }
273 }
addToSite($site)
Add this object to a site.
Definition: ElggObject.php:202
ui datepicker title
Definition: admin.php:592
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.
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
getSites($options="", $limit=10, $offset=0)
Return sites that this object is a member of.
Definition: ElggObject.php:186
load($guid)
Loads the full ElggObject when given a guid.
Definition: ElggObject.php:94
$value
Definition: longtext.php:29
add_site_object($site_guid, $object_guid)
Add an object to a site.
$comment description
Definition: save.php:49
if($screenshots) $description
Definition: full.php:173
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:478
$title
Definition: save.php:24
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
$options
Definition: index.php:14
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an ElggEntity and optionally for type and subtype.
Definition: entities.php:1886
$limit
Definition: userpicker.php:33
$key
Definition: summary.php:34
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
elgg global
Pointer to the global context.
Definition: elgglib.js:12
prepareObject($object)
{}
Definition: ElggObject.php:214
__construct($row=null)
Create a new ElggObject.
Definition: ElggObject.php:54
$attrs
Definition: ajax_loader.php:30
setDisplayName($displayName)
{}
Definition: ElggObject.php:169
getDisplayName()
{}
Definition: ElggObject.php:162
canComment($user_guid=0)
Can a user comment on this object?
Definition: ElggObject.php:248
$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:232
_elgg_cache_entity(ElggEntity $entity)
Cache an entity.
Definition: entities.php:101
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
Elgg Object.
Definition: ElggObject.php:22