3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 * @param string $apiEntity
473 * The api entity being called.
474 * This sort-of duplicates $mode in a confusing way. Probably not by design.
476 public function initialize($apiEntity = NULL) {
477 $this->_select
= array();
478 $this->_element
= array();
479 $this->_tables
= array();
480 $this->_whereTables
= array();
481 $this->_where
= array();
482 $this->_qill
= array();
483 $this->_options
= array();
484 $this->_cfIDs
= array();
485 $this->_paramLookup
= array();
486 $this->_having
= array();
488 $this->_customQuery
= NULL;
490 // reset cached static variables - CRM-5803
491 self
::$_activityRole = NULL;
492 self
::$_considerCompActivities = NULL;
493 self
::$_withContactActivitiesOnly = NULL;
495 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
496 $this->_element
['contact_id'] = 1;
497 $this->_tables
['civicrm_contact'] = 1;
499 if (!empty($this->_params
)) {
500 $this->buildParamsLookup();
503 $this->_whereTables
= $this->_tables
;
505 $this->selectClause($apiEntity);
506 $this->_whereClause
= $this->whereClause($apiEntity);
507 if (array_key_exists('civicrm_contribution', $this->_whereTables
)) {
508 $component = 'contribution';
510 if (array_key_exists('civicrm_membership', $this->_whereTables
)) {
511 $component = 'membership';
513 if (isset($component)) {
514 CRM_Financial_BAO_FinancialType
::buildPermissionedClause($this->_whereClause
, $component);
517 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
518 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
520 $this->openedSearchPanes(TRUE);
524 * Function for same purpose as convertFormValues.
526 * Like convert form values this function exists to pre-Process parameters from the form.
528 * It is unclear why they are different functions & likely relates to advances search
529 * versus search builder.
531 * The direction we are going is having the form convert values to a standardised format &
532 * moving away from weird & wonderful where clause switches.
534 * Fix and handle contact deletion nicely.
536 * this code is primarily for search builder use case where different clauses can specify if they want deleted.
540 public function buildParamsLookup() {
541 $trashParamExists = FALSE;
542 $paramByGroup = array();
543 foreach ($this->_params
as $k => $param) {
544 if (!empty($param[0]) && $param[0] == 'contact_is_deleted') {
545 $trashParamExists = TRUE;
547 if (!empty($param[3])) {
548 $paramByGroup[$param[3]][$k] = $param;
552 if ($trashParamExists) {
553 $this->_skipDeleteClause
= TRUE;
555 //cycle through group sets and explicitly add trash param if not set
556 foreach ($paramByGroup as $setID => $set) {
558 !in_array(array('contact_is_deleted', '=', '1', $setID, '0'), $this->_params
) &&
559 !in_array(array('contact_is_deleted', '=', '0', $setID, '0'), $this->_params
)
561 $this->_params
[] = array(
562 'contact_is_deleted',
572 foreach ($this->_params
as $value) {
573 if (empty($value[0])) {
576 $cfID = CRM_Core_BAO_CustomField
::getKeyID($value[0]);
578 if (!array_key_exists($cfID, $this->_cfIDs
)) {
579 $this->_cfIDs
[$cfID] = array();
581 // Set wildcard value based on "and/or" selection
582 foreach ($this->_params
as $key => $param) {
583 if ($param[0] == $value[0] . '_operator') {
584 $value[4] = $param[2] == 'or';
588 $this->_cfIDs
[$cfID][] = $value;
591 if (!array_key_exists($value[0], $this->_paramLookup
)) {
592 $this->_paramLookup
[$value[0]] = array();
594 if ($value[0] !== 'group') {
595 // Just trying to unravel how group interacts here! This whole function is weird.
596 $this->_paramLookup
[$value[0]][] = $value;
602 * Some composite fields do not appear in the fields array hack to make them part of the query.
605 * The api entity being called.
606 * This sort-of duplicates $mode in a confusing way. Probably not by design.
608 public function addSpecialFields($apiEntity) {
609 static $special = array('contact_type', 'contact_sub_type', 'sort_name', 'display_name');
610 // if get called via Contact.get API having address_id as return parameter
611 if ($apiEntity == 'Contact') {
612 $special[] = 'address_id';
614 foreach ($special as $name) {
615 if (!empty($this->_returnProperties
[$name])) {
616 if ($name == 'address_id') {
617 $this->_tables
['civicrm_address'] = 1;
618 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
619 $this->_element
['address_id'] = 1;
622 $this->_select
[$name] = "contact_a.{$name} as $name";
623 $this->_element
[$name] = 1;
630 * Given a list of conditions in params and a list of desired
631 * return Properties generate the required select and from
632 * clauses. Note that since the where clause introduces new
633 * tables, the initial attempt also retrieves all variables used
636 * @param string $apiEntity
637 * The api entity being called.
638 * This sort-of duplicates $mode in a confusing way. Probably not by design.
640 public function selectClause($apiEntity = NULL) {
642 // @todo Tidy up this. This arises because 1) we are ignoring the $mode & adding a new
643 // param ($apiEntity) instead - presumably an oversight & 2 because
644 // contact is not implemented as a component.
645 $this->addSpecialFields($apiEntity);
647 foreach ($this->_fields
as $name => $field) {
648 // skip component fields
649 // there are done by the alter query below
650 // and need not be done on every field
651 // @todo remove these & handle using metadata - only obscure fields
652 // that are hack-added should need to be excluded from the main loop.
654 (substr($name, 0, 12) == 'participant_') ||
655 (substr($name, 0, 7) == 'pledge_') ||
656 (substr($name, 0, 5) == 'case_') ||
657 (substr($name, 0, 8) == 'payment_')
662 // redirect to activity select clause
664 (substr($name, 0, 9) == 'activity_') ||
665 ($name == 'parent_id')
667 CRM_Activity_BAO_Query
::select($this);
671 // if this is a hierarchical name, we ignore it
672 $names = explode('-', $name);
673 if (count($names) > 1 && isset($names[1]) && is_numeric($names[1])) {
677 // make an exception for special cases, to add the field in select clause
678 $makeException = FALSE;
680 //special handling for groups/tags
681 if (in_array($name, array('groups', 'tags', 'notes'))
682 && isset($this->_returnProperties
[substr($name, 0, -1)])
684 // @todo instead of setting make exception to get us into
685 // an if clause that has handling for these fields buried with in it
686 // move the handling to here.
687 $makeException = TRUE;
690 // since note has 3 different options we need special handling
691 // note / note_subject / note_body
692 if ($name == 'notes') {
693 foreach (array('note', 'note_subject', 'note_body') as $noteField) {
694 if (isset($this->_returnProperties
[$noteField])) {
695 $makeException = TRUE;
701 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
703 !empty($this->_paramLookup
[$name])
704 ||
!empty($this->_returnProperties
[$name])
705 ||
$this->pseudoConstantNameIsInReturnProperties($field)
709 // add to cfIDs array if not present
710 if (!array_key_exists($cfID, $this->_cfIDs
)) {
711 $this->_cfIDs
[$cfID] = array();
714 elseif (isset($field['where'])) {
715 list($tableName, $fieldName) = explode('.', $field['where'], 2);
716 if (isset($tableName)) {
717 if (CRM_Utils_Array
::value($tableName, self
::$_dependencies)) {
718 $this->_tables
['civicrm_address'] = 1;
719 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
720 $this->_element
['address_id'] = 1;
723 if ($tableName == 'im_provider' ||
$tableName == 'email_greeting' ||
724 $tableName == 'postal_greeting' ||
$tableName == 'addressee'
726 if ($tableName == 'im_provider') {
727 CRM_Core_OptionValue
::select($this);
730 if (in_array($tableName,
731 array('email_greeting', 'postal_greeting', 'addressee'))) {
732 $this->_element
["{$name}_id"] = 1;
733 $this->_select
["{$name}_id"] = "contact_a.{$name}_id as {$name}_id";
734 $this->_pseudoConstantsSelect
[$name] = array('pseudoField' => $tableName, 'idCol' => "{$name}_id");
735 $this->_pseudoConstantsSelect
[$name]['select'] = "{$name}.{$fieldName} as $name";
736 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
738 if ($tableName == 'email_greeting') {
740 $this->_pseudoConstantsSelect
[$name]['join']
741 = " LEFT JOIN civicrm_option_group option_group_email_greeting ON (option_group_email_greeting.name = 'email_greeting')";
742 $this->_pseudoConstantsSelect
[$name]['join'] .=
743 " 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 ) ";
745 elseif ($tableName == 'postal_greeting') {
747 $this->_pseudoConstantsSelect
[$name]['join']
748 = " LEFT JOIN civicrm_option_group option_group_postal_greeting ON (option_group_postal_greeting.name = 'postal_greeting')";
749 $this->_pseudoConstantsSelect
[$name]['join'] .=
750 " 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 ) ";
752 elseif ($tableName == 'addressee') {
754 $this->_pseudoConstantsSelect
[$name]['join']
755 = " LEFT JOIN civicrm_option_group option_group_addressee ON (option_group_addressee.name = 'addressee')";
756 $this->_pseudoConstantsSelect
[$name]['join'] .=
757 " LEFT JOIN civicrm_option_value addressee ON (contact_a.addressee_id = addressee.value AND option_group_addressee.id = addressee.option_group_id ) ";
759 $this->_pseudoConstantsSelect
[$name]['table'] = $tableName;
762 $greetField = "{$name}_display";
763 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
764 $this->_element
[$greetField] = 1;
766 $greetField = "{$name}_custom";
767 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
768 $this->_element
[$greetField] = 1;
772 if (!in_array($tableName, array('civicrm_state_province', 'civicrm_country', 'civicrm_county'))) {
773 $this->_tables
[$tableName] = 1;
776 // also get the id of the tableName
777 $tName = substr($tableName, 8);
778 if (in_array($tName, array('country', 'state_province', 'county'))) {
779 if ($tName == 'state_province') {
780 $this->_pseudoConstantsSelect
['state_province_name'] = array(
781 'pseudoField' => "{$tName}",
782 'idCol' => "{$tName}_id",
783 'bao' => 'CRM_Core_BAO_Address',
784 'table' => "civicrm_{$tName}",
785 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
788 $this->_pseudoConstantsSelect
[$tName] = array(
789 'pseudoField' => 'state_province_abbreviation',
790 'idCol' => "{$tName}_id",
791 'table' => "civicrm_{$tName}",
792 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
796 $this->_pseudoConstantsSelect
[$name] = array(
797 'pseudoField' => "{$tName}_id",
798 'idCol' => "{$tName}_id",
799 'bao' => 'CRM_Core_BAO_Address',
800 'table' => "civicrm_{$tName}",
801 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
805 $this->_select
["{$tName}_id"] = "civicrm_address.{$tName}_id as {$tName}_id";
806 $this->_element
["{$tName}_id"] = 1;
808 elseif ($tName != 'contact') {
809 $this->_select
["{$tName}_id"] = "{$tableName}.id as {$tName}_id";
810 $this->_element
["{$tName}_id"] = 1;
813 //special case for phone
814 if ($name == 'phone') {
815 $this->_select
['phone_type_id'] = "civicrm_phone.phone_type_id as phone_type_id";
816 $this->_element
['phone_type_id'] = 1;
819 // if IM then select provider_id also
820 // to get "IM Service Provider" in a file to be exported, CRM-3140
822 $this->_select
['provider_id'] = "civicrm_im.provider_id as provider_id";
823 $this->_element
['provider_id'] = 1;
826 if ($tName == 'contact' && $fieldName == 'organization_name') {
827 // special case, when current employer is set for Individual contact
828 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name";
830 elseif ($tName == 'contact' && $fieldName === 'id') {
831 // Handled elsewhere, explicitly ignore. Possibly for all tables...
833 elseif (in_array($tName, array('country', 'county'))) {
834 $this->_pseudoConstantsSelect
[$name]['select'] = "{$field['where']} as `$name`";
835 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
837 elseif ($tName == 'state_province') {
838 $this->_pseudoConstantsSelect
[$tName]['select'] = "{$field['where']} as `$name`";
839 $this->_pseudoConstantsSelect
[$tName]['element'] = $name;
841 elseif (strpos($name, 'contribution_soft_credit') !== FALSE) {
842 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_params
)) {
843 $this->_select
[$name] = "{$field['where']} as `$name`";
847 // If we have an option group defined then rather than joining the option value table in
848 // (which is an unindexed join) we render the option value on output.
849 // @todo - extend this to other pseudoconstants.
850 if ($this->pseudoConstantNameIsInReturnProperties($field)) {
851 $pseudoFieldName = $field['pseudoconstant']['optionGroupName'];
852 $this->_pseudoConstantsSelect
[$pseudoFieldName] = array(
853 'pseudoField' => $field['name'],
854 'idCol' => $field['name'],
855 'field_name' => $field['name'],
856 'bao' => $field['bao'],
857 'pseudoconstant' => $field['pseudoconstant'],
859 $this->_tables
[$tableName] = 1;
860 $this->_element
[$pseudoFieldName] = 1;
862 $this->_select
[$name] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$name`");
864 if (!in_array($tName, array('state_province', 'country', 'county'))) {
865 $this->_element
[$name] = 1;
870 elseif ($name === 'tags') {
871 //@todo move this handling outside the big IF & ditch $makeException
872 $this->_useGroupBy
= TRUE;
873 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags";
874 $this->_element
[$name] = 1;
875 $this->_tables
['civicrm_tag'] = 1;
876 $this->_tables
['civicrm_entity_tag'] = 1;
878 elseif ($name === 'groups') {
879 //@todo move this handling outside the big IF & ditch $makeException
880 $this->_useGroupBy
= TRUE;
881 // Duplicates will be created here but better to sort them out in php land.
882 $this->_select
[$name] = "
884 GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')),
885 GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id)
888 $this->_element
[$name] = 1;
889 $this->_tables
['civicrm_group_contact'] = 1;
890 $this->_tables
['civicrm_group_contact_cache'] = 1;
891 $this->_pseudoConstantsSelect
["{$name}"] = array(
892 'pseudoField' => "groups",
896 elseif ($name === 'notes') {
897 //@todo move this handling outside the big IF & ditch $makeException
898 // if note field is subject then return subject else body of the note
899 $noteColumn = 'note';
900 if (isset($noteField) && $noteField == 'note_subject') {
901 $noteColumn = 'subject';
904 $this->_useGroupBy
= TRUE;
905 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_note.$noteColumn)) as notes";
906 $this->_element
[$name] = 1;
907 $this->_tables
['civicrm_note'] = 1;
909 elseif ($name === 'current_employer') {
910 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer";
911 $this->_element
[$name] = 1;
915 if ($cfID && !empty($field['is_search_range'])) {
916 // this is a custom field with range search enabled, so we better check for two/from values
917 if (!empty($this->_paramLookup
[$name . '_from'])) {
918 if (!array_key_exists($cfID, $this->_cfIDs
)) {
919 $this->_cfIDs
[$cfID] = array();
921 foreach ($this->_paramLookup
[$name . '_from'] as $pID => $p) {
922 // search in the cdID array for the same grouping
924 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
925 if ($c[3] == $p[3]) {
926 $this->_cfIDs
[$cfID][$cID][2]['from'] = $p[2];
931 $p[2] = array('from' => $p[2]);
932 $this->_cfIDs
[$cfID][] = $p;
936 if (!empty($this->_paramLookup
[$name . '_to'])) {
937 if (!array_key_exists($cfID, $this->_cfIDs
)) {
938 $this->_cfIDs
[$cfID] = array();
940 foreach ($this->_paramLookup
[$name . '_to'] as $pID => $p) {
941 // search in the cdID array for the same grouping
943 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
944 if ($c[4] == $p[4]) {
945 $this->_cfIDs
[$cfID][$cID][2]['to'] = $p[2];
950 $p[2] = array('to' => $p[2]);
951 $this->_cfIDs
[$cfID][] = $p;
958 // add location as hierarchical elements
959 $this->addHierarchicalElements();
961 // add multiple field like website
962 $this->addMultipleElements();
965 CRM_Core_Component
::alterQuery($this, 'select');
967 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'select');
969 if (!empty($this->_cfIDs
)) {
970 // @todo This function is the select function but instead of running 'select' it
971 // is running the whole query.
972 $this->_customQuery
= new CRM_Core_BAO_CustomQuery($this->_cfIDs
, TRUE, $this->_locationSpecificCustomFields
);
973 $this->_customQuery
->query();
974 $this->_select
= array_merge($this->_select
, $this->_customQuery
->_select
);
975 $this->_element
= array_merge($this->_element
, $this->_customQuery
->_element
);
976 $this->_tables
= array_merge($this->_tables
, $this->_customQuery
->_tables
);
977 $this->_whereTables
= array_merge($this->_whereTables
, $this->_customQuery
->_whereTables
);
978 $this->_options
= $this->_customQuery
->_options
;
983 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
985 public function addHierarchicalElements() {
986 if (empty($this->_returnProperties
['location'])) {
989 if (!is_array($this->_returnProperties
['location'])) {
993 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
994 $processed = array();
997 $addressCustomFields = CRM_Core_BAO_CustomField
::getFieldsForImport('Address');
998 $addressCustomFieldIds = array();
1000 foreach ($this->_returnProperties
['location'] as $name => $elements) {
1001 $lCond = self
::getPrimaryCondition($name);
1004 $locationTypeId = array_search($name, $locationTypes);
1005 if ($locationTypeId === FALSE) {
1008 $lCond = "location_type_id = $locationTypeId";
1009 $this->_useDistinct
= TRUE;
1011 //commented for CRM-3256
1012 $this->_useGroupBy
= TRUE;
1015 $name = str_replace(' ', '_', $name);
1017 $tName = "$name-location_type";
1018 $ltName = "`$name-location_type`";
1019 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1020 $this->_select
["{$tName}"] = "`$tName`.name as `{$tName}`";
1021 $this->_element
["{$tName}_id"] = 1;
1022 $this->_element
["{$tName}"] = 1;
1024 $locationTypeName = $tName;
1025 $locationTypeJoin = array();
1027 $addAddress = FALSE;
1029 foreach ($elements as $elementFullName => $dontCare) {
1031 $elementName = $elementCmpName = $elementFullName;
1033 if (substr($elementCmpName, 0, 5) == 'phone') {
1034 $elementCmpName = 'phone';
1037 if (in_array($elementCmpName, array_keys($addressCustomFields))) {
1038 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($elementCmpName)) {
1039 $addressCustomFieldIds[$cfID][$name] = 1;
1042 //add address table only once
1043 if ((in_array($elementCmpName, self
::$_locationSpecificFields) ||
!empty($addressCustomFieldIds))
1045 && !in_array($elementCmpName, array('email', 'phone', 'im', 'openid'))
1047 $tName = "$name-address";
1048 $aName = "`$name-address`";
1049 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1050 $this->_element
["{$tName}_id"] = 1;
1051 $addressJoin = "\nLEFT JOIN civicrm_address $aName ON ($aName.contact_id = contact_a.id AND $aName.$lCond)";
1052 $this->_tables
[$tName] = $addressJoin;
1053 $locationTypeJoin[$tName] = " ( $aName.location_type_id = $ltName.id ) ";
1054 $processed[$aName] = 1;
1058 $cond = $elementType = '';
1059 if (strpos($elementName, '-') !== FALSE) {
1060 // this is either phone, email or IM
1061 list($elementName, $elementType) = explode('-', $elementName);
1063 if (($elementName != 'phone') && ($elementName != 'im')) {
1064 $cond = self
::getPrimaryCondition($elementType);
1066 // CRM-13011 : If location type is primary, do not restrict search to the phone
1067 // type id - we want the primary phone, regardless of what type it is.
1068 // Otherwise, restrict to the specified phone type for the given field.
1069 if ((!$cond) && ($elementName == 'phone')) {
1070 $cond = "phone_type_id = '$elementType'";
1072 elseif ((!$cond) && ($elementName == 'im')) {
1073 // IM service provider id, CRM-3140
1074 $cond = "provider_id = '$elementType'";
1076 $elementType = '-' . $elementType;
1079 $field = CRM_Utils_Array
::value($elementName, $this->_fields
);
1081 // hack for profile, add location id
1084 // fix for CRM-882( to handle phone types )
1085 !is_numeric($elementType)
1087 if (is_numeric($name)) {
1088 $field = CRM_Utils_Array
::value($elementName . "-Primary$elementType", $this->_fields
);
1091 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1094 elseif (is_numeric($name)) {
1095 //this for phone type to work
1096 if (in_array($elementName, array('phone', 'phone_ext'))) {
1097 $field = CRM_Utils_Array
::value($elementName . "-Primary" . $elementType, $this->_fields
);
1100 $field = CRM_Utils_Array
::value($elementName . "-Primary", $this->_fields
);
1104 //this is for phone type to work for profile edit
1105 if (in_array($elementName, array('phone', 'phone_ext'))) {
1106 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1109 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId", $this->_fields
);
1114 // Check if there is a value, if so also add to where Clause
1116 if ($this->_params
) {
1118 if (isset($locationTypeId)) {
1119 $nm .= "-$locationTypeId";
1121 if (!is_numeric($elementType)) {
1122 $nm .= "$elementType";
1125 foreach ($this->_params
as $id => $values) {
1126 if ((is_array($values) && $values[0] == $nm) ||
1127 (in_array($elementName, array('phone', 'im'))
1128 && (strpos($values[0], $nm) !== FALSE)
1138 if ($field && isset($field['where'])) {
1139 list($tableName, $fieldName) = explode('.', $field['where'], 2);
1140 $pf = substr($tableName, 8);
1141 $tName = $name . '-' . $pf . $elementType;
1142 if (isset($tableName)) {
1143 if ($tableName == 'civicrm_state_province' ||
$tableName == 'civicrm_country' ||
$tableName == 'civicrm_county') {
1144 $this->_select
["{$tName}_id"] = "{$aName}.{$pf}_id as `{$tName}_id`";
1147 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1150 $this->_element
["{$tName}_id"] = 1;
1151 if (substr($tName, -15) == '-state_province') {
1152 // FIXME: hack to fix CRM-1900
1153 $a = Civi
::settings()->get('address_format');
1155 if (substr_count($a, 'state_province_name') > 0) {
1156 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1157 'pseudoField' => "{$pf}_id",
1158 'idCol' => "{$tName}_id",
1159 'bao' => 'CRM_Core_BAO_Address',
1161 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.name as `{$name}-{$elementFullName}`";
1164 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1165 'pseudoField' => 'state_province_abbreviation',
1166 'idCol' => "{$tName}_id",
1168 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.abbreviation as `{$name}-{$elementFullName}`";
1172 if (substr($elementFullName, 0, 2) == 'im') {
1173 $provider = "{$name}-{$elementFullName}-provider_id";
1174 $this->_select
[$provider] = "`$tName`.provider_id as `{$name}-{$elementFullName}-provider_id`";
1175 $this->_element
[$provider] = 1;
1177 if ($pf == 'country' ||
$pf == 'county') {
1178 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1179 'pseudoField' => "{$pf}_id",
1180 'idCol' => "{$tName}_id",
1181 'bao' => 'CRM_Core_BAO_Address',
1183 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1186 $this->_select
["{$name}-{$elementFullName}"] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1190 if (in_array($pf, array('state_province', 'country', 'county'))) {
1191 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['element'] = "{$name}-{$elementFullName}";
1194 $this->_element
["{$name}-{$elementFullName}"] = 1;
1197 if (empty($processed["`$tName`"])) {
1198 $processed["`$tName`"] = 1;
1199 $newName = $tableName . '_' . $index;
1200 switch ($tableName) {
1201 case 'civicrm_phone':
1202 case 'civicrm_email':
1204 case 'civicrm_openid':
1206 $this->_tables
[$tName] = "\nLEFT JOIN $tableName `$tName` ON contact_a.id = `$tName`.contact_id";
1207 if ($tableName != 'civicrm_phone') {
1208 $this->_tables
[$tName] .= " AND `$tName`.$lCond";
1210 elseif (is_numeric($name)) {
1211 $this->_select
[$tName] = "IF (`$tName`.is_primary = $name, `$tName`.phone, NULL) as `$tName`";
1214 // this special case to add phone type
1216 $phoneTypeCondition = " AND `$tName`.$cond ";
1217 //gross hack to pickup corrupted data also, CRM-7603
1218 if (strpos($cond, 'phone_type_id') !== FALSE) {
1219 $phoneTypeCondition = " AND ( `$tName`.$cond OR `$tName`.phone_type_id IS NULL ) ";
1220 if (!empty($lCond)) {
1221 $phoneTypeCondition .= " AND ( `$tName`.$lCond ) ";
1224 $this->_tables
[$tName] .= $phoneTypeCondition;
1227 //build locationType join
1228 $locationTypeJoin[$tName] = " ( `$tName`.location_type_id = $ltName.id )";
1231 $this->_whereTables
[$tName] = $this->_tables
[$tName];
1235 case 'civicrm_state_province':
1236 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $tName;
1237 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1238 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.state_province_id";
1240 $this->_whereTables
["{$name}-address"] = $addressJoin;
1244 case 'civicrm_country':
1245 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1246 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1247 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.country_id";
1249 $this->_whereTables
["{$name}-address"] = $addressJoin;
1253 case 'civicrm_county':
1254 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1255 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1256 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.county_id";
1258 $this->_whereTables
["{$name}-address"] = $addressJoin;
1264 $this->_whereTables
["{$name}-address"] = $addressJoin;
1273 // add location type join
1274 $ltypeJoin = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locationTypeJoin) . " )";
1275 $this->_tables
[$locationTypeName] = $ltypeJoin;
1277 // table should be present in $this->_whereTables,
1278 // to add its condition in location type join, CRM-3939.
1279 if ($addWhereCount) {
1280 $locClause = array();
1281 foreach ($this->_whereTables
as $tableName => $clause) {
1282 if (!empty($locationTypeJoin[$tableName])) {
1283 $locClause[] = $locationTypeJoin[$tableName];
1287 if (!empty($locClause)) {
1288 $this->_whereTables
[$locationTypeName] = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locClause) . " )";
1293 if (!empty($addressCustomFieldIds)) {
1294 $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds);
1295 foreach ($addressCustomFieldIds as $cfID => $locTypeName) {
1296 foreach ($locTypeName as $name => $dnc) {
1297 $this->_locationSpecificCustomFields
[$cfID] = array($name, array_search($name, $locationTypes));
1298 $fieldName = "$name-custom_{$cfID}";
1299 $tName = "$name-address-custom-{$cfID}";
1300 $aName = "`$name-address-custom-{$cfID}`";
1301 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1302 $this->_element
["{$tName}_id"] = 1;
1303 $this->_select
[$fieldName] = "`$tName`.{$customQuery->_fields[$cfID]['column_name']} as `{$fieldName}`";
1304 $this->_element
[$fieldName] = 1;
1305 $this->_tables
[$tName] = "\nLEFT JOIN {$customQuery->_fields[$cfID]['table_name']} $aName ON ($aName.entity_id = `$name-address`.id)";
1312 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
1314 public function addMultipleElements() {
1315 if (empty($this->_returnProperties
['website'])) {
1318 if (!is_array($this->_returnProperties
['website'])) {
1322 foreach ($this->_returnProperties
['website'] as $key => $elements) {
1323 foreach ($elements as $elementFullName => $dontCare) {
1324 $tName = "website-{$key}-{$elementFullName}";
1325 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1326 $this->_select
["{$tName}"] = "`$tName`.url as `{$tName}`";
1327 $this->_element
["{$tName}_id"] = 1;
1328 $this->_element
["{$tName}"] = 1;
1330 $type = "website-{$key}-website_type_id";
1331 $this->_select
[$type] = "`$tName`.website_type_id as `{$type}`";
1332 $this->_element
[$type] = 1;
1333 $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website `$tName` ON (`$tName`.contact_id = contact_a.id AND `$tName`.website_type_id = $key )";
1339 * Generate the query based on what type of query we need.
1341 * @param bool $count
1342 * @param bool $sortByChar
1343 * @param bool $groupContacts
1344 * @param bool $onlyDeleted
1347 * sql query parts as an array
1349 public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE, $onlyDeleted = FALSE) {
1350 // build permission clause
1351 $this->generatePermissionClause($onlyDeleted, $count);
1354 if (isset($this->_rowCountClause
)) {
1355 $select = "SELECT {$this->_rowCountClause}";
1357 elseif (isset($this->_distinctComponentClause
)) {
1358 // we add distinct to get the right count for components
1359 // for the more complex result set, we use GROUP BY the same id
1361 $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount";
1364 $select = 'SELECT count(DISTINCT contact_a.id) as rowCount';
1366 $from = $this->_simpleFromClause
;
1367 if ($this->_useDistinct
) {
1368 $this->_useGroupBy
= TRUE;
1371 elseif ($sortByChar) {
1372 $select = 'SELECT DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) as sort_name';
1373 $from = $this->_simpleFromClause
;
1375 elseif ($groupContacts) {
1376 $select = 'SELECT contact_a.id as id';
1377 if ($this->_useDistinct
) {
1378 $this->_useGroupBy
= TRUE;
1380 $from = $this->_simpleFromClause
;
1383 if (!empty($this->_paramLookup
['group'])) {
1385 list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup
['group'][0];
1387 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
1388 $this->_paramLookup
['group'][0][1] = key($value);
1391 // Presumably the lines below come into manage groups screen.
1392 // make sure there is only one element
1393 // this is used when we are running under smog and need to know
1394 // how the contact was added (CRM-1203)
1395 $groups = (array) CRM_Utils_Array
::value($this->_paramLookup
['group'][0][1], $this->_paramLookup
['group'][0][2], $this->_paramLookup
['group'][0][2]);
1396 if ((count($this->_paramLookup
['group']) == 1) &&
1397 (count($groups) == 1)
1399 $groupId = $groups[0];
1401 //check if group is saved search
1402 $group = new CRM_Contact_BAO_Group();
1403 $group->id
= $groupId;
1406 if (!isset($group->saved_search_id
)) {
1407 $tbName = "`civicrm_group_contact-{$groupId}`";
1408 // CRM-17254 don't retrieve extra fields if contact_id is specifically requested
1409 // as this will add load to an intentionally light query.
1410 // ideally this code would be removed as it appears to be to support CRM-1203
1411 // and passing in the required returnProperties from the url would
1412 // make more sense that globally applying the requirements of one form.
1413 if (($this->_returnProperties
!= array('contact_id'))) {
1414 $this->_select
['group_contact_id'] = "$tbName.id as group_contact_id";
1415 $this->_element
['group_contact_id'] = 1;
1416 $this->_select
['status'] = "$tbName.status as status";
1417 $this->_element
['status'] = 1;
1421 $this->_useGroupBy
= TRUE;
1423 if ($this->_useDistinct
&& !isset($this->_distinctComponentClause
)) {
1424 if (!($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
)) {
1426 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
1427 $this->_useDistinct
= FALSE;
1428 $this->_useGroupBy
= TRUE;
1432 $select = "SELECT ";
1433 if (isset($this->_distinctComponentClause
)) {
1434 $select .= "{$this->_distinctComponentClause}, ";
1436 $select .= implode(', ', $this->_select
);
1437 $from = $this->_fromClause
;
1441 if (!empty($this->_whereClause
)) {
1442 $where = "WHERE {$this->_whereClause}";
1445 if (!empty($this->_permissionWhereClause
) && empty($this->_displayRelationshipType
)) {
1446 if (empty($where)) {
1447 $where = "WHERE $this->_permissionWhereClause";
1450 $where = "$where AND $this->_permissionWhereClause";
1455 if (!empty($this->_having
)) {
1456 foreach ($this->_having
as $havingSets) {
1457 foreach ($havingSets as $havingSet) {
1458 $havingValue[] = $havingSet;
1461 $having = ' HAVING ' . implode(' AND ', $havingValue);
1464 // if we are doing a transform, do it here
1465 // use the $from, $where and $having to get the contact ID
1466 if ($this->_displayRelationshipType
) {
1467 $this->filterRelatedContacts($from, $where, $having);
1470 return array($select, $from, $where, $having);
1474 * Get where values from the parameters.
1476 * @param string $name
1477 * @param mixed $grouping
1481 public function getWhereValues($name, $grouping) {
1483 foreach ($this->_params
as $values) {
1484 if ($values[0] == $name && $values[3] == $grouping) {
1495 * @param bool $relative
1496 * @param string $from
1499 public static function fixDateValues($relative, &$from, &$to) {
1501 list($from, $to) = CRM_Utils_Date
::getFromTo($relative, $from, $to);
1506 * Convert values from form-appropriate to query-object appropriate.
1508 * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax.
1509 * So, ideally we would convert all fields to look like
1512 * // Set the operator for legacy reasons, but it is ignored
1514 * // array in sql filter syntax
1515 * 2 => array('BETWEEN' => array(1,60),
1520 * There are some examples of the syntax in
1521 * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship
1523 * More notes at CRM_Core_DAO::createSQLFilter
1525 * and a list of supported operators in CRM_Core_DAO
1527 * @param array $formValues
1528 * @param int $wildcard
1529 * @param bool $useEquals
1531 * @param string $apiEntity
1533 * @param array $entityReferenceFields
1534 * Field names of any entity reference fields (which will need reformatting to IN syntax).
1538 public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL,
1539 $entityReferenceFields = array()) {
1541 if (empty($formValues)) {
1545 self
::filterCountryFromValuesIfStateExists($formValues);
1547 foreach ($formValues as $id => &$val) {
1548 // CRM-19374 - we don't want to change $val in $formValues.
1549 // Assign it to a temp variable which operates while iteration.
1552 if (self
::isAlreadyProcessedForQueryFormat($values)) {
1553 $params[] = $values;
1557 self
::legacyConvertFormValues($id, $values);
1559 // The form uses 1 field to represent two db fields
1560 if ($id == 'contact_type' && $values && (!is_array($values) ||
!array_intersect(array_keys($values), CRM_Core_DAO
::acceptedSQLOperators()))) {
1561 $contactType = array();
1563 foreach ((array) $values as $key => $type) {
1564 $types = explode('__', is_numeric($type) ?
$key : $type);
1565 $contactType[$types[0]] = $types[0];
1566 // Add sub-type if specified
1567 if (!empty($types[1])) {
1568 $subType[$types[1]] = $types[1];
1571 $params[] = array('contact_type', 'IN', $contactType, 0, 0);
1573 $params[] = array('contact_sub_type', 'IN', $subType, 0, 0);
1576 elseif ($id == 'privacy') {
1577 if (is_array($formValues['privacy'])) {
1578 $op = !empty($formValues['privacy']['do_not_toggle']) ?
'=' : '!=';
1579 foreach ($formValues['privacy'] as $key => $value) {
1581 $params[] = array($key, $op, $value, 0, 0);
1586 elseif ($id == 'email_on_hold') {
1587 if ($formValues['email_on_hold']['on_hold']) {
1588 $params[] = array('on_hold', '=', $formValues['email_on_hold']['on_hold'], 0, 0);
1591 elseif (substr($id, 0, 7) == 'custom_'
1593 substr($id, -9, 9) == '_relative'
1594 ||
substr($id, -5, 5) == '_from'
1595 ||
substr($id, -3, 3) == '_to'
1598 self
::convertCustomRelativeFields($formValues, $params, $values, $id);
1600 elseif (preg_match('/_date_relative$/', $id) ||
1601 $id == 'event_relative' ||
1602 $id == 'case_from_relative' ||
1603 $id == 'case_to_relative' ||
1604 $id == 'participant_relative'
1606 if ($id == 'event_relative') {
1607 $fromRange = 'event_start_date_low';
1608 $toRange = 'event_end_date_high';
1610 elseif ($id == 'participant_relative') {
1611 $fromRange = 'participant_register_date_low';
1612 $toRange = 'participant_register_date_high';
1614 elseif ($id == 'case_from_relative') {
1615 $fromRange = 'case_from_start_date_low';
1616 $toRange = 'case_from_start_date_high';
1618 elseif ($id == 'case_to_relative') {
1619 $fromRange = 'case_to_end_date_low';
1620 $toRange = 'case_to_end_date_high';
1623 $dateComponent = explode('_date_relative', $id);
1624 $fromRange = "{$dateComponent[0]}_date_low";
1625 $toRange = "{$dateComponent[0]}_date_high";
1628 if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
1629 // relative dates are not processed correctly as lower date value were ignored,
1630 // to ensure both high and low date value got added IF there is relative date,
1631 // we need to reset $formValues by unset and then adding again via CRM_Contact_BAO_Query::fixDateValues(...)
1632 if (!empty($formValues[$id])) {
1633 unset($formValues[$fromRange]);
1634 unset($formValues[$toRange]);
1636 CRM_Contact_BAO_Query
::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
1640 elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
1642 $params[] = array($id, 'IN', explode(',', $values), 0, 0);
1645 $values = CRM_Contact_BAO_Query
::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity);
1650 $params[] = $values;
1657 * Function to support legacy format for groups and tags.
1660 * @param array|int $values
1663 public static function legacyConvertFormValues($id, &$values) {
1664 $legacyElements = array(
1669 'membership_type_id',
1670 'membership_status_id',
1672 if (in_array($id, $legacyElements) && is_array($values)) {
1673 // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
1674 // as per the recent Search fixes $values need to be in standard array(id1, id2) format
1675 $values = CRM_Utils_Array
::convertCheckboxFormatToArray($values);
1680 * Fix values from query from/to something no-one cared enough to document.
1683 * @param array $values
1684 * @param int $wildcard
1685 * @param bool $useEquals
1687 * @param string $apiEntity
1689 * @return array|null
1691 public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) {
1692 // skip a few search variables
1693 static $skipWhere = NULL;
1694 static $likeNames = NULL;
1697 // Change camelCase EntityName to lowercase with underscores
1698 $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity);
1700 // check if $value is in OK (Operator as Key) format as used by Get API
1701 if (CRM_Utils_System
::isNull($values)) {
1713 'display_relationship_type',
1717 if (in_array($id, $skipWhere) ||
1718 substr($id, 0, 4) == '_qf_' ||
1719 substr($id, 0, 7) == 'hidden_'
1725 (substr($id, 0, strlen($apiEntity)) != $apiEntity) &&
1726 (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_') &&
1727 (substr($id, 0, 7) != 'custom_')
1729 $id = $apiEntity . '_' . $id;
1733 $likeNames = array('sort_name', 'email', 'note', 'display_name');
1736 // email comes in via advanced search
1737 // so use wildcard always
1738 if ($id == 'email') {
1742 if (!$useEquals && in_array($id, $likeNames)) {
1743 $result = array($id, 'LIKE', $values, 0, 1);
1745 elseif (is_string($values) && strpos($values, '%') !== FALSE) {
1746 $result = array($id, 'LIKE', $values, 0, 0);
1748 elseif ($id == 'contact_type' ||
1749 (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO
::acceptedSQLOperators(), TRUE))
1751 $result = array($id, 'IN', $values, 0, $wildcard);
1754 $result = array($id, '=', $values, 0, $wildcard);
1761 * Get the where clause for a single field.
1763 * @param array $values
1764 * @param string $apiEntity
1766 public function whereClauseSingle(&$values, $apiEntity = NULL) {
1767 // do not process custom fields or prefixed contact ids or component params
1768 if (CRM_Core_BAO_CustomField
::getKeyID($values[0]) ||
1769 (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) ||
1770 (substr($values[0], 0, 13) == 'contribution_') ||
1771 (substr($values[0], 0, 6) == 'event_') ||
1772 (substr($values[0], 0, 12) == 'participant_') ||
1773 (substr($values[0], 0, 7) == 'member_') ||
1774 (substr($values[0], 0, 6) == 'grant_') ||
1775 (substr($values[0], 0, 7) == 'pledge_') ||
1776 (substr($values[0], 0, 5) == 'case_') ||
1777 (substr($values[0], 0, 10) == 'financial_') ||
1778 (substr($values[0], 0, 8) == 'payment_') ||
1779 (substr($values[0], 0, 11) == 'membership_')
1784 // skip for hook injected fields / params
1785 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
1786 if (array_key_exists($values[0], $extFields)) {
1790 switch ($values[0]) {
1791 case 'deleted_contacts':
1792 $this->deletedContacts($values);
1795 case 'contact_sub_type':
1796 $this->contactSubType($values);
1801 $this->group($values);
1804 // case tag comes from find contacts
1806 $this->tagSearch($values);
1810 case 'contact_tags':
1811 $this->tag($values);
1816 case 'note_subject':
1817 $this->notes($values);
1821 $this->ufUser($values);
1825 case 'display_name':
1826 $this->sortName($values);
1830 case 'postal_greeting':
1831 case 'email_greeting':
1832 $this->greetings($values);
1837 $this->email($values, $apiEntity);
1840 case 'phone_numeric':
1841 $this->phone_numeric($values);
1844 case 'phone_phone_type_id':
1845 case 'phone_location_type_id':
1846 $this->phone_option_group($values);
1849 case 'street_address':
1850 $this->street_address($values);
1853 case 'street_number':
1854 $this->street_number($values);
1857 case 'sortByCharacter':
1858 $this->sortByCharacter($values);
1861 case 'location_type':
1862 $this->locationType($values);
1866 $this->county($values);
1869 case 'state_province':
1870 case 'state_province_id':
1871 case 'state_province_name':
1872 $this->stateProvince($values);
1877 $this->country($values, FALSE);
1881 case 'postal_code_low':
1882 case 'postal_code_high':
1883 $this->postalCode($values);
1886 case 'activity_date':
1887 case 'activity_date_low':
1888 case 'activity_date_high':
1889 case 'activity_role':
1890 case 'activity_status_id':
1891 case 'activity_status':
1892 case 'activity_priority':
1893 case 'activity_priority_id':
1894 case 'followup_parent_id':
1896 case 'source_contact_id':
1897 case 'activity_text':
1898 case 'activity_option':
1899 case 'test_activities':
1900 case 'activity_type_id':
1901 case 'activity_type':
1902 case 'activity_survey_id':
1903 case 'activity_tags':
1904 case 'activity_taglist':
1905 case 'activity_test':
1906 case 'activity_campaign_id':
1907 case 'activity_engagement_level':
1909 case 'activity_result':
1910 case 'source_contact':
1911 CRM_Activity_BAO_Query
::whereClauseSingle($values, $this);
1916 case 'birth_date_low':
1917 case 'birth_date_high':
1918 case 'deceased_date_low':
1919 case 'deceased_date_high':
1920 $this->demographics($values);
1923 case 'age_asof_date':
1924 // handled by demographics
1927 case 'log_date_low':
1928 case 'log_date_high':
1929 $this->modifiedDates($values);
1933 $this->changeLog($values);
1936 case 'do_not_phone':
1937 case 'do_not_email':
1940 case 'do_not_trade':
1942 $this->privacy($values);
1945 case 'privacy_options':
1946 $this->privacyOptions($values);
1949 case 'privacy_operator':
1950 case 'privacy_toggle':
1951 // these are handled by privacy options
1954 case 'preferred_communication_method':
1955 $this->preferredCommunication($values);
1958 case 'relation_type_id':
1959 case 'relation_start_date_high':
1960 case 'relation_start_date_low':
1961 case 'relation_end_date_high':
1962 case 'relation_end_date_low':
1963 case 'relation_target_name':
1964 case 'relation_status':
1965 case 'relation_date_low':
1966 case 'relation_date_high':
1967 $this->relationship($values);
1968 $this->_relationshipValuesAdded
= TRUE;
1971 case 'task_status_id':
1972 $this->task($values);
1976 // since this case is handled with the above
1979 case 'prox_distance':
1980 CRM_Contact_BAO_ProximityQuery
::process($this, $values);
1983 case 'prox_street_address':
1985 case 'prox_postal_code':
1986 case 'prox_state_province_id':
1987 case 'prox_country_id':
1988 // handled by the proximity_distance clause
1992 $this->restWhere($values);
1998 * Given a list of conditions in params generate the required where clause.
2000 * @param string $apiEntity
2004 public function whereClause($apiEntity = NULL) {
2005 $this->_where
[0] = array();
2006 $this->_qill
[0] = array();
2008 $this->includeContactIds();
2009 if (!empty($this->_params
)) {
2010 foreach (array_keys($this->_params
) as $id) {
2011 if (empty($this->_params
[$id][0])) {
2014 // check for both id and contact_id
2015 if ($this->_params
[$id][0] == 'id' ||
$this->_params
[$id][0] == 'contact_id') {
2016 $this->_where
[0][] = self
::buildClause("contact_a.id", $this->_params
[$id][1], $this->_params
[$id][2]);
2019 $this->whereClauseSingle($this->_params
[$id], $apiEntity);
2023 CRM_Core_Component
::alterQuery($this, 'where');
2025 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'where');
2028 if ($this->_customQuery
) {
2029 // Added following if condition to avoid the wrong value display for 'my account' / any UF info.
2030 // Hope it wont affect the other part of civicrm.. if it does please remove it.
2031 if (!empty($this->_customQuery
->_where
)) {
2032 $this->_where
= CRM_Utils_Array
::crmArrayMerge($this->_where
, $this->_customQuery
->_where
);
2035 $this->_qill
= CRM_Utils_Array
::crmArrayMerge($this->_qill
, $this->_customQuery
->_qill
);
2039 $andClauses = array();
2042 if (!empty($this->_where
)) {
2043 foreach ($this->_where
as $grouping => $values) {
2044 if ($grouping > 0 && !empty($values)) {
2045 $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) ';
2050 if (!empty($this->_where
[0])) {
2051 $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where
[0]) . ' ) ';
2053 if (!empty($clauses)) {
2054 $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
2057 if ($validClauses > 1) {
2058 $this->_useDistinct
= TRUE;
2062 return implode(' AND ', $andClauses);
2066 * Generate where clause for any parameters not already handled.
2068 * @param array $values
2072 public function restWhere(&$values) {
2073 $name = CRM_Utils_Array
::value(0, $values);
2074 $op = CRM_Utils_Array
::value(1, $values);
2075 $value = CRM_Utils_Array
::value(2, $values);
2076 $grouping = CRM_Utils_Array
::value(3, $values);
2077 $wildcard = CRM_Utils_Array
::value(4, $values);
2079 if (isset($grouping) && empty($this->_where
[$grouping])) {
2080 $this->_where
[$grouping] = array();
2083 $multipleFields = array('url');
2085 //check if the location type exists for fields
2087 $locType = explode('-', $name);
2089 if (!in_array($locType[0], $multipleFields)) {
2090 //add phone type if exists
2091 if (isset($locType[2]) && $locType[2]) {
2092 $locType[2] = CRM_Core_DAO
::escapeString($locType[2]);
2096 $field = CRM_Utils_Array
::value($name, $this->_fields
);
2099 $field = CRM_Utils_Array
::value($locType[0], $this->_fields
);
2108 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
2109 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
2110 if (isset($locType[1]) && is_numeric($locType[1])) {
2111 $lType = $locationType[$locType[1]];
2114 $field['title'] .= " ($lType)";
2117 if (substr($name, 0, 14) === 'state_province') {
2118 if (isset($locType[1]) && is_numeric($locType[1])) {
2120 $aName = "{$lType}-address";
2121 $where = "`$aName`.state_province_id";
2124 $where = "civicrm_address.state_province_id";
2127 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value);
2128 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
2129 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2131 elseif (!empty($field['pseudoconstant'])) {
2132 $this->optionValueQuery(
2133 $name, $op, $value, $grouping,
2134 'CRM_Contact_DAO_Contact',
2140 if ($name == 'gender_id') {
2141 self
::$_openedPanes[ts('Demographics')] = TRUE;
2144 elseif (substr($name, 0, 7) === 'country' ||
substr($name, 0, 6) === 'county') {
2145 $name = (substr($name, 0, 7) === 'country') ?
"country_id" : "county_id";
2146 if (isset($locType[1]) && is_numeric($locType[1])) {
2148 $aName = "{$lType}-address";
2149 $where = "`$aName`.$name";
2152 $where = "civicrm_address.$name";
2155 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value, 'Positive');
2157 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
2158 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2160 elseif ($name === 'world_region') {
2161 $this->optionValueQuery(
2162 $name, $op, $value, $grouping,
2170 elseif ($name === 'is_deceased') {
2171 $this->setQillAndWhere($name, $op, $value, $grouping, $field);
2172 self
::$_openedPanes[ts('Demographics')] = TRUE;
2174 elseif ($name === 'created_date' ||
$name === 'modified_date' ||
$name === 'deceased_date' ||
$name === 'birth_date') {
2175 $appendDateTime = TRUE;
2176 if ($name === 'deceased_date' ||
$name === 'birth_date') {
2177 $appendDateTime = FALSE;
2178 self
::$_openedPanes[ts('Demographics')] = TRUE;
2180 $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime);
2182 elseif ($name === 'contact_id') {
2183 if (is_int($value)) {
2184 $this->_where
[$grouping][] = self
::buildClause($field['where'], $op, $value);
2185 $this->_qill
[$grouping][] = "$field[title] $op $value";
2188 elseif ($name === 'name') {
2189 $value = $strtolower(CRM_Core_DAO
::escapeString($value));
2192 $value = self
::getWildCardedValue($wildcard, $op, $value);
2194 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
2195 $wc = self
::caseImportant($op) ?
"LOWER({$field['where']})" : "{$field['where']}";
2196 $this->_where
[$grouping][] = self
::buildClause($wc, $op, "'$value'");
2197 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2199 elseif ($name === 'current_employer') {
2202 $value = self
::getWildCardedValue($wildcard, $op, $value);
2204 $ceWhereClause = self
::buildClause("contact_a.organization_name", $op,
2207 $ceWhereClause .= " AND contact_a.contact_type = 'Individual'";
2208 $this->_where
[$grouping][] = $ceWhereClause;
2209 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2211 elseif ($name === 'email_greeting') {
2212 $filterCondition = array('greeting_type' => 'email_greeting');
2213 $this->optionValueQuery(
2214 $name, $op, $value, $grouping,
2215 CRM_Core_PseudoConstant
::greeting($filterCondition),
2217 ts('Email Greeting')
2220 elseif ($name === 'postal_greeting') {
2221 $filterCondition = array('greeting_type' => 'postal_greeting');
2222 $this->optionValueQuery(
2223 $name, $op, $value, $grouping,
2224 CRM_Core_PseudoConstant
::greeting($filterCondition),
2226 ts('Postal Greeting')
2229 elseif ($name === 'addressee') {
2230 $filterCondition = array('greeting_type' => 'addressee');
2231 $this->optionValueQuery(
2232 $name, $op, $value, $grouping,
2233 CRM_Core_PseudoConstant
::greeting($filterCondition),
2238 elseif (substr($name, 0, 4) === 'url-') {
2239 $tName = 'civicrm_website';
2240 $this->_whereTables
[$tName] = $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )";
2241 $value = $strtolower(CRM_Core_DAO
::escapeString($value));
2244 $value = self
::getWildCardedValue($wildcard, $op, $value);
2247 $this->_where
[$grouping][] = $d = self
::buildClause('civicrm_website.url', $op, $value);
2248 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2250 elseif ($name === 'contact_is_deleted') {
2251 $this->setQillAndWhere('is_deleted', $op, $value, $grouping, $field);
2253 elseif (!empty($field['where'])) {
2255 if (!empty($field['type'])) {
2256 $type = CRM_Utils_Type
::typeToString($field['type']);
2259 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2261 if (isset($locType[1]) &&
2262 is_numeric($locType[1])
2266 //get the location name
2267 list($tName, $fldName) = self
::getLocationTableName($field['where'], $locType);
2268 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
2269 $fieldName = "LOWER(`$tName`.$fldName)";
2271 // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
2272 $this->_tables
[$tName] = $this->_whereTables
[$tName] = 1;
2276 if ($tableName == 'civicrm_contact') {
2277 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
2278 $fieldName = "LOWER(contact_a.{$fieldName})";
2281 if ($op != 'IN' && !is_numeric($value) && !is_array($value)) {
2282 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
2283 $fieldName = "LOWER({$field['where']})";
2286 $fieldName = "{$field['where']}";
2291 list($qillop, $qillVal) = self
::buildQillForFieldValue(NULL, $field['title'], $value, $op);
2292 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(
2293 1 => $field['title'],
2295 3 => (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) ?
$qillVal : "'$qillVal'"));
2297 if (is_array($value)) {
2298 // traditionally an array being passed has been a fatal error. We can take advantage of this to add support
2299 // for api style operators for functions that hit this point without worrying about regression
2300 // (the previous comments indicated the condition for hitting this point were unknown
2301 // per CRM-14743 we are adding modified_date & created_date operator support
2302 $operations = array_keys($value);
2303 foreach ($operations as $operator) {
2304 if (!in_array($operator, CRM_Core_DAO
::acceptedSQLOperators())) {
2305 //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators
2306 //so this condition will satisfy the search for now
2307 if (strpos($op, 'IN') !== FALSE) {
2308 $value = array($op => $value);
2310 // we don't know when this might happen
2312 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", array(1 => $operator)));
2316 $this->_where
[$grouping][] = CRM_Core_DAO
::createSQLFilter($fieldName, $value, $type);
2319 if (!strpos($op, 'IN')) {
2320 $value = $strtolower($value);
2324 $value = self
::getWildCardedValue($wildcard, $op, $value);
2327 $this->_where
[$grouping][] = self
::buildClause($fieldName, $op, $value, $type);
2331 if ($setTables && isset($field['where'])) {
2332 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2333 if (isset($tableName)) {
2334 $this->_tables
[$tableName] = 1;
2335 $this->_whereTables
[$tableName] = 1;
2347 public static function getLocationTableName(&$where, &$locType) {
2348 if (isset($locType[1]) && is_numeric($locType[1])) {
2349 list($tbName, $fldName) = explode(".", $where);
2351 //get the location name
2352 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
2353 $specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
2354 if (in_array($locType[0], $specialFields)) {
2355 //hack to fix / special handing for phone_ext
2356 if ($locType[0] == 'phone_ext') {
2357 $locType[0] = 'phone';
2359 if (isset($locType[2]) && $locType[2]) {
2360 $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}";
2363 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2366 elseif (in_array($locType[0],
2371 'street_number_suffix',
2373 'supplemental_address_1',
2374 'supplemental_address_2',
2377 'postal_code_suffix',
2383 //fix for search by profile with address fields.
2384 $tName = "{$locationType[$locType[1]]}-address";
2386 elseif (in_array($locType[0],
2394 $tName = "{$locationType[$locType[1]]}-email";
2396 elseif ($locType[0] == 'provider_id') {
2397 $tName = "{$locationType[$locType[1]]}-im";
2399 elseif ($locType[0] == 'openid') {
2400 $tName = "{$locationType[$locType[1]]}-openid";
2403 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2405 $tName = str_replace(' ', '_', $tName);
2406 return array($tName, $fldName);
2408 CRM_Core_Error
::fatal();
2412 * Given a result dao, extract the values and return that array
2414 * @param CRM_Core_DAO $dao
2417 * values for this query
2419 public function store($dao) {
2422 foreach ($this->_element
as $key => $dontCare) {
2423 if (property_exists($dao, $key)) {
2424 if (strpos($key, '-') !== FALSE) {
2425 $values = explode('-', $key);
2426 $lastElement = array_pop($values);
2428 $cnt = count($values);
2430 foreach ($values as $v) {
2431 if (!array_key_exists($v, $current)) {
2432 $current[$v] = array();
2434 //bad hack for im_provider
2435 if ($lastElement == 'provider_id') {
2436 if ($count < $cnt) {
2437 $current = &$current[$v];
2440 $lastElement = "{$v}_{$lastElement}";
2444 $current = &$current[$v];
2449 $current[$lastElement] = $dao->$key;
2452 $value[$key] = $dao->$key;
2460 * Getter for tables array.
2464 public function tables() {
2465 return $this->_tables
;
2469 * Where tables is sometimes used to create the from clause, but, not reliably, set this AND set tables
2470 * It's unclear the intent - there is a 'simpleFrom' clause which takes whereTables into account & a fromClause which doesn't
2471 * logic may have eroded
2474 public function whereTables() {
2475 return $this->_whereTables
;
2479 * Generate the where clause (used in match contacts and permissions)
2481 * @param array $params
2482 * @param array $fields
2483 * @param array $tables
2484 * @param $whereTables
2485 * @param bool $strict
2489 public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) {
2490 $query = new CRM_Contact_BAO_Query($params, NULL, $fields,
2494 $tables = array_merge($query->tables(), $tables);
2495 $whereTables = array_merge($query->whereTables(), $whereTables);
2497 return $query->_whereClause
;
2501 * Create the from clause.
2503 * @param array $tables
2504 * Tables that need to be included in this from clause.
2505 * if null, return mimimal from clause (i.e. civicrm_contact)
2506 * @param array $inner
2507 * Tables that should be inner-joined.
2508 * @param array $right
2509 * Tables that should be right-joined.
2511 * @param bool $primaryLocation
2517 public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1) {
2519 $from = ' FROM civicrm_contact contact_a';
2520 if (empty($tables)) {
2524 if (!empty($tables['civicrm_worldregion'])) {
2525 $tables = array_merge(array('civicrm_country' => 1), $tables);
2528 if ((!empty($tables['civicrm_state_province']) ||
!empty($tables['civicrm_country']) ||
2529 CRM_Utils_Array
::value('civicrm_county', $tables)
2530 ) && empty($tables['civicrm_address'])
2532 $tables = array_merge(array('civicrm_address' => 1),
2537 // add group_contact and group table is subscription history is present
2538 if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) {
2539 $tables = array_merge(array(
2540 'civicrm_group' => 1,
2541 'civicrm_group_contact' => 1,
2547 // to handle table dependencies of components
2548 CRM_Core_Component
::tableNames($tables);
2549 // to handle table dependencies of hook injected tables
2550 CRM_Contact_BAO_Query_Hook
::singleton()->setTableDependency($tables);
2552 //format the table list according to the weight
2553 $info = CRM_Core_TableHierarchy
::info();
2555 foreach ($tables as $key => $value) {
2557 if (strpos($key, '-') !== FALSE) {
2558 $keyArray = explode('-', $key);
2559 $k = CRM_Utils_Array
::value('civicrm_' . $keyArray[1], $info, 99);
2561 elseif (strpos($key, '_') !== FALSE) {
2562 $keyArray = explode('_', $key);
2563 if (is_numeric(array_pop($keyArray))) {
2564 $k = CRM_Utils_Array
::value(implode('_', $keyArray), $info, 99);
2567 $k = CRM_Utils_Array
::value($key, $info, 99);
2571 $k = CRM_Utils_Array
::value($key, $info, 99);
2573 $tempTable[$k . ".$key"] = $key;
2576 $newTables = array();
2577 foreach ($tempTable as $key) {
2578 $newTables[$key] = $tables[$key];
2581 $tables = $newTables;
2583 foreach ($tables as $name => $value) {
2588 if (!empty($inner[$name])) {
2591 elseif (!empty($right[$name])) {
2599 // if there is already a join statement in value, use value itself
2600 if (strpos($value, 'JOIN')) {
2601 $from .= " $value ";
2604 $from .= " $side JOIN $name ON ( $value ) ";
2609 case 'civicrm_address':
2610 if ($primaryLocation) {
2611 $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )";
2614 //CRM-14263 further handling of address joins further down...
2615 $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id ) ";
2619 case 'civicrm_phone':
2620 $from .= " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) ";
2623 case 'civicrm_email':
2624 $from .= " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) ";
2628 $from .= " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) ";
2632 $from .= " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) ";
2633 $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'";
2634 $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)";
2637 case 'civicrm_openid':
2638 $from .= " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id AND civicrm_openid.is_primary = 1 )";
2641 case 'civicrm_worldregion':
2642 $from .= " $side JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id ";
2643 $from .= " $side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ";
2646 case 'civicrm_location_type':
2647 $from .= " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id ";
2650 case 'civicrm_group':
2651 $from .= " $side JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id ";
2654 case 'civicrm_group_contact':
2655 $from .= " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id ";
2658 case 'civicrm_group_contact_cache':
2659 $from .= " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id ";
2662 case 'civicrm_activity':
2663 case 'civicrm_activity_tag':
2664 case 'activity_type':
2665 case 'activity_status':
2667 case 'civicrm_activity_contact':
2668 case 'source_contact':
2669 case 'activity_priority':
2670 $from .= CRM_Activity_BAO_Query
::from($name, $mode, $side);
2673 case 'civicrm_entity_tag':
2674 $from .= " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND
2675 civicrm_entity_tag.entity_id = contact_a.id ) ";
2678 case 'civicrm_note':
2679 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND
2680 contact_a.id = civicrm_note.entity_id ) ";
2683 case 'civicrm_subscription_history':
2684 $from .= " $side JOIN civicrm_subscription_history
2685 ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id
2686 AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id";
2689 case 'civicrm_relationship':
2690 if (self
::$_relType == 'reciprocal') {
2691 if (self
::$_relationshipTempTable) {
2692 // we have a temptable to join on
2693 $tbl = self
::$_relationshipTempTable;
2694 $from .= " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id";
2697 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)";
2698 $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)";
2701 elseif (self
::$_relType == 'b') {
2702 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )";
2703 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
2706 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )";
2707 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
2712 $from .= " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')";
2713 $from .= " INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)";
2717 $from .= " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id ";
2720 case 'civicrm_grant':
2721 $from .= CRM_Grant_BAO_Query
::from($name, $mode, $side);
2724 case 'civicrm_website':
2725 $from .= " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id ";
2729 $locationTypeName = '';
2730 if (strpos($name, '-address') != 0) {
2731 $locationTypeName = 'address';
2733 elseif (strpos($name, '-phone') != 0) {
2734 $locationTypeName = 'phone';
2736 elseif (strpos($name, '-email') != 0) {
2737 $locationTypeName = 'email';
2739 elseif (strpos($name, '-im') != 0) {
2740 $locationTypeName = 'im';
2742 elseif (strpos($name, '-openid') != 0) {
2743 $locationTypeName = 'openid';
2746 if ($locationTypeName) {
2747 //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
2748 $parts = explode('-', $name);
2749 $locationID = array_search($parts[0], CRM_Core_BAO_Address
::buildOptions('location_type_id', 'get', array('name' => $parts[0])));
2750 $from .= " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
2753 $from .= CRM_Core_Component
::from($name, $mode, $side);
2755 $from .= CRM_Contact_BAO_Query_Hook
::singleton()->buildSearchfrom($name, $mode, $side);
2764 * WHERE / QILL clause for deleted_contacts
2766 * @param array $values
2768 public function deletedContacts($values) {
2769 list($_, $_, $value, $grouping, $_) = $values;
2771 // *prepend* to the relevant grouping as this is quite an important factor
2772 array_unshift($this->_qill
[$grouping], ts('Search in Trash'));
2777 * Where / qill clause for contact_type
2781 public function contactType(&$values) {
2782 list($name, $op, $value, $grouping, $wildcard) = $values;
2784 $subTypes = array();
2787 // account for search builder mapping multiple values
2788 if (!is_array($value)) {
2789 $values = self
::parseSearchBuilderString($value, 'String');
2790 if (is_array($values)) {
2791 $value = array_flip($values);
2795 if (is_array($value)) {
2796 foreach ($value as $k => $v) {
2801 if (strpos($k, CRM_Core_DAO
::VALUE_SEPARATOR
)) {
2802 list($contactType, $subType) = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $k, 2);
2805 if (!empty($subType)) {
2806 $subTypes[$subType] = 1;
2808 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2813 $contactTypeANDSubType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value, 2);
2814 $contactType = $contactTypeANDSubType[0];
2815 $subType = CRM_Utils_Array
::value(1, $contactTypeANDSubType);
2816 if (!empty($subType)) {
2817 $subTypes[$subType] = 1;
2819 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2823 if (!empty($clause)) {
2825 if ($op == 'IN' ||
$op == 'NOT IN') {
2826 $this->_where
[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')';
2829 $type = array_pop($clause);
2830 $this->_where
[$grouping][] = self
::buildClause("contact_a.contact_type", $op, $contactType);
2833 $this->_qill
[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill);
2835 if (!empty($subTypes)) {
2836 $this->includeContactSubTypes($subTypes, $grouping);
2842 * Where / qill clause for contact_sub_type.
2844 * @param array $values
2846 public function contactSubType(&$values) {
2847 list($name, $op, $value, $grouping, $wildcard) = $values;
2848 $this->includeContactSubTypes($value, $grouping, $op);
2856 public function includeContactSubTypes($value, $grouping, $op = 'LIKE') {
2858 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2860 $value = $value[$op];
2864 $alias = "contact_a.contact_sub_type";
2865 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
2867 $op = str_replace('IN', 'LIKE', $op);
2868 $op = str_replace('=', 'LIKE', $op);
2869 $op = str_replace('!', 'NOT ', $op);
2871 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
2872 $this->_where
[$grouping][] = self
::buildClause($alias, $op, $value, 'String');
2874 elseif (is_array($value)) {
2875 foreach ($value as $k => $v) {
2876 $clause[$k] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($v, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
2880 $clause[$value] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($value, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
2883 if (!empty($clause)) {
2884 $this->_where
[$grouping][] = "( " . implode(' OR ', $clause) . " )";
2886 $this->_qill
[$grouping][] = ts('Contact Subtype %1 ', array(1 => $qillOperators[$op])) . implode(' ' . ts('or') . ' ', array_keys($clause));
2890 * Where / qill clause for groups.
2894 public function group($values) {
2895 list($name, $op, $value, $grouping, $wildcard) = $values;
2897 // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first
2898 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2900 $value = $value[$op];
2903 // Replace pseudo operators from search builder
2904 $op = str_replace('EMPTY', 'NULL', $op);
2906 if (strpos($op, 'NULL')) {
2910 if (count($value) > 1) {
2911 if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) {
2912 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", array(1 => $op)));
2914 $this->_useDistinct
= TRUE;
2917 if (isset($value)) {
2918 $value = CRM_Utils_Array
::value($op, $value, $value);
2921 if ($name == 'group_type') {
2922 $value = array_keys($this->getGroupsFromTypeCriteria($value));
2925 $regularGroupIDs = $smartGroupIDs = array();
2926 foreach ((array) $value as $id) {
2927 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
2928 $smartGroupIDs[] = $id;
2931 $regularGroupIDs[] = $id;
2935 $isNotOp = ($op == 'NOT IN' ||
$op == '!=');
2938 $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
2940 is_array($gcsValues[2])
2942 foreach ($gcsValues[2] as $k => $v) {
2944 $statii[] = "'" . CRM_Utils_Type
::escape($k, 'String') . "'";
2949 $statii[] = "'Added'";
2951 $groupClause = array();
2952 if (count($regularGroupIDs) ||
empty($value)) {
2953 $groupIds = implode(',', (array) $regularGroupIDs);
2954 $gcTable = "`civicrm_group_contact-{$groupIds}`";
2955 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
2957 $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")";
2959 $this->_tables
[$gcTable] = $this->_whereTables
[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
2960 if (strpos($op, 'IN') !== FALSE) {
2961 $clause = "{$gcTable}.group_id $op ( $groupIds ) %s ";
2963 elseif ($op == '!=') {
2964 $clause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds %s)";
2967 $clause = "{$gcTable}.group_id $op $groupIds %s ";
2970 // include child groups IDs if any
2971 $childGroupIds = CRM_Contact_BAO_Group
::getChildGroupIds($regularGroupIDs);
2973 if (count($childGroupIds)) {
2974 $gcTable = ($op == '!=') ?
'cgc' : $gcTable;
2975 $childClause = " OR {$gcTable}.group_id IN (" . implode(',', $childGroupIds) . ") ";
2977 $groupClause[] = sprintf($clause, $childClause);
2980 //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table
2981 if (count($smartGroupIDs)) {
2982 $groupClause[] = " ( " . $this->addGroupContactCache($smartGroupIDs, NULL, "contact_a", $op) . " ) ";
2985 $and = ($op == 'IS NULL') ?
' AND ' : ' OR ';
2986 $this->_where
[$grouping][] = implode($and, $groupClause);
2988 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
2989 $this->_qill
[$grouping][] = ts("Group(s) %1 %2", array(1 => $qillop, 2 => $qillVal));
2990 if (strpos($op, 'NULL') === FALSE) {
2991 $this->_qill
[$grouping][] = ts("Group Status %1", array(1 => implode(' ' . ts('or') . ' ', $statii)));
2996 * Function translates selection of group type into a list of groups.
3001 public function getGroupsFromTypeCriteria($value) {
3002 $groupIds = array();
3003 foreach ((array) $value as $groupTypeValue) {
3004 $groupList = CRM_Core_PseudoConstant
::group($groupTypeValue);
3005 $groupIds = ($groupIds +
$groupList);
3011 * @param array $groups
3012 * @param string $tableAlias
3013 * @param string $joinTable
3016 * @return null|string
3018 public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a", $op) {
3019 $isNullOp = (strpos($op, 'NULL') !== FALSE);
3020 $groupsIds = $groups;
3021 if (!$isNullOp && !$groups) {
3024 elseif (strpos($op, 'IN') !== FALSE) {
3025 $groups = array($op => $groups);
3027 elseif (is_array($groups) && count($groups)) {
3028 $groups = array('IN' => $groups);
3031 // Find all the groups that are part of a saved search.
3032 $smartGroupClause = self
::buildClause("id", $op, $groups, 'Int');
3034 SELECT id, cache_date, saved_search_id, children
3036 WHERE $smartGroupClause
3037 AND ( saved_search_id != 0
3038 OR saved_search_id IS NOT NULL
3039 OR children IS NOT NULL )
3042 $group = CRM_Core_DAO
::executeQuery($sql);
3044 while ($group->fetch()) {
3045 $this->_useDistinct
= TRUE;
3046 if (!$this->_smartGroupCache ||
$group->cache_date
== NULL) {
3047 CRM_Contact_BAO_GroupContactCache
::load($group);
3050 if ($group->N
== 0) {
3055 $tableAlias = "`civicrm_group_contact_cache_";
3056 $tableAlias .= ($isNullOp) ?
"a`" : implode(',', (array) $groupsIds) . "`";
3059 $this->_tables
[$tableAlias] = $this->_whereTables
[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.id = {$tableAlias}.contact_id ";
3060 return self
::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
3064 * Where / qill clause for cms users
3068 public function ufUser(&$values) {
3069 list($name, $op, $value, $grouping, $wildcard) = $values;
3072 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3074 $this->_qill
[$grouping][] = ts('CMS User');
3076 elseif ($value == 0) {
3077 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3079 $this->_where
[$grouping][] = " civicrm_uf_match.contact_id IS NULL";
3080 $this->_qill
[$grouping][] = ts('Not a CMS User');
3085 * All tag search specific.
3087 * @param array $values
3089 public function tagSearch(&$values) {
3090 list($name, $op, $value, $grouping, $wildcard) = $values;
3093 $value = "%{$value}%";
3095 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3096 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3098 $etTable = "`civicrm_entity_tag-" . $value . "`";
3099 $tTable = "`civicrm_tag-" . $value . "`";
3101 if ($useAllTagTypes[2]) {
3102 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3103 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id)
3104 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )";
3106 // search tag in cases
3107 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3108 $tCaseTable = "`civicrm_case_tag-" . $value . "`";
3109 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3110 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3111 LEFT JOIN civicrm_case
3112 ON (civicrm_case_contact.case_id = civicrm_case.id
3113 AND civicrm_case.is_deleted = 0 )
3114 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id )
3115 LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )";
3116 // search tag in activities
3117 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3118 $tActTable = "`civicrm_act_tag-" . $value . "`";
3119 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3120 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3122 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3123 = " LEFT JOIN civicrm_activity_contact
3124 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3125 LEFT JOIN civicrm_activity
3126 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3127 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3128 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id )
3129 LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )";
3131 $this->_where
[$grouping][] = "({$tTable}.name $op '" . $value . "' OR {$tCaseTable}.name $op '" . $value . "' OR {$tActTable}.name $op '" . $value . "')";
3132 $this->_qill
[$grouping][] = ts('Tag %1 %2', array(1 => $tagTypesText[2], 2 => $op)) . ' ' . $value;
3135 $etTable = "`civicrm_entity_tag-" . $value . "`";
3136 $tTable = "`civicrm_tag-" . $value . "`";
3137 $this->_tables
[$etTable] = $this->_whereTables
[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND
3138 {$etTable}.entity_table = 'civicrm_contact' )
3139 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) ";
3141 $this->_where
[$grouping][] = self
::buildClause("{$tTable}.name", $op, $value, 'String');
3142 $this->_qill
[$grouping][] = ts('Tagged %1', array(1 => $op)) . ' ' . $value;
3147 * Where / qill clause for tag.
3149 * @param array $values
3151 public function tag(&$values) {
3152 list($name, $op, $value, $grouping, $wildcard) = $values;
3154 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, array('onlyActive' => FALSE));
3155 // API/Search Builder format array(operator => array(values))
3156 if (is_array($value)) {
3157 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3159 $value = $value[$op];
3161 if (count($value) > 1) {
3162 $this->_useDistinct
= TRUE;
3164 $value = implode(',', (array) $value);
3167 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3168 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3170 $etTable = "`civicrm_entity_tag-" . $value . "`";
3172 if ($useAllTagTypes[2]) {
3173 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3174 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3176 // search tag in cases
3177 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3178 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3179 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3181 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3182 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3183 LEFT JOIN civicrm_case
3184 ON (civicrm_case_contact.case_id = civicrm_case.id
3185 AND civicrm_case.is_deleted = 0 )
3186 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) ";
3187 // search tag in activities
3188 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3189 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3190 = " LEFT JOIN civicrm_activity_contact
3191 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3192 LEFT JOIN civicrm_activity
3193 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3194 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3195 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) ";
3198 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3199 $this->_where
[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)";
3202 $this->_where
[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
3206 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3207 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3210 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3211 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3212 $op = str_replace('EMPTY', 'NULL', $op);
3213 $this->_where
[$grouping][] = "{$etTable}.tag_id $op";
3215 // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag
3216 elseif ($op == '!=') {
3217 $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') . ")";
3219 elseif ($op == '=' ||
strstr($op, 'IN')) {
3220 $op = ($op == '=') ?
'IN' : $op;
3221 $this->_where
[$grouping][] = "{$etTable}.tag_id $op ( $value )";
3224 $this->_qill
[$grouping][] = ts('Tagged %1 %2', array(1 => $qillop, 2 => $qillVal));
3228 * Where/qill clause for notes
3230 * @param array $values
3232 public function notes(&$values) {
3233 list($name, $op, $value, $grouping, $wildcard) = $values;
3235 $noteOptionValues = $this->getWhereValues('note_option', $grouping);
3236 $noteOption = CRM_Utils_Array
::value('2', $noteOptionValues, '6');
3237 $noteOption = ($name == 'note_body') ?
2 : (($name == 'note_subject') ?
3 : $noteOption);
3239 $this->_useDistinct
= TRUE;
3241 $this->_tables
['civicrm_note'] = $this->_whereTables
['civicrm_note']
3242 = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) ";
3244 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
3246 $value = $strtolower(CRM_Core_DAO
::escapeString($n));
3248 if (strpos($value, '%') === FALSE) {
3249 $value = "%$value%";
3253 elseif ($op == 'IS NULL' ||
$op == 'IS NOT NULL') {
3259 if ($noteOption %
2 == 0) {
3260 $clauses[] = self
::buildClause('civicrm_note.note', $op, $value, 'String');
3261 $label = ts('Note: Body Only');
3263 if ($noteOption %
3 == 0) {
3264 $clauses[] = self
::buildClause('civicrm_note.subject', $op, $value, 'String');
3265 $label = $label ?
ts('Note: Body and Subject') : ts('Note: Subject Only');
3267 $this->_where
[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
3268 list($qillOp, $qillVal) = self
::buildQillForFieldValue(NULL, $name, $n, $op);
3269 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillOp, 3 => $qillVal));
3273 * @param string $name
3279 public function nameNullOrEmptyOp($name, $op, $grouping) {
3283 $this->_where
[$grouping][] = "contact_a.$name $op";
3284 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3288 $this->_where
[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')";
3289 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3292 case 'IS NOT EMPTY':
3293 $this->_where
[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')";
3294 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3303 * Where / qill clause for sort_name
3305 * @param array $values
3307 public function sortName(&$values) {
3308 list($fieldName, $op, $value, $grouping, $wildcard) = $values;
3310 // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY
3311 if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) {
3315 $input = $value = trim($value);
3317 if (!strlen($value)) {
3321 $config = CRM_Core_Config
::singleton();
3325 //By default, $sub elements should be joined together with OR statements (don't change this variable).
3328 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
3330 $firstChar = substr($value, 0, 1);
3331 $lastChar = substr($value, -1, 1);
3332 $quotes = array("'", '"');
3333 // If string is quoted, strip quotes and otherwise don't alter it
3334 if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) {
3335 $value = trim($value, implode('', $quotes));
3337 // Replace spaces with wildcards for a LIKE operation
3338 // UNLESS string contains a comma (this exception is a tiny bit questionable)
3339 elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) {
3340 $value = str_replace(' ', '%', $value);
3342 $value = $strtolower(CRM_Core_DAO
::escapeString(trim($value)));
3343 if (strlen($value)) {
3344 $fieldsub = array();
3345 $value = "'" . self
::getWildCardedValue($wildcard, $op, $value) . "'";
3346 if ($fieldName == 'sort_name') {
3347 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
3348 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.sort_name)" : "contact_a.sort_name";
3351 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
3352 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.display_name)" : "contact_a.display_name";
3354 $fieldsub[] = " ( $wc $op $value )";
3355 if ($config->includeNickNameInName
) {
3356 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
3357 $wc = self
::caseImportant($op) ?
"LOWER(contact_a.nick_name)" : "contact_a.nick_name";
3358 $fieldsub[] = " ( $wc $op $value )";
3360 if ($config->includeEmailInName
) {
3361 $fieldsub[] = " ( civicrm_email.email $op $value ) ";
3363 $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
3366 $sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
3368 $this->_where
[$grouping][] = $sub;
3369 if ($config->includeEmailInName
) {
3370 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3371 $this->_qill
[$grouping][] = ts('Name or Email') . " $op - '$input'";
3374 $this->_qill
[$grouping][] = ts('Name') . " $op - '$input'";
3379 * Where/qill clause for greeting fields.
3381 * @param array $values
3383 public function greetings(&$values) {
3384 list($name, $op, $value, $grouping, $wildcard) = $values;
3385 $name .= '_display';
3387 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
3388 $this->_qill
[$grouping][] = ts('Greeting %1 %2', array(1 => $qillop, 2 => $qillVal));
3389 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value, 'String');
3393 * Where / qill clause for email
3395 * @param array $values
3396 * @param string $apiEntity
3398 protected function email(&$values, $apiEntity) {
3399 list($name, $op, $value, $grouping, $wildcard) = $values;
3400 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3402 // CRM-18147: for Contact's GET API, email fieldname got appended with its entity as in {$apiEntiy}_{$name}
3403 // so following code is use build whereClause for contact's primart email id
3404 if (!empty($apiEntity)) {
3405 $dataType = 'String';
3406 if ($name == 'email_id') {
3407 $dataType = 'Integer';
3411 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.is_primary', '=', 1, 'Integer');
3412 $this->_where
[$grouping][] = self
::buildClause("civicrm_email.$name", $op, $value, $dataType);
3416 $n = strtolower(trim($value));
3418 if (substr($n, 0, 1) == '"' &&
3419 substr($n, -1, 1) == '"'
3421 $n = substr($n, 1, -1);
3422 $value = CRM_Core_DAO
::escapeString($n);
3426 $value = self
::getWildCardedValue($wildcard, $op, $n);
3428 $this->_qill
[$grouping][] = ts('Email') . " $op '$n'";
3429 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, $value, 'String');
3432 $this->_qill
[$grouping][] = ts('Email') . " $op ";
3433 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, NULL, 'String');
3438 * Where / qill clause for phone number
3440 * @param array $values
3442 public function phone_numeric(&$values) {
3443 list($name, $op, $value, $grouping, $wildcard) = $values;
3444 // Strip non-numeric characters; allow wildcards
3445 $number = preg_replace('/[^\d%]/', '', $value);
3447 if (strpos($number, '%') === FALSE) {
3448 $number = "%$number%";
3451 $this->_qill
[$grouping][] = ts('Phone number contains') . " $number";
3452 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String');
3453 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3458 * Where / qill clause for phone type/location
3460 * @param array $values
3462 public function phone_option_group($values) {
3463 list($name, $op, $value, $grouping, $wildcard) = $values;
3464 $option = ($name == 'phone_phone_type_id' ?
'phone_type_id' : 'location_type_id');
3465 $options = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', $option);
3466 $optionName = $options[$value];
3467 $this->_qill
[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ?
ts('type') : ('location')) . " $op $optionName";
3468 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer');
3469 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3473 * Where / qill clause for street_address.
3475 * @param array $values
3477 public function street_address(&$values) {
3478 list($name, $op, $value, $grouping, $wildcard) = $values;
3487 $value = strtolower($n);
3488 if (strpos($value, '%') === FALSE) {
3489 // only add wild card if not there
3490 $value = "%{$value}%";
3493 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
3494 $this->_where
[$grouping][] = self
::buildClause('LOWER(civicrm_address.street_address)', $op, $value, 'String');
3495 $this->_qill
[$grouping][] = ts('Street') . " $op '$n'";
3498 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_address', $op, NULL, 'String');
3499 $this->_qill
[$grouping][] = ts('Street') . " $op ";
3502 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3506 * Where / qill clause for street_unit.
3508 * @param array $values
3510 public function street_number(&$values) {
3511 list($name, $op, $value, $grouping, $wildcard) = $values;
3519 if (strtolower($n) == 'odd') {
3520 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )";
3521 $this->_qill
[$grouping][] = ts('Street Number is odd');
3523 elseif (strtolower($n) == 'even') {
3524 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )";
3525 $this->_qill
[$grouping][] = ts('Street Number is even');
3528 $value = strtolower($n);
3530 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
3531 $this->_where
[$grouping][] = self
::buildClause('LOWER(civicrm_address.street_number)', $op, $value, 'String');
3532 $this->_qill
[$grouping][] = ts('Street Number') . " $op '$n'";
3535 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3539 * Where / qill clause for sorting by character.
3541 * @param array $values
3543 public function sortByCharacter(&$values) {
3544 list($name, $op, $value, $grouping, $wildcard) = $values;
3546 $name = trim($value);
3547 $cond = " contact_a.sort_name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($name)) . "%'";
3548 $this->_where
[$grouping][] = $cond;
3549 $this->_qill
[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', array(1 => $name));
3553 * Where / qill clause for including contact ids.
3555 public function includeContactIDs() {
3556 if (!$this->_includeContactIds ||
empty($this->_params
)) {
3560 $contactIds = array();
3561 foreach ($this->_params
as $id => $values) {
3562 if (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
3563 $contactIds[] = substr($values[0], CRM_Core_Form
::CB_PREFIX_LEN
);
3566 CRM_Utils_Type
::validateAll($contactIds, 'Positive');
3567 if (!empty($contactIds)) {
3568 $this->_where
[0][] = " ( contact_a.id IN (" . implode(',', $contactIds) . " ) ) ";
3573 * Where / qill clause for postal code.
3575 * @param array $values
3577 public function postalCode(&$values) {
3578 // skip if the fields dont have anything to do with postal_code
3579 if (empty($this->_fields
['postal_code'])) {
3583 list($name, $op, $value, $grouping, $wildcard) = $values;
3585 // Handle numeric postal code range searches properly by casting the column as numeric
3586 if (is_numeric($value)) {
3587 $field = "IF (civicrm_address.postal_code REGEXP '^[0-9]+$', CAST(civicrm_address.postal_code AS UNSIGNED), 0)";
3588 $val = CRM_Utils_Type
::escape($value, 'Integer');
3591 $field = 'civicrm_address.postal_code';
3592 // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string.
3593 if (!is_array($value)) {
3594 $val = CRM_Utils_Type
::escape($value, 'String');
3597 // Do we need to escape values here? I would expect buildClause does.
3602 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3604 if ($name == 'postal_code') {
3605 $this->_where
[$grouping][] = self
::buildClause($field, $op, $val, 'String');
3606 $this->_qill
[$grouping][] = ts('Postal code') . " {$op} {$value}";
3608 elseif ($name == 'postal_code_low') {
3609 $this->_where
[$grouping][] = " ( $field >= '$val' ) ";
3610 $this->_qill
[$grouping][] = ts('Postal code greater than or equal to \'%1\'', array(1 => $value));
3612 elseif ($name == 'postal_code_high') {
3613 $this->_where
[$grouping][] = " ( $field <= '$val' ) ";
3614 $this->_qill
[$grouping][] = ts('Postal code less than or equal to \'%1\'', array(1 => $value));
3619 * Where / qill clause for location type.
3621 * @param array $values
3622 * @param null $status
3626 public function locationType(&$values, $status = NULL) {
3627 list($name, $op, $value, $grouping, $wildcard) = $values;
3629 if (is_array($value)) {
3630 $this->_where
[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')';
3631 $this->_tables
['civicrm_address'] = 1;
3632 $this->_whereTables
['civicrm_address'] = 1;
3634 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
3636 foreach ($value as $id) {
3637 $names[] = $locationType[$id];
3640 $this->_primaryLocation
= FALSE;
3643 $this->_qill
[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3646 return implode(' ' . ts('or') . ' ', $names);
3653 * @param bool $fromStateProvince
3655 * @return array|NULL
3657 public function country(&$values, $fromStateProvince = TRUE) {
3658 list($name, $op, $value, $grouping, $wildcard) = $values;
3660 if (!$fromStateProvince) {
3661 $stateValues = $this->getWhereValues('state_province', $grouping);
3662 if (!empty($stateValues)) {
3663 // return back to caller if there are state province values
3664 // since that handles this case
3669 $countryClause = $countryQill = NULL;
3670 if ($values && !empty($value)) {
3671 $this->_tables
['civicrm_address'] = 1;
3672 $this->_whereTables
['civicrm_address'] = 1;
3674 $countryClause = self
::buildClause('civicrm_address.country_id', $op, $value, 'Positive');
3675 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, 'country_id', $value, $op);
3676 $countryQill = ts("%1 %2 %3", array(1 => 'Country', 2 => $qillop, 3 => $qillVal));
3678 if (!$fromStateProvince) {
3679 $this->_where
[$grouping][] = $countryClause;
3680 $this->_qill
[$grouping][] = $countryQill;
3684 if ($fromStateProvince) {
3685 if (!empty($countryClause)) {
3688 " ...AND... " . $countryQill,
3692 return array(NULL, NULL);
3698 * Where / qill clause for county (if present).
3700 * @param array $values
3701 * @param null $status
3705 public function county(&$values, $status = NULL) {
3706 list($name, $op, $value, $grouping, $wildcard) = $values;
3708 if (!is_array($value)) {
3709 // force the county to be an array
3710 $value = array($value);
3713 // check if the values are ids OR names of the counties
3714 $inputFormat = 'id';
3715 foreach ($value as $v) {
3716 if (!is_numeric($v)) {
3717 $inputFormat = 'name';
3725 elseif ($op == '!=') {
3729 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3730 $op = str_replace('EMPTY', 'NULL', $op);
3733 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3734 $clause = "civicrm_address.county_id $op";
3736 elseif ($inputFormat == 'id') {
3737 $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')';
3739 $county = CRM_Core_PseudoConstant
::county();
3740 foreach ($value as $id) {
3741 $names[] = CRM_Utils_Array
::value($id, $county);
3745 $inputClause = array();
3746 $county = CRM_Core_PseudoConstant
::county();
3747 foreach ($value as $name) {
3748 $name = trim($name);
3749 $inputClause[] = CRM_Utils_Array
::key($name, $county);
3751 $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')';
3754 $this->_tables
['civicrm_address'] = 1;
3755 $this->_whereTables
['civicrm_address'] = 1;
3757 $this->_where
[$grouping][] = $clause;
3759 $this->_qill
[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3762 return implode(' ' . ts('or') . ' ', $names);
3767 * Where / qill clause for state/province AND country (if present).
3769 * @param array $values
3770 * @param null $status
3774 public function stateProvince(&$values, $status = NULL) {
3775 list($name, $op, $value, $grouping, $wildcard) = $values;
3777 $stateClause = self
::buildClause('civicrm_address.state_province_id', $op, $value, 'Positive');
3778 $this->_tables
['civicrm_address'] = 1;
3779 $this->_whereTables
['civicrm_address'] = 1;
3781 $countryValues = $this->getWhereValues('country', $grouping);
3782 list($countryClause, $countryQill) = $this->country($countryValues, TRUE);
3783 if ($countryClause) {
3784 $clause = "( $stateClause AND $countryClause )";
3787 $clause = $stateClause;
3790 $this->_where
[$grouping][] = $clause;
3791 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
3793 $this->_qill
[$grouping][] = ts("State/Province %1 %2 %3", array(1 => $qillop, 2 => $qillVal, 3 => $countryQill));
3796 return implode(' ' . ts('or') . ' ', $qillVal) . $countryQill;
3801 * Where / qill clause for change log.
3803 * @param array $values
3805 public function changeLog(&$values) {
3806 list($name, $op, $value, $grouping, $wildcard) = $values;
3808 $targetName = $this->getWhereValues('changed_by', $grouping);
3813 $name = trim($targetName[2]);
3814 $name = strtolower(CRM_Core_DAO
::escapeString($name));
3815 $name = $targetName[4] ?
"%$name%" : $name;
3816 $this->_where
[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
3817 $this->_tables
['civicrm_log'] = $this->_whereTables
['civicrm_log'] = 1;
3818 $this->_qill
[$grouping][] = ts('Modified By') . " $name";
3824 public function modifiedDates($values) {
3825 $this->_useDistinct
= TRUE;
3827 // CRM-11281, default to added date if not set
3828 $fieldTitle = ts('Added Date');
3829 $fieldName = 'created_date';
3830 foreach (array_keys($this->_params
) as $id) {
3831 if ($this->_params
[$id][0] == 'log_date') {
3832 if ($this->_params
[$id][2] == 2) {
3833 $fieldTitle = ts('Modified Date');
3834 $fieldName = 'modified_date';
3839 $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle);
3841 self
::$_openedPanes[ts('Change Log')] = TRUE;
3847 public function demographics(&$values) {
3848 list($name, $op, $value, $grouping, $wildcard) = $values;
3850 if (($name == 'age_low') ||
($name == 'age_high')) {
3851 $this->ageRangeQueryBuilder($values,
3852 'contact_a', 'age', 'birth_date', ts('Age')
3855 elseif (($name == 'birth_date_low') ||
($name == 'birth_date_high')) {
3857 $this->dateQueryBuilder($values,
3858 'contact_a', 'birth_date', 'birth_date', ts('Birth Date')
3861 elseif (($name == 'deceased_date_low') ||
($name == 'deceased_date_high')) {
3863 $this->dateQueryBuilder($values,
3864 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date')
3868 self
::$_openedPanes[ts('Demographics')] = TRUE;
3874 public function privacy(&$values) {
3875 list($name, $op, $value, $grouping, $wildcard) = $values;
3876 //fixed for profile search listing CRM-4633
3877 if (strpbrk($value, "[")) {
3878 $value = "'{$value}'";
3880 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
3883 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
3885 $field = CRM_Utils_Array
::value($name, $this->_fields
);
3886 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
3887 $title = $field ?
$field['title'] : $name;
3888 $this->_qill
[$grouping][] = "$title $op $value";
3894 public function privacyOptions($values) {
3895 list($name, $op, $value, $grouping, $wildcard) = $values;
3897 if (empty($value) ||
!is_array($value)) {
3901 // get the operator and toggle values
3902 $opValues = $this->getWhereValues('privacy_operator', $grouping);
3905 strtolower($opValues[2] == 'AND')
3910 $toggleValues = $this->getWhereValues('privacy_toggle', $grouping);
3912 if ($toggleValues &&
3913 $toggleValues[2] == 2
3920 foreach ($value as $dontCare => $pOption) {
3921 $clauses[] = " ( contact_a.{$pOption} = 1 ) ";
3922 $field = CRM_Utils_Array
::value($pOption, $this->_fields
);
3923 $title = $field ?
$field['title'] : $pOption;
3924 $qill[] = " $title = 1 ";
3927 $this->_where
[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )';
3928 $this->_qill
[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )';
3934 public function preferredCommunication(&$values) {
3935 list($name, $op, $value, $grouping, $wildcard) = $values;
3937 if (!is_array($value)) {
3938 $value = str_replace(array('(', ')'), '', explode(",", $value));
3940 elseif (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3942 $value = $value[$op];
3944 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
3946 if (self
::caseImportant($op)) {
3947 $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value);
3948 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
3949 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
3952 $this->_where
[$grouping][] = self
::buildClause("contact_a.preferred_communication_method", $op, $value);
3953 $this->_qill
[$grouping][] = ts('Preferred Communication Method %1 %2', array(1 => $qillop, 2 => $qillVal));
3957 * Where / qill clause for relationship.
3959 * @param array $values
3961 public function relationship(&$values) {
3962 list($name, $op, $value, $grouping, $wildcard) = $values;
3963 if ($this->_relationshipValuesAdded
) {
3966 // also get values array for relation_target_name
3967 // for relationship search we always do wildcard
3968 $relationType = $this->getWhereValues('relation_type_id', $grouping);
3969 $targetName = $this->getWhereValues('relation_target_name', $grouping);
3970 $relStatus = $this->getWhereValues('relation_status', $grouping);
3971 $relPermission = $this->getWhereValues('relation_permission', $grouping);
3972 $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
3974 $nameClause = $name = NULL;
3976 $name = trim($targetName[2]);
3977 if (substr($name, 0, 1) == '"' &&
3978 substr($name, -1, 1) == '"'
3980 $name = substr($name, 1, -1);
3981 $name = strtolower(CRM_Core_DAO
::escapeString($name));
3982 $nameClause = "= '$name'";
3985 $name = strtolower(CRM_Core_DAO
::escapeString($name));
3986 $nameClause = "LIKE '%{$name}%'";
3990 $rTypeValues = array();
3991 if (!empty($relationType)) {
3992 $rel = explode('_', $relationType[2]);
3993 self
::$_relType = $rel[1];
3994 $params = array('id' => $rel[0]);
3995 $rType = CRM_Contact_BAO_RelationshipType
::retrieve($params, $rTypeValues);
3997 if (!empty($rTypeValues) && $rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
3998 // if we don't know which end of the relationship we are dealing with we'll create a temp table
3999 //@todo unless we are dealing with a target group
4000 self
::$_relType = 'reciprocal';
4002 // if we are creating a temp table we build our own where for the relationship table
4003 $relationshipTempTable = NULL;
4004 if (self
::$_relType == 'reciprocal' && empty($targetGroup)) {
4006 self
::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO
::createTempTableName('civicrm_rel');
4008 $where[$grouping][] = " sort_name $nameClause ";
4012 $where = &$this->_where
;
4014 $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )";
4018 $allRelationshipType = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
4020 if ($nameClause ||
!$targetGroup) {
4021 if (!empty($relationType)) {
4022 $this->_qill
[$grouping][] = $allRelationshipType[$relationType[2]] . " $name";
4025 $this->_qill
[$grouping][] = $name;
4029 //check to see if the target contact is in specified group
4031 //add contacts from static groups
4032 $this->_tables
['civicrm_relationship_group_contact'] = $this->_whereTables
['civicrm_relationship_group_contact']
4033 = " 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'";
4034 $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" .
4035 implode(",", $targetGroup[2]) . ") ) ";
4037 //add contacts from saved searches
4038 $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", "contact_b", $op);
4040 //set the group where clause
4042 $groupWhere[] = "( " . $ssWhere . " )";
4044 $this->_where
[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
4046 //Get the names of the target groups for the qill
4047 $groupNames = CRM_Core_PseudoConstant
::group();
4048 $qillNames = array();
4049 foreach ($targetGroup[2] as $groupId) {
4050 if (array_key_exists($groupId, $groupNames)) {
4051 $qillNames[] = $groupNames[$groupId];
4054 if (!empty($relationType)) {
4055 $this->_qill
[$grouping][] = $allRelationshipType[$relationType[2]] . " ( " . implode(", ", $qillNames) . " )";
4058 $this->_qill
[$grouping][] = implode(", ", $qillNames);
4062 // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
4063 $today = date('Ymd');
4064 //check for active, inactive and all relation status
4065 if ($relStatus[2] == 0) {
4066 $where[$grouping][] = "(
4067 civicrm_relationship.is_active = 1 AND
4068 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
4069 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
4071 $this->_qill
[$grouping][] = ts('Relationship - Active and Current');
4073 elseif ($relStatus[2] == 1) {
4074 $where[$grouping][] = "(
4075 civicrm_relationship.is_active = 0 OR
4076 civicrm_relationship.end_date < {$today} OR
4077 civicrm_relationship.start_date > {$today}
4079 $this->_qill
[$grouping][] = ts('Relationship - Inactive or not Current');
4083 if (in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
)) {
4086 $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
4088 //check for permissioned, non-permissioned and all permissioned relations
4089 if ($relPermission[2] == 1) {
4090 $where[$grouping][] = "(
4091 civicrm_relationship.is_permission_a_b = 1
4093 $this->_qill
[$grouping][] = ts('Relationship - Permissioned');
4095 elseif ($relPermission[2] == 2) {
4096 //non-allowed permission relationship.
4097 $where[$grouping][] = "(
4098 civicrm_relationship.is_permission_a_b = 0
4100 $this->_qill
[$grouping][] = ts('Relationship - Non-permissioned');
4103 $this->addRelationshipDateClauses($grouping, $where);
4104 if (!empty($relationType) && !empty($rType) && isset($rType->id
)) {
4105 $where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id
;
4107 $this->_tables
['civicrm_relationship'] = $this->_whereTables
['civicrm_relationship'] = 1;
4108 $this->_useDistinct
= TRUE;
4109 $this->_relationshipValuesAdded
= TRUE;
4110 // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
4112 // @todo creating a temp table could be expanded to group filter
4113 // as even creating a temp table of all relationships is much much more efficient than
4114 // an OR in the join
4115 if ($relationshipTempTable) {
4117 if (!empty($where[$grouping])) {
4118 $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
4119 $whereClause = str_replace('contact_b', 'c', $whereClause);
4122 CREATE TEMPORARY TABLE {$relationshipTempTable}
4123 (SELECT contact_id_b as contact_id, civicrm_relationship.id
4124 FROM civicrm_relationship
4125 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
4128 (SELECT contact_id_a as contact_id, civicrm_relationship.id
4129 FROM civicrm_relationship
4130 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id
4133 CRM_Core_DAO
::executeQuery($sql);
4139 * Add start & end date criteria in
4140 * @param string $grouping
4141 * @param array $where
4142 * = array to add where clauses to, in case you are generating a temp table.
4143 * not the main query.
4145 public function addRelationshipDateClauses($grouping, &$where) {
4146 $dateValues = array();
4152 foreach ($dateTypes as $dateField) {
4153 $dateValueLow = $this->getWhereValues('relation_' . $dateField . '_low', $grouping);
4154 $dateValueHigh = $this->getWhereValues('relation_' . $dateField . '_high', $grouping);
4155 if (!empty($dateValueLow)) {
4156 $date = date('Ymd', strtotime($dateValueLow[2]));
4157 $where[$grouping][] = "civicrm_relationship.$dateField >= $date";
4158 $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);
4160 if (!empty($dateValueHigh)) {
4161 $date = date('Ymd', strtotime($dateValueHigh[2]));
4162 $where[$grouping][] = "civicrm_relationship.$dateField <= $date";
4163 $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);
4169 * Default set of return properties.
4174 * derault return properties
4176 public static function &defaultReturnProperties($mode = 1) {
4177 if (!isset(self
::$_defaultReturnProperties)) {
4178 self
::$_defaultReturnProperties = array();
4181 if (!isset(self
::$_defaultReturnProperties[$mode])) {
4182 // add activity return properties
4183 if ($mode & CRM_Contact_BAO_Query
::MODE_ACTIVITY
) {
4184 self
::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query
::defaultReturnProperties($mode, FALSE);
4187 self
::$_defaultReturnProperties[$mode] = CRM_Core_Component
::defaultReturnProperties($mode, FALSE);
4190 if (empty(self
::$_defaultReturnProperties[$mode])) {
4191 self
::$_defaultReturnProperties[$mode] = array(
4194 'legal_identifier' => 1,
4195 'external_identifier' => 1,
4196 'contact_type' => 1,
4197 'contact_sub_type' => 1,
4199 'display_name' => 1,
4200 'preferred_mail_format' => 1,
4207 'formal_title' => 1,
4208 'communication_style_id' => 1,
4211 'street_address' => 1,
4212 'supplemental_address_1' => 1,
4213 'supplemental_address_2' => 1,
4216 'postal_code_suffix' => 1,
4217 'state_province' => 1,
4219 'world_region' => 1,
4226 'household_name' => 1,
4227 'organization_name' => 1,
4228 'deceased_date' => 1,
4233 'current_employer' => 1,
4234 // FIXME: should we use defaultHierReturnProperties() for the below?
4235 'do_not_email' => 1,
4238 'do_not_phone' => 1,
4239 'do_not_trade' => 1,
4241 'contact_is_deleted' => 1,
4242 'preferred_communication_method' => 1,
4243 'preferred_language' => 1,
4247 return self
::$_defaultReturnProperties[$mode];
4251 * Get primary condition for a sql clause.
4255 * @return string|NULL
4257 public static function getPrimaryCondition($value) {
4258 if (is_numeric($value)) {
4259 $value = (int ) $value;
4260 return ($value == 1) ?
'is_primary = 1' : 'is_primary = 0';
4266 * Wrapper for a simple search query.
4268 * @param array $params
4269 * @param array $returnProperties
4270 * @param bool $count
4274 public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) {
4275 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
4276 list($select, $from, $where, $having) = $query->query();
4277 $groupBy = ($query->_useGroupBy
) ?
'GROUP BY contact_a.id' : '';
4279 $query = "$select $from $where $groupBy $having";
4284 * These are stub comments as this function needs more explanation - particularly in terms of how it
4285 * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.
4287 * This function was originally written as a wrapper for the api query but is called from multiple places
4288 * in the core code directly so the name is misleading. This function does not use the searchQuery function
4289 * but it is unclear as to whehter that is historical or there is a reason
4290 * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function
4292 * @param array $params
4293 * @param array $returnProperties
4294 * @param null $fields
4295 * @param string $sort
4296 * @param int $offset
4297 * @param int $row_count
4298 * @param bool $smartGroupCache
4299 * ?? update smart group cache?.
4300 * @param bool $count
4301 * Return count obnly.
4302 * @param bool $skipPermissions
4303 * Should permissions be ignored or should the logged in user's permissions be applied.
4305 * This basically correlates to the component.
4306 * @param string $apiEntity
4307 * The api entity being called.
4308 * This sort-of duplicates $mode in a confusing way. Probably not by design.
4312 public static function apiQuery(
4314 $returnProperties = NULL,
4319 $smartGroupCache = TRUE,
4321 $skipPermissions = TRUE,
4322 $mode = CRM_Contact_BAO_Query
::MODE_CONTACTS
,
4326 $query = new CRM_Contact_BAO_Query(
4327 $params, $returnProperties,
4328 NULL, TRUE, FALSE, $mode,
4330 TRUE, $smartGroupCache,
4335 //this should add a check for view deleted if permissions are enabled
4336 if ($skipPermissions) {
4337 $query->_skipDeleteClause
= TRUE;
4339 $query->generatePermissionClause(FALSE, $count);
4341 // note : this modifies _fromClause and _simpleFromClause
4342 $query->includePseudoFieldsJoin($sort);
4344 list($select, $from, $where, $having) = $query->query($count);
4346 $options = $query->_options
;
4347 if (!empty($query->_permissionWhereClause
)) {
4348 if (empty($where)) {
4349 $where = "WHERE $query->_permissionWhereClause";
4352 $where = "$where AND $query->_permissionWhereClause";
4356 $sql = "$select $from $where $having";
4359 if ($query->_useGroupBy
) {
4360 $sql .= self
::getGroupByFromSelectColumns($query->_select
, 'contact_a.id');
4362 if (!empty($sort)) {
4363 $sort = CRM_Utils_Type
::escape($sort, 'String');
4364 $sql .= " ORDER BY $sort ";
4366 if ($row_count > 0 && $offset >= 0) {
4367 $offset = CRM_Utils_Type
::escape($offset, 'Int');
4368 $row_count = CRM_Utils_Type
::escape($row_count, 'Int');
4369 $sql .= " LIMIT $offset, $row_count ";
4372 $dao = CRM_Core_DAO
::executeQuery($sql);
4374 // @todo derive this from the component class rather than hard-code two options.
4375 $entityIDField = ($mode == CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
) ?
'contribution_id' : 'contact_id';
4378 while ($dao->fetch()) {
4380 $noRows = $dao->rowCount
;
4382 return array($noRows, NULL);
4384 $val = $query->store($dao);
4385 $convertedVals = $query->convertToPseudoNames($dao, TRUE, TRUE);
4387 if (!empty($convertedVals)) {
4388 $val = array_replace_recursive($val, $convertedVals);
4390 $values[$dao->$entityIDField] = $val;
4393 return array($values, $options);
4397 * Get the actual custom field name by stripping off the appended string.
4399 * The string could be _relative, _from, or _to
4401 * @todo use metadata rather than convention to do this.
4403 * @param string $parameterName
4404 * The name of the parameter submitted to the form.
4411 public static function getCustomFieldName($parameterName) {
4412 if (substr($parameterName, -5, 5) == '_from') {
4413 return substr($parameterName, 0, strpos($parameterName, '_from'));
4415 if (substr($parameterName, -9, 9) == '_relative') {
4416 return substr($parameterName, 0, strpos($parameterName, '_relative'));
4418 if (substr($parameterName, -3, 3) == '_to') {
4419 return substr($parameterName, 0, strpos($parameterName, '_to'));
4424 * Convert submitted values for relative custom fields to query object format.
4426 * The query will support the sqlOperator format so convert to that format.
4428 * @param array $formValues
4430 * @param array $params
4431 * Converted parameters for the query object.
4432 * @param string $values
4434 * @param string $fieldName
4435 * Submitted field name. (Matches form field not DB field.)
4437 protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) {
4438 if (empty($values)) {
4439 // e.g we might have relative set & from & to empty. The form flow is a bit funky &
4440 // this function gets called again after they fields have been converted which can get ugly.
4443 $customFieldName = self
::getCustomFieldName($fieldName);
4445 if (substr($fieldName, -9, 9) == '_relative') {
4446 list($from, $to) = CRM_Utils_Date
::getFromTo($values, NULL, NULL);
4449 if ($fieldName == $customFieldName . '_to' && CRM_Utils_Array
::value($customFieldName . '_from', $formValues)) {
4450 // Both to & from are set. We only need to acton one, choosing from.
4454 $from = CRM_Utils_Array
::value($customFieldName . '_from', $formValues, NULL);
4455 $to = CRM_Utils_Array
::value($customFieldName . '_to', $formValues, NULL);
4457 if (self
::isCustomDateField($customFieldName)) {
4458 list($from, $to) = CRM_Utils_Date
::getFromTo(NULL, $from, $to);
4464 $relativeFunction = array('BETWEEN' => array($from, $to));
4467 $relativeFunction = array('>=' => $from);
4471 $relativeFunction = array('<=' => $to);
4483 * Are we dealing with custom field of type date.
4489 public static function isCustomDateField($fieldName) {
4490 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) == FALSE) {
4493 if ('Date' == civicrm_api3('CustomField', 'getvalue', array('id' => $customFieldID, 'return' => 'data_type'))) {
4500 * Has this field already been reformatting to Query object syntax.
4502 * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems
4503 * to come with associated double queries & is possibly damaging performance.
4505 * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns
4508 * @param mixed $values
4509 * Value in formValues for the field.
4513 public static function isAlreadyProcessedForQueryFormat($values) {
4514 if (!is_array($values)) {
4517 if (($operator = CRM_Utils_Array
::value(1, $values)) == FALSE) {
4520 return in_array($operator, CRM_Core_DAO
::acceptedSQLOperators());
4524 * If the state and country are passed remove state.
4526 * Country is implicit from the state, but including both results in
4527 * a poor query as there is no combined index on state AND country.
4531 * @param array $formValues
4533 public static function filterCountryFromValuesIfStateExists(&$formValues) {
4534 if (!empty($formValues['country']) && !empty($formValues['state_province'])) {
4535 // The use of array map sanitises the data by ensuring we are dealing with integers.
4536 $states = implode(', ', array_map('intval', $formValues['state_province']));
4537 $countryList = CRM_Core_DAO
::singleValueQuery(
4538 "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE id IN ($states)"
4540 if ($countryList == $formValues['country']) {
4541 unset($formValues['country']);
4547 * For some special cases, grouping by subset of select fields becomes mandatory.
4548 * Hence, full_group_by mode is handled by appending any_value
4549 * keyword to select fields not present in groupBy
4551 * @param array $selectClauses
4552 * @param array $groupBy - Columns already included in GROUP By clause.
4556 public static function appendAnyValueToSelect($selectClauses, $groupBy) {
4557 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
4558 $sqlMode = explode(',', CRM_Core_DAO
::singleValueQuery('SELECT @@sql_mode'));
4560 // Disable only_full_group_by mode for lower sql versions.
4561 if (version_compare($mysqlVersion, '5.7', '<') ||
(!empty($sqlMode) && !in_array('ONLY_FULL_GROUP_BY', $sqlMode))) {
4562 $key = array_search('ONLY_FULL_GROUP_BY', $sqlMode);
4563 unset($sqlMode[$key]);
4564 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = '" . implode(',', $sqlMode) . "'");
4567 $groupBy = array_map('trim', (array) $groupBy);
4568 $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
4569 foreach ($selectClauses as $key => &$val) {
4570 list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
4571 // append ANY_VALUE() keyword
4572 if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
4573 $val = str_replace($selectColumn, "ANY_VALUE({$selectColumn})", $val);
4578 return "SELECT " . implode(', ', $selectClauses) . " ";
4582 * Include Select columns in groupBy clause.
4584 * @param array $selectClauses
4585 * @param array $groupBy - Columns already included in GROUP By clause.
4589 public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
4590 $groupBy = (array) $groupBy;
4591 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
4592 $sqlMode = CRM_Core_DAO
::singleValueQuery('SELECT @@sql_mode');
4594 //return if ONLY_FULL_GROUP_BY is not enabled.
4595 if (!version_compare($mysqlVersion, '5.7', '<') && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) {
4596 $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
4597 foreach ($selectClauses as $key => $val) {
4598 $aliasArray = preg_split('/ as /i', $val);
4599 // if more than 1 alias we need to split by ','.
4600 if (count($aliasArray) > 2) {
4601 $aliasArray = preg_split('/,/', $val);
4602 foreach ($aliasArray as $key => $value) {
4603 $alias = current(preg_split('/ as /i', $value));
4604 if (!in_array($alias, $groupBy) && preg_match($regexToExclude, trim($alias)) !== 1) {
4605 $groupBy[] = $alias;
4610 list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
4611 $dateRegex = '/^(DATE_FORMAT|DATE_ADD|CASE)/i';
4612 $tableName = current(explode('.', $selectColumn));
4613 $primaryKey = "{$tableName}.id";
4614 // exclude columns which are already included in groupBy and aggregate functions from select
4615 // CRM-18439 - Also exclude the columns which are functionally dependent on columns in $groupBy (MySQL 5.7+)
4616 if (!in_array($selectColumn, $groupBy) && !in_array($primaryKey, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
4617 if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
4618 $groupBy[] = $alias;
4621 $groupBy[] = $selectColumn;
4628 if (!empty($groupBy)) {
4629 return " GROUP BY " . implode(', ', $groupBy);
4635 * Create and query the db for an contact search.
4637 * @param int $offset
4638 * The offset for the query.
4639 * @param int $rowCount
4640 * The number of rows to return.
4641 * @param string|CRM_Utils_Sort $sort
4642 * The order by string.
4643 * @param bool $count
4644 * Is this a count only query ?.
4645 * @param bool $includeContactIds
4646 * Should we include contact ids?.
4647 * @param bool $sortByChar
4648 * If true returns the distinct array of first characters for search results.
4649 * @param bool $groupContacts
4650 * If true, return only the contact ids.
4651 * @param bool $returnQuery
4652 * Should we return the query as a string.
4653 * @param string $additionalWhereClause
4654 * If the caller wants to further restrict the search (used for components).
4655 * @param null $sortOrder
4656 * @param string $additionalFromClause
4657 * Should be clause with proper joins, effective to reduce where clause load.
4659 * @param bool $skipOrderAndLimit
4661 * @return CRM_Core_DAO
4663 public function searchQuery(
4664 $offset = 0, $rowCount = 0, $sort = NULL,
4665 $count = FALSE, $includeContactIds = FALSE,
4666 $sortByChar = FALSE, $groupContacts = FALSE,
4667 $returnQuery = FALSE,
4668 $additionalWhereClause = NULL, $sortOrder = NULL,
4669 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE
4672 if ($includeContactIds) {
4673 $this->_includeContactIds
= TRUE;
4674 $this->_whereClause
= $this->whereClause();
4677 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
);
4679 // if we’re explicitly looking for a certain contact’s contribs, events, etc.
4680 // and that contact happens to be deleted, set $onlyDeleted to true
4681 foreach ($this->_params
as $values) {
4682 $name = CRM_Utils_Array
::value(0, $values);
4683 $op = CRM_Utils_Array
::value(1, $values);
4684 $value = CRM_Utils_Array
::value(2, $values);
4685 if ($name == 'contact_id' and $op == '=') {
4686 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) {
4687 $onlyDeleted = TRUE;
4693 // building the query string
4694 $groupBy = $groupByCol = NULL;
4696 if (isset($this->_groupByComponentClause
)) {
4697 $groupBy = $this->_groupByComponentClause
;
4698 $groupCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause
));
4699 $groupByCol = explode(', ', $groupCols);
4701 elseif ($this->_useGroupBy
) {
4702 $groupByCol = 'contact_a.id';
4703 $groupBy = ' GROUP BY contact_a.id';
4706 if ($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
&& (!$count)) {
4707 $groupByCol = 'civicrm_activity.id';
4708 $groupBy = 'GROUP BY civicrm_activity.id ';
4711 $order = $orderBy = $limit = '';
4713 list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
4715 if ($rowCount > 0 && $offset >= 0) {
4716 $offset = CRM_Utils_Type
::escape($offset, 'Int');
4717 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
4718 $limit = " LIMIT $offset, $rowCount ";
4723 $this->_sort
= $sort;
4726 $this->includePseudoFieldsJoin($sort);
4728 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);
4730 if (!empty($groupByCol)) {
4731 $groupBy = self
::getGroupByFromSelectColumns($this->_select
, $groupByCol);
4734 if ($additionalWhereClause) {
4735 $where = $where . ' AND ' . $additionalWhereClause;
4738 //additional from clause should be w/ proper joins.
4739 if ($additionalFromClause) {
4740 $from .= "\n" . $additionalFromClause;
4743 // if we are doing a transform, do it here
4744 // use the $from, $where and $having to get the contact ID
4745 if ($this->_displayRelationshipType
) {
4746 $this->filterRelatedContacts($from, $where, $having);
4749 if ($skipOrderAndLimit) {
4750 $query = "$select $from $where $having $groupBy";
4753 $query = "$select $from $where $having $groupBy $order $limit";
4760 return CRM_Core_DAO
::singleValueQuery($query);
4763 $dao = CRM_Core_DAO
::executeQuery($query);
4764 if ($groupContacts) {
4766 while ($dao->fetch()) {
4770 return implode(',', $ids);
4777 * Fetch a list of contacts from the prev/next cache for displaying a search results page
4779 * @param string $cacheKey
4780 * @param int $offset
4781 * @param int $rowCount
4782 * @param bool $includeContactIds
4783 * @return CRM_Core_DAO
4785 public function getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds) {
4786 $this->_includeContactIds
= $includeContactIds;
4787 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
);
4788 list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
4789 $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);
4790 $order = " ORDER BY pnc.id";
4791 $groupByCol = array('contact_a.id', 'pnc.id');
4792 $groupBy = self
::getGroupByFromSelectColumns($this->_select
, $groupByCol);
4793 $limit = " LIMIT $offset, $rowCount";
4794 $query = "$select $from $where $groupBy $order $limit";
4796 return CRM_Core_DAO
::executeQuery($query);
4800 * Populate $this->_permissionWhereClause with permission related clause and update other
4801 * query related properties.
4803 * Function calls ACL permission class and hooks to filter the query appropriately
4805 * Note that these 2 params were in the code when extracted from another function
4806 * and a second round extraction would be to make them properties of the class
4808 * @param bool $onlyDeleted
4809 * Only get deleted contacts.
4810 * @param bool $count
4811 * Return Count only.
4813 public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) {
4814 if (!$this->_skipPermission
) {
4815 $this->_permissionWhereClause
= CRM_ACL_API
::whereClause(
4816 CRM_Core_Permission
::VIEW
,
4818 $this->_whereTables
,
4821 $this->_skipDeleteClause
4824 // regenerate fromClause since permission might have added tables
4825 if ($this->_permissionWhereClause
) {
4826 //fix for row count in qill (in contribute/membership find)
4828 $this->_useDistinct
= TRUE;
4831 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
4832 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
4833 // note : this modifies _fromClause and _simpleFromClause
4834 $this->includePseudoFieldsJoin($this->_sort
);
4838 // add delete clause if needed even if we are skipping permission
4840 if (!$this->_skipDeleteClause
) {
4841 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
4842 $this->_permissionWhereClause
= '(contact_a.is_deleted)';
4846 $this->_permissionWhereClause
= '(contact_a.is_deleted = 0)';
4855 public function setSkipPermission($val) {
4856 $this->_skipPermission
= $val;
4860 * @param null $context
4864 public function &summaryContribution($context = NULL) {
4865 list($innerselect, $from, $where, $having) = $this->query(TRUE);
4869 SELECT COUNT( conts.total_amount ) as total_count,
4870 SUM( conts.total_amount ) as total_amount,
4871 AVG( conts.total_amount ) as total_avg,
4872 conts.currency as currency";
4873 if ($this->_permissionWhereClause
) {
4874 $where .= " AND " . $this->_permissionWhereClause
;
4876 if ($context == 'search') {
4877 $where .= " AND contact_a.is_deleted = 0 ";
4879 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
4880 if (!empty($financialTypes)) {
4881 $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL";
4882 $from .= " LEFT JOIN civicrm_line_item li
4883 ON civicrm_contribution.id = li.contribution_id AND
4884 li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") ";
4887 $where .= " AND civicrm_contribution.financial_type_id IN (0)";
4890 // make sure contribution is completed - CRM-4989
4891 $completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 ";
4894 $summary['total'] = array();
4895 $summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a";
4896 $innerQuery = "SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
4897 civicrm_contribution.currency $from $completedWhere";
4898 $query = "$select FROM (
4899 $innerQuery GROUP BY civicrm_contribution.id
4903 $dao = CRM_Core_DAO
::executeQuery($query);
4905 $summary['total']['count'] = 0;
4906 $summary['total']['amount'] = $summary['total']['avg'] = array();
4907 while ($dao->fetch()) {
4908 $summary['total']['count'] +
= $dao->total_count
;
4909 $summary['total']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
4910 $summary['total']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
4913 $orderBy = 'ORDER BY civicrm_contribution_total_amount_count DESC';
4914 $groupBy = 'GROUP BY currency, civicrm_contribution.total_amount';
4915 $modeSQL = "$select, SUBSTRING_INDEX(GROUP_CONCAT(conts.total_amount
4916 ORDER BY conts.civicrm_contribution_total_amount_count DESC SEPARATOR ';'), ';', 1) as amount,
4917 MAX(conts.civicrm_contribution_total_amount_count) as civicrm_contribution_total_amount_count
4919 $groupBy $orderBy) as conts
4922 $summary['total']['mode'] = CRM_Contribute_BAO_Contribution
::computeStats('mode', $modeSQL);
4924 $medianSQL = "{$from} {$completedWhere}";
4925 $summary['total']['median'] = CRM_Contribute_BAO_Contribution
::computeStats('median', $medianSQL, 'civicrm_contribution');
4926 $summary['total']['currencyCount'] = count($summary['total']['median']);
4928 if (!empty($summary['total']['amount'])) {
4929 $summary['total']['amount'] = implode(', ', $summary['total']['amount']);
4930 $summary['total']['avg'] = implode(', ', $summary['total']['avg']);
4931 $summary['total']['mode'] = implode(', ', $summary['total']['mode']);
4932 $summary['total']['median'] = implode(', ', $summary['total']['median']);
4935 $summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0;
4938 // soft credit summary
4939 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled()) {
4940 $softCreditWhere = "{$completedWhere} AND civicrm_contribution_soft.id IS NOT NULL";
4943 SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency $from $softCreditWhere
4944 GROUP BY civicrm_contribution_soft.id
4947 $dao = CRM_Core_DAO
::executeQuery($query);
4948 $summary['soft_credit']['count'] = 0;
4949 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = array();
4950 while ($dao->fetch()) {
4951 $summary['soft_credit']['count'] +
= $dao->total_count
;
4952 $summary['soft_credit']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
4953 $summary['soft_credit']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
4955 if (!empty($summary['soft_credit']['amount'])) {
4956 $summary['soft_credit']['amount'] = implode(', ', $summary['soft_credit']['amount']);
4957 $summary['soft_credit']['avg'] = implode(', ', $summary['soft_credit']['avg']);
4960 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0;
4965 //@todo - this could be one query using the IF in mysql - eg
4966 // SELECT sum(total_completed), sum(count_completed), sum(count_cancelled), sum(total_cancelled) FROM (
4967 // SELECT civicrm_contribution.total_amount, civicrm_contribution.currency ,
4968 // IF(civicrm_contribution.contribution_status_id = 1, 1, 0 ) as count_completed,
4969 // IF(civicrm_contribution.contribution_status_id = 1, total_amount, 0 ) as total_completed,
4970 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, 1, 0 ) as count_cancelled,
4971 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, total_amount, 0 ) as total_cancelled
4972 // FROM civicrm_contact contact_a
4973 // LEFT JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id
4974 // WHERE ( ... where clause....
4975 // AND (civicrm_contribution.cancel_date IS NOT NULL OR civicrm_contribution.contribution_status_id = 1)
4979 SELECT COUNT( conts.total_amount ) as cancel_count,
4980 SUM( conts.total_amount ) as cancel_amount,
4981 AVG( conts.total_amount ) as cancel_avg,
4982 conts.currency as currency";
4984 $where .= " AND civicrm_contribution.cancel_date IS NOT NULL ";
4985 if ($context == 'search') {
4986 $where .= " AND contact_a.is_deleted = 0 ";
4989 $query = "$select FROM (
4990 SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $where
4991 GROUP BY civicrm_contribution.id
4995 $dao = CRM_Core_DAO
::executeQuery($query);
4998 if ($dao->fetch()) {
4999 $summary['cancel']['count'] = $dao->cancel_count
;
5000 $summary['cancel']['amount'] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
5001 $summary['cancel']['avg'] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
5005 $summary['cancel']['count'] = 0;
5006 $summary['cancel']['amount'] = $summary['cancel']['avg'] = array();
5007 while ($dao->fetch()) {
5008 $summary['cancel']['count'] +
= $dao->cancel_count
;
5009 $summary['cancel']['amount'][] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
5010 $summary['cancel']['avg'][] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
5012 $summary['cancel']['amount'] = implode(', ', $summary['cancel']['amount']);
5013 $summary['cancel']['avg'] = implode(', ', $summary['cancel']['avg']);
5020 * Getter for the qill object.
5024 public function qill() {
5025 return $this->_qill
;
5029 * Default set of return default hier return properties.
5033 public static function &defaultHierReturnProperties() {
5034 if (!isset(self
::$_defaultHierReturnProperties)) {
5035 self
::$_defaultHierReturnProperties = array(
5038 'legal_identifier' => 1,
5039 'external_identifier' => 1,
5040 'contact_type' => 1,
5041 'contact_sub_type' => 1,
5043 'display_name' => 1,
5050 'formal_title' => 1,
5051 'communication_style_id' => 1,
5052 'email_greeting' => 1,
5053 'postal_greeting' => 1,
5057 'preferred_communication_method' => 1,
5058 'do_not_phone' => 1,
5059 'do_not_email' => 1,
5062 'do_not_trade' => 1,
5063 'location' => array(
5065 'location_type' => 1,
5066 'street_address' => 1,
5068 'state_province' => 1,
5070 'postal_code_suffix' => 1,
5073 'phone-Mobile' => 1,
5086 'location_type' => 1,
5087 'street_address' => 1,
5089 'state_province' => 1,
5091 'postal_code_suffix' => 1,
5094 'phone-Mobile' => 1,
5108 return self
::$_defaultHierReturnProperties;
5112 * Build query for a date field.
5114 * @param array $values
5115 * @param string $tableName
5116 * @param string $fieldName
5117 * @param string $dbFieldName
5118 * @param string $fieldTitle
5119 * @param bool $appendTimeStamp
5121 public function dateQueryBuilder(
5122 &$values, $tableName, $fieldName,
5123 $dbFieldName, $fieldTitle,
5124 $appendTimeStamp = TRUE
5126 list($name, $op, $value, $grouping, $wildcard) = $values;
5128 if ($name == "{$fieldName}_low" ||
5129 $name == "{$fieldName}_high"
5131 if (isset($this->_rangeCache
[$fieldName]) ||
!$value) {
5134 $this->_rangeCache
[$fieldName] = 1;
5136 $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL;
5138 if ($name == $fieldName . '_low') {
5140 $firstPhrase = ts('greater than or equal to');
5141 $firstDate = CRM_Utils_Date
::processDate($value);
5143 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5144 if (!empty($secondValues) && $secondValues[2]) {
5146 $secondPhrase = ts('less than or equal to');
5147 $secondValue = $secondValues[2];
5149 if ($appendTimeStamp && strlen($secondValue) == 10) {
5150 $secondValue .= ' 23:59:59';
5152 $secondDate = CRM_Utils_Date
::processDate($secondValue);
5155 elseif ($name == $fieldName . '_high') {
5157 $firstPhrase = ts('less than or equal to');
5159 if ($appendTimeStamp && strlen($value) == 10) {
5160 $value .= ' 23:59:59';
5162 $firstDate = CRM_Utils_Date
::processDate($value);
5164 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5165 if (!empty($secondValues) && $secondValues[2]) {
5167 $secondPhrase = ts('greater than or equal to');
5168 $secondValue = $secondValues[2];
5169 $secondDate = CRM_Utils_Date
::processDate($secondValue);
5173 if (!$appendTimeStamp) {
5174 $firstDate = substr($firstDate, 0, 8);
5176 $firstDateFormat = CRM_Utils_Date
::customFormat($firstDate);
5179 if (!$appendTimeStamp) {
5180 $secondDate = substr($secondDate, 0, 8);
5182 $secondDateFormat = CRM_Utils_Date
::customFormat($secondDate);
5185 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5187 $this->_where
[$grouping][] = "
5188 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5189 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5191 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\"";
5194 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5195 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
5199 if ($name == $fieldName) {
5200 //In Get API, for operators other then '=' the $value is in array(op => value) format
5201 if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
5203 $value = $value[$op];
5206 $date = $format = NULL;
5207 if (strstr($op, 'IN')) {
5209 foreach ($value as &$date) {
5210 $date = CRM_Utils_Date
::processDate($date);
5211 if (!$appendTimeStamp) {
5212 $date = substr($date, 0, 8);
5214 $format[] = CRM_Utils_Date
::customFormat($date);
5216 $date = "('" . implode("','", $value) . "')";
5217 $format = implode(', ', $format);
5219 elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) {
5220 $date = CRM_Utils_Date
::processDate($value);
5221 if (!$appendTimeStamp) {
5222 $date = substr($date, 0, 8);
5224 $format = CRM_Utils_Date
::customFormat($date);
5229 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op $date";
5232 $this->_where
[$grouping][] = self
::buildClause("{$tableName}.{$dbFieldName}", $op);
5235 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5237 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
5238 $this->_qill
[$grouping][] = "$fieldTitle $op $format";
5244 * @param string $tableName
5245 * @param string $fieldName
5246 * @param string $dbFieldName
5247 * @param $fieldTitle
5248 * @param null $options
5250 public function numberRangeBuilder(
5252 $tableName, $fieldName,
5253 $dbFieldName, $fieldTitle,
5256 list($name, $op, $value, $grouping, $wildcard) = $values;
5258 if ($name == "{$fieldName}_low" ||
5259 $name == "{$fieldName}_high"
5261 if (isset($this->_rangeCache
[$fieldName])) {
5264 $this->_rangeCache
[$fieldName] = 1;
5266 $secondOP = $secondPhrase = $secondValue = NULL;
5268 if ($name == "{$fieldName}_low") {
5270 $firstPhrase = ts('greater than');
5272 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5273 if (!empty($secondValues)) {
5275 $secondPhrase = ts('less than');
5276 $secondValue = $secondValues[2];
5281 $firstPhrase = ts('less than');
5283 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5284 if (!empty($secondValues)) {
5286 $secondPhrase = ts('greater than');
5287 $secondValue = $secondValues[2];
5292 $this->_where
[$grouping][] = "
5293 ( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND
5294 ( {$tableName}.{$dbFieldName} $secondOP {$secondValue} )
5296 $displayValue = $options ?
$options[$value] : $value;
5297 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5299 $this->_qill
[$grouping][]
5300 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5303 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}";
5304 $displayValue = $options ?
$options[$value] : $value;
5305 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5307 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5312 if ($name == $fieldName) {
5316 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}";
5318 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5319 $displayValue = $options ?
$options[$value] : $value;
5320 $this->_qill
[$grouping][] = "$fieldTitle - $phrase \"$displayValue\"";
5327 * @param string $tableName
5328 * @param string $fieldName
5329 * @param string $dbFieldName
5330 * @param $fieldTitle
5331 * @param null $options
5333 public function ageRangeQueryBuilder(
5335 $tableName, $fieldName,
5336 $dbFieldName, $fieldTitle,
5339 list($name, $op, $value, $grouping, $wildcard) = $values;
5341 $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping);
5342 $asofDate = NULL; // will be treated as current day
5343 if ($asofDateValues) {
5344 $asofDate = CRM_Utils_Date
::processDate($asofDateValues[2]);
5345 $asofDateFormat = CRM_Utils_Date
::customFormat(substr($asofDate, 0, 8));
5346 $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat;
5349 if ($name == "{$fieldName}_low" ||
5350 $name == "{$fieldName}_high"
5352 if (isset($this->_rangeCache
[$fieldName])) {
5355 $this->_rangeCache
[$fieldName] = 1;
5357 $secondOP = $secondPhrase = $secondValue = NULL;
5359 if ($name == "{$fieldName}_low") {
5360 $firstPhrase = ts('greater than or equal to');
5361 // NB: age > X means date of birth < Y
5363 $firstDate = self
::calcDateFromAge($asofDate, $value, 'min');
5365 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5366 if (!empty($secondValues)) {
5368 $secondPhrase = ts('less than or equal to');
5369 $secondValue = $secondValues[2];
5370 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'max');
5375 $firstPhrase = ts('less than or equal to');
5376 $firstDate = self
::calcDateFromAge($asofDate, $value, 'max');
5378 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5379 if (!empty($secondValues)) {
5381 $secondPhrase = ts('greater than or equal to');
5382 $secondValue = $secondValues[2];
5383 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'min');
5388 $this->_where
[$grouping][] = "
5389 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5390 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5392 $displayValue = $options ?
$options[$value] : $value;
5393 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5395 $this->_qill
[$grouping][]
5396 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5399 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5400 $displayValue = $options ?
$options[$value] : $value;
5401 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5403 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5409 * Calculate date from age.
5411 * @param string $asofDate
5413 * @param string $type
5417 public static function calcDateFromAge($asofDate, $age, $type) {
5418 $date = new DateTime($asofDate);
5419 if ($type == "min") {
5420 // minimum age is $age: dob <= date - age "235959"
5421 $date->sub(new DateInterval("P" . $age . "Y"));
5422 return $date->format('Ymd') . "235959";
5425 // max age is $age: dob >= date - (age + 1y) + 1d "000000"
5426 $date->sub(new DateInterval("P" . ($age +
1) . "Y"))->add(new DateInterval("P1D"));
5427 return $date->format('Ymd') . "000000";
5432 * Given the field name, operator, value & its data type
5433 * builds the where Clause for the query
5434 * used for handling 'IS NULL'/'IS NOT NULL' operators
5436 * @param string $field
5440 * @param string $value
5442 * @param string $dataType
5443 * Data type of the field.
5446 * Where clause for the query.
5448 public static function buildClause($field, $op, $value = NULL, $dataType = NULL) {
5450 $clause = "$field $op";
5458 $clause = " (NULLIF($field, '') IS NULL) ";
5461 case 'IS NOT EMPTY':
5462 $clause = " (NULLIF($field, '') IS NOT NULL) ";
5467 // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter.
5468 if (!empty($value) && (!is_array($value) ||
!array_key_exists($op, $value))) {
5469 $value = array($op => (array) $value);
5473 if (empty($dataType)) {
5474 $dataType = 'String';
5476 if (is_array($value)) {
5477 //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more
5478 // widely used and consistent across the codebase
5479 // adding this here won't accept the search functions which don't submit an array
5480 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, $value, $dataType)) != FALSE) {
5482 return $queryString;
5485 // 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))
5486 // but we got only array(2,5) from the form.
5487 // We could get away with keeping this in 4.6 if we make it such that it throws an enotice in 4.7 so
5488 // people have to de-slopify it.
5489 if (!empty($value[0])) {
5490 if ($op != 'BETWEEN') {
5491 $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReport;
5493 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, array($op => $value), $dataType)) != FALSE) {
5494 return $queryString;
5499 $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReportUsingOldFormat;
5500 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, array($op => array_keys($value)), $dataType)) != FALSE) {
5501 return $queryString;
5506 $value = CRM_Utils_Type
::escape($value, $dataType);
5507 // if we don't have a dataType we should assume
5508 if ($dataType == 'String' ||
$dataType == 'Text') {
5509 $value = "'" . strtolower($value) . "'";
5511 return "$clause $value";
5516 * @param bool $reset
5520 public function openedSearchPanes($reset = FALSE) {
5521 if (!$reset ||
empty($this->_whereTables
)) {
5522 return self
::$_openedPanes;
5525 // pane name to table mapper
5526 $panesMapper = array(
5527 ts('Contributions') => 'civicrm_contribution',
5528 ts('Memberships') => 'civicrm_membership',
5529 ts('Events') => 'civicrm_participant',
5530 ts('Relationships') => 'civicrm_relationship',
5531 ts('Activities') => 'civicrm_activity',
5532 ts('Pledges') => 'civicrm_pledge',
5533 ts('Cases') => 'civicrm_case',
5534 ts('Grants') => 'civicrm_grant',
5535 ts('Address Fields') => 'civicrm_address',
5536 ts('Notes') => 'civicrm_note',
5537 ts('Change Log') => 'civicrm_log',
5538 ts('Mailings') => 'civicrm_mailing',
5540 CRM_Contact_BAO_Query_Hook
::singleton()->getPanesMapper($panesMapper);
5542 foreach (array_keys($this->_whereTables
) as $table) {
5543 if ($panName = array_search($table, $panesMapper)) {
5544 self
::$_openedPanes[$panName] = TRUE;
5548 return self
::$_openedPanes;
5554 public function setOperator($operator) {
5555 $validOperators = array('AND', 'OR');
5556 if (!in_array($operator, $validOperators)) {
5559 $this->_operator
= $operator;
5565 public function getOperator() {
5566 return $this->_operator
;
5574 public function filterRelatedContacts(&$from, &$where, &$having) {
5575 static $_rTypeProcessed = NULL;
5576 static $_rTypeFrom = NULL;
5577 static $_rTypeWhere = NULL;
5579 if (!$_rTypeProcessed) {
5580 $_rTypeProcessed = TRUE;
5582 // create temp table with contact ids
5583 $tableName = CRM_Core_DAO
::createTempTableName('civicrm_transform', TRUE);
5584 $sql = "CREATE TEMPORARY TABLE $tableName ( contact_id int primary key) ENGINE=HEAP";
5585 CRM_Core_DAO
::executeQuery($sql);
5588 REPLACE INTO $tableName ( contact_id )
5594 CRM_Core_DAO
::executeQuery($sql);
5596 $qillMessage = ts('Contacts with a Relationship Type of: ');
5597 $rTypes = CRM_Core_PseudoConstant
::relationshipType();
5599 if (is_numeric($this->_displayRelationshipType
)) {
5600 $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType
]['label_a_b'];
5602 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )
5603 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )
5606 WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}
5607 AND displayRelType.is_active = 1
5611 list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType
);
5612 if ($dirOne == 'a') {
5613 $relationshipTypeLabel = $rTypes[$relType]['label_a_b'];
5615 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )
5616 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )
5620 $relationshipTypeLabel = $rTypes[$relType]['label_b_a'];
5622 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )
5623 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )
5627 WHERE displayRelType.relationship_type_id = $relType
5628 AND displayRelType.is_active = 1
5631 $this->_qill
[0][] = $qillMessage . "'" . $relationshipTypeLabel . "'";
5634 if (!empty($this->_permissionWhereClause
)) {
5635 $_rTypeWhere .= "AND $this->_permissionWhereClause";
5638 if (strpos($from, $_rTypeFrom) === FALSE) {
5639 // lets replace all the INNER JOIN's in the $from so we dont exclude other data
5640 // this happens when we have an event_type in the quert (CRM-7969)
5641 $from = str_replace("INNER JOIN", "LEFT JOIN", $from);
5642 $from .= $_rTypeFrom;
5643 $where = $_rTypeWhere;
5650 * See CRM-19811 for why this is database hurty without apparent benefit.
5656 public static function caseImportant($op) {
5658 in_array($op, array('LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ?
FALSE : TRUE;
5662 * @param $returnProperties
5667 public static function componentPresent(&$returnProperties, $prefix) {
5668 foreach ($returnProperties as $name => $dontCare) {
5669 if (substr($name, 0, strlen($prefix)) == $prefix) {
5677 * Builds the necessary structures for all fields that are similar to option value look-ups.
5679 * @param string $name
5680 * the name of the field.
5682 * the sql operator, this function should handle ALL SQL operators.
5683 * @param string $value
5684 * depends on the operator and who's calling the query builder.
5685 * @param int $grouping
5686 * the index where to place the where clause.
5687 * @param string $daoName
5689 * @param array $field
5690 * an array that contains various properties of the field identified by $name.
5691 * @param string $label
5692 * The label for this field element.
5693 * @param string $dataType
5694 * The data type for this element.
5695 * @param bool $useIDsOnly
5697 public function optionValueQuery(
5705 $dataType = 'String',
5709 $pseudoFields = array(
5716 'communication_style_id',
5720 list($tableName, $fieldName) = explode('.', $field['where'], 2);
5721 if ($tableName == 'civicrm_contact') {
5722 $wc = "contact_a.$fieldName";
5725 $wc = "$tableName.id";
5729 // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
5730 $wc = self
::caseImportant($op) ?
"LOWER({$field['where']})" : "{$field['where']}";
5732 if (in_array($name, $pseudoFields)) {
5733 if (!in_array($name, array('gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'))) {
5734 $wc = "contact_a.{$name}_id";
5736 $dataType = 'Positive';
5737 $value = (!$value) ?
0 : $value;
5739 if ($name == "world_region") {
5740 $field['name'] = $name;
5743 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue($daoName, $field['name'], $value, $op);
5744 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal));
5745 $this->_where
[$grouping][] = self
::buildClause($wc, $op, $value, $dataType);
5749 * Check and explode a user defined numeric string into an array
5750 * this was the protocol used by search builder in the old old days before we had
5751 * super nice js widgets to do the hard work
5753 * @param string $string
5754 * @param string $dataType
5755 * The dataType we should check for the values, default integer.
5757 * @return bool|array
5758 * false if string does not match the pattern
5759 * array of numeric values if string does match the pattern
5761 public static function parseSearchBuilderString($string, $dataType = 'Integer') {
5762 $string = trim($string);
5763 if (substr($string, 0, 1) != '(' ||
substr($string, -1, 1) != ')') {
5767 $string = substr($string, 1, -1);
5768 $values = explode(',', $string);
5769 if (empty($values)) {
5773 $returnValues = array();
5774 foreach ($values as $v) {
5775 if ($dataType == 'Integer' && !is_numeric($v)) {
5778 elseif ($dataType == 'String' && !is_string($v)) {
5781 $returnValues[] = trim($v);
5784 if (empty($returnValues)) {
5788 return $returnValues;
5792 * Convert the pseudo constants id's to their names
5794 * @param CRM_Core_DAO $dao
5795 * @param bool $return
5796 * @param bool $usedForAPI
5798 * @return array|NULL
5800 public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) {
5801 if (empty($this->_pseudoConstantsSelect
)) {
5805 foreach ($this->_pseudoConstantsSelect
as $key => $value) {
5806 if (!empty($this->_pseudoConstantsSelect
[$key]['sorting'])) {
5810 if (is_object($dao) && property_exists($dao, $value['idCol'])) {
5811 $val = $dao->{$value['idCol']};
5812 if ($key == 'groups') {
5813 $dao->groups
= $this->convertGroupIDStringToLabelString($dao, $val);
5817 if (CRM_Utils_System
::isNull($val)) {
5820 elseif (!empty($value['pseudoconstant'])) {
5821 // If pseudoconstant is set that is kind of defacto for 'we have a bit more info about this'
5822 // and we can use the metadata to figure it out.
5823 // ideally this bit of IF will absorb & replace all the rest in time as we move to
5824 // more metadata based choices.
5825 if (strpos($val, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE) {
5826 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($val, CRM_Core_DAO
::VALUE_SEPARATOR
));
5827 foreach ($dbValues as $pseudoValue) {
5828 $convertedValues[] = CRM_Core_PseudoConstant
::getLabel($value['bao'], $value['idCol'], $pseudoValue);
5831 $dao->$key = ($usedForAPI) ?
$convertedValues : implode(', ', $convertedValues);
5832 $realFieldName = CRM_Utils_Array
::value('field_name', $this->_pseudoConstantsSelect
[$key]);
5833 if ($usedForAPI && $realFieldName) {
5834 // normally we would see 2 fields returned for pseudoConstants. An exception is
5835 // preferred_communication_method where there is no id-variant.
5836 // For the api we prioritise getting the real data returned.
5837 // over the resolved version
5838 $dao->$realFieldName = $dbValues;
5843 // This is basically the same as the default but since we have the bao we can use
5844 // a cached function.
5845 $dao->$key = CRM_Core_PseudoConstant
::getLabel($value['bao'], $value['idCol'], $val);
5848 elseif ($baoName = CRM_Utils_Array
::value('bao', $value, NULL)) {
5850 $idColumn = "{$key}_id";
5851 $dao->$idColumn = $val;
5853 if ($key == 'state_province_name') {
5854 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::stateProvince($val);
5857 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::getLabel($baoName, $value['pseudoField'], $val);
5860 elseif ($value['pseudoField'] == 'state_province_abbreviation') {
5861 $dao->$key = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($val);
5863 // @todo handle this in the section above for pseudoconstants.
5864 elseif (in_array($value['pseudoField'], array('participant_role_id', 'participant_role'))) {
5865 // @todo define bao on this & merge into the above condition.
5866 $viewValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $val);
5868 if ($value['pseudoField'] == 'participant_role') {
5869 $pseudoOptions = CRM_Core_PseudoConstant
::get('CRM_Event_DAO_Participant', 'role_id');
5870 foreach ($viewValues as $k => $v) {
5871 $viewValues[$k] = $pseudoOptions[$v];
5874 $dao->$key = ($usedForAPI && count($viewValues) > 1) ?
$viewValues : implode(', ', $viewValues);
5877 $labels = CRM_Core_OptionGroup
::values($value['pseudoField']);
5878 $dao->$key = $labels[$val];
5881 // return converted values in array format
5883 if (strpos($key, '-') !== FALSE) {
5884 $keyVal = explode('-', $key);
5885 $current = &$values;
5886 $lastElement = array_pop($keyVal);
5887 foreach ($keyVal as $v) {
5888 if (!array_key_exists($v, $current)) {
5889 $current[$v] = array();
5891 $current = &$current[$v];
5893 $current[$lastElement] = $dao->$key;
5896 $values[$key] = $dao->$key;
5903 'gender_id' => 'gender',
5904 'prefix_id' => 'individual_prefix',
5905 'suffix_id' => 'individual_suffix',
5906 'communication_style_id' => 'communication_style',
5907 ) as $realField => $labelField) {
5908 // This is a temporary routine for handling these fields while
5909 // we figure out how to handled them based on metadata in
5910 /// export and search builder. CRM-19815, CRM-19830.
5911 if (isset($dao->$realField) && is_numeric($dao->$realField) && isset($dao->$labelField)) {
5912 $dao->$realField = $dao->$labelField;
5920 * Include pseudo fields LEFT JOIN.
5921 * @param string|array $sort can be a object or string
5923 * @return array|NULL
5925 public function includePseudoFieldsJoin($sort) {
5926 if (!$sort ||
empty($this->_pseudoConstantsSelect
)) {
5929 $sort = is_string($sort) ?
$sort : $sort->orderBy();
5932 foreach ($this->_pseudoConstantsSelect
as $name => $value) {
5933 if (!empty($value['table'])) {
5934 $regex = "/({$value['table']}\.|{$name})/";
5935 if (preg_match($regex, $sort)) {
5936 $this->_elemnt
[$value['element']] = 1;
5937 $this->_select
[$value['element']] = $value['select'];
5938 $this->_pseudoConstantsSelect
[$name]['sorting'] = 1;
5939 $present[$value['table']] = $value['join'];
5943 $presentSimpleFrom = $present;
5945 if (array_key_exists('civicrm_worldregion', $this->_whereTables
) &&
5946 array_key_exists('civicrm_country', $presentSimpleFrom)
5948 unset($presentSimpleFrom['civicrm_country']);
5950 if (array_key_exists('civicrm_worldregion', $this->_tables
) &&
5951 array_key_exists('civicrm_country', $present)
5953 unset($present['civicrm_country']);
5956 $presentClause = $presentSimpleFromClause = NULL;
5957 if (!empty($present)) {
5958 $presentClause = implode(' ', $present);
5960 if (!empty($presentSimpleFrom)) {
5961 $presentSimpleFromClause = implode(' ', $presentSimpleFrom);
5964 $this->_fromClause
= $this->_fromClause
. $presentClause;
5965 $this->_simpleFromClause
= $this->_simpleFromClause
. $presentSimpleFromClause;
5967 return array($presentClause, $presentSimpleFromClause);
5971 * Build qill for field.
5973 * Qill refers to the query detail visible on the UI.
5975 * @param string $daoName
5976 * @param string $fieldName
5977 * @param mixed $fieldValue
5979 * @param array $pseudoExtraParam
5981 * Type of the field per CRM_Utils_Type
5985 public static function buildQillForFieldValue(
5990 $pseudoExtraParam = array(),
5991 $type = CRM_Utils_Type
::T_STRING
5993 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
5995 //API usually have fieldValue format as array(operator => array(values)),
5996 //so we need to separate operator out of fieldValue param
5997 if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
5998 $op = key($fieldValue);
5999 $fieldValue = $fieldValue[$op];
6002 // if Operator chosen is NULL/EMPTY then
6003 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
6004 return array(CRM_Utils_Array
::value($op, $qillOperators, $op), '');
6007 if ($fieldName == 'activity_type_id') {
6008 $pseudoOptions = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
6010 elseif ($fieldName == 'country_id') {
6011 $pseudoOptions = CRM_Core_PseudoConstant
::country();
6013 elseif ($fieldName == 'county_id') {
6014 $pseudoOptions = CRM_Core_PseudoConstant
::county();
6016 elseif ($fieldName == 'world_region') {
6017 $pseudoOptions = CRM_Core_PseudoConstant
::worldRegion();
6019 elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') {
6020 $pseudoOptions = CRM_Event_BAO_Event
::getEvents(0, $fieldValue, TRUE, TRUE, TRUE);
6022 elseif ($fieldName == 'contribution_product_id') {
6023 $pseudoOptions = CRM_Contribute_PseudoConstant
::products();
6025 elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
6026 $pseudoOptions = CRM_Core_PseudoConstant
::group();
6028 elseif ($daoName == 'CRM_Batch_BAO_EntityBatch' && $fieldName == 'batch_id') {
6029 $pseudoOptions = CRM_Contribute_PseudoConstant
::batch();
6032 $pseudoOptions = CRM_Core_PseudoConstant
::get($daoName, $fieldName, $pseudoExtraParam);
6035 if (is_array($fieldValue)) {
6036 $qillString = array();
6037 if (!empty($pseudoOptions)) {
6038 foreach ((array) $fieldValue as $val) {
6039 $qillString[] = CRM_Utils_Array
::value($val, $pseudoOptions, $val);
6041 $fieldValue = implode(', ', $qillString);
6044 if ($type == CRM_Utils_Type
::T_DATE
) {
6045 foreach ($fieldValue as $index => $value) {
6046 $fieldValue[$index] = CRM_Utils_Date
::customFormat($value);
6050 // @todo - this is a bit specific (one operator).
6051 // However it is covered by a unit test so can be altered later with
6053 if ($op == 'BETWEEN') {
6054 $separator = ' AND ';
6056 $fieldValue = implode($separator, $fieldValue);
6059 elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) {
6060 $fieldValue = $pseudoOptions[$fieldValue];
6062 elseif ($type === CRM_Utils_Type
::T_DATE
) {
6063 $fieldValue = CRM_Utils_Date
::customFormat($fieldValue);
6066 return array(CRM_Utils_Array
::value($op, $qillOperators, $op), $fieldValue);
6070 * Alter value to reflect wildcard settings.
6072 * The form will have tried to guess whether this is a good field to wildcard but there is
6073 * also a site-wide setting that specifies whether it is OK to append the wild card to the beginning
6074 * or only the end of the string
6076 * @param bool $wildcard
6077 * This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard.
6079 * Generally '=' or 'LIKE'.
6080 * @param string $value
6081 * The search string.
6085 public static function getWildCardedValue($wildcard, $op, $value) {
6086 if ($wildcard && $op == 'LIKE') {
6087 if (CRM_Core_Config
::singleton()->includeWildCardInName
&& (substr($value, 0, 1) != '%')) {
6100 * Process special fields of Search Form in OK (Operator in Key) format
6102 * @param array $formValues
6103 * @param array $specialFields
6104 * Special params to be processed
6105 * @param array $changeNames
6106 * Array of fields whose name should be changed
6108 public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = array()) {
6109 // Array of special fields whose value are considered only for NULL or EMPTY operators
6110 $nullableFields = array('contribution_batch_id');
6112 foreach ($specialFields as $element) {
6113 $value = CRM_Utils_Array
::value($element, $formValues);
6115 if (is_array($value)) {
6116 if (in_array($element, array_keys($changeNames))) {
6117 unset($formValues[$element]);
6118 $element = $changeNames[$element];
6120 $formValues[$element] = array('IN' => $value);
6122 elseif (in_array($value, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
6123 $formValues[$element] = array($value => 1);
6125 elseif (!in_array($element, $nullableFields)) {
6126 // if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
6127 $isWilcard = strstr($value, '%') ?
FALSE : CRM_Core_Config
::singleton()->includeWildCardInName
;
6128 $formValues[$element] = array('LIKE' => self
::getWildCardedValue($isWilcard, 'LIKE', $value));
6135 * Parse and assimilate the various sort options.
6137 * Side-effect: if sorting on a common column from a related table (`city`, `postal_code`,
6138 * `email`), the related table may be joined automatically.
6140 * At time of writing, this code is deeply flawed and should be rewritten. For the moment,
6141 * it's been extracted to a standalone function.
6143 * @param string|CRM_Utils_Sort $sort
6144 * The order by string.
6145 * @param bool $sortByChar
6146 * If true returns the distinct array of first characters for search results.
6147 * @param null $sortOrder
6148 * Who knows? Hu knows. He who knows Hu knows who.
6149 * @param string $additionalFromClause
6150 * Should be clause with proper joins, effective to reduce where clause load.
6152 * list(string $orderByClause, string $additionalFromClause).
6154 protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause) {
6156 $orderByArray = array();
6157 $config = CRM_Core_Config
::singleton();
6158 if ($config->includeOrderByClause ||
6159 isset($this->_distinctComponentClause
)
6162 if (is_string($sort)) {
6166 $orderBy = trim($sort->orderBy());
6168 // Deliberately remove the backticks again, as they mess up the evil
6169 // string munging below. This balanced by re-escaping before use.
6170 $orderBy = str_replace('`', '', $orderBy);
6172 if (!empty($orderBy)) {
6173 // this is special case while searching for
6174 // change log CRM-1718
6175 if (preg_match('/sort_name/i', $orderBy)) {
6176 $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy);
6179 $order = " ORDER BY $orderBy";
6182 $order .= " $sortOrder";
6185 // always add contact_a.id to the ORDER clause
6186 // so the order is deterministic
6187 if (strpos('contact_a.id', $order) === FALSE) {
6188 $order .= ", contact_a.id";
6192 elseif ($sortByChar) {
6193 $orderByArray = array("UPPER(LEFT(contact_a.sort_name, 1)) asc");
6196 $order = " ORDER BY contact_a.sort_name asc, contact_a.id";
6199 if (!$order && empty($orderByArray)) {
6200 return array($order, $additionalFromClause);
6202 // Remove this here & add it at the end for simplicity.
6203 $order = trim(str_replace('ORDER BY', '', $order));
6205 // hack for order clause
6206 $fieldOrder = explode(' ', $order);
6207 $field = $fieldOrder[0];
6213 $this->_tables
["civicrm_address"] = $this->_whereTables
["civicrm_address"] = 1;
6214 $order = str_replace($field, "civicrm_address.{$field}", $order);
6218 case 'state_province':
6219 $this->_tables
["civicrm_{$field}"] = $this->_whereTables
["civicrm_{$field}"] = 1;
6220 if (is_array($this->_returnProperties
) && empty($this->_returnProperties
)) {
6221 $additionalFromClause .= " LEFT JOIN civicrm_{$field} ON civicrm_{$field}.id = civicrm_address.{$field}_id";
6223 $order = str_replace($field, "civicrm_{$field}.name", $order);
6227 $this->_tables
["civicrm_email"] = $this->_whereTables
["civicrm_email"] = 1;
6228 $order = str_replace($field, "civicrm_email.{$field}", $order);
6232 //CRM-12565 add "`" around $field if it is a pseudo constant
6233 foreach ($this->_pseudoConstantsSelect
as $key => $value) {
6234 if (!empty($value['element']) && $value['element'] == $field) {
6235 $order = str_replace($field, "`{$field}`", $order);
6239 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6240 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6243 // The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
6244 // clause. But this magic helper which forgivingly reescapes ORDER BY.
6245 // Note: $sortByChar implies that $order was hard-coded/trusted, so it can do funky things.
6246 if ($order && !$sortByChar) {
6247 $order = CRM_Utils_Type
::escape($order, 'MysqlOrderBy');
6248 return array(' ORDER BY ' . $order, $additionalFromClause);
6250 return array($order, $additionalFromClause);
6254 * Convert a string of group IDs to a string of group labels.
6256 * The original string may include duplicates and groups the user does not have
6257 * permission to see.
6259 * @param CRM_Core_DAO $dao
6260 * @param string $val
6264 public function convertGroupIDStringToLabelString(&$dao, $val) {
6265 $groupIDs = explode(',', $val);
6267 // The pseudoConstant function does not actually cache.
6270 $allGroups = CRM_Core_PseudoConstant
::group();
6272 // Note that groups that the user does not have permission to will be excluded (good).
6273 $groups = array_intersect_key($allGroups, array_flip($groupIDs));
6274 return implode(', ', $groups);
6279 * Set the qill and where properties for a field.
6281 * This function is intended as a short-term function to encourage refactoring
6282 * & re-use - but really we should just have less special-casing.
6284 * @param string $name
6286 * @param string|array $value
6287 * @param string $grouping
6288 * @param string $field
6290 public function setQillAndWhere($name, $op, $value, $grouping, $field) {
6291 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value);
6292 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
6293 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(
6294 1 => $field['title'],
6301 * Has the pseudoconstant of the field been requested.
6303 * For example if the field is payment_instrument_id then it
6304 * has been requested if either payment_instrument_id or payment_instrument
6305 * have been requested. Payment_instrument is the option groun name field value.
6307 * @param array $field
6311 private function pseudoConstantNameIsInReturnProperties($field) {
6312 if (!isset($field['pseudoconstant']['optionGroupName'])) {
6315 if (empty($field['bao']) ||
$field['bao'] != 'CRM_Contact_BAO_Contact') {
6320 if (CRM_Utils_Array
::value($field['pseudoconstant']['optionGroupName'], $this->_returnProperties
)) {
6323 if (CRM_Utils_Array
::value($field['name'], $this->_returnProperties
)) {