diff options
author | icewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-03-13 15:53:23 +0000 |
---|---|---|
committer | icewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-03-13 15:53:23 +0000 |
commit | ccf07a2057a0176ea4354282467c6cb121a5875a (patch) | |
tree | d8d235476eb18ab05319058f5ffde6dd044e9cd6 /engine/lib | |
parent | e05c80acc01561082c3690fa27080b940766397d (diff) | |
download | elgg-ccf07a2057a0176ea4354282467c6cb121a5875a.tar.gz elgg-ccf07a2057a0176ea4354282467c6cb121a5875a.tar.bz2 |
Marcus Povey <marcus@dushka.co.uk>
* Added ElggSite
git-svn-id: https://code.elgg.org/elgg/trunk@200 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/lib')
-rw-r--r-- | engine/lib/sites.php | 688 |
1 files changed, 221 insertions, 467 deletions
diff --git a/engine/lib/sites.php b/engine/lib/sites.php index 07c4d6e58..177f0adbd 100644 --- a/engine/lib/sites.php +++ b/engine/lib/sites.php @@ -7,625 +7,379 @@ * @package Elgg
* @subpackage Core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Curverider Ltd
+ * @author Marcus Povey <marcus@dushka.co.uk>
* @copyright Curverider Ltd 2008
* @link http://elgg.org/
*/
- /** * @class ElggSite - * This class represents an elgg site. + * Representation of a "site" in the system. * @author Marcus Povey <marcus@dushka.co.uk> */ - class ElggSite + class ElggSite extends ElggEntity { - - /** - * This contains the site's main properties (id, etc) - * @var array - */ - private $attributes; - /** * Construct a new site object, optionally from a given id value. * - * @param mixed $id + * @param mixed $guid If an int, load that GUID. + * If a db row then will attempt to load the rest of the data. + * @throws Exception if there was a problem creating the site. */ - function __construct($id = null) + function __construct($guid = null) { - $this->attributes = array(); + // Create attributes array if not already created + if (!is_array($this->attributes)) $this->attributes = array(); - if (!empty($id)) { - - $site = null; + if (!empty($guid)) + { + // Is $guid is a DB row - either a entity row, or a site table row. + if ($guid instanceof stdClass) { + + // Load what details we have + $objarray = (array)$guid; + foreach ($objarray as $key => $value) + $this->attributes[$key] = $value; + + // Load the rest + $this->load($objarray['guid']); + } - if ($id instanceof stdClass) { - $site = $id; // This is a db row, so serialise directly - - } else if ($id instanceof ElggSite) { - //$site = $id; - $site = new stdClass; - foreach ($id->attributes as $k => $v) - $site->$k = $v; - - } else if (strpos($id, "http") !== false) { - $site = get_site_byurl($id); - } else { - $tmp = get_site((int)$id); // This is an integer ID - $site = new stdClass; - foreach ($tmp->attributes as $k => $v) - $site->$k = $v; - + // Is $guid is an ElggSite? Use a copy constructor + else if ($guid instanceof ElggSite) + { + foreach ($guid->attributes as $key => $value) + $this->attributes[$key] = $value; } - if ($site) { - $objarray = (array) $site; + // Is this is an ElggEntity but not an ElggSite = ERROR! + else if ($guid instanceof ElggEntity) + throw new InvalidParameterException("Passing a non-ElggSite to an ElggSite constructor!"); - foreach($objarray as $key => $value) { - $this->attributes[$key] = $value; - error_log("$key => $value"); - } - } + // We assume if we have got this far, $guid is an int else - throw new IOException("Could not create ElggSite object"); + if (!$this->load($guid)) throw new IOException("Could not create a new ElggSite object from GUID:$guid"); } } - function __get($name) { - if (isset($this->attributes[$name])) { - return $this->attributes[$name]; + function __get($name) { return $this->get($name); } + function __set($name, $value) { return $this->set($name, $value); } + + /** + * Override the load function. + * This function will ensure that all data is loaded (were possible), so + * if only part of the ElggSite is loaded, it'll load the rest. + * + * @param int $guid + */ + protected function load($guid) + { + // Test to see if we have the generic stuff + if (!array_key_exists('type',$this->attributes)) + if (!parent::load($guid)) + return false; + + // Test to see if we have the site specific stuff + if (!array_key_exists('url', $this->attributes)) + { + // Load missing data + $row = get_site_as_row($guid); + + // Now put these into the attributes array as core values + $objarray = (array) $row; + foreach($objarray as $key => $value) + $this->attributes[$key] = $value; } - return null; - } - - function __set($name, $value) { - $this->attributes[$name] = $value; + return true; } /** - * Return the owner of this site. - * - * @return mixed - */ - function getOwner() { return get_user($this->owner_id); } - - /** - * Return a list of users using this site. - * - * @param int $limit - * @param int $offset - * @return array of ElggUsers + * Override the save function. */ - function getMembers($limit, $offset) { return get_site_users($this->id, $limit, $offset); } + public function save() + { + // Save generic stuff + if (!parent::save()) + return false; + + // Now save specific stuff + return create_site_entity($this->get('guid'), $this->get('name'), $this->get('description'), $this->get('url')); + } /** - * Get an array of member ElggObjects. - * - * @param string $type - * @param int $limit - * @param int $offset + * Delete this site. */ - function getObjects($type="", $limit = 10, $offset = 0) { return get_site_objects($this->id, $type, $limit, $offset); } + public function delete() + { + if (!parent::delete()) + return false; + + return delete_site_entity($guid); + } /** - * Get the collections associated with a site. + * Return a list of users using this site. * - * @param string $type * @param int $limit * @param int $offset - * @return unknown + * @return array of ElggUsers */ - function getCollections($type="", $limit = 10, $offset = 0) { return get_site_collections($this->id, $type, $limit, $offset); } + public function getMembers($limit = 10, $offset = 0) { get_site_members($this->getGUID(), $limit, $offset); } /** * Add a user to the site. * - * @param int $user_id + * @param int $user_guid */ - function addUser($user_id) { return add_site_user($this->id, $user_id); } + public function addUser($user_guid) { return add_site_user($this->getGUID(), $user_guid); } /** * Remove a site user. * - * @param int $user_id + * @param int $user_guid */ - function removeUser($user_id) { return remove_site_user($this->id, $user_id); } + public function removeUser($user_guid) { return remove_site_user($this->getGUID(), $user_guid); } /** - * Set the meta data. + * Get an array of member ElggObjects. * - * @param string $name - * @param string $value - * @param int $access_id - * @param string $vartype + * @param string $subtype + * @param int $limit + * @param int $offset */ - function setMetadata($name, $value, $access_id = 0, $vartype = "") { return set_site_metadata($name, $value, $access_id, $this->id, $vartype); } + public function getObjects($subtype="", $limit = 10, $offset = 0) { get_site_objects($this->getGUID(), $subtype, $limit, $offset); } /** - * Get the metadata for a site. + * Add an object to the site. * - * @param string $name + * @param int $user_id */ - function getMetadata($name) { return get_site_metadata($name, $this->id); } + public function addObject($object_guid) { return add_site_object($this->getGUID(), $object_guid); } /** - * Clear the metadata for a given site. + * Remove a site user. * - * @param string $name - */ - function clearMetadata($name = "") { return remove_site_metadata($this->id, $name); } - - /** - * Adds an annotation to a site. By default, the type is detected automatically; however, - * it can also be set. Note that by default, annotations are private. - * - * @param string $name - * @param string $value - * @param int $access_id - * @param int $owner_id - * @param string $vartype + * @param int $user_id */ - function annotate($name, $value, $access_id = 0, $owner_id = 0, $vartype = "") { return add_site_annotation($name, $value, $access_id, $owner_id, $this->id, $vartype); } - + public function removeObject($object_guid) { return remove_site_object($this->getGUID(), $object_guid); } + /** - * Get the annotations for a site. + * Get the collections associated with a site. * - * @param string $name + * @param string $type * @param int $limit * @param int $offset + * @return unknown */ - function getAnnotations($name, $limit = 50, $offset = 0) { return get_site_annotations($name, $this->id, $limit, $offset); } + public function getCollections($subtype="", $limit = 10, $offset = 0) { get_site_collections($this->getGUID(), $subtype, $limit, $offset); } - /** - * Return the annotations for the site. - * - * @param string $name The type of annotation. - */ - function countAnnotations($name) { return count_site_annotations($name, $this->id); } - /** - * Get the average of an integer type annotation. - * - * @param string $name - */ - function getAnnotationsAvg($name) { return get_site_annotations_avg($name, $this->id); } - - /** - * Get the sum of integer type annotations of a given type. - * - * @param string $name - */ - function getAnnotationsSum($name) { return get_site_annotations_sum($name, $this->id); } - - /** - * Get the minimum of integer type annotations of given type. - * - * @param string $name - */ - function getAnnotationsMin($name) { return get_site_annotations_min($name, $this->id); } - - /** - * Get the maximum of integer type annotations of a given type. - * - * @param string $name - */ - function getAnnotationsMax($name) { return get_site_annotations_max($name, $this->id); } - - /** - * Remove all annotations or all annotations of a given site. - * - * @param string $name - */ - function removeAnnotations($name = "") { return remove_site_annotations($this->id, $name); } - - /** - * Saves or updates the site to the db depending on whether or not id is specified. - */ - function save() - { - if ($this->id > 0) - return update_site($this->id, $this->title, $this->description, $this->url, $this->owner_id, $this->access_id); // ID Specified, update ID - else - { - $this->id = create_site($this->title, $this->description, $this->url, $this->owner_id, $this->access_id); // Create a site - if (!$this->id) throw new IOException("Unable to save new ElggSite"); - - return $this->id; - } - } - /** - * Delete a given site. - */ - function delete() { return delete_site($this->id); } } /** - * Convert a database row to a new ElggSite - * - * @param stdClass $row - * @return stdClass or ElggSite + * Create or update the extras table for a given site. + * Call create_entity first. + * + * @param int $guid + * @param string $name + * @param string $description + * @param string $url */ - function row_to_elggsite($row) + function create_site_entity($guid, $name, $description, $url) { - if (!($row instanceof stdClass)) - return $row; + global $CONFIG; + + $guid = (int)$guid; + $name = sanitise_string($name); + $description = sanitise_string($description); + $url = sanitise_string($url); + + $row = get_entity_as_row($guid); + + if ($row) + { + // Exists and you have access to it - return new ElggSite($row); + // Delete any existing stuff + delete_site_entity($guid); + + // Insert it + $result = insert_data("INSERT into {$CONFIG->dbprefix}sites_entity (name, description, url) values ('$name','$description','$url')"); + if ($result!==false) + return true; + } + + return false; } /** - * Get sites based on the provided information. - * - * @param int $user_id - * @param string $type - * @param string $metadata_name - * @param string $metadata_value - * @param string $order_by - * @param int $limit - * @param int $offset + * Delete a site's extra data. + * + * @param int $guid */ - function get_sites($user_id = 0, $type = "", $metadata_name = "", $metadata_value = "", $order_by = "created desc", $limit = 10, $offset = 0) + function delete_site_entity($guid) { - // I'm not sure what this is meant to do, or how its useful. + global $CONFIG; - //return get_objects_from_metadatas(0, 'site', $metadata_name, $metadata_value, $user_id, $order_by, $limit, $offset); - } - - /** - * Retrieves details about a site, if the current user is allowed to see it - * - * @param int $object_id The ID of the object to load - * @return object A database representation of the object - */ - function get_site($site_id) { + $guid = (int)$guid; - global $CONFIG; + $row = get_entity_as_row($guid); - $site_id = (int) $site_id; - $access = get_access_list(); + // Check to see if we have access and it exists + if ($row) + { + // Delete any existing stuff + return delete_data("DELETE from {$CONFIG->dbprefix}sites_entity where guid=$guid"); + } - return row_to_elggsite(get_data_row("select * from {$CONFIG->dbprefix}sites where id=$site_id and (access_id in {$access} or (access_id = 0 and owner_id = {$_SESSION['id']}))")); + return false; } /** - * Retrieve details about a site via its URL, if the current user is allowed to see it - * - * @param string $url - * @return object A database representation of the object + * Add a user to a site. + * + * @param int $site_guid + * @param int $user_guid */ - function get_site_byurl($url) + function add_site_user($site_guid, $user_guid) { global $CONFIG; - $url = sanitise_string(trim($url)); - $access = get_access_list(); - - return row_to_elggsite(get_data_row("select * from {$CONFIG->dbprefix}sites where url='$url' and (access_id in {$access} or (access_id = 0 and owner_id = {$_SESSION['id']}))")); + $site_guid = (int)$site_guid; + $user_guid = (int)$user_guid; + return add_entity_relationship($user_guid, "member_of_site", $site_guid); } /** - * Get a list of users using a given site. - * - * @param int $site_id - * @param int $limit - * @param int $offset + * Remove a user from a site. + * + * @param int $site_guid + * @param int $user_guid */ - function get_site_users($site_id, $limit, $offset) + function remove_site_user($site_guid, $user_guid) { - $site_id = (int)$site_id; - $limit = (int)$limit; - $offset = (int)$offset; + $site_guid = (int)$site_guid; + $user_guid = (int)$user_guid; - return get_data("SELECT u.* from users u, users_sites s where s.site_id=$site_id and u.id = s.user_id","row_to_elgguser"); + return remove_entity_relationship($user_guid, "member_of_site", $site_guid); } /** - * Get the objects for a given site - * - * @param int $site_id - * @param string $type - * @param int $limit + * Get the members of a site. + * + * @param int $site_guid + * @param int $limit * @param int $offset */ - function get_site_objects($site_id, $type = "", $limit = 10, $offset = 0) + function get_site_members($site_guid, $limit = 10, $offset = 0) { - /*global $CONFIG; - - $site_id = (int)$site_id; - $type = sanitise_string($type); + $site_guid = (int)$site_guid; $limit = (int)$limit; $offset = (int)$offset; - $owner_id = (int)$owner_id; if ($owner_id==0) $owner_id = $_SESSION['id']; - - $access = get_access_list(); - $type = get_data_row("SELECT * from {$CONFIG->dbprefix}object_types where name='$type'"); - if (!$type) - return false; - - return get_data("SELECT * from {$CONFIG->dbprefix}objects where site_id=$site_id and type_id={$type->id} and (access_id in {$access} or (access_id = 0 and owner_id = {$_SESSION['id']}))","row_to_elggobject");
- */
- return get_objects(0,$type, $limit, $offset, $site_id); - } - - /** - * Get the collections associated with this site. - * - * @param int $site_id - * @param string $type - * @param int $limit - * @param int $offset - */ - function get_site_collections($site_id, $type = "", $limit = 10, $offset = 0) - { - // TODO : Writeme - throw new NotImplementedException("Writeme!"); + return get_entities_from_relationship("member_of_site", $site_guid, true, "user", "", 0, "time_created desc", $limit, $offset); } /** - * Add a site user. - * - * @param int $site_id - * @param int $user_id + * Add an object to a site. + * + * @param int $site_guid + * @param int $object_guid */ - function add_site_user($site_id, $user_id) + function add_site_object($site_guid, $object_guid) { global $CONFIG; - $site = get_site($site_id); - $user = get_user($user_id); + $site_guid = (int)$site_guid; + $object_guid = (int)$object_guid; - if (($site) && ($user)) - return insert_data("INSERT into {$CONFIG->dbprefix}users_sites (site_id, user_id) VALUES ({$site->id},{$user->id})"); - - return false; + return add_entity_relationship($object_guid, "member_of_site", $site_guid); } /** - * Remove a site user. - * - * @param int $site_id - * @param int $user_id + * Remove an object from a site. + * + * @param int $site_guid + * @param int $object_guid */ - function remove_site_user($site_id, $user_id) + function remove_site_object($site_guid, $object_guid) { - global $CONFIG; - - $site_id = (int)$site_id; - $user_id = (int)$user_id; + $site_guid = (int)$site_guid; + $object_guid = (int)$object_guid; - return delete_data("DELETE from {$CONFIG->dbprefix}users_sites where site_id=$site_id and user_id=$user_id"); - } - - /** - * Set the site metadata. - * - * @param string $name - * @param string $value - * @param int $access_id - * @param int $site_id - * @param string $vartype - */ - function set_site_metadata($name, $value, $access_id, $site_id, $vartype = "") - { - $name = sanitise_string($name); - $value = sanitise_string($value); - $access_id = (int)$access_id; - $site_id = (int)$site_id; - $vartype = sanitise_string($vartype); - $owner_id = $_SESSION['id']; - - $id = create_metadata($site_id, 'site', $name, $value, $vartype, $owner_id, $access_id); - return $id; + return remove_entity_relationship($object_guid, "member_of_site", $site_guid); } /** - * Get the site metadata. - * - * @param string $name - * @param int $site_id + * Get the objects belonging to a site. + * + * @param int $site_guid + * @param string $subtype + * @param int $limit + * @param int $offset */ - function get_site_metadata($name, $site_id) + function get_site_objects($site_guid, $subtype = "", $limit = 10, $offset = 0) { - $name = sanitise_string($name); - $site_id = (int)$site_id; + $site_guid = (int)$site_guid; + $subtype = sanitise_string($subtype); + $limit = (int)$limit; + $offset = (int)$offset; - return get_metadatas($site_id, 'site'); + return get_entities_from_relationship("member_of_site", $site_guid, true, "object", $subtype, 0, "time_created desc", $limit, $offset); } /** - * Remove site metadata - * - * @param int $site_id - * @param string $name + * Add a collection to a site. + * + * @param int $site_guid + * @param int $collection_guid */ - function remove_site_metadata($site_id, $name) + function add_site_collection($site_guid, $collection_guid) { - $result = get_metadatas($site_id, 'site', $name); + global $CONFIG; - if ($result) - { - foreach ($result as $r) - delete_metadata($r->id); - } + $site_guid = (int)$site_guid; + $collection_guid = (int)$collection_guid; - return false; + return add_entity_relationship($collection_guid, "member_of_site", $site_guid); } /** - * Adds an annotation to a site. By default, the type is detected automatically; - * however, it can also be set. Note that by default, annotations are private. + * Remove a collection from a site. * - * @param string $name - * @param string $value - * @param int $access_id - * @param int $owner_id - * @param int $site_id - * @param string $vartype + * @param int $site_guid + * @param int $collection_guid */ - function add_site_annotation($name, $value, $access_id, $owner_id, $site_id, $vartype) + function remove_site_collection($site_guid, $collection_guid) { - $name = sanitise_string($name); - $value = sanitise_string($value); - $access_id = (int)$access_id; - $owner_id = (int)$owner_id; if ($owner_id==0) $owner_id = $_SESSION['id']; - $site_id = (int)$site_id; - $vartype = sanitise_string($vartype); + $site_guid = (int)$site_guid; + $collection_guid = (int)$collection_guid; - $id = create_annotation($site_id, 'site', $name, $value, $vartype, $owner_id, $access_id); - - return $id; + return remove_entity_relationship($collection_guid, "member_of_site", $site_guid); } /** - * Get the annotations for a site. - * - * @param string $name - * @param int $site_id - * @param int $limit + * Get the collections belonging to a site. + * + * @param int $site_guid + * @param string $subtype + * @param int $limit * @param int $offset */ - function get_site_annotations($name, $site_id, $limit, $offset) + function get_site_collections($site_guid, $subtype = "", $limit = 10, $offset = 0) { - $name = sanitise_string($name); - $site_id = (int)$site_id; + $site_guid = (int)$site_guid; + $subtype = sanitise_string($subtype); $limit = (int)$limit; $offset = (int)$offset; - $owner_id = (int)$owner_id; if ($owner_id==0) $owner_id = $_SESSION['id']; // Consider adding the option to change in param? - return get_annotations($site_id, 'site', "", "","", $owner_id, "created desc", $limit, $offset); + return get_entities_from_relationship("member_of_site", $site_guid, true, "collection", $subtype, 0, "time_created desc", $limit, $offset); } - /** - * Count the site annotations for a site of a given type. - * - * @param string $name - * @param int $site_id - */ - function count_site_annotations($name, $site_id) { return count_annotations($site_id, 'site', $name); } - - /** - * Get the average of an integer type annotation. - * - * @param string $name - * @param int $site_id - */ - function get_site_annotations_avg($name, $site_id) { return get_annotations_avg($site_id, 'site', $name); } - /** - * Get the sum of integer type annotations of a given type. - * - * @param string $name - * @param int $site_id - */ - function get_site_annotations_sum($name, $site_id) { return get_annotations_sum($site_id, 'site', $name); } - - /** - * Get the min of integer type annotations of a given type. - * - * @param string $name - * @param int $site_id - */ - function get_site_annotations_min($name, $site_id) { return get_annotations_min($site_id, 'site', $name); } - - /** - * Get the max of integer type annotations of a given type. - * - * @param string $name - * @param int $site_id - */ - function get_site_annotations_max($name, $site_id) { return get_annotations_max($site_id, 'site', $name); } - - /** - * Remove all site annotations, or site annotations of a given type. - * - * @param int $site_id - * @param string $name - */ - function remove_site_annotations($site_id, $name) - { - $annotations = get_annotations($site_id, 'site', $name); - - if($annotations) - { - foreach ($annotations as $a) - { - delete_annotation($a->id); - } - - return true; - } - - return false; - } - - /** - * Create a site. - * - * @param string $title - * @param string $description - * @param string $url - * @param int $owner_id - * @param int $access_id - */ - function create_site($title, $description, $url, $owner_id = 0, $access_id = 0) - { - global $CONFIG; - - $title = sanitise_string($title); - $description = sanitise_string($description); - $url = sanitise_string($url); - $owner_id = (int)$owner_id; - $access_id = (int)$access_id; - $time = time(); - - return insert_data("INSERT into {$CONFIG->dbprefix}sites (name, description, url, owner_id, created, last_updated, access_id) VALUES ('$title','$description','$url',$owner_id,'$time','$time', $access_id)"); - } - - /** - * Saves or updates the site to the db depending on whether or not id is specified - * - * @param int $id - * @param string $title - * @param string $description - * @param string $url - * @param int $owner_id - * @param int $access_id - */ - function update_site($id, $title, $description, $url, $owner_id, $access_id) - { - global $CONFIG; - - $id = (int)$id; - $title = sanitise_string($title); - $description = sanitise_string($description); - $url = sanitise_string($url); - $owner_id = (int)$owner_id; - $access_id = (int)$access_id; - $time = time(); - - $access = get_access_list(); - - return update_data("UPDATE {$CONFIG->dbprefix}sites set name='$title', description='$description', url='$url', last_updated='$time', owner_id=$owner_id, access_id=$access_id WHERE id=$id and owner_id = {$_SESSION['id']}"); - } - - /** - * Delete a given site. - * - * @param int $site_id - */ - function delete_site($site_id) - { - global $CONFIG; - - $site_id = (int)$site_id; - - return delete_data("DELETE from {$CONFIG->dbprefix}sites where id=$site_id"); - } /**
* Initialise site handling
|