Пример #1
0
/**
 * Return default results for searches on users.
 *
 * @param string       $hook   name of hook
 * @param string       $type   type of hook
 * @param unknown_type $value  current value
 * @param array        $params parameters
 *
 * @return array
 */
function search_advanced_users_hook($hook, $type, $value, $params)
{
    if (!empty($value)) {
        return;
    }
    if (elgg_get_plugin_setting('search_hooks_enabled', 'search_advanced', 'yes') == 'no') {
        return;
    }
    $return_only_count = elgg_extract('count', $params, false);
    $db_prefix = elgg_get_config('dbprefix');
    $params['joins'] = ["JOIN {$db_prefix}users_entity ue ON e.guid = ue.guid"];
    if (isset($params["container_guid"])) {
        $container_entity = get_entity($params["container_guid"]);
    }
    if (isset($container_entity) && $container_entity instanceof ElggGroup) {
        // check for group membership relation
        $params["relationship"] = "member";
        $params["relationship_guid"] = $params["container_guid"];
        $params["inverse_relationship"] = false;
        unset($params["container_guid"]);
    } else {
        // check for site relation ship
        $params["relationship"] = "member_of_site";
        $params["relationship_guid"] = elgg_get_site_entity()->getGUID();
        $params["inverse_relationship"] = true;
    }
    $i = 0;
    if (!empty($params["query"])) {
        $query_parts = (array) search_advanced_tag_query_to_array($params['query']);
        if (empty($query_parts)) {
            return ['entities' => [], 'count' => 0];
        }
        $fields = array('username', 'name');
        $where = search_advanced_get_where_sql('ue', $fields, $params, FALSE);
        // profile fields
        $profile_fields = array_keys(elgg_get_config('profile_fields'));
        if (!empty($profile_fields)) {
            $profile_field_metadata_search_values = elgg_get_plugin_setting("user_profile_fields_metadata_search", "search_advanced", array());
            if (!empty($profile_field_metadata_search_values)) {
                $profile_field_metadata_search_values = json_decode($profile_field_metadata_search_values, true);
            }
            foreach ($profile_fields as $key => $field) {
                if (in_array($field, $profile_field_metadata_search_values)) {
                    unset($profile_fields[$key]);
                }
            }
            $tag_name_ids = search_advanced_get_metastring_ids($profile_fields);
            if (!empty($tag_name_ids)) {
                $likes = [];
                foreach ($query_parts as $query_value) {
                    $query_value = sanitise_string($query_value);
                    if (!empty($query_value)) {
                        $likes[] = "msv{$i}.string LIKE '%{$query_value}%'";
                    }
                }
                $params["joins"][] = "JOIN {$db_prefix}metadata md{$i} on e.guid = md{$i}.entity_guid";
                $params["joins"][] = "JOIN {$db_prefix}metastrings msv{$i} ON md{$i}.value_id = msv{$i}.id";
                $md_where = "((md{$i}.name_id IN (" . implode(",", $tag_name_ids) . ")) AND (" . implode(" OR ", $likes) . "))";
                $where = "(({$where}) OR ({$md_where}))";
                $i++;
            }
        }
        $params['wheres'] = array($where);
    }
    // additional filters
    $search_filter = elgg_extract('search_filter', $params);
    if (!empty($search_filter)) {
        // on profile fields?
        $profile_fields = elgg_extract('profile_fields', $search_filter);
        if (!empty($profile_fields)) {
            $profile_field_likes = [];
            // use soundex on some fields
            $profile_soundex = elgg_extract('profile_fields_soundex', $search_filter);
            foreach ($profile_fields as $field_name => $field_value) {
                $field_value = trim(sanitise_string($field_value));
                if (!empty($field_value)) {
                    $tag_name_id = elgg_get_metastring_id($field_name);
                    $params["joins"][] = "JOIN {$db_prefix}metadata md{$i} on e.guid = md{$i}.entity_guid";
                    $params["joins"][] = "JOIN {$db_prefix}metastrings msv{$i} ON md{$i}.value_id = msv{$i}.id";
                    // do a soundex match
                    if (is_array($profile_soundex) && in_array($field_name, $profile_soundex)) {
                        $profile_field_likes[] = "md{$i}.name_id = {$tag_name_id} AND soundex(CONCAT('X', msv{$i}.string)) = soundex(CONCAT('X','{$field_value}'))";
                    } else {
                        $profile_field_likes[] = "md{$i}.name_id = {$tag_name_id} AND msv{$i}.string LIKE '%{$field_value}%'";
                    }
                    $i++;
                }
            }
            if (!empty($profile_field_likes)) {
                $profile_field_where = "(" . implode(" AND ", $profile_field_likes) . ")";
                if (empty($params["wheres"])) {
                    $params["wheres"] = array($profile_field_where);
                } else {
                    $params["wheres"] = array($params["wheres"][0] . " AND " . $profile_field_where);
                }
            }
        }
    }
    if (empty($params['wheres'])) {
        return ['entities' => [], 'count' => 0];
    }
    $wheres = (array) elgg_extract("wheres", $params);
    $wheres[] = "ue.banned = 'no'";
    $params["wheres"] = $wheres;
    // override subtype -- All users should be returned regardless of subtype.
    $params['subtype'] = ELGG_ENTITIES_ANY_VALUE;
    $params['count'] = true;
    $count = elgg_get_entities_from_relationship($params);
    // no need to continue if nothing here.
    if (!$count || $return_only_count) {
        return ['entities' => [], 'count' => $count];
    }
    $params['count'] = false;
    $entities = elgg_get_entities_from_relationship($params);
    $query = sanitise_string($params['query']);
    // add the volatile data for why these entities have been returned.
    foreach ($entities as $entity) {
        $name = search_get_highlighted_relevant_substrings($entity->name, $query);
        $entity->setVolatileData('search_matched_title', $name);
        $username = search_get_highlighted_relevant_substrings($entity->username, $query);
        $entity->setVolatileData('search_matched_description', $username);
    }
    return ['entities' => $entities, 'count' => $count];
}
Пример #2
0
         $combined_count = $results['count'];
         $results['count'] = 0;
         $results_html .= elgg_view($view, array('results' => $results, 'params' => $current_params));
         if ($combined_count > $current_params['limit']) {
             $results_html .= '<hr /><div class="elgg-subtext">' . elgg_echo('theme_haarlem_intranet:search:more') . '</div><hr />';
         }
     }
 }
 // determine menu counters
 $db_prefix = elgg_get_config('dbprefix');
 $count_query = "SELECT es.subtype, count(distinct e.guid) as total";
 $count_query .= " FROM {$db_prefix}entities e";
 $count_query .= " JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid";
 $count_query .= " JOIN {$db_prefix}entity_subtypes es ON e.subtype = es.id";
 $fields = array('title', 'description');
 $where = search_advanced_get_where_sql('oe', $fields, $params);
 // add tags search
 if ($valid_tag_names = elgg_get_registered_tag_metadata_names()) {
     $tag_name_ids = array();
     foreach ($valid_tag_names as $tag_name) {
         $tag_name_ids[] = add_metastring($tag_name);
     }
     $count_query .= " JOIN {$db_prefix}metadata md on e.guid = md.entity_guid";
     $count_query .= " JOIN {$db_prefix}metastrings msv ON md.value_id = msv.id";
     $md_where = "((md.name_id IN (" . implode(",", $tag_name_ids) . ")) AND msv.string = '" . sanitise_string($params["query"]) . "')";
 }
 // add wheres
 $count_query .= " WHERE e.type = 'object' AND es.subtype IN ('" . implode("', '", $current_params['subtype']) . "') AND ";
 if ($container_guid) {
     $count_query .= "e.container_guid = " . $container_guid . " AND ";
 }
