aboutsummaryrefslogtreecommitdiff
path: root/mod/search/index.php
blob: d62e3be72c4fbcc847b25a27dfd42c5423e6b0e6 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php

// $search_type == all || entities || trigger plugin hook
$search_type = get_input('search_type', 'all');

// @todo there is a bug in get_input that makes variables have slashes sometimes.
$query = stripslashes(get_input('q', get_input('tag', '', FALSE), FALSE));

// get limit and offset.  override if on search dashboard, where only 2
// of each most recent entity types will be shown.
$limit = ($search_type == 'all') ? 2 : get_input('limit', 10);
$offset = ($search_type == 'all') ? 0 : get_input('offset', 0);

$entity_type = get_input('entity_type', NULL);
$entity_subtype = get_input('entity_subtype', NULL);
$owner_guid = get_input('owner_guid', NULL);
$friends = (int)get_input('friends', 0);

// set up search params
$params = array(
	'query' => $query,
	'offset' => $offset,
	'limit' => $limit,
	'search_type' => $search_type,
	'type' => $entity_type,
	'subtype' => $entity_subtype,
//	'tag_type' => $tag_type,
	'owner_guid' => $owner_guid,
//	'friends' => $friends
);

$results_html = '';
//$results_html .= elgg_view_title(elgg_echo('search:results')) . "<input type=\"text\" value=\"$query\" />";
$results_html .= elgg_view_title(elgg_echo('search:results'));
$types = get_registered_entity_types();
$custom_types = trigger_plugin_hook('search_types', 'get_types', $params, array());

// add submenu items for all and native types
// @todo should these maintain any existing type / subtype filters or reset?
$data = htmlspecialchars(http_build_query(array(
	'q' => $query,
	'entity_subtype' => $subtype,
	'entity_type' => $type,
	'owner_guid' => $owner_guid,
	'search_type' => 'all',
	'friends' => $friends
)));
$url = "{$CONFIG->wwwroot}pg/search/?$data";
add_submenu_item(elgg_echo('all'), $url);

foreach ($types as $type => $subtypes) {
	// @todo when using index table, can include result counts on each of these.
	if (is_array($subtypes) && count($subtypes)) {
		foreach ($subtypes as $subtype) {
			$label = "item:$type:$subtype";

			$data = htmlspecialchars(http_build_query(array(
				'q' => $query,
				'entity_subtype' => $subtype,
				'entity_type' => $type,
				'owner_guid' => $owner_guid,
				'search_type' => 'entities',
				'friends' => $friends
			)));

			$url = "{$CONFIG->wwwroot}pg/search/?$data";

			add_submenu_item(elgg_echo($label), $url);
		}
	} else {
		$label = "item:$type";

		$data = htmlspecialchars(http_build_query(array(
			'q' => $query,
			'entity_type' => $type,
			'owner_guid' => $owner_guid,
			'search_type' => 'entities',
			'friends' => $friends
		)));

		$url = "{$CONFIG->wwwroot}pg/search/?$data";

		add_submenu_item(elgg_echo($label), $url);
	}
}

// add submenu for custom searches
foreach ($custom_types as $type) {
	$label = "search_types:$type";

	$data = http_build_query(array(
		'q' => urlencode($query),
		'entity_subtype' => $entity_subtype,
		'entity_type' => urlencode($entity_type),
		'owner_guid' => urlencode($owner_guid),
		'search_type' => $type,
		'friends' => $friends
	));

	$url = "{$CONFIG->wwwroot}pg/search/?$data";

	add_submenu_item(elgg_echo($label), $url);
}


// check that we have an actual query
if (!$query) {
	$body .= "No query.";
	$layout = elgg_view_layout('two_column_left_sidebar', '', $body);
	page_draw($title, $layout);

	return;
}

if ($search_type == 'all' || $search_type == 'entities') {
	// to pass the correct search type to the views
	$params['search_type'] = 'entities';

	// foreach through types.
	// if a plugin returns FALSE for subtype ignore it.
	// if a plugin returns NULL or '' for subtype, pass to generic type search function.
	// if still NULL or '' or empty(array()) no results found. (== don't show??)
	foreach ($types as $type => $subtypes) {
		if ($search_type != 'all' && $entity_type != $type) {
			continue;
		}

		if (is_array($subtypes) && count($subtypes)) {
			foreach ($subtypes as $subtype) {
				// no need to search if we're not interested in these results
				// @todo when using index table, allow search to get full count.
				if ($search_type != 'all' && $entity_subtype != $subtype) {
					continue;
				}
				$params['subtype'] = $subtype;
				$params['type'] = $type;

				$entities = trigger_plugin_hook('search', "$type:$subtype", $params, NULL);
				if ($entities === FALSE) {
					// someone is saying not to display these types in searches.
					continue;
				} elseif (is_array($entities) && !count($entities)) {
					// no results, but results searched in hook.
				} elseif (!$entities) {
					// no results and not hooked.  use default type search.
					// don't change the params here, since it's really a different subtype.
					// Will be passed to elgg_get_entities().
					$results = trigger_plugin_hook('search', $type, $params, array());
				}

				if (is_array($results['entities']) && $results['count']) {
					$results_html .= search_get_listing_html($results['entities'], $results['count'], $params);
				}
			}
		}

		// pull in default type entities with no subtypes
		// @todo this might currently means "all entities regardless of subtype"
		$params['type'] = $type;
		$params['subtype'] = 0;

		$results = trigger_plugin_hook('search', $type, $params, array());
		if ($results === FALSE) {
			// someone is saying not to display these types in searches.
			continue;
		}

		if (is_array($results['entities']) && $results['count']) {
			$results_html .= search_get_listing_html($results['entities'], $results['count'], $params);
		}
	}
}

