1: <?php
2: /**
3: * Copyright 2012-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2012-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * This class manages the sortpref preference.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2012-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Prefs_Sort implements ArrayAccess, IteratorAggregate
24: {
25: /* Preference name in backend. */
26: const SORTPREF = 'sortpref';
27:
28: /**
29: * The sortpref value.
30: *
31: * @var array
32: */
33: protected $_sortpref = array();
34:
35: /**
36: * Constructor.
37: */
38: public function __construct()
39: {
40: global $prefs;
41:
42: $sortpref = @unserialize($prefs->getValue(self::SORTPREF));
43: if (is_array($sortpref)) {
44: $this->_sortpref = $sortpref;
45: }
46: }
47:
48: /**
49: * Garbage collection.
50: */
51: public function gc()
52: {
53: foreach (IMP_Mailbox::get(array_keys($this->_sortpref)) as $val) {
54: /* Purge if mailbox doesn't exist or this is a search query (not
55: * a virtual folder). */
56: if (!$val->exists || $val->query) {
57: unset($this[strval($val)]);
58: }
59: }
60: }
61:
62: /**
63: * Upgrade the preference from IMP 4 value.
64: */
65: public function upgradePrefs()
66: {
67: global $prefs;
68:
69: if (!$prefs->isDefault(self::SORTPREF)) {
70: foreach ($this->_sortpref as $key => $val) {
71: if (($sb = $this->newSortbyValue($val['b'])) !== null) {
72: $this->_sortpref[$key]['b'] = $sb;
73: }
74: }
75:
76: $this->_save();
77: }
78: }
79:
80: /**
81: * Get the new sortby pref value for IMP 5.
82: *
83: * @param integer $sortby The old (IMP 4) value.
84: *
85: * @return integer Null if no change or else the converted sort value.
86: */
87: public function newSortbyValue($sortby)
88: {
89: switch ($sortby) {
90: case 1: // SORTARRIVAL
91: /* Sortarrival was the same thing as sequence sort in IMP 4. */
92: return Horde_Imap_Client::SORT_SEQUENCE;
93:
94: case 2: // SORTDATE
95: return IMP::IMAP_SORT_DATE;
96:
97: case 161: // SORTTHREAD
98: return Horde_Imap_Client::SORT_THREAD;
99: }
100:
101: return null;
102: }
103:
104: /**
105: * Save the preference to the backend.
106: */
107: protected function _save()
108: {
109: $GLOBALS['prefs']->setValue(self::SORTPREF, serialize($this->_sortpref));
110: }
111:
112: /* ArrayAccess methods. */
113:
114: public function offsetExists($offset)
115: {
116: return isset($this->_sortpref[$offset]);
117: }
118:
119: public function offsetGet($offset)
120: {
121: $ob = $this->_offsetGet($offset);
122:
123: try {
124: $GLOBALS['injector']->getInstance('Horde_Core_Hooks')->callHook(
125: 'mbox_sort',
126: 'imp',
127: array($ob)
128: );
129: } catch (Horde_Exception_HookNotSet $e) {}
130:
131: return $ob;
132: }
133:
134: /**
135: */
136: protected function _offsetGet($offset)
137: {
138: return new IMP_Prefs_Sort_Sortpref(
139: $offset,
140: isset($this->_sortpref[$offset]['b']) ? $this->_sortpref[$offset]['b'] : null,
141: isset($this->_sortpref[$offset]['d']) ? $this->_sortpref[$offset]['d'] : null
142: );
143: }
144:
145: /**
146: * Alter a sortpref entry.
147: *
148: * @param string $offset The mailbox name.
149: * @param array $value An array with two possible keys: 'by' and 'dir'.
150: */
151: public function offsetSet($offset, $value)
152: {
153: if (empty($value)) {
154: return;
155: }
156:
157: $ob = $this->_offsetGet($offset);
158:
159: if (isset($value['by'])) {
160: $ob->sortby = $value['by'];
161: }
162: if (isset($value['dir'])) {
163: $ob->sortdir = $value['dir'];
164: }
165:
166: $this->_sortpref[$offset] = $ob->toArray();
167: $this->_save();
168: }
169:
170: public function offsetUnset($offset)
171: {
172: if (isset($this->_sortpref[$offset])) {
173: unset($this->_sortpref[$offset]);
174: $this->_save();
175: }
176: }
177:
178: /* IteratorAggregate method. */
179:
180: public function getIterator()
181: {
182: return new ArrayIterator($this->_sortpref);
183: }
184:
185: }
186: