Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files Browse the repository at this point in the history
Refactored filter.php.Created elements and FilterHelper.
Ioannis committed May 1, 2024
1 parent f86ac2d commit f959a8a
Showing 12 changed files with 501 additions and 198 deletions.
7 changes: 3 additions & 4 deletions app/src/View/Helper/FieldHelper.php
@@ -172,9 +172,8 @@ public function calculateLabelAndDescription(string $fieldName): array
* Emit a date/time form control.
* This is a wrapper function for $this->control()
*
* @param string $fieldName Form field
* @param string $dateType Standard, DateOnly, FromTime, ThroughTime
* @param array|null $queryParams Request Query parameters used by the filtering Blocks to get the date values
* @param string $fieldName Form field
* @param string $dateType Standard, DateOnly, FromTime, ThroughTime
* @param string|null $label
*
* @return string HTML element
@@ -183,13 +182,13 @@ public function calculateLabelAndDescription(string $fieldName): array

public function dateField(string $fieldName,
string $dateType=DateTypeEnum::Standard,
array $queryParams=null,
string $label=null): string
{
// Initialize
$dateFormat = $dateType === DateTypeEnum::DateOnly ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss';
$dateTitle = $dateType === DateTypeEnum::DateOnly ? 'datepicker.enterDate' : 'datepicker.enterDateTime';
$datePattern = $dateType === DateTypeEnum::DateOnly ? '\d{4}-\d{2}-\d{2}' : '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}';
$queryParams = $this->getView()->getRequest()->getQueryParams();
$date_object = null;

if(isset($queryParams)) {
160 changes: 160 additions & 0 deletions app/src/View/Helper/FilterHelper.php
@@ -0,0 +1,160 @@
<?php
/**
* COmanage Registry Filter Helper
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

namespace App\View\Helper;

use Cake\Collection\Collection;
use Cake\Utility\{Inflector, Hash};
use Cake\View\Helper;

class FilterHelper extends Helper
{
/**
* Calculate Form Default Field Options
*
* @param string $columnName
* @param string $label
*
* @return array
*/
public function calculateFieldParams(string $columnName, string $label): array{
$queryParameters = $this->getView()->getRequest()->getQueryParams();
$searchableAttributesExtras = $this->getView()->get('vv_searchable_attributes_extras') ?? [];
$populatedVarData = $this->getView()->get(
// The populated variables are in plural while the column names are singular
// Convention: It is a prerequisite that the vvar should be the plural of the column name
lcfirst(Inflector::pluralize(Inflector::camelize($columnName)))
);

// Field options
$formParams = [
'label' => $label,
'type' => isset($populatedVarData) ? 'select' : 'text',
// Options will be ignored for non-select fields
'options' => $populatedVarData,
'value' => $queryParameters[$columnName] ?? '',
'required' => false,
'class' => 'form-control',
// Empty will be ignored for non-select fields
'empty' => true
];

// Custom/Additional option items defined in the ModelTable::initialize::setFilterConfig
// Example: CousTable
if(isset($searchableAttributesExtras[$columnName]['options'])) {
// Flatten the custom options
$customOptionsFlattened = Hash::flatten($searchableAttributesExtras[$columnName]['options']);
// Get the key of the placeholder string
$dataKey = array_search('@DATA@', $customOptionsFlattened, true);
if($dataKey !== false) {
$customOptionsFlattened[$dataKey] = $formParams['options'];
$formParams['options'] = Hash::expand($customOptionsFlattened);
}
}

return $formParams;
}

/**
*
* @return array[] [search_params, $field_booleans_columns, $field_datetime_columns, $field_generic_columns]
*/
public function explodeFieldsByType(): array
{
// Get the query string and separate the search params from the non-search params
$queryParameters = $this->getView()->getRequest()->getQueryParams();
$searchableAttributes = $this->getView()->get('vv_searchable_attributes') ?? [];

// Filter the search params and take params with aliases into consideration
$search_params = [];
$field_booleans_columns = [];
$field_datetime_columns = [];
$field_generic_columns = [];
foreach ($searchableAttributes as $attr => $value) {
if($value['type'] == 'boolean') {
$field_booleans_columns[$attr] = $value;
} elseif ($value['type'] == 'timestamp') {
$field_datetime_columns[$attr] = $value;
} else {
$field_generic_columns[$attr] = $value;
}

if(isset($queryParameters[$attr])) {
$search_params[$attr] = $queryParameters[$attr];
continue;
}

if(isset($value['alias']) && is_array($value['alias'])) {
foreach ($value['alias'] as $alias_key) {
if(isset($queryParameters[$alias_key])) {
$search_params[$attr][$alias_key] = $queryParameters[$alias_key];
}
}
}
}

return [
$search_params,
$field_booleans_columns,
$field_datetime_columns,
$field_generic_columns,
];
}

/**
* Return an array of the Form hidden fields and values
*
* @return array
*/
public function getHiddenFields(): array
{
// Get the query string and separate the search params from the non-search params
$queryParameters = $this->getView()->getRequest()->getQueryParams();
$searchableAttributes = $this->getView()->get('vv_searchable_attributes') ?? [];

// Search attributes collection
$alias_params = (new Collection($searchableAttributes))
->filter(fn ($val, $attr) => (\is_array($val) && \array_key_exists('alias', $val)) )
->extract('alias')
->unfold()
->toArray();

// For the non-search params, we need to search the alias params as well
$searchable_parameters = [
...array_keys($searchableAttributes),
...$alias_params
];

// Pass back the non-search params as hidden fields, but always exclude the page parameter
// because we need to start new searches on-page one (or we're likely to end up with a 404).
return (new Collection($queryParameters))
->filter(fn($value, $key) => !\in_array($key, $searchable_parameters, true) && $key != 'page')
->toArray();
}
}
8 changes: 6 additions & 2 deletions app/templates/Standard/index.php
@@ -50,14 +50,18 @@
$linkActions = ['edit', 'view'];

// $vv_template_path will be set for plugins
$templatePath = $vv_template_path ?? ROOT . DS . "templates" . DS . $modelsName;
$templatePath = $vv_template_path ?? ROOT . DS . 'templates' . DS . $modelsName;

// Read the index configuration ($indexColumns) and the associated actions for this model
$incFile = $templatePath . DS . "columns.inc";
$incFile = $templatePath . DS . 'columns.inc';

if(!is_readable($incFile)) {
throw new \InvalidArgumentException("$incFile is not readable");
}
include($incFile);
if(isset($indexColumns)) {
$this->set('vv_indexColumns', $indexColumns);
}

// $linkFilter is used for models that belong to a specific parent model (eg: co_id)
$linkFilter = [];
63 changes: 63 additions & 0 deletions app/templates/element/filter/checkboxes.php
@@ -0,0 +1,63 @@
<?php
/**
* COmanage Registry Top Filters Checkboxes
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/


/*
* Parameters:
* $field_booleans_columns : array, required
*/

declare(strict_types = 1);

// Get the query string and separate the search params from the non-search params
$query = $this->request->getQueryParams();

?>


<?php if(!empty($field_booleans_columns)): ?>
<div class="top-filters-checkboxes input">
<div class="top-filters-checkbox-fields">
<?php foreach($field_booleans_columns as $key => $options): ?>
<div class="filter-boolean <?= empty($options['active']) ? 'filter-inactive' : 'filter-active' ?>">
<div class="form-check form-check-inline">
<?php
print $this->Form->label($options['label'] ?? $key);
print $this->Form->checkbox($key, [
'id' => str_replace("_", "-", $key),
'class' => 'form-check-input',
'checked' => $query[$key] ?? 0,
'hiddenField' => false,
'required' => false
]);
?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
64 changes: 64 additions & 0 deletions app/templates/element/filter/dateSingle.php
@@ -0,0 +1,64 @@
<?php
/**
* COmanage Registry Top Filters Checkboxes
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v5.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/


/*
* Parameters:
* $columns : array, required
* $key : string, required
* $options : array, required
* $query : array, required
*/

declare(strict_types = 1);

use Cake\Utility\Inflector;
use App\Lib\Enum\DateTypeEnum;

// $columns = the passed parameter $indexColumns as found in columns.inc;
// provides overrides for labels and sorting.
$columns = $vv_indexColumns;

$wrapperCssClass = 'filter-active';
if(empty($options['active'])) {
$wrapperCssClass = 'filter-inactive';
}

$label = Inflector::humanize(
Inflector::underscore(
$options['label'] ?? $columns[$key]['label']
)
);

?>

<div class="top-filters-fields-date filter-standard <?= $wrapperCssClass ?>">
<?= $this->Form->label($key, $label) ?>
<div class="d-flex">
<?= $this->Field->dateField($key, DateTypeEnum::DateOnly) ?>
</div>
</div>
@@ -25,12 +25,24 @@
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

/*
* Parameters:
* $field_datetime_columns : array, required
*/

declare(strict_types = 1);


use App\Lib\Enum\DateTypeEnum;
use Cake\Utility\Inflector;

// $columns = the passed parameter $indexColumns as found in columns.inc;
// provides overrides for labels and sorting.
$columns = $vv_indexColumns;

?>

<div class="top-filters-fields-subgroups">
<?php if(!empty($field_datetime_columns)): ?>
<?php foreach($field_datetime_columns as $key => $options): ?>
<div class="input">
<div class="top-search-date-label">
@@ -42,18 +54,18 @@
<?php
// Create a text field to hold our value.
print $this->Form->label("{$key}_starts_at", __d('field', 'starts_at'), ['class' => 'filter-datepicker-lbl']);
print $this->Field->dateField("{$key}_starts_at", DateTypeEnum::DateOnly, $query);
print $this->Field->dateField("{$key}_starts_at", DateTypeEnum::DateOnly);
?>
</div>
<!-- Ends at -->
<div class="top-search-end-date">
<?php
// Create a text field to hold our value.
print $this->Form->label("{$key}_ends_at", __d('field','ends_at'), ['class' => 'filter-datepicker-lbl']);
print $this->Field->dateField("{$key}_ends_at", DateTypeEnum::DateOnly, $query);
print $this->Field->dateField("{$key}_ends_at", DateTypeEnum::DateOnly);
?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>

0 comments on commit f959a8a

Please sign in to comment.