From 7d836a0147da6ad66ac2f9e2a993a58f77fe7e33 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 8 Jun 2018 14:36:40 +0200 Subject: [PATCH] - 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) --- application/libraries/FiltersLib.php | 2 +- application/libraries/PhrasesLib.php | 12 ++- application/views/templates/FHC-Header.php | 2 +- application/widgets/FilterWidget.php | 85 ++++++++++------------ public/js/FilterWidget.js | 22 ++---- public/js/PhrasesLib.js | 6 +- public/js/infocenter/infocenterDetails.js | 22 +++--- public/js/tablesort/tablesort.js | 2 +- 8 files changed, 67 insertions(+), 86 deletions(-) diff --git a/application/libraries/FiltersLib.php b/application/libraries/FiltersLib.php index afe6bf9a8..a4703bf7d 100644 --- a/application/libraries/FiltersLib.php +++ b/application/libraries/FiltersLib.php @@ -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'; diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index e50fc4d22..f895cf637 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -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 diff --git a/application/views/templates/FHC-Header.php b/application/views/templates/FHC-Header.php index 33326cb56..81cbc9dcd 100755 --- a/application/views/templates/FHC-Header.php +++ b/application/views/templates/FHC-Header.php @@ -96,7 +96,7 @@ function _generateJSPhrasesStorageObject($phrases) $toPrint = "\n"; $toPrint .= ''; $toPrint .= "\n\n"; diff --git a/application/widgets/FilterWidget.php b/application/widgets/FilterWidget.php index e7b2a11a9..6350e8057 100644 --- a/application/widgets/FilterWidget.php +++ b/application/widgets/FilterWidget.php @@ -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) { diff --git a/public/js/FilterWidget.js b/public/js/FilterWidget.js index 621129ae8..c96a06590 100644 --- a/public/js/FilterWidget.js +++ b/public/js/FilterWidget.js @@ -60,11 +60,10 @@ var FHC_FilterWidget = { }, /** - * To refresh the whole FilterWidget GUI + * Alias call to method display only to inprove the readability of the code */ refresh: function() { - FHC_FilterWidget._resetFilterOptions(); FHC_FilterWidget.display(); }, @@ -114,19 +113,12 @@ var FHC_FilterWidget = { /** * To reset the Filter Options GUI */ - _resetFilterOptions: function() { + _resetGUI: function() { $("#dragAndDropFieldsArea").html(""); - $("#addField").html(""); + $("#addField").html(""); $("#appliedFilters").html(""); - $("#addFilter").html(""); - }, - - /** - * To reset the Filter Table GUI - */ - _resetTableDataset: function() { - + $("#addFilter").html(""); $("#filterTableDataset > thead > tr").html(""); $("#filterTableDataset > tbody").html(""); }, @@ -157,15 +149,13 @@ var FHC_FilterWidget = { * This method calls all the other methods needed to rendere the GUI for a FilterWidget * The parameter data contains all the data about the FilterWidget and it is given as parameter * to all the methods that here are called + * NOTE: think very carefully before changing the order of the calls */ _renderFilterWidget: function(data) { FHC_FilterWidget._initSessionStorage(); // initialize the session storage FHC_FilterWidget._turnOffEvents(); // turns all the events off - - // Reset the entire GUI - FHC_FilterWidget._resetFilterOptions(); - FHC_FilterWidget._resetTableDataset(); + FHC_FilterWidget._resetGUI(); // Reset the entire GUI // Render the GUI for this FilterWidget FHC_FilterWidget._setFilterName(data); // set the name in the GUI diff --git a/public/js/PhrasesLib.js b/public/js/PhrasesLib.js index 4818c7de4..d8c67d4f8 100644 --- a/public/js/PhrasesLib.js +++ b/public/js/PhrasesLib.js @@ -10,9 +10,9 @@ */ /** - * Definition and initialization of object FHC_PhraseLib + * Definition and initialization of object FHC_PhrasesLib */ -var FHC_PhraseLib = { +var FHC_PhrasesLib = { //------------------------------------------------------------------------------------------------------------------ // Public methods @@ -46,7 +46,7 @@ var FHC_PhraseLib = { params = []; } - return FHC_PhraseLib._replacePhraseVariable(phraseObj.text, params); // parsing + return FHC_PhrasesLib._replacePhraseVariable(phraseObj.text, params); // parsing } } } diff --git a/public/js/infocenter/infocenterDetails.js b/public/js/infocenter/infocenterDetails.js index 2fd8585f7..d4efed894 100644 --- a/public/js/infocenter/infocenterDetails.js +++ b/public/js/infocenter/infocenterDetails.js @@ -132,7 +132,7 @@ $(document).ready(function () var notizTitle = $(this).find("td:eq(1)").text(); var notizContent = this.title; - $("#notizform label:first").text(FHC_PhraseLib.t('infocenter', 'notizAendern')).css("color", "red"); + $("#notizform label:first").text(FHC_PhrasesLib.t('infocenter', 'notizAendern')).css("color", "red"); $("#notizform :input[type='reset']").css("display", "inline-block"); $("#notizform :input[name='hiddenNotizId']").val(notizId); @@ -241,7 +241,7 @@ var InfocenterDetails = { saveZgv: function(data) { var zgvError = function(){ - $("#zgvSpeichern_" + prestudentid).before("" + FHC_PhraseLib.t('ui', 'fehlerBeimSpeichern') + "  "); + $("#zgvSpeichern_" + prestudentid).before("" + FHC_PhrasesLib.t('ui', 'fehlerBeimSpeichern') + "  "); }; var prestudentid = data.prestudentid; @@ -256,7 +256,7 @@ var InfocenterDetails = { if (FHC_AjaxClient.hasData(data)) { InfocenterDetails._refreshLog(); - $("#zgvSpeichern_" + prestudentid).before("" + FHC_PhraseLib.t('ui', 'gespeichert') + "  "); + $("#zgvSpeichern_" + prestudentid).before("" + FHC_PhrasesLib.t('ui', 'gespeichert') + "  "); } else { @@ -387,11 +387,11 @@ var InfocenterDetails = { if (data.length > 0) InfocenterDetails.getParkedDate(personid); else - $("#unparkmsg").removeClass().addClass("text-warning").text(FHC_PhraseLib.t('infocenter', 'nichtsZumAusparken')); + $("#unparkmsg").removeClass().addClass("text-warning").text(FHC_PhrasesLib.t('infocenter', 'nichtsZumAusparken')); } }, errorCallback: function(){ - $("#unparkmsg").removeClass().addClass("text-danger").text(FHC_PhraseLib.t('infocenter', 'fehlerBeimAusparken')); + $("#unparkmsg").removeClass().addClass("text-danger").text(FHC_PhrasesLib.t('infocenter', 'fehlerBeimAusparken')); }, veilTimeout: 0 } @@ -435,8 +435,8 @@ var InfocenterDetails = { { $("#parking").html( '
'+ - '     '+ - FHC_PhraseLib.t('global', 'bis') + '  '+ + '     '+ + FHC_PhrasesLib.t('global', 'bis') + '  '+ ' '+ ''+ '
'); @@ -462,8 +462,8 @@ var InfocenterDetails = { var parkdate = $.datepicker.parseDate("yy-mm-dd", date); var gerparkdate = $.datepicker.formatDate("dd.mm.yy", parkdate); $("#parking").html( - FHC_PhraseLib.t('infocenter', 'bewerberGeparktBis')+'  '+gerparkdate+'     '+ - ' '+ + FHC_PhrasesLib.t('infocenter', 'bewerberGeparktBis')+'  '+gerparkdate+'     '+ + ' '+ '' ); @@ -486,11 +486,11 @@ var InfocenterDetails = { { $("#notizmsg").empty(); $("#notizform :input[name='hiddenNotizId']").val(""); - $("#notizform label:first").text(FHC_PhraseLib.t('infocenter', 'notizHinzufuegen')).css("color", "black"); + $("#notizform label:first").text(FHC_PhrasesLib.t('infocenter', 'notizHinzufuegen')).css("color", "black"); $("#notizform :input[type='reset']").css("display", "none"); }, _errorSaveNotiz: function() { - $("#notizmsg").text(FHC_PhraseLib.t('ui', 'fehlerBeimSpeichern')); + $("#notizmsg").text(FHC_PhrasesLib.t('ui', 'fehlerBeimSpeichern')); } }; diff --git a/public/js/tablesort/tablesort.js b/public/js/tablesort/tablesort.js index 1f901e74c..597d9e4a1 100644 --- a/public/js/tablesort/tablesort.js +++ b/public/js/tablesort/tablesort.js @@ -61,7 +61,7 @@ var Tablesort = { size: size, cssDisabled: 'disabled', savePages: false, - output: '{startRow} – {endRow} / {totalRows} ' + FHC_PhraseLib.t('global', 'zeilen') + output: '{startRow} – {endRow} / {totalRows} ' + FHC_PhrasesLib.t('global', 'zeilen') } ); }