Changes to the FilterWidget III

This commit is contained in:
Paolo
2018-06-06 11:46:26 +02:00
parent 20bffadbc4
commit d63a092287
11 changed files with 774 additions and 547 deletions
+145 -354
View File
@@ -49,6 +49,150 @@ class Filters extends FHC_Controller
$this->outputJsonSuccess($rowNumber);
}
/**
*
*/
public function sortSelectedFields()
{
$selectedFields = $this->input->post('selectedFields');
if ($this->filterslib->sortSelectedFields($selectedFields) == true)
{
$this->getFilter();
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function removeSelectedField()
{
$selectedField = $this->input->post('selectedField');
if ($this->filterslib->removeSelectedField($selectedField) == true)
{
$this->getFilter();
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function addSelectedField()
{
$selectedField = $this->input->post('selectedField');
if ($this->filterslib->addSelectedField($selectedField) == true)
{
$this->getFilter();
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function removeAppliedFilter()
{
$appliedFilter = $this->input->post('appliedFilter');
if ($this->filterslib->removeAppliedFilter($appliedFilter) == true)
{
$this->outputJsonSuccess('Removed');
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function applyFilters()
{
$appliedFilters = $this->input->post('appliedFilters');
$appliedFiltersOperations = $this->input->post('appliedFiltersOperations');
$appliedFiltersConditions = $this->input->post('appliedFiltersConditions');
$appliedFiltersOptions = $this->input->post('appliedFiltersOptions');
if ($this->filterslib->applyFilters(
$appliedFilters,
$appliedFiltersOperations,
$appliedFiltersConditions,
$appliedFiltersOptions
) == true)
{
$this->outputJsonSuccess('Applied');
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function addFilter()
{
$filter = $this->input->post('filter');
if ($this->filterslib->addFilter($filter) == true)
{
$this->getFilter();
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function saveCustomFilter()
{
$customFilterDescription = $this->input->post('customFilterDescription');
if ($this->filterslib->saveCustomFilter($customFilterDescription) == true)
{
$this->outputJsonSuccess('Saved');
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
/**
*
*/
public function removeCustomFilter()
{
$filter_id = $this->input->post('filter_id');
if ($this->filterslib->removeCustomFilter($filter_id) == true)
{
$this->outputJsonSuccess('Removed');
}
else
{
$this->outputJsonError('Wrong parameter');
}
}
//------------------------------------------------------------------------------------------------------------------
// Private methods
@@ -77,361 +221,8 @@ class Filters extends FHC_Controller
}
else // Otherwise an error will be written in the output
{
$this->outputJsonError('Parameter '.self::FILTER_PAGE_PARAM.' not provided!');
echo 'Parameter "'.self::FILTER_PAGE_PARAM.'" not provided!';
exit;
}
}
// /**
// *
// */
// public function sortSelectedFields()
// {
// $selectedFieldsLst = $this->input->post('selectedFieldsLst');
//
// $filterUniqueId = $this->_getFilterUniqueId();
//
// $json = new stdClass();
//
// $session = $this->_readSession($filterUniqueId);
//
// $allSelectedFields = $this->_getFromSession('allSelectedFields');
// $allColumnsAliases = $this->_getFromSession('allColumnsAliases');
//
// $json->selectedFields = $this->_getFromSession('selectedFields');
// $json->columnsAliases = $this->_getFromSession('columnsAliases');
//
// if (isset($selectedFieldsLst) && is_array($selectedFieldsLst))
// {
// $json->selectedFields = $selectedFieldsLst;
// $json->columnsAliases = array();
//
// for ($i = 0; $i < count($json->selectedFields); $i++)
// {
// $pos = array_search($json->selectedFields[$i], $allSelectedFields);
//
// $json->columnsAliases[$i] = $json->selectedFields[$i];
//
// if ($pos !== false)
// {
// if ($allColumnsAliases != null && is_array($allColumnsAliases))
// {
// $json->columnsAliases[$i] = $allColumnsAliases[$pos];
// }
// }
// }
// }
//
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['selectedFields'] = $json->selectedFields;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['columnsAliases'] = $json->columnsAliases;
//
// $this->output->set_content_type('application/json')->set_output(json_encode($json));
// }
//
// /**
// *
// */
// public function saveFilter()
// {
// $this->_saveFilter($this->input->post("customFilterDescription"), $this->_getFilterUniqueId());
//
// $this->output->set_content_type('application/json')->set_output(json_encode('Filter saved'));
// }
//
// /**
// *
// */
// private function _saveFilter($customFilterDescription, $filterUniqueId)
// {
// $objToBeSaved = new stdClass();
//
// $filterSessionArray = $this->_readSession($filterUniqueId);
//
// $objToBeSaved->name = $customFilterDescription;
//
// if (isset($filterSessionArray[self::SELECTED_FIELDS]))
// {
// $selectedFields = $filterSessionArray[self::SELECTED_FIELDS];
// $objToBeSaved->columns = array();
//
// for ($selectedFieldsCounter = 0; $selectedFieldsCounter < count($selectedFields); $selectedFieldsCounter++)
// {
// $objToBeSaved->columns[$selectedFieldsCounter] = new stdClass();
// $objToBeSaved->columns[$selectedFieldsCounter]->name = $selectedFields[$selectedFieldsCounter];
// }
// }
//
// if (isset($filterSessionArray[self::SELECTED_FILTERS]))
// {
// $selectedFilters = $filterSessionArray[self::SELECTED_FILTERS];
// $objToBeSaved->filters = array();
//
// for ($selectedFiltersCounter = 0; $selectedFiltersCounter < count($selectedFilters); $selectedFiltersCounter++)
// {
// $objToBeSaved->filters[$selectedFiltersCounter] = new stdClass();
// $objToBeSaved->filters[$selectedFiltersCounter]->name = $selectedFilters[$selectedFiltersCounter];
//
// if (isset($filterSessionArray[self::ACTIVE_FILTERS])
// && isset($filterSessionArray[self::ACTIVE_FILTERS][$selectedFilters[$selectedFiltersCounter]]))
// {
// $objToBeSaved->filters[$selectedFiltersCounter]->condition = $filterSessionArray[self::ACTIVE_FILTERS][$selectedFilters[$selectedFiltersCounter]];
// }
//
// if (isset($filterSessionArray[self::ACTIVE_FILTERS_OPERATION])
// && isset($filterSessionArray[self::ACTIVE_FILTERS_OPERATION][$selectedFilters[$selectedFiltersCounter]]))
// {
// $objToBeSaved->filters[$selectedFiltersCounter]->operation = $filterSessionArray[self::ACTIVE_FILTERS_OPERATION][$selectedFilters[$selectedFiltersCounter]];
// }
//
// if (isset($filterSessionArray[self::ACTIVE_FILTERS_OPTION])
// && isset($filterSessionArray[self::ACTIVE_FILTERS_OPTION][$selectedFilters[$selectedFiltersCounter]]))
// {
// $objToBeSaved->filters[$selectedFiltersCounter]->option = $filterSessionArray[self::ACTIVE_FILTERS_OPTION][$selectedFilters[$selectedFiltersCounter]];
// }
// }
// }
//
// $desc = $customFilterDescription;
// $descPGArray = '{"'.$desc.'", "'.$desc.'", "'.$desc.'", "'.$desc.'"}';
//
// $resultBenutzer = $this->BenutzerModel->load(getAuthUID());
// $personId = $resultBenutzer->retval[0]->person_id;
//
// $result = $this->FiltersModel->loadWhere(array(
// 'app' => $_SESSION[self::SESSION_NAME][$filterUniqueId]['app'],
// 'dataset_name' => $_SESSION[self::SESSION_NAME][$filterUniqueId]['datasetName'],
// 'description' => $descPGArray,
// 'person_id' => $personId
// ));
//
// if (hasData($result))
// {
// $this->FiltersModel->update(
// array(
// 'app' => $_SESSION[self::SESSION_NAME][$filterUniqueId]['app'],
// 'dataset_name' => $_SESSION[self::SESSION_NAME][$filterUniqueId]['datasetName'],
// 'description' => $descPGArray,
// 'person_id' => $personId
// ),
// array(
// 'filter' => json_encode($objToBeSaved)
// )
// );
// }
// else
// {
// $this->FiltersModel->insert(array(
// 'app' => $_SESSION[self::SESSION_NAME][$filterUniqueId]['app'],
// 'dataset_name' => $_SESSION[self::SESSION_NAME][$filterUniqueId]['datasetName'],
// 'filter_kurzbz' => uniqid($personId, true),
// 'person_id' => $personId,
// 'description' => $descPGArray,
// 'sort' => null,
// 'default_filter' => false,
// 'filter' => json_encode($objToBeSaved),
// 'oe_kurzbz' => null
// ));
// }
// }
//
// /**
// *
// */
// public function deleteCustomFilter()
// {
// $filter_id = $this->input->post('filter_id');
//
// if (is_numeric($filter_id))
// {
// $this->FiltersModel->deleteCustomFilter($filter_id);
//
// $this->output->set_content_type('application/json')->set_output(json_encode('Removed'));
// }
// }
//
// /**
// *
// */
// public function removeSelectedFields()
// {
// $fieldName = $this->input->post('fieldName');
// $filterUniqueId = $this->_getFilterUniqueId();
//
// $session = $this->_readSession($filterUniqueId);
//
// $allSelectedFields = $this->_getFromSession('allSelectedFields');
// $allColumnsAliases = $this->_getFromSession('allColumnsAliases');
//
// $selectedFields = $this->_getFromSession('selectedFields');
// $columnsAliases = $this->_getFromSession('columnsAliases');
//
// if (($pos = array_search($fieldName, $selectedFields)) !== false)
// {
// array_splice($selectedFields, $pos, 1);
//
// if ($columnsAliases != null && is_array($columnsAliases))
// {
// array_splice($columnsAliases, $pos, 1);
// }
// }
//
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['selectedFields'] = $selectedFields;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['columnsAliases'] = $columnsAliases;
//
// $json = new stdClass();
//
// $json->allSelectedFields = $allSelectedFields;
// $json->allColumnsAliases = $allColumnsAliases;
// $json->selectedFields = $selectedFields;
// $json->columnsAliases = $columnsAliases;
//
// $this->output->set_content_type('application/json')->set_output(json_encode($json));
// }
//
// /**
// *
// */
// public function removeSelectedFilters()
// {
// $fieldName = $this->input->post('fieldName');
// $filterUniqueId = $this->_getFilterUniqueId();
//
// $session = $this->_readSession($filterUniqueId);
//
// $selectedFilters = $this->_getFromSession('selectedFilters');
// $selectedFiltersActiveFilters = $this->_getFromSession('activeFilters');
// $selectedFiltersActiveFiltersOperation = $this->_getFromSession('activeFiltersOperation');
// $selectedFiltersActiveFiltersOption = $this->_getFromSession('activeFiltersOption');
//
// if (($pos = array_search($fieldName, $selectedFilters)) !== false)
// {
// array_splice($selectedFilters, $pos, 1);
// array_splice($selectedFiltersActiveFilters, $pos, 1);
// array_splice($selectedFiltersActiveFiltersOperation, $pos, 1);
// array_splice($selectedFiltersActiveFiltersOption, $pos, 1);
// }
//
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['selectedFilters'] = $selectedFilters;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFilters'] = $selectedFiltersActiveFilters;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFiltersOperation'] = $selectedFiltersActiveFiltersOperation;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFiltersOption'] = $selectedFiltersActiveFiltersOption;
//
// $json = new stdClass();
//
// $json->selectedFilters = $selectedFilters;
// $json->selectedFiltersActiveFilters = $selectedFiltersActiveFilters;
// $json->selectedFiltersActiveFiltersOperation = $selectedFiltersActiveFiltersOperation;
// $json->selectedFiltersActiveFiltersOption = $selectedFiltersActiveFiltersOption;
//
// $this->output->set_content_type('application/json')->set_output(json_encode($json));
// }
//
// /**
// *
// */
// public function addSelectedFields()
// {
// $fieldName = $this->input->post('fieldName');
// $filterUniqueId = $this->_getFilterUniqueId();
//
// $session = $this->_readSession($filterUniqueId);
//
// $allSelectedFields = $this->_getFromSession('allSelectedFields');
// $allColumnsAliases = $this->_getFromSession('allColumnsAliases');
//
// $selectedFields = $this->_getFromSession('selectedFields');
// $columnsAliases = $this->_getFromSession('columnsAliases');
//
// if (($pos = array_search($fieldName, $allSelectedFields)) !== false
// && array_search($fieldName, $selectedFields) === false)
// {
// array_push($selectedFields, $fieldName);
//
// if ($columnsAliases != null && is_array($columnsAliases))
// {
// array_push($columnsAliases, $allColumnsAliases[$pos]);
// }
// }
//
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['selectedFields'] = $selectedFields;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['columnsAliases'] = $columnsAliases;
//
// $json = new stdClass();
//
// $json->allSelectedFields = $allSelectedFields;
// $json->allColumnsAliases = $allColumnsAliases;
// $json->selectedFields = $selectedFields;
// $json->columnsAliases = $columnsAliases;
//
// $this->output->set_content_type('application/json')->set_output(json_encode($json));
// }
//
// /**
// *
// */
// public function addSelectedFilters()
// {
// $fieldName = $this->input->post('fieldName');
// $filterUniqueId = $this->_getFilterUniqueId();
//
// $session = $this->_readSession($filterUniqueId);
//
// $selectedFilters = $this->_getFromSession('selectedFilters');
// $selectedFiltersActiveFilters = $this->_getFromSession('activeFilters');
// $selectedFiltersActiveFiltersOperation = $this->_getFromSession('activeFiltersOperation');
// $selectedFiltersActiveFiltersOption = $this->_getFromSession('activeFiltersOption');
//
// if (!in_array($fieldName, $selectedFilters))
// {
// array_push($selectedFilters, $fieldName);
// $selectedFiltersActiveFilters[$fieldName] = "";
// $selectedFiltersActiveFiltersOperation[$fieldName] = "";
// $selectedFiltersActiveFiltersOption[$fieldName] = "";
// }
//
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['selectedFilters'] = $selectedFilters;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFilters'] = $selectedFiltersActiveFilters;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFiltersOperation'] = $selectedFiltersActiveFiltersOperation;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFiltersOption'] = $selectedFiltersActiveFiltersOption;
//
// $json = new stdClass();
//
// $json->selectedFilters = $selectedFilters;
// $json->selectedFiltersActiveFilters = $selectedFiltersActiveFilters;
// $json->selectedFiltersActiveFiltersOperation = $selectedFiltersActiveFiltersOperation;
// $json->selectedFiltersActiveFiltersOption = $selectedFiltersActiveFiltersOption;
//
// $this->output->set_content_type('application/json')->set_output(json_encode($json));
// }
//
// /**
// *
// */
// public function applyFilter()
// {
// $fieldNames = $this->input->post('filterNames');
// $filterOperations = $this->input->post('filterOperations');
// $filterOperationValues = $this->input->post('filterOperationValues');
// $filterOptions = $this->input->post('filterOptions');
// $filterUniqueId = $this->_getFilterUniqueId();
//
// $session = $this->_readSession($filterUniqueId);
//
// $activeFilters = array_combine($fieldNames, $filterOperationValues);
// $activeFiltersOperation = array_combine($fieldNames, $filterOperations);
// $activeFiltersOption = array_combine($fieldNames, $filterOptions);
//
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFilters'] = $activeFilters;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFiltersOperation'] = $activeFiltersOperation;
// $_SESSION[self::SESSION_NAME][$filterUniqueId]['activeFiltersOption'] = $activeFiltersOption;
//
// $json = new stdClass();
//
// $json->fieldNames = $fieldNames;
// $json->activeFilters = $activeFilters;
// $json->activeFiltersOperation = $activeFiltersOperation;
// $json->activeFiltersOption = $activeFiltersOption;
//
// $this->output->set_content_type('application/json')->set_output(json_encode($json));
// }
}
@@ -744,7 +744,7 @@ class InfoCenter extends FHC_Controller
'link' => sprintf($toPrint, site_url('system/infocenter/InfoCenter'), 'filter_id', $filterId),
'description' => $description,
'subscriptDescription' => 'Remove',
'subscriptLinkClass' => 'remove-filter',
'subscriptLinkClass' => 'remove-custom-filter',
'subscriptLinkValue' => $filterId
);
}
+359 -42
View File
@@ -19,6 +19,7 @@ class FiltersLib
const SESSION_DATASET_METADATA = 'datasetMetadata';
const SESSION_DATASET = 'dataset';
const SESSION_ROW_NUMBER = 'rowNumber';
const SESSION_RELOAD_DATASET = 'reloadDataset';
// Alias for the dynamic table used to retrive the dataset
const DATASET_TABLE_ALIAS = 'datasetFilterTable';
@@ -28,10 +29,10 @@ class FiltersLib
const FHC_CONTROLLER_ID = 'fhc_controller_id';
// ...to identify a single filter widget in the DB
const FILTER_ID = 'filter_id';
const APP_PARAMETER = 'app';
const DATASET_NAME_PARAMETER = 'datasetName';
const FILTER_KURZBZ_PARAMETER = 'filterKurzbz';
const FILTER_ID = 'filter_id';
// ...stament to retrive the dataset
const QUERY_PARAMETER = 'query';
@@ -42,7 +43,7 @@ class FiltersLib
const COLUMNS_ALIASES = 'columnsAliases';
// ...to format/mark records of a dataset
const FORMAT_RAW = 'formatRaw';
const FORMAT_ROW = 'formatRow';
const MARK_ROW = 'markRow';
// ...to hide the options for a filter
@@ -88,40 +89,6 @@ class FiltersLib
//------------------------------------------------------------------------------------------------------------------
// Public methods
/**
* Return an unique string that identify this filter widget
* NOTE: The default value is the URI where the FilterWidget is called
* If the fhc_controller_id is present then is also used
*/
private function _getFilterUniqueId($params)
{
//
if ($params != null
&& is_array($params)
&& isset($params[self::FILTER_PAGE_PARAM])
&& !empty(trim($params[self::FILTER_PAGE_PARAM])))
{
$filterUniqueId = $params[self::FILTER_PAGE_PARAM];
}
else
{
// Gets the current page URI
$filterUniqueId = $this->_ci->router->directory.$this->_ci->router->class.'/'.$this->_ci->router->method;
}
// If the FHC_CONTROLLER_ID parameter is present in the HTTP GET
if (isset($_GET[self::FHC_CONTROLLER_ID]))
{
$filterUniqueId .= '/'.$this->_ci->input->get(self::FHC_CONTROLLER_ID); // then use it
}
elseif (isset($_POST[self::FHC_CONTROLLER_ID])) // else if the FHC_CONTROLLER_ID parameter is present in the HTTP POST
{
$filterUniqueId .= '/'.$this->_ci->input->post(self::FHC_CONTROLLER_ID); // then use it
}
return $filterUniqueId;
}
/**
* Returns the whole session for this filter widget if found, otherwise null
*/
@@ -294,21 +261,19 @@ class FiltersLib
/**
* Generate the query to retrive the dataset for a filter
* NOTE: filterJson should be already checked using the method parseFilterJson
*/
public function generateDatasetQuery($query, $filterJson)
public function generateDatasetQuery($query, $filters)
{
$datasetQuery = null;
// If the given query is valid and the json of the filter is valid
if (!empty(trim($query)) && $filterJson != null)
if (!empty(trim($query)) && $filters != null && is_array($filters))
{
$definedFilters = $filterJson->filters;
$where = '';
for ($filtersCounter = 0; $filtersCounter < count($definedFilters); $filtersCounter++)
for ($filtersCounter = 0; $filtersCounter < count($filters); $filtersCounter++)
{
$filterDefinition = $definedFilters[$filtersCounter];
$filterDefinition = $filters[$filtersCounter];
if ($filtersCounter > 0) $where .= ' AND ';
@@ -367,9 +332,361 @@ class FiltersLib
return $filterName;
}
/**
*
*/
public function sortSelectedFields($selectedFields)
{
$sortSelectedFields = false;
if (isset($selectedFields) && is_array($selectedFields) && count($selectedFields) > 0)
{
$fields = $this->getElementSession(self::SESSION_FIELDS);
if (!array_diff($selectedFields, $fields))
{
$this->setElementSession(self::SESSION_SELECTED_FIELDS, $selectedFields);
$sortSelectedFields = true;
}
}
return $sortSelectedFields;
}
/**
*
*/
public function removeSelectedField($selectedField)
{
$removeSelectedField = false;
if (isset($selectedField) && !empty(trim($selectedField)))
{
$fields = $this->getElementSession(self::SESSION_FIELDS);
$selectedFields = $this->getElementSession(self::SESSION_SELECTED_FIELDS);
if (in_array($selectedField, $fields))
{
if (($pos = array_search($selectedField, $selectedFields)) !== false)
{
array_splice($selectedFields, $pos, 1);
}
$this->setElementSession(self::SESSION_SELECTED_FIELDS, $selectedFields);
$removeSelectedField = true;
}
}
return $removeSelectedField;
}
/**
*
*/
public function addSelectedField($selectedField)
{
$removeSelectedField = false;
if (isset($selectedField) && !empty(trim($selectedField)))
{
$fields = $this->getElementSession(self::SESSION_FIELDS);
$selectedFields = $this->getElementSession(self::SESSION_SELECTED_FIELDS);
if (in_array($selectedField, $fields))
{
array_push($selectedFields, $selectedField);
$this->setElementSession(self::SESSION_SELECTED_FIELDS, $selectedFields);
$removeSelectedField = true;
}
}
return $removeSelectedField;
}
/**
*
*/
public function removeAppliedFilter($appliedFilter)
{
$removeAppliedFilter = false;
if (isset($appliedFilter) && !empty(trim($appliedFilter)))
{
$fields = $this->getElementSession(self::SESSION_FIELDS);
$filters = $this->getElementSession(self::SESSION_FILTERS);
if (in_array($appliedFilter, $fields))
{
$pos = false;
for($i = 0; $i < count($filters); $i++)
{
if ($filters[$i]->name == $appliedFilter)
{
$pos = $i;
break;
}
}
if ($pos !== false)
{
array_splice($filters, $pos, 1);
}
$this->setElementSession(self::SESSION_FILTERS, $filters);
$this->setElementSession(self::SESSION_RELOAD_DATASET, true);
$removeAppliedFilter = true;
}
}
return $removeAppliedFilter;
}
/**
*
*/
public function applyFilters($appliedFilters, $appliedFiltersOperations, $appliedFiltersConditions, $appliedFiltersOptions)
{
$applyFilters = false;
if (isset($appliedFilters) && is_array($appliedFilters)
&& isset($appliedFiltersOperations) && is_array($appliedFiltersOperations))
{
$fields = $this->getElementSession(self::SESSION_FIELDS);
if (!array_diff($appliedFilters, $fields))
{
$filters = array();
for ($i = 0; $i < count($appliedFilters); $i++)
{
$filterDefinition = new stdClass();
$filterDefinition->name = $appliedFilters[$i];
$filterDefinition->operation = $appliedFiltersOperations[$i];
$filterDefinition->condition = null;
if (isset($appliedFiltersConditions) && isset($appliedFiltersConditions[$i]))
{
$filterDefinition->condition = $appliedFiltersConditions[$i];
}
$filterDefinition->option = null;
if (isset($appliedFiltersOptions) && isset($appliedFiltersOptions[$i]))
{
$filterDefinition->option = $appliedFiltersOptions[$i];
}
$filters[$i] = $filterDefinition;
}
$this->setElementSession(self::SESSION_FILTERS, $filters);
$this->setElementSession(self::SESSION_RELOAD_DATASET, true);
$applyFilters = true;
}
}
return $applyFilters;
}
/**
*
*/
public function addFilter($filter)
{
$addFilter = false;
if (isset($filter) && !empty(trim($filter)))
{
$fields = $this->getElementSession(self::SESSION_FIELDS);
$filters = $this->getElementSession(self::SESSION_FILTERS);
if (in_array($filter, $fields))
{
$pos = false;
for($i = 0; $i < count($filters); $i++)
{
if ($filters[$i]->name == $filter)
{
$pos = $i;
break;
}
}
if ($pos === false)
{
$filterDefinition = new stdClass();
$filterDefinition->name = $filter;
$filterDefinition->operation = null;
$filterDefinition->condition = null;
$filterDefinition->option = null;
array_push($filters, $filterDefinition);
}
$this->setElementSession(self::SESSION_FILTERS, $filters);
$addFilter = true;
}
}
return $addFilter;
}
/**
*
*/
public function saveCustomFilter($customFilterDescription)
{
$saveCustomFilter = false; // by default returns a failure
// Checks parameter customFilterDescription
if (!isset($customFilterDescription) || empty(trim($customFilterDescription)))
{
return $saveCustomFilter;
}
$this->_ci->load->model('system/Filters_model', 'FiltersModel'); // to load the filter definitions
$this->_ci->load->model('person/Benutzer_model', 'BenutzerModel'); // to get the person_id of the authenticated user
$this->_ci->FiltersModel->resetQuery(); // reset any previous built query
$this->_ci->BenutzerModel->resetQuery(); // reset any previous built query
// Loads data for the authenticated user
$authBenutzer = $this->_ci->BenutzerModel->loadWhere(array('uid' => getAuthUID()));
if (hasData($authBenutzer)) // if data are found
{
// person_id of the authenticated user
$authPersonId = $authBenutzer->retval[0]->person_id;
// Postgres array for the description
$descPGArray = str_replace('%desc%', $customFilterDescription, '{"%desc%", "%desc%", "%desc%", "%desc%"}');
// Loads the definition to check if is already present in the DB
$definition = $this->_ci->FiltersModel->loadWhere(array(
'app' => $this->getElementSession(self::APP_PARAMETER),
'dataset_name' => $this->getElementSession(self::DATASET_NAME_PARAMETER),
'description' => $descPGArray,
'person_id' => $authPersonId
));
// New definition to be json encoded
$jsonDeifinition = new stdClass();
$jsonDeifinition->name = $customFilterDescription; // name of the filter
// Generates the "column" property
$jsonDeifinition->columns = array();
$selectedFields = $this->getElementSession(self::SESSION_SELECTED_FIELDS); // retrived the selected fields
for ($i = 0; $i < count($selectedFields); $i++)
{
// Each element is an object with a property called "name"
$jsonDeifinition->columns[$i] = new stdClass();
$jsonDeifinition->columns[$i]->name = $selectedFields[$i];
}
// List of applied filters
$jsonDeifinition->filters = $this->getElementSession(self::SESSION_FILTERS);
// If it is already present
if (hasData($definition))
{
// update it
$this->_ci->FiltersModel->update(
array(
'app' => $this->getElementSession(self::APP_PARAMETER),
'dataset_name' => $this->getElementSession(self::DATASET_NAME_PARAMETER),
'description' => $descPGArray,
'person_id' => $authPersonId
),
array(
'filter' => json_encode($jsonDeifinition)
)
);
$saveCustomFilter = true;
}
else // otherwise insert a new one
{
$this->_ci->FiltersModel->insert(
array(
'app' => $this->getElementSession(self::APP_PARAMETER),
'dataset_name' => $this->getElementSession(self::DATASET_NAME_PARAMETER),
'filter_kurzbz' => uniqid($authPersonId, true),
'description' => $descPGArray,
'person_id' => $authPersonId,
'sort' => null,
'default_filter' => false,
'filter' => json_encode($jsonDeifinition),
'oe_kurzbz' => null
)
);
$saveCustomFilter = true;
}
}
return $saveCustomFilter;
}
/**
*
*/
public function removeCustomFilter($filterId)
{
$removeCustomFilter = false;
if (isset($filterId) && is_numeric($filterId) && $filterId > 0)
{
$this->_ci->load->model('system/Filters_model', 'FiltersModel'); // to remove the filter definitions from DB
// delete it!
$this->_ci->FiltersModel->delete(array('filter_id' => $filterId));
$removeCustomFilter = true;
}
return $removeCustomFilter;
}
//------------------------------------------------------------------------------------------------------------------
// Private methods
/**
* Return an unique string that identify this filter widget
* NOTE: The default value is the URI where the FilterWidget is called
* If the fhc_controller_id is present then is also used
*/
private function _getFilterUniqueId($params)
{
//
if ($params != null
&& is_array($params)
&& isset($params[self::FILTER_PAGE_PARAM])
&& !empty(trim($params[self::FILTER_PAGE_PARAM])))
{
$filterUniqueId = $params[self::FILTER_PAGE_PARAM];
}
else
{
// Gets the current page URI
$filterUniqueId = $this->_ci->router->directory.$this->_ci->router->class.'/'.$this->_ci->router->method;
}
// If the FHC_CONTROLLER_ID parameter is present in the HTTP GET
if (isset($_GET[self::FHC_CONTROLLER_ID]))
{
$filterUniqueId .= '/'.$this->_ci->input->get(self::FHC_CONTROLLER_ID); // then use it
}
elseif (isset($_POST[self::FHC_CONTROLLER_ID])) // else if the FHC_CONTROLLER_ID parameter is present in the HTTP POST
{
$filterUniqueId .= '/'.$this->_ci->input->post(self::FHC_CONTROLLER_ID); // then use it
}
return $filterUniqueId;
}
/**
*
*/
@@ -214,7 +214,7 @@
ucfirst($this->p->t('global', 'gesperrtVon')),
ucfirst($this->p->t('global', 'parkdatum'))
),
'formatRaw' => function($datasetRaw) {
'formatRow' => function($datasetRaw) {
$datasetRaw->{'Details'} = sprintf(
'<a href="%s/%s?fhc_controller_id=%s">Details</a>',
@@ -169,7 +169,7 @@
'additionalColumns' => array('Details'),
'columnsAliases' => array('PersonID','Vorname','Nachname','GebDatum','Nation','Letzte Aktion','Letzter Bearbeiter',
'StSem','GesendetAm','NumAbgeschickt','Studiengänge','Sperrdatum','GesperrtVon'),
'formatRaw' => function($datasetRaw) {
'formatRow' => function($datasetRaw) {
$datasetRaw->{'Details'} = sprintf(
'<a href="%s/%s?fhc_controller_id=%s">Details</a>',
@@ -6,8 +6,6 @@
</span>
<span>
<select id="addField" class="drop-down-fields">
<option value="">Select a field to add...</option>
</select>
<select id="addField" class="drop-down-fields"></select>
</span>
</div>
@@ -8,9 +8,7 @@
</span>
<span>
<select id="addFilter" class="drop-down-filters">
<option value="">Select a filter to add...</option>
</select>
<select id="addFilter" class="drop-down-filters"></select>
</span>
<span>
+128 -16
View File
@@ -12,6 +12,9 @@ class FilterWidget extends Widget
const WIDGET_URL_SELECT_FILTERS = 'widgets/filter/selectFilters';
const WIDGET_URL_SAVE_FILTER = 'widgets/filter/saveFilter';
const DEFAULT_DATE_FORMAT = 'd.m.Y H:i:s';
const DEFAULT_MARK_ROW_CLASS = 'text-danger';
// Fitler info from DB
private $_app;
private $_datasetName;
@@ -26,7 +29,7 @@ class FilterWidget extends Widget
private $_columnsAliases;
// To format or mark rows of the dataset
private $_formatRaw;
private $_formatRow;
private $_markRow;
// To have a column in the GUI with checkboxes to select rows in the table
@@ -129,7 +132,7 @@ class FilterWidget extends Widget
$this->_query = null;
$this->_additionalColumns = null;
$this->_columnsAliases = null;
$this->_formatRaw = null;
$this->_formatRow = null;
$this->_markRow = null;
$this->_checkboxes = null;
$this->_hideHeader = null;
@@ -179,9 +182,9 @@ class FilterWidget extends Widget
}
// Parameter that contains a function to format the rows of the dataset
if (isset($args[FiltersLib::FORMAT_RAW]) && is_callable($args[FiltersLib::FORMAT_RAW]))
if (isset($args[FiltersLib::FORMAT_ROW]) && is_callable($args[FiltersLib::FORMAT_ROW]))
{
$this->_formatRaw = $args[FiltersLib::FORMAT_RAW];
$this->_formatRow = $args[FiltersLib::FORMAT_ROW];
}
// Parameter that contains a function to mark in the GUI the rows of the dataset
@@ -259,6 +262,39 @@ class FilterWidget extends Widget
$this->filterslib->setSession(null);
$session = null;
}
else // else if the filter loaded in session is the same that is being requested
{
// Get SESSION_RELOAD_DATASET from the session
$reloadDataset = $this->filterslib->getElementSession(FiltersLib::SESSION_RELOAD_DATASET);
if ($reloadDataset === true) // if it's value is very true, reload the dataset
{
// set as false to stop changing the dataset
$this->filterslib->setElementSession(FiltersLib::SESSION_RELOAD_DATASET, false);
// Generate dataset query using filters from the session
$datasetQuery = $this->filterslib->generateDatasetQuery(
$this->_query,
$this->filterslib->getElementSession(FiltersLib::SESSION_FILTERS)
);
// Then retrive dataset from DB
$dataset = $this->filterslib->getDataset($datasetQuery);
// Save changes into session if data are valid
if (!isError($dataset))
{
$formattedDataset = $this->_formatDataset($dataset); // format the dataset using markRow and formatRow
// Set the new dataset and it's attributes in the session
$this->filterslib->setElementSession(
FiltersLib::SESSION_DATASET_METADATA,
$this->FiltersModel->getExecutedQueryMetaData()
);
$this->filterslib->setElementSession(FiltersLib::SESSION_ROW_NUMBER, count($dataset->retval));
$this->filterslib->setElementSession(FiltersLib::SESSION_DATASET, $formattedDataset);
}
}
}
}
// If the session is empty
@@ -277,7 +313,7 @@ class FilterWidget extends Widget
if ($parsedFilterJson != null) // if the json is valid
{
// Generate dataset query
$datasetQuery = $this->filterslib->generateDatasetQuery($this->_query, $parsedFilterJson);
$datasetQuery = $this->filterslib->generateDatasetQuery($this->_query, $parsedFilterJson->filters);
// Then retrive dataset from DB
$dataset = $this->filterslib->getDataset($datasetQuery);
@@ -288,20 +324,27 @@ class FilterWidget extends Widget
// Save changes into session if data are valid
if (!isError($dataset))
{
$formattedDataset = $this->_formatDataset($dataset); // format the dataset using markRow and formatRow
// Stores an array that contains all the data useful for
$this->filterslib->setSession(
array(
FiltersLib::FILTER_ID => $this->_filterId,
FiltersLib::SESSION_FILTER_NAME => $filterName,
FiltersLib::SESSION_FIELDS => $this->FiltersModel->getExecutedQueryListFields(),
FiltersLib::SESSION_SELECTED_FIELDS => $this->_getColumnsNames($parsedFilterJson->columns),
FiltersLib::SESSION_COLUMNS_ALIASES => $this->_columnsAliases,
FiltersLib::SESSION_ADDITIONAL_COLUMNS => $this->_additionalColumns,
FiltersLib::SESSION_CHECKBOXES => $this->_checkboxes,
FiltersLib::SESSION_FILTERS => $parsedFilterJson->filters,
FiltersLib::SESSION_DATASET_METADATA => $this->FiltersModel->getExecutedQueryMetaData(),
FiltersLib::SESSION_ROW_NUMBER => count($dataset->retval),
FiltersLib::SESSION_DATASET => $dataset->retval
FiltersLib::FILTER_ID => $this->_filterId, //
FiltersLib::APP_PARAMETER => $this->_app, //
FiltersLib::DATASET_NAME_PARAMETER => $this->_datasetName, //
FiltersLib::SESSION_FILTER_NAME => $filterName, //
FiltersLib::SESSION_FIELDS => $this->FiltersModel->getExecutedQueryListFields(), //
FiltersLib::SESSION_SELECTED_FIELDS => $this->_getColumnsNames($parsedFilterJson->columns), //
FiltersLib::SESSION_COLUMNS_ALIASES => $this->_columnsAliases, //
FiltersLib::SESSION_ADDITIONAL_COLUMNS => $this->_additionalColumns, //
FiltersLib::SESSION_CHECKBOXES => $this->_checkboxes, //
FiltersLib::SESSION_FILTERS => $parsedFilterJson->filters, //
FiltersLib::SESSION_DATASET_METADATA => $this->FiltersModel->getExecutedQueryMetaData(), //
FiltersLib::SESSION_ROW_NUMBER => count($dataset->retval), //
FiltersLib::SESSION_DATASET => $formattedDataset, //
FiltersLib::SESSION_RELOAD_DATASET => false //
)
);
}
@@ -311,6 +354,75 @@ class FilterWidget extends Widget
// var_dump($this->filterslib->getSession());
}
/**
*
*/
private function _formatDataset($rawDataset)
{
$formattedDataset = null;
if (hasData($rawDataset) && is_array($rawDataset->retval))
{
$formattedDataset = array();
for ($rowCounter = 0; $rowCounter < count($rawDataset->retval); $rowCounter++)
{
$row = $rawDataset->retval[$rowCounter];
$formattedRow = $this->_formatRow($row);
$formattedRow->MARK_ROW_CLASS = $this->_markRow($row);
$formattedDataset[] = $formattedRow;
}
}
return $formattedDataset;
}
/**
*
*/
private function _formatRow($datasetRaw)
{
$tmpDatasetRaw = clone $datasetRaw;
foreach ($tmpDatasetRaw as $columnName => $columnValue)
{
if (is_bool($columnValue))
{
$tmpDatasetRaw->{$columnValue} = $columnValue === true ? 'true' : 'false';
}
elseif (DateTime::createFromFormat('Y-m-d G:i:s', $columnValue) !== false)
{
$tmpDatasetRaw->{$columnValue} = date(self::DEFAULT_DATE_FORMAT, strtotime($columnValue));
}
}
if ($this->_formatRow != null)
{
$formatRow = $this->_formatRow;
$tmpDatasetRaw = $formatRow($tmpDatasetRaw);
}
return $tmpDatasetRaw;
}
/**
*
*/
private function _markRow($datasetRaw)
{
if (is_object($datasetRaw))
{
if ($this->_markRow != null)
{
$markRow = $this->_markRow;
$class = $markRow($datasetRaw);
}
}
return !isset($class) ? '' : $class;
}
/**
*
*/