Elgg  Version 2.3
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  }
36 
45  final public static function getExternalAttributes() {
46  return [
47  'title' => null,
48  'description' => null,
49  ];
50  }
51 
67  public function __construct($row = null) {
68  $this->initializeAttributes();
69 
70  if (!empty($row)) {
71  // Is $row is a DB row from the entity table
72  if ($row instanceof \stdClass) {
73  // Load the rest
74  if (!$this->load($row)) {
75  $msg = "Failed to load new " . get_class($this) . " for GUID: " . $row->guid;
76  throw new \IOException($msg);
77  }
78  } else if (is_numeric($row)) {
79  // $row is a GUID so load
80  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
81  if (!$this->load($row)) {
82  throw new \IOException("Failed to load new " . get_class($this) . " from GUID:" . $row);
83  }
84  } else {
85  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
86  }
87  }
88  }
89 
98  protected function load($guid) {
99  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'object', $this->attributes);
100  $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
101  $attr_loader->secondary_loader = 'get_object_entity_as_row';
102 
103  $attrs = $attr_loader->getRequiredAttributes($guid);
104  if (!$attrs) {
105  return false;
106  }
107 
108  $this->attributes = $attrs;
109  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
110  _elgg_services()->entityCache->set($this);
111 
112  return true;
113  }
114 
118  protected function create() {
119 
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 
127  $dbprefix = elgg_get_config('dbprefix');
128  $query = "INSERT INTO {$dbprefix}objects_entity
129  (guid, title, description)
130  VALUES
131  (:guid, :title, :description)";
132 
133  $params = [
134  ':guid' => (int) $guid,
135  ':title' => (string) $this->title,
136  ':description' => (string) $this->description,
137  ];
138 
139  $result = $this->getDatabase()->insertData($query, $params);
140 
141  if ($result === false) {
142  // TODO(evan): Throw an exception here?
143  return false;
144  }
145 
146  return $guid;
147  }
148 
152  protected function update() {
153 
154  if (!parent::update()) {
155  return false;
156  }
157 
158  $dbprefix = elgg_get_config('dbprefix');
159 
160  $query = "
161  UPDATE {$dbprefix}objects_entity
162  SET title = :title,
163  description = :description
164  WHERE guid = :guid
165  ";
166 
167  $params = [
168  ':guid' => $this->guid,
169  ':title' => (string) $this->title,
170  ':description' => (string) $this->description,
171  ];
172 
173  return $this->getDatabase()->updateData($query, false, $params) !== false;
174  }
175 
179  public function getDisplayName() {
180  return $this->title;
181  }
182 
186  public function setDisplayName($displayName) {
187  $this->title = $displayName;
188  }
189 
193  protected function prepareObject($object) {
194  $object = parent::prepareObject($object);
195  $object->title = $this->getDisplayName();
196  $object->description = $this->description;
197  $object->tags = $this->tags ? $this->tags : array();
198  return $object;
199  }
200 
201  /*
202  * EXPORTABLE INTERFACE
203  */
204 
211  public function getExportableValues() {
212  return array_merge(parent::getExportableValues(), array(
213  'title',
214  'description',
215  ));
216  }
217 
228  public function canComment($user_guid = 0, $default = null) {
229  $result = parent::canComment($user_guid, $default);
230  if ($result !== null) {
231  return $result;
232  }
233 
234  if ($user_guid == 0) {
235  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
236  }
237 
238  // must be logged in to comment
239  if (!$user_guid) {
240  return false;
241  }
242 
243  // must be member of group
244  if (elgg_instanceof($this->getContainerEntity(), 'group')) {
246  return false;
247  }
248  }
249 
250  // no checks on read access since a user cannot see entities outside his access
251  return true;
252  }
253 }
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggObject.php:45
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:48
loadAdditionalSelectValues(array $data)
Stores non-attributes from the loading of the entity as volatile data.
canWriteToContainer($user_guid=0, $type= 'all', $subtype= 'all')
Can a user add an entity to this container.
load($guid)
Loads the full when given a guid.
Definition: ElggObject.php:98
$default
Definition: checkbox.php:34
initializeAttributes()
Initialize the attributes array to include the type, title, and description.
Definition: ElggObject.php:30
$guid
Removes an admin notice.
$title
Definition: save.php:22
elgg input elgg input elgg input tags
Definition: admin.css.php:505
$params
Definition: login.php:72
ui datepicker title
Definition: admin.css.php:662
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:736
canComment($user_guid=0, $default=null)
Can a user comment on this object?
Definition: ElggObject.php:228
if($categories) $description
Definition: full.php:176
$dbprefix
Definition: index.php:13
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
prepareObject($object)
{}
Definition: ElggObject.php:193
__construct($row=null)
Create a new .
Definition: ElggObject.php:67
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
$attrs
Definition: ajax_loader.php:30
setDisplayName($displayName)
{}
Definition: ElggObject.php:186
getDisplayName()
{}
Definition: ElggObject.php:179
$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:211