View source
<?php
include_once 'commons_search_solr_user.features.inc';
function commons_search_solr_user_apachesolr_index_document_build_user($document, $entity, $env_id) {
$flagging_users = module_invoke('flag', 'get_content_flags', 'user', $entity->uid, 'commons_follow_user');
foreach ($flagging_users as $uid => $flaginfo) {
$document
->setMultiValue('im_flag_commons_follow_user', $uid);
}
if (!empty($entity->field_name_first)) {
$document
->addField('tes_firstname', $entity->field_name_first[LANGUAGE_NONE][0]['value']);
}
if (!empty($entity->field_name_last)) {
$document
->addField('tes_lastname', $entity->field_name_last[LANGUAGE_NONE][0]['value']);
}
$document
->addField('tes_username', $entity->name);
}
function commons_search_solr_user_apachesolr_query_alter($query) {
global $user;
$query
->addParam('qf', 'tes_firstname');
$query
->addParam('qf', 'tes_lastname');
$query
->addParam('qf', 'tes_username');
if (isset($_GET['flagged']) && ($flagged = check_plain($_GET['flagged']))) {
$query
->addFilter('im_flag_commons_follow_user', $user->uid);
}
}
function commons_search_solr_user_theme($existing, $type, $theme, $path) {
return array(
'commons_search_solr_user_results' => array(
'variables' => array(
'title' => '',
'results' => array(),
),
'template' => 'commons-search-solr-user-results',
),
);
}
function commons_search_solr_user_strongarm_alter(&$items) {
if (!empty($items['search_active_modules']->value['user'])) {
unset($items['search_active_modules']->value['user']);
}
}
function commons_search_solr_user_apachesolr_search_page_alter(&$build, $search_page) {
global $user;
if ($search_page['page_id'] == 'user_search' && isset($build['search_results']['#markup']) && empty($build['search_form']['basic']['keys']['#default_value'])) {
$limit = isset($search_page['settings']['apachesolr_search_per_page']) ? $search_page['settings']['apachesolr_search_per_page'] : 12;
$flagged = isset($_GET['flagged']) && is_numeric($_GET['flagged']) ? check_plain($_GET['flagged']) : 0;
$out_results = array();
if ($flagged && ($flag = flag_get_flag('commons_follow_user'))) {
$fl_results = db_select('flagging', 'f')
->fields('f', array(
'entity_id',
'uid',
))
->condition('f.entity_type', 'user', '=')
->condition('f.uid', $user->uid, '=')
->condition('f.fid', $flag->fid, '=')
->extend('PagerDefault')
->limit($limit)
->addTag('commons_search_solr_user_people')
->addMetaData('flagged', $flagged)
->execute();
while ($record = $fl_results
->fetchAssoc()) {
$out_results[] = user_view(user_load($record['entity_id']), 'search_results');
}
$build['pager'] = array(
'#theme' => 'pager',
);
}
else {
$users = new EntityFieldQuery();
$results = $users
->entityCondition('entity_type', 'user')
->propertyCondition('status', 1)
->addTag('commons_search_solr_user_people')
->pager($limit, 1)
->execute();
$user_ids = array_keys($results['user']);
foreach ($user_ids as $uid) {
$out_results[] = user_view(user_load($uid), 'search_results');
$user_ids;
}
$build['pager'] = array(
'#markup' => theme('pager__commons_search_solr_user', $users->pager),
);
}
if (!empty($out_results)) {
$build['search_results'] = array(
'#theme' => 'commons_search_solr_user_results',
'#title' => t('People directory'),
'#results' => $out_results,
);
}
}
}
function commons_search_solr_user_user_view_alter(&$build) {
if ($build['#view_mode'] == 'search_results') {
$build['username'] = array(
'#theme' => 'username',
'#account' => $build['#account'],
'#prefix' => '<h4>',
'#suffix' => '</h4>',
'#weight' => 0,
);
$user_picture_uri = !empty($build['#account']->picture) ? $build['#account']->picture->uri : variable_get('user_picture_default', '');
$build['user_picture'] = array(
'#theme' => 'image_formatter',
'#item' => array(
'uri' => $user_picture_uri,
'alt' => t('Profile image'),
),
'#image_style' => '50x50',
'#path' => array(
'path' => 'user/' . $build['#account']->uid,
'options' => array(
'attributes' => array(
'class' => array(
'user-picture',
),
),
),
),
'#weight' => 1,
);
if (!empty($build['#account']->og_user_node[LANGUAGE_NONE])) {
$groups = array();
foreach ($build['#account']->og_user_node[LANGUAGE_NONE] as $og_node) {
if ($gnode = node_load($og_node['target_id'])) {
$groups['group-item group-' . $gnode->nid] = array(
'title' => $gnode->title,
'href' => 'node/' . $gnode->nid,
'attributes' => array(
'class' => array(
'group-link',
),
),
);
}
}
$build['group_membership'] = array(
'#theme' => 'links__profile_groups',
'#links' => $groups,
'#heading' => array(
'text' => t('Recently contributed to'),
'level' => 'h5',
'class' => array(
'associated-groups-title',
),
),
'#attributes' => array(
'class' => array(
'associated-groups',
),
),
'#weight' => 3,
);
}
if (isset($build['flags'])) {
if (isset($build['flags']['abuse_user'])) {
unset($build['flags']['abuse_user']);
}
unset($build['flags']['#type']);
foreach ($build['flags'] as $key => &$data) {
if (substr($key, 0, 1) != '#' && isset($data['#type'])) {
unset($data['#type']);
}
}
$build['flags']['#weight'] = 4;
}
if (isset($build['summary'])) {
$build['summary']['#access'] = FALSE;
}
}
}
function commons_search_solr_user_entity_info_alter(&$entity_info) {
$entity_info['user']['view modes']['search_results'] = array(
'label' => t('Search results'),
'custom settings' => TRUE,
);
}
function commons_search_solr_user_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_block_form') {
unset($form['custom_search_types']['#options']['c-user']);
$form['custom_search_types']['#options']['s-user'] = t('People');
if (strtolower(arg(0)) == 'people') {
$form['search_block_form']['#default_value'] = urldecode(arg(1));
$other_options = $form['custom_search_types']['#options'];
$form['custom_search_types']['#options'] = array_merge(array(
's-user' => t('People'),
), $other_options);
}
$form['#submit'][] = 'commons_search_solr_user_search_submit';
}
}
function commons_search_solr_user_form_apachesolr_search_custom_page_search_form_alter(&$form, &$form_state) {
if ($form['#search_page']['page_id'] == 'user_search') {
$form['#attributes']['class'][] = 'user-search-form';
if (!user_is_anonymous()) {
$form['basic']['flagged'] = array(
'#type' => 'checkbox',
'#title' => t('Following'),
'#default_value' => empty($_GET['flagged']) ? FALSE : TRUE,
'#attributes' => array(
'onclick' => 'this.form.submit();',
),
);
}
$form['basic']['retain-filters']['#type'] = 'hidden';
$form['basic']['retain-filters']['#value'] = 0;
$form['basic']['keys']['#title'] = t('Limit to');
$form['basic']['submit']['#value'] = t('Search');
$form['basic']['submit']['#weight'] = 10;
$form['#submit'][] = 'commons_search_solr_user_user_search_submit';
}
}
function commons_search_solr_user_user_search_submit($form, &$form_state) {
$form_state['redirect']['1']['query']['flagged'] = $form_state['values']['flagged'];
}
function commons_search_solr_user_search_submit($form, &$form_state) {
if ($form_state['values']['custom_search_types'] == 's-user') {
$user_search_page = apachesolr_search_page_load('user_search');
$keys = '/' . $form_state['values']['search_block_form'];
$form_state['redirect'] = $user_search_page['search_path'] . $keys;
}
}
function commons_search_solr_user_module_implements_alter(&$modules, $hook) {
switch ($hook) {
case 'form_alter':
$module = $modules['commons_search_solr_user'];
unset($modules['commons_search_solr_user']);
$modules['commons_search_solr_user'] = $module;
break;
}
}
function commons_search_solr_user_preprocess_user_profile(&$variables, $hook) {
if ($variables['elements']['#view_mode'] == 'search_results') {
$variables['theme_hook_suggestions'][] = 'user_profile__search_results';
}
}
function commons_search_solr_user_preprocess_commons_search_solr_user_results(&$variables, $hook) {
$variables['title_attributes_array']['class'][] = 'user-search-results-title';
$variables['content_attributes_array']['class'][] = 'user-search-results-content';
$variables['content'] = array();
if (!empty($variables['results'])) {
$variables['content']['results'] = $variables['results'];
}
}
function commons_search_solr_user_process_search_results(&$variables, $hook) {
if ($variables['search_page']['page_id'] == 'user_search') {
$variables['title'] = ' ';
}
}
function commons_search_solr_user_post_features_enable_feature($component) {
if ($component == 'apachesolr_search_page') {
$revert = array(
'commons_search_solr_user' => array(
'apachesolr_search_page',
),
);
features_revert($revert);
drupal_static_reset('apachesolr_search_load_all_search_pages');
$revert = array(
'commons_search_solr_user' => array(
'menu_links',
),
);
features_revert($revert);
}
}