- Added FiltersLib to contain all the shared logic of a FilterWidget

- Adapted Filters controller and FilterWidget to use this new lib
- Better memory usage in FilterWidget
- PhrasesLib now checks if categories is an empty array, it it is then avoid to call the method to load phrases (avoids an ugly error)
- Renamed the class FHC_PhraseLib to FHC_PhrasesLib (like the filename)
This commit is contained in:
Paolo
2018-06-08 14:36:40 +02:00
parent d6578277c2
commit 7d836a0147
8 changed files with 67 additions and 86 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ class FiltersLib
const SESSION_ADDITIONAL_COLUMNS = 'additionalColumns';
const SESSION_CHECKBOXES = 'checkboxes';
const SESSION_FILTERS = 'filters';
const SESSION_DATASET_METADATA = 'datasetMetadata';
const SESSION_METADATA = 'datasetMetadata';
const SESSION_DATASET = 'dataset';
const SESSION_ROW_NUMBER = 'rowNumber';
const SESSION_RELOAD_DATASET = 'reloadDataset';
+5 -7
View File
@@ -257,7 +257,8 @@ class PhrasesLib
$language = $this->_ci->PersonModel->getLanguage(getAuthUID());
}
$this->_setPhrases($categories, $language);
// If only categories is not an empty array then loads phrases
if (count($categories) > 0) $this->_setPhrases($categories, $language);
}
}
}
@@ -285,8 +286,7 @@ class PhrasesLib
{
// is assoc array -> Loads specific phrasentexte by category and phrases
$isIndexArray = false;
$phrases = $this->_ci->PhraseModel
->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language);
$phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language);
}
// If language is not default language and phrasentext is null -> fallback to default language
@@ -296,13 +296,11 @@ class PhrasesLib
$defaultPhrases = null;
if ($isIndexArray)
{
$defaultPhrases = $this->_ci->PhraseModel
->getPhrasesByCategoryAndLanguage($categories, DEFAULT_LANGUAGE);
$defaultPhrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, DEFAULT_LANGUAGE);
}
else
{
$defaultPhrases = $this->_ci->PhraseModel
->getPhrasesByCategoryAndPhrasesAndLanguage($categories, DEFAULT_LANGUAGE);
$defaultPhrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndPhrasesAndLanguage($categories, DEFAULT_LANGUAGE);
}
// combine array with phrasentexte in users language and in default language
+1 -1
View File
@@ -96,7 +96,7 @@ function _generateJSPhrasesStorageObject($phrases)
$toPrint = "\n";
$toPrint .= '<script type="text/javascript">';
$toPrint .= "\n";
$toPrint .= 'var FHC_JS_PHRASES_STORAGE_OBJECT = '.$ci->pj->getJSON();
$toPrint .= ' var FHC_JS_PHRASES_STORAGE_OBJECT = '.$ci->pj->getJSON().';';
$toPrint .= "\n";
$toPrint .= '</script>';
$toPrint .= "\n\n";
+39 -46
View File
@@ -283,15 +283,12 @@ 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
$this->_formatDataset($dataset); // marks rows using markRow and format rowns using formatRow
// Set the new dataset and it's attributes in the session
$this->filterslib->setElementSession(
FiltersLib::SESSION_DATASET_METADATA,
$this->FiltersModel->getExecutedQueryMetaData()
);
// Set the new dataset and its attributes in the session
$this->filterslib->setElementSession(FiltersLib::SESSION_METADATA, $this->FiltersModel->getExecutedQueryMetaData());
$this->filterslib->setElementSession(FiltersLib::SESSION_ROW_NUMBER, count($dataset->retval));
$this->filterslib->setElementSession(FiltersLib::SESSION_DATASET, $formattedDataset);
$this->filterslib->setElementSession(FiltersLib::SESSION_DATASET, $dataset->retval);
}
}
}
@@ -324,7 +321,7 @@ 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
$this->_formatDataset($dataset); // marks rows using markRow and format rowns using formatRow
// Stores an array that contains all the data useful for
$this->filterslib->setSession(
@@ -341,90 +338,86 @@ class FilterWidget extends Widget
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_METADATA => $this->FiltersModel->getExecutedQueryMetaData(), //
FiltersLib::SESSION_ROW_NUMBER => count($dataset->retval), //
FiltersLib::SESSION_DATASET => $formattedDataset, //
FiltersLib::SESSION_DATASET => $dataset->retval, //
FiltersLib::SESSION_RELOAD_DATASET => false //
)
);
}
}
}
// var_dump($this->filterslib->getSession());
}
/**
*
* Calls the method _markRow and _formatRow to marks rows using markRow and format rowns using formatRow
* NOTE: this method operates directly on the retrived dataset: parameter passed by reference
*/
private function _formatDataset($rawDataset)
private function _formatDataset(&$rawDataset)
{
$formattedDataset = null;
if (hasData($rawDataset) && is_array($rawDataset->retval))
{
$formattedDataset = array();
// For each row of the data set
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;
// Calls the methods to mark and to format a row
// NOTE: keep this order! the markRow function given as parameter is supposing to work
// on a raw dataset, NOT on a formatted one
$rawDataset->retval[$rowCounter]->MARK_ROW_CLASS = $this->_markRow($rawDataset->retval[$rowCounter]);
$this->_formatRow($rawDataset->retval[$rowCounter]);
}
}
return $formattedDataset;
}
/**
*
* Formats the columns of all the rows of the entire dataset
* - converts booleans into strings "true" and "false"
* - format dates using the format string defined in DEFAULT_DATE_FORMAT
* Calls the parameter formatRow if it was given and if it is a valid funtion
* NOTE: this method operates directly on the retrived dataset: parameter passed by reference
*/
private function _formatRow($datasetRaw)
private function _formatRow(&$rawDatasetRow)
{
$tmpDatasetRaw = clone $datasetRaw;
foreach ($tmpDatasetRaw as $columnName => $columnValue)
// For each column of the row
foreach ($rawDatasetRow as $columnName => $columnValue)
{
// Basic conversions
if (is_bool($columnValue))
{
$tmpDatasetRaw->{$columnValue} = $columnValue === true ? 'true' : 'false';
$rawDatasetRow->{$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));
$rawDatasetRow->{$columnValue} = date(self::DEFAULT_DATE_FORMAT, strtotime($columnValue));
}
}
if ($this->_formatRow != null)
// If a valid function call the given formatRow
if ($this->_formatRow != null && is_callable($this->_formatRow))
{
$formatRow = $this->_formatRow;
$tmpDatasetRaw = $formatRow($tmpDatasetRaw);
$formatRowFunction = $this->_formatRow;
$rawDatasetRow = $formatRowFunction($rawDatasetRow);
}
return $tmpDatasetRaw;
}
/**
*
* Returns a string that contains a class name used to mark rows in the dataset table
* Calls the parameter markRow if it was given and if it is a valid funtion
*/
private function _markRow($datasetRaw)
private function _markRow($rawDatasetRow)
{
if (is_object($datasetRaw))
// If a valid function call the given markRow
if ($this->_markRow != null && is_callable($this->_markRow))
{
if ($this->_markRow != null)
{
$markRow = $this->_markRow;
$class = $markRow($datasetRaw);
}
$markRowFunction = $this->_markRow;
$class = $markRowFunction($rawDatasetRow);
}
return !isset($class) ? '' : $class;
}
/**
*
* Utility method that retrives the name of the columns present in a filter JSON definition
*/
private function _getColumnsNames($columns)
{