// call custom searches
if ($search_type != 'entities' || $search_type == 'all') {
	// get custom search types
	$types = trigger_plugin_hook('search_types', 'get_types', $params, array());

	if (is_array($types)) {
		foreach ($types as $type) {
			if ($search_type != 'all' && $search_type != $type) {
				continue;
			}

			$params['search_type'] = $type;
			unset($params['subtype']);

			$results = trigger_plugin_hook('search', $type, $params, array());

			if ($results === FALSE) {
				// someone is saying not to display these types in searches.
				continue;
			}

			if (is_array($results['entities']) && $results['count']) {
				$results_html .= search_get_listing_html($results['entities'], $results['count'], $params);
			}
		}
	}
}

//if ($search_type !== 'all') {
//	var_dump('here');
//	$entities = trigger_plugin_hook('search', $search_type, '', $return);
//}
/*

call search_section_start to display long bar with types and titles
call search

*/

if (!$results_html) {
	$body = elgg_echo('search:no_results');
} else {
	$body = $results_html;
}

$layout = elgg_view_layout('two_column_left_sidebar', '', $body);

page_draw($title, $layout);







return;


/** Main search page */

global $CONFIG;

$tag = get_input('tag');
$offset = get_input('offset', 0);
$viewtype = get_input('search_viewtype','list');
if ($viewtype == 'gallery') {
	$limit = get_input('limit', 12); // 10 items in list view
} else {
	$limit = get_input('limit', 10); // 12 items in gallery view
}
$searchtype = get_input('searchtype', 'all');
$type = get_input('type', '');
$subtype = get_input('subtype', '');
$owner_guid = get_input('owner_guid', '');
$tagtype = get_input('tagtype', '');
$friends = (int)get_input('friends', 0);
$title = sprintf(elgg_echo('searchtitle'), $tag);

if (substr_count($owner_guid, ',')) {
	$owner_guid_array = explode(',', $owner_guid);
} else {
	$owner_guid_array = $owner_guid;
}
if ($friends > 0) {
	if ($friends = get_user_friends($friends, '', 9999)) {
		$owner_guid_array = array();
		foreach($friends as $friend) {
			$owner_guid_array[] = $friend->guid;
		}
	} else {
		$owner_guid = -1;
	}
}

// Set up submenus
if ($types = get_registered_entity_types()) {
	foreach($types as $ot => $subtype_array) {
		if (is_array($subtype_array) && count($subtype_array)) {
			foreach($subtype_array as $object_subtype) {
				$label = 'item:' . $ot;
				if (!empty($object_subtype)) {
					$label .= ':' . $object_subtype;
				}

				$data = http_build_query(array(
					'tag' => urlencode($tag),
					'subtype' => $object_subtype,
					'type' => urlencode($ot),
					//'tagtype' => urlencode($md_type),
					'owner_guid' => urlencode($owner_guid)
				));

				$url = "{$CONFIG->wwwroot}pg/search/?$data";

				add_submenu_item(elgg_echo($label), $url);
			}
		}
	}

	$data = http_build_query(array(
		'tag' => urlencode($tag),
		'owner_guid' => urlencode($owner_guid)
	));

	add_submenu_item(elgg_echo('all'), "{$CONFIG->wwwroot}pg/search/?$data");
}

// pull in search types for external or aggregated searches.
if ($search_types = trigger_plugin_hook('search', 'types', '', NULL, array())) {

}

$body = '';
if (!empty($tag)) {
	// start with blank results.
	$results = array(
		'entities' => array(),
		'total' => 0
	);

	// do the actual searchts
	$params = array(
		'tag' => $tag,
		'offset' => $offset,
		'limit' => $limit,
		'searchtype' => $searchtype,
		'type' => $type,
		'subtype' => $subtype,
		'tagtype' => $tagtype,
		'owner_guid' => $owner_guid_array
	);

	$results = trigger_plugin_hook('search', 'entities', $params, $results);

	if (empty($type) && empty($subtype)) {
		$title = sprintf(elgg_echo('searchtitle'),$tag);
	} else {
		if (empty($type)) {
			$type = 'object';
		}
		$itemtitle = 'item:' . $type;
		if (!empty($subtype)) {
			$itemtitle .= ':' . $subtype;
		}
		$itemtitle = elgg_echo($itemtitle);
		$title = sprintf(elgg_echo('advancedsearchtitle'),$itemtitle,$tag);
	}

	$body .= elgg_view_title($title); // elgg_view_title(sprintf(elgg_echo('searchtitle'),$tag));

	// call the old (now-deprecated) search hook here
	$body .= trigger_plugin_hook('search','',$tag, '');

	$body .= elgg_view('search/startblurb', array('query' => $query));

	if ($results->total > 0) {
		$body .= elgg_view('search/entity_list', array(
			'entities' => $results->entities,
			'count' => $results->total,
			'offset' => $offset,
			'limit' => $limit,
			'baseurl' => $_SERVER['REQUEST_URI'],
			'fullview' => false,
			'context' => 'search',
			'viewtypetoggle' => true,
			'viewtype' => $viewtype,
			'pagination' => true
		));
	} else {
		$body .= elgg_view('page_elements/contentwrapper', array('body' => elgg_echo('search:noresults')));
	}

	elgg_view_entity_list($results->entities, count($results->entities), 0, count($results->entities), false);
} else {
	// if no tag was given, give the user a box to input a search term
	$body .= elgg_view_title(elgg_echo('search:enterterm'));
	$body .= elgg_view('page_elements/contentwrapper', array('body' => '<div>' . elgg_view('page_elements/searchbox') . '</div>'));
}

$layout = elgg_view_layout('two_column_left_sidebar','',$body);

page_draw($title, $layout);