/** * Returns entities based upon metadata. Also accepts all * options available to elgg_get_entities(). Supports * the singular option shortcut. * * NB: Using metadata_names and metadata_values results in a * "names IN (...) AND values IN (...)" clause. This is subtly * differently than default multiple metadata_name_value_pairs, which use * "(name = value) AND (name = value)" clauses. * * When in doubt, use name_value_pairs. * * @see elgg_get_entities * * @param array $options Array in format: * * metadata_names => NULL|ARR metadata names * * metadata_values => NULL|ARR metadata values * * metadata_name_value_pairs => NULL|ARR ( * name => 'name', * value => 'value', * 'operand' => '=', * 'case_sensitive' => TRUE * ) * Currently if multiple values are sent via * an array (value => array('value1', 'value2') * the pair's operand will be forced to "IN". * * metadata_name_value_pairs_operator => NULL|STR The operator to use for combining * (name = value) OPERATOR (name = value); default AND * * metadata_case_sensitive => BOOL Overall Case sensitive * * order_by_metadata => NULL|ARR array( * 'name' => 'metadata_text1', * 'direction' => ASC|DESC, * 'as' => text|integer * ) * Also supports array('name' => 'metadata_text1') * * metadata_owner_guids => NULL|ARR guids for metadata owners * * @return mixed If count, int. If not count, array. false on errors. * @since 1.7.0 */ function elgg_get_entities_from_metadata(array $options = array()) { $defaults = array('metadata_names' => ELGG_ENTITIES_ANY_VALUE, 'metadata_values' => ELGG_ENTITIES_ANY_VALUE, 'metadata_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE, 'metadata_name_value_pairs_operator' => 'AND', 'metadata_case_sensitive' => TRUE, 'order_by_metadata' => array(), 'metadata_owner_guids' => ELGG_ENTITIES_ANY_VALUE); $options = array_merge($defaults, $options); $singulars = array('metadata_name', 'metadata_value', 'metadata_name_value_pair', 'metadata_owner_guid'); $options = elgg_normalise_plural_options_array($options, $singulars); if (!($options = elgg_entities_get_metastrings_options('metadata', $options))) { return FALSE; } return elgg_get_entities($options); }
/** * Get users that match the search parameters. * * Searches on username, display name, and profile fields * * @param string $hook Hook name * @param string $type Hook type * @param array $value Empty array * @param array $params Search parameters * @return array */ function search_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 n_table.value_id = msv.id"); // username and display name $fields = array('username', 'name'); $where = search_get_where_sql('ue', $fields, $params, FALSE); // profile fields $profile_fields = array_keys(elgg_get_config('profile_fields')); // get the where clauses for the md names // can't use egef_metadata() because the n_table join comes too late. $clauses = elgg_entities_get_metastrings_options('metadata', array('metadata_names' => $profile_fields)); $params['joins'] = array_merge($clauses['joins'], $params['joins']); // no fulltext index, can't disable fulltext search in this function. // $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}))"); // override subtype -- All users should be returned regardless of subtype. $params['subtype'] = ELGG_ENTITIES_ANY_VALUE; $params['count'] = true; $count = elgg_get_entities($params); // no need to continue if nothing here. if (!$count) { return array('entities' => array(), 'count' => $count); } $params['count'] = FALSE; $params['order_by'] = search_get_order_by_sql('e', 'ue', $params['sort'], $params['order']); $entities = elgg_get_entities($params); // add the volatile data for why these entities have been returned. foreach ($entities as $entity) { $title = search_get_highlighted_relevant_substrings($entity->name, $query); // include the username if it matches but the display name doesn't. if (false !== strpos($entity->username, $query)) { $username = search_get_highlighted_relevant_substrings($entity->username, $query); $title .= " ({$username})"; } $entity->setVolatileData('search_matched_title', $title); $matched = ''; foreach ($profile_fields as $md_name) { $metadata = $entity->{$md_name}; if (is_array($metadata)) { foreach ($metadata as $text) { if (stristr($text, $query)) { $matched .= elgg_echo("profile:{$md_name}") . ': ' . search_get_highlighted_relevant_substrings($text, $query); } } } else { if (stristr($metadata, $query)) { $matched .= elgg_echo("profile:{$md_name}") . ': ' . search_get_highlighted_relevant_substrings($metadata, $query); } } } $entity->setVolatileData('search_matched_description', $matched); } return array('entities' => $entities, 'count' => $count); }
/** * Returns entities based upon annotations. Also accepts all options available * to elgg_get_entities() and elgg_get_entities_from_metadata(). * * Entity creation time is selected as maxtime. To sort based upon * this, pass 'order_by' => 'maxtime asc' || 'maxtime desc' * * @see elgg_get_entities * @see elgg_get_entities_from_metadata * * @param array $options Array in format: * * annotation_names => NULL|ARR annotations names * * annotation_values => NULL|ARR annotations values * * annotation_name_value_pairs => NULL|ARR (name = 'name', value => 'value', * 'operator' => '=', 'case_sensitive' => TRUE) entries. * Currently if multiple values are sent via an array (value => array('value1', 'value2') * the pair's operator will be forced to "IN". * * annotation_name_value_pairs_operator => NULL|STR The operator to use for combining * (name = value) OPERATOR (name = value); default AND * * annotation_case_sensitive => BOOL Overall Case sensitive * * order_by_annotation => NULL|ARR (array('name' => 'annotation_text1', 'direction' => ASC|DESC, * 'as' => text|integer), * * Also supports array('name' => 'annotation_text1') * * annotation_owner_guids => NULL|ARR guids for annotaiton owners * * annotation_ids => NULL|ARR Annotation IDs * * @return mixed If count, int. If not count, array. false on errors. * @since 1.7.0 */ function elgg_get_entities_from_annotations(array $options = array()) { $defaults = array('annotation_names' => ELGG_ENTITIES_ANY_VALUE, 'annotation_values' => ELGG_ENTITIES_ANY_VALUE, 'annotation_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE, 'annotation_name_value_pairs_operator' => 'AND', 'annotation_case_sensitive' => TRUE, 'order_by_annotation' => array(), 'annotation_created_time_lower' => ELGG_ENTITIES_ANY_VALUE, 'annotation_created_time_upper' => ELGG_ENTITIES_ANY_VALUE, 'annotation_owner_guids' => ELGG_ENTITIES_ANY_VALUE, 'annotation_ids' => ELGG_ENTITIES_ANY_VALUE, 'order_by' => 'maxtime desc', 'group_by' => 'a.entity_guid'); $options = array_merge($defaults, $options); $singulars = array('annotation_name', 'annotation_value', 'annotation_name_value_pair', 'annotation_owner_guid', 'annotation_id'); $options = elgg_normalise_plural_options_array($options, $singulars); if (!($options = elgg_entities_get_metastrings_options('annotation', $options))) { return FALSE; } // special sorting for annotations //@todo overrides other sorting $options['selects'][] = "max(n_table.time_created) as maxtime"; $options['group_by'] = 'n_table.entity_guid'; $time_wheres = elgg_get_entity_time_where_sql('a', $options['annotation_created_time_upper'], $options['annotation_created_time_lower']); if ($time_wheres) { $options['wheres'] = array_merge($options['wheres'], $time_wheres); } return elgg_get_entities_from_metadata($options); }