Skip to content

Commit

Permalink
feature(search): search hooks now preserve custom joins and wheres
Browse files Browse the repository at this point in the history
Search hooks can now be triggered with custom joins and wheres. Previously, those
would always be replaced for object, group and user searches.
  • Loading branch information
hypeJunction committed Dec 21, 2015
1 parent e47785f commit 6504161
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions mod/search/search_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
*/
function search_objects_hook($hook, $type, $value, $params) {

$params['joins'] = (array) elgg_extract('joins', $params, array());
$params['wheres'] = (array) elgg_extract('wheres', $params, array());

$db_prefix = elgg_get_config('dbprefix');

$join = "JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid";
$params['joins'] = array($join);
$fields = array('title', 'description');
array_unshift($params['joins'], $join);

$fields = array('title', 'description');
$where = search_get_where_sql('oe', $fields, $params);

$params['wheres'] = array($where);
$params['wheres'][] = $where;

$params['count'] = TRUE;
$count = elgg_get_entities($params);

Expand Down Expand Up @@ -64,18 +67,20 @@ function search_objects_hook($hook, $type, $value, $params) {
* @return array
*/
function search_groups_hook($hook, $type, $value, $params) {

$params['joins'] = (array) elgg_extract('joins', $params, array());
$params['wheres'] = (array) elgg_extract('wheres', $params, array());

$db_prefix = elgg_get_config('dbprefix');

$query = sanitise_string($params['query']);

$join = "JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid";
$params['joins'] = array($join);
array_unshift($params['joins'], $join);

$fields = array('name', 'description');

$where = search_get_where_sql('ge', $fields, $params);

$params['wheres'] = array($where);
$params['wheres'][] = $where;

// override subtype -- All groups should be returned regardless of subtype.
$params['subtype'] = ELGG_ENTITIES_ANY_VALUE;
Expand Down Expand Up @@ -119,13 +124,16 @@ function search_groups_hook($hook, $type, $value, $params) {
* @return array
*/
function search_users_hook($hook, $type, $value, $params) {

$params['joins'] = (array) elgg_extract('joins', $params, array());
$params['wheres'] = (array) elgg_extract('wheres', $params, array());

$db_prefix = elgg_get_config('dbprefix');

$query = sanitise_string($params['query']);

$params['joins'] = array(
"JOIN {$db_prefix}users_entity ue ON e.guid = ue.guid",
);
$join = "JOIN {$db_prefix}users_entity ue ON e.guid = ue.guid";
array_unshift($params['joins'], $join);

// username and display name
$fields = array('username', 'name');
Expand All @@ -149,9 +157,9 @@ function search_users_hook($hook, $type, $value, $params) {
// $md_where .= " AND " . search_get_where_sql('msv', array('string'), $params, FALSE);
$md_where = "(({$clauses['wheres'][0]}) AND msv.string LIKE '%$query%')";

$params['wheres'] = array("(($where) OR ($md_where))");
$params['wheres'][] = "(($where) OR ($md_where))";
} else {
$params['wheres'] = array("$where");
$params['wheres'][] = "$where";
}

// override subtype -- All users should be returned regardless of subtype.
Expand Down Expand Up @@ -220,6 +228,10 @@ function search_users_hook($hook, $type, $value, $params) {
* @return array
*/
function search_tags_hook($hook, $type, $value, $params) {

$params['joins'] = (array) elgg_extract('joins', $params, array());
$params['wheres'] = (array) elgg_extract('wheres', $params, array());

$db_prefix = elgg_get_config('dbprefix');

$valid_tag_names = elgg_get_registered_tag_metadata_names();
Expand Down

0 comments on commit 6504161

Please sign in to comment.