Skip to content

Commit

Permalink
Replace log_date with created_date & modified_date in advanced search
Browse files Browse the repository at this point in the history
This is almost the last step in getting rid of an awful lot of technical debt & hard to maintain code around jcalendar & datepicker.

It does change the UI a bit - the code to create the current UI is pretty tough going and from seeing users try
to work with it I don't think all that code is making it more usable. OTOH we can remove a maintainability blockerr
by switching over
  • Loading branch information
eileenmcnaughton committed Nov 1, 2019
1 parent f512329 commit 80b5c9f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 287 deletions.
57 changes: 6 additions & 51 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1600,41 +1600,6 @@ public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals
}

self::filterCountryFromValuesIfStateExists($formValues);
// We shouldn't have to whitelist fields to not hack but here we are, for now.
$nonLegacyDateFields = [
'participant_register_date_relative',
'receive_date_relative',
'pledge_end_date_relative',
'pledge_create_date_relative',
'pledge_start_date_relative',
'pledge_payment_scheduled_date_relative',
'relationship_start_date_relative',
'relationship_end_date_relative',
'membership_join_date_relative',
'membership_start_date_relative',
'membership_end_date_relative',
'case_start_date_relative',
'case_end_date_relative',
'mailing_job_start_date_relative',
'birth_date_relative',
'deceased_date_relative',
'relation_active_period_date_relative',
];
// Handle relative dates first
foreach (array_keys($formValues) as $id) {
if (
!in_array($id, $nonLegacyDateFields) && (
preg_match('/_date_relative$/', $id))
) {
$dateComponent = explode('_date_relative', $id);
$fromRange = "{$dateComponent[0]}_date_low";
$toRange = "{$dateComponent[0]}_date_high";

if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
CRM_Contact_BAO_Query::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
}
}
}

foreach ($formValues as $id => $values) {
if (self::isAlreadyProcessedForQueryFormat($values)) {
Expand Down Expand Up @@ -1692,13 +1657,6 @@ public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals
) {
self::convertCustomRelativeFields($formValues, $params, $values, $id);
}
elseif (
!in_array($id, $nonLegacyDateFields) && (
preg_match('/_date_relative$/', $id))
) {
// Already handled in previous loop
continue;
}
elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
FALSE)) {
$params[] = [$id, 'IN', explode(',', $values), 0, 0];
Expand Down Expand Up @@ -3999,15 +3957,8 @@ public function changeLog(&$values) {
$name = $targetName[4] ? "%$name%" : $name;
$this->_where[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
$this->_tables['civicrm_log'] = $this->_whereTables['civicrm_log'] = 1;
$fieldTitle = ts('Added By');
foreach ($this->_params as $params) {
if ($params[0] == 'log_date') {
if ($params[2] == 2) {
$fieldTitle = ts('Modified By');
}
break;
}
}
$fieldTitle = ts('Altered By');

list($qillop, $qillVal) = self::buildQillForFieldValue(NULL, 'changed_by', $name, 'LIKE');
$this->_qill[$grouping][] = ts("%1 %2 '%3'", [
1 => $fieldTitle,
Expand Down Expand Up @@ -5298,6 +5249,10 @@ public function dateQueryBuilder(
// @todo - remove dateFormat - pretty sure it's never passed in...
list($name, $op, $value, $grouping, $wildcard) = $values;

if ($tableName === 'civicrm_contact') {
// Special handling for contact table as it has a known alias in advanced search.
$tableName = 'contact_a';
}
if ($name == "{$fieldName}_low" ||
$name == "{$fieldName}_high"
) {
Expand Down
29 changes: 0 additions & 29 deletions CRM/Contact/BAO/SavedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,35 +386,6 @@ protected function assignTestValue($fieldName, &$fieldDef, $counter) {
}
}

/**
* Store relative dates in separate array format
*
* @param array $queryParams
* @param array $formValues
* @deprecated
*/
public static function saveRelativeDates(&$queryParams, $formValues) {
// This is required only until all fields are converted to datepicker fields as the new format is truer to the
// form format and simply saves (e.g) custom_3_relative => "this.year"
$relativeDates = ['relative_dates' => []];
$specialDateFields = [
'log_date_relative',
];
foreach ($formValues as $id => $value) {
if (in_array($id, $specialDateFields) && !empty($value)) {
$entityName = strstr($id, '_date', TRUE);
if (empty($entityName)) {
$entityName = strstr($id, '_relative', TRUE);
}
$relativeDates['relative_dates'][$entityName] = $value;
}
}
// merge with original queryParams if relative date value(s) found
if (count($relativeDates['relative_dates'])) {
$queryParams = array_merge($queryParams, $relativeDates);
}
}

/**
* Store search variables in $queryParams which were skipped while processing query params,
* precisely at CRM_Contact_BAO_Query::fixWhereValues(...). But these variable are required in
Expand Down
12 changes: 6 additions & 6 deletions CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ public static function getSearchFieldMetadata() {
'sort_name' => ['title' => ts('Complete OR Partial Name'), 'template_grouping' => 'basic'],
'email' => ['title' => ts('Complete OR Partial Email'), 'entity' => 'Email', 'template_grouping' => 'basic'],
'contact_tags' => ['name' => 'contact_tags', 'type' => CRM_Utils_Type::T_INT, 'is_pseudofield' => TRUE, 'template_grouping' => 'basic'],
'created_date' => ['name' => 'created_date', 'template_grouping' => 'changeLog'],
'modified_date' => ['name' => 'modified_date', 'template_grouping' => 'changeLog'],
'birth_date' => ['name' => 'birth_date', 'template_grouping' => 'demographic'],
'deceased_date' => ['name' => 'deceased_date', 'template_grouping' => 'demographic'],
'is_deceased' => ['is_deceased', 'template_grouping' => 'demographic'],
Expand Down Expand Up @@ -503,17 +505,15 @@ public static function activity(&$form) {

/**
* @param CRM_Core_Form $form
*
* @throws \CiviCRM_API3_Exception
*/
public static function changeLog(&$form) {
$form->add('hidden', 'hidden_changeLog', 1);

$form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('changeLog')]);
$form->addFormFieldsFromMetadata();
// block for change log
$form->addElement('text', 'changed_by', ts('Modified By'), NULL);

$dates = [1 => ts('Added'), 2 => ts('Modified')];
$form->addRadio('log_date', NULL, $dates, ['allowClear' => TRUE]);

CRM_Core_Form_Date::buildDateRange($form, 'log_date', 1, '_low', '_high', ts('From:'), FALSE, FALSE);
}

/**
Expand Down
1 change: 0 additions & 1 deletion CRM/Contact/Form/Task/SaveSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ public function postProcess() {
// Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the
// sql operator syntax at the query layer.
if (!$isSearchBuilder) {
CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
$savedSearch->form_values = serialize($queryParams);
}
Expand Down
43 changes: 5 additions & 38 deletions templates/CRM/Contact/Form/Search/Criteria/ChangeLog.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,16 @@
<div id="changelog" class="form-item">
<table class="form-layout">
<tr>
<td colspan="2">
{$form.log_date.html}
</td>
</tr>
<tr>
<td width="30%">
<span class="modifiedBy"><label>{ts}Modified By{/ts}</label></span>
<span class="hiddenElement addedBy"><label>{ts}Added By{/ts}</label></span>
<td>
<span class="modifiedBy"><label>{ts}Modified By{/ts}</label></span></br>
{$form.changed_by.html}
</td>
<td>
<span class="modifiedBy"><label>{ts}Modified Between{/ts}</label></span>
<span class="hiddenElement addedBy"><label>{ts}Added Between{/ts}</label></span>
{include file="CRM/Core/DatePickerRangeWrapper.tpl" fieldName="created_date"}
</td>
</tr>
<tr>
<td>
{$form.changed_by.html}
{include file="CRM/Core/DatePickerRangeWrapper.tpl" fieldName="modified_date"}
</td>
{include file="CRM/Core/DateRange.tpl" fieldName="log_date" from='_low' to='_high'}
</tr>
</table>
</div>

{literal}
<script type="text/javascript">
CRM.$(function($) {
function updateChangeLogLabels() {
var changeType = $('input[name=log_date]:checked').val();
if (changeType == 2) {
$('.addedBy').hide();
$('.modifiedBy').show();
}
else {
if (changeType == 1) {
$('.addedBy').show();
$('.modifiedBy').hide();
}
}
}
$('[name=log_date]:input').change(updateChangeLogLabels);
updateChangeLogLabels();
});


</script>
{/literal}
42 changes: 2 additions & 40 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1027,46 +1027,6 @@ public function testGetSummaryQueryWithFinancialACLEnabled() {
$this->disableFinancialACLs();
}

/**
* When we have a relative date in search criteria, check that convertFormValues() sets _low & _high date fields and returns other criteria.
* CRM-21816 fix relative dates in search bug
*/
public function testConvertFormValuesCRM21816() {
$fv = [
// next 60 days
"member_end_date_relative" => "starting_2.month",
"member_end_date_low" => "20180101000000",
"member_end_date_high" => "20180331235959",
"membership_is_current_member" => "1",
"member_is_primary" => "1",
];
// $fv is modified by convertFormValues()
$fv_orig = $fv;
$params = CRM_Contact_BAO_Query::convertFormValues($fv);

// restructure for easier testing
$modparams = [];
foreach ($params as $p) {
$modparams[$p[0]] = $p;
}

// Check member_end_date_low is in params
$this->assertTrue(is_array($modparams['member_end_date_low']));
// ... fv and params should match
$this->assertEquals($modparams['member_end_date_low'][2], $fv['member_end_date_low']);
// ... fv & fv_orig should be different
$this->assertNotEquals($fv['member_end_date_low'], $fv_orig['member_end_date_low']);

// same for member_end_date_high
$this->assertTrue(is_array($modparams['member_end_date_high']));
$this->assertEquals($modparams['member_end_date_high'][2], $fv['member_end_date_high']);
$this->assertNotEquals($fv['member_end_date_high'], $fv_orig['member_end_date_high']);

// Check other fv values are in params
$this->assertEquals($modparams['membership_is_current_member'][2], $fv_orig['membership_is_current_member']);
$this->assertEquals($modparams['member_is_primary'][2], $fv_orig['member_is_primary']);
}

/**
* Create contributions to test summary calculations.
*
Expand All @@ -1078,6 +1038,8 @@ public function testConvertFormValuesCRM21816() {
* Donation |NULL | 300.00 |SSF | Donation,Donation | 2 | 200.00,100.00
* Donation |2019-02-13 00:00:00 | 50.00 |SSF | Donation | 1 | 50.00
* Member Dues |2019-02-13 00:00:00 | 50.00 |SSF | Member Dues | 1 | 50.00
*
* @throws \CRM_Core_Exception
*/
protected function createContributionsForSummaryQueryTests() {
$contactID = $this->individualCreate();
Expand Down
Loading

0 comments on commit 80b5c9f

Please sign in to comment.