Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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  global $CONFIG;
120 
121  $guid = parent::create();
122  if (!$guid) {
123  // @todo this probably means permission to create entity was denied
124  // Is returning false the correct thing to do
125  return false;
126  }
127  $title = sanitize_string($this->title);
129 
130  $query = "INSERT into {$CONFIG->dbprefix}objects_entity
131  (guid, title, description) values ($guid, '$title', '$description')";
132 
133  $result = $this->getDatabase()->insertData($query);
134  if ($result === false) {
135  // TODO(evan): Throw an exception here?
136  return false;
137  }
138 
139  return $guid;
140  }
141 
145  protected function update() {
146  global $CONFIG;
147 
148  if (!parent::update()) {
149  return false;
150  }
151 
152  $guid = (int)$this->guid;
153  $title = sanitize_string($this->title);
155 
156  $query = "
157  UPDATE {$CONFIG->dbprefix}objects_entity
158  SET title = '$title',
159  description = '$description'
160  WHERE guid = $guid
161  ";
162 
163  return $this->getDatabase()->updateData($query) !== false;
164  }
165 
169  public function getDisplayName() {
170  return $this->title;
171  }
172 
176  public function setDisplayName($displayName) {
177  $this->title = $displayName;
178  }
179 
183  protected function prepareObject($object) {
184  $object = parent::prepareObject($object);
185  $object->title = $this->getDisplayName();
186  $object->description = $this->description;
187  $object->tags = $this->tags ? $this->tags : array();
188  return $object;
189  }
190 
191  /*
192  * EXPORTABLE INTERFACE
193  */
194 
201  public function getExportableValues() {
202  return array_merge(parent::getExportableValues(), array(
203  'title',
204  'description',
205  ));
206  }
207 
217  public function canComment($user_guid = 0) {
218  $result = parent::canComment($user_guid);
219  if ($result !== null) {
220  return $result;
221  }
222 
223  if ($user_guid == 0) {
224  $user_guid = _elgg_services()->session->getLoggedInUserGuid();
225  }
226 
227  // must be logged in to comment
228  if (!$user_guid) {
229  return false;
230  }
231 
232  // must be member of group
233  if (elgg_instanceof($this->getContainerEntity(), 'group')) {
235  return false;
236  }
237  }
238 
239  // no checks on read access since a user cannot see entities outside his access
240  return true;
241  }
242 }
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
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:46
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
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
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
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:716
if($categories) $description
Definition: full.php:166
global $CONFIG
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
prepareObject($object)
{}
Definition: ElggObject.php:183
__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:176
getDisplayName()
{}
Definition: ElggObject.php:169
canComment($user_guid=0)
Can a user comment on this object?
Definition: ElggObject.php:217
$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:201