00001 <?php
00002
00022 class ElggSite extends ElggEntity
00023 {
00030 protected function initialise_attributes()
00031 {
00032 parent::initialise_attributes();
00033
00034 $this->attributes['type'] = "site";
00035 $this->attributes['name'] = "";
00036 $this->attributes['description'] = "";
00037 $this->attributes['url'] = "";
00038 $this->attributes['tables_split'] = 2;
00039 }
00040
00048 function __construct($guid = null)
00049 {
00050 $this->initialise_attributes();
00051
00052 if (!empty($guid))
00053 {
00054
00055 if ($guid instanceof stdClass) {
00056
00057 if (!$this->load($guid->guid))
00058 throw new IOException(sprintf(elgg_echo('IOException:FailedToLoadGUID'), get_class(), $guid->guid));
00059 }
00060
00061
00062 else if ($guid instanceof ElggSite)
00063 {
00064 foreach ($guid->attributes as $key => $value)
00065 $this->attributes[$key] = $value;
00066 }
00067
00068
00069 else if ($guid instanceof ElggEntity)
00070 throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggSite'));
00071
00072
00073 else if (strpos($guid, "http")!==false)
00074 {
00075 $guid = get_site_by_url($guid);
00076 foreach ($guid->attributes as $key => $value)
00077 $this->attributes[$key] = $value;
00078
00079 }
00080
00081
00082 else if (is_numeric($guid)) {
00083 if (!$this->load($guid)) throw new IOException(sprintf(elgg_echo('IOException:FailedToLoadGUID'), get_class(), $guid));
00084 }
00085
00086 else
00087 throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnrecognisedValue'));
00088 }
00089 }
00090
00098 protected function load($guid)
00099 {
00100
00101 if (!parent::load($guid))
00102 return false;
00103
00104
00105 if ($this->attributes['type']!='site')
00106 throw new InvalidClassException(sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $guid, get_class()));
00107
00108
00109 $row = get_site_entity_as_row($guid);
00110 if (($row) && (!$this->isFullyLoaded())) $this->attributes['tables_loaded'] ++;
00111
00112
00113 $objarray = (array) $row;
00114 foreach($objarray as $key => $value)
00115 $this->attributes[$key] = $value;
00116
00117 return true;
00118 }
00119
00123 public function save()
00124 {
00125
00126 if (!parent::save())
00127 return false;
00128
00129
00130 return create_site_entity($this->get('guid'), $this->get('name'), $this->get('description'), $this->get('url'));
00131 }
00132
00136 public function delete()
00137 {
00138 global $CONFIG;
00139 if ($CONFIG->site->getGUID() == $this->guid)
00140 throw new SecurityException('SecurityException:deletedisablecurrentsite');
00141
00142 return parent::delete;
00143 }
00144
00150 public function disable($reason = "")
00151 {
00152 global $CONFIG;
00153 if ($CONFIG->site->getGUID() == $this->guid)
00154 throw new SecurityException('SecurityException:deletedisablecurrentsite');
00155
00156 return parent::disable($reason);
00157 }
00158
00166 public function getMembers($limit = 10, $offset = 0) { get_site_members($this->getGUID(), $limit, $offset); }
00167
00173 public function addUser($user_guid) { return add_site_user($this->getGUID(), $user_guid); }
00174
00180 public function removeUser($user_guid) { return remove_site_user($this->getGUID(), $user_guid); }
00181
00189 public function getObjects($subtype="", $limit = 10, $offset = 0) { get_site_objects($this->getGUID(), $subtype, $limit, $offset); }
00190
00196 public function addObject($object_guid) { return add_site_object($this->getGUID(), $object_guid); }
00197
00203 public function removeObject($object_guid) { return remove_site_object($this->getGUID(), $object_guid); }
00204
00213 public function getCollections($subtype="", $limit = 10, $offset = 0) { get_site_collections($this->getGUID(), $subtype, $limit, $offset); }
00214
00215
00216
00220 public function getExportableValues()
00221 {
00222 return array_merge(parent::getExportableValues(), array(
00223 'name',
00224 'description',
00225 'url',
00226 ));
00227 }
00228
00229 }
00230
00236 function get_site_entity_as_row($guid)
00237 {
00238 global $CONFIG;
00239
00240 $guid = (int)$guid;
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 if (isset($CONFIG->debug) && $CONFIG->debug == true)
00255 error_log("** Sub part of GUID:$guid loaded from DB");
00256
00257 return get_data_row("SELECT * from {$CONFIG->dbprefix}sites_entity where guid=$guid");
00258
00259 }
00260
00270 function create_site_entity($guid, $name, $description, $url)
00271 {
00272 global $CONFIG;
00273
00274 $guid = (int)$guid;
00275 $name = sanitise_string($name);
00276 $description = sanitise_string($description);
00277 $url = sanitise_string($url);
00278
00279 $row = get_entity_as_row($guid);
00280
00281 if ($row)
00282 {
00283
00284 if ($exists = get_data_row("SELECT guid from {$CONFIG->dbprefix}sites_entity where guid = {$guid}")) {
00285 $result = update_data("UPDATE {$CONFIG->dbprefix}sites_entity set name='$name', description='$description', url='$url' where guid=$guid");
00286 if ($result!=false)
00287 {
00288
00289 $entity = get_entity($guid);
00290 if (trigger_elgg_event('update',$entity->type,$entity)) {
00291 return $guid;
00292 } else {
00293 $entity->delete();
00294
00295 }
00296 }
00297 }
00298 else
00299 {
00300
00301 $result = insert_data("INSERT into {$CONFIG->dbprefix}sites_entity (guid, name, description, url) values ($guid, '$name','$description','$url')");
00302 if ($result!==false) {
00303 $entity = get_entity($guid);
00304 if (trigger_elgg_event('create',$entity->type,$entity)) {
00305 return $guid;
00306 } else {
00307 $entity->delete();
00308
00309 }
00310 }
00311 }
00312 }
00313
00314 return false;
00315 }
00316
00324 function delete_site_entity($guid)
00325 {
00326 system_message(sprintf(elgg_echo('deprecatedfunction'), 'delete_user_entity'));
00327
00328 return 1;
00329 }
00330
00337 function add_site_user($site_guid, $user_guid)
00338 {
00339 global $CONFIG;
00340
00341 $site_guid = (int)$site_guid;
00342 $user_guid = (int)$user_guid;
00343
00344 return add_entity_relationship($user_guid, "member_of_site", $site_guid);
00345 }
00346
00353 function remove_site_user($site_guid, $user_guid)
00354 {
00355 $site_guid = (int)$site_guid;
00356 $user_guid = (int)$user_guid;
00357
00358 return remove_entity_relationship($user_guid, "member_of_site", $site_guid);
00359 }
00360
00368 function get_site_members($site_guid, $limit = 10, $offset = 0)
00369 {
00370 $site_guid = (int)$site_guid;
00371 $limit = (int)$limit;
00372 $offset = (int)$offset;
00373
00374 return get_entities_from_relationship("member_of_site", $site_guid, true, "user", "", 0, "time_created desc", $limit, $offset);
00375 }
00376
00385 function list_site_members($site_guid, $limit = 10, $fullview = true) {
00386
00387 $offset = (int) get_input('offset');
00388 $limit = (int) $limit;
00389 $count = (int) get_entities_from_relationship("member_of_site", $site_guid, true, "user", "", 0, "time_created desc", $limit, $offset, true);
00390 $entities = get_site_members($site_guid, $limit, $offset);
00391
00392 return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview);
00393
00394 }
00395
00402 function add_site_object($site_guid, $object_guid)
00403 {
00404 global $CONFIG;
00405
00406 $site_guid = (int)$site_guid;
00407 $object_guid = (int)$object_guid;
00408
00409 return add_entity_relationship($object_guid, "member_of_site", $site_guid);
00410 }
00411
00418 function remove_site_object($site_guid, $object_guid)
00419 {
00420 $site_guid = (int)$site_guid;
00421 $object_guid = (int)$object_guid;
00422
00423 return remove_entity_relationship($object_guid, "member_of_site", $site_guid);
00424 }
00425
00434 function get_site_objects($site_guid, $subtype = "", $limit = 10, $offset = 0)
00435 {
00436 $site_guid = (int)$site_guid;
00437 $subtype = sanitise_string($subtype);
00438 $limit = (int)$limit;
00439 $offset = (int)$offset;
00440
00441 return get_entities_from_relationship("member_of_site", $site_guid, true, "object", $subtype, 0, "time_created desc", $limit, $offset);
00442 }
00443
00450 function add_site_collection($site_guid, $collection_guid)
00451 {
00452 global $CONFIG;
00453
00454 $site_guid = (int)$site_guid;
00455 $collection_guid = (int)$collection_guid;
00456
00457 return add_entity_relationship($collection_guid, "member_of_site", $site_guid);
00458 }
00459
00466 function remove_site_collection($site_guid, $collection_guid)
00467 {
00468 $site_guid = (int)$site_guid;
00469 $collection_guid = (int)$collection_guid;
00470
00471 return remove_entity_relationship($collection_guid, "member_of_site", $site_guid);
00472 }
00473
00482 function get_site_collections($site_guid, $subtype = "", $limit = 10, $offset = 0)
00483 {
00484 $site_guid = (int)$site_guid;
00485 $subtype = sanitise_string($subtype);
00486 $limit = (int)$limit;
00487 $offset = (int)$offset;
00488
00489 return get_entities_from_relationship("member_of_site", $site_guid, true, "collection", $subtype, 0, "time_created desc", $limit, $offset);
00490 }
00491
00495 function get_site_by_url($url)
00496 {
00497 global $CONFIG;
00498
00499 $url = sanitise_string($url);
00500
00501 $row = get_data_row("SELECT * from {$CONFIG->dbprefix}sites_entity where url='$url'");
00502
00503 if ($row)
00504 return new ElggSite($row);
00505
00506 return false;
00507 }
00508
00522 function search_for_site($criteria, $limit = 10, $offset = 0, $order_by = "", $count = false)
00523 {
00524 global $CONFIG;
00525
00526 $criteria = sanitise_string($criteria);
00527 $limit = (int)$limit;
00528 $offset = (int)$offset;
00529 $order_by = sanitise_string($order_by);
00530
00531 $access = get_access_sql_suffix("e");
00532
00533 if ($order_by == "") $order_by = "e.time_created desc";
00534
00535 if ($count) {
00536 $query = "SELECT count(e.guid) as total ";
00537 } else {
00538 $query = "SELECT e.* ";
00539 }
00540 $query .= "from {$CONFIG->dbprefix}entities e join {$CONFIG->dbprefix}sites_entity s on e.guid=s.guid where match(s.name,s.description,s.url) against ('$criteria') and $access";
00541
00542 if (!$count) {
00543 $query .= " order by $order_by limit $offset, $limit";
00544 return get_data($query, "entity_row_to_elggstar");
00545 } else {
00546 if ($count = get_data_row($query)) {
00547 return $count->total;
00548 }
00549 }
00550 return false;
00551 }
00552
00558 function get_site_domain($guid)
00559 {
00560 $guid = (int)$guid;
00561
00562 $site = get_entity($guid);
00563 if ($site instanceof ElggSite) {
00564 $breakdown = parse_url($site->url);
00565 return $breakdown['host'];
00566 }
00567
00568 return false;
00569 }
00570
00584 function sites_init($event, $object_type, $object) {
00585
00586 global $CONFIG;
00587
00588 if (is_installed() && is_db_installed()) {
00589
00590 $site = trigger_plugin_hook("siteid","system");
00591 if ($site === null || $site === false) {
00592 $CONFIG->site_id = (int) datalist_get('default_site');
00593 } else {
00594 $CONFIG->site_id = $site;
00595 }
00596 $CONFIG->site_guid = $CONFIG->site_id;
00597 $CONFIG->site = get_entity($CONFIG->site_guid);
00598
00599 return true;
00600 }
00601
00602 return true;
00603 }
00604
00605
00606
00607 register_elgg_event_handler('boot','system','sites_init',2);
00608
00609 ?>