1<?php
2/**
3 * Copyright 2010-2017 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 2010-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * This class provides the data structure for a message flag.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2010-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 *
23 * @property-read string $abbreviation  The abbreviation to use in the minimal
24 *                                      view.
25 * @property string $bgcolor  The background color.
26 * @property-read boolean $bgdefault  Is the background color the default?
27 * @property-read boolean $canset  Can this flag be set by the user?
28 * @property-read string $css  The CSS class for the icon when the flag is
29 *                             set.
30 * @property-read string $cssicon  The CSS class for the icon.
31 * @property-read string $fgcolor  The foreground (text) color.
32 * @property-read string $form_set  Form value to use when setting flag.
33 * @property-read string $form_unset  Form value to use when unsetting flag.
34 * @property-read string $hash  Unique hash of this flag.
35 * @property-read string $id  Unique ID.
36 * @property-read string $label  The query label.
37 * @property-read string $span  Return SPAN HTML to output the icon for use in
38 *                              a mailbox row.
39 */
40abstract class IMP_Flag_Base implements Serializable
41{
42    /* Default background color. */
43    const DEFAULT_BG = '#fff';
44
45    /**
46     * The abbreviation.
47     *
48     * @var string
49     */
50    protected $_abbreviation = '';
51
52    /**
53     * The background color.
54     *
55     * @var string
56     */
57    protected $_bgcolor = '';
58
59    /**
60     * Is this flag settable by the user?
61     *
62     * @var boolean
63     */
64    protected $_canset = false;
65
66    /**
67     * The CSS class.
68     *
69     * @var string
70     */
71    protected $_css = '';
72
73    /**
74     * The CSS class for the icon.
75     *
76     * @var string
77     */
78    protected $_cssIcon = '';
79
80    /**
81     * Unique ID.
82     *
83     * @var string
84     */
85    protected $_id = '';
86
87    /**
88     */
89    public function __get($name)
90    {
91        switch ($name) {
92        case 'abbreviation':
93            return $this->_abbreviation;
94
95        case 'bgcolor':
96            return $this->_bgcolor
97                ? $this->_bgcolor
98                : self::DEFAULT_BG;
99
100        case 'bgdefault':
101            return ($this->bgcolor == self::DEFAULT_BG);
102
103        case 'canset':
104            return $this->_canset;
105
106        case 'css':
107            return $this->_css;
108
109        case 'cssicon':
110            return $this->_cssIcon
111                ? $this->_cssIcon
112                : $this->_css;
113
114        case 'span':
115            return $this->_css
116                ? '<span class="iconImg msgflags ' . $this->css . '" title="' . htmlspecialchars($this->label) . '">&nbsp;</span>'
117                : '';
118
119        case 'fgcolor':
120            return (Horde_Image::brightness($this->bgcolor) < 128)
121                ? '#f6f6f6'
122                : '#000';
123
124        case 'form_set':
125            return $this->id;
126
127        case 'form_unset':
128            return '0\\' . $this->id;
129
130        case 'hash':
131            return hash(
132                (PHP_MINOR_VERSION >= 4) ? 'fnv132' : 'sha1',
133                $this->id
134            );
135
136        case 'id':
137            return $this->_id;
138
139        case 'label':
140            return $this->getLabel();
141        }
142    }
143
144    /**
145     * Set properties.
146     *
147     * @param string $name   Available properties:
148     *   - bgcolor: (string) The background color.
149     * @param string $value  Property value.
150     */
151    public function __set($name, $value)
152    {
153        switch ($name) {
154        case 'bgcolor':
155            $this->_bgcolor = ($value == self::DEFAULT_BG)
156                ? ''
157                : $value;
158            break;
159        }
160    }
161
162    /**
163     * Given a list of flag objects, determines if this flag's status has
164     * changed.
165     *
166     * @param array $obs    A list of IMP_Flag_Base objects.
167     * @param boolean $add  True if these flags were added, false if they were
168     *                      removed.
169     *
170     * @return mixed  Null if no change, true if flag is added, false if flag
171     *                is removed.
172     */
173    public function changed($obs, $add)
174    {
175        return null;
176    }
177
178    /**
179     * Return the flag label.
180     *
181     * @param boolean $set  Return label for setting the flag?
182     *
183     * @return string  The label.
184     */
185    public function getLabel($set = true)
186    {
187        return $set
188            ? $this->_getLabel()
189            : sprintf(_("Not %s"), $this->_getLabel());
190    }
191
192    /**
193     * Determines if the flag exists given some input data.
194     *
195     * @param mixed $data  The input data to check.
196     *
197     * @return boolean  True if flag exists.
198     */
199    public function match($data)
200    {
201        return false;
202    }
203
204    /**
205     * Return the flag label.
206     * Necessary evil as gettext strings can not be set directly to object
207     * properties.
208     *
209     * @return string  The label.
210     */
211    abstract protected function _getLabel();
212
213    /* Magic methods. */
214
215    /**
216     * String representation of the object.
217     *
218     * @return string  String representation (Flag ID).
219     */
220    public function __toString()
221    {
222        return $this->id;
223    }
224
225    /* Serializable methods. */
226
227    /**
228     */
229    public function serialize()
230    {
231        return $this->_bgcolor;
232    }
233
234    /**
235     */
236    public function unserialize($data)
237    {
238        $this->_bgcolor = $data;
239    }
240
241}
242