File: Sortpref.php

package info (click to toggle)
php-horde-imp 6.2.27-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 20,952 kB
  • sloc: php: 36,468; xml: 25,887; javascript: 11,917; makefile: 18; sh: 3
file content (180 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @category  Horde
 * @copyright 2012-2017 Horde LLC
 * @license   http://www.horde.org/licenses/gpl GPL
 * @package   IMP
 */

/**
 * This class provides the object representation for the sort preference for
 * a mailbox.
 *
 * @author    Michael Slusarz <slusarz@horde.org>
 * @category  Horde
 * @copyright 2012-2017 Horde LLC
 * @license   http://www.horde.org/licenses/gpl GPL
 * @package   IMP
 *
 * @property IMP_Mailbox $mbox  Mailbox for these preferences.
 * @property integer $sortby  The sortby value.
 * @property boolean $sortby_default  Is the sortby value the default?
 * @property boolean $sortby_locked  Is the sortby value locked?
 * @property integer $sortdir  The sortdir value.
 * @property boolean $sortdir_default  Is the sortdir value the default?
 * @property boolean $sortdir_locked  Is the sortdir value locked?
 */
class IMP_Prefs_Sort_Sortpref
{
    /**
     * Mailbox object.
     *
     * @var IMP_Mailbox
     */
    protected $_mbox;

    /**
     * The sortby value.
     *
     * @var array
     */
    protected $_sortby;

    /**
     * The sortdir value.
     *
     * @var array
     */
    protected $_sortdir;

    /**
     * Constructor.
     *
     * @param string $mbox      Mailbox.
     * @param integer $sortby   Sortby value.
     * @param integer $sortdir  Sortdir value.
     */
    public function __construct($mbox, $sortby = null, $sortdir = null)
    {
        $this->_mbox = IMP_Mailbox::get($mbox);
        $this->_sortby = $sortby;
        $this->_sortdir = $sortdir;
    }

    /**
     */
    public function __get($name)
    {
        global $prefs;

        switch ($name) {
        case 'mbox':
            return $this->_mbox;

        case 'sortby':
            if (is_null($this->_sortby)) {
                return ($by = $prefs->getValue('sortby'))
                    ? $by
                    /* Sanity check: make sure we have a sort value. */
                    : Horde_Imap_Client::SORT_SEQUENCE;
            }
            return $this->_sortby;

        case 'sortby_default':
            return is_null($this->_sortby);

        case 'sortby_locked':
            return $this->_mbox->search
                /* For now, only allow sorting in search mailboxes guaranteed
                 * to consist of a single mailbox. */
                ? !$this->_mbox->systemquery
                : $prefs->isLocked(IMP_Prefs_Sort::SORTPREF);

        case 'sortdir':
            return is_null($this->_sortdir)
                ? $prefs->getValue('sortdir')
                : $this->_sortdir;

        case 'sortdir_default':
            return is_null($this->_sortdir);

        case 'sortdir_locked':
            return $this->_mbox->search
                /* Search results can always/easily be reversed. */
                ? false
                : $prefs->isLocked(IMP_Prefs_Sort::SORTPREF);
        }
    }

    /**
     */
    public function __set($name, $value)
    {
        switch ($name) {
        case 'sortby':
            $this->_sortby = $value;
            break;

        case 'sortdir':
            $this->_sortdir = $value;
            break;
        }
    }

    /**
     * Converts sortby value given current mailbox attributes.
     */
    public function convertSortby()
    {
        if ($this->_mbox->access_sort) {
            switch ($this->sortby) {
            case Horde_Imap_Client::SORT_THREAD:
                if (!$this->_mbox->access_sortthread) {
                    $this->_sortby = Horde_Imap_Client::SORT_SUBJECT;
                }
                break;

            case Horde_Imap_Client::SORT_FROM:
                /* If the preference is to sort by From Address, when we are
                 * in the Drafts or Sent mailboxes, sort by To Address. */
                if ($this->_mbox->special_outgoing) {
                    $this->_sortby = Horde_Imap_Client::SORT_TO;
                }
                break;

            case Horde_Imap_Client::SORT_TO:
                if (!$this->_mbox->special_outgoing) {
                    $this->_sortby = Horde_Imap_Client::SORT_FROM;
                }
                break;
            }
        } else {
            $this->_sortby = Horde_Imap_Client::SORT_SEQUENCE;
        }
    }

    /**
     * Returns the array representation of this object.
     */
    public function toArray()
    {
        $ret = array();

        if (!is_null($this->_sortby)) {
            $ret['b'] = $this->_sortby;
        }
        if (!is_null($this->_sortdir)) {
            $ret['d'] = $this->_sortdir;
        }

        return empty($ret)
            ? null
            : $ret;
    }

}