From 11f62409f3e96e36d1e0ff6907439e850ebb109f Mon Sep 17 00:00:00 2001 From: ben Date: Wed, 16 Apr 2008 15:03:15 +0000 Subject: Added the getURL and getOwnerUser methods to ElggEntity (gets the URL to display the current entity and the ElggUser entity that represents the owner respectively) git-svn-id: https://code.elgg.org/elgg/trunk@475 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'engine/lib/entities.php') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 2cbdc2e73..2752b4a08 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -281,6 +281,13 @@ * @todo document me */ public function getOwner() { return $this->get('owner_guid'); } + + /** + * Returns the actual entity of the user who owns this entity, if any + * + * @return ElggEntity The owning user + */ + public function getOwnerUser() { return get_entity($this->get('owner_guid')); } /** * Enter description here... @@ -313,6 +320,13 @@ * @todo document me */ public function getTimeUpdated() { return $this->get('time_updated'); } + + /** + * Gets the display URL for this entity + * + * @return string The URL + */ + public function getURL() { return get_entity_url($this->getGUID()); } /** * Save generic attributes to the entities table. @@ -901,6 +915,73 @@ } + /** + * Gets the URL for an entity, given a particular GUID + * + * @param int $entity_guid The GUID of the entity + * @return string The URL of the entity + */ + function get_entity_url($entity_guid) { + + global $CONFIG; + if ($entity = get_entity($entity_guid)) { + + $url = ""; + + if (isset($CONFIG->entity_url_handler[$entity->getType()][$entity->getSubType()])) { + $function = $CONFIG->entity_url_handler[$entity->getType()][$entity->getSubType()]; + if (is_callable($function)) { + $url = $function($entity); + } + } + if (isset($CONFIG->entity_url_handler[$entity->getType()]['all'])) { + $function = $CONFIG->entity_url_handler[$entity->getType()]['all']; + if (is_callable($function)) { + $url = $function($entity); + } + } + if (isset($CONFIG->entity_url_handler['all']['all'])) { + $function = $CONFIG->entity_url_handler['all']['all']; + if (is_callable($function)) { + $url = $function($entity); + } + } + + if ($url == "") { + $url = $CONFIG->url . "view/" . $entity_guid; + } + return $url; + + } + return false; + + } + + /** + * Sets the URL handler for a particular entity type and subtype + * + * @param string $function_name The function to register + * @param string $entity_type The entity type + * @param string $entity_subtype The entity subtype + * @return true|false Depending on success + */ + function register_entity_url_handler($function_name, $entity_type = "all", $entity_subtype = "all") { + global $CONFIG; + + if (!is_callable($function_name)) return false; + + if (!isset($CONFIG->entity_url_handler)) { + $CONFIG->entity_url_handler = array(); + } + if (!isset($CONFIG->entity_url_handler[$entity_type])) { + $CONFIG->entity_url_handler[$entity_type] = array(); + } + $CONFIG->entity_url_handler[$entity_type][$entity_subtype] = $function_name; + + return true; + + } + /** * Page handler for generic entities view system * -- cgit v1.2.3