All pastes #123752 Raw Edit

Mine

public text v1 · immutable
·published 2006-08-09 15:32 UTC
rendered paste body
<?php
// $Id: civicrm_search.module,v 1.3 2006/04/17 07:02:29 morbus Exp $

/**
 * @file
 * Enables users to create online civicrm_searchs and for other users to sign it.
 */

/**
 * Implementation of hook_help().
 */
function civicrm_search_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Provides a simpler and quicker front-end to CiviCRM searches.');

    case strstr($section, 'civicrm/search'):
      return '<p>'.t('Use the search options below to find individuals by name, address, group, or more.').'</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function civicrm_search_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('title'    => t('CiviCRM Search'),
                     'path'     => 'civicrm/search',
                     'access'   => user_access('access CiviCRM'),
                     'callback' => 'civicrm_search_i_form',
    );
  }

  return $items;
}

/**
 * Search form for individuals.
 */
function civicrm_search_i_form($query_string = NULL) {
  $output = NULL; // prenup our output and our search queries.
  $query = array(); if (isset($query_string)) 
    { $query = _decode_search_request($query_string); }

  $form = array();
  $form['name-based'] = array(
    '#type'          => 'fieldset',
    '#title'         => t('Name-based Searching'),
    '#collapsible'   => TRUE,
    '#collapsed'     => isset($query_string) ? ($query['type'] == 'name-based' ? FALSE : TRUE) : FALSE,
  );
  $form['name-based']['first_name'] = array(
    '#type'          => 'textfield',
    '#title'         => t('First Name'),
    '#default_value' => isset($query['first_name']) ? $query['first_name'] : NULL,
  );
  $form['name-based']['last_name'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Last Name'),
    '#default_value' => isset($query['last_name']) ? $query['last_name'] : NULL,
  ); // soothing ocean sounds mixed with sleep inducing melodies.
  $form['name-based']['type']    = array('#type' => 'value',  '#value' => 'name-based', );
  $form['name-based']['submit']  = array('#type' => 'submit', '#value' => t('Search for Matching Individuals'), );
  $output .= drupal_get_form('civicrm_search_i_form_name_based', $form);

  if (isset($query_string) && isset($query['type'])) {

  }

  return $output;
}

/* all our search forms redirect back to the main URL, transformed slightly. */
function civicrm_search_i_form_submit($form_values) { return 'civicrm/search/i/'._encode_search_request($form_values); }
function civicrm_search_i_form_name_based_submit($form_id, $form_values) { return civicrm_search_i_form_submit($form_values); }

/**
 * Turns a string of keypairs into an array.
 */
function _decode_search_request($query) {
  $params = array(); // result returned.
  $query  = str_replace('+', ' ', $query);
  $pairs = explode(' ', $query);
  foreach ($pairs as $pair) {
    $fpair = explode(':', $pair);
    $params[$fpair[0]] = strstr($fpair[1], ',')
     ? explode(',', $fpair[1]) : $fpair[1];
  } return $params;
}

/**
 * Turns an array into a string of keypairs.
 */
function _encode_search_request($form_values) {
  $params = array(); // holds our returned result.
  $valid_keys = array('type',               'first_name',                   'last_name',
                      'county',             'postal_code',                  'interest',
                     );
  foreach ($valid_keys as $valid_key) {
    if (isset($form_values[$valid_key])) {
      if (is_array($form_values[$valid_key])) {
        $params[] = "$valid_key:".implode(',', $form_values[$valid_key]);
      } else { $params[] = "$valid_key:$form_values[$valid_key]"; }
    }
  } return implode(' ', $params);
}