From 8ae90f855a4acf9d7dade1b846bf0b563ce97928 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Thu, 13 Nov 2014 16:53:17 +0530 Subject: [PATCH] Cannot use search builder on multiple Grant and Case types --- CRM/Case/BAO/Query.php | 17 +++-- CRM/Contact/Form/Search/Builder.php | 2 +- CRM/Core/BAO/Mapping.php | 3 + CRM/Grant/BAO/Query.php | 79 ++++++++++++++++------ api/v3/Case.php | 1 + api/v3/Grant.php | 1 + templates/CRM/Grant/Form/Search/Common.tpl | 4 +- 7 files changed, 79 insertions(+), 28 deletions(-) diff --git a/CRM/Case/BAO/Query.php b/CRM/Case/BAO/Query.php index fa25990ade..bb4b7ba1f8 100644 --- a/CRM/Case/BAO/Query.php +++ b/CRM/Case/BAO/Query.php @@ -260,7 +260,7 @@ class CRM_Case_BAO_Query { case 'case_status_id': $statuses = CRM_Case_PseudoConstant::caseStatus(); // Standardize input from checkboxes or single value - if (is_array($value)) { + if (is_array($value) && $query->_mode == CRM_Contact_BAO_Query::MODE_CASE) { $value = array_keys($value, 1); } foreach ((array) $value as $k) { @@ -275,12 +275,16 @@ class CRM_Case_BAO_Query { } if ($val) { $query->_where[$grouping][] = "civicrm_case.status_id IN (" . implode(',', $val) . ")"; - $query->_qill[$grouping][] = ts('Case Status is %1', array(1 => implode(' ' . ts('or') . ' ', $names))); - $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; } + else { + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_case.status_id', $op, $value, "Integer"); + } + $query->_qill[$grouping][] = ts('Case Status is %1', array(1 => implode(' ' . ts('or') . ' ', $names))); + $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; return; case 'case_type_id': + case 'case_type': $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); if (is_array($value)) { @@ -300,7 +304,12 @@ class CRM_Case_BAO_Query { $names[] = $caseTypes[$caseTypeId]; } - $query->_where[$grouping][] = "(civicrm_case.case_type_id IN (" . implode(',', $val) . "))"; + if ($val) { + $query->_where[$grouping][] = "(civicrm_case.case_type_id IN (" . implode(',', $val) . "))"; + } + else { + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_case.case_type_id', $op, $value, "Integer"); + } $query->_qill[$grouping][] = ts('Case Type is %1', array(1 => implode(' ' . ts('or') . ' ', $names))); $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 380d04a269..8a5df7fb78 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -443,7 +443,7 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { 'member_is_pay_later' => 'yesno', 'is_override' => 'yesno', ); - $entities = array('contact', 'address', 'activity', 'participant', 'pledge', 'member', 'contribution'); + $entities = array('contact', 'address', 'activity', 'participant', 'pledge', 'member', 'contribution', 'case', 'grant'); CRM_Contact_BAO_Query_Hook::singleton()->alterSearchBuilderOptions($entities, $options); foreach ($entities as $entity) { $fields = civicrm_api3($entity, 'getfields'); diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 7ec58dbafe..9db15191c2 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -461,6 +461,9 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { if (CRM_Core_Permission::access('CiviGrant')) { $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); unset($fields['Grant']['grant_contact_id']); + if ($mappingType == 'Search Builder') { + unset($fields['Grant']['grant_type_id']); + } $compArray['Grant'] = ts('Grant'); } } diff --git a/CRM/Grant/BAO/Query.php b/CRM/Grant/BAO/Query.php index 0854220f62..7a9aa36d12 100644 --- a/CRM/Grant/BAO/Query.php +++ b/CRM/Grant/BAO/Query.php @@ -52,7 +52,7 @@ class CRM_Grant_BAO_Query { * @access public */ static function select(&$query) { - if ($query->_mode & CRM_Contact_BAO_Query::MODE_GRANT) { + if (($query->_mode & CRM_Contact_BAO_Query::MODE_GRANT) || !empty($query->_returnProperties)) { if (!empty($query->_returnProperties['grant_status_id'])) { $query->_select['grant_status_id'] = 'grant_status.id as grant_status_id'; $query->_element['grant_status'] = 1; @@ -127,6 +127,7 @@ class CRM_Grant_BAO_Query { static function whereClauseSingle(&$values, &$query) { $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; list($name, $op, $value, $grouping, $wildcard) = $values; + $val = $names = array(); switch ($name) { case 'grant_money_transfer_date_low': case 'grant_money_transfer_date_high': @@ -185,29 +186,67 @@ class CRM_Grant_BAO_Query { return; case 'grant_type_id': + case 'grant_type': + $grantTypes = CRM_Core_OptionGroup::values('grant_type'); + if (is_array($value)) { + foreach ($value as $k => $v) { + if ($v) { + $val[] = $v; + } + } + if (count($val) > 0) { + // Overwrite $value so it works with an IN where statement. + $op = 'IN'; + $value = '(' . implode(',', $val) . ')'; + } + } + if (!empty($val)) { + foreach($val as $id) { + $names[] = CRM_Utils_Array::value($id, $grantTypes); + } + } + else { + if (!empty($value)) { + $names[] = $grantTypes[$value]; + } + } - $value = $strtolower(CRM_Core_DAO::escapeString(trim($value))); - - $query->_where[$grouping][] = "civicrm_grant.grant_type_id $op '{$value}'"; + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_grant.grant_type_id', $op, $value, "Integer"); - $grantTypes = CRM_Core_OptionGroup::values('grant_type'); - $value = $grantTypes[$value]; - $query->_qill[$grouping][] = ts('Grant Type %2 %1', array(1 => $value, 2 => $op)); + $query->_qill[$grouping][] = ts('Grant Type %2 %1', array(1 => implode(' ' . ts('or') . ' ', $names), 2 => $op)); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; case 'grant_status_id': - - $value = $strtolower(CRM_Core_DAO::escapeString(trim($value))); - - $query->_where[$grouping][] = "civicrm_grant.status_id $op '{$value}'"; - - + case 'grant_status': $grantStatus = CRM_Core_OptionGroup::values('grant_status'); - $value = $grantStatus[$value]; + if (is_array($value)) { + foreach ($value as $k => $v) { + if ($v) { + $val[] = $v; + } + } + if (count($val) > 0) { + // Overwrite $value so it works with an IN where statement. + $op = 'IN'; + $value = '(' . implode(',', $val) . ')'; + } + } + if (!empty($val)) { + foreach($val as $id) { + $names[] = CRM_Utils_Array::value($id, $grantStatus); + } + } + else { + if (!empty($value)) { + $names[] = $grantStatus[$value]; + } + } + + $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_grant.status_id', $op, $value, "Integer"); - $query->_qill[$grouping][] = ts('Grant Status %2 %1', array(1 => $value, 2 => $op)); + $query->_qill[$grouping][] = ts('Grant Status %2 %1', array(1 => implode(' ' . ts('or') . ' ', $names), 2 => $op)); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -326,15 +365,13 @@ class CRM_Grant_BAO_Query { static function buildSearchForm(&$form) { $grantType = CRM_Core_OptionGroup::values('grant_type'); - $form->add('select', 'grant_type_id', ts('Grant Type'), - array('' => ts('- any -')) + $grantType, - FALSE, array('class' => 'crm-select2') + $form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE, + array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2') ); $grantStatus = CRM_Core_OptionGroup::values('grant_status'); - $form->add('select', 'grant_status_id', ts('Grant Status'), - array('' => ts('- any -')) + $grantStatus, - FALSE, array('class' => 'crm-select2') + $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE, + array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2') ); $form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate')); diff --git a/api/v3/Case.php b/api/v3/Case.php index d76b1afa24..bd30bd7331 100644 --- a/api/v3/Case.php +++ b/api/v3/Case.php @@ -147,6 +147,7 @@ function _civicrm_api3_case_create_spec(&$params) { $params['contact_id']['title'] = 'Case Client'; $params['contact_id']['api.required'] = 1; $params['status_id']['api.default'] = 1; + $params['status_id']['api.aliases'] = array('case_status'); $params['creator_id']['api.default'] = 'user_contact_id'; $params['creator_id']['type'] = CRM_Utils_Type::T_INT; $params['creator_id']['title'] = 'Case Created By'; diff --git a/api/v3/Grant.php b/api/v3/Grant.php index 797e03439c..6bfbf6e94e 100644 --- a/api/v3/Grant.php +++ b/api/v3/Grant.php @@ -60,6 +60,7 @@ function civicrm_api3_grant_create($params) { */ function _civicrm_api3_grant_create_spec(&$params) { $params['grant_type_id']['api.required'] = 1; + $params['status_id']['api.aliases'] = array('grant_status'); } /** diff --git a/templates/CRM/Grant/Form/Search/Common.tpl b/templates/CRM/Grant/Form/Search/Common.tpl index 12383177cf..32a2fc52d8 100644 --- a/templates/CRM/Grant/Form/Search/Common.tpl +++ b/templates/CRM/Grant/Form/Search/Common.tpl @@ -29,11 +29,11 @@ {$form.grant_report_received.html} - {$form.grant_status_id.label}
+ {$form.grant_status_id.html} - {$form.grant_type_id.label}
+ {$form.grant_type_id.html} -- 2.25.1