diff options
| author | ben <ben@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-04-15 16:23:25 +0000 | 
|---|---|---|
| committer | ben <ben@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-04-15 16:23:25 +0000 | 
| commit | 9cc5d6d7fcd4dd3a4a6bc6309e86cd83f751b355 (patch) | |
| tree | 066886e94983bd677b93eb078b5cc584b2668d6f /engine/lib | |
| parent | bf88f65d3010b11d45ab991c427376e6bf10f19f (diff) | |
| download | elgg-9cc5d6d7fcd4dd3a4a6bc6309e86cd83f751b355.tar.gz elgg-9cc5d6d7fcd4dd3a4a6bc6309e86cd83f751b355.tar.bz2  | |
get_entities now allows for arrays of GUIDs to be supplied as an owner, and can be asked to count entities rather than return them; as a result, get_user_friends_objects and count_* functionality is also now available
git-svn-id: https://code.elgg.org/elgg/trunk@468 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/lib')
| -rw-r--r-- | engine/lib/database.php | 32 | ||||
| -rw-r--r-- | engine/lib/entities.php | 57 | ||||
| -rw-r--r-- | engine/lib/users.php | 62 | 
3 files changed, 130 insertions, 21 deletions
diff --git a/engine/lib/database.php b/engine/lib/database.php index 045f72c72..0979592c0 100644 --- a/engine/lib/database.php +++ b/engine/lib/database.php @@ -359,6 +359,38 @@          	return mysql_real_escape_string(trim($string));
          }
 +	/**
 +	 * Wrapper function for Americans
 +	 *
 +	 * @param string $string The string to sanitise
 +	 * @return string Sanitised string
 +	 * @uses sanitise_string
 +	 */
 +        function sanitize_string($string) {
 +        	return sanitise_string($string);
 +        }
 +        
 +	/**
 +	 * Sanitises an integer for database use
 +	 *
 +	 * @param int $int
 +	 * @return int Sanitised integer
 +	 */
 +        function sanitise_int($int) {
 +        	return (int) $int;
 +        }
 +        
 +	/**
 +	 * Wrapper function for Americans
 +	 *
 +	 * @param int $int
 +	 * @return int Sanitised integer
 +	 * @uses sanitise_string
 +	 */
 +        function sanitize_int($int) {
 +        	return (int) $int;
 +        }
 +        
  	// Stuff for initialisation
  		register_event_handler('boot','system','init_db',0);
 diff --git a/engine/lib/entities.php b/engine/lib/entities.php index c4136de49..3fbd6587c 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -686,7 +686,7 @@  	/**  	 * Return the entity for a given guid as the correct object. -	 * @param $guid +	 * @param int $guid The GUID of the entity  	 * @return a child of ElggEntity appropriate for the type.  	 */  	function get_entity($guid) @@ -695,22 +695,22 @@  	}  	/** -	 * Return entities matching a given query. +	 * Return entities matching a given query, or the number thereof  	 *  -	 * @param string $type -	 * @param string $subtype -	 * @param int $owner_guid -	 * @param string $order_by -	 * @param int $limit -	 * @param int $offset +	 * @param string $type The type of entity (eg "user", "object" etc) +	 * @param string $subtype The arbitrary subtype of the entity +	 * @param int $owner_guid The GUID of the owning user +	 * @param string $order_by The field to order by; by default, time_created desc +	 * @param int $limit The number of entities to return; 10 by default +	 * @param int $offset The indexing offset, 0 by default
 +	 * @param boolean $count Set to true to get a count rather than the entities themselves (limits and offsets don't apply in this context). Defaults to false.  	 */ -	function get_entities($type = "", $subtype = "", $owner_guid = 0, $order_by = "time_created desc", $limit = 10, $offset = 0) +	function get_entities($type = "", $subtype = "", $owner_guid = 0, $order_by = "time_created desc", $limit = 10, $offset = 0, $count = false)  	{  		global $CONFIG;  		$type = sanitise_string($type);  		$subtype = get_subtype_id($type, $subtype); -		$owner_guid = (int)$owner_guid;  		$order_by = sanitise_string($order_by);  		$limit = (int)$limit;  		$offset = (int)$offset; @@ -723,18 +723,35 @@  			$where[] = "type='$type'";  		if ($subtype)  			$where[] = "subtype=$subtype"; -		if ($owner_guid != "") -			$where[] = "owner_guid='$owner_guid'"; - -		$query = "SELECT * from {$CONFIG->dbprefix}entities where "; +		if ($owner_guid != "") {
 +			if (!is_array($owner_guid)) {
 +				$owner_guid = (int) $owner_guid; +				$where[] = "owner_guid = '$owner_guid'";
 +			} else if (sizeof($owner_guid) > 0) {
 +				// Cast every element to the owner_guid array to int
 +				$owner_guid = array_map("sanitise_int", $owner_guid);
 +				$owner_guid = implode(",",$owner_guid);
 +				$where[] = "owner_guid in ({$owner_guid})";
 +			}
 +		} +
 +		if (!$count) { +			$query = "SELECT * from {$CONFIG->dbprefix}entities where ";
 +		} else {
 +			$query = "select count(guid) as total from {$CONFIG->dbprefix}entities where ";
 +		}  		foreach ($where as $w)  			$query .= " $w and "; -		$query .= " (access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']}))"; // Add access controls -		$query .= " order by $order_by limit $offset, $limit"; // Add order and limit - -		$dt = get_data($query, "entity_row_to_elggstar"); -		
 -		return $dt; +		$query .= " (access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']}))"; // Add access controls
 +		if (!$count) {
 +			$query .= " order by $order_by";
 +			$query .= " limit $offset, $limit"; // Add order and limit +			$dt = get_data($query, "entity_row_to_elggstar");
 +			return $dt;
 +		} else {
 +			$total = get_data_row($query);
 +			return $total->total;
 +		}  	}  	/** diff --git a/engine/lib/users.php b/engine/lib/users.php index 68383d8e5..166225b9e 100644 --- a/engine/lib/users.php +++ b/engine/lib/users.php @@ -251,6 +251,16 @@  		public function getObjects($subtype="", $limit = 10, $offset = 0) { return get_user_objects($this->getGUID(), $subtype, $limit, $offset); }
  		/**
 +		 * Counts the number of ElggObjects owned by this user
 +		 *
 +		 * @param string $subtype The subtypes of the objects, if any
 +		 * @return int The number of ElggObjects
 +		 */
 +		public function countObjects($subtype = "") {
 +			return count_user_objects($this->getGUID(), $subtype);
 +		}
 +
 +		/**
  		 * Get the collections associated with a user.
  		 *
  		 * @param string $subtype Optionally, the subtype of result we want to limit to
 @@ -403,7 +413,7 @@  	function get_user_friends($user_guid, $subtype = "", $limit = 10, $offset = 0) {
  		return get_entities_from_relationship("friend",$user_guid,false,"user",$subtype,0,"time_created desc",$limit,$offset);
  	}
 -	
 +
  	/**
  	 * Obtains a list of objects owned by a user
  	 *
 @@ -419,6 +429,56 @@  	}
  	/**
 +	 * Counts the objects (optionally of a particular subtype) owned by a user
 +	 *
 +	 * @param int $user_guid The GUID of the owning user
 +	 * @param string $subtype Optionally, the subtype of objects
 +	 * @return int The number of objects the user owns (of this subtype)
 +	 */
 +	function count_user_objects($user_guid, $subtype = "") {
 +		$total = get_entities('object', $subtype, $user_guid, "time_created desc", null, null, true);
 +		return $total;
 +	}
 +	
 +/**
 +	 * Obtains a list of objects owned by a user's friends
 +	 *
 +	 * @param int $user_guid The GUID of the user to get the friends of
 +	 * @param string $subtype Optionally, the subtype of objects
 +	 * @param int $limit The number of results to return (default 10)
 +	 * @param int $offset Indexing offset, if any
 +	 * @return false|array An array of ElggObjects or false, depending on success
 +	 */
 +	function get_user_friends_objects($user_guid, $subtype = "", $limit = 10, $offset = 0) {
 +		if ($friends = get_user_friends($user_guid, $subtype, 999999, 0)) {
 +			$friendguids = array();
 +			foreach($friends as $friend) {
 +				$friendguids[] = $friend->getGUID();
 +			}
 +			return get_entities('object',$subtype,$friendguids, "time_created desc", $limit, $offset);
 +		}
 +		return false;
 +	}
 +	
 +	/**
 +	 * Counts the number of objects owned by a user's friends
 +	 *
 +	 * @param int $user_guid The GUID of the user to get the friends of
 +	 * @param string $subtype Optionally, the subtype of objects
 +	 * @return int The number of objects
 +	 */
 +	function count_user_friends_objects($user_guid, $subtype = "") {
 +		if ($friends = get_user_friends($user_guid, $subtype, 999999, 0)) {
 +			$friendguids = array();
 +			foreach($friends as $friend) {
 +				$friendguids[] = $friend->getGUID();
 +			}
 +			return get_entities('object',$subtype,$friendguids, "time_created desc", $limit, $offset, true);
 +		}
 +		return 0;
 +	}
 +	
 +	/**
  	 * Get a user object from a GUID.
  	 * 
  	 * This function returns an ElggUser from a given GUID.
  | 
