aboutsummaryrefslogtreecommitdiff
path: root/mod/search/start.php
blob: a434fc2b561c851a65e7cb449eebbf6675d67323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

	/**
	 * Elgg core search.
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @author Curverider Ltd <info@elgg.com>
	 * @link http://elgg.org/
	 */

	/**
	 * Initialise search helper functions.
	 *
	 */
	function search_init() {
		global $CONFIG;

		// page handler for search actions and results
		register_page_handler('search','search_page_handler');

		// hook into the search callback to use the metadata system (this is the part that will go away!)
		register_plugin_hook('search:entities', 'all', 'search_original_hook');

		// list of available search types should include our base parts
		register_plugin_hook('searchtypes', 'all', 'search_base_search_types_hook');

		// add in CSS for search elements
		extend_view('css', 'search/css');
	}
	
	/**
	 * Page handler for search
	 *
	 * @param array $page Page elements from pain page handler
	 */
	function search_page_handler($page) {
		global $CONFIG;
		
		if(!get_input('tag')) {
			set_input('tag', $page[0]);	
		}

		include_once($CONFIG->path . "mod/search/index.php");
	}

	/**
	 * Core search hook.
	 * Returns an object with two parts:
	 *    ->entities: an array of instantiated entities that have been decorated with 
	 *                volatile "search" data indicating what they matched. These are
	 *                the entities to be displayed to the user on this page.
	 *    ->total:    total number of entities overall. This function can update this
	 *                limit to ask for more pages in the pagination.
	 */
	function search_original_hook($hook, $type, $returnvalue, $params) {
	    	$tag = $params['tag'];
		$offset = $params['offset']; // starting page
		$limit = $params['limit']; // number per page
		$searchtype = $params['searchtype']; // the search type we're looking for
		$object_type = $params['object_type'];
		$subtype = $params['subtype'];
		$owner_guid = $params['owner_guid'];
		$tagtype = $params['tagtype'];
		
		$count = get_entities_from_metadata($tagtype, elgg_strtolower($tag), $object_type, $subtype, $owner_guid, $limit, $offset, "", 0, true); 
		$ents =  get_entities_from_metadata($tagtype, elgg_strtolower($tag), $object_type, $subtype, $owner_guid, $limit, $offset, "", 0, false);	


		/*
		 * Foreach entity
		 *	get the metadata keys
		 *	If the value matches, hang onto the key
		 *	add all the matched keys to VolatileData
		 *   This tells us *why* each entity matched
		 */
		foreach ($ents as $ent) {
		    	$metadata = get_metadata_for_entity($ent->getGUID());
			$matched = array();
			if ($metadata) {
				foreach ($metadata as $tuple) {
					if ($tag === $tuple->value) {
						// This is one of the matching elements
						$matched[] = $tuple->name;
					}
				}
				$ent->setVolatileData("search", $matched);
			}
		}

		// merge in our entities with any coming in from elsewhere
		$returnvalue->entities = array_merge($returnvalue->entities, $ents);

		// expand the total entity count if necessary
		if ($count > $returnvalue->total) {
		    	$returnvalue->total = $count;
		}

		return $returnvalue;
	}

        /**
	 * return our base search types
	 */
        function search_base_search_types_hook($hook, $type, $returnvalue, $params) {
	    if (!is_array($returnvalue)) {
		$returnvalue = array();
	    }

	    return $returnvalue;
	}

	/** Register init system event **/

	register_elgg_event_handler('init','system','search_init');

?>