Esempio n. 1
0
 static function getContactList(&$config)
 {
     require_once 'CRM/Core/BAO/Preferences.php';
     $name = CRM_Utils_Type::escape($_GET['s'], 'String');
     $limit = '10';
     $list = array_keys(CRM_Core_BAO_Preferences::valueOptions('contact_autocomplete_options'), '1');
     $select = array('sort_name');
     $where = '';
     $from = array();
     foreach ($list as $value) {
         $suffix = substr($value, 0, 2) . substr($value, -1);
         switch ($value) {
             case 'street_address':
             case 'city':
                 $selectText = $value;
                 $value = "address";
                 $suffix = 'sts';
             case 'phone':
             case 'email':
                 $select[] = $value == 'address' ? $selectText : $value;
                 $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) ";
                 break;
             case 'country':
             case 'state_province':
                 $select[] = "{$suffix}.name";
                 if (!in_array('address', $from)) {
                     $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) ';
                 }
                 $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id  ) ";
                 break;
         }
     }
     $select = implode(', ', $select);
     $from = implode(' ', $from);
     if (CRM_Utils_Array::value('limit', $_GET)) {
         $limit = CRM_Utils_Type::escape($_GET['limit'], 'Positive');
     }
     // add acl clause here
     require_once 'CRM/Contact/BAO/Contact/Permission.php';
     list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
     if ($aclWhere) {
         $where .= " AND {$aclWhere} ";
     }
     $query = "\nSELECT DISTINCT(cc.id) as id, CONCAT_WS( ' :: ', {$select} ) as data\nFROM civicrm_contact cc {$from}\n{$aclFrom}\nWHERE sort_name LIKE '%{$name}%' {$where} \nORDER BY sort_name\nLIMIT 0, {$limit}\n";
     // send query to hook to be modified if needed
     require_once 'CRM/Utils/Hook.php';
     CRM_Utils_Hook::contactListQuery($query, $name, CRM_Utils_Array::value('context', $_GET), CRM_Utils_Array::value('id', $_GET));
     $dao = CRM_Core_DAO::executeQuery($query);
     $contactList = null;
     while ($dao->fetch()) {
         echo $contactList = "{$dao->data}|{$dao->id}\n";
     }
     exit;
 }
Esempio n. 2
0
 public static function getContactPhone()
 {
     $queryString = NULL;
     //check for mobile type
     $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
     $mobileType = CRM_Utils_Array::value('Mobile', $phoneTypes);
     $name = CRM_Utils_Array::value('name', $_GET);
     if ($name) {
         $name = CRM_Utils_Type::escape($name, 'String');
         $queryString = " ( cc.sort_name LIKE '%{$name}%' OR cp.phone LIKE '%{$name}%' ) ";
     } else {
         $cid = CRM_Utils_Array::value('cid', $_GET);
         if ($cid) {
             //check cid for integer
             $contIDS = explode(',', $cid);
             foreach ($contIDS as $contID) {
                 CRM_Utils_Type::escape($contID, 'Integer');
             }
             $queryString = " cc.id IN ( {$cid} )";
         }
     }
     if ($queryString) {
         $offset = CRM_Utils_Array::value('offset', $_GET, 0);
         $rowCount = CRM_Utils_Array::value('rowcount', $_GET, 20);
         $offset = CRM_Utils_Type::escape($offset, 'Int');
         $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
         // add acl clause here
         list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
         if ($aclWhere) {
             $aclWhere = " AND {$aclWhere}";
         }
         $query = "\nSELECT sort_name name, cp.phone, cc.id\nFROM   civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id\n       {$aclFrom}\nWHERE  cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}\n       {$aclWhere}\nLIMIT {$offset}, {$rowCount}\n";
         // send query to hook to be modified if needed
         CRM_Utils_Hook::contactListQuery($query, $name, CRM_Utils_Request::retrieve('context', 'String', CRM_Core_DAO::$_nullObject), CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject));
         $dao = CRM_Core_DAO::executeQuery($query);
         while ($dao->fetch()) {
             $result[] = array('text' => '"' . $dao->name . '" (' . $dao->phone . ')', 'id' => CRM_Utils_Array::value('id', $_GET) ? "{$dao->id}::{$dao->phone}" : '"' . $dao->name . '" <' . $dao->phone . '>');
         }
     }
     if ($result) {
         CRM_Utils_JSON::output($result);
     }
     CRM_Utils_System::civiExit();
 }
Esempio n. 3
0
 static function getContactList(&$config)
 {
     require_once 'CRM/Core/BAO/Preferences.php';
     $name = CRM_Utils_Array::value('s', $_GET);
     $name = CRM_Utils_Type::escape($name, 'String');
     $limit = '10';
     $list = array_keys(CRM_Core_BAO_Preferences::valueOptions('contact_autocomplete_options'), '1');
     $select = array('sort_name');
     $where = '';
     $from = array();
     foreach ($list as $value) {
         $suffix = substr($value, 0, 2) . substr($value, -1);
         switch ($value) {
             case 'street_address':
             case 'city':
                 $selectText = $value;
                 $value = "address";
                 $suffix = 'sts';
             case 'phone':
             case 'email':
                 $select[] = $value == 'address' ? $selectText : $value;
                 $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) ";
                 break;
             case 'country':
             case 'state_province':
                 $select[] = "{$suffix}.name";
                 if (!in_array('address', $from)) {
                     $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) ';
                 }
                 $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id  ) ";
                 break;
         }
     }
     $select = implode(', ', $select);
     $from = implode(' ', $from);
     if (CRM_Utils_Array::value('limit', $_GET)) {
         $limit = CRM_Utils_Type::escape($_GET['limit'], 'Positive');
     }
     // add acl clause here
     require_once 'CRM/Contact/BAO/Contact/Permission.php';
     list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
     if ($aclWhere) {
         $where .= " AND {$aclWhere} ";
     }
     if (CRM_Utils_Array::value('org', $_GET)) {
         $where .= " AND contact_type = \"Organization\"";
         //set default for current_employer
         if ($orgId = CRM_Utils_Array::value('id', $_GET)) {
             $where .= " AND cc.id = {$orgId}";
         }
     }
     //contact's based of relationhip type
     $relType = null;
     if (isset($_GET['rel'])) {
         $relation = explode('_', $_GET['rel']);
         $relType = CRM_Utils_Type::escape($relation[0], 'Integer');
         $rel = CRM_Utils_Type::escape($relation[2], 'String');
     }
     $config =& CRM_Core_Config::singleton();
     if ($config->includeWildCardInName) {
         $strSearch = "%{$name}%";
     } else {
         $strSearch = "{$name}%";
     }
     $whereClause = " WHERE sort_name LIKE '{$strSearch}' {$where} ";
     $additionalFrom = '';
     if ($relType) {
         $additionalFrom = "\n            INNER JOIN civicrm_relationship_type r ON ( \n                r.id = {$relType}\n                AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL )\n                AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL )\n            )";
     }
     $query = "\nSELECT DISTINCT(cc.id) as id, CONCAT_WS( ' :: ', {$select} ) as data\nFROM civicrm_contact cc {$from}\n{$aclFrom}\n{$additionalFrom}\n{$whereClause} \nORDER BY sort_name\nLIMIT 0, {$limit}\n";
     // send query to hook to be modified if needed
     require_once 'CRM/Utils/Hook.php';
     CRM_Utils_Hook::contactListQuery($query, $name, CRM_Utils_Array::value('context', $_GET), CRM_Utils_Array::value('id', $_GET));
     $dao = CRM_Core_DAO::executeQuery($query);
     $contactList = null;
     while ($dao->fetch()) {
         echo $contactList = "{$dao->data}|{$dao->id}\n";
     }
     //return organization name if doesn't exist in db
     if (!$contactList) {
         if (CRM_Utils_Array::value('org', $_GET)) {
             echo CRM_Utils_Array::value('s', $_GET);
         } else {
             if (CRM_Utils_Array::value('context', $_GET) == 'customfield') {
                 echo "{$name}|{$name}\n";
             }
         }
     }
     exit;
 }
