Skip to content

CFM-291-refactor_filtering_block #190

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 6 additions & 15 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,24 +182,16 @@ 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}';
$date_object = null;

if(isset($queryParams)) {
if(!empty($queryParams[$fieldName])) {
$date_object = FrozenTime::parse($queryParams[$fieldName]);
}
} else {
// This is an entity view. We are getting the data from the object
$entity = $this->getView()->get('vv_obj');
$date_object = $entity->$fieldName;
}
$queryParams = $this->getView()->getRequest()->getQueryParams();
$date_object = !empty($queryParams[$fieldName])
? FrozenTime::parse($queryParams[$fieldName])
: $this->getEntity()?->$fieldName;

// Create the options array for the (text input) form control
$coptions = [];
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>