Fix #1571 : Cleanup user settings, fix user settings calls

This commit is contained in:
Romain Neutron
2013-11-07 15:55:26 +01:00
parent e48c2cc158
commit bea211074b
6 changed files with 142 additions and 22 deletions

View File

@@ -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');
}
}
}

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -0,0 +1,73 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2012 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Application;
class patch_381alpha3a implements patchInterface
{
/** @var string */
private $release = '3.8.1-alpha.3';
/** @var array */
private $concern = array(base::APPLICATION_BOX);
/**
* {@inheritdoc}
*/
public function get_release()
{
return $this->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;
}
}

View File

@@ -154,7 +154,7 @@
{% for notification in nots %}
<li>
<label class="checkbox" for="notif_{{ notification["id"] }}">
<input type="checkbox" id="notif_{{ notification["id"] }}" name="notifications[{{ notification["id"] }}]" {% if not app["authentication"].getUser().getPrefs("notification_" ~ notification["id"]) == "0" %}checked{% endif %} value="1"/>
<input type="checkbox" id="notif_{{ notification["id"] }}" name="notifications[{{ notification["id"] }}]" {% if not app["authentication"].getUser().get_notifications_preference(app, notification["id"]) == "0" %}checked{% endif %} value="1"/>
{{ notification["description"] }}
</label>
<p class="form_alert help-block"></p>

View File

@@ -48,4 +48,50 @@ class userTest extends PhraseanetPHPUnitAbstract
$repo = self::$DI['app']['EM']->getRepository('Entities\UsrAuthProvider');
$this->assertNull($repo->findWithProviderAndId('custom-one', 12345));
}
public function testGetPref()
{
$user = $this->get_user();
$this->assertNull($user->getPrefs('lalala'));
$this->assertSame('popo', $user->getPrefs('lalala', 'popo'));
$this->assertSame(\User_Adapter::$def_values['editing_top_box'], $user->getPrefs('editing_top_box'));
}
public function testSetPref()
{
$user = $this->get_user();
$user->setPrefs('prout', 'pooop');
$this->assertSame('pooop', $user->getPrefs('prout'));
}
public function testGetNotificationPref()
{
$user = $this->get_user();
$this->assertSame('1', $user->get_notifications_preference(self::$DI['app'], 'eventsmanager_notify_push'));
}
public function testNotificationPref()
{
$user = $this->get_user();
$this->assertSame('1', $user->get_notifications_preference(self::$DI['app'], 'eventsmanager_notify_push'));
$user->set_notification_preference(self::$DI['app'], 'eventsmanager_notify_push', false);
$this->assertSame('0', $user->get_notifications_preference(self::$DI['app'], 'eventsmanager_notify_push'));
$user->set_notification_preference(self::$DI['app'], 'eventsmanager_notify_push', true);
$this->assertSame('1', $user->get_notifications_preference(self::$DI['app'], 'eventsmanager_notify_push'));
}
private function get_user()
{
$usr_id = \User_Adapter::get_usr_id_from_login(self::$DI['app'], 'notif_ref_test');
if ($usr_id) {
$user = \User_Adapter::getInstance($usr_id, self::$DI['app']);
$user->delete();
}
return \User_Adapter::create(self::$DI['app'], 'notif_ref_test', mt_rand(), null, false);
}
}