Esempio n. 4
0
/**
 * Old Contact quick search api.
 *
 * @deprecated
 *
 * @param array $params
 *
 * @return array
 * @throws \API_Exception
 */
function civicrm_api3_contact_getquick($params)
{
    $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String');
    $table_name = CRM_Utils_String::munge($params['table_name']);
    // get the autocomplete options from settings
    $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_autocomplete_options'));
    // get the option values for contact autocomplete
    $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name');
    $list = array();
    foreach ($acpref as $value) {
        if ($value && !empty($acOptions[$value])) {
            $list[$value] = $acOptions[$value];
        }
    }
    // If we are doing quicksearch by a field other than name, make sure that field is added to results
    if (!empty($params['field_name'])) {
        $field_name = CRM_Utils_String::munge($params['field_name']);
        // Unique name contact_id = id
        if ($field_name == 'contact_id') {
            $field_name = 'id';
        }
        // phone_numeric should be phone
        $searchField = str_replace('_numeric', '', $field_name);
        if (!in_array($searchField, $list)) {
            $list[] = $searchField;
        }
    } else {
        // Set field name to first name for exact match checking.
        $field_name = 'sort_name';
    }
    $select = $actualSelectElements = array('sort_name');
    $where = '';
    $from = array();
    foreach ($list as $value) {
        $suffix = substr($value, 0, 2) . substr($value, -1);
        switch ($value) {
            case 'street_address':
            case 'city':
            case 'postal_code':
                $selectText = $value;
                $value = "address";
                $suffix = 'sts';
            case 'phone':
            case 'email':
                $actualSelectElements[] = $select[] = $value == 'address' ? $selectText : $value;
                if ($value == 'phone') {
                    $actualSelectElements[] = $select[] = 'phone_ext';
                }
                $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) ";
                break;
            case 'country':
            case 'state_province':
                $select[] = "{$suffix}.name as {$value}";
                $actualSelectElements[] = "{$suffix}.name";
                if (!in_array('address', $from)) {
                    $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) ';
                }
                $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id  ) ";
                break;
            default:
                if ($value != 'id') {
                    $suffix = 'cc';
                    if (!empty($params['field_name']) && $params['field_name'] == 'value') {
                        $suffix = CRM_Utils_String::munge(CRM_Utils_Array::value('table_name', $params, 'cc'));
                    }
                    $actualSelectElements[] = $select[] = $suffix . '.' . $value;
                }
                break;
        }
    }
    $config = CRM_Core_Config::singleton();
    $as = $select;
    $select = implode(', ', $select);
    if (!empty($select)) {
        $select = ", {$select}";
    }
    $actualSelectElements = implode(', ', $actualSelectElements);
    $selectAliases = $from;
    unset($selectAliases['address']);
    $selectAliases = implode(', ', array_keys($selectAliases));
    if (!empty($selectAliases)) {
        $selectAliases = ", {$selectAliases}";
    }
    $from = implode(' ', $from);
    $limit = (int) CRM_Utils_Array::value('limit', $params);
    $limit = $limit > 0 ? $limit : Civi::settings()->get('search_autocomplete_count');
    // add acl clause here
    list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
    if ($aclWhere) {
        $where .= " AND {$aclWhere} ";
    }
    if (!empty($params['org'])) {
        $where .= " AND contact_type = \"Organization\"";
        // CRM-7157, hack: get current employer details when
        // employee_id is present.
        $currEmpDetails = array();
        if (!empty($params['employee_id'])) {
            if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', (int) $params['employee_id'], 'employer_id')) {
                if ($config->includeWildCardInName) {
                    $strSearch = "%{$name}%";
                } else {
                    $strSearch = "{$name}%";
                }
                // get current employer details
                $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name\n                    FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '{$strSearch}'");
                if ($dao->fetch()) {
                    $currEmpDetails = array('id' => $dao->id, 'data' => $dao->data);
                }
            }
        }
    }
    if (!empty($params['contact_sub_type'])) {
        $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String');
        $where .= " AND cc.contact_sub_type = '{$contactSubType}'";
    }
    if (!empty($params['contact_type'])) {
        $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String');
        $where .= " AND cc.contact_type LIKE '{$contactType}'";
    }
    // Set default for current_employer or return contact with particular id
    if (!empty($params['id'])) {
        $where .= " AND cc.id = " . (int) $params['id'];
    }
    if (!empty($params['cid'])) {
        $where .= " AND cc.id <> " . (int) $params['cid'];
    }
    // Contact's based of relationhip type
    $relType = NULL;
    if (!empty($params['rel'])) {
        $relation = explode('_', CRM_Utils_Array::value('rel', $params));
        $relType = CRM_Utils_Type::escape($relation[0], 'Integer');
        $rel = CRM_Utils_Type::escape($relation[2], 'String');
    }
    if ($config->includeWildCardInName) {
        $strSearch = "%{$name}%";
    } else {
        $strSearch = "{$name}%";
    }
    $includeEmailFrom = $includeNickName = $exactIncludeNickName = '';
    if ($config->includeNickNameInName) {
        $includeNickName = " OR nick_name LIKE '{$strSearch}'";
        $exactIncludeNickName = " OR nick_name LIKE '{$name}'";
    }
    //CRM-10687
    if (!empty($params['field_name']) && !empty($params['table_name'])) {
        $whereClause = " WHERE ( {$table_name}.{$field_name} LIKE '{$strSearch}') {$where}";
        $exactWhereClause = " WHERE ( {$table_name}.{$field_name} = '{$name}') {$where}";
        // Search by id should be exact
        if ($field_name == 'id' || $field_name == 'external_identifier') {
            $whereClause = $exactWhereClause;
        }
    } else {
        $whereClause = " WHERE ( sort_name LIKE '{$strSearch}' {$includeNickName} ) {$where} ";
        $exactWhereClause = " WHERE ( sort_name LIKE '{$name}' {$exactIncludeNickName} ) {$where} ";
        if ($config->includeEmailInName) {
            if (!in_array('email', $list)) {
                $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
            }
            $emailWhere = " WHERE email LIKE '{$strSearch}'";
        }
    }
    $additionalFrom = '';
    if ($relType) {
        $additionalFrom = "\n            INNER JOIN civicrm_relationship_type r ON (\n                r.id = {$relType}\n                AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL )\n                AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL )\n            )";
    }
    // check if only CMS users are requested
    if (!empty($params['cmsuser'])) {
        $additionalFrom = "\n      INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id)\n      ";
    }
    $orderByInner = $orderByOuter = "ORDER BY exactFirst";
    if ($config->includeOrderByClause) {
        $orderByInner = "ORDER BY exactFirst, sort_name";
        $orderByOuter .= ", sort_name";
    }
    //CRM-5954
    $query = "\n        SELECT DISTINCT(id), data, sort_name {$selectAliases}, exactFirst\n        FROM   (\n            ( SELECT IF({$table_name}.{$field_name} = '{$name}', 0, 1) as exactFirst, cc.id as id, CONCAT_WS( ' :: ',\n            {$actualSelectElements} )\n             as data\n            {$select}\n            FROM   civicrm_contact cc {$from}\n    {$aclFrom}\n    {$additionalFrom}\n    {$whereClause}\n    {$orderByInner}\n    LIMIT 0, {$limit} )\n    ";
    if (!empty($emailWhere)) {
        $query .= "\n      UNION (\n        SELECT IF({$table_name}.{$field_name} = '{$name}', 0, 1) as exactFirst, cc.id as id, CONCAT_WS( ' :: ',\n          {$actualSelectElements} )\n          as data\n          {$select}\n          FROM   civicrm_contact cc {$from}\n        {$aclFrom}\n        {$additionalFrom} {$includeEmailFrom}\n        {$emailWhere} AND cc.is_deleted = 0 " . ($aclWhere ? " AND {$aclWhere} " : '') . "\n        {$orderByInner}\n      LIMIT 0, {$limit}\n      )\n    ";
    }
    $query .= ") t\n    {$orderByOuter}\n    LIMIT    0, {$limit}\n  ";
    // send query to hook to be modified if needed
    CRM_Utils_Hook::contactListQuery($query, $name, empty($params['context']) ? NULL : CRM_Utils_Type::escape($params['context'], 'String'), empty($params['id']) ? NULL : $params['id']);
    $dao = CRM_Core_DAO::executeQuery($query);
    $contactList = array();
    $listCurrentEmployer = TRUE;
    while ($dao->fetch()) {
        $t = array('id' => $dao->id);
        foreach ($as as $k) {
            $t[$k] = isset($dao->{$k}) ? $dao->{$k} : '';
        }
        $t['data'] = $dao->data;
        $contactList[] = $t;
        if (!empty($params['org']) && !empty($currEmpDetails) && $dao->id == $currEmpDetails['id']) {
            $listCurrentEmployer = FALSE;
        }
    }
    //return organization name if doesn't exist in db
    if (empty($contactList)) {
        if (!empty($params['org'])) {
            if ($listCurrentEmployer && !empty($currEmpDetails)) {
                $contactList = array(array('data' => $currEmpDetails['data'], 'id' => $currEmpDetails['id']));
            } else {
                $contactList = array(array('data' => $name, 'id' => $name));
            }
        }
    }
    return civicrm_api3_create_success($contactList, $params, 'Contact', 'getquick');
}
function civicrm_api3_contact_getquick($params)
{
    civicrm_api3_verify_mandatory($params, NULL, array('name'));
    $name = CRM_Utils_Array::value('name', $params);
    // get the autocomplete options from settings
    require_once 'CRM/Core/BAO/Setting.php';
    $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_autocomplete_options'));
    // get the option values for contact autocomplete
    $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name');
    $list = array();
    foreach ($acpref as $value) {
        if ($value && CRM_Utils_Array::value($value, $acOptions)) {
            $list[$value] = $acOptions[$value];
        }
    }
    $select = $actualSelectElements = array('sort_name');
    $where = '';
    $from = array();
    foreach ($list as $value) {
        $suffix = substr($value, 0, 2) . substr($value, -1);
        switch ($value) {
            case 'street_address':
            case 'city':
                $selectText = $value;
                $value = "address";
                $suffix = 'sts';
            case 'phone':
            case 'email':
                $actualSelectElements[] = $select[] = $value == 'address' ? $selectText : $value;
                $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) ";
                break;
            case 'country':
            case 'state_province':
                $select[] = "{$suffix}.name as {$value}";
                $actualSelectElements[] = "{$suffix}.name";
                if (!in_array('address', $from)) {
                    $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) ';
                }
                $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id  ) ";
                break;
        }
    }
    $config = CRM_Core_Config::singleton();
    $as = $select;
    $select = implode(', ', $select);
    if (!empty($select)) {
        $select = ", {$select}";
    }
    $actualSelectElements = implode(', ', $actualSelectElements);
    $selectAliases = $from;
    unset($selectAliases['address']);
    $selectAliases = implode(', ', array_keys($selectAliases));
    if (!empty($selectAliases)) {
        $selectAliases = ", {$selectAliases}";
    }
    $from = implode(' ', $from);
    $limit = CRM_Utils_Array::value('limit', $params, 10);
    // add acl clause here
    require_once 'CRM/Contact/BAO/Contact/Permission.php';
    list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
    if ($aclWhere) {
        $where .= " AND {$aclWhere} ";
    }
    if (CRM_Utils_Array::value('org', $params)) {
        $where .= " AND contact_type = \"Organization\"";
        //set default for current_employer
        if ($orgId = CRM_Utils_Array::value('id', $params)) {
            $where .= " AND cc.id = {$orgId}";
        }
        // CRM-7157, hack: get current employer details when
        // employee_id is present.
        $currEmpDetails = array();
        if (CRM_Utils_Array::value('employee_id', $params)) {
            if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', CRM_Utils_Array::value('employee_id', $params), 'employer_id')) {
                if ($config->includeWildCardInName) {
                    $strSearch = "%{$name}%";
                } else {
                    $strSearch = "{$name}%";
                }
                // get current employer details
                $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name\n                    FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '{$strSearch}'");
                if ($dao->fetch()) {
                    $currEmpDetails = array('id' => $dao->id, 'data' => $dao->data);
                }
            }
        }
    }
    if (CRM_Utils_Array::value('cid', $params)) {
        $where .= " AND cc.id <> {$params['cid']}";
    }
    //contact's based of relationhip type
    $relType = NULL;
    if (CRM_Utils_Array::value('rel', $params)) {
        $relation = explode('_', CRM_Utils_Array::value('rel', $params));
        $relType = CRM_Utils_Type::escape($relation[0], 'Integer');
        $rel = CRM_Utils_Type::escape($relation[2], 'String');
    }
    if ($config->includeWildCardInName) {
        $strSearch = "%{$name}%";
    } else {
        $strSearch = "{$name}%";
    }
    $includeEmailFrom = $includeNickName = $exactIncludeNickName = '';
    if ($config->includeNickNameInName) {
        $includeNickName = " OR nick_name LIKE '{$strSearch}'";
        $exactIncludeNickName = " OR nick_name LIKE '{$name}'";
    }
    if ($config->includeEmailInName) {
        if (!in_array('email', $list)) {
            $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
        }
        $whereClause = " WHERE ( email LIKE '{$strSearch}' OR sort_name LIKE '{$strSearch}' {$includeNickName} ) {$where} ";
        $exactWhereClause = " WHERE ( email LIKE '{$name}' OR sort_name LIKE '{$name}' {$exactIncludeNickName} ) {$where} ";
    } else {
        $whereClause = " WHERE ( sort_name LIKE '{$strSearch}' {$includeNickName} ) {$where} ";
        $exactWhereClause = " WHERE ( sort_name LIKE '{$name}' {$exactIncludeNickName} ) {$where} ";
    }
    $additionalFrom = '';
    if ($relType) {
        $additionalFrom = "\n            INNER JOIN civicrm_relationship_type r ON (\n                r.id = {$relType}\n                AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL )\n                AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL )\n            )";
    }
    //CRM-5954
    $query = "\n        SELECT DISTINCT(id), data, sort_name {$selectAliases}\n        FROM   (\n            ( SELECT 0 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select}\n            FROM   civicrm_contact cc {$from}\n    {$aclFrom}\n    {$additionalFrom} {$includeEmailFrom}\n    {$exactWhereClause}\n    LIMIT 0, {$limit} )\n    UNION\n    ( SELECT 1 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select}\n    FROM   civicrm_contact cc {$from}\n    {$aclFrom}\n    {$additionalFrom} {$includeEmailFrom}\n    {$whereClause}\n    ORDER BY sort_name\n    LIMIT 0, {$limit} )\n) t\nORDER BY exactFirst, sort_name\nLIMIT    0, {$limit}\n    ";
    // send query to hook to be modified if needed
    require_once 'CRM/Utils/Hook.php';
    CRM_Utils_Hook::contactListQuery($query, $name, CRM_Utils_Array::value('context', $params), CRM_Utils_Array::value('id', $params));
    $dao = CRM_Core_DAO::executeQuery($query);
    $contactList = array();
    $listCurrentEmployer = TRUE;
    while ($dao->fetch()) {
        $t = array('id' => $dao->id);
        foreach ($as as $k) {
            $t[$k] = $dao->{$k};
        }
        $t['data'] = $dao->data;
        $contactList[] = $t;
        if (CRM_Utils_Array::value('org', $params) && !empty($currEmpDetails) && $dao->id == $currEmpDetails['id']) {
            $listCurrentEmployer = FALSE;
        }
    }
    //return organization name if doesn't exist in db
    if (empty($contactList)) {
        if (CRM_Utils_Array::value('org', $params)) {
            if ($listCurrentEmployer && !empty($currEmpDetails)) {
                $contactList = array(array('data' => $currEmpDetails['data'], 'id' => $currEmpDetails['id']));
            } else {
                $contactList = array(array('data' => $name, 'id' => $name));
            }
        }
    }
    return civicrm_api3_create_success($contactList, $params);
}