Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Dec 2, 2016
1 parent 5f97821 commit 6bd4041
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
31 changes: 17 additions & 14 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1744,12 +1744,6 @@ public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals =
) {
$result = array($id, 'IN', $values, 0, $wildcard);
}
// Hack for CRM-19325. This is ugly because it creates a dependency
// to the form, that returns the string 'NULL' if the user wants
// contributions that are not contained in any batch.
elseif ($id == 'contribution_batch_id' && $values == 'NULL') {
$result = array($id, 'IS', 'NULL', 0, 0);
}
else {
$result = array($id, '=', $values, 0, $wildcard);
}
Expand Down Expand Up @@ -5950,6 +5944,13 @@ public static function buildQillForFieldValue(
) {
$qillOperators = CRM_Core_SelectValues::getSearchBuilderOperators();

//API usually have fieldValue format as array(operator => array(values)),
//so we need to separate operator out of fieldValue param
if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
$op = key($fieldValue);
$fieldValue = $fieldValue[$op];
}

// if Operator chosen is NULL/EMPTY then
if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
return array(CRM_Utils_Array::value($op, $qillOperators, $op), '');
Expand All @@ -5976,17 +5977,13 @@ public static function buildQillForFieldValue(
elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
$pseudoOptions = CRM_Core_PseudoConstant::group();
}
elseif ($daoName == 'CRM_Batch_BAO_EntityBatch' && $fieldName == 'batch_id') {
$pseudoOptions = CRM_Contribute_PseudoConstant::batch();
}
elseif ($daoName) {
$pseudoOptions = CRM_Core_PseudoConstant::get($daoName, $fieldName, $pseudoExtraParam);
}

//API usually have fieldValue format as array(operator => array(values)),
//so we need to separate operator out of fieldValue param
if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
$op = key($fieldValue);
$fieldValue = $fieldValue[$op];
}

if (is_array($fieldValue)) {
$qillString = array();
if (!empty($pseudoOptions)) {
Expand Down Expand Up @@ -6061,6 +6058,9 @@ public static function getWildCardedValue($wildcard, $op, $value) {
* Array of fields whose name should be changed
*/
public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = array()) {
// Array of special fields whose value are considered only for NULL or EMPTY operators
$nullableFields = array('contribution_batch_id');

foreach ($specialFields as $element) {
$value = CRM_Utils_Array::value($element, $formValues);
if ($value) {
Expand All @@ -6071,7 +6071,10 @@ public static function processSpecialFormValue(&$formValues, $specialFields, $ch
}
$formValues[$element] = array('IN' => $value);
}
else {
elseif (in_array($value, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
$formValues[$element] = array($value => 1);
}
elseif (!in_array($element, $nullableFields)) {
// if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
$isWilcard = strstr($value, '%') ? FALSE : CRM_Core_Config::singleton()->includeWildCardInName;
$formValues[$element] = array('LIKE' => self::getWildCardedValue($isWilcard, 'LIKE', $value));
Expand Down
11 changes: 4 additions & 7 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,9 @@ public static function whereClauseSingle(&$values, &$query) {
return;

case 'contribution_batch_id':
$batches = CRM_Contribute_PseudoConstant::batch();
// The key 'NULL' indicates that the user is looking for contributions
// that are not contained in a batch, see CRM-19325.
$batches['NULL'] = ts('(none)');
$query->_where[$grouping][] = " civicrm_entity_batch.batch_id $op $value";
$query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $op, 2 => $batches[$value]));
list($qillOp, $qillValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Batch_BAO_EntityBatch', 'batch_id', $value, $op);
$query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $qillOp, 2 => $qillValue));
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_entity_batch.batch_id', $op, $value);
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
$query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1;
return;
Expand Down Expand Up @@ -1129,7 +1126,7 @@ public static function buildSearchForm(&$form) {
array(
'' => ts('- any -'),
// CRM-19325
'NULL' => ts('None'),
'IS NULL' => ts('None'),
) + $batches,
FALSE, array('class' => 'crm-select2')
);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public function postProcess() {
'contribution_product_id',
'invoice_id',
'payment_instrument_id',
'contribution_batch_id',
);
CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);

Expand Down Expand Up @@ -337,7 +338,6 @@ public function postProcess() {
);
}

// Why is this called twice? (see line 317)
$this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
$selector = new CRM_Contribute_Selector_Search($this->_queryParams,
$this->_action,
Expand Down

0 comments on commit 6bd4041

Please sign in to comment.