- $sub[] = " ( civicrm_email.email $op $value ) ";
- }
- }
- else {
- // the string should be treated as a series of keywords to be matched with match ANY OR
- // match ALL depending on Civi config settings (see CiviAdmin)
-
- // The Civi configuration setting can be overridden if the string *starts* with the case
- // insenstive strings 'AND:' or 'OR:'TO THINK ABOUT: what happens when someone searches
- // for the following "AND: 'a string in quotes'"? - probably nothing - it would make the
- // AND OR variable reduntant because there is only one search string?
-
- // Check to see if the $subGlue is overridden in the search text
- if (strtolower(substr($name, 0, 4)) == 'and:') {
- $name = substr($name, 4);
- $subGlue = ' AND ';
- }
- if (strtolower(substr($name, 0, 3)) == 'or:') {
- $name = substr($name, 3);
- $subGlue = ' OR ';
- }
-
- $firstChar = substr($name, 0, 1);
- $lastChar = substr($name, -1, 1);
- $quotes = array("'", '"');
- if ((strlen($name) > 2) && in_array($firstChar, $quotes) &&
- in_array($lastChar, $quotes)
- ) {
- $name = substr($name, 1);
- $name = substr($name, 0, -1);
- $pieces = array($name);
- }
- else {
- $pieces = explode(' ', $name);
- }
- foreach ($pieces as $piece) {
- $value = $strtolower(CRM_Core_DAO::escapeString(trim($piece)));
- if (strlen($value)) {
- // Added If as a sanitization - without it, when you do an OR search, any string with
- // double spaces (i.e. " ") or that has a space after the keyword (e.g. "OR: ") will
- // return all contacts because it will include a condition similar to "OR contact
- // name LIKE '%'". It might be better to replace this with array_filter.
- $fieldsub = array();
- if ($wildcard) {
- if ($config->includeWildCardInName) {
- $value = "'%$value%'";
- }
- else {
- $value = "'$value%'";
- }
- $op = 'LIKE';
- }
- else {
- $value = "'$value'";
- }
- if ($newName == 'sort_name') {
- $wc = self::caseImportant($op) ? "LOWER(contact_a.sort_name)" : "contact_a.sort_name";
- }
- else {
- $wc = self::caseImportant($op) ? "LOWER(contact_a.display_name)" : "contact_a.display_name";
- }
- $fieldsub[] = " ( $wc $op $value )";
- if ($config->includeNickNameInName) {
- $wc = self::caseImportant($op) ? "LOWER(contact_a.nick_name)" : "contact_a.nick_name";
- $fieldsub[] = " ( $wc $op $value )";
- }
- if ($config->includeEmailInName) {
- $fieldsub[] = " ( civicrm_email.email $op $value ) ";
- }
- $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
- // I seperated the glueing in two. The first stage should always be OR because we are searching for matches in *ANY* of these fields
- }