3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 * This is the heart of the search query building mechanism.
37 class CRM_Contact_BAO_Query
{
40 * The various search modes.
45 NO_RETURN_PROPERTIES
= 'CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES',
51 MODE_PLEDGEBANK
= 256,
60 * The default set of return properties.
64 static $_defaultReturnProperties = NULL;
67 * The default set of hier return properties.
71 static $_defaultHierReturnProperties;
74 * The set of input params.
86 * The set of output params
90 public $_returnProperties;
100 * The name of the elements that are in the select clause
101 * used to extract the values
108 * The tables involved in the query
115 * The table involved in the where clause
119 public $_whereTables;
133 public $_whereClause;
136 * Additional permission Where Clause
140 public $_permissionWhereClause;
150 * Additional permission from clause
154 public $_permissionFromClause;
157 * The from clause for the simple select and alphabetical
162 public $_simpleFromClause;
172 * The english language version of the query
179 * All the fields that could potentially be involved in
187 * The cache to translate the option values into labels.
194 * Are we in search mode.
198 public $_search = TRUE;
201 * Should we skip permission checking.
205 public $_skipPermission = FALSE;
208 * Should we skip adding of delete clause.
212 public $_skipDeleteClause = FALSE;
215 * Are we in strict mode (use equality over LIKE)
219 public $_strict = FALSE;
222 * What operator to use to group the clauses.
226 public $_operator = 'AND';
231 * Should we only search on primary location.
235 public $_primaryLocation = TRUE;
238 * Are contact ids part of the query.
242 public $_includeContactIds = FALSE;
245 * Should we use the smart group cache.
249 public $_smartGroupCache = TRUE;
252 * Should we display contacts with a specific relationship type.
256 public $_displayRelationshipType = NULL;
259 * Reference to the query object for custom values.
263 public $_customQuery;
266 * Should we enable the distinct clause, used if we are including
267 * more than one group
271 public $_useDistinct = FALSE;
274 * Should we just display one contact record
276 public $_useGroupBy = FALSE;
279 * The relationship type direction
290 static $_activityRole;
293 * Consider the component activity type
294 * during activity search.
298 static $_considerCompActivities;
301 * Consider with contact activities only,
302 * during activity search.
306 static $_withContactActivitiesOnly;
309 * Use distinct component clause for component searches
313 public $_distinctComponentClause;
315 public $_rowCountClause;
318 * Use groupBy component clause for component searches
322 public $_groupByComponentClause;
325 * Track open panes, useful in advance search
329 public static $_openedPanes = array();
332 * For search builder - which custom fields are location-dependent
335 public $_locationSpecificCustomFields = array();
338 * The tables which have a dependency on location and/or address
342 static $_dependencies = array(
343 'civicrm_state_province' => 1,
344 'civicrm_country' => 1,
345 'civicrm_county' => 1,
346 'civicrm_address' => 1,
347 'civicrm_location_type' => 1,
351 * List of location specific fields.
353 static $_locationSpecificFields = array(
358 'supplemental_address_1',
359 'supplemental_address_2',
362 'postal_code_suffix',
376 * Remember if we handle either end of a number or date range
377 * so we can skip the other
379 protected $_rangeCache = array();
381 * Set to true when $this->relationship is run to avoid adding twice
384 protected $_relationshipValuesAdded = FALSE;
387 * Set to the name of the temp table if one has been created
390 static $_relationshipTempTable = NULL;
392 public $_pseudoConstantsSelect = array();
395 * Class constructor which also does all the work.
397 * @param array $params
398 * @param array $returnProperties
399 * @param array $fields
400 * @param bool $includeContactIds
401 * @param bool $strict
402 * @param bool|int $mode - mode the search is operating on
404 * @param bool $skipPermission
405 * @param bool $searchDescendentGroups
406 * @param bool $smartGroupCache
407 * @param null $displayRelationshipType
408 * @param string $operator
409 * @param string $apiEntity
411 * @return \CRM_Contact_BAO_Query
413 public function __construct(
414 $params = NULL, $returnProperties = NULL, $fields = NULL,
415 $includeContactIds = FALSE, $strict = FALSE, $mode = 1,
416 $skipPermission = FALSE, $searchDescendentGroups = TRUE,
417 $smartGroupCache = TRUE, $displayRelationshipType = NULL,
421 $this->_params
= &$params;
422 if ($this->_params
== NULL) {
423 $this->_params
= array();
426 if ($returnProperties === self
::NO_RETURN_PROPERTIES
) {
427 $this->_returnProperties
= array();
429 elseif (empty($returnProperties)) {
430 $this->_returnProperties
= self
::defaultReturnProperties($mode);
433 $this->_returnProperties
= &$returnProperties;
436 $this->_includeContactIds
= $includeContactIds;
437 $this->_strict
= $strict;
438 $this->_mode
= $mode;
439 $this->_skipPermission
= $skipPermission;
440 $this->_smartGroupCache
= $smartGroupCache;
441 $this->_displayRelationshipType
= $displayRelationshipType;
442 $this->setOperator($operator);
445 $this->_fields
= &$fields;
446 $this->_search
= FALSE;
447 $this->_skipPermission
= TRUE;
450 $this->_fields
= CRM_Contact_BAO_Contact
::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission);
452 $fields = CRM_Core_Component
::getQueryFields(!$this->_skipPermission
);
453 unset($fields['note']);
454 $this->_fields
= array_merge($this->_fields
, $fields);
456 // add activity fields
457 $fields = CRM_Activity_BAO_Activity
::exportableFields();
458 $this->_fields
= array_merge($this->_fields
, $fields);
460 // add any fields provided by hook implementers
461 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
462 $this->_fields
= array_merge($this->_fields
, $extFields);
465 // basically do all the work once, and then reuse it
466 $this->initialize($apiEntity);
470 * Function which actually does all the work for the constructor.
472 public function initialize($apiEntity = NULL) {
473 $this->_select
= array();
474 $this->_element
= array();
475 $this->_tables
= array();
476 $this->_whereTables
= array();
477 $this->_where
= array();
478 $this->_qill
= array();
479 $this->_options
= array();
480 $this->_cfIDs
= array();
481 $this->_paramLookup
= array();
482 $this->_having
= array();
484 $this->_customQuery
= NULL;
486 // reset cached static variables - CRM-5803
487 self
::$_activityRole = NULL;
488 self
::$_considerCompActivities = NULL;
489 self
::$_withContactActivitiesOnly = NULL;
491 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
492 $this->_element
['contact_id'] = 1;
493 $this->_tables
['civicrm_contact'] = 1;
495 if (!empty($this->_params
)) {
496 $this->buildParamsLookup();
499 $this->_whereTables
= $this->_tables
;
501 $this->selectClause($apiEntity);
502 $this->_whereClause
= $this->whereClause($apiEntity);
503 if (array_key_exists('civicrm_contribution', $this->_whereTables
)) {
504 $component = 'contribution';
506 if (array_key_exists('civicrm_membership', $this->_whereTables
)) {
507 $component = 'membership';
509 if (isset($component)) {
510 CRM_Financial_BAO_FinancialType
::buildPermissionedClause($this->_whereClause
, $component);
513 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
514 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
516 $this->openedSearchPanes(TRUE);
520 * Function for same purpose as convertFormValues.
522 * Like convert form values this function exists to pre-Process parameters from the form.
524 * It is unclear why they are different functions & likely relates to advances search
525 * versus search builder.
527 * The direction we are going is having the form convert values to a standardised format &
528 * moving away from wierd & wonderful where clause switches.
530 * Fix and handle contact deletion nicely.
532 * this code is primarily for search builder use case where different clauses can specify if they want deleted.
536 public function buildParamsLookup() {
537 $trashParamExists = FALSE;
538 $paramByGroup = array();
539 foreach ($this->_params
as $k => $param) {
540 if (!empty($param[0]) && $param[0] == 'contact_is_deleted') {
541 $trashParamExists = TRUE;
543 if (!empty($param[3])) {
544 $paramByGroup[$param[3]][$k] = $param;
548 if ($trashParamExists) {
549 $this->_skipDeleteClause
= TRUE;
551 //cycle through group sets and explicitly add trash param if not set
552 foreach ($paramByGroup as $setID => $set) {
554 !in_array(array('contact_is_deleted', '=', '1', $setID, '0'), $this->_params
) &&
555 !in_array(array('contact_is_deleted', '=', '0', $setID, '0'), $this->_params
)
557 $this->_params
[] = array(
558 'contact_is_deleted',
568 foreach ($this->_params
as $value) {
569 if (empty($value[0])) {
572 $cfID = CRM_Core_BAO_CustomField
::getKeyID($value[0]);
574 if (!array_key_exists($cfID, $this->_cfIDs
)) {
575 $this->_cfIDs
[$cfID] = array();
577 // Set wildcard value based on "and/or" selection
578 foreach ($this->_params
as $key => $param) {
579 if ($param[0] == $value[0] . '_operator') {
580 $value[4] = $param[2] == 'or';
584 $this->_cfIDs
[$cfID][] = $value;
587 if (!array_key_exists($value[0], $this->_paramLookup
)) {
588 $this->_paramLookup
[$value[0]] = array();
590 if ($value[0] !== 'group') {
591 // Just trying to unravel how group interacts here! This whole function is wieid.
592 $this->_paramLookup
[$value[0]][] = $value;
598 * Some composite fields do not appear in the fields array hack to make them part of the query.
600 public function addSpecialFields($apiEntity) {
601 static $special = array('contact_type', 'contact_sub_type', 'sort_name', 'display_name');
602 // if get called via Contact.get API having address_id as return parameter
603 if ($apiEntity == 'Contact') {
604 $special[] = 'address_id';
606 foreach ($special as $name) {
607 if (!empty($this->_returnProperties
[$name])) {
608 if ($name == 'address_id') {
609 $this->_tables
['civicrm_address'] = 1;
610 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
611 $this->_element
['address_id'] = 1;
614 $this->_select
[$name] = "contact_a.{$name} as $name";
615 $this->_element
[$name] = 1;
622 * Given a list of conditions in params and a list of desired
623 * return Properties generate the required select and from
624 * clauses. Note that since the where clause introduces new
625 * tables, the initial attempt also retrieves all variables used
628 public function selectClause($apiEntity = NULL) {
630 $this->addSpecialFields($apiEntity);
632 foreach ($this->_fields
as $name => $field) {
633 // skip component fields
634 // there are done by the alter query below
635 // and need not be done on every field
637 (substr($name, 0, 12) == 'participant_') ||
638 (substr($name, 0, 7) == 'pledge_') ||
639 (substr($name, 0, 5) == 'case_') ||
640 (substr($name, 0, 13) == 'contribution_' &&
641 (strpos($name, 'source') !== FALSE && strpos($name, 'recur') !== FALSE)) ||
642 (substr($name, 0, 8) == 'payment_')
647 // redirect to activity select clause
649 (substr($name, 0, 9) == 'activity_') ||
650 ($name == 'parent_id')
652 CRM_Activity_BAO_Query
::select($this);
656 // if this is a hierarchical name, we ignore it
657 $names = explode('-', $name);
658 if (count($names) > 1 && isset($names[1]) && is_numeric($names[1])) {
662 // make an exception for special cases, to add the field in select clause
663 $makeException = FALSE;
665 //special handling for groups/tags
666 if (in_array($name, array('groups', 'tags', 'notes'))
667 && isset($this->_returnProperties
[substr($name, 0, -1)])
669 $makeException = TRUE;
672 // since note has 3 different options we need special handling
673 // note / note_subject / note_body
674 if ($name == 'notes') {
675 foreach (array('note', 'note_subject', 'note_body') as $noteField) {
676 if (isset($this->_returnProperties
[$noteField])) {
677 $makeException = TRUE;
683 if (in_array($name, array('prefix_id', 'suffix_id', 'gender_id', 'communication_style_id'))) {
684 if (CRM_Utils_Array
::value($field['pseudoconstant']['optionGroupName'], $this->_returnProperties
)) {
685 $makeException = TRUE;
689 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
690 if (!empty($this->_paramLookup
[$name]) ||
!empty($this->_returnProperties
[$name]) ||
694 // add to cfIDs array if not present
695 if (!array_key_exists($cfID, $this->_cfIDs
)) {
696 $this->_cfIDs
[$cfID] = array();
699 elseif (isset($field['where'])) {
700 list($tableName, $fieldName) = explode('.', $field['where'], 2);
701 if (isset($tableName)) {
702 if (CRM_Utils_Array
::value($tableName, self
::$_dependencies)) {
703 $this->_tables
['civicrm_address'] = 1;
704 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
705 $this->_element
['address_id'] = 1;
708 if ($tableName == 'im_provider' ||
$tableName == 'email_greeting' ||
709 $tableName == 'postal_greeting' ||
$tableName == 'addressee'
711 if ($tableName == 'im_provider') {
712 CRM_Core_OptionValue
::select($this);
715 if (in_array($tableName,
716 array('email_greeting', 'postal_greeting', 'addressee'))) {
717 $this->_element
["{$name}_id"] = 1;
718 $this->_select
["{$name}_id"] = "contact_a.{$name}_id as {$name}_id";
719 $this->_pseudoConstantsSelect
[$name] = array('pseudoField' => $tableName, 'idCol' => "{$name}_id");
720 $this->_pseudoConstantsSelect
[$name]['select'] = "{$name}.{$fieldName} as $name";
721 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
723 if ($tableName == 'email_greeting') {
724 $this->_pseudoConstantsSelect
[$name]['join']
725 = " LEFT JOIN civicrm_option_group option_group_email_greeting ON (option_group_email_greeting.name = 'email_greeting')";
726 $this->_pseudoConstantsSelect
[$name]['join'] .=
727 " LEFT JOIN civicrm_option_value email_greeting ON (contact_a.email_greeting_id = email_greeting.value AND option_group_email_greeting.id = email_greeting.option_group_id ) ";
729 elseif ($tableName == 'postal_greeting') {
730 $this->_pseudoConstantsSelect
[$name]['join']
731 = " LEFT JOIN civicrm_option_group option_group_postal_greeting ON (option_group_postal_greeting.name = 'postal_greeting')";
732 $this->_pseudoConstantsSelect
[$name]['join'] .=
733 " LEFT JOIN civicrm_option_value postal_greeting ON (contact_a.postal_greeting_id = postal_greeting.value AND option_group_postal_greeting.id = postal_greeting.option_group_id ) ";
735 elseif ($tableName == 'addressee') {
736 $this->_pseudoConstantsSelect
[$name]['join']
737 = " LEFT JOIN civicrm_option_group option_group_addressee ON (option_group_addressee.name = 'addressee')";
738 $this->_pseudoConstantsSelect
[$name]['join'] .=
739 " LEFT JOIN civicrm_option_value addressee ON (contact_a.addressee_id = addressee.value AND option_group_addressee.id = addressee.option_group_id ) ";
741 $this->_pseudoConstantsSelect
[$name]['table'] = $tableName;
744 $greetField = "{$name}_display";
745 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
746 $this->_element
[$greetField] = 1;
748 $greetField = "{$name}_custom";
749 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
750 $this->_element
[$greetField] = 1;
754 if (!in_array($tableName, array('civicrm_state_province', 'civicrm_country', 'civicrm_county'))) {
755 $this->_tables
[$tableName] = 1;
758 // also get the id of the tableName
759 $tName = substr($tableName, 8);
760 if (in_array($tName, array('country', 'state_province', 'county'))) {
761 if ($tName == 'state_province') {
762 $this->_pseudoConstantsSelect
['state_province_name'] = array(
763 'pseudoField' => "{$tName}",
764 'idCol' => "{$tName}_id",
765 'bao' => 'CRM_Core_BAO_Address',
766 'table' => "civicrm_{$tName}",
767 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
770 $this->_pseudoConstantsSelect
[$tName] = array(
771 'pseudoField' => 'state_province_abbreviation',
772 'idCol' => "{$tName}_id",
773 'table' => "civicrm_{$tName}",
774 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
778 $this->_pseudoConstantsSelect
[$name] = array(
779 'pseudoField' => "{$tName}_id",
780 'idCol' => "{$tName}_id",
781 'bao' => 'CRM_Core_BAO_Address',
782 'table' => "civicrm_{$tName}",
783 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
787 $this->_select
["{$tName}_id"] = "civicrm_address.{$tName}_id as {$tName}_id";
788 $this->_element
["{$tName}_id"] = 1;
790 elseif ($tName != 'contact') {
791 $this->_select
["{$tName}_id"] = "{$tableName}.id as {$tName}_id";
792 $this->_element
["{$tName}_id"] = 1;
795 //special case for phone
796 if ($name == 'phone') {
797 $this->_select
['phone_type_id'] = "civicrm_phone.phone_type_id as phone_type_id";
798 $this->_element
['phone_type_id'] = 1;
801 // if IM then select provider_id also
802 // to get "IM Service Provider" in a file to be exported, CRM-3140
804 $this->_select
['provider_id'] = "civicrm_im.provider_id as provider_id";
805 $this->_element
['provider_id'] = 1;
808 if ($tName == 'contact') {
809 // special case, when current employer is set for Individual contact
810 if ($fieldName == 'organization_name') {
811 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name";
813 elseif ($fieldName != 'id') {
814 if ($fieldName == 'prefix_id') {
815 $this->_pseudoConstantsSelect
['individual_prefix'] = array(
816 'pseudoField' => 'prefix_id',
817 'idCol' => "prefix_id",
818 'bao' => 'CRM_Contact_BAO_Contact',
821 if ($fieldName == 'suffix_id') {
822 $this->_pseudoConstantsSelect
['individual_suffix'] = array(
823 'pseudoField' => 'suffix_id',
824 'idCol' => "suffix_id",
825 'bao' => 'CRM_Contact_BAO_Contact',
828 if ($fieldName == 'gender_id') {
829 $this->_pseudoConstantsSelect
['gender'] = array(
830 'pseudoField' => 'gender_id',
831 'idCol' => "gender_id",
832 'bao' => 'CRM_Contact_BAO_Contact',
835 if ($name == 'communication_style_id') {
836 $this->_pseudoConstantsSelect
['communication_style'] = array(
837 'pseudoField' => 'communication_style_id',
838 'idCol' => "communication_style_id",
839 'bao' => 'CRM_Contact_BAO_Contact',
842 $this->_select
[$name] = "contact_a.{$fieldName} as `$name`";
845 elseif (in_array($tName, array('country', 'county'))) {
846 $this->_pseudoConstantsSelect
[$name]['select'] = "{$field['where']} as `$name`";
847 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
849 elseif ($tName == 'state_province') {
850 $this->_pseudoConstantsSelect
[$tName]['select'] = "{$field['where']} as `$name`";
851 $this->_pseudoConstantsSelect
[$tName]['element'] = $name;
853 elseif (strpos($name, 'contribution_soft_credit') !== FALSE) {
854 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_params
)) {
855 $this->_select
[$name] = "{$field['where']} as `$name`";
859 $this->_select
[$name] = "{$field['where']} as `$name`";
861 if (!in_array($tName, array('state_province', 'country', 'county'))) {
862 $this->_element
[$name] = 1;
867 elseif ($name === 'tags') {
868 $this->_useGroupBy
= TRUE;
869 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags";
870 $this->_element
[$name] = 1;
871 $this->_tables
['civicrm_tag'] = 1;
872 $this->_tables
['civicrm_entity_tag'] = 1;
874 elseif ($name === 'groups') {
875 $this->_useGroupBy
= TRUE;
876 // Duplicates will be created here but better to sort them out in php land.
877 $this->_select
[$name] = "
879 GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')),
880 GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id)
883 $this->_element
[$name] = 1;
884 $this->_tables
['civicrm_group_contact'] = 1;
885 $this->_tables
['civicrm_group_contact_cache'] = 1;
886 $this->_pseudoConstantsSelect
["{$name}"] = array(
887 'pseudoField' => "groups",
891 elseif ($name === 'notes') {
892 // if note field is subject then return subject else body of the note
893 $noteColumn = 'note';
894 if (isset($noteField) && $noteField == 'note_subject') {
895 $noteColumn = 'subject';
898 $this->_useGroupBy
= TRUE;
899 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_note.$noteColumn)) as notes";
900 $this->_element
[$name] = 1;
901 $this->_tables
['civicrm_note'] = 1;
903 elseif ($name === 'current_employer') {
904 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer";
905 $this->_element
[$name] = 1;
909 if ($cfID && !empty($field['is_search_range'])) {
910 // this is a custom field with range search enabled, so we better check for two/from values
911 if (!empty($this->_paramLookup
[$name . '_from'])) {
912 if (!array_key_exists($cfID, $this->_cfIDs
)) {
913 $this->_cfIDs
[$cfID] = array();
915 foreach ($this->_paramLookup
[$name . '_from'] as $pID => $p) {
916 // search in the cdID array for the same grouping
918 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
919 if ($c[3] == $p[3]) {
920 $this->_cfIDs
[$cfID][$cID][2]['from'] = $p[2];
925 $p[2] = array('from' => $p[2]);
926 $this->_cfIDs
[$cfID][] = $p;
930 if (!empty($this->_paramLookup
[$name . '_to'])) {
931 if (!array_key_exists($cfID, $this->_cfIDs
)) {
932 $this->_cfIDs
[$cfID] = array();
934 foreach ($this->_paramLookup
[$name . '_to'] as $pID => $p) {
935 // search in the cdID array for the same grouping
937 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
938 if ($c[4] == $p[4]) {
939 $this->_cfIDs
[$cfID][$cID][2]['to'] = $p[2];
944 $p[2] = array('to' => $p[2]);
945 $this->_cfIDs
[$cfID][] = $p;
952 // add location as hierarchical elements
953 $this->addHierarchicalElements();
955 // add multiple field like website
956 $this->addMultipleElements();
959 CRM_Core_Component
::alterQuery($this, 'select');
961 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'select');
963 if (!empty($this->_cfIDs
)) {
964 // @todo This function is the select function but instead of running 'select' it
965 // is running the whole query.
966 $this->_customQuery
= new CRM_Core_BAO_CustomQuery($this->_cfIDs
, TRUE, $this->_locationSpecificCustomFields
);
967 $this->_customQuery
->query();
968 $this->_select
= array_merge($this->_select
, $this->_customQuery
->_select
);
969 $this->_element
= array_merge($this->_element
, $this->_customQuery
->_element
);
970 $this->_tables
= array_merge($this->_tables
, $this->_customQuery
->_tables
);
971 $this->_whereTables
= array_merge($this->_whereTables
, $this->_customQuery
->_whereTables
);
972 $this->_options
= $this->_customQuery
->_options
;
977 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
979 public function addHierarchicalElements() {
980 if (empty($this->_returnProperties
['location'])) {
983 if (!is_array($this->_returnProperties
['location'])) {
987 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
988 $processed = array();
991 $addressCustomFields = CRM_Core_BAO_CustomField
::getFieldsForImport('Address');
992 $addressCustomFieldIds = array();
994 foreach ($this->_returnProperties
['location'] as $name => $elements) {
995 $lCond = self
::getPrimaryCondition($name);
998 $locationTypeId = array_search($name, $locationTypes);
999 if ($locationTypeId === FALSE) {
1002 $lCond = "location_type_id = $locationTypeId";
1003 $this->_useDistinct
= TRUE;
1005 //commented for CRM-3256
1006 $this->_useGroupBy
= TRUE;
1009 $name = str_replace(' ', '_', $name);
1011 $tName = "$name-location_type";
1012 $ltName = "`$name-location_type`";
1013 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1014 $this->_select
["{$tName}"] = "`$tName`.name as `{$tName}`";
1015 $this->_element
["{$tName}_id"] = 1;
1016 $this->_element
["{$tName}"] = 1;
1018 $locationTypeName = $tName;
1019 $locationTypeJoin = array();
1021 $addAddress = FALSE;
1023 foreach ($elements as $elementFullName => $dontCare) {
1025 $elementName = $elementCmpName = $elementFullName;
1027 if (substr($elementCmpName, 0, 5) == 'phone') {
1028 $elementCmpName = 'phone';
1031 if (in_array($elementCmpName, array_keys($addressCustomFields))) {
1032 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($elementCmpName)) {
1033 $addressCustomFieldIds[$cfID][$name] = 1;
1036 //add address table only once
1037 if ((in_array($elementCmpName, self
::$_locationSpecificFields) ||
!empty($addressCustomFieldIds))
1039 && !in_array($elementCmpName, array('email', 'phone', 'im', 'openid'))
1041 $tName = "$name-address";
1042 $aName = "`$name-address`";
1043 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1044 $this->_element
["{$tName}_id"] = 1;
1045 $addressJoin = "\nLEFT JOIN civicrm_address $aName ON ($aName.contact_id = contact_a.id AND $aName.$lCond)";
1046 $this->_tables
[$tName] = $addressJoin;
1047 $locationTypeJoin[$tName] = " ( $aName.location_type_id = $ltName.id ) ";
1048 $processed[$aName] = 1;
1052 $cond = $elementType = '';
1053 if (strpos($elementName, '-') !== FALSE) {
1054 // this is either phone, email or IM
1055 list($elementName, $elementType) = explode('-', $elementName);
1057 if (($elementName != 'phone') && ($elementName != 'im')) {
1058 $cond = self
::getPrimaryCondition($elementType);
1060 // CRM-13011 : If location type is primary, do not restrict search to the phone
1061 // type id - we want the primary phone, regardless of what type it is.
1062 // Otherwise, restrict to the specified phone type for the given field.
1063 if ((!$cond) && ($elementName == 'phone')) {
1064 $cond = "phone_type_id = '$elementType'";
1066 elseif ((!$cond) && ($elementName == 'im')) {
1067 // IM service provider id, CRM-3140
1068 $cond = "provider_id = '$elementType'";
1070 $elementType = '-' . $elementType;
1073 $field = CRM_Utils_Array
::value($elementName, $this->_fields
);
1075 // hack for profile, add location id
1078 // fix for CRM-882( to handle phone types )
1079 !is_numeric($elementType)
1081 if (is_numeric($name)) {
1082 $field = CRM_Utils_Array
::value($elementName . "-Primary$elementType", $this->_fields
);
1085 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1088 elseif (is_numeric($name)) {
1089 //this for phone type to work
1090 if (in_array($elementName, array('phone', 'phone_ext'))) {
1091 $field = CRM_Utils_Array
::value($elementName . "-Primary" . $elementType, $this->_fields
);
1094 $field = CRM_Utils_Array
::value($elementName . "-Primary", $this->_fields
);
1098 //this is for phone type to work for profile edit
1099 if (in_array($elementName, array('phone', 'phone_ext'))) {
1100 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1103 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId", $this->_fields
);
1108 // Check if there is a value, if so also add to where Clause
1110 if ($this->_params
) {
1112 if (isset($locationTypeId)) {
1113 $nm .= "-$locationTypeId";
1115 if (!is_numeric($elementType)) {
1116 $nm .= "$elementType";
1119 foreach ($this->_params
as $id => $values) {
1120 if ((is_array($values) && $values[0] == $nm) ||
1121 (in_array($elementName, array('phone', 'im'))
1122 && (strpos($values[0], $nm) !== FALSE)
1132 if ($field && isset($field['where'])) {
1133 list($tableName, $fieldName) = explode('.', $field['where'], 2);
1134 $pf = substr($tableName, 8);
1135 $tName = $name . '-' . $pf . $elementType;
1136 if (isset($tableName)) {
1137 if ($tableName == 'civicrm_state_province' ||
$tableName == 'civicrm_country' ||
$tableName == 'civicrm_county') {
1138 $this->_select
["{$tName}_id"] = "{$aName}.{$pf}_id as `{$tName}_id`";
1141 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1144 $this->_element
["{$tName}_id"] = 1;
1145 if (substr($tName, -15) == '-state_province') {
1146 // FIXME: hack to fix CRM-1900
1147 $a = Civi
::settings()->get('address_format');
1149 if (substr_count($a, 'state_province_name') > 0) {
1150 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1151 'pseudoField' => "{$pf}_id",
1152 'idCol' => "{$tName}_id",
1153 'bao' => 'CRM_Core_BAO_Address',
1155 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.name as `{$name}-{$elementFullName}`";
1158 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1159 'pseudoField' => 'state_province_abbreviation',
1160 'idCol' => "{$tName}_id",
1162 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.abbreviation as `{$name}-{$elementFullName}`";
1166 if (substr($elementFullName, 0, 2) == 'im') {
1167 $provider = "{$name}-{$elementFullName}-provider_id";
1168 $this->_select
[$provider] = "`$tName`.provider_id as `{$name}-{$elementFullName}-provider_id`";
1169 $this->_element
[$provider] = 1;
1171 if ($pf == 'country' ||
$pf == 'county') {
1172 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1173 'pseudoField' => "{$pf}_id",
1174 'idCol' => "{$tName}_id",
1175 'bao' => 'CRM_Core_BAO_Address',
1177 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1180 $this->_select
["{$name}-{$elementFullName}"] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1184 if (in_array($pf, array('state_province', 'country', 'county'))) {
1185 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['element'] = "{$name}-{$elementFullName}";
1188 $this->_element
["{$name}-{$elementFullName}"] = 1;
1191 if (empty($processed["`$tName`"])) {
1192 $processed["`$tName`"] = 1;
1193 $newName = $tableName . '_' . $index;
1194 switch ($tableName) {
1195 case 'civicrm_phone':
1196 case 'civicrm_email':
1198 case 'civicrm_openid':
1200 $this->_tables
[$tName] = "\nLEFT JOIN $tableName `$tName` ON contact_a.id = `$tName`.contact_id";
1201 if ($tableName != 'civicrm_phone') {
1202 $this->_tables
[$tName] .= " AND `$tName`.$lCond";
1204 elseif (is_numeric($name)) {
1205 $this->_select
[$tName] = "IF (`$tName`.is_primary = $name, `$tName`.phone, NULL) as `$tName`";
1208 // this special case to add phone type
1210 $phoneTypeCondition = " AND `$tName`.$cond ";
1211 //gross hack to pickup corrupted data also, CRM-7603
1212 if (strpos($cond, 'phone_type_id') !== FALSE) {
1213 $phoneTypeCondition = " AND ( `$tName`.$cond OR `$tName`.phone_type_id IS NULL ) ";
1214 if (!empty($lCond)) {
1215 $phoneTypeCondition .= " AND ( `$tName`.$lCond ) ";
1218 $this->_tables
[$tName] .= $phoneTypeCondition;
1221 //build locationType join
1222 $locationTypeJoin[$tName] = " ( `$tName`.location_type_id = $ltName.id )";
1225 $this->_whereTables
[$tName] = $this->_tables
[$tName];
1229 case 'civicrm_state_province':
1230 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $tName;
1231 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1232 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.state_province_id";
1234 $this->_whereTables
["{$name}-address"] = $addressJoin;
1238 case 'civicrm_country':
1239 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1240 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1241 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.country_id";
1243 $this->_whereTables
["{$name}-address"] = $addressJoin;
1247 case 'civicrm_county':
1248 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1249 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1250 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.county_id";
1252 $this->_whereTables
["{$name}-address"] = $addressJoin;
1258 $this->_whereTables
["{$name}-address"] = $addressJoin;
1267 // add location type join
1268 $ltypeJoin = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locationTypeJoin) . " )";
1269 $this->_tables
[$locationTypeName] = $ltypeJoin;
1271 // table should be present in $this->_whereTables,
1272 // to add its condition in location type join, CRM-3939.
1273 if ($addWhereCount) {
1274 $locClause = array();
1275 foreach ($this->_whereTables
as $tableName => $clause) {
1276 if (!empty($locationTypeJoin[$tableName])) {
1277 $locClause[] = $locationTypeJoin[$tableName];
1281 if (!empty($locClause)) {
1282 $this->_whereTables
[$locationTypeName] = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locClause) . " )";
1287 if (!empty($addressCustomFieldIds)) {
1288 $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds);
1289 foreach ($addressCustomFieldIds as $cfID => $locTypeName) {
1290 foreach ($locTypeName as $name => $dnc) {
1291 $this->_locationSpecificCustomFields
[$cfID] = array($name, array_search($name, $locationTypes));
1292 $fieldName = "$name-custom_{$cfID}";
1293 $tName = "$name-address-custom-{$cfID}";
1294 $aName = "`$name-address-custom-{$cfID}`";
1295 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1296 $this->_element
["{$tName}_id"] = 1;
1297 $this->_select
[$fieldName] = "`$tName`.{$customQuery->_fields[$cfID]['column_name']} as `{$fieldName}`";
1298 $this->_element
[$fieldName] = 1;
1299 $this->_tables
[$tName] = "\nLEFT JOIN {$customQuery->_fields[$cfID]['table_name']} $aName ON ($aName.entity_id = `$name-address`.id)";
1306 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
1308 public function addMultipleElements() {
1309 if (empty($this->_returnProperties
['website'])) {
1312 if (!is_array($this->_returnProperties
['website'])) {
1316 foreach ($this->_returnProperties
['website'] as $key => $elements) {
1317 foreach ($elements as $elementFullName => $dontCare) {
1318 $tName = "website-{$key}-{$elementFullName}";
1319 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1320 $this->_select
["{$tName}"] = "`$tName`.url as `{$tName}`";
1321 $this->_element
["{$tName}_id"] = 1;
1322 $this->_element
["{$tName}"] = 1;
1324 $type = "website-{$key}-website_type_id";
1325 $this->_select
[$type] = "`$tName`.website_type_id as `{$type}`";
1326 $this->_element
[$type] = 1;
1327 $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website `$tName` ON (`$tName`.contact_id = contact_a.id AND `$tName`.website_type_id = $key )";
1333 * Generate the query based on what type of query we need.
1335 * @param bool $count
1336 * @param bool $sortByChar
1337 * @param bool $groupContacts
1338 * @param bool $onlyDeleted
1341 * sql query parts as an array
1343 public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE, $onlyDeleted = FALSE) {
1344 // build permission clause
1345 $this->generatePermissionClause($onlyDeleted, $count);
1348 if (isset($this->_rowCountClause
)) {
1349 $select = "SELECT {$this->_rowCountClause}";
1351 elseif (isset($this->_distinctComponentClause
)) {
1352 // we add distinct to get the right count for components
1353 // for the more complex result set, we use GROUP BY the same id
1355 $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount";
1358 $select = 'SELECT count(DISTINCT contact_a.id) as rowCount';
1360 $from = $this->_simpleFromClause
;
1361 if ($this->_useDistinct
) {
1362 $this->_useGroupBy
= TRUE;
1365 elseif ($sortByChar) {
1366 $select = 'SELECT DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) as sort_name';
1367 $from = $this->_simpleFromClause
;
1369 elseif ($groupContacts) {
1370 $select = 'SELECT contact_a.id as id';
1371 if ($this->_useDistinct
) {
1372 $this->_useGroupBy
= TRUE;
1374 $from = $this->_simpleFromClause
;
1377 if (!empty($this->_paramLookup
['group'])) {
1379 list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup
['group'][0];
1381 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
1382 $this->_paramLookup
['group'][0][1] = key($value);
1385 // Presumably the lines below come into manage groups screen.
1386 // make sure there is only one element
1387 // this is used when we are running under smog and need to know
1388 // how the contact was added (CRM-1203)
1389 $groups = (array) CRM_Utils_Array
::value($this->_paramLookup
['group'][0][1], $this->_paramLookup
['group'][0][2], $this->_paramLookup
['group'][0][2]);
1390 if ((count($this->_paramLookup
['group']) == 1) &&
1391 (count($groups) == 1)
1393 $groupId = $groups[0];
1395 //check if group is saved search
1396 $group = new CRM_Contact_BAO_Group();
1397 $group->id
= $groupId;
1400 if (!isset($group->saved_search_id
)) {
1401 $tbName = "`civicrm_group_contact-{$groupId}`";
1402 // CRM-17254 don't retrieve extra fields if contact_id is specifically requested
1403 // as this will add load to an intentionally light query.
1404 // ideally this code would be removed as it appears to be to support CRM-1203
1405 // and passing in the required returnProperties from the url would
1406 // make more sense that globally applying the requirements of one form.
1407 if (($this->_returnProperties
!= array('contact_id'))) {
1408 $this->_select
['group_contact_id'] = "$tbName.id as group_contact_id";
1409 $this->_element
['group_contact_id'] = 1;
1410 $this->_select
['status'] = "$tbName.status as status";
1411 $this->_element
['status'] = 1;
1415 $this->_useGroupBy
= TRUE;
1417 if ($this->_useDistinct
&& !isset($this->_distinctComponentClause
)) {
1418 if (!($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
)) {
1420 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
1421 $this->_useDistinct
= FALSE;
1422 $this->_useGroupBy
= TRUE;
1426 $select = "SELECT ";
1427 if (isset($this->_distinctComponentClause
)) {
1428 $select .= "{$this->_distinctComponentClause}, ";
1430 $select .= implode(', ', $this->_select
);
1431 $from = $this->_fromClause
;
1435 if (!empty($this->_whereClause
)) {
1436 $where = "WHERE {$this->_whereClause}";
1439 if (!empty($this->_permissionWhereClause
) && empty($this->_displayRelationshipType
)) {
1440 if (empty($where)) {
1441 $where = "WHERE $this->_permissionWhereClause";
1444 $where = "$where AND $this->_permissionWhereClause";
1449 if (!empty($this->_having
)) {
1450 foreach ($this->_having
as $havingSets) {
1451 foreach ($havingSets as $havingSet) {
1452 $havingValue[] = $havingSet;
1455 $having = ' HAVING ' . implode(' AND ', $havingValue);
1458 // if we are doing a transform, do it here
1459 // use the $from, $where and $having to get the contact ID
1460 if ($this->_displayRelationshipType
) {
1461 $this->filterRelatedContacts($from, $where, $having);
1464 return array($select, $from, $where, $having);
1468 * Get where values from the parameters.
1470 * @param string $name
1471 * @param mixed $grouping
1475 public function getWhereValues($name, $grouping) {
1477 foreach ($this->_params
as $values) {
1478 if ($values[0] == $name && $values[3] == $grouping) {
1489 * @param bool $relative
1490 * @param string $from
1493 public static function fixDateValues($relative, &$from, &$to) {
1495 list($from, $to) = CRM_Utils_Date
::getFromTo($relative, $from, $to);
1500 * Convert values from form-appropriate to query-object appropriate.
1502 * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax.
1503 * So, ideally we would convert all fields to look like
1506 * // Set the operator for legacy reasons, but it is ignored
1508 * // array in sql filter syntax
1509 * 2 => array('BETWEEN' => array(1,60),
1514 * There are some examples of the syntax in
1515 * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship
1517 * More notes at CRM_Core_DAO::createSQLFilter
1519 * and a list of supported operators in CRM_Core_DAO
1521 * @param array $formValues
1522 * @param int $wildcard
1523 * @param bool $useEquals
1525 * @param string $apiEntity
1527 * @param array $entityReferenceFields
1528 * Field names of any entity reference fields (which will need reformatting to IN syntax).
1532 public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL,
1533 $entityReferenceFields = array()) {
1535 if (empty($formValues)) {
1539 self
::filterCountryFromValuesIfStateExists($formValues);
1541 foreach ($formValues as $id => &$val) {
1542 // CRM-19374 - we don't want to change $val in $formValues.
1543 // Assign it to a temp variable which operates while iteration.
1546 if (self
::isAlreadyProcessedForQueryFormat($values)) {
1547 $params[] = $values;
1551 self
::legacyConvertFormValues($id, $values);
1553 // The form uses 1 field to represent two db fields
1554 if ($id == 'contact_type' && $values && (!is_array($values) ||
!array_intersect(array_keys($values), CRM_Core_DAO
::acceptedSQLOperators()))) {
1555 $contactType = array();
1557 foreach ((array) $values as $key => $type) {
1558 $types = explode('__', is_numeric($type) ?
$key : $type);
1559 $contactType[$types[0]] = $types[0];
1560 // Add sub-type if specified
1561 if (!empty($types[1])) {
1562 $subType[$types[1]] = $types[1];
1565 $params[] = array('contact_type', 'IN', $contactType, 0, 0);
1567 $params[] = array('contact_sub_type', 'IN', $subType, 0, 0);
1570 elseif ($id == 'privacy') {
1571 if (is_array($formValues['privacy'])) {
1572 $op = !empty($formValues['privacy']['do_not_toggle']) ?
'=' : '!=';
1573 foreach ($formValues['privacy'] as $key => $value) {
1575 $params[] = array($key, $op, $value, 0, 0);
1580 elseif ($id == 'email_on_hold') {
1581 if ($formValues['email_on_hold']['on_hold']) {
1582 $params[] = array('on_hold', '=', $formValues['email_on_hold']['on_hold'], 0, 0);
1585 elseif (substr($id, 0, 7) == 'custom_'
1587 substr($id, -9, 9) == '_relative'
1588 ||
substr($id, -5, 5) == '_from'
1589 ||
substr($id, -3, 3) == '_to'
1592 self
::convertCustomRelativeFields($formValues, $params, $values, $id);
1594 elseif (preg_match('/_date_relative$/', $id) ||
1595 $id == 'event_relative' ||
1596 $id == 'case_from_relative' ||
1597 $id == 'case_to_relative' ||
1598 $id == 'participant_relative'
1600 if ($id == 'event_relative') {
1601 $fromRange = 'event_start_date_low';
1602 $toRange = 'event_end_date_high';
1604 elseif ($id == 'participant_relative') {
1605 $fromRange = 'participant_register_date_low';
1606 $toRange = 'participant_register_date_high';
1608 elseif ($id == 'case_from_relative') {
1609 $fromRange = 'case_from_start_date_low';
1610 $toRange = 'case_from_start_date_high';
1612 elseif ($id == 'case_to_relative') {
1613 $fromRange = 'case_to_end_date_low';
1614 $toRange = 'case_to_end_date_high';
1617 $dateComponent = explode('_date_relative', $id);
1618 $fromRange = "{$dateComponent[0]}_date_low";
1619 $toRange = "{$dateComponent[0]}_date_high";
1622 if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
1623 CRM_Contact_BAO_Query
::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
1627 elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
1629 $params[] = array($id, 'IN', explode(',', $values), 0, 0);
1632 $values = CRM_Contact_BAO_Query
::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity);
1637 $params[] = $values;
1644 * Function to support legacy format for groups and tags.
1647 * @param array|int $values
1650 public static function legacyConvertFormValues($id, &$values) {
1651 $legacyElements = array(
1656 'membership_type_id',
1657 'membership_status_id',
1659 if (in_array($id, $legacyElements) && is_array($values)) {
1660 // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
1661 // as per the recent Search fixes $values need to be in standard array(id1, id2) format
1662 $values = CRM_Utils_Array
::convertCheckboxFormatToArray($values);
1667 * Fix values from query from/to something no-one cared enough to document.
1670 * @param array $values
1671 * @param int $wildcard
1672 * @param bool $useEquals
1674 * @param string $apiEntity
1676 * @return array|null
1678 public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) {
1679 // skip a few search variables
1680 static $skipWhere = NULL;
1681 static $likeNames = NULL;
1684 // Change camelCase EntityName to lowercase with underscores
1685 $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity);
1687 // check if $value is in OK (Operator as Key) format as used by Get API
1688 if (CRM_Utils_System
::isNull($values)) {
1700 'display_relationship_type',
1704 if (in_array($id, $skipWhere) ||
1705 substr($id, 0, 4) == '_qf_' ||
1706 substr($id, 0, 7) == 'hidden_'
1712 (substr($id, 0, strlen($apiEntity)) != $apiEntity) &&
1713 (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_') &&
1714 (substr($id, 0, 7) != 'custom_')
1716 $id = $apiEntity . '_' . $id;
1720 $likeNames = array('sort_name', 'email', 'note', 'display_name');
1723 // email comes in via advanced search
1724 // so use wildcard always
1725 if ($id == 'email') {
1729 if (!$useEquals && in_array($id, $likeNames)) {
1730 $result = array($id, 'LIKE', $values, 0, 1);
1732 elseif (is_string($values) && strpos($values, '%') !== FALSE) {
1733 $result = array($id, 'LIKE', $values, 0, 0);
1735 elseif ($id == 'contact_type' ||
1736 (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO
::acceptedSQLOperators(), TRUE))
1738 $result = array($id, 'IN', $values, 0, $wildcard);
1741 $result = array($id, '=', $values, 0, $wildcard);
1748 * Get the where clause for a single field.
1750 * @param array $values
1751 * @param string $apiEntity
1753 public function whereClauseSingle(&$values, $apiEntity = NULL) {
1754 // do not process custom fields or prefixed contact ids or component params
1755 if (CRM_Core_BAO_CustomField
::getKeyID($values[0]) ||
1756 (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) ||
1757 (substr($values[0], 0, 13) == 'contribution_') ||
1758 (substr($values[0], 0, 6) == 'event_') ||
1759 (substr($values[0], 0, 12) == 'participant_') ||
1760 (substr($values[0], 0, 7) == 'member_') ||
1761 (substr($values[0], 0, 6) == 'grant_') ||
1762 (substr($values[0], 0, 7) == 'pledge_') ||
1763 (substr($values[0], 0, 5) == 'case_') ||
1764 (substr($values[0], 0, 10) == 'financial_') ||
1765 (substr($values[0], 0, 8) == 'payment_') ||
1766 (substr($values[0], 0, 11) == 'membership_')
1771 // skip for hook injected fields / params
1772 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
1773 if (array_key_exists($values[0], $extFields)) {
1777 switch ($values[0]) {
1778 case 'deleted_contacts':
1779 $this->deletedContacts($values);
1782 case 'contact_sub_type':
1783 $this->contactSubType($values);
1787 $this->group($values);
1791 // so we resolve this into a list of groups & proceed as if they had been
1793 list($name, $op, $value, $grouping, $wildcard) = $values;
1794 $values[0] = 'group';
1796 $this->_paramLookup
['group'][0][0] = 'group';
1797 $this->_paramLookup
['group'][0][1] = 'IN';
1798 $this->_paramLookup
['group'][0][2] = $values[2] = $this->getGroupsFromTypeCriteria($value);
1799 $this->group($values);
1802 // case tag comes from find contacts
1804 $this->tagSearch($values);
1808 case 'contact_tags':
1809 $this->tag($values);
1814 case 'note_subject':
1815 $this->notes($values);
1819 $this->ufUser($values);
1823 case 'display_name':
1824 $this->sortName($values);
1828 case 'postal_greeting':
1829 case 'email_greeting':
1830 $this->greetings($values);
1835 $this->email($values, $apiEntity);
1838 case 'phone_numeric':
1839 $this->phone_numeric($values);
1842 case 'phone_phone_type_id':
1843 case 'phone_location_type_id':
1844 $this->phone_option_group($values);
1847 case 'street_address':
1848 $this->street_address($values);
1851 case 'street_number':
1852 $this->street_number($values);
1855 case 'sortByCharacter':
1856 $this->sortByCharacter($values);
1859 case 'location_type':
1860 $this->locationType($values);
1864 $this->county($values);
1867 case 'state_province':
1868 case 'state_province_id':
1869 case 'state_province_name':
1870 $this->stateProvince($values);
1875 $this->country($values, FALSE);
1879 case 'postal_code_low':
1880 case 'postal_code_high':
1881 $this->postalCode($values);
1884 case 'activity_date':
1885 case 'activity_date_low':
1886 case 'activity_date_high':
1887 case 'activity_role':
1888 case 'activity_status_id':
1889 case 'activity_status':
1890 case 'followup_parent_id':
1892 case 'source_contact_id':
1893 case 'activity_subject':
1894 case 'test_activities':
1895 case 'activity_type_id':
1896 case 'activity_type':
1897 case 'activity_survey_id':
1898 case 'activity_tags':
1899 case 'activity_taglist':
1900 case 'activity_test':
1901 case 'activity_campaign_id':
1902 case 'activity_engagement_level':
1904 case 'activity_result':
1905 case 'source_contact':
1906 CRM_Activity_BAO_Query
::whereClauseSingle($values, $this);
1911 case 'birth_date_low':
1912 case 'birth_date_high':
1913 case 'deceased_date_low':
1914 case 'deceased_date_high':
1915 $this->demographics($values);
1918 case 'age_asof_date':
1919 // handled by demographics
1922 case 'log_date_low':
1923 case 'log_date_high':
1924 $this->modifiedDates($values);
1928 $this->changeLog($values);
1931 case 'do_not_phone':
1932 case 'do_not_email':
1935 case 'do_not_trade':
1937 $this->privacy($values);
1940 case 'privacy_options':
1941 $this->privacyOptions($values);
1944 case 'privacy_operator':
1945 case 'privacy_toggle':
1946 // these are handled by privacy options
1949 case 'preferred_communication_method':
1950 $this->preferredCommunication($values);
1953 case 'relation_type_id':
1954 case 'relation_start_date_high':
1955 case 'relation_start_date_low':
1956 case 'relation_end_date_high':
1957 case 'relation_end_date_low':
1958 case 'relation_target_name':
1959 case 'relation_status':
1960 case 'relation_date_low':
1961 case 'relation_date_high':
1962 $this->relationship($values);
1963 $this->_relationshipValuesAdded
= TRUE;
1966 case 'task_status_id':
1967 $this->task($values);
1971 // since this case is handled with the above
1974 case 'prox_distance':
1975 CRM_Contact_BAO_ProximityQuery
::process($this, $values);
1978 case 'prox_street_address':
1980 case 'prox_postal_code':
1981 case 'prox_state_province_id':
1982 case 'prox_country_id':
1983 // handled by the proximity_distance clause
1987 $this->restWhere($values);
1993 * Given a list of conditions in params generate the required where clause.
1995 * @param string $apiEntity
1999 public function whereClause($apiEntity = NULL) {
2000 $this->_where
[0] = array();
2001 $this->_qill
[0] = array();
2003 $this->includeContactIds();
2004 if (!empty($this->_params
)) {
2005 foreach (array_keys($this->_params
) as $id) {
2006 if (empty($this->_params
[$id][0])) {
2009 // check for both id and contact_id
2010 if ($this->_params
[$id][0] == 'id' ||
$this->_params
[$id][0] == 'contact_id') {
2011 $this->_where
[0][] = self
::buildClause("contact_a.id", $this->_params
[$id][1], $this->_params
[$id][2]);
2014 $this->whereClauseSingle($this->_params
[$id], $apiEntity);
2018 CRM_Core_Component
::alterQuery($this, 'where');
2020 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'where');
2023 if ($this->_customQuery
) {
2024 // Added following if condition to avoid the wrong value display for 'my account' / any UF info.
2025 // Hope it wont affect the other part of civicrm.. if it does please remove it.
2026 if (!empty($this->_customQuery
->_where
)) {
2027 $this->_where
= CRM_Utils_Array
::crmArrayMerge($this->_where
, $this->_customQuery
->_where
);
2030 $this->_qill
= CRM_Utils_Array
::crmArrayMerge($this->_qill
, $this->_customQuery
->_qill
);
2034 $andClauses = array();
2037 if (!empty($this->_where
)) {
2038 foreach ($this->_where
as $grouping => $values) {
2039 if ($grouping > 0 && !empty($values)) {
2040 $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) ';
2045 if (!empty($this->_where
[0])) {
2046 $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where
[0]) . ' ) ';
2048 if (!empty($clauses)) {
2049 $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
2052 if ($validClauses > 1) {
2053 $this->_useDistinct
= TRUE;
2057 return implode(' AND ', $andClauses);
2061 * Generate where clause for any parameters not already handled.
2063 * @param array $values
2067 public function restWhere(&$values) {
2068 $name = CRM_Utils_Array
::value(0, $values);
2069 $op = CRM_Utils_Array
::value(1, $values);
2070 $value = CRM_Utils_Array
::value(2, $values);
2071 $grouping = CRM_Utils_Array
::value(3, $values);
2072 $wildcard = CRM_Utils_Array
::value(4, $values);
2074 if (isset($grouping) && empty($this->_where
[$grouping])) {
2075 $this->_where
[$grouping] = array();
2078 $multipleFields = array('url');
2080 //check if the location type exists for fields
2082 $locType = explode('-', $name);
2084 if (!in_array($locType[0], $multipleFields)) {
2085 //add phone type if exists
2086 if (isset($locType[2]) && $locType[2]) {
2087 $locType[2] = CRM_Core_DAO
::escapeString($locType[2]);
2091 $field = CRM_Utils_Array
::value($name, $this->_fields
);
2094 $field = CRM_Utils_Array
::value($locType[0], $this->_fields
);
2103 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
2104 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
2105 if (isset($locType[1]) && is_numeric($locType[1])) {
2106 $lType = $locationType[$locType[1]];
2109 $field['title'] .= " ($lType)";
2112 if (substr($name, 0, 14) === 'state_province') {
2113 if (isset($locType[1]) && is_numeric($locType[1])) {
2115 $aName = "{$lType}-address";
2116 $where = "`$aName`.state_province_id";
2119 $where = "civicrm_address.state_province_id";
2122 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value);
2123 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
2124 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2126 elseif (!empty($field['pseudoconstant'])) {
2127 $this->optionValueQuery(
2128 $name, $op, $value, $grouping,
2129 'CRM_Contact_DAO_Contact',
2135 if ($name == 'gender_id') {
2136 self
::$_openedPanes[ts('Demographics')] = TRUE;
2139 elseif (substr($name, 0, 7) === 'country' ||
substr($name, 0, 6) === 'county') {
2140 $name = (substr($name, 0, 7) === 'country') ?
"country_id" : "county_id";
2141 if (isset($locType[1]) && is_numeric($locType[1])) {
2143 $aName = "{$lType}-address";
2144 $where = "`$aName`.$name";
2147 $where = "civicrm_address.$name";
2150 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value, 'Positive');
2152 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
2153 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2155 elseif ($name === 'world_region') {
2156 $this->optionValueQuery(
2157 $name, $op, $value, $grouping,
2165 elseif ($name === 'is_deceased') {
2166 $this->setQillAndWhere($name, $op, $value, $grouping, $field);
2167 self
::$_openedPanes[ts('Demographics')] = TRUE;
2169 elseif ($name === 'created_date' ||
$name === 'modified_date' ||
$name === 'deceased_date' ||
$name === 'birth_date') {
2170 $appendDateTime = TRUE;
2171 if ($name === 'deceased_date' ||
$name === 'birth_date') {
2172 $appendDateTime = FALSE;
2173 self
::$_openedPanes[ts('Demographics')] = TRUE;
2175 $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime);
2177 elseif ($name === 'contact_id') {
2178 if (is_int($value)) {
2179 $this->_where
[$grouping][] = self
::buildClause($field['where'], $op, $value);
2180 $this->_qill
[$grouping][] = "$field[title] $op $value";
2183 elseif ($name === 'name') {
2184 $value = $strtolower(CRM_Core_DAO
::escapeString($value));
2187 $value = self
::getWildCardedValue($wildcard, $op, $value);
2189 $wc = self
::caseImportant($op) ?
"LOWER({$field['where']})" : "{$field['where']}";
2190 $this->_where
[$grouping][] = self
::buildClause($wc, $op, "'$value'");
2191 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2193 elseif ($name === 'current_employer') {
2194 $value = $strtolower(CRM_Core_DAO
::escapeString($value));
2197 $value = self
::getWildCardedValue($wildcard, $op, $value);
2199 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.organization_name)" : "contact_a.organization_name";
2200 $ceWhereClause = self
::buildClause($wc, $op,
2203 $ceWhereClause .= " AND contact_a.contact_type = 'Individual'";
2204 $this->_where
[$grouping][] = $ceWhereClause;
2205 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2207 elseif ($name === 'email_greeting') {
2208 $filterCondition = array('greeting_type' => 'email_greeting');
2209 $this->optionValueQuery(
2210 $name, $op, $value, $grouping,
2211 CRM_Core_PseudoConstant
::greeting($filterCondition),
2213 ts('Email Greeting')
2216 elseif ($name === 'postal_greeting') {
2217 $filterCondition = array('greeting_type' => 'postal_greeting');
2218 $this->optionValueQuery(
2219 $name, $op, $value, $grouping,
2220 CRM_Core_PseudoConstant
::greeting($filterCondition),
2222 ts('Postal Greeting')
2225 elseif ($name === 'addressee') {
2226 $filterCondition = array('greeting_type' => 'addressee');
2227 $this->optionValueQuery(
2228 $name, $op, $value, $grouping,
2229 CRM_Core_PseudoConstant
::greeting($filterCondition),
2234 elseif (substr($name, 0, 4) === 'url-') {
2235 $tName = 'civicrm_website';
2236 $this->_whereTables
[$tName] = $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )";
2237 $value = $strtolower(CRM_Core_DAO
::escapeString($value));
2240 $value = self
::getWildCardedValue($wildcard, $op, $value);
2243 $this->_where
[$grouping][] = $d = self
::buildClause('civicrm_website.url', $op, $value);
2244 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2246 elseif ($name === 'contact_is_deleted') {
2247 $this->setQillAndWhere('is_deleted', $op, $value, $grouping, $field);
2249 elseif (!empty($field['where'])) {
2251 if (!empty($field['type'])) {
2252 $type = CRM_Utils_Type
::typeToString($field['type']);
2255 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2257 if (isset($locType[1]) &&
2258 is_numeric($locType[1])
2262 //get the location name
2263 list($tName, $fldName) = self
::getLocationTableName($field['where'], $locType);
2265 $fieldName = "LOWER(`$tName`.$fldName)";
2267 // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
2268 $this->_tables
[$tName] = $this->_whereTables
[$tName] = 1;
2272 if ($tableName == 'civicrm_contact') {
2273 $fieldName = "LOWER(contact_a.{$fieldName})";
2276 if ($op != 'IN' && !is_numeric($value) && !is_array($value)) {
2277 $fieldName = "LOWER({$field['where']})";
2280 $fieldName = "{$field['where']}";
2285 list($qillop, $qillVal) = self
::buildQillForFieldValue(NULL, $field['title'], $value, $op);
2286 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(
2287 1 => $field['title'],
2289 3 => (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) ?
$qillVal : "'$qillVal'"));
2291 if (is_array($value)) {
2292 // traditionally an array being passed has been a fatal error. We can take advantage of this to add support
2293 // for api style operators for functions that hit this point without worrying about regression
2294 // (the previous comments indicated the condition for hitting this point were unknown
2295 // per CRM-14743 we are adding modified_date & created_date operator support
2296 $operations = array_keys($value);
2297 foreach ($operations as $operator) {
2298 if (!in_array($operator, CRM_Core_DAO
::acceptedSQLOperators())) {
2299 //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators
2300 //so this condition will satisfy the search for now
2301 if (strpos($op, 'IN') !== FALSE) {
2302 $value = array($op => $value);
2304 // we don't know when this might happen
2306 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", array(1 => $operator)));
2310 $this->_where
[$grouping][] = CRM_Core_DAO
::createSQLFilter($fieldName, $value, $type);
2313 if (!strpos($op, 'IN')) {
2314 $value = $strtolower($value);
2318 $value = self
::getWildCardedValue($wildcard, $op, $value);
2321 $this->_where
[$grouping][] = self
::buildClause($fieldName, $op, $value, $type);
2325 if ($setTables && isset($field['where'])) {
2326 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2327 if (isset($tableName)) {
2328 $this->_tables
[$tableName] = 1;
2329 $this->_whereTables
[$tableName] = 1;
2342 public static function getLocationTableName(&$where, &$locType) {
2343 if (isset($locType[1]) && is_numeric($locType[1])) {
2344 list($tbName, $fldName) = explode(".", $where);
2346 //get the location name
2347 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
2348 $specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
2349 if (in_array($locType[0], $specialFields)) {
2350 //hack to fix / special handing for phone_ext
2351 if ($locType[0] == 'phone_ext') {
2352 $locType[0] = 'phone';
2354 if (isset($locType[2]) && $locType[2]) {
2355 $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}";
2358 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2361 elseif (in_array($locType[0],
2366 'street_number_suffix',
2368 'supplemental_address_1',
2369 'supplemental_address_2',
2372 'postal_code_suffix',
2378 //fix for search by profile with address fields.
2379 $tName = "{$locationType[$locType[1]]}-address";
2381 elseif (in_array($locType[0],
2389 $tName = "{$locationType[$locType[1]]}-email";
2391 elseif ($locType[0] == 'provider_id') {
2392 $tName = "{$locationType[$locType[1]]}-im";
2394 elseif ($locType[0] == 'openid') {
2395 $tName = "{$locationType[$locType[1]]}-openid";
2398 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2400 $tName = str_replace(' ', '_', $tName);
2401 return array($tName, $fldName);
2403 CRM_Core_Error
::fatal();
2407 * Given a result dao, extract the values and return that array
2409 * @param CRM_Core_DAO $dao
2412 * values for this query
2414 public function store($dao) {
2417 foreach ($this->_element
as $key => $dontCare) {
2418 if (property_exists($dao, $key)) {
2419 if (strpos($key, '-') !== FALSE) {
2420 $values = explode('-', $key);
2421 $lastElement = array_pop($values);
2423 $cnt = count($values);
2425 foreach ($values as $v) {
2426 if (!array_key_exists($v, $current)) {
2427 $current[$v] = array();
2429 //bad hack for im_provider
2430 if ($lastElement == 'provider_id') {
2431 if ($count < $cnt) {
2432 $current = &$current[$v];
2435 $lastElement = "{$v}_{$lastElement}";
2439 $current = &$current[$v];
2444 $current[$lastElement] = $dao->$key;
2447 $value[$key] = $dao->$key;
2455 * Getter for tables array.
2459 public function tables() {
2460 return $this->_tables
;
2464 * Where tables is sometimes used to create the from clause, but, not reliably, set this AND set tables
2465 * It's unclear the intent - there is a 'simpleFrom' clause which takes whereTables into account & a fromClause which doesn't
2466 * logic may have eroded
2469 public function whereTables() {
2470 return $this->_whereTables
;
2474 * Generate the where clause (used in match contacts and permissions)
2476 * @param array $params
2477 * @param array $fields
2478 * @param array $tables
2479 * @param $whereTables
2480 * @param bool $strict
2484 public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) {
2485 $query = new CRM_Contact_BAO_Query($params, NULL, $fields,
2489 $tables = array_merge($query->tables(), $tables);
2490 $whereTables = array_merge($query->whereTables(), $whereTables);
2492 return $query->_whereClause
;
2496 * Create the from clause.
2498 * @param array $tables
2499 * Tables that need to be included in this from clause.
2500 * if null, return mimimal from clause (i.e. civicrm_contact)
2501 * @param array $inner
2502 * Tables that should be inner-joined.
2503 * @param array $right
2504 * Tables that should be right-joined.
2506 * @param bool $primaryLocation
2512 public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1) {
2514 $from = ' FROM civicrm_contact contact_a';
2515 if (empty($tables)) {
2519 if (!empty($tables['civicrm_worldregion'])) {
2520 $tables = array_merge(array('civicrm_country' => 1), $tables);
2523 if ((!empty($tables['civicrm_state_province']) ||
!empty($tables['civicrm_country']) ||
2524 CRM_Utils_Array
::value('civicrm_county', $tables)
2525 ) && empty($tables['civicrm_address'])
2527 $tables = array_merge(array('civicrm_address' => 1),
2532 // add group_contact and group table is subscription history is present
2533 if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) {
2534 $tables = array_merge(array(
2535 'civicrm_group' => 1,
2536 'civicrm_group_contact' => 1,
2542 // to handle table dependencies of components
2543 CRM_Core_Component
::tableNames($tables);
2544 // to handle table dependencies of hook injected tables
2545 CRM_Contact_BAO_Query_Hook
::singleton()->setTableDependency($tables);
2547 //format the table list according to the weight
2548 $info = CRM_Core_TableHierarchy
::info();
2550 foreach ($tables as $key => $value) {
2552 if (strpos($key, '-') !== FALSE) {
2553 $keyArray = explode('-', $key);
2554 $k = CRM_Utils_Array
::value('civicrm_' . $keyArray[1], $info, 99);
2556 elseif (strpos($key, '_') !== FALSE) {
2557 $keyArray = explode('_', $key);
2558 if (is_numeric(array_pop($keyArray))) {
2559 $k = CRM_Utils_Array
::value(implode('_', $keyArray), $info, 99);
2562 $k = CRM_Utils_Array
::value($key, $info, 99);
2566 $k = CRM_Utils_Array
::value($key, $info, 99);
2568 $tempTable[$k . ".$key"] = $key;
2571 $newTables = array();
2572 foreach ($tempTable as $key) {
2573 $newTables[$key] = $tables[$key];
2576 $tables = $newTables;
2578 foreach ($tables as $name => $value) {
2583 if (!empty($inner[$name])) {
2586 elseif (!empty($right[$name])) {
2594 // if there is already a join statement in value, use value itself
2595 if (strpos($value, 'JOIN')) {
2596 $from .= " $value ";
2599 $from .= " $side JOIN $name ON ( $value ) ";
2604 case 'civicrm_address':
2605 if ($primaryLocation) {
2606 $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )";
2609 //CRM-14263 further handling of address joins further down...
2610 $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id ) ";
2614 case 'civicrm_phone':
2615 $from .= " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) ";
2618 case 'civicrm_email':
2619 $from .= " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) ";
2623 $from .= " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) ";
2627 $from .= " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) ";
2628 $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'";
2629 $from .= " $side JOIN civicrm_option_value im_provider ON (civicrm_im.provider_id = im_provider.value AND option_group_imProvider.id = im_provider.option_group_id)";
2632 case 'civicrm_openid':
2633 $from .= " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id AND civicrm_openid.is_primary = 1 )";
2636 case 'civicrm_worldregion':
2637 $from .= " $side JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id ";
2638 $from .= " $side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ";
2641 case 'civicrm_location_type':
2642 $from .= " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id ";
2645 case 'civicrm_group':
2646 $from .= " $side JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id ";
2649 case 'civicrm_group_contact':
2650 $from .= " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id ";
2653 case 'civicrm_group_contact_cache':
2654 $from .= " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id ";
2657 case 'civicrm_activity':
2658 case 'civicrm_activity_tag':
2659 case 'activity_type':
2660 case 'activity_status':
2662 case 'civicrm_activity_contact':
2663 case 'source_contact':
2664 $from .= CRM_Activity_BAO_Query
::from($name, $mode, $side);
2667 case 'civicrm_entity_tag':
2668 $from .= " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND
2669 civicrm_entity_tag.entity_id = contact_a.id ) ";
2672 case 'civicrm_note':
2673 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND
2674 contact_a.id = civicrm_note.entity_id ) ";
2677 case 'civicrm_subscription_history':
2678 $from .= " $side JOIN civicrm_subscription_history
2679 ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id
2680 AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id";
2683 case 'civicrm_relationship':
2684 if (self
::$_relType == 'reciprocal') {
2685 if (self
::$_relationshipTempTable) {
2686 // we have a temptable to join on
2687 $tbl = self
::$_relationshipTempTable;
2688 $from .= " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id";
2691 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)";
2692 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id OR civicrm_relationship.contact_id_b = contact_b.id)";
2695 elseif (self
::$_relType == 'b') {
2696 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )";
2697 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
2700 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )";
2701 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
2706 $from .= " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')";
2707 $from .= " INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)";
2711 $from .= " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id ";
2714 case 'civicrm_grant':
2715 $from .= CRM_Grant_BAO_Query
::from($name, $mode, $side);
2718 case 'civicrm_website':
2719 $from .= " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id ";
2723 $locationTypeName = '';
2724 if (strpos($name, '-address') != 0) {
2725 $locationTypeName = 'address';
2727 elseif (strpos($name, '-phone') != 0) {
2728 $locationTypeName = 'phone';
2730 elseif (strpos($name, '-email') != 0) {
2731 $locationTypeName = 'email';
2733 elseif (strpos($name, '-im') != 0) {
2734 $locationTypeName = 'im';
2736 elseif (strpos($name, '-openid') != 0) {
2737 $locationTypeName = 'openid';
2740 if ($locationTypeName) {
2741 //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
2742 $parts = explode('-', $name);
2743 $locationID = array_search($parts[0], CRM_Core_BAO_Address
::buildOptions('location_type_id', 'get', array('name' => $parts[0])));
2744 $from .= " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
2747 $from .= CRM_Core_Component
::from($name, $mode, $side);
2749 $from .= CRM_Contact_BAO_Query_Hook
::singleton()->buildSearchfrom($name, $mode, $side);
2758 * WHERE / QILL clause for deleted_contacts
2760 * @param array $values
2762 public function deletedContacts($values) {
2763 list($_, $_, $value, $grouping, $_) = $values;
2765 // *prepend* to the relevant grouping as this is quite an important factor
2766 array_unshift($this->_qill
[$grouping], ts('Search in Trash'));
2771 * Where / qill clause for contact_type
2775 public function contactType(&$values) {
2776 list($name, $op, $value, $grouping, $wildcard) = $values;
2778 $subTypes = array();
2781 // account for search builder mapping multiple values
2782 if (!is_array($value)) {
2783 $values = self
::parseSearchBuilderString($value, 'String');
2784 if (is_array($values)) {
2785 $value = array_flip($values);
2789 if (is_array($value)) {
2790 foreach ($value as $k => $v) {
2795 if (strpos($k, CRM_Core_DAO
::VALUE_SEPARATOR
)) {
2796 list($contactType, $subType) = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $k, 2);
2799 if (!empty($subType)) {
2800 $subTypes[$subType] = 1;
2802 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2807 $contactTypeANDSubType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value, 2);
2808 $contactType = $contactTypeANDSubType[0];
2809 $subType = CRM_Utils_Array
::value(1, $contactTypeANDSubType);
2810 if (!empty($subType)) {
2811 $subTypes[$subType] = 1;
2813 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2817 if (!empty($clause)) {
2819 if ($op == 'IN' ||
$op == 'NOT IN') {
2820 $this->_where
[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')';
2823 $type = array_pop($clause);
2824 $this->_where
[$grouping][] = self
::buildClause("contact_a.contact_type", $op, $contactType);
2827 $this->_qill
[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill);
2829 if (!empty($subTypes)) {
2830 $this->includeContactSubTypes($subTypes, $grouping);
2836 * Where / qill clause for contact_sub_type
2840 public function contactSubType(&$values) {
2841 list($name, $op, $value, $grouping, $wildcard) = $values;
2842 $this->includeContactSubTypes($value, $grouping, $op);
2850 public function includeContactSubTypes($value, $grouping, $op = 'LIKE') {
2852 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2854 $value = $value[$op];
2858 $alias = "contact_a.contact_sub_type";
2859 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
2861 $op = str_replace('IN', 'LIKE', $op);
2862 $op = str_replace('=', 'LIKE', $op);
2863 $op = str_replace('!', 'NOT ', $op);
2865 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
2866 $this->_where
[$grouping][] = self
::buildClause($alias, $op, $value, 'String');
2868 elseif (is_array($value)) {
2869 foreach ($value as $k => $v) {
2870 $clause[$k] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($v, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
2874 $clause[$value] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($value, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
2877 if (!empty($clause)) {
2878 $this->_where
[$grouping][] = "( " . implode(' OR ', $clause) . " )";
2880 $this->_qill
[$grouping][] = ts('Contact Subtype %1 ', array(1 => $qillOperators[$op])) . implode(' ' . ts('or') . ' ', array_keys($clause));
2884 * Where / qill clause for groups.
2888 public function group($values) {
2889 list($name, $op, $value, $grouping, $wildcard) = $values;
2891 // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first
2892 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2894 $value = $value[$op];
2897 // Replace pseudo operators from search builder
2898 $op = str_replace('EMPTY', 'NULL', $op);
2900 if (strpos($op, 'NULL')) {
2904 if (count($value) > 1) {
2905 if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) {
2906 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", array(1 => $op)));
2908 $this->_useDistinct
= TRUE;
2911 if (isset($value)) {
2912 $value = CRM_Utils_Array
::value($op, $value, $value);
2917 $isNotOp = ($op == 'NOT IN' ||
$op == '!=');
2920 $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
2922 is_array($gcsValues[2])
2924 foreach ($gcsValues[2] as $k => $v) {
2926 $statii[] = "'" . CRM_Utils_Type
::escape($k, 'String') . "'";
2931 $statii[] = '"Added"';
2934 $ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op);
2935 $isSmart = (!$ssClause) ?
FALSE : TRUE;
2936 if (!is_array($value) &&
2937 count($statii) == 1 &&
2938 $statii[0] == '"Added"' &&
2941 if (!empty($value) && CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $value, 'saved_search_id')) {
2945 $groupClause = NULL;
2948 $groupIds = implode(',', (array) $value);
2949 $gcTable = "`civicrm_group_contact-{$groupIds}`";
2950 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
2952 $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")";
2954 $this->_tables
[$gcTable] = $this->_whereTables
[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
2955 if (strpos($op, 'IN') !== FALSE) {
2956 $groupClause = "{$gcTable}.group_id $op ( $groupIds )";
2958 elseif ($op == '!=') {
2959 $groupClause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds)";
2962 $groupClause = "{$gcTable}.group_id $op $groupIds";
2967 $and = ($op == 'IS NULL') ?
'AND' : 'OR';
2969 $groupClause = "( ( $groupClause ) $and ( $ssClause ) )";
2972 $groupClause = $ssClause;
2976 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
2977 $this->_qill
[$grouping][] = ts("Group(s) %1 %2", array(1 => $qillop, 2 => $qillVal));
2978 if (strpos($op, 'NULL') === FALSE) {
2979 $this->_qill
[$grouping][] = ts("Group Status %1", array(1 => implode(' ' . ts('or') . ' ', $statii)));
2982 $this->_where
[$grouping][] = $groupClause;
2987 * Function translates selection of group type into a list of groups.
2992 public function getGroupsFromTypeCriteria($value) {
2993 $groupIds = array();
2994 foreach ((array) $value as $groupTypeValue) {
2995 $groupList = CRM_Core_PseudoConstant
::group($groupTypeValue);
2996 $groupIds = ($groupIds +
$groupList);
3002 * @param array $groups
3003 * @param string $tableAlias
3004 * @param string $joinTable
3007 * @return null|string
3009 public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a", $op) {
3010 $isNullOp = (strpos($op, 'NULL') !== FALSE);
3011 $groupsIds = $groups;
3012 if (!$isNullOp && !$groups) {
3015 elseif (strpos($op, 'IN') !== FALSE) {
3016 $groups = array($op => $groups);
3018 elseif (is_array($groups) && count($groups)) {
3019 $groups = array('IN' => $groups);
3022 // Find all the groups that are part of a saved search.
3023 $smartGroupClause = self
::buildClause("id", $op, $groups, 'Int');
3025 SELECT id, cache_date, saved_search_id, children
3027 WHERE $smartGroupClause
3028 AND ( saved_search_id != 0
3029 OR saved_search_id IS NOT NULL
3030 OR children IS NOT NULL )
3033 $group = CRM_Core_DAO
::executeQuery($sql);
3035 while ($group->fetch()) {
3036 $this->_useDistinct
= TRUE;
3037 if (!$this->_smartGroupCache ||
$group->cache_date
== NULL) {
3038 CRM_Contact_BAO_GroupContactCache
::load($group);
3041 if ($group->N
== 0) {
3046 $tableAlias = "`civicrm_group_contact_cache_";
3047 $tableAlias .= ($isNullOp) ?
"a`" : implode(',', (array) $groupsIds) . "`";
3050 $this->_tables
[$tableAlias] = $this->_whereTables
[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.id = {$tableAlias}.contact_id ";
3051 return self
::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
3055 * Where / qill clause for cms users
3059 public function ufUser(&$values) {
3060 list($name, $op, $value, $grouping, $wildcard) = $values;
3063 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3065 $this->_qill
[$grouping][] = ts('CMS User');
3067 elseif ($value == 0) {
3068 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3070 $this->_where
[$grouping][] = " civicrm_uf_match.contact_id IS NULL";
3071 $this->_qill
[$grouping][] = ts('Not a CMS User');
3076 * All tag search specific.
3078 * @param array $values
3080 public function tagSearch(&$values) {
3081 list($name, $op, $value, $grouping, $wildcard) = $values;
3084 $value = "%{$value}%";
3086 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3087 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3089 $etTable = "`civicrm_entity_tag-" . $value . "`";
3090 $tTable = "`civicrm_tag-" . $value . "`";
3092 if ($useAllTagTypes[2]) {
3093 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3094 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id)
3095 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )";
3097 // search tag in cases
3098 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3099 $tCaseTable = "`civicrm_case_tag-" . $value . "`";
3100 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3101 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3102 LEFT JOIN civicrm_case
3103 ON (civicrm_case_contact.case_id = civicrm_case.id
3104 AND civicrm_case.is_deleted = 0 )
3105 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id )
3106 LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )";
3107 // search tag in activities
3108 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3109 $tActTable = "`civicrm_act_tag-" . $value . "`";
3110 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3111 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3113 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3114 = " LEFT JOIN civicrm_activity_contact
3115 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3116 LEFT JOIN civicrm_activity
3117 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3118 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3119 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id )
3120 LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )";
3122 $this->_where
[$grouping][] = "({$tTable}.name $op '" . $value . "' OR {$tCaseTable}.name $op '" . $value . "' OR {$tActTable}.name $op '" . $value . "')";
3123 $this->_qill
[$grouping][] = ts('Tag %1 %2', array(1 => $tagTypesText[2], 2 => $op)) . ' ' . $value;
3126 $etTable = "`civicrm_entity_tag-" . $value . "`";
3127 $tTable = "`civicrm_tag-" . $value . "`";
3128 $this->_tables
[$etTable] = $this->_whereTables
[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND
3129 {$etTable}.entity_table = 'civicrm_contact' )
3130 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) ";
3132 $this->_where
[$grouping][] = self
::buildClause("{$tTable}.name", $op, $value, 'String');
3133 $this->_qill
[$grouping][] = ts('Tagged %1', array(1 => $op)) . ' ' . $value;
3138 * Where / qill clause for tag
3140 * @param array $values
3142 public function tag(&$values) {
3143 list($name, $op, $value, $grouping, $wildcard) = $values;
3145 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, array('onlyActive' => FALSE));
3146 // API/Search Builder format array(operator => array(values))
3147 if (is_array($value)) {
3148 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3150 $value = $value[$op];
3152 if (count($value) > 1) {
3153 $this->_useDistinct
= TRUE;
3155 $value = implode(',', (array) $value);
3158 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3159 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3161 $etTable = "`civicrm_entity_tag-" . $value . "`";
3163 if ($useAllTagTypes[2]) {
3164 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3165 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3167 // search tag in cases
3168 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3169 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3170 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3172 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3173 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3174 LEFT JOIN civicrm_case
3175 ON (civicrm_case_contact.case_id = civicrm_case.id
3176 AND civicrm_case.is_deleted = 0 )
3177 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) ";
3178 // search tag in activities
3179 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3180 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3181 = " LEFT JOIN civicrm_activity_contact
3182 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3183 LEFT JOIN civicrm_activity
3184 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3185 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3186 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) ";
3189 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3190 $this->_where
[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)";
3193 $this->_where
[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
3197 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3198 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3201 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3202 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3203 $op = str_replace('EMPTY', 'NULL', $op);
3204 $this->_where
[$grouping][] = "{$etTable}.tag_id $op";
3206 // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag
3207 elseif ($op == '!=') {
3208 $this->_where
[$grouping][] = "{$etTable}.entity_id NOT IN (SELECT entity_id FROM civicrm_entity_tag cet WHERE cet.entity_table = 'civicrm_contact' AND " . self
::buildClause("cet.tag_id", '=', $value, 'Int') . ")";
3210 elseif ($op == '=' ||
strstr($op, 'IN')) {
3211 $op = ($op == '=') ?
'IN' : $op;
3212 $this->_where
[$grouping][] = "{$etTable}.tag_id $op ( $value )";
3215 $this->_qill
[$grouping][] = ts('Tagged %1 %2', array(1 => $qillop, 2 => $qillVal));
3219 * Where/qill clause for notes
3221 * @param array $values
3223 public function notes(&$values) {
3224 list($name, $op, $value, $grouping, $wildcard) = $values;
3226 $noteOptionValues = $this->getWhereValues('note_option', $grouping);
3227 $noteOption = CRM_Utils_Array
::value('2', $noteOptionValues, '6');
3228 $noteOption = ($name == 'note_body') ?
2 : (($name == 'note_subject') ?
3 : $noteOption);
3230 $this->_useDistinct
= TRUE;
3232 $this->_tables
['civicrm_note'] = $this->_whereTables
['civicrm_note']
3233 = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) ";
3235 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
3237 $value = $strtolower(CRM_Core_DAO
::escapeString($n));
3239 if (strpos($value, '%') === FALSE) {
3240 $value = "%$value%";
3244 elseif ($op == 'IS NULL' ||
$op == 'IS NOT NULL') {
3250 if ($noteOption %
2 == 0) {
3251 $clauses[] = self
::buildClause('civicrm_note.note', $op, $value, 'String');
3252 $label = ts('Note: Body Only');
3254 if ($noteOption %
3 == 0) {
3255 $clauses[] = self
::buildClause('civicrm_note.subject', $op, $value, 'String');
3256 $label = $label ?
ts('Note: Body and Subject') : ts('Note: Subject Only');
3258 $this->_where
[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
3259 list($qillOp, $qillVal) = self
::buildQillForFieldValue(NULL, $name, $n, $op);
3260 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillOp, 3 => $qillVal));
3264 * @param string $name
3270 public function nameNullOrEmptyOp($name, $op, $grouping) {
3274 $this->_where
[$grouping][] = "contact_a.$name $op";
3275 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3279 $this->_where
[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')";
3280 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3283 case 'IS NOT EMPTY':
3284 $this->_where
[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')";
3285 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3294 * Where / qill clause for sort_name
3296 * @param array $values
3298 public function sortName(&$values) {
3299 list($fieldName, $op, $value, $grouping, $wildcard) = $values;
3301 // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY
3302 if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) {
3306 $input = $value = trim($value);
3308 if (!strlen($value)) {
3312 $config = CRM_Core_Config
::singleton();
3316 //By default, $sub elements should be joined together with OR statements (don't change this variable).
3319 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
3321 $firstChar = substr($value, 0, 1);
3322 $lastChar = substr($value, -1, 1);
3323 $quotes = array("'", '"');
3324 // If string is quoted, strip quotes and otherwise don't alter it
3325 if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) {
3326 $value = trim($value, implode('', $quotes));
3328 // Replace spaces with wildcards for a LIKE operation
3329 // UNLESS string contains a comma (this exception is a tiny bit questionable)
3330 elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) {
3331 $value = str_replace(' ', '%', $value);
3333 $value = $strtolower(CRM_Core_DAO
::escapeString(trim($value)));
3334 if (strlen($value)) {
3335 $fieldsub = array();
3336 $value = "'" . self
::getWildCardedValue($wildcard, $op, $value) . "'";
3337 if ($fieldName == 'sort_name') {
3338 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.sort_name)" : "contact_a.sort_name";
3341 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.display_name)" : "contact_a.display_name";
3343 $fieldsub[] = " ( $wc $op $value )";
3344 if ($config->includeNickNameInName
) {
3345 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.nick_name)" : "contact_a.nick_name";
3346 $fieldsub[] = " ( $wc $op $value )";
3348 if ($config->includeEmailInName
) {
3349 $fieldsub[] = " ( civicrm_email.email $op $value ) ";
3351 $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
3354 $sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
3356 $this->_where
[$grouping][] = $sub;
3357 if ($config->includeEmailInName
) {
3358 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3359 $this->_qill
[$grouping][] = ts('Name or Email') . " $op - '$input'";
3362 $this->_qill
[$grouping][] = ts('Name') . " $op - '$input'";
3367 * Where/qill clause for greeting fields.
3369 * @param array $values
3371 public function greetings(&$values) {
3372 list($name, $op, $value, $grouping, $wildcard) = $values;
3373 $name .= '_display';
3375 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
3376 $this->_qill
[$grouping][] = ts('Greeting %1 %2', array(1 => $qillop, 2 => $qillVal));
3377 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value, 'String');
3381 * Where / qill clause for email
3383 * @param array $values
3384 * @param string $apiEntity
3386 protected function email(&$values, $apiEntity) {
3387 list($name, $op, $value, $grouping, $wildcard) = $values;
3388 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3390 // CRM-18147: for Contact's GET API, email fieldname got appended with its entity as in {$apiEntiy}_{$name}
3391 // so following code is use build whereClause for contact's primart email id
3392 if (!empty($apiEntity)) {
3393 $dataType = 'String';
3394 if ($name == 'email_id') {
3395 $dataType = 'Integer';
3399 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.is_primary', '=', 1, 'Integer');
3400 $this->_where
[$grouping][] = self
::buildClause("civicrm_email.$name", $op, $value, $dataType);
3404 $n = strtolower(trim($value));
3406 if (substr($n, 0, 1) == '"' &&
3407 substr($n, -1, 1) == '"'
3409 $n = substr($n, 1, -1);
3410 $value = CRM_Core_DAO
::escapeString($n);
3414 $value = self
::getWildCardedValue($wildcard, $op, $n);
3416 $this->_qill
[$grouping][] = ts('Email') . " $op '$n'";
3417 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, $value, 'String');
3420 $this->_qill
[$grouping][] = ts('Email') . " $op ";
3421 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, NULL, 'String');
3426 * Where / qill clause for phone number
3428 * @param array $values
3430 public function phone_numeric(&$values) {
3431 list($name, $op, $value, $grouping, $wildcard) = $values;
3432 // Strip non-numeric characters; allow wildcards
3433 $number = preg_replace('/[^\d%]/', '', $value);
3435 if (strpos($number, '%') === FALSE) {
3436 $number = "%$number%";
3439 $this->_qill
[$grouping][] = ts('Phone number contains') . " $number";
3440 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String');
3441 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3446 * Where / qill clause for phone type/location
3448 * @param array $values
3450 public function phone_option_group($values) {
3451 list($name, $op, $value, $grouping, $wildcard) = $values;
3452 $option = ($name == 'phone_phone_type_id' ?
'phone_type_id' : 'location_type_id');
3453 $options = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', $option);
3454 $optionName = $options[$value];
3455 $this->_qill
[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ?
ts('type') : ('location')) . " $op $optionName";
3456 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer');
3457 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3461 * Where / qill clause for street_address.
3463 * @param array $values
3465 public function street_address(&$values) {
3466 list($name, $op, $value, $grouping, $wildcard) = $values;
3475 $value = strtolower($n);
3476 if (strpos($value, '%') === FALSE) {
3477 // only add wild card if not there
3478 $value = "%{$value}%";
3481 $this->_where
[$grouping][] = self
::buildClause('LOWER(civicrm_address.street_address)', $op, $value, 'String');
3482 $this->_qill
[$grouping][] = ts('Street') . " $op '$n'";
3485 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_address', $op, NULL, 'String');
3486 $this->_qill
[$grouping][] = ts('Street') . " $op ";
3489 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3493 * Where / qill clause for street_unit.
3495 * @param array $values
3497 public function street_number(&$values) {
3498 list($name, $op, $value, $grouping, $wildcard) = $values;
3506 if (strtolower($n) == 'odd') {
3507 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )";
3508 $this->_qill
[$grouping][] = ts('Street Number is odd');
3510 elseif (strtolower($n) == 'even') {
3511 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )";
3512 $this->_qill
[$grouping][] = ts('Street Number is even');
3515 $value = strtolower($n);
3517 $this->_where
[$grouping][] = self
::buildClause('LOWER(civicrm_address.street_number)', $op, $value, 'String');
3518 $this->_qill
[$grouping][] = ts('Street Number') . " $op '$n'";
3521 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3525 * Where / qill clause for sorting by character.
3527 * @param array $values
3529 public function sortByCharacter(&$values) {
3530 list($name, $op, $value, $grouping, $wildcard) = $values;
3532 $name = trim($value);
3533 $cond = " contact_a.sort_name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($name)) . "%'";
3534 $this->_where
[$grouping][] = $cond;
3535 $this->_qill
[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', array(1 => $name));
3539 * Where / qill clause for including contact ids.
3541 public function includeContactIDs() {
3542 if (!$this->_includeContactIds ||
empty($this->_params
)) {
3546 $contactIds = array();
3547 foreach ($this->_params
as $id => $values) {
3548 if (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
3549 $contactIds[] = substr($values[0], CRM_Core_Form
::CB_PREFIX_LEN
);
3552 if (!empty($contactIds)) {
3553 $this->_where
[0][] = " ( contact_a.id IN (" . implode(',', $contactIds) . " ) ) ";
3558 * Where / qill clause for postal code.
3560 * @param array $values
3562 public function postalCode(&$values) {
3563 // skip if the fields dont have anything to do with postal_code
3564 if (empty($this->_fields
['postal_code'])) {
3568 list($name, $op, $value, $grouping, $wildcard) = $values;
3570 // Handle numeric postal code range searches properly by casting the column as numeric
3571 if (is_numeric($value)) {
3572 $field = "IF (civicrm_address.postal_code REGEXP '^[0-9]+$', CAST(civicrm_address.postal_code AS UNSIGNED), 0)";
3573 $val = CRM_Utils_Type
::escape($value, 'Integer');
3576 $field = 'civicrm_address.postal_code';
3577 // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string.
3578 if (!is_array($value)) {
3579 $val = CRM_Utils_Type
::escape($value, 'String');
3582 // Do we need to escape values here? I would expect buildClause does.
3587 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3589 if ($name == 'postal_code') {
3590 $this->_where
[$grouping][] = self
::buildClause($field, $op, $val, 'String');
3591 $this->_qill
[$grouping][] = ts('Postal code') . " {$op} {$value}";
3593 elseif ($name == 'postal_code_low') {
3594 $this->_where
[$grouping][] = " ( $field >= '$val' ) ";
3595 $this->_qill
[$grouping][] = ts('Postal code greater than or equal to \'%1\'', array(1 => $value));
3597 elseif ($name == 'postal_code_high') {
3598 $this->_where
[$grouping][] = " ( $field <= '$val' ) ";
3599 $this->_qill
[$grouping][] = ts('Postal code less than or equal to \'%1\'', array(1 => $value));
3604 * Where / qill clause for location type.
3606 * @param array $values
3607 * @param null $status
3611 public function locationType(&$values, $status = NULL) {
3612 list($name, $op, $value, $grouping, $wildcard) = $values;
3614 if (is_array($value)) {
3615 $this->_where
[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')';
3616 $this->_tables
['civicrm_address'] = 1;
3617 $this->_whereTables
['civicrm_address'] = 1;
3619 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
3621 foreach ($value as $id) {
3622 $names[] = $locationType[$id];
3625 $this->_primaryLocation
= FALSE;
3628 $this->_qill
[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3631 return implode(' ' . ts('or') . ' ', $names);
3638 * @param bool $fromStateProvince
3640 * @return array|NULL
3642 public function country(&$values, $fromStateProvince = TRUE) {
3643 list($name, $op, $value, $grouping, $wildcard) = $values;
3645 if (!$fromStateProvince) {
3646 $stateValues = $this->getWhereValues('state_province', $grouping);
3647 if (!empty($stateValues)) {
3648 // return back to caller if there are state province values
3649 // since that handles this case
3654 $countryClause = $countryQill = NULL;
3655 if ($values && !empty($value)) {
3656 $this->_tables
['civicrm_address'] = 1;
3657 $this->_whereTables
['civicrm_address'] = 1;
3659 $countryClause = self
::buildClause('civicrm_address.country_id', $op, $value, 'Positive');
3660 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, 'country_id', $value, $op);
3661 $countryQill = ts("%1 %2 %3", array(1 => 'Country', 2 => $qillop, 3 => $qillVal));
3663 if (!$fromStateProvince) {
3664 $this->_where
[$grouping][] = $countryClause;
3665 $this->_qill
[$grouping][] = $countryQill;
3669 if ($fromStateProvince) {
3670 if (!empty($countryClause)) {
3673 " ...AND... " . $countryQill,
3677 return array(NULL, NULL);
3683 * Where / qill clause for county (if present).
3685 * @param array $values
3686 * @param null $status
3690 public function county(&$values, $status = NULL) {
3691 list($name, $op, $value, $grouping, $wildcard) = $values;
3693 if (!is_array($value)) {
3694 // force the county to be an array
3695 $value = array($value);
3698 // check if the values are ids OR names of the counties
3699 $inputFormat = 'id';
3700 foreach ($value as $v) {
3701 if (!is_numeric($v)) {
3702 $inputFormat = 'name';
3710 elseif ($op == '!=') {
3714 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3715 $op = str_replace('EMPTY', 'NULL', $op);
3718 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3719 $clause = "civicrm_address.county_id $op";
3721 elseif ($inputFormat == 'id') {
3722 $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')';
3724 $county = CRM_Core_PseudoConstant
::county();
3725 foreach ($value as $id) {
3726 $names[] = CRM_Utils_Array
::value($id, $county);
3730 $inputClause = array();
3731 $county = CRM_Core_PseudoConstant
::county();
3732 foreach ($value as $name) {
3733 $name = trim($name);
3734 $inputClause[] = CRM_Utils_Array
::key($name, $county);
3736 $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')';
3739 $this->_tables
['civicrm_address'] = 1;
3740 $this->_whereTables
['civicrm_address'] = 1;
3742 $this->_where
[$grouping][] = $clause;
3744 $this->_qill
[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3747 return implode(' ' . ts('or') . ' ', $names);
3752 * Where / qill clause for state/province AND country (if present).
3754 * @param array $values
3755 * @param null $status
3759 public function stateProvince(&$values, $status = NULL) {
3760 list($name, $op, $value, $grouping, $wildcard) = $values;
3762 $stateClause = self
::buildClause('civicrm_address.state_province_id', $op, $value, 'Positive');
3763 $this->_tables
['civicrm_address'] = 1;
3764 $this->_whereTables
['civicrm_address'] = 1;
3766 $countryValues = $this->getWhereValues('country', $grouping);
3767 list($countryClause, $countryQill) = $this->country($countryValues, TRUE);
3768 if ($countryClause) {
3769 $clause = "( $stateClause AND $countryClause )";
3772 $clause = $stateClause;
3775 $this->_where
[$grouping][] = $clause;
3776 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
3778 $this->_qill
[$grouping][] = ts("State/Province %1 %2 %3", array(1 => $qillop, 2 => $qillVal, 3 => $countryQill));
3781 return implode(' ' . ts('or') . ' ', $qillVal) . $countryQill;
3786 * Where / qill clause for change log.
3788 * @param array $values
3790 public function changeLog(&$values) {
3791 list($name, $op, $value, $grouping, $wildcard) = $values;
3793 $targetName = $this->getWhereValues('changed_by', $grouping);
3798 $name = trim($targetName[2]);
3799 $name = strtolower(CRM_Core_DAO
::escapeString($name));
3800 $name = $targetName[4] ?
"%$name%" : $name;
3801 $this->_where
[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
3802 $this->_tables
['civicrm_log'] = $this->_whereTables
['civicrm_log'] = 1;
3803 $this->_qill
[$grouping][] = ts('Modified By') . " $name";
3809 public function modifiedDates($values) {
3810 $this->_useDistinct
= TRUE;
3812 // CRM-11281, default to added date if not set
3813 $fieldTitle = ts('Added Date');
3814 $fieldName = 'created_date';
3815 foreach (array_keys($this->_params
) as $id) {
3816 if ($this->_params
[$id][0] == 'log_date') {
3817 if ($this->_params
[$id][2] == 2) {
3818 $fieldTitle = ts('Modified Date');
3819 $fieldName = 'modified_date';
3824 $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle);
3826 self
::$_openedPanes[ts('Change Log')] = TRUE;
3832 public function demographics(&$values) {
3833 list($name, $op, $value, $grouping, $wildcard) = $values;
3835 if (($name == 'age_low') ||
($name == 'age_high')) {
3836 $this->ageRangeQueryBuilder($values,
3837 'contact_a', 'age', 'birth_date', ts('Age')
3840 elseif (($name == 'birth_date_low') ||
($name == 'birth_date_high')) {
3842 $this->dateQueryBuilder($values,
3843 'contact_a', 'birth_date', 'birth_date', ts('Birth Date')
3846 elseif (($name == 'deceased_date_low') ||
($name == 'deceased_date_high')) {
3848 $this->dateQueryBuilder($values,
3849 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date')
3853 self
::$_openedPanes[ts('Demographics')] = TRUE;
3859 public function privacy(&$values) {
3860 list($name, $op, $value, $grouping, $wildcard) = $values;
3861 //fixed for profile search listing CRM-4633
3862 if (strpbrk($value, "[")) {
3863 $value = "'{$value}'";
3865 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
3868 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
3870 $field = CRM_Utils_Array
::value($name, $this->_fields
);
3871 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
3872 $title = $field ?
$field['title'] : $name;
3873 $this->_qill
[$grouping][] = "$title $op $value";
3879 public function privacyOptions($values) {
3880 list($name, $op, $value, $grouping, $wildcard) = $values;
3882 if (empty($value) ||
!is_array($value)) {
3886 // get the operator and toggle values
3887 $opValues = $this->getWhereValues('privacy_operator', $grouping);
3890 strtolower($opValues[2] == 'AND')
3895 $toggleValues = $this->getWhereValues('privacy_toggle', $grouping);
3897 if ($toggleValues &&
3898 $toggleValues[2] == 2
3905 foreach ($value as $dontCare => $pOption) {
3906 $clauses[] = " ( contact_a.{$pOption} = 1 ) ";
3907 $field = CRM_Utils_Array
::value($pOption, $this->_fields
);
3908 $title = $field ?
$field['title'] : $pOption;
3909 $qill[] = " $title = 1 ";
3912 $this->_where
[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )';
3913 $this->_qill
[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )';
3919 public function preferredCommunication(&$values) {
3920 list($name, $op, $value, $grouping, $wildcard) = $values;
3922 if (!is_array($value)) {
3923 $value = str_replace(array('(', ')'), '', explode(",", $value));
3925 elseif (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3927 $value = $value[$op];
3929 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
3931 if (self
::caseImportant($op)) {
3932 $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value);
3933 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
3934 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
3937 $this->_where
[$grouping][] = self
::buildClause("contact_a.preferred_communication_method", $op, $value);
3938 $this->_qill
[$grouping][] = ts('Preferred Communication Method %1 %2', array(1 => $qillop, 2 => $qillVal));
3942 * Where / qill clause for relationship.
3944 * @param array $values
3946 public function relationship(&$values) {
3947 list($name, $op, $value, $grouping, $wildcard) = $values;
3948 if ($this->_relationshipValuesAdded
) {
3951 // also get values array for relation_target_name
3952 // for relationship search we always do wildcard
3953 $relationType = $this->getWhereValues('relation_type_id', $grouping);
3954 $targetName = $this->getWhereValues('relation_target_name', $grouping);
3955 $relStatus = $this->getWhereValues('relation_status', $grouping);
3956 $relPermission = $this->getWhereValues('relation_permission', $grouping);
3957 $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
3959 $nameClause = $name = NULL;
3961 $name = trim($targetName[2]);
3962 if (substr($name, 0, 1) == '"' &&
3963 substr($name, -1, 1) == '"'
3965 $name = substr($name, 1, -1);
3966 $name = strtolower(CRM_Core_DAO
::escapeString($name));
3967 $nameClause = "= '$name'";
3970 $name = strtolower(CRM_Core_DAO
::escapeString($name));
3971 $nameClause = "LIKE '%{$name}%'";
3975 $rTypeValues = array();
3976 if (!empty($relationType)) {
3977 $rel = explode('_', $relationType[2]);
3978 self
::$_relType = $rel[1];
3979 $params = array('id' => $rel[0]);
3980 $rType = CRM_Contact_BAO_RelationshipType
::retrieve($params, $rTypeValues);
3982 if (!empty($rTypeValues) && $rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
3983 // if we don't know which end of the relationship we are dealing with we'll create a temp table
3984 //@todo unless we are dealing with a target group
3985 self
::$_relType = 'reciprocal';
3987 // if we are creating a temp table we build our own where for the relationship table
3988 $relationshipTempTable = NULL;
3989 if (self
::$_relType == 'reciprocal' && empty($targetGroup)) {
3991 self
::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO
::createTempTableName('civicrm_rel');
3993 $where[$grouping][] = " sort_name $nameClause ";
3997 $where = &$this->_where
;
3999 $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )";
4003 $allRelationshipType = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
4005 if ($nameClause ||
!$targetGroup) {
4006 if (!empty($relationType)) {
4007 $this->_qill
[$grouping][] = $allRelationshipType[$relationType[2]] . " $name";
4010 $this->_qill
[$grouping][] = $name;
4014 //check to see if the target contact is in specified group
4016 //add contacts from static groups
4017 $this->_tables
['civicrm_relationship_group_contact'] = $this->_whereTables
['civicrm_relationship_group_contact']
4018 = " LEFT JOIN civicrm_group_contact civicrm_relationship_group_contact ON civicrm_relationship_group_contact.contact_id = contact_b.id AND civicrm_relationship_group_contact.status = 'Added'";
4019 $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" .
4020 implode(",", $targetGroup[2]) . ") ) ";
4022 //add contacts from saved searches
4023 $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", "contact_b", $op);
4025 //set the group where clause
4027 $groupWhere[] = "( " . $ssWhere . " )";
4029 $this->_where
[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
4031 //Get the names of the target groups for the qill
4032 $groupNames = CRM_Core_PseudoConstant
::group();
4033 $qillNames = array();
4034 foreach ($targetGroup[2] as $groupId) {
4035 if (array_key_exists($groupId, $groupNames)) {
4036 $qillNames[] = $groupNames[$groupId];
4039 if (!empty($relationType)) {
4040 $this->_qill
[$grouping][] = $allRelationshipType[$relationType[2]] . " ( " . implode(", ", $qillNames) . " )";
4043 $this->_qill
[$grouping][] = implode(", ", $qillNames);
4047 // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
4048 $today = date('Ymd');
4049 //check for active, inactive and all relation status
4050 if ($relStatus[2] == 0) {
4051 $where[$grouping][] = "(
4052 civicrm_relationship.is_active = 1 AND
4053 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
4054 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
4056 $this->_qill
[$grouping][] = ts('Relationship - Active and Current');
4058 elseif ($relStatus[2] == 1) {
4059 $where[$grouping][] = "(
4060 civicrm_relationship.is_active = 0 OR
4061 civicrm_relationship.end_date < {$today} OR
4062 civicrm_relationship.start_date > {$today}
4064 $this->_qill
[$grouping][] = ts('Relationship - Inactive or not Current');
4068 if (in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
)) {
4071 $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
4073 //check for permissioned, non-permissioned and all permissioned relations
4074 if ($relPermission[2] == 1) {
4075 $where[$grouping][] = "(
4076 civicrm_relationship.is_permission_a_b = 1
4078 $this->_qill
[$grouping][] = ts('Relationship - Permissioned');
4080 elseif ($relPermission[2] == 2) {
4081 //non-allowed permission relationship.
4082 $where[$grouping][] = "(
4083 civicrm_relationship.is_permission_a_b = 0
4085 $this->_qill
[$grouping][] = ts('Relationship - Non-permissioned');
4088 $this->addRelationshipDateClauses($grouping, $where);
4089 if (!empty($relationType) && !empty($rType) && isset($rType->id
)) {
4090 $where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id
;
4092 $this->_tables
['civicrm_relationship'] = $this->_whereTables
['civicrm_relationship'] = 1;
4093 $this->_useDistinct
= TRUE;
4094 $this->_relationshipValuesAdded
= TRUE;
4095 // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
4097 // @todo creating a temp table could be expanded to group filter
4098 // as even creating a temp table of all relationships is much much more efficient than
4099 // an OR in the join
4100 if ($relationshipTempTable) {
4102 if (!empty($where[$grouping])) {
4103 $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
4104 $whereClause = str_replace('contact_b', 'c', $whereClause);
4107 CREATE TEMPORARY TABLE {$relationshipTempTable}
4108 (SELECT contact_id_b as contact_id, civicrm_relationship.id
4109 FROM civicrm_relationship
4110 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
4113 (SELECT contact_id_a as contact_id, civicrm_relationship.id
4114 FROM civicrm_relationship
4115 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id
4118 CRM_Core_DAO
::executeQuery($sql);
4124 * Add start & end date criteria in
4125 * @param string $grouping
4126 * @param array $where
4127 * = array to add where clauses to, in case you are generating a temp table.
4128 * not the main query.
4130 public function addRelationshipDateClauses($grouping, &$where) {
4131 $dateValues = array();
4137 foreach ($dateTypes as $dateField) {
4138 $dateValueLow = $this->getWhereValues('relation_' . $dateField . '_low', $grouping);
4139 $dateValueHigh = $this->getWhereValues('relation_' . $dateField . '_high', $grouping);
4140 if (!empty($dateValueLow)) {
4141 $date = date('Ymd', strtotime($dateValueLow[2]));
4142 $where[$grouping][] = "civicrm_relationship.$dateField >= $date";
4143 $this->_qill
[$grouping][] = ($dateField == 'end_date' ?
ts('Relationship Ended on or After') : ts('Relationship Recorded Start Date On or After')) . " " . CRM_Utils_Date
::customFormat($date);
4145 if (!empty($dateValueHigh)) {
4146 $date = date('Ymd', strtotime($dateValueHigh[2]));
4147 $where[$grouping][] = "civicrm_relationship.$dateField <= $date";
4148 $this->_qill
[$grouping][] = ($dateField == 'end_date' ?
ts('Relationship Ended on or Before') : ts('Relationship Recorded Start Date On or Before')) . " " . CRM_Utils_Date
::customFormat($date);
4154 * Default set of return properties.
4159 * derault return properties
4161 public static function &defaultReturnProperties($mode = 1) {
4162 if (!isset(self
::$_defaultReturnProperties)) {
4163 self
::$_defaultReturnProperties = array();
4166 if (!isset(self
::$_defaultReturnProperties[$mode])) {
4167 // add activity return properties
4168 if ($mode & CRM_Contact_BAO_Query
::MODE_ACTIVITY
) {
4169 self
::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query
::defaultReturnProperties($mode, FALSE);
4172 self
::$_defaultReturnProperties[$mode] = CRM_Core_Component
::defaultReturnProperties($mode, FALSE);
4175 if (empty(self
::$_defaultReturnProperties[$mode])) {
4176 self
::$_defaultReturnProperties[$mode] = array(
4179 'legal_identifier' => 1,
4180 'external_identifier' => 1,
4181 'contact_type' => 1,
4182 'contact_sub_type' => 1,
4184 'display_name' => 1,
4185 'preferred_mail_format' => 1,
4192 'formal_title' => 1,
4193 'communication_style_id' => 1,
4196 'street_address' => 1,
4197 'supplemental_address_1' => 1,
4198 'supplemental_address_2' => 1,
4201 'postal_code_suffix' => 1,
4202 'state_province' => 1,
4204 'world_region' => 1,
4211 'household_name' => 1,
4212 'organization_name' => 1,
4213 'deceased_date' => 1,
4218 'current_employer' => 1,
4219 // FIXME: should we use defaultHierReturnProperties() for the below?
4220 'do_not_email' => 1,
4223 'do_not_phone' => 1,
4224 'do_not_trade' => 1,
4226 'contact_is_deleted' => 1,
4227 'preferred_communication_method' => 1,
4228 'preferred_language' => 1,
4232 return self
::$_defaultReturnProperties[$mode];
4236 * Get primary condition for a sql clause.
4240 * @return string|NULL
4242 public static function getPrimaryCondition($value) {
4243 if (is_numeric($value)) {
4244 $value = (int ) $value;
4245 return ($value == 1) ?
'is_primary = 1' : 'is_primary = 0';
4251 * Wrapper for a simple search query.
4253 * @param array $params
4254 * @param array $returnProperties
4255 * @param bool $count
4259 public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) {
4260 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
4261 list($select, $from, $where, $having) = $query->query();
4262 $groupBy = ($query->_useGroupBy
) ?
'GROUP BY contact_a.id' : '';
4264 return "$select $from $where $groupBy $having";
4268 * These are stub comments as this function needs more explanation - particularly in terms of how it
4269 * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.
4271 * This function was originally written as a wrapper for the api query but is called from multiple places
4272 * in the core code directly so the name is misleading. This function does not use the searchQuery function
4273 * but it is unclear as to whehter that is historical or there is a reason
4274 * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function
4276 * @param array $params
4277 * @param array $returnProperties
4278 * @param null $fields
4279 * @param string $sort
4280 * @param int $offset
4281 * @param int $row_count
4282 * @param bool $smartGroupCache
4283 * ?? update smart group cache?.
4284 * @param bool $count
4285 * Return count obnly.
4286 * @param bool $skipPermissions
4287 * Should permissions be ignored or should the logged in user's permissions be applied.
4289 * This basically correlates to the component.
4293 public static function apiQuery(
4295 $returnProperties = NULL,
4300 $smartGroupCache = TRUE,
4302 $skipPermissions = TRUE,
4307 $query = new CRM_Contact_BAO_Query(
4308 $params, $returnProperties,
4309 NULL, TRUE, FALSE, $mode,
4311 TRUE, $smartGroupCache,
4316 //this should add a check for view deleted if permissions are enabled
4317 if ($skipPermissions) {
4318 $query->_skipDeleteClause
= TRUE;
4320 $query->generatePermissionClause(FALSE, $count);
4322 // note : this modifies _fromClause and _simpleFromClause
4323 $query->includePseudoFieldsJoin($sort);
4325 list($select, $from, $where, $having) = $query->query($count);
4327 $options = $query->_options
;
4328 if (!empty($query->_permissionWhereClause
)) {
4329 if (empty($where)) {
4330 $where = "WHERE $query->_permissionWhereClause";
4333 $where = "$where AND $query->_permissionWhereClause";
4337 $sql = "$select $from $where $having";
4340 if ($query->_useGroupBy
) {
4341 $sql .= self
::getGroupByFromSelectColumns($query->_select
, 'contact_a.id');
4343 if (!empty($sort)) {
4344 $sort = CRM_Utils_Type
::escape($sort, 'String');
4345 $sql .= " ORDER BY $sort ";
4347 if ($row_count > 0 && $offset >= 0) {
4348 $offset = CRM_Utils_Type
::escape($offset, 'Int');
4349 $row_count = CRM_Utils_Type
::escape($row_count, 'Int');
4350 $sql .= " LIMIT $offset, $row_count ";
4353 $dao = CRM_Core_DAO
::executeQuery($sql);
4355 // @todo derive this from the component class rather than hard-code two options.
4356 $entityIDField = ($mode == CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
) ?
'contribution_id' : 'contact_id';
4359 while ($dao->fetch()) {
4361 $noRows = $dao->rowCount
;
4363 return array($noRows, NULL);
4365 $val = $query->store($dao);
4366 $convertedVals = $query->convertToPseudoNames($dao, TRUE);
4368 if (!empty($convertedVals)) {
4369 $val = array_replace_recursive($val, $convertedVals);
4371 $values[$dao->$entityIDField] = $val;
4374 return array($values, $options);
4378 * Get the actual custom field name by stripping off the appended string.
4380 * The string could be _relative, _from, or _to
4382 * @todo use metadata rather than convention to do this.
4384 * @param string $parameterName
4385 * The name of the parameter submitted to the form.
4392 public static function getCustomFieldName($parameterName) {
4393 if (substr($parameterName, -5, 5) == '_from') {
4394 return substr($parameterName, 0, strpos($parameterName, '_from'));
4396 if (substr($parameterName, -9, 9) == '_relative') {
4397 return substr($parameterName, 0, strpos($parameterName, '_relative'));
4399 if (substr($parameterName, -3, 3) == '_to') {
4400 return substr($parameterName, 0, strpos($parameterName, '_to'));
4405 * Convert submitted values for relative custom fields to query object format.
4407 * The query will support the sqlOperator format so convert to that format.
4409 * @param array $formValues
4411 * @param array $params
4412 * Converted parameters for the query object.
4413 * @param string $values
4415 * @param string $fieldName
4416 * Submitted field name. (Matches form field not DB field.)
4418 protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) {
4419 if (empty($values)) {
4420 // e.g we might have relative set & from & to empty. The form flow is a bit funky &
4421 // this function gets called again after they fields have been converted which can get ugly.
4424 $customFieldName = self
::getCustomFieldName($fieldName);
4426 if (substr($fieldName, -9, 9) == '_relative') {
4427 list($from, $to) = CRM_Utils_Date
::getFromTo($values, NULL, NULL);
4430 if ($fieldName == $customFieldName . '_to' && CRM_Utils_Array
::value($customFieldName . '_from', $formValues)) {
4431 // Both to & from are set. We only need to acton one, choosing from.
4435 $from = CRM_Utils_Array
::value($customFieldName . '_from', $formValues, NULL);
4436 $to = CRM_Utils_Array
::value($customFieldName . '_to', $formValues, NULL);
4438 if (self
::isCustomDateField($customFieldName)) {
4439 list($from, $to) = CRM_Utils_Date
::getFromTo(NULL, $from, $to);
4445 $relativeFunction = array('BETWEEN' => array($from, $to));
4448 $relativeFunction = array('>=' => $from);
4452 $relativeFunction = array('<=' => $to);
4464 * Are we dealing with custom field of type date.
4470 public static function isCustomDateField($fieldName) {
4471 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) == FALSE) {
4474 if ('Date' == civicrm_api3('CustomField', 'getvalue', array('id' => $customFieldID, 'return' => 'data_type'))) {
4481 * Has this field already been reformatting to Query object syntax.
4483 * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems
4484 * to come with associated double queries & is possibly damaging performance.
4486 * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns
4489 * @param mixed $values
4490 * Value in formValues for the field.
4494 public static function isAlreadyProcessedForQueryFormat($values) {
4495 if (!is_array($values)) {
4498 if (($operator = CRM_Utils_Array
::value(1, $values)) == FALSE) {
4501 return in_array($operator, CRM_Core_DAO
::acceptedSQLOperators());
4505 * If the state and country are passed remove state.
4507 * Country is implicit from the state, but including both results in
4508 * a poor query as there is no combined index on state AND country.
4512 * @param array $formValues
4514 public static function filterCountryFromValuesIfStateExists(&$formValues) {
4515 if (!empty($formValues['country']) && !empty($formValues['state_province'])) {
4516 // The use of array map sanitises the data by ensuring we are dealing with integers.
4517 $states = implode(', ', array_map('intval', $formValues['state_province']));
4518 $countryList = CRM_Core_DAO
::singleValueQuery(
4519 "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE id IN ($states)"
4521 if ($countryList == $formValues['country']) {
4522 unset($formValues['country']);
4528 * For some special cases, grouping by subset of select fields becomes mandatory.
4529 * Hence, full_group_by mode is handled by appending any_value
4530 * keyword to select fields not present in groupBy
4532 * @param array $selectClauses
4533 * @param array $groupBy - Columns already included in GROUP By clause.
4537 public static function appendAnyValueToSelect($selectClauses, $groupBy) {
4538 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
4539 $sqlMode = explode(',', CRM_Core_DAO
::singleValueQuery('SELECT @@sql_mode'));
4541 // Disable only_full_group_by mode for lower sql versions.
4542 if (version_compare($mysqlVersion, '5.7', '<') ||
(!empty($sqlMode) && !in_array('ONLY_FULL_GROUP_BY', $sqlMode))) {
4543 $key = array_search('ONLY_FULL_GROUP_BY', $sqlMode);
4544 unset($sqlMode[$key]);
4545 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = '" . implode(',', $sqlMode) . "'");
4548 $groupBy = array_map('trim', (array) $groupBy);
4549 $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
4550 foreach ($selectClauses as $key => &$val) {
4551 list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
4552 // append ANY_VALUE() keyword
4553 if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
4554 $val = str_replace($selectColumn, "ANY_VALUE({$selectColumn})", $val);
4559 return "SELECT " . implode(', ', $selectClauses) . " ";
4563 * Include Select columns in groupBy clause.
4565 * @param array $selectClauses
4566 * @param array $groupBy - Columns already included in GROUP By clause.
4570 public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
4571 $groupBy = (array) $groupBy;
4572 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
4573 $sqlMode = CRM_Core_DAO
::singleValueQuery('SELECT @@sql_mode');
4575 //return if ONLY_FULL_GROUP_BY is not enabled.
4576 if (!version_compare($mysqlVersion, '5.7', '<') && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) {
4577 $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
4578 foreach ($selectClauses as $key => $val) {
4579 $aliasArray = preg_split('/ as /i', $val);
4580 // if more than 1 alias we need to split by ','.
4581 if (count($aliasArray) > 2) {
4582 $aliasArray = preg_split('/,/', $val);
4583 foreach ($aliasArray as $key => $value) {
4584 $alias = current(preg_split('/ as /i', $value));
4585 if (!in_array($alias, $groupBy) && preg_match($regexToExclude, trim($alias)) !== 1) {
4586 $groupBy[] = $alias;
4591 list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
4592 $dateRegex = '/^(DATE_FORMAT|DATE_ADD|CASE)/i';
4593 $tableName = current(explode('.', $selectColumn));
4594 $primaryKey = "{$tableName}.id";
4595 // exclude columns which are already included in groupBy and aggregate functions from select
4596 // CRM-18439 - Also exclude the columns which are functionally dependent on columns in $groupBy (MySQL 5.7+)
4597 if (!in_array($selectColumn, $groupBy) && !in_array($primaryKey, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
4598 if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
4599 $groupBy[] = $alias;
4602 $groupBy[] = $selectColumn;
4609 if (!empty($groupBy)) {
4610 return " GROUP BY " . implode(', ', $groupBy);
4616 * Create and query the db for an contact search.
4618 * @param int $offset
4619 * The offset for the query.
4620 * @param int $rowCount
4621 * The number of rows to return.
4622 * @param string|CRM_Utils_Sort $sort
4623 * The order by string.
4624 * @param bool $count
4625 * Is this a count only query ?.
4626 * @param bool $includeContactIds
4627 * Should we include contact ids?.
4628 * @param bool $sortByChar
4629 * If true returns the distinct array of first characters for search results.
4630 * @param bool $groupContacts
4631 * If true, return only the contact ids.
4632 * @param bool $returnQuery
4633 * Should we return the query as a string.
4634 * @param string $additionalWhereClause
4635 * If the caller wants to further restrict the search (used for components).
4636 * @param null $sortOrder
4637 * @param string $additionalFromClause
4638 * Should be clause with proper joins, effective to reduce where clause load.
4640 * @param bool $skipOrderAndLimit
4642 * @return CRM_Core_DAO
4644 public function searchQuery(
4645 $offset = 0, $rowCount = 0, $sort = NULL,
4646 $count = FALSE, $includeContactIds = FALSE,
4647 $sortByChar = FALSE, $groupContacts = FALSE,
4648 $returnQuery = FALSE,
4649 $additionalWhereClause = NULL, $sortOrder = NULL,
4650 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE
4653 if ($includeContactIds) {
4654 $this->_includeContactIds
= TRUE;
4655 $this->_whereClause
= $this->whereClause();
4658 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
);
4660 // if we’re explicitly looking for a certain contact’s contribs, events, etc.
4661 // and that contact happens to be deleted, set $onlyDeleted to true
4662 foreach ($this->_params
as $values) {
4663 $name = CRM_Utils_Array
::value(0, $values);
4664 $op = CRM_Utils_Array
::value(1, $values);
4665 $value = CRM_Utils_Array
::value(2, $values);
4666 if ($name == 'contact_id' and $op == '=') {
4667 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) {
4668 $onlyDeleted = TRUE;
4674 // building the query string
4675 $groupBy = $groupByCol = NULL;
4677 if (isset($this->_groupByComponentClause
)) {
4678 $groupBy = $this->_groupByComponentClause
;
4679 $groupCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause
));
4680 $groupByCol = explode(', ', $groupCols);
4682 elseif ($this->_useGroupBy
) {
4683 $groupByCol = 'contact_a.id';
4684 $groupBy = ' GROUP BY contact_a.id';
4687 if ($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
&& (!$count)) {
4688 $groupByCol = 'civicrm_activity.id';
4689 $groupBy = 'GROUP BY civicrm_activity.id ';
4692 $order = $orderBy = $limit = '';
4694 list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
4696 if ($rowCount > 0 && $offset >= 0) {
4697 $offset = CRM_Utils_Type
::escape($offset, 'Int');
4698 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
4699 $limit = " LIMIT $offset, $rowCount ";
4704 $this->_sort
= $sort;
4707 $this->includePseudoFieldsJoin($sort);
4709 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);
4711 if (!empty($groupByCol)) {
4712 $groupBy = self
::getGroupByFromSelectColumns($this->_select
, $groupByCol);
4715 if ($additionalWhereClause) {
4716 $where = $where . ' AND ' . $additionalWhereClause;
4719 //additional from clause should be w/ proper joins.
4720 if ($additionalFromClause) {
4721 $from .= "\n" . $additionalFromClause;
4724 // if we are doing a transform, do it here
4725 // use the $from, $where and $having to get the contact ID
4726 if ($this->_displayRelationshipType
) {
4727 $this->filterRelatedContacts($from, $where, $having);
4730 if ($skipOrderAndLimit) {
4731 $query = "$select $from $where $having $groupBy";
4734 $query = "$select $from $where $having $groupBy $order $limit";
4741 return CRM_Core_DAO
::singleValueQuery($query);
4744 $dao = CRM_Core_DAO
::executeQuery($query);
4745 if ($groupContacts) {
4747 while ($dao->fetch()) {
4751 return implode(',', $ids);
4758 * Fetch a list of contacts from the prev/next cache for displaying a search results page
4760 * @param string $cacheKey
4761 * @param int $offset
4762 * @param int $rowCount
4763 * @param bool $includeContactIds
4764 * @return CRM_Core_DAO
4766 public function getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds) {
4767 $this->_includeContactIds
= $includeContactIds;
4768 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
);
4769 list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
4770 $from = " FROM civicrm_prevnext_cache pnc INNER JOIN civicrm_contact contact_a ON contact_a.id = pnc.entity_id1 AND pnc.cacheKey = '$cacheKey' " . substr($from, 31);
4771 $order = " ORDER BY pnc.id";
4772 $groupByCol = array('contact_a.id', 'pnc.id');
4773 $groupBy = self
::getGroupByFromSelectColumns($this->_select
, $groupByCol);
4774 $limit = " LIMIT $offset, $rowCount";
4775 $query = "$select $from $where $groupBy $order $limit";
4777 return CRM_Core_DAO
::executeQuery($query);
4781 * Populate $this->_permissionWhereClause with permission related clause and update other
4782 * query related properties.
4784 * Function calls ACL permission class and hooks to filter the query appropriately
4786 * Note that these 2 params were in the code when extracted from another function
4787 * and a second round extraction would be to make them properties of the class
4789 * @param bool $onlyDeleted
4790 * Only get deleted contacts.
4791 * @param bool $count
4792 * Return Count only.
4794 public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) {
4795 if (!$this->_skipPermission
) {
4796 $this->_permissionWhereClause
= CRM_ACL_API
::whereClause(
4797 CRM_Core_Permission
::VIEW
,
4799 $this->_whereTables
,
4802 $this->_skipDeleteClause
4805 // regenerate fromClause since permission might have added tables
4806 if ($this->_permissionWhereClause
) {
4807 //fix for row count in qill (in contribute/membership find)
4809 $this->_useDistinct
= TRUE;
4812 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
4813 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
4814 // note : this modifies _fromClause and _simpleFromClause
4815 $this->includePseudoFieldsJoin($this->_sort
);
4819 // add delete clause if needed even if we are skipping permission
4821 if (!$this->_skipDeleteClause
) {
4822 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
4823 $this->_permissionWhereClause
= '(contact_a.is_deleted)';
4827 $this->_permissionWhereClause
= '(contact_a.is_deleted = 0)';
4836 public function setSkipPermission($val) {
4837 $this->_skipPermission
= $val;
4841 * @param null $context
4845 public function &summaryContribution($context = NULL) {
4846 list($innerselect, $from, $where, $having) = $this->query(TRUE);
4850 SELECT COUNT( conts.total_amount ) as total_count,
4851 SUM( conts.total_amount ) as total_amount,
4852 AVG( conts.total_amount ) as total_avg,
4853 conts.currency as currency";
4854 if ($this->_permissionWhereClause
) {
4855 $where .= " AND " . $this->_permissionWhereClause
;
4857 if ($context == 'search') {
4858 $where .= " AND contact_a.is_deleted = 0 ";
4860 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
4861 if (!empty($financialTypes)) {
4862 $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL";
4863 $from .= " LEFT JOIN civicrm_line_item li
4864 ON civicrm_contribution.id = li.contribution_id AND
4865 li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") ";
4868 $where .= " AND civicrm_contribution.financial_type_id IN (0)";
4871 // make sure contribution is completed - CRM-4989
4872 $completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 ";
4875 $summary['total'] = array();
4876 $summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a";
4877 $innerQuery = "SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
4878 civicrm_contribution.currency $from $completedWhere";
4879 $query = "$select FROM (
4880 $innerQuery GROUP BY civicrm_contribution.id
4884 $dao = CRM_Core_DAO
::executeQuery($query);
4886 $summary['total']['count'] = 0;
4887 $summary['total']['amount'] = $summary['total']['avg'] = array();
4888 while ($dao->fetch()) {
4889 $summary['total']['count'] +
= $dao->total_count
;
4890 $summary['total']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
4891 $summary['total']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
4894 $orderBy = 'ORDER BY civicrm_contribution_total_amount_count DESC';
4895 $groupBy = 'GROUP BY currency, civicrm_contribution.total_amount';
4896 $modeSQL = "$select, SUBSTRING_INDEX(GROUP_CONCAT(conts.total_amount
4897 ORDER BY conts.civicrm_contribution_total_amount_count DESC SEPARATOR ';'), ';', 1) as amount,
4898 MAX(conts.civicrm_contribution_total_amount_count) as civicrm_contribution_total_amount_count
4900 $groupBy $orderBy) as conts
4903 $summary['total']['mode'] = CRM_Contribute_BAO_Contribution
::computeStats('mode', $modeSQL);
4905 $medianSQL = "{$from} {$completedWhere}";
4906 $summary['total']['median'] = CRM_Contribute_BAO_Contribution
::computeStats('median', $medianSQL, 'civicrm_contribution');
4907 $summary['total']['currencyCount'] = count($summary['total']['median']);
4909 if (!empty($summary['total']['amount'])) {
4910 $summary['total']['amount'] = implode(', ', $summary['total']['amount']);
4911 $summary['total']['avg'] = implode(', ', $summary['total']['avg']);
4912 $summary['total']['mode'] = implode(', ', $summary['total']['mode']);
4913 $summary['total']['median'] = implode(', ', $summary['total']['median']);
4916 $summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0;
4919 // soft credit summary
4920 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled()) {
4921 $softCreditWhere = "{$completedWhere} AND civicrm_contribution_soft.id IS NOT NULL";
4924 SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency $from $softCreditWhere
4925 GROUP BY civicrm_contribution_soft.id
4928 $dao = CRM_Core_DAO
::executeQuery($query);
4929 $summary['soft_credit']['count'] = 0;
4930 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = array();
4931 while ($dao->fetch()) {
4932 $summary['soft_credit']['count'] +
= $dao->total_count
;
4933 $summary['soft_credit']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
4934 $summary['soft_credit']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
4936 if (!empty($summary['soft_credit']['amount'])) {
4937 $summary['soft_credit']['amount'] = implode(', ', $summary['soft_credit']['amount']);
4938 $summary['soft_credit']['avg'] = implode(', ', $summary['soft_credit']['avg']);
4941 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0;
4946 //@todo - this could be one query using the IF in mysql - eg
4947 // SELECT sum(total_completed), sum(count_completed), sum(count_cancelled), sum(total_cancelled) FROM (
4948 // SELECT civicrm_contribution.total_amount, civicrm_contribution.currency ,
4949 // IF(civicrm_contribution.contribution_status_id = 1, 1, 0 ) as count_completed,
4950 // IF(civicrm_contribution.contribution_status_id = 1, total_amount, 0 ) as total_completed,
4951 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, 1, 0 ) as count_cancelled,
4952 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, total_amount, 0 ) as total_cancelled
4953 // FROM civicrm_contact contact_a
4954 // LEFT JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id
4955 // WHERE ( ... where clause....
4956 // AND (civicrm_contribution.cancel_date IS NOT NULL OR civicrm_contribution.contribution_status_id = 1)
4960 SELECT COUNT( conts.total_amount ) as cancel_count,
4961 SUM( conts.total_amount ) as cancel_amount,
4962 AVG( conts.total_amount ) as cancel_avg,
4963 conts.currency as currency";
4965 $where .= " AND civicrm_contribution.cancel_date IS NOT NULL ";
4966 if ($context == 'search') {
4967 $where .= " AND contact_a.is_deleted = 0 ";
4970 $query = "$select FROM (
4971 SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $where
4972 GROUP BY civicrm_contribution.id
4976 $dao = CRM_Core_DAO
::executeQuery($query);
4979 if ($dao->fetch()) {
4980 $summary['cancel']['count'] = $dao->cancel_count
;
4981 $summary['cancel']['amount'] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
4982 $summary['cancel']['avg'] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
4986 $summary['cancel']['count'] = 0;
4987 $summary['cancel']['amount'] = $summary['cancel']['avg'] = array();
4988 while ($dao->fetch()) {
4989 $summary['cancel']['count'] +
= $dao->cancel_count
;
4990 $summary['cancel']['amount'][] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
4991 $summary['cancel']['avg'][] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
4993 $summary['cancel']['amount'] = implode(', ', $summary['cancel']['amount']);
4994 $summary['cancel']['avg'] = implode(', ', $summary['cancel']['avg']);
5001 * Getter for the qill object.
5005 public function qill() {
5006 return $this->_qill
;
5010 * Default set of return default hier return properties.
5014 public static function &defaultHierReturnProperties() {
5015 if (!isset(self
::$_defaultHierReturnProperties)) {
5016 self
::$_defaultHierReturnProperties = array(
5019 'legal_identifier' => 1,
5020 'external_identifier' => 1,
5021 'contact_type' => 1,
5022 'contact_sub_type' => 1,
5024 'display_name' => 1,
5031 'formal_title' => 1,
5032 'communication_style_id' => 1,
5033 'email_greeting' => 1,
5034 'postal_greeting' => 1,
5038 'preferred_communication_method' => 1,
5039 'do_not_phone' => 1,
5040 'do_not_email' => 1,
5043 'do_not_trade' => 1,
5044 'location' => array(
5046 'location_type' => 1,
5047 'street_address' => 1,
5049 'state_province' => 1,
5051 'postal_code_suffix' => 1,
5054 'phone-Mobile' => 1,
5067 'location_type' => 1,
5068 'street_address' => 1,
5070 'state_province' => 1,
5072 'postal_code_suffix' => 1,
5075 'phone-Mobile' => 1,
5089 return self
::$_defaultHierReturnProperties;
5093 * Build query for a date field.
5095 * @param array $values
5096 * @param string $tableName
5097 * @param string $fieldName
5098 * @param string $dbFieldName
5099 * @param string $fieldTitle
5100 * @param bool $appendTimeStamp
5102 public function dateQueryBuilder(
5103 &$values, $tableName, $fieldName,
5104 $dbFieldName, $fieldTitle,
5105 $appendTimeStamp = TRUE
5107 list($name, $op, $value, $grouping, $wildcard) = $values;
5109 if ($name == "{$fieldName}_low" ||
5110 $name == "{$fieldName}_high"
5112 if (isset($this->_rangeCache
[$fieldName]) ||
!$value) {
5115 $this->_rangeCache
[$fieldName] = 1;
5117 $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL;
5119 if ($name == $fieldName . '_low') {
5121 $firstPhrase = ts('greater than or equal to');
5122 $firstDate = CRM_Utils_Date
::processDate($value);
5124 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5125 if (!empty($secondValues) && $secondValues[2]) {
5127 $secondPhrase = ts('less than or equal to');
5128 $secondValue = $secondValues[2];
5130 if ($appendTimeStamp && strlen($secondValue) == 10) {
5131 $secondValue .= ' 23:59:59';
5133 $secondDate = CRM_Utils_Date
::processDate($secondValue);
5136 elseif ($name == $fieldName . '_high') {
5138 $firstPhrase = ts('less than or equal to');
5140 if ($appendTimeStamp && strlen($value) == 10) {
5141 $value .= ' 23:59:59';
5143 $firstDate = CRM_Utils_Date
::processDate($value);
5145 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5146 if (!empty($secondValues) && $secondValues[2]) {
5148 $secondPhrase = ts('greater than or equal to');
5149 $secondValue = $secondValues[2];
5150 $secondDate = CRM_Utils_Date
::processDate($secondValue);
5154 if (!$appendTimeStamp) {
5155 $firstDate = substr($firstDate, 0, 8);
5157 $firstDateFormat = CRM_Utils_Date
::customFormat($firstDate);
5160 if (!$appendTimeStamp) {
5161 $secondDate = substr($secondDate, 0, 8);
5163 $secondDateFormat = CRM_Utils_Date
::customFormat($secondDate);
5166 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5168 $this->_where
[$grouping][] = "
5169 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5170 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5172 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\"";
5175 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5176 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
5180 if ($name == $fieldName) {
5181 //In Get API, for operators other then '=' the $value is in array(op => value) format
5182 if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
5184 $value = $value[$op];
5187 $date = $format = NULL;
5188 if (strstr($op, 'IN')) {
5190 foreach ($value as &$date) {
5191 $date = CRM_Utils_Date
::processDate($date);
5192 if (!$appendTimeStamp) {
5193 $date = substr($date, 0, 8);
5195 $format[] = CRM_Utils_Date
::customFormat($date);
5197 $date = "('" . implode("','", $value) . "')";
5198 $format = implode(', ', $format);
5200 elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) {
5201 $date = CRM_Utils_Date
::processDate($value);
5202 if (!$appendTimeStamp) {
5203 $date = substr($date, 0, 8);
5205 $format = CRM_Utils_Date
::customFormat($date);
5210 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op $date";
5213 $this->_where
[$grouping][] = self
::buildClause("{$tableName}.{$dbFieldName}", $op);
5216 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5218 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
5219 $this->_qill
[$grouping][] = "$fieldTitle $op $format";
5225 * @param string $tableName
5226 * @param string $fieldName
5227 * @param string $dbFieldName
5228 * @param $fieldTitle
5229 * @param null $options
5231 public function numberRangeBuilder(
5233 $tableName, $fieldName,
5234 $dbFieldName, $fieldTitle,
5237 list($name, $op, $value, $grouping, $wildcard) = $values;
5239 if ($name == "{$fieldName}_low" ||
5240 $name == "{$fieldName}_high"
5242 if (isset($this->_rangeCache
[$fieldName])) {
5245 $this->_rangeCache
[$fieldName] = 1;
5247 $secondOP = $secondPhrase = $secondValue = NULL;
5249 if ($name == "{$fieldName}_low") {
5251 $firstPhrase = ts('greater than');
5253 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5254 if (!empty($secondValues)) {
5256 $secondPhrase = ts('less than');
5257 $secondValue = $secondValues[2];
5262 $firstPhrase = ts('less than');
5264 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5265 if (!empty($secondValues)) {
5267 $secondPhrase = ts('greater than');
5268 $secondValue = $secondValues[2];
5273 $this->_where
[$grouping][] = "
5274 ( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND
5275 ( {$tableName}.{$dbFieldName} $secondOP {$secondValue} )
5277 $displayValue = $options ?
$options[$value] : $value;
5278 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5280 $this->_qill
[$grouping][]
5281 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5284 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}";
5285 $displayValue = $options ?
$options[$value] : $value;
5286 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5288 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5293 if ($name == $fieldName) {
5297 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}";
5299 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5300 $displayValue = $options ?
$options[$value] : $value;
5301 $this->_qill
[$grouping][] = "$fieldTitle - $phrase \"$displayValue\"";
5308 * @param string $tableName
5309 * @param string $fieldName
5310 * @param string $dbFieldName
5311 * @param $fieldTitle
5312 * @param null $options
5314 public function ageRangeQueryBuilder(
5316 $tableName, $fieldName,
5317 $dbFieldName, $fieldTitle,
5320 list($name, $op, $value, $grouping, $wildcard) = $values;
5322 $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping);
5323 $asofDate = NULL; // will be treated as current day
5324 if ($asofDateValues) {
5325 $asofDate = CRM_Utils_Date
::processDate($asofDateValues[2]);
5326 $asofDateFormat = CRM_Utils_Date
::customFormat(substr($asofDate, 0, 8));
5327 $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat;
5330 if ($name == "{$fieldName}_low" ||
5331 $name == "{$fieldName}_high"
5333 if (isset($this->_rangeCache
[$fieldName])) {
5336 $this->_rangeCache
[$fieldName] = 1;
5338 $secondOP = $secondPhrase = $secondValue = NULL;
5340 if ($name == "{$fieldName}_low") {
5341 $firstPhrase = ts('greater than or equal to');
5342 // NB: age > X means date of birth < Y
5344 $firstDate = self
::calcDateFromAge($asofDate, $value, 'min');
5346 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5347 if (!empty($secondValues)) {
5349 $secondPhrase = ts('less than or equal to');
5350 $secondValue = $secondValues[2];
5351 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'max');
5356 $firstPhrase = ts('less than or equal to');
5357 $firstDate = self
::calcDateFromAge($asofDate, $value, 'max');
5359 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5360 if (!empty($secondValues)) {
5362 $secondPhrase = ts('greater than or equal to');
5363 $secondValue = $secondValues[2];
5364 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'min');
5369 $this->_where
[$grouping][] = "
5370 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5371 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5373 $displayValue = $options ?
$options[$value] : $value;
5374 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5376 $this->_qill
[$grouping][]
5377 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5380 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5381 $displayValue = $options ?
$options[$value] : $value;
5382 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5384 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5390 * Calculate date from age.
5392 * @param string $asofDate
5394 * @param string $type
5398 public static function calcDateFromAge($asofDate, $age, $type) {
5399 $date = new DateTime($asofDate);
5400 if ($type == "min") {
5401 // minimum age is $age: dob <= date - age "235959"
5402 $date->sub(new DateInterval("P" . $age . "Y"));
5403 return $date->format('Ymd') . "235959";
5406 // max age is $age: dob >= date - (age + 1y) + 1d "000000"
5407 $date->sub(new DateInterval("P" . ($age +
1) . "Y"))->add(new DateInterval("P1D"));
5408 return $date->format('Ymd') . "000000";
5413 * Given the field name, operator, value & its data type
5414 * builds the where Clause for the query
5415 * used for handling 'IS NULL'/'IS NOT NULL' operators
5417 * @param string $field
5421 * @param string $value
5423 * @param string $dataType
5424 * Data type of the field.
5427 * Where clause for the query.
5429 public static function buildClause($field, $op, $value = NULL, $dataType = NULL) {
5431 $clause = "$field $op";
5439 $clause = " (NULLIF($field, '') IS NULL) ";
5442 case 'IS NOT EMPTY':
5443 $clause = " (NULLIF($field, '') IS NOT NULL) ";
5448 // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter.
5449 if (!empty($value) && (!is_array($value) ||
!array_key_exists($op, $value))) {
5450 $value = array($op => (array) $value);
5454 if (empty($dataType)) {
5455 $dataType = 'String';
5457 if (is_array($value)) {
5458 //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more
5459 // widely used and consistent across the codebase
5460 // adding this here won't accept the search functions which don't submit an array
5461 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, $value, $dataType)) != FALSE) {
5463 return $queryString;
5466 // This is the here-be-dragons zone. We have no other hopes left for an array so lets assume it 'should' be array('IN' => array(2,5))
5467 // but we got only array(2,5) from the form.
5468 // We could get away with keeping this in 4.6 if we make it such that it throws an enotice in 4.7 so
5469 // people have to de-slopify it.
5470 if (!empty($value[0])) {
5471 if ($op != 'BETWEEN') {
5472 $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReport;
5474 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, array($op => $value), $dataType)) != FALSE) {
5475 return $queryString;
5480 $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReportUsingOldFormat;
5481 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, array($op => array_keys($value)), $dataType)) != FALSE) {
5482 return $queryString;
5487 $value = CRM_Utils_Type
::escape($value, $dataType);
5488 // if we don't have a dataType we should assume
5489 if ($dataType == 'String' ||
$dataType == 'Text') {
5490 $value = "'" . strtolower($value) . "'";
5492 return "$clause $value";
5497 * @param bool $reset
5501 public function openedSearchPanes($reset = FALSE) {
5502 if (!$reset ||
empty($this->_whereTables
)) {
5503 return self
::$_openedPanes;
5506 // pane name to table mapper
5507 $panesMapper = array(
5508 ts('Contributions') => 'civicrm_contribution',
5509 ts('Memberships') => 'civicrm_membership',
5510 ts('Events') => 'civicrm_participant',
5511 ts('Relationships') => 'civicrm_relationship',
5512 ts('Activities') => 'civicrm_activity',
5513 ts('Pledges') => 'civicrm_pledge',
5514 ts('Cases') => 'civicrm_case',
5515 ts('Grants') => 'civicrm_grant',
5516 ts('Address Fields') => 'civicrm_address',
5517 ts('Notes') => 'civicrm_note',
5518 ts('Change Log') => 'civicrm_log',
5519 ts('Mailings') => 'civicrm_mailing',
5521 CRM_Contact_BAO_Query_Hook
::singleton()->getPanesMapper($panesMapper);
5523 foreach (array_keys($this->_whereTables
) as $table) {
5524 if ($panName = array_search($table, $panesMapper)) {
5525 self
::$_openedPanes[$panName] = TRUE;
5529 return self
::$_openedPanes;
5535 public function setOperator($operator) {
5536 $validOperators = array('AND', 'OR');
5537 if (!in_array($operator, $validOperators)) {
5540 $this->_operator
= $operator;
5546 public function getOperator() {
5547 return $this->_operator
;
5555 public function filterRelatedContacts(&$from, &$where, &$having) {
5556 static $_rTypeProcessed = NULL;
5557 static $_rTypeFrom = NULL;
5558 static $_rTypeWhere = NULL;
5560 if (!$_rTypeProcessed) {
5561 $_rTypeProcessed = TRUE;
5563 // create temp table with contact ids
5564 $tableName = CRM_Core_DAO
::createTempTableName('civicrm_transform', TRUE);
5565 $sql = "CREATE TEMPORARY TABLE $tableName ( contact_id int primary key) ENGINE=HEAP";
5566 CRM_Core_DAO
::executeQuery($sql);
5569 REPLACE INTO $tableName ( contact_id )
5575 CRM_Core_DAO
::executeQuery($sql);
5577 $qillMessage = ts('Contacts with a Relationship Type of: ');
5578 $rTypes = CRM_Core_PseudoConstant
::relationshipType();
5580 if (is_numeric($this->_displayRelationshipType
)) {
5581 $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType
]['label_a_b'];
5583 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )
5584 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )
5587 WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}
5588 AND displayRelType.is_active = 1
5592 list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType
);
5593 if ($dirOne == 'a') {
5594 $relationshipTypeLabel = $rTypes[$relType]['label_a_b'];
5596 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )
5597 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )
5601 $relationshipTypeLabel = $rTypes[$relType]['label_b_a'];
5603 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )
5604 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )
5608 WHERE displayRelType.relationship_type_id = $relType
5609 AND displayRelType.is_active = 1
5612 $this->_qill
[0][] = $qillMessage . "'" . $relationshipTypeLabel . "'";
5615 if (!empty($this->_permissionWhereClause
)) {
5616 $_rTypeWhere .= "AND $this->_permissionWhereClause";
5619 if (strpos($from, $_rTypeFrom) === FALSE) {
5620 // lets replace all the INNER JOIN's in the $from so we dont exclude other data
5621 // this happens when we have an event_type in the quert (CRM-7969)
5622 $from = str_replace("INNER JOIN", "LEFT JOIN", $from);
5623 $from .= $_rTypeFrom;
5624 $where = $_rTypeWhere;
5635 public static function caseImportant($op) {
5637 in_array($op, array('LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ?
FALSE : TRUE;
5641 * @param $returnProperties
5646 public static function componentPresent(&$returnProperties, $prefix) {
5647 foreach ($returnProperties as $name => $dontCare) {
5648 if (substr($name, 0, strlen($prefix)) == $prefix) {
5656 * Builds the necessary structures for all fields that are similar to option value look-ups.
5658 * @param string $name
5659 * the name of the field.
5661 * the sql operator, this function should handle ALL SQL operators.
5662 * @param string $value
5663 * depends on the operator and who's calling the query builder.
5664 * @param int $grouping
5665 * the index where to place the where clause.
5666 * @param string $daoName
5668 * @param array $field
5669 * an array that contains various properties of the field identified by $name.
5670 * @param string $label
5671 * The label for this field element.
5672 * @param string $dataType
5673 * The data type for this element.
5674 * @param bool $useIDsOnly
5676 public function optionValueQuery(
5684 $dataType = 'String',
5688 $pseudoFields = array(
5695 'communication_style_id',
5699 list($tableName, $fieldName) = explode('.', $field['where'], 2);
5700 if ($tableName == 'civicrm_contact') {
5701 $wc = "contact_a.$fieldName";
5704 $wc = "$tableName.id";
5708 $wc = self
::caseImportant($op) ?
"LOWER({$field['where']})" : "{$field['where']}";
5710 if (in_array($name, $pseudoFields)) {
5711 if (!in_array($name, array('gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'))) {
5712 $wc = "contact_a.{$name}_id";
5714 $dataType = 'Positive';
5715 $value = (!$value) ?
0 : $value;
5717 if ($name == "world_region") {
5718 $field['name'] = $name;
5721 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue($daoName, $field['name'], $value, $op);
5722 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal));
5723 $this->_where
[$grouping][] = self
::buildClause($wc, $op, $value, $dataType);
5727 * Check and explode a user defined numeric string into an array
5728 * this was the protocol used by search builder in the old old days before we had
5729 * super nice js widgets to do the hard work
5731 * @param string $string
5732 * @param string $dataType
5733 * The dataType we should check for the values, default integer.
5735 * @return bool|array
5736 * false if string does not match the pattern
5737 * array of numeric values if string does match the pattern
5739 public static function parseSearchBuilderString($string, $dataType = 'Integer') {
5740 $string = trim($string);
5741 if (substr($string, 0, 1) != '(' ||
substr($string, -1, 1) != ')') {
5745 $string = substr($string, 1, -1);
5746 $values = explode(',', $string);
5747 if (empty($values)) {
5751 $returnValues = array();
5752 foreach ($values as $v) {
5753 if ($dataType == 'Integer' && !is_numeric($v)) {
5756 elseif ($dataType == 'String' && !is_string($v)) {
5759 $returnValues[] = trim($v);
5762 if (empty($returnValues)) {
5766 return $returnValues;
5770 * Convert the pseudo constants id's to their names
5772 * @param CRM_Core_DAO $dao
5773 * @param bool $return
5774 * @param bool $usedForAPI
5776 * @return array|NULL
5778 public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) {
5779 if (empty($this->_pseudoConstantsSelect
)) {
5783 foreach ($this->_pseudoConstantsSelect
as $key => $value) {
5784 if (!empty($this->_pseudoConstantsSelect
[$key]['sorting'])) {
5788 if (is_object($dao) && property_exists($dao, $value['idCol'])) {
5789 $val = $dao->{$value['idCol']};
5790 if ($key == 'groups') {
5791 $dao->groups
= $this->convertGroupIDStringToLabelString($dao, $val);
5795 if (CRM_Utils_System
::isNull($val)) {
5798 elseif ($baoName = CRM_Utils_Array
::value('bao', $value, NULL)) {
5800 $idColumn = "{$key}_id";
5801 $dao->$idColumn = $val;
5803 if ($key == 'state_province_name') {
5804 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($val);
5807 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::getLabel($baoName, $value['pseudoField'], $val);
5810 elseif ($value['pseudoField'] == 'state_province_abbreviation') {
5811 $dao->$key = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($val);
5813 // FIX ME: we should potentially move this to component Query and write a wrapper function that
5814 // handles pseudoconstant fixes for all component
5815 elseif (in_array($value['pseudoField'], array('participant_role_id', 'participant_role'))) {
5816 $viewValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $val);
5818 if ($value['pseudoField'] == 'participant_role') {
5819 $pseudoOptions = CRM_Core_PseudoConstant
::get('CRM_Event_DAO_Participant', 'role_id');
5820 foreach ($viewValues as $k => $v) {
5821 $viewValues[$k] = $pseudoOptions[$v];
5824 $dao->$key = ($usedForAPI && count($viewValues) > 1) ?
$viewValues : implode(', ', $viewValues);
5827 $labels = CRM_Core_OptionGroup
::values($value['pseudoField']);
5828 $dao->$key = $labels[$val];
5831 // return converted values in array format
5833 if (strpos($key, '-') !== FALSE) {
5834 $keyVal = explode('-', $key);
5835 $current = &$values;
5836 $lastElement = array_pop($keyVal);
5837 foreach ($keyVal as $v) {
5838 if (!array_key_exists($v, $current)) {
5839 $current[$v] = array();
5841 $current = &$current[$v];
5843 $current[$lastElement] = $dao->$key;
5846 $values[$key] = $dao->$key;
5855 * Include pseudo fields LEFT JOIN.
5856 * @param string|array $sort can be a object or string
5858 * @return array|NULL
5860 public function includePseudoFieldsJoin($sort) {
5861 if (!$sort ||
empty($this->_pseudoConstantsSelect
)) {
5864 $sort = is_string($sort) ?
$sort : $sort->orderBy();
5867 foreach ($this->_pseudoConstantsSelect
as $name => $value) {
5868 if (!empty($value['table'])) {
5869 $regex = "/({$value['table']}\.|{$name})/";
5870 if (preg_match($regex, $sort)) {
5871 $this->_elemnt
[$value['element']] = 1;
5872 $this->_select
[$value['element']] = $value['select'];
5873 $this->_pseudoConstantsSelect
[$name]['sorting'] = 1;
5874 $present[$value['table']] = $value['join'];
5878 $presentSimpleFrom = $present;
5880 if (array_key_exists('civicrm_worldregion', $this->_whereTables
) &&
5881 array_key_exists('civicrm_country', $presentSimpleFrom)
5883 unset($presentSimpleFrom['civicrm_country']);
5885 if (array_key_exists('civicrm_worldregion', $this->_tables
) &&
5886 array_key_exists('civicrm_country', $present)
5888 unset($present['civicrm_country']);
5891 $presentClause = $presentSimpleFromClause = NULL;
5892 if (!empty($present)) {
5893 $presentClause = implode(' ', $present);
5895 if (!empty($presentSimpleFrom)) {
5896 $presentSimpleFromClause = implode(' ', $presentSimpleFrom);
5899 $this->_fromClause
= $this->_fromClause
. $presentClause;
5900 $this->_simpleFromClause
= $this->_simpleFromClause
. $presentSimpleFromClause;
5902 return array($presentClause, $presentSimpleFromClause);
5906 * Build qill for field.
5908 * Qill refers to the query detail visible on the UI.
5910 * @param string $daoName
5911 * @param string $fieldName
5912 * @param mixed $fieldValue
5914 * @param array $pseudoExtraParam
5916 * Type of the field per CRM_Utils_Type
5920 public static function buildQillForFieldValue(
5925 $pseudoExtraParam = array(),
5926 $type = CRM_Utils_Type
::T_STRING
5928 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
5930 // if Operator chosen is NULL/EMPTY then
5931 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
5932 return array(CRM_Utils_Array
::value($op, $qillOperators, $op), '');
5935 if ($fieldName == 'activity_type_id') {
5936 $pseudoOptions = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
5938 elseif ($fieldName == 'country_id') {
5939 $pseudoOptions = CRM_Core_PseudoConstant
::country();
5941 elseif ($fieldName == 'county_id') {
5942 $pseudoOptions = CRM_Core_PseudoConstant
::county();
5944 elseif ($fieldName == 'world_region') {
5945 $pseudoOptions = CRM_Core_PseudoConstant
::worldRegion();
5947 elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') {
5948 $pseudoOptions = CRM_Event_BAO_Event
::getEvents(0, $fieldValue, TRUE, TRUE, TRUE);
5950 elseif ($fieldName == 'contribution_product_id') {
5951 $pseudoOptions = CRM_Contribute_PseudoConstant
::products();
5953 elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
5954 $pseudoOptions = CRM_Core_PseudoConstant
::group();
5957 $pseudoOptions = CRM_Core_PseudoConstant
::get($daoName, $fieldName, $pseudoExtraParam);
5960 //API usually have fieldValue format as array(operator => array(values)),
5961 //so we need to separate operator out of fieldValue param
5962 if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
5963 $op = key($fieldValue);
5964 $fieldValue = $fieldValue[$op];
5967 if (is_array($fieldValue)) {
5968 $qillString = array();
5969 if (!empty($pseudoOptions)) {
5970 foreach ((array) $fieldValue as $val) {
5971 $qillString[] = CRM_Utils_Array
::value($val, $pseudoOptions, $val);
5973 $fieldValue = implode(', ', $qillString);
5976 if ($type == CRM_Utils_Type
::T_DATE
) {
5977 foreach ($fieldValue as $index => $value) {
5978 $fieldValue[$index] = CRM_Utils_Date
::customFormat($value);
5982 // @todo - this is a bit specific (one operator).
5983 // However it is covered by a unit test so can be altered later with
5985 if ($op == 'BETWEEN') {
5986 $separator = ' AND ';
5988 $fieldValue = implode($separator, $fieldValue);
5991 elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) {
5992 $fieldValue = $pseudoOptions[$fieldValue];
5994 elseif ($type === CRM_Utils_Type
::T_DATE
) {
5995 $fieldValue = CRM_Utils_Date
::customFormat($fieldValue);
5998 return array(CRM_Utils_Array
::value($op, $qillOperators, $op), $fieldValue);
6002 * Alter value to reflect wildcard settings.
6004 * The form will have tried to guess whether this is a good field to wildcard but there is
6005 * also a site-wide setting that specifies whether it is OK to append the wild card to the beginning
6006 * or only the end of the string
6008 * @param bool $wildcard
6009 * This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard.
6011 * Generally '=' or 'LIKE'.
6012 * @param string $value
6013 * The search string.
6017 public static function getWildCardedValue($wildcard, $op, $value) {
6018 if ($wildcard && $op == 'LIKE') {
6019 if (CRM_Core_Config
::singleton()->includeWildCardInName
&& (substr($value, 0, 1) != '%')) {
6032 * Process special fields of Search Form in OK (Operator in Key) format
6034 * @param array $formValues
6035 * @param array $specialFields
6036 * Special params to be processed
6037 * @param array $changeNames
6038 * Array of fields whose name should be changed
6040 public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = array()) {
6041 foreach ($specialFields as $element) {
6042 $value = CRM_Utils_Array
::value($element, $formValues);
6044 if (is_array($value)) {
6045 if (in_array($element, array_keys($changeNames))) {
6046 unset($formValues[$element]);
6047 $element = $changeNames[$element];
6049 $formValues[$element] = array('IN' => $value);
6052 // if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
6053 $isWilcard = strstr($value, '%') ?
FALSE : CRM_Core_Config
::singleton()->includeWildCardInName
;
6054 $formValues[$element] = array('LIKE' => self
::getWildCardedValue($isWilcard, 'LIKE', $value));
6061 * Parse and assimilate the various sort options.
6063 * Side-effect: if sorting on a common column from a related table (`city`, `postal_code`,
6064 * `email`), the related table may be joined automatically.
6066 * At time of writing, this code is deeply flawed and should be rewritten. For the moment,
6067 * it's been extracted to a standalone function.
6069 * @param string|CRM_Utils_Sort $sort
6070 * The order by string.
6071 * @param bool $sortByChar
6072 * If true returns the distinct array of first characters for search results.
6073 * @param null $sortOrder
6074 * Who knows? Hu knows. He who knows Hu knows who.
6075 * @param string $additionalFromClause
6076 * Should be clause with proper joins, effective to reduce where clause load.
6078 * list(string $orderByClause, string $additionalFromClause).
6080 protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause) {
6082 $config = CRM_Core_Config
::singleton();
6083 if ($config->includeOrderByClause ||
6084 isset($this->_distinctComponentClause
)
6087 if (is_string($sort)) {
6091 $orderBy = trim($sort->orderBy());
6093 // Deliberately remove the backticks again, as they mess up the evil
6094 // string munging below. This balanced by re-escaping before use.
6095 $orderBy = str_replace('`', '', $orderBy);
6097 if (!empty($orderBy)) {
6098 // this is special case while searching for
6099 // change log CRM-1718
6100 if (preg_match('/sort_name/i', $orderBy)) {
6101 $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy);
6104 $order = " ORDER BY $orderBy";
6107 $order .= " $sortOrder";
6110 // always add contact_a.id to the ORDER clause
6111 // so the order is deterministic
6112 if (strpos('contact_a.id', $order) === FALSE) {
6113 $order .= ", contact_a.id";
6117 elseif ($sortByChar) {
6118 $order = " ORDER BY UPPER(LEFT(contact_a.sort_name, 1)) asc";
6121 $order = " ORDER BY contact_a.sort_name asc, contact_a.id";
6125 // hack for order clause
6127 $fieldStr = trim(str_replace('ORDER BY', '', $order));
6128 $fieldOrder = explode(' ', $fieldStr);
6129 $field = $fieldOrder[0];
6135 $this->_tables
["civicrm_address"] = $this->_whereTables
["civicrm_address"] = 1;
6136 $order = str_replace($field, "civicrm_address.{$field}", $order);
6140 case 'state_province':
6141 $this->_tables
["civicrm_{$field}"] = $this->_whereTables
["civicrm_{$field}"] = 1;
6142 if (is_array($this->_returnProperties
) && empty($this->_returnProperties
)) {
6143 $additionalFromClause .= " LEFT JOIN civicrm_{$field} ON civicrm_{$field}.id = civicrm_address.{$field}_id";
6145 $order = str_replace($field, "civicrm_{$field}.name", $order);
6149 $this->_tables
["civicrm_email"] = $this->_whereTables
["civicrm_email"] = 1;
6150 $order = str_replace($field, "civicrm_email.{$field}", $order);
6154 //CRM-12565 add "`" around $field if it is a pseudo constant
6155 foreach ($this->_pseudoConstantsSelect
as $key => $value) {
6156 if (!empty($value['element']) && $value['element'] == $field) {
6157 $order = str_replace($field, "`{$field}`", $order);
6161 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6162 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6166 // The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
6167 // clause. But this magic helper which forgivingly reescapes ORDER BY.
6168 // Note: $sortByChar implies that $order was hard-coded/trusted, so it can do funky things.
6169 if ($order && !$sortByChar) {
6170 $order = ' ORDER BY ' . CRM_Utils_Type
::escape(preg_replace('/^\s*ORDER BY\s*/', '', $order), 'MysqlOrderBy');
6171 return array($order, $additionalFromClause);
6173 return array($order, $additionalFromClause);
6177 * Convert a string of group IDs to a string of group labels.
6179 * The original string may include duplicates and groups the user does not have
6180 * permission to see.
6182 * @param CRM_Core_DAO $dao
6183 * @param string $val
6187 public function convertGroupIDStringToLabelString(&$dao, $val) {
6188 $groupIDs = explode(',', $val);
6190 // The pseudoConstant function does not actually cache.
6193 $allGroups = CRM_Core_PseudoConstant
::group();
6195 // Note that groups that the user does not have permission to will be excluded (good).
6196 $groups = array_intersect_key($allGroups, array_flip($groupIDs));
6197 return implode(', ', $groups);
6202 * Set the qill and where properties for a field.
6204 * This function is intended as a short-term function to encourage refactoring
6205 * & re-use - but really we should just have less special-casing.
6207 * @param string $name
6209 * @param string|array $value
6210 * @param string $grouping
6211 * @param string $field
6213 public function setQillAndWhere($name, $op, $value, $grouping, $field) {
6214 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value);
6215 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
6216 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(
6217 1 => $field['title'],