You are here

function _merci_staff_potential_references_standard in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.2

Same name and namespace in other branches
  1. 8.2 modules/merci_staff/merci_staff.module \_merci_staff_potential_references_standard()
  2. 6.2 modules/merci_staff/merci_staff.module \_merci_staff_potential_references_standard()

Helper function for _civicrm_cck_potential_references(): referenceable nodes defined by content types.

1 call to _merci_staff_potential_references_standard()
_merci_staff_potential_references in modules/merci_staff/merci_staff.module

File

modules/merci_staff/merci_staff.module, line 200
merci_staff functions

Code

function _merci_staff_potential_references_standard($field, $string = '', $exact_string = FALSE, $limit = '10') {
  $args = $whereClause = $contactTypes = $contactSubTypes = array();
  if (!civicrm_initialize()) {
    return;
  }
  global $civicrm_root;
  require_once $civicrm_root . '/CRM/Contact/BAO/ContactType.php';
  require_once $civicrm_root . '/CRM/Core/BAO/UFMatch.php';
  $basicTypes = CRM_Contact_BAO_ContactType::basicTypePairs();
  foreach ($basicTypes as $name => $label) {
    if (is_array($field[$name])) {
      $contactNames = array_filter($field[$name]);
      if (!empty($contactNames)) {
        if (in_array($name, $contactNames)) {
          $contactTypes[] = $name;
        }
        else {
          $contactSubTypes = array_merge($contactSubTypes, array_keys($contactNames));
        }
      }
    }
  }
  if (!empty($contactTypes)) {
    $contactTypes = implode("','", $contactTypes);
    $whereClause[] = "contact_type IN ( '{$contactTypes}' )";
  }
  if (!empty($contactSubTypes)) {
    $contactSubTypes = implode("','", $contactSubTypes);
    $whereClause[] = "contact_sub_type IN ( '{$contactSubTypes}' )";
  }
  $whereClause = empty($whereClause) ? '' : '(' . implode(' OR ', $whereClause) . ') AND';
  $related_clause = "";
  if (isset($string)) {
    if ($exact_string) {
      $string_clause = " AND display_name = %1";
      $args[] = $string;
    }
    else {
      $string_clause = " AND display_name LIKE %1";
      $args[] = "%%" . $string . "%";
    }
  }
  $q = "\n    SELECT civicrm_contact.id, display_name\n    FROM civicrm_contact\n    JOIN civicrm_uf_match ON civicrm_contact.id = civicrm_uf_match.contact_id\n    WHERE {$whereClause}\n    display_name IS NOT NULL\n    AND display_name NOT LIKE ''\n    AND display_name NOT LIKE '<Last>%%'\n    AND display_name NOT LIKE '%@%%'\n    AND display_name NOT LIKE '--%%'\n    AND display_name NOT LIKE '- -%%'\n    AND display_name NOT LIKE ',%%'\n    AND display_name NOT LIKE '..%%'\n    " . $string_clause . " LIMIT {$limit}";
  $params = array(
    1 => array(
      $args[0],
      "String",
    ),
  );
  $dao = CRM_Core_DAO::executeQuery($q, $params);
  $references = array();
  while ($dao
    ->fetch()) {
    $uid = CRM_Core_BAO_UFMatch::getUFId($dao->id);
    $username = db_query("SELECT name FROM {users} WHERE uid = :uid", array(
      ':uid' => $uid,
    ))
      ->fetchField();
    $references[$dao->id] = array(
      'title' => $username,
      'rendered' => $dao->display_name,
    );
  }
  return $references;
}