From ac8dc55c78d5f64b91a1eeda023cd1caff900511 Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 12 Feb 2009 12:53:32 +0000 Subject: Alternative river system trial. This is a test. git-svn-id: https://code.elgg.org/elgg/trunk@2723 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/river2.php | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 engine/lib/river2.php (limited to 'engine/lib/river2.php') diff --git a/engine/lib/river2.php b/engine/lib/river2.php new file mode 100644 index 000000000..577cefc16 --- /dev/null +++ b/engine/lib/river2.php @@ -0,0 +1,252 @@ +access_id; + + $type = $object->getType(); + $subtype = $object->getSubtype(); + + $action_type = sanitise_string($action_type); + + // Load config + global $CONFIG; + + // Attempt to save river item; return success status + return insert_data("insert into {$CONFIG->dbprefix}river " . + " set type = '{$type}', " . + " subtype = '{$subtype}', " . + " action_type = '{$action_type}', " . + " access_id = {$access_id}, " . + " view = '{$view}', " . + " subject_guid = {$subject_guid}, " . + " object_guid = {$object_guid}, " . + " posted = {$posted} "); + + } + + /** + * Removes all items relating to a particular acting entity from the river + * + * @param int $subject_guid The GUID of the entity + * @return true|false Depending on success + */ + function remove_from_river_by_subject( + $subject_guid + ) { + + // Sanitise + $subject_guid = (int) $subject_guid; + + // Load config + global $CONFIG; + + // Remove + return delete_data("delete from {$CONFIG->dbprefix}river where subject_guid = {$subject_guid}"); + + } + + /** + * Removes all items relating to a particular entity being acted upon from the river + * + * @param int $object_guid The GUID of the entity + * @return true|false Depending on success + */ + function remove_from_river_by_object( + $object_guid + ) { + + // Sanitise + $object_guid = (int) $object_guid; + + // Load config + global $CONFIG; + + // Remove + return delete_data("delete from {$CONFIG->dbprefix}river where object_guid = {$object_guid}"); + + } + + /** + * Retrieves items from the river. All parameters are optional. + * + * @param int|array $subject_guid Acting entity to restrict to. Default: all + * @param int|array $object_guid Entity being acted on to restrict to. Default: all + * @param string $subject_relationship If set to a relationship type, this will use $subject_guid as the starting point and set the subjects to be all users this entity has this relationship with (eg 'friend'). Default: blank + * @param string $type The type of entity to restrict to. Default: all + * @param string $subtype The subtype of entity to restrict to. Default: all + * @param string $action_type The type of river action to restrict to. Default: all + * @param int $limit The number of items to retrieve. Default: 20 + * @param int $offset The page offset. Default: 0 + * @param int $posted_min The minimum time period to look at. Default: none + * @param int $posted_max The maximum time period to look at. Default: none + * @return array|false Depending on success + */ + function get_river_items( + $subject_guid = 0, + $object_guid = 0, + $subject_relationship = '', + $type = '', + $subtype = '', + $action_type = '', + $limit = 20, + $offset = 0, + $posted_min = 0, + $posted_max = 0 + ) { + + // Sanitise variables + if (!is_array($subject_guid)) { + $subject_guid = (int) $subject_guid; + } else { + foreach($subject_guid as $key => $temp) { + $subject_guid[$key] = (int) $temp; + } + } + if (!is_array($object_guid)) { + $object_guid = (int) $object_guid; + } else { + foreach($object_guid as $key => $temp) { + $object_guid[$key] = (int) $temp; + } + } + if (!empty($type)) $action_type = sanitise_string($type); + if (!empty($subtype)) $action_type = sanitise_string($subtype); + if (!empty($action_type)) $action_type = sanitise_string($action_type); + $limit = (int) $limit; + $offset = (int) $offset; + $posted_min = (int) $posted_min; + $posted_max = (int) $posted_max; + + // Construct 'where' clauses for the river + $where = array(); + $where[] = get_access_sql_suffix(); + + if (empty($subject_relationship)) { + if (!empty($subject_guid)) + if (!is_array($subject_guid)) { + $where[] = " subject_guid = {$subject_guid} "; + } else { + $where[] = " subject_guid in (" . implode(',',$subject_guid) . ") "; + } + } else { + if (!is_array($subject_guid)) + if ($entities = get_entities_from_relationship($subject_relationship,$subject_guid,false,'','',0,'',9999)) { + $guids = array(); + foreach($entities as $entity) $guids[] = (int) $entity->guid; + $guids[] = $subject_guid; + $where[] = " subject_guid in (" . implode(',',$guids) . ") "; + } + } + if (!empty($object_guid)) + if (!is_array($object_guid)) { + $where[] = " object_guid = {$object_guid} "; + } else { + $where[] = " object_guid in (" . implode(',',$object_guid) . ") "; + } + if (!empty($type)) $where[] = " type = '{$type}' "; + if (!empty($subtype)) $where[] = " subtype = '{$subtype}' "; + if (!empty($action_type)) $where[] = " action_type = '{$action_type}' "; + if (!empty($posted_min)) $where[] = " posted > {$posted_min} "; + if (!empty($posted_max)) $where[] = " posted < {$posted_max} "; + + $whereclause = implode(' and ', $where); + + // Construct main SQL + $sql = "select id,type,subtype,action_type,access_id,view,subject_guid,object_guid,posted from {$CONFIG->dbprefix}river where {$whereclause} limit {$offset},{$limit}"; + + // Get data + return get_data($sql); + + } + + /** + * Returns a human-readable representation of a river item + * + * @see get_river_items + * + * @param stdClass $item A river item object as returned from get_river_items + * @return string|false Depending on success + */ + function elgg_view_river_item($item) { + if (isset($item->view)) { + + if (elgg_view_exists($item->view)) { + $body = elgg_view($item->view,array( + 'item' => $item + )); + } + return elgg_view('river/item/wrapper',array( + 'item' => $item, + 'body' => $body + )); + + } + return false; + } + + function elgg_view_river_items($subject_guid = 0, + $object_guid = 0, + $subject_relationship = '', + $type = '', + $subtype = '', + $action_type = '', + $limit = 20, + $posted_min = 0, + $posted_max = 0) { + + // Get input from outside world and sanitise it + $offset = (int) get_input('offset',0); + + // Get river items, if they exist + if ($riveritems = get_river_items($subject_guid,$object_guid,$subject_relationship,$type,$subtype,$action_type,$limit,$offset,$posted_min,$posted_max)) { + + return elgg_view('river/item/list',array( + 'limit' => $limit, + 'offset' => $offset, + 'items' => $riveritems, + )); + + } + + return ''; + + } + +?> \ No newline at end of file -- cgit v1.2.3