Commit 7ed3194a authored by jaapjansma's avatar jaapjansma
Browse files

Added date filter to filter date with the PHP Date Format.

parent 2ff26346
......@@ -2,6 +2,7 @@
* Search tasks (eg. Export) work with Member,Contribute,Participant,Case...
* Added source to retrieve the owner membership, when owner membership is not set (meaning it is already the primary) then it will return itself.
* Added date filter to filter date with the PHP Date Format.
# Version 1.3.0
......
......@@ -134,6 +134,7 @@ class Factory {
$this->addOutput('participant_search', new Definition('CRM_DataprocessorSearch_ParticipantSearch'), E::ts('Participant Search'));
$this->addOutput('export_csv', new Definition('CRM_DataprocessorOutputExport_CSV'), E::ts('CSV Export'));
$this->addFilter('simple_sql_filter', new Definition('Civi\DataProcessor\FilterHandler\SimpleSqlFilter'), E::ts('Field filter'));
$this->addFilter('date_filter', new Definition('Civi\DataProcessor\FilterHandler\DateFilter'), E::ts('Date filter'));
$this->addFilter('multiple_field_filter', new Definition('Civi\DataProcessor\FilterHandler\MultipleFieldFilter'), E::ts('Text in multiple fields Filter'));
$this->addFilter('activity_filter', new Definition('Civi\DataProcessor\FilterHandler\ActivityFilter'), E::ts('Activity filter'));
$this->addFilter('contact_filter', new Definition('Civi\DataProcessor\FilterHandler\ContactFilter'), E::ts('Contact filter'));
......
......@@ -527,9 +527,9 @@ abstract class AbstractFilterHandler {
}
// and a min value input box
$form->add('text', "{$alias}_min", E::ts('Min'), ['class' => 'six']);
$form->add('text', "{$alias}_min", E::ts('Min'), ['class' => $sizeClass]);
// and a max value input box
$form->add('text', "{$alias}_max", E::ts('Max'), ['class' => 'six']);
$form->add('text', "{$alias}_max", E::ts('Max'), ['class' => $sizeClass]);
if (isset($defaultFilterValue['min'])) {
$defaults[$alias.'_min'] = $defaultFilterValue['min'];
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessor\FilterHandler;
use Civi\DataProcessor\DataFlow\SqlDataFlow;
use Civi\DataProcessor\DataSpecification\CustomFieldSpecification;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\Exception\InvalidConfigurationException;
use CRM_Dataprocessor_ExtensionUtil as E;
class DateFilter extends AbstractFieldFilterHandler {
/**
* Initialize the filter
*
* @throws \Civi\DataProcessor\Exception\DataSourceNotFoundException
* @throws \Civi\DataProcessor\Exception\InvalidConfigurationException
* @throws \Civi\DataProcessor\Exception\FieldNotFoundException
*/
protected function doInitialization() {
if (!isset($this->configuration['datasource']) || !isset($this->configuration['field'])) {
throw new InvalidConfigurationException(E::ts("Filter %1 requires a field to filter on. None given.", array(1=>$this->title)));
}
$this->initializeField($this->configuration['datasource'], $this->configuration['field']);
$this->fieldSpecification->type = 'Int';
}
/**
* @param array $filter
* The filter settings
* @return mixed
*/
public function setFilter($filter) {
$this->resetFilter();
$dataFlow = $this->dataSource->ensureField($this->inputFieldSpecification);
if ($dataFlow && $dataFlow instanceof SqlDataFlow) {
$value = null;
if (isset($filter['value']) && is_array($filter['value'])) {
$value = array();
foreach($filter['value'] as $v) {
$dateTime = new \DateTime($v);
$value[] = $dateTime->format('Y-m-d');
}
} elseif (isset($filter['value'])) {
$dateTime = new \DateTime($filter['value']);
$value = $dateTime->format('Y-m-d');
}
$this->whereClause = new SqlDataFlow\SimpleWhereClause($dataFlow->getName(), $this->inputFieldSpecification->name, $filter['op'], $value, 'String');
$dataFlow->addWhereClause($this->whereClause);
}
}
/**
* Returns true when this filter has additional configuration
*
* @return bool
*/
public function hasConfiguration() {
return true;
}
/**
* When this filter type has additional configuration you can add
* the fields on the form with this function.
*
* @param \CRM_Core_Form $form
* @param array $filter
*/
public function buildConfigurationForm(\CRM_Core_Form $form, $filter=array()) {
$fieldSelect = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFilterFieldsInDataSources($filter['data_processor_id'], [self::class, 'filterDateFields']);
$form->add('select', 'field', E::ts('Field'), $fieldSelect, true, array(
'style' => 'min-width:250px',
'class' => 'crm-select2 huge data-processor-field-for-name',
'placeholder' => E::ts('- select -'),
));
if (isset($filter['configuration'])) {
$configuration = $filter['configuration'];
if (isset($configuration['field']) && isset($configuration['datasource'])) {
$defaults['field'] = $configuration['datasource'] . '::' . $configuration['field'];
$form->setDefaults($defaults);
}
}
}
/**
* When this filter type has configuration specify the template file name
* for the configuration form.
*
* @return false|string
*/
public function getConfigurationTemplateFileName() {
return "CRM/Dataprocessor/Form/Filter/Configuration/DateFilter.tpl";
}
/**
* File name of the template to add this filter to the criteria form.
*
* @return string
*/
public function getTemplateFileName() {
return "CRM/Dataprocessor/Form/Filter/DateFilter.tpl";
}
/**
* Process the submitted values and create a configuration array
*
* @param $submittedValues
* @return array
*/
public function processConfiguration($submittedValues) {
list($datasource, $field) = explode('::', $submittedValues['field'], 2);
$configuration['field'] = $field;
$configuration['datasource'] = $datasource;
return $configuration;
}
protected function getOperatorOptions(FieldSpecification $fieldSpec) {
return array(
'=' => E::ts('Is equal to'),
'<=' => E::ts('Is less than or equal to'),
'>=' => E::ts('Is greater than or equal to'),
'<' => E::ts('Is less than'),
'>' => E::ts('Is greater than'),
'!=' => E::ts('Is not equal to'),
'null' => E::ts('Is empty'),
'not null' => E::ts('Is not empty'),
'bw' => E::ts('Is between'),
'nbw' => E::ts('Is not between'),
);
}
/**
* Filters the date fields
*
* @param \Civi\DataProcessor\DataSpecification\FieldSpecification $field
* @return bool
*/
public static function filterDateFields(FieldSpecification $field) {
if ($field->type == 'Date') {
return true;
}
return false;
}
}
{crmScope extensionKey='dataprocessor'}
<div class="crm-section">
<div class="label">{$form.field.label}</div>
<div class="content">{$form.field.html}</div>
<div class="clear"></div>
</div>
{/crmScope}
{crmScope extensionKey='dataprocessor'}
{assign var=fieldOp value=$filter.alias|cat:"_op"}
{assign var=filterVal value=$filter.alias|cat:"_value"}
{assign var=filterMin value=$filter.alias|cat:"_min"}
{assign var=filterMax value=$filter.alias|cat:"_max"}
<tr>
<td class="label">{$filter.title}</td>
<td>
{if $form.$fieldOp.html}
<span class="filter-processor-element {$filter.alias}">{$form.$fieldOp.html}</span>
<span class="filter-processor-show-close {$filter.alias}">&nbsp;</span>
{/if}
</td>
<td>
{if $filter.type == 'Date' || $filter.type == 'Timestamp'}
{include file="CRM/Dataprocessor/Form/Filter/DateRange.tpl" fieldName=$filter.alias from='_from' to='_to'}
{else}
<span id="{$filterVal}_cell">
{$form.$filterVal.label}&nbsp;{$form.$filterVal.html}</span>
<span id="{$filterMin}_max_cell">{$form.$filterMin.label}&nbsp;{$form.$filterMin.html}&nbsp;&nbsp;{$form.$filterMax.label}&nbsp;{$form.$filterMax.html}</span>
<p class="description">{ts 1='https://www.php.net/manual/en/datetime.formats.php'}Enter a date according to PHP Date Function. <br />
E.g. '2020-12-31' or '2 years ago'. <br />
See <a href="%1"php.net</a> for more information.{/ts}</p>
{/if}
</td>
</tr>
{/crmScope}
......@@ -7,11 +7,52 @@
<th>{ts}Operator{/ts}</th>
<th>{ts}Value{/ts}</th>
</tr>
{include file=$filter_template filterName=$filter.name filter=$filter}
{include file=$filter_template filterName=$filter.alias filter=$filter}
</table>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
{/crmScope}
\ No newline at end of file
<script type="text/javascript">
var val = "dnc";
{assign var=fieldOp value=$filter.alias|cat:"_op"}
{if !($filter.operatorType & 4) && !$filter.no_display && $form.$fieldOp.html}
var val = document.getElementById("{$fieldOp}").value;
{/if}
showHideMaxMinVal("{$filter.alias}", val );
initializeOperator("{$filter.alias}");
{literal}
function showHideMaxMinVal( field, val ) {
var fldVal = field + "_value_cell";
var fldMinMax = field + "_min_max_cell";
if ( val == "bw" || val == "nbw" ) {
cj('#' + fldVal ).hide();
cj('#' + fldMinMax ).show();
} else if (val =="nll" || val == "nnll") {
cj('#' + fldVal).hide() ;
cj('#' + field + '_value').val('');
cj('#' + fldMinMax ).hide();
} else {
cj('#' + fldVal ).show();
cj('#' + fldMinMax ).hide();
}
}
function initializeOperator(filterName) {
var currentOp = cj('.filter-processor-element.'+filterName+' select option:selected');
cj('.filter-processor-element.'+filterName).addClass('hiddenElement');
cj('.filter-processor-show-close.'+filterName).html(currentOp.html() + '&nbsp;<i class="crm-i fa-pencil">&nbsp;</i>');
cj('.filter-processor-show-close.'+filterName).attr('title', '{/literal}{ts}Change{/ts}{literal}');
cj('.filter-processor-show-close.'+filterName).addClass('crm-editable-enabled');
cj('.filter-processor-show-close.'+filterName).click(function () {
cj('.filter-processor-element.'+filterName).removeClass('hiddenElement');
cj('.filter-processor-show-close.'+filterName).removeClass('crm-editable-enabled');
cj('.filter-processor-show-close.'+filterName).addClass('hiddenElement');
});
}
{/literal}
</script>
{/crmScope}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment