From 4cc82af980b5bceb94298277c464d2953b4ed137 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Wed, 4 Dec 2013 18:18:39 +0100 Subject: [PATCH] Fix settings --- .../Phrasea/Controller/Client/Root.php | 24 +++--- lib/Alchemy/Phrasea/Controller/Prod/Query.php | 15 ++-- lib/Alchemy/Phrasea/Controller/Prod/Root.php | 2 +- .../Phrasea/Controller/User/Preferences.php | 2 +- .../Configuration/DisplaySettingService.php | 20 +++++ lib/Alchemy/Phrasea/Model/Entities/User.php | 73 ------------------- .../Model/Manipulator/UserManipulator.php | 32 +++++--- lib/classes/databox/cgu.php | 2 +- lib/classes/eventsmanager/notifyAbstract.php | 11 +-- templates/web/account/account.html.twig | 2 +- .../common/caption_templates/answer.html.twig | 2 +- .../internal_publi.html.twig | 2 +- .../caption_templates/lazaret.html.twig | 2 +- templates/web/prod/WorkZone/Macros.html.twig | 10 +-- .../web/prod/actions/edit_default.html.twig | 14 ++-- templates/web/prod/index.html.twig | 46 ++++++------ .../web/prod/results/answerabstract.html.twig | 2 +- .../web/prod/results/answerlist.html.twig | 2 +- templates/web/prod/results/help.html.twig | 2 +- templates/web/prod/results/record.html.twig | 8 +- .../Phrasea/Application/LightboxTest.php | 1 + .../Phrasea/Controller/Admin/UsersTest.php | 1 + .../Phrasea/Controller/Client/RootTest.php | 4 +- .../Phrasea/Controller/Prod/OrderTest.php | 7 +- .../Phrasea/Controller/Prod/PushTest.php | 2 + .../Phrasea/Controller/Prod/UploadTest.php | 1 + .../Phrasea/Controller/Root/LoginTest.php | 19 +++-- .../Controller/User/PreferencesTest.php | 15 ++-- .../Phrasea/Model/Manager/UserManagerTest.php | 4 +- .../Model/Manipulator/UserManipulatorTest.php | 8 +- tests/classes/PhraseanetPHPUnitAbstract.php | 20 +++++ tests/classes/userTest.php | 30 ++++---- .../__CG__AlchemyPhraseaModelEntitiesUser.php | 33 --------- 33 files changed, 186 insertions(+), 232 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Client/Root.php b/lib/Alchemy/Phrasea/Controller/Client/Root.php index ef3bb47e1a..c5bb2cf3ab 100644 --- a/lib/Alchemy/Phrasea/Controller/Client/Root.php +++ b/lib/Alchemy/Phrasea/Controller/Client/Root.php @@ -97,8 +97,8 @@ class Root implements ControllerProviderInterface $app['EM']->persist($userQuery); $app['EM']->flush(); - if ($app['authentication']->getUser()->getSettingValue('start_page') === 'LAST_QUERY') { - $app['authentication']->getUser()->setSettingValue('start_page_query', $query); + if ($app['settings']->getUserSetting($app['authentication']->getUser(), 'start_page') === 'LAST_QUERY') { + $app['manipulator.user']->setUserSetting($app['authentication']->getUser(), 'start_page_query', $query); } foreach ($options->getDataboxes() as $databox) { @@ -263,10 +263,10 @@ class Root implements ControllerProviderInterface 'module' => 'client', 'menubar' => $app['twig']->render('common/menubar.html.twig', ['module' => 'client']), 'css_file' => $this->getCssFile($app), - 'basket_status' => $app['authentication']->getUser()->getSettingValue('client_basket_status', '1'), - 'mod_pres' => $app['authentication']->getUser()->getSettingValue('client_view', '' ), - 'start_page' => $app['authentication']->getUser()->getSettingValue('start_page'), - 'start_page_query' => $app['authentication']->getUser()->getSettingValue('start_page_query', '') + 'basket_status' => $app['settings']->getUserSetting($app['authentication']->getUser(), 'client_basket_status', '1'), + 'mod_pres' => $app['settings']->getUserSetting($app['authentication']->getUser(), 'client_view', '' ), + 'start_page' => $app['settings']->getUserSetting($app['authentication']->getUser(), 'start_page'), + 'start_page_query' => $app['settings']->getUserSetting($app['authentication']->getUser(), 'start_page_query', '') ])); } @@ -350,7 +350,7 @@ class Root implements ControllerProviderInterface $cssPath = __DIR__ . '/../../../../../www/skins/client/'; $css = []; - $cssFile = $app['authentication']->getUser()->getSettingValue('client_css'); + $cssFile = $app['settings']->getUserSetting($app['authentication']->getUser(), 'client_css'); $finder = new Finder(); @@ -418,7 +418,7 @@ class Root implements ControllerProviderInterface */ private function getDefaultClientStartPage(Application $app) { - $startPage = strtoupper($app['authentication']->getUser()->getSettingValue('start_page')); + $startPage = strtoupper($app['settings']->getUserSetting($app['authentication']->getUser(), 'start_page')); if ($startPage === 'PUBLI') { return $this->getPublicationStartPage($app); @@ -441,7 +441,7 @@ class Root implements ControllerProviderInterface { $collections = $queryParameters = []; - $searchSet = json_decode($app['authentication']->getUser()->getSettingValue('search')); + $searchSet = json_decode($app['settings']->getUserSetting($app['authentication']->getUser(), 'search')); if ($searchSet && isset($searchSet->bases)) { foreach ($searchSet->bases as $bases) { @@ -451,9 +451,9 @@ class Root implements ControllerProviderInterface $collections = array_keys($app['acl']->get($app['authentication']->getUser())->get_granted_base()); } - $queryParameters["mod"] = $app['authentication']->getUser()->getSettingValue('client_view', '3X6'); + $queryParameters["mod"] = $app['settings']->getUserSetting($app['authentication']->getUser(), 'client_view', '3X6'); $queryParameters["bas"] = $collections; - $queryParameters["qry"] = $app['authentication']->getUser()->getSettingValue('start_page_query', 'all'); + $queryParameters["qry"] = $app['settings']->getUserSetting($app['authentication']->getUser(), 'start_page_query', 'all'); $queryParameters["pag"] = 0; $queryParameters["search_type"] = SearchEngineOptions::RECORD_RECORD; $queryParameters["qryAdv"] = ''; @@ -479,7 +479,7 @@ class Root implements ControllerProviderInterface { return $app['twig']->render('client/home_inter_pub_basket.html.twig', [ 'feeds' => Aggregate::createFromUser($app, $app['authentication']->getUser()), - 'image_size' => (int) $app['authentication']->getUser()->getSettingValue('images_size') + 'image_size' => (int) $app['settings']->getUserSetting($app['authentication']->getUser(), 'images_size') ]); } diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Query.php b/lib/Alchemy/Phrasea/Controller/Prod/Query.php index bffb604049..da251c5452 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Query.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Query.php @@ -52,7 +52,7 @@ class Query implements ControllerProviderInterface { $query = (string) $request->request->get('qry'); - $mod = $app['authentication']->getUser()->getSettingValue('view'); + $mod = $app['settings']->getUserSetting($app['authentication']->getUser(), 'view'); $json = []; @@ -60,7 +60,7 @@ class Query implements ControllerProviderInterface $form = $options->serialize(); - $perPage = (int) $app['authentication']->getUser()->getSettingValue('images_per_page'); + $perPage = (int) $app['settings']->getUserSetting($app['authentication']->getUser(), 'images_per_page'); $page = (int) $request->request->get('pag'); $firstPage = $page < 1; @@ -72,15 +72,10 @@ class Query implements ControllerProviderInterface $result = $app['phraseanet.SE']->query($query, (($page - 1) * $perPage), $perPage, $options); - $userQuery = new UserQuery(); - $userQuery->setUser($app['authentication']->getUser()); - $userQuery->setQuery($result->getQuery()); + $app['manipulator.user']->logQuery($app['authentication']->getUser(), $result->getQuery()); - $app['EM']->persist($userQuery); - $app['EM']->flush(); - - if ($app['authentication']->getUser()->getSettingValue('start_page') === 'LAST_QUERY') { - $app['authentication']->getUser()->setSettingValue('start_page_query', $result->getQuery()); + if ($app['settings']->getUserSetting($app['authentication']->getUser(), 'start_page') === 'LAST_QUERY') { + $app['manipulator.user']->setUserSetting($app['authentication']->getUser(), 'start_page_query', $result->getQuery()); } foreach ($options->getDataboxes() as $databox) { diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Root.php b/lib/Alchemy/Phrasea/Controller/Prod/Root.php index c41b36e1c3..3c96a27cb8 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Root.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Root.php @@ -65,7 +65,7 @@ class Root implements ControllerProviderInterface $css[$baseName] = $baseName; } - $cssfile = $app['authentication']->getUser()->getSettingValue('css'); + $cssfile = $app['settings']->getUserSetting($app['authentication']->getUser(), 'css'); if (!$cssfile && isset($css['000000'])) { $cssfile = '000000'; diff --git a/lib/Alchemy/Phrasea/Controller/User/Preferences.php b/lib/Alchemy/Phrasea/Controller/User/Preferences.php index 21342fbd54..e0280822f8 100644 --- a/lib/Alchemy/Phrasea/Controller/User/Preferences.php +++ b/lib/Alchemy/Phrasea/Controller/User/Preferences.php @@ -85,7 +85,7 @@ class Preferences implements ControllerProviderInterface $success = false; if (null !== $prop && null !== $value) { - $app['authentication']->getUser()->setSettingValue($prop, $value); + $app['manipulator.user']->setUserSetting($app['authentication']->getUser(), $prop, $value); $success = true; $msg = $app->trans('Preference saved !'); } diff --git a/lib/Alchemy/Phrasea/Core/Configuration/DisplaySettingService.php b/lib/Alchemy/Phrasea/Core/Configuration/DisplaySettingService.php index 916f47da0e..040e4b1a26 100644 --- a/lib/Alchemy/Phrasea/Core/Configuration/DisplaySettingService.php +++ b/lib/Alchemy/Phrasea/Core/Configuration/DisplaySettingService.php @@ -91,6 +91,26 @@ class DisplaySettingService return $user->getSettings()->get($name)->getValue(); } + + /** + * Return a user notification setting given a user. + * + * @param User $user + * @param string $name + * @param mixed $default + * + * @return mixed + */ + public function getUserNotificationSetting(User $user, $name, $default = false) + { + if (false === $user->getNotificationSettings()->containsKey($name)) { + + return $default; + } + + return $user->getUserNotificationSetting()->get($name)->getValue(); + } + /** * Returns application setting value. * diff --git a/lib/Alchemy/Phrasea/Model/Entities/User.php b/lib/Alchemy/Phrasea/Model/Entities/User.php index 0ae4961ae2..24fc17cfa0 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/User.php +++ b/lib/Alchemy/Phrasea/Model/Entities/User.php @@ -992,34 +992,6 @@ class User return $this; } - /** - * Sets the value of a user setting. - * - * @param $name - * @param $value - * - * @return $this - */ - public function setSettingValue($name, $value) - { - foreach ($this->settings as $setting) { - if ($name === $setting->getName()) { - $setting->setValue($value); - $this->cachedSettings = null; - - return $this; - } - } - - $setting = new UserSetting(); - $setting->setName($name); - $setting->setValue($value); - - $this->cachedSettings = null; - - return $this; - } - /** * @return ArrayCollection */ @@ -1040,51 +1012,6 @@ class User return $this; } - /** - * Sets the value of a notification setting. - * - * @param $name - * @param $value - * - * @return $this - */ - public function setNotificationSettingValue($name, $value) - { - foreach ($this->notificationSettings as $setting) { - if ($name === $setting->getName()) { - $setting->setValue((Boolean) $value); - - return $this; - } - } - - $notificationSetting = new UserNotificationSetting(); - $notificationSetting->setName($name); - $notificationSetting->setValue((Boolean) $value); - - $this->notificationSettings->add($notificationSetting); - - return $this; - } - - /** - * Retrieves user notification setting value. - * - * @param $name - * - * @return boolean - */ - public function getNotificationSettingValue($name) - { - foreach ($this->notificationSettings as $setting) { - if ($name === $setting->getName()) { - return (Boolean) $setting->getValue(); - } - } - - return true; - } - /** * @return boolean */ diff --git a/lib/Alchemy/Phrasea/Model/Manipulator/UserManipulator.php b/lib/Alchemy/Phrasea/Model/Manipulator/UserManipulator.php index f4368c746f..95863228bc 100644 --- a/lib/Alchemy/Phrasea/Model/Manipulator/UserManipulator.php +++ b/lib/Alchemy/Phrasea/Model/Manipulator/UserManipulator.php @@ -184,13 +184,17 @@ class UserManipulator implements ManipulatorInterface * @param string $name * @param string $value */ - public function addUserSetting(User $user, $name, $value) + public function setUserSetting(User $user, $name, $value) { - $userSetting = new UserSetting(); - $userSetting->setUser($user); - $userSetting->setName($name); - $userSetting->setValue($value); - $user->addSetting($userSetting); + if ($user->getSettings()->containsKey($name)) { + $user->getSettings()->get($name)->setValue($value); + } else { + $userSetting = new UserSetting(); + $userSetting->setUser($user); + $userSetting->setName($name); + $userSetting->setValue($value); + $user->addSetting($userSetting); + } $this->manager->update($user); } @@ -202,13 +206,17 @@ class UserManipulator implements ManipulatorInterface * @param string $name * @param string $value */ - public function addNotificationSetting(User $user, $name, $value) + public function setNotificationSetting(User $user, $name, $value) { - $notifSetting = new UserNotificationSetting(); - $notifSetting->setName($name); - $notifSetting->setValue($value); - $notifSetting->setUser($user); - $user->addNotificationSettings($notifSetting); + if ($user->getNotificationSettings()->containsKey($name)) { + $user->getNotificationSettings()->get($name)->setValue((Boolean) $value); + } else { + $userSetting = new UserNotificationSetting(); + $userSetting->setUser($user); + $userSetting->setName($name); + $userSetting->setValue($value); + $user->addNotificationSettings($userSetting); + } $this->manager->update($user); } diff --git a/lib/classes/databox/cgu.php b/lib/classes/databox/cgu.php index 4574dad3b4..8d7f3fcf40 100644 --- a/lib/classes/databox/cgu.php +++ b/lib/classes/databox/cgu.php @@ -62,7 +62,7 @@ class databox_cgu if ( ! $app['acl']->get($app['authentication']->getUser())->has_access_to_sbas($databox->get_sbas_id())) { continue; } - $userValidation = ($app['authentication']->getUser()->getSettingValue('terms_of_use_' . $databox->get_sbas_id()) !== $update && trim($value) !== ''); + $userValidation = ($app['settings']->getUserSetting($app['authentication']->getUser(), 'terms_of_use_' . $databox->get_sbas_id()) !== $update && trim($value) !== ''); } if ($userValidation) diff --git a/lib/classes/eventsmanager/notifyAbstract.php b/lib/classes/eventsmanager/notifyAbstract.php index 8ec559d554..5e7e9385a5 100644 --- a/lib/classes/eventsmanager/notifyAbstract.php +++ b/lib/classes/eventsmanager/notifyAbstract.php @@ -32,15 +32,10 @@ abstract class eventsmanager_notifyAbstract extends eventsmanager_eventAbstract abstract public function icon_url(); - protected function get_prefs($class, $usr_id) + protected function shouldSendNotificationFor($usrId) { - $user = $this->app['manipulator.user']->getRepository()->find($usr_id); + $user = $this->app['manipulator.user']->getRepository()->find($usrId); - return $user->getNotificationSettingValue($class); - } - - protected function shouldSendNotificationFor($usr_id) - { - return 0 !== (int) $this->get_prefs(get_class($this), $usr_id); + return $this->app['settings']->getUserNotificationSetting($user, get_class($this)); } } diff --git a/templates/web/account/account.html.twig b/templates/web/account/account.html.twig index e06e8fd1af..302398cc72 100644 --- a/templates/web/account/account.html.twig +++ b/templates/web/account/account.html.twig @@ -154,7 +154,7 @@ {% for notification in nots %}
  • diff --git a/templates/web/common/caption_templates/answer.html.twig b/templates/web/common/caption_templates/answer.html.twig index 8a03620a5b..41bc87f319 100644 --- a/templates/web/common/caption_templates/answer.html.twig +++ b/templates/web/common/caption_templates/answer.html.twig @@ -2,7 +2,7 @@ {% for value in record.get_caption().get_highlight_fields(highlight, null, searchEngine, includeBusiness, options) %}
    {{ value.label }} : {{ value.value | thesaurus }}
    {% endfor %} - {% if app['authentication'].getUser().getSettingValue('technical_display') == 'group' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'technical_display') == 'group' %}
    {% include 'common/technical_datas.html.twig' %} {% endif %} diff --git a/templates/web/common/caption_templates/internal_publi.html.twig b/templates/web/common/caption_templates/internal_publi.html.twig index 8a03620a5b..41bc87f319 100644 --- a/templates/web/common/caption_templates/internal_publi.html.twig +++ b/templates/web/common/caption_templates/internal_publi.html.twig @@ -2,7 +2,7 @@ {% for value in record.get_caption().get_highlight_fields(highlight, null, searchEngine, includeBusiness, options) %}
    {{ value.label }} : {{ value.value | thesaurus }}
    {% endfor %} - {% if app['authentication'].getUser().getSettingValue('technical_display') == 'group' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'technical_display') == 'group' %}
    {% include 'common/technical_datas.html.twig' %} {% endif %} diff --git a/templates/web/common/caption_templates/lazaret.html.twig b/templates/web/common/caption_templates/lazaret.html.twig index 8a03620a5b..41bc87f319 100644 --- a/templates/web/common/caption_templates/lazaret.html.twig +++ b/templates/web/common/caption_templates/lazaret.html.twig @@ -2,7 +2,7 @@ {% for value in record.get_caption().get_highlight_fields(highlight, null, searchEngine, includeBusiness, options) %}
    {{ value.label }} : {{ value.value | thesaurus }}
    {% endfor %} - {% if app['authentication'].getUser().getSettingValue('technical_display') == 'group' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'technical_display') == 'group' %}
    {% include 'common/technical_datas.html.twig' %} {% endif %} diff --git a/templates/web/prod/WorkZone/Macros.html.twig b/templates/web/prod/WorkZone/Macros.html.twig index e42be80d43..55d3fc860d 100644 --- a/templates/web/prod/WorkZone/Macros.html.twig +++ b/templates/web/prod/WorkZone/Macros.html.twig @@ -276,10 +276,10 @@ {% macro element(wz_scope, container, contained, record, ord) %} {% set box_height = 110 %} - {% if app['authentication'].getUser().getSettingValue('basket_title_display') == '1' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'basket_title_display') == '1' %} {% set box_height = (box_height + 20) %} {% endif %} - {% if app['authentication'].getUser().getSettingValue('basket_status_display') == '1' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'basket_status_display') == '1' %} {% set box_height = (box_height + 20) %} {% endif %} @@ -289,12 +289,12 @@ class="CHIM diapo CHIM_{{record.get_serialize_key()}}" style="height:{{box_height}}px;" id="CHIM_{% if wz_scope == 'groupings' %}{{record.get_serialize_key()}}{% else %}{{ contained.getId() }}{% endif %}"> - {% if app['authentication'].getUser().getSettingValue('basket_title_display') == '1' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'basket_title_display') == '1' %}
    {{record.get_title()}}
    {% endif %} - {% if app['authentication'].getUser().getSettingValue('basket_status_display') == '1' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'basket_status_display') == '1' %}
    {{record.get_status_icons|raw}}
    @@ -317,7 +317,7 @@ class="WorkZoneElementRemover {{ wz_scope }}" title="{{ 'delete' | trans }}" > X - {% if app['authentication'].getUser().getSettingValue('basket_caption_display') == '1' %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'basket_caption_display') == '1' %}
    {% endif %} diff --git a/templates/web/prod/actions/edit_default.html.twig b/templates/web/prod/actions/edit_default.html.twig index 02f05f9d43..8a53b8a7b8 100644 --- a/templates/web/prod/actions/edit_default.html.twig +++ b/templates/web/prod/actions/edit_default.html.twig @@ -4,8 +4,8 @@ {% set cont_width = 130 %} {% set cont_height = 140 %} {% else %} - {% set cont_width = app['authentication'].getUser().getSettingValue('editing_images_size') %} - {% set cont_height = app['authentication'].getUser().getSettingValue('editing_images_size') %} + {% set cont_width = app['settings'].getUserSetting(app['authentication'].getUser(), 'editing_images_size') %} + {% set cont_height = app['settings'].getUserSetting(app['authentication'].getUser(), 'editing_images_size') %} {% endif %} {% set i = record.get_number() %} @@ -71,8 +71,8 @@ {% trans %}prod::editing::fields: status{% endtrans %} {% set cssfile = '000000' %} - {% if app['authentication'].getUser().getSettingValue('css') %} - {% set cssfile = app['authentication'].getUser().getSettingValue('css') %} + {% if app['settings'].getUserSetting(app['authentication'].getUser(), 'css') %} + {% set cssfile = app['settings'].getUserSetting(app['authentication'].getUser(), 'css') %} {% endif %} {% for field in fields %} {% set i = field.get_id() %} @@ -127,7 +127,7 @@ -
    +
    @@ -150,7 +150,7 @@
    -
    +
    {{_self.HTML_fieldlist(recordsRequest, fields)}}
    @@ -230,7 +230,7 @@
    -
    +
      {% if thesaurus %} diff --git a/templates/web/prod/index.html.twig b/templates/web/prod/index.html.twig index db49536b1a..418b923576 100644 --- a/templates/web/prod/index.html.twig +++ b/templates/web/prod/index.html.twig @@ -96,12 +96,12 @@ {% endblock %} @@ -123,7 +123,7 @@
    - {% set ratio = app['authentication'].getUser().getSettingValue('search_window') %} + {% set ratio = app['settings'].getUserSetting(app['authentication'].getUser(), 'search_window') %} {% if ratio == 0 %} {% set ratio = '0.333' %} {% endif %} @@ -283,7 +283,7 @@
    - + @@ -680,11 +680,11 @@