diff --git a/lib/Alchemy/Phrasea/Controller/Root/Account.php b/lib/Alchemy/Phrasea/Controller/Root/Account.php index 467701d37f..1b3e8f5c11 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Account.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Account.php @@ -424,13 +424,10 @@ class Account implements ControllerProviderInterface foreach ($app['events-manager']->list_notifications_available($app['authentication']->getUser()->get_id()) as $notifications) { foreach ($notifications as $notification) { - $notifId = $notification['id']; - $notifName = sprintf('notification_%s', $notifId); - - if (isset($requestedNotifications[$notifId])) { - $app['authentication']->getUser()->setPrefs($notifName, '1'); + if (isset($requestedNotifications[$notification['id']])) { + $app['authentication']->getUser()->set_notification_preference($app, $notification['id'], '1'); } else { - $app['authentication']->getUser()->setPrefs($notifName, '0'); + $app['authentication']->getUser()->set_notification_preference($app, $notification['id'], '0'); } } } diff --git a/lib/classes/User/Adapter.php b/lib/classes/User/Adapter.php index ae1d0fdb1f..98b464bfc2 100644 --- a/lib/classes/User/Adapter.php +++ b/lib/classes/User/Adapter.php @@ -69,7 +69,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface * * @var array */ - protected static $def_values = array( + public static $def_values = array( 'view' => 'thumbs', 'images_per_page' => 20, 'images_size' => 120, @@ -318,6 +318,9 @@ class User_Adapter implements User_Interface, cache_cacheableInterface protected $password; + protected $preferences_loaded = false; + protected $notifications_preferences_loaded = false; + /** * * @param Integer $id @@ -1307,7 +1310,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface protected function load_preferences() { - if ($this->_prefs) { + if ($this->preferences_loaded) { return $this; } @@ -1328,36 +1331,36 @@ class User_Adapter implements User_Interface, cache_cacheableInterface } $this->_prefs[$k] = $v; - $this->update_pref($k, $v); } } + $this->preferences_loaded = true; + return $this; } protected function load_notifications_preferences(Application $app) { + $this->load_preferences(); + $notifications = $app['events-manager']->list_notifications_available($this->id); foreach ($notifications as $notification_group => $nots) { foreach ($nots as $notification) { if (!isset($this->_prefs['notification_' . $notification['id']])) { $this->_prefs['notification_' . $notification['id']] = '1'; - - $this->update_pref('notification_' . $notification['id'], '1'); } } } $this->notification_preferences_loaded = true; } - protected $notifications_preferences_loaded = false; public function get_notifications_preference(Application $app, $notification_id) { if (!$this->notifications_preferences_loaded) $this->load_notifications_preferences($app); - return $this->_prefs['notification_' . $notification_id]; + return isset($this->_prefs['notification_' . $notification_id]) ? $this->_prefs['notification_' . $notification_id] : '0'; } public function set_notification_preference(Application $app, $notification_id, $value) @@ -1365,7 +1368,12 @@ class User_Adapter implements User_Interface, cache_cacheableInterface if (!$this->notifications_preferences_loaded) $this->load_notifications_preferences($app); - return $this->_prefs['notification_' . $notification_id] = $value ? '1' : '0'; + $prop = 'notification_' . $notification_id; + $value = $value ? '1' : '0'; + + $this->setPrefs($prop, $value); + + return ; } public function get_display_name() @@ -1490,15 +1498,11 @@ class User_Adapter implements User_Interface, cache_cacheableInterface return $this->_prefs[$prop]; } - public function getPrefs($prop) + public function getPrefs($prop, $default = null) { $this->load_preferences(); - if (!isset($this->_prefs[$prop])) { - $this->_prefs[$prop] = null; - $this->update_pref($prop, null); - } - return $this->_prefs[$prop]; + return array_key_exists($prop, $this->_prefs) ? $this->_prefs[$prop] : $default; } public static function updateClientInfos(Application $app, $app_id) diff --git a/lib/classes/eventsmanager/notifyAbstract.php b/lib/classes/eventsmanager/notifyAbstract.php index df9a1ef5d9..272a7f82a3 100644 --- a/lib/classes/eventsmanager/notifyAbstract.php +++ b/lib/classes/eventsmanager/notifyAbstract.php @@ -26,7 +26,7 @@ abstract class eventsmanager_notifyAbstract extends eventsmanager_eventAbstract protected function get_prefs($class, $usr_id) { $user = User_Adapter::getInstance($usr_id, $this->app); - $pref = $user->getPrefs('notification_' . $class); + $pref = $user->get_notifications_preference($this->app, $class); return null !== $pref ? $pref : 1; } diff --git a/lib/classes/patch/381alpha3a.php b/lib/classes/patch/381alpha3a.php new file mode 100644 index 0000000000..4d391c529c --- /dev/null +++ b/lib/classes/patch/381alpha3a.php @@ -0,0 +1,73 @@ +release; + } + + /** + * {@inheritdoc} + */ + public function require_all_upgrades() + { + return false; + } + + /** + * {@inheritdoc} + */ + public function concern() + { + return $this->concern; + } + + /** + * {@inheritdoc} + */ + public function apply(base $appbox, Application $app) + { + $propSql = $propArgs = array(); + $n = 0; + + foreach (\User_Adapter::$def_values as $prop => $value) { + if ('start_page_query' === $prop) { + continue; + } + $propSql[] = '(prop = :prop_'.$n.' AND value = :value_'.$n.')'; + $propArgs[':prop_'.$n] = $prop; + $propArgs[':value_'.$n] = $value; + $n++; + } + + $sql = "DELETE FROM usr_settings + WHERE 1 AND (".implode(' OR ', $propSql)." OR value IS NULL OR (value = 1 AND prop LIKE 'notification_%'))"; + + $stmt = $appbox->get_connection()->prepare($sql); + $stmt->execute($propArgs); + $stmt->closeCursor(); + + return true; + } +} diff --git a/templates/web/account/account.html.twig b/templates/web/account/account.html.twig index 9b579b6d83..ddaf32b5b4 100644 --- a/templates/web/account/account.html.twig +++ b/templates/web/account/account.html.twig @@ -154,7 +154,7 @@ {% for notification in nots %}