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 provides the object representation for the sort preference for
16: * a mailbox.
17: *
18: * @author Michael Slusarz <slusarz@horde.org>
19: * @category Horde
20: * @copyright 2012-2014 Horde LLC
21: * @license http://www.horde.org/licenses/gpl GPL
22: * @package IMP
23: *
24: * @property IMP_Mailbox $mbox Mailbox for these preferences.
25: * @property integer $sortby The sortby value.
26: * @property boolean $sortby_default Is the sortby value the default?
27: * @property boolean $sortby_locked Is the sortby value locked?
28: * @property integer $sortdir The sortdir value.
29: * @property boolean $sortdir_default Is the sortdir value the default?
30: * @property boolean $sortdir_locked Is the sortdir value locked?
31: */
32: class IMP_Prefs_Sort_Sortpref
33: {
34: /**
35: * Mailbox object.
36: *
37: * @var IMP_Mailbox
38: */
39: protected $_mbox;
40:
41: /**
42: * The sortby value.
43: *
44: * @var array
45: */
46: protected $_sortby;
47:
48: /**
49: * The sortdir value.
50: *
51: * @var array
52: */
53: protected $_sortdir;
54:
55: /**
56: * Constructor.
57: *
58: * @param string $mbox Mailbox.
59: * @param integer $sortby Sortby value.
60: * @param integer $sortdir Sortdir value.
61: */
62: public function __construct($mbox, $sortby = null, $sortdir = null)
63: {
64: $this->_mbox = IMP_Mailbox::get($mbox);
65: $this->_sortby = $sortby;
66: $this->_sortdir = $sortdir;
67: }
68:
69: /**
70: */
71: public function __get($name)
72: {
73: global $prefs;
74:
75: switch ($name) {
76: case 'mbox':
77: return $this->_mbox;
78:
79: case 'sortby':
80: if (is_null($this->_sortby)) {
81: return ($by = $prefs->getValue('sortby'))
82: ? $by
83: /* Sanity check: make sure we have a sort value. */
84: : Horde_Imap_Client::SORT_SEQUENCE;
85: }
86: return $this->_sortby;
87:
88: case 'sortby_default':
89: return is_null($this->_sortby);
90:
91: case 'sortby_locked':
92: return $this->_mbox->search
93: /* For now, only allow sorting in search mailboxes guaranteed
94: * to consist of a single mailbox. */
95: ? !$this->_mbox->systemquery
96: : $prefs->isLocked(IMP_Prefs_Sort::SORTPREF);
97:
98: case 'sortdir':
99: return is_null($this->_sortdir)
100: ? $prefs->getValue('sortdir')
101: : $this->_sortdir;
102:
103: case 'sortdir_default':
104: return is_null($this->_sortdir);
105:
106: case 'sortdir_locked':
107: return $this->_mbox->search
108: /* Search results can always/easily be reversed. */
109: ? false
110: : $prefs->isLocked(IMP_Prefs_Sort::SORTPREF);
111: }
112: }
113:
114: /**
115: */
116: public function __set($name, $value)
117: {
118: switch ($name) {
119: case 'sortby':
120: $this->_sortby = $value;
121: break;
122:
123: case 'sortdir':
124: $this->_sortdir = $value;
125: break;
126: }
127: }
128:
129: /**
130: * Converts sortby value given current mailbox attributes.
131: */
132: public function convertSortby()
133: {
134: if ($this->_mbox->access_sort) {
135: switch ($this->sortby) {
136: case Horde_Imap_Client::SORT_THREAD:
137: if (!$this->_mbox->access_sortthread) {
138: $this->_sortby = Horde_Imap_Client::SORT_SUBJECT;
139: }
140: break;
141:
142: case Horde_Imap_Client::SORT_FROM:
143: /* If the preference is to sort by From Address, when we are
144: * in the Drafts or Sent mailboxes, sort by To Address. */
145: if ($this->_mbox->special_outgoing) {
146: $this->_sortby = Horde_Imap_Client::SORT_TO;
147: }
148: break;
149:
150: case Horde_Imap_Client::SORT_TO:
151: if (!$this->_mbox->special_outgoing) {
152: $this->_sortby = Horde_Imap_Client::SORT_FROM;
153: }
154: break;
155: }
156: } else {
157: $this->_sortby = Horde_Imap_Client::SORT_SEQUENCE;
158: }
159: }
160:
161: /**
162: * Returns the array representation of this object.
163: */
164: public function toArray()
165: {
166: $ret = array();
167:
168: if (!is_null($this->_sortby)) {
169: $ret['b'] = $this->_sortby;
170: }
171: if (!is_null($this->_sortdir)) {
172: $ret['d'] = $this->_sortdir;
173: }
174:
175: return empty($ret)
176: ? null
177: : $ret;
178: }
179:
180: }
181: