diff --git a/application/modules/opac/controllers/BibController.php b/application/modules/opac/controllers/BibController.php index d5d764f1dbf42e7d615a167a6a0dccefce852207..daa0f69013203058788287f9af0756dacdf9f959 100644 --- a/application/modules/opac/controllers/BibController.php +++ b/application/modules/opac/controllers/BibController.php @@ -220,10 +220,8 @@ class BibController extends ZendAfi_Controller_Action { $id_division = (int)$this->_getParam('id_division'); $page = (int)$this->_getParam('page'); - $default_filters = $this->_getDefaultFilters(); - - $helper = (new ZendAfi_View_Helper_Accueil_Library($id_module, Class_Profil::getCurrentProfil() - ->getBoitesDivision($id_division)[$id_module])) + $helper = (new ZendAfi_View_Helper_Accueil_Library($id_module, + $this->_getSettingsFor($id_division, $id_module))) ->setView($this->view) ->setPage($page) ->setSelectedFilters($this->_getSelectedFilters()); @@ -238,72 +236,70 @@ class BibController extends ZendAfi_Controller_Action { } - protected function _getDefaultFilters() { - $id_module = (int)$this->_getParam('id_module'); - $id_division = (int)$this->_getParam('id_division'); - - $filter_keys = (new Class_Systeme_ModulesAccueil_Library())->getStaticFiltersKeys(); - $default_keys = array_map(function($key) - { - return Class_Systeme_ModulesAccueil_Library::ADD_DEFAULT_PREFIX . $key; - }, - $filter_keys); + protected function _getSettingsFor($id_division, $id_module) { + $settings = Class_Profil::getCurrentProfil() + ->getBoitesDivision($id_division)[$id_module]; - $defaults_to_save = []; + if(!Class_Users::isCurrentUserCanAccesBackend()) + return $settings; - foreach($this->_helper->selectedFilters($default_keys) as $key => $value) - $defaults_to_save[str_replace(Class_Systeme_ModulesAccueil_Library::ADD_DEFAULT_PREFIX, '', $key)] = $value; + $new_settings = $this->_addFiltersToDefault($this->_deleteFiltersFromDefault($settings)); - $default_keys_to_delete = array_map(function($key) - { - return Class_Systeme_ModulesAccueil_Library::DELETE_DEFAULT_PREFIX . $key; - }, - $filter_keys); + if (array_values($settings['preferences']['default_filters']) + != array_values($new_settings['preferences']['default_filters'])) + Class_Profil::getCurrentProfil() + ->updateModuleConfigAccueil($id_module, $new_settings) + ->save(); - $defaults_to_delete = []; + return $new_settings; + } - foreach($this->_helper->selectedFilters($default_keys_to_delete) as $key => $value) - $defaults_to_delete[str_replace(Class_Systeme_ModulesAccueil_Library::DELETE_DEFAULT_PREFIX, '', $key)] = $value; - $module_params = Class_Profil::getCurrentProfil() - ->getBoitesDivision($id_division)[$id_module]; + protected function _addFiltersToDefault($settings) { + return $this->_updateDefaultFilters($settings, + Class_Systeme_ModulesAccueil_Library::ADD_DEFAULT_PREFIX, + function($filters_to_save, $saved_filters) + { + return $filters_to_save + $saved_filters; + }); + } - if($defaults_to_save && Class_Users::isCurrentUserCanAccesBackend()) { - if(!isset($module_params['preferences']['default_filters'])) - $module_params['preferences']['default_filters'] = []; + protected function _deleteFiltersFromDefault($settings) { + return $this->_updateDefaultFilters($settings, + Class_Systeme_ModulesAccueil_Library::DELETE_DEFAULT_PREFIX, + function($filters_to_delete, $saved_filters) + { + return array_filter($saved_filters, + function($filter) use ($filters_to_delete) + { + foreach($filters_to_delete as $key => $value) + return !($value == $filter); + }); + }); + } - $module_params['preferences']['default_filters'] = $defaults_to_save + $module_params['preferences']['default_filters']; - Class_Profil::getCurrentProfil() - ->updateModuleConfigAccueil($id_module, $module_params) - ->save(); - } + protected function _updateDefaultFilters($settings, $prefix, $callback) { + $filter_keys = (new Class_Systeme_ModulesAccueil_Library())->getStaticFiltersKeys(); + $add_keys = array_map(function($key) use ($prefix) + { + return $prefix . $key; + }, + $filter_keys); - if($defaults_to_delete && Class_Users::isCurrentUserCanAccesBackend()) { - if(!isset($module_params['preferences']['default_filters'])) - continue; - - $module_params['preferences']['default_filters'] = array_filter($module_params['preferences']['default_filters'], - function($filter) use ($defaults_to_delete) - { - foreach($defaults_to_delete as $key => $value) - if ( $value == $filter) - return false; - return true; - }); - Class_Profil::getCurrentProfil() - ->updateModuleConfigAccueil($id_module, $module_params) - ->save(); - } + $filters = []; - $module_params = Class_Profil::getCurrentProfil() - ->getBoitesDivision($id_division)[$id_module]; + foreach($this->_helper->selectedFilters($add_keys) as $key => $value) + $filters[str_replace($prefix, '', $key)] = $value; - if(!isset($module_params['preferences']['default_filters'])) - return []; + if(!$filters) + return $settings; - return $module_params['preferences']['default_filters']; + $settings['preferences']['default_filters'] = call_user_func_array($callback, + [$filters, + $settings['preferences']['default_filters']]); + return $settings; } diff --git a/library/Class/Systeme/ModulesAccueil/Library.php b/library/Class/Systeme/ModulesAccueil/Library.php index 775477375bba3a55960f129e2544c2e8f2468b73..4c9c6a46ef9f589acab4af6f80ceb216a33484de 100644 --- a/library/Class/Systeme/ModulesAccueil/Library.php +++ b/library/Class/Systeme/ModulesAccueil/Library.php @@ -78,6 +78,7 @@ class Class_Systeme_ModulesAccueil_Library extends Class_Systeme_ModulesAccueil_ 'filters' => '', 'filters_display_modes' => self::DISPLAY_LIST, 'filters_position' => self::POSITION_RIGHT, + 'default_filters' => [], 'linked' => 0, 'osm_map' => 0];