Пример #3
0
/**
 * Return default results for searches on users.
 *
 * @todo add profile field MD searching
 *
 * @param string       $hook   name of hook
 * @param string       $type   type of hook
 * @param unknown_type $value  current value
 * @param array        $params parameters
 *
 * @return array
 */
function search_advanced_users_hook($hook, $type, $value, $params)
{
    $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 {$db_prefix}metadata md on e.guid = md.entity_guid", "JOIN {$db_prefix}metastrings msv ON md.value_id = msv.id");
    if (isset($params["container_guid"])) {
        $entity = get_entity($params["container_guid"]);
    }
    if (isset($entity) && $entity instanceof ElggGroup) {
        // check for group membership relation
        $params["relationship"] = "member";
        $params["relationship_guid"] = $params["container_guid"];
        $params["inverse_relationship"] = TRUE;
        unset($params["container_guid"]);
    } else {
        // check for site relation ship
        if (empty($_SESSION["search_advanced:multisite"])) {
            $params["relationship"] = "member_of_site";
            $params["relationship_guid"] = elgg_get_site_entity()->getGUID();
            $params["inverse_relationship"] = TRUE;
        }
    }
    if (!empty($params["query"])) {
        $fields = array('username', 'name');
        $where = search_advanced_get_where_sql('ue', $fields, $params, FALSE);
        // profile fields
        $profile_fields = array_keys(elgg_get_config('profile_fields'));
        if ($profile_fields) {
            $profile_field_metadata_search_values = elgg_get_plugin_setting("user_profile_fields_metadata_search", "search_advanced", array());
            if (!empty($profile_field_metadata_search_values)) {
                $profile_field_metadata_search_values = json_decode($profile_field_metadata_search_values, true);
            }
            $tag_name_ids = array();
            foreach ($profile_fields as $field) {
                if (!in_array($field, $profile_field_metadata_search_values)) {
                    $tag_name_ids[] = elgg_get_metastring_id($field);
                }
            }
            if (!empty($tag_name_ids)) {
                $likes = array();
                if (elgg_get_plugin_setting("enable_multi_tag", "search_advanced") == "yes") {
                    $separator = elgg_get_plugin_setting("multi_tag_separator", "search_advanced", "comma");
                    if ($separator == "comma") {
                        $query_array = explode(",", $query);
                    } else {
                        $query_array = explode(" ", $query);
                    }
                    foreach ($query_array as $query_value) {
                        $query_value = trim($query_value);
                        if (!empty($query_value)) {
                            $likes[] = "msv.string LIKE '%{$query_value}%'";
                        }
                    }
                } else {
                    $likes[] = "msv.string LIKE '%{$query}%'";
                }
                $md_where = "((md.name_id IN (" . implode(",", $tag_name_ids) . ")) AND (" . implode(" OR ", $likes) . "))";
                $where = "(({$where}) OR ({$md_where}))";
            }
        }
        $params['wheres'] = array($where);
    }
    $profile_fields = $params["profile_filter"];
    if (!empty($profile_fields)) {
        $profile_field_likes = array();
        $profile_soundex = $params["profile_soundex"];
        $i = 0;
        foreach ($profile_fields as $field_name => $field_value) {
            $field_value = trim(sanitise_string($field_value));
            if (!empty($field_value)) {
                $tag_name_id = elgg_get_metastring_id($field_name);
                $i++;
                if ($i > 1) {
                    $params["joins"][] = "JOIN {$db_prefix}metadata md{$i} on e.guid = md{$i}.entity_guid";
                    $params["joins"][] = "JOIN {$db_prefix}metastrings msv{$i} ON md{$i}.value_id = msv{$i}.id";
                }
                // do a soundex match
                if (is_array($profile_soundex) && in_array($field_name, $profile_soundex)) {
                    if ($i > 1) {
                        $profile_field_likes[] = "md{$i}.name_id = {$tag_name_id} AND soundex(CONCAT('X', msv{$i}.string)) = soundex(CONCAT('X','{$field_value}'))";
                    } else {
                        $profile_field_likes[] = "md.name_id = {$tag_name_id} AND soundex(CONCAT('X', msv.string)) = soundex(CONCAT('X', '{$field_value}'))";
                    }
                } else {
                    if ($i > 1) {
                        $profile_field_likes[] = "md{$i}.name_id = {$tag_name_id} AND msv{$i}.string LIKE '%{$field_value}%'";
                    } else {
                        $profile_field_likes[] = "md.name_id = {$tag_name_id} AND msv.string LIKE '%{$field_value}%'";
                    }
                }
            }
        }
        if (!empty($profile_field_likes)) {
            $profile_field_where = "(" . implode(" AND ", $profile_field_likes) . ")";
            if (empty($params["wheres"])) {
                $params["wheres"] = array($profile_field_where);
            } else {
                $params["wheres"] = array($params["wheres"][0] . " AND " . $profile_field_where);
            }
        }
    }
    $wheres = (array) elgg_extract("wheres", $params);
    $wheres[] = "ue.banned = 'no'";
    $params["wheres"] = $wheres;
    // override subtype -- All users should be returned regardless of subtype.
    $params['subtype'] = ELGG_ENTITIES_ANY_VALUE;
    $params['count'] = TRUE;
    $count = elgg_get_entities_from_relationship($params);
    // no need to continue if nothing here.
    if (!$count || $params["search_advanced_count_only"] == true) {
        return array('entities' => array(), 'count' => $count);
    }
    $params['count'] = FALSE;
    $entities = elgg_get_entities_from_relationship($params);
    // add the volatile data for why these entities have been returned.
    foreach ($entities as $entity) {
        $username = search_get_highlighted_relevant_substrings($entity->username, $query);
        $entity->setVolatileData('search_matched_title', $username);
        $name = search_get_highlighted_relevant_substrings($entity->name, $query);
        $entity->setVolatileData('search_matched_description', $name);
    }
    return array('entities' => $entities, 'count' => $count);
}