Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Added] Added ability to filter exports by date #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions controllers/AmForms_ExportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ public function actionEditExport(array $variables = array())
if (! empty($variables['exportId'])) {
$variables['export'] = craft()->amForms_exports->getExportById($variables['exportId']);

if ( isset( $variables[ 'export' ][ 'criteria' ] ) and !empty( $variables[ 'export' ][ 'criteria' ] ) ) {
foreach ( $variables[ 'export' ][ 'criteria' ] as $key => $value ) {
if ( $key != 'fields' ) {

if ( !empty( $value ) and isset( $value[0] ) and !empty( $value[0] ) and is_array( $value[0] ) and in_array( 'date', array_keys( $value[0] ) ) ) {
foreach ( $value as $subKey => $subValue ) {
$tmpCriteria = $variables[ 'export' ][ 'criteria' ];
$tmpCriteria[ $key ][ $subKey ][ 'date' ] = DateTime::createFromString( $subValue[ 'date' ] );

$variables[ 'export' ]->setAttributes( array(
'criteria' => $tmpCriteria
) );
}
}
}
}
}

if (! $variables['export']) {
throw new Exception(Craft::t('No export exists with the ID “{id}”.', array('id' => $variables['exportId'])));
}
Expand Down Expand Up @@ -133,6 +151,18 @@ public function actionSaveExport()
}
}

// Reformat date fields for better usability
foreach ($criteria['fields'] as $key => $field) {
if ( isset( $criteria[ $field ] ) and !empty( $criteria[ $field ] ) and is_array( $criteria[ $field ][ $key ] ) and in_array( 'date', array_keys( $criteria[ $field ][ $key ] ) ) ) {
$localeData = craft()->i18n->getLocaleData(craft()->language);
$dateFormatter = $localeData->getDateFormatter();
$format = $dateFormatter->getDatepickerPhpFormat();

$criteria[ $field ][ $key ][ 'date' ] = DateTime::createFromFormat( '!' . $format, $criteria[ $field ][ $key ][ 'date' ], craft()->timezone )
->format( 'Y-m-d' ) . ' ' . ( $criteria[ $field . '-time' ][ $key ][ 'time' ] ? $criteria[ $field . '-time' ][ $key ][ 'time' ] : '00:00' );
}
}

// Remove unnecessary criteria
foreach ($criteria as $fieldId => $values) {
if (is_numeric($fieldId) && ! in_array($fieldId, $criteria['fields'])) {
Expand Down
26 changes: 26 additions & 0 deletions services/AmForms_ExportsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,32 @@ private function _addExportCriteria(AmForms_ExportModel $export, &$criteria)
}
$criteria->{$field->handle} = $setCriteria;
break;
case 'Date':

$occurances = array_count_values( $export->criteria[ 'fields' ] );


if ( isset( $occurances[ $field->id ] ) ) {
$dateCriteria = array();

// Multiple dates for field
if ( $occurances[ $field->id ] > 1 ) {
$dateCriteria = array( 'and' );

foreach ( $export->criteria[ $field->id ] as $key => $value ) {
$dateCriteria[] = $export->criteria[ $field->id . '-operator' ][ $key ] . ' ' . $value[ 'date' ];
}

// Single date for field
} else {
$dateCriteria = $export->criteria[ $field->id . '-operator' ][ 0 ] . ' ' . $export->criteria[ $field->id ][ 0 ][ 'date' ];
}

if ( !empty( $dateCriteria ) ) {
$criteria->{$field->handle} = $dateCriteria;
}
}
break;
}
}

Expand Down
4 changes: 3 additions & 1 deletion templates/exports/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@
<tbody>
{% if export.criteria|length and export.criteria.fields|length -%}
{% for criteriaKey, criteriaField in export.criteria.fields %}
{% set givenValue = (attribute(attribute(export.criteria, criteriaField), criteriaKey) is defined ? attribute(attribute(export.criteria, criteriaField), criteriaKey) : null) %}

{% include 'amforms/exports/_fields/template' with {
fields: exportFields,
criteriaCounter: loop.index0,
givenField: criteriaField,
givenValue: (attribute(attribute(export.criteria, criteriaField), criteriaKey) is defined ? attribute(attribute(export.criteria, criteriaField), criteriaKey) : null)
givenValue: givenValue
} %}
{% endfor %}
{% endif -%}
Expand Down
38 changes: 38 additions & 0 deletions templates/exports/_fields/template.twig
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,44 @@
value: fieldValue
}) }}

{% case 'Date' %}
{# If we are dealing with a date field we need to do a little extra processign #}
{% if fieldValue is iterable and 'date' in fieldValue|keys %}
{% set fieldValue = {
date: attribute( fieldValue, 'date' ) ?? null,
time: attribute( attribute( attribute( export.criteria, criteriaField ~ '-time' ), criteriaKey ), 'time' ) ?? null,
operator: attribute( attribute( export.criteria, criteriaField ~ '-operator' ), criteriaKey ) ?? null,
} %}
{% endif %}

{# Operator #}
{{ forms.select({
id: form.id ~ criteriaCounter ~ field.handle ~ '-operator',
name: form.id ~ '[criteria][' ~ field.id ~ '-operator][]',
options: [
{ label: '=', value: '=' },
{ label: '>', value: '>' },
{ label: '>=', value: '>=' },
{ label: '<', value: '<' },
{ label: '<=', value: '<=' },
{ label: '!=', value: '!=' },
],
value: fieldValue.operator ?? ''
}) }}

{# Date #}

{{ forms.date({
id: form.id ~ criteriaCounter ~ field.handle,
name: form.id ~ '[criteria][' ~ field.id ~ '][]',
value: fieldValue.date ?? ''
}) }}
{{ forms.time({
id: form.id ~ criteriaCounter ~ field.handle ~ '-time',
name: form.id ~ '[criteria][' ~ field.id ~ '-time][]',
value: fieldValue.time ?? ''
}) }}

{% default %}
{{ 'Not supported yet'|t }}

Expand Down