00001 <?php
00002
00022 class ElggObject extends ElggEntity
00023 {
00030 protected function initialise_attributes()
00031 {
00032 parent::initialise_attributes();
00033
00034 $this->attributes['type'] = "object";
00035 $this->attributes['title'] = "";
00036 $this->attributes['description'] = "";
00037 $this->attributes['tables_split'] = 2;
00038 }
00039
00047 function __construct($guid = null)
00048 {
00049 $this->initialise_attributes();
00050
00051 if (!empty($guid))
00052 {
00053
00054 if ($guid instanceof stdClass) {
00055
00056 if (!$this->load($guid->guid))
00057 throw new IOException(sprintf(elgg_echo('IOException:FailedToLoadGUID'), get_class(), $guid->guid));
00058 }
00059
00060
00061 else if ($guid instanceof ElggObject)
00062 {
00063 foreach ($guid->attributes as $key => $value)
00064 $this->attributes[$key] = $value;
00065 }
00066
00067
00068 else if ($guid instanceof ElggEntity)
00069 throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggObject'));
00070
00071
00072 else if (is_numeric($guid)) {
00073 if (!$this->load($guid)) IOException(sprintf(elgg_echo('IOException:FailedToLoadGUID'), get_class(), $guid));
00074 }
00075
00076 else
00077 throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnrecognisedValue'));
00078 }
00079 }
00080
00089 protected function load($guid)
00090 {
00091
00092 if (!parent::load($guid))
00093 return false;
00094
00095
00096 if ($this->attributes['type']!='object')
00097 throw new InvalidClassException(sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $guid, get_class()));
00098
00099
00100 $row = get_object_entity_as_row($guid);
00101 if (($row) && (!$this->isFullyLoaded())) $this->attributes['tables_loaded'] ++;
00102
00103
00104 $objarray = (array) $row;
00105 foreach($objarray as $key => $value)
00106 $this->attributes[$key] = $value;
00107
00108 return true;
00109 }
00110
00115 public function save()
00116 {
00117
00118 if (!parent::save())
00119 return false;
00120
00121
00122 return create_object_entity($this->get('guid'), $this->get('title'), $this->get('description'), $this->get('container_guid'));
00123 }
00124
00132 function getSites($subtype="", $limit = 10, $offset = 0) {
00133 return get_site_objects($this->getGUID(), $subtype, $limit, $offset);
00134 }
00135
00142 function addToSite($site_guid) {
00143 return add_site_object($this->getGUID(), $site_guid);
00144 }
00145
00152 function setContainer($container_guid)
00153 {
00154 $container_guid = (int)$container_guid;
00155
00156 return $this->set('container_guid', $container_guid);
00157 }
00158
00164 function getContainer()
00165 {
00166 return $this->get('container_guid');
00167 }
00168
00174 function getContainerEntity()
00175 {
00176 $result = get_entity($this->getContainer());
00177
00178 if (($result) && ($result instanceof ElggGroup))
00179 return $result;
00180
00181 return false;
00182 }
00183
00192
00193
00194
00195
00199 public function getExportableValues()
00200 {
00201 return array_merge(parent::getExportableValues(), array(
00202 'title',
00203 'description',
00204 ));
00205 }
00206 }
00207
00213 function get_object_entity_as_row($guid)
00214 {
00215 global $CONFIG;
00216
00217 $guid = (int)$guid;
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 if ($CONFIG->debug)
00232 error_log("** Sub part of GUID:$guid loaded from DB");
00233
00234 return get_data_row("SELECT * from {$CONFIG->dbprefix}objects_entity where guid=$guid");
00235
00236 }
00237
00246 function create_object_entity($guid, $title, $description)
00247 {
00248 global $CONFIG;
00249
00250 $guid = (int)$guid;
00251 $title = sanitise_string($title);
00252 $description = sanitise_string($description);
00253
00254 $row = get_entity_as_row($guid);
00255
00256 if ($row)
00257 {
00258
00259 if ($exists = get_data_row("SELECT guid from {$CONFIG->dbprefix}objects_entity where guid = {$guid}")) {
00260 $result = update_data("UPDATE {$CONFIG->dbprefix}objects_entity set title='$title', description='$description' where guid=$guid");
00261 if ($result!=false)
00262 {
00263
00264 $entity = get_entity($guid);
00265 if (trigger_elgg_event('update',$entity->type,$entity)) {
00266 return $guid;
00267 } else {
00268 $entity->delete();
00269 }
00270 }
00271 }
00272 else
00273 {
00274
00275
00276 $result = insert_data("INSERT into {$CONFIG->dbprefix}objects_entity (guid, title, description) values ($guid, '$title','$description')");
00277 if ($result!==false) {
00278 $entity = get_entity($guid);
00279 if (trigger_elgg_event('create',$entity->type,$entity)) {
00280 return $guid;
00281 } else {
00282 $entity->delete();
00283
00284 }
00285 }
00286 }
00287
00288 }
00289
00290 return false;
00291 }
00292
00300 function delete_object_entity($guid)
00301 {
00302 system_message(sprintf(elgg_echo('deprecatedfunction'), 'delete_user_entity'));
00303
00304 return 1;
00305 }
00306
00320 function search_for_object($criteria, $limit = 10, $offset = 0, $order_by = "", $count = false)
00321 {
00322 global $CONFIG;
00323
00324 $criteria = sanitise_string($criteria);
00325 $limit = (int)$limit;
00326 $offset = (int)$offset;
00327 $order_by = sanitise_string($order_by);
00328 $container_guid = (int)$container_guid;
00329
00330 $access = get_access_sql_suffix("e");
00331
00332 if ($order_by == "") $order_by = "e.time_created desc";
00333
00334 if ($count) {
00335 $query = "SELECT count(e.guid) as total ";
00336 } else {
00337 $query = "SELECT e.* ";
00338 }
00339 $query .= "from {$CONFIG->dbprefix}entities e join {$CONFIG->dbprefix}objects_entity o on e.guid=o.guid where match(o.title,o.description) against ('$criteria') and $access";
00340
00341 if (!$count) {
00342 $query .= " order by $order_by limit $offset, $limit";
00343 return get_data($query, "entity_row_to_elggstar");
00344 } else {
00345 if ($count = get_data_row($query)) {
00346 return $count->total;
00347 }
00348 }
00349 return false;
00350 }
00351
00360 function get_object_sites($object_guid, $limit = 10, $offset = 0) {
00361 $object_guid = (int)$object_guid;
00362 $limit = (int)$limit;
00363 $offset = (int)$offset;
00364
00365 return get_entities_from_relationship("member_of_site", $object_guid, false, "site", "", 0, "time_created desc", $limit, $offset);
00366 }
00367
00368 ?>