Save user IMAP flags in separate array.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 12 Apr 2010 19:46:05 +0000 (13:46 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 12 Apr 2010 20:00:28 +0000 (14:00 -0600)
Allows for easier changing of system flags in the future, instead of
having to traverse through all user pref entries.

imp/config/prefs.php.dist
imp/lib/Imap/Flags.php

index 5da230d..20732cf 100644 (file)
@@ -1147,7 +1147,10 @@ $_prefs['flagmanagement'] = array(
     'type' => 'special'
 );
 
-// Message flags
+// Message flags - system defaults
+// This entry should normally never be changed.  It provides the base flags
+// for all users. Flags specific to a certain user should be defined in
+// 'msgflags_user'.
 $_prefs['msgflags'] = array(
     // Format:
     //   KEY: Flag name
@@ -1277,6 +1280,19 @@ $_prefs['msgflags'] = array(
     'type' => 'implicit'
 );
 
+// Message flags - user specific
+// This array contains the list of flags created by the user through the
+// flags UI. Additionally, an admin can define default non-system flags
+// for a user in this preference.
+// See the 'msgflags' preference for the format of this value.
+$_prefs['msgflags_user'] = array(
+    // 'value' = json_encode(array())
+    'value' => '[]',
+    'locked' => false,
+    'shared' => false,
+    'type' => 'implicit'
+);
+
 // The default color to use for flags that don't require row highlighting.
 $_prefs['msgflags_color'] = array(
     'value' => '#ffffff',
index b3981af..f24826f 100644 (file)
@@ -24,11 +24,29 @@ class IMP_Imap_Flags
     protected $_flags = null;
 
     /**
+     * The 'msgflags_user' preference value.
+     *
+     * @var array
+     */
+    protected $_userflags = null;
+
+    /**
      * Save the flag list to the prefs backend.
+     *
+     * @param boolean $user  If true, update the user flag list. Otherwise,
+     *                       update the system flag list.
      */
-    protected function _save()
+    protected function _save($user = true)
     {
-        $GLOBALS['prefs']->setValue('msgflags', json_encode($this->_flags));
+        global $prefs;
+
+        if ($user) {
+            if (!$prefs->isLocked('msgflags_user')) {
+                $prefs->setValue('msgflags_user', json_encode($this->_userflags));
+            }
+        } elseif (!$prefs->isLocked('msgflags')) {
+            $prefs->setValue('msgflags', json_encode(array_diff_key($this->_flags, $this->_userflags)));
+        }
     }
 
     /**
@@ -109,7 +127,11 @@ class IMP_Imap_Flags
             return;
         }
 
-        $this->_flags = json_decode($GLOBALS['prefs']->getValue('msgflags'), true);
+        $this->_userflags = json_decode($GLOBALS['prefs']->getValue('msgflags_user'), true);
+        $this->_flags = array_merge(
+            $this->_userflags,
+            json_decode($GLOBALS['prefs']->getValue('msgflags'), true)
+        );
 
         /* Sanity checking. */
         if (is_array($this->_flags)) {
@@ -140,7 +162,7 @@ class IMP_Imap_Flags
         for ($i = 0;; ++$i) {
             $curr = self::PREFIX . $i;
             if (!isset($this->_flags[$curr])) {
-                $this->_flags[$curr] = array(
+                $entry = array(
                     // 'a' => These flags are not shown in mimp
                     // TODO: Generate random background
                     'b' => '#ffffff',
@@ -150,6 +172,9 @@ class IMP_Imap_Flags
                     't' => 'imapp'
                 );
 
+                $this->_flags[$curr] = $entry;
+                $this->_userflags[$curr] = $entry;
+
                 $this->_save();
                 return $curr;
             }
@@ -171,7 +196,7 @@ class IMP_Imap_Flags
                 $this->_flags[$label][$key] = $val;
             }
 
-            $this->_save();
+            $this->_save(isset($this->_updateflags[$label]));
         }
     }
 
@@ -189,7 +214,7 @@ class IMP_Imap_Flags
         if (isset($this->_flags[$label]) &&
             $this->_flags[$label]['l'] &&
             !empty($this->_flags[$label]['d'])) {
-            unset($this->_flags[$label]);
+            unset($this->_flags[$label], $this->_userflags[$label]);
             $this->_save();
             return true;
         }