1: <?php
2: /**
3: * Copyright 2010-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 2010-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Date-related search queries.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2010-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Search_Element_Daterange extends IMP_Search_Element
24: {
25: /**
26: * Constructor.
27: *
28: * @param mixed $begin Either a DateTime object of the beginning date or
29: * null (all messages since the beginning of time).
30: * @param mixed $end Either a DateTime object of the ending date or
31: * null (all messages until the end of time).
32: * @param boolean $not Is this a not search?
33: */
34: public function __construct($begin, $end, $not = false)
35: {
36: /* Data element:
37: * b = (integer) UNIX timestamp - beginning.
38: * e = (integer) UNIX timestamp - ending.
39: * n = (integer) Do a NOT search? */
40: $this->_data = new stdClass;
41: $this->_data->b = ($begin instanceof DateTime)
42: ? $begin->format('U')
43: : 0;
44: $this->_data->e = ($end instanceof DateTime)
45: ? $end->format('U')
46: : 0;
47: $this->_data->n = intval($not);
48:
49: /* Flip $begin and $end if $end is earlier than $begin. */
50: if ($this->_data->b &&
51: $this->_data->e &&
52: ($this->_data->b > $this->_data->e)) {
53: $tmp = $this->_data->e;
54: $this->_data->e = $this->_data->b;
55: $this->_data->b = $tmp;
56: }
57: }
58:
59: /**
60: */
61: public function createQuery($mbox, $queryob)
62: {
63: if ($this->_data->b == $this->_data->e) {
64: $queryob->dateSearch(
65: // Cast to timestamp - see PHP Bug #40171/Horde Bug #9513
66: new DateTime('@' . ($this->_data->b)),
67: Horde_Imap_Client_Search_Query::DATE_ON,
68: true,
69: $this->_data->n
70: );
71: } else {
72: if (!empty($this->_data->b)) {
73: $queryob->dateSearch(
74: new DateTime('@' . ($this->_data->b)),
75: Horde_Imap_Client_Search_Query::DATE_SINCE,
76: true,
77: $this->_data->n
78: );
79: }
80: if (!empty($this->_data->e)) {
81: $queryob->dateSearch(
82: new DateTime('@' . ($this->_data->e + 86400)),
83: Horde_Imap_Client_Search_Query::DATE_BEFORE,
84: true,
85: $this->_data->n
86: );
87: }
88: }
89:
90: return $queryob;
91: }
92:
93: /**
94: */
95: public function queryText()
96: {
97: if (empty($this->_data->e)) {
98: return sprintf(
99: _("After '%s'"),
100: gmstrftime('%x', $this->_data->b)
101: );
102: }
103:
104: if (empty($this->_data->b)) {
105: return sprintf(
106: _("Before '%s'"),
107: gmstrftime('%x', $this->_data->e)
108: );
109: }
110:
111: if ($this->_data->b == $this->_data->e) {
112: return sprintf(
113: _("On '%s'"),
114: gmstrftime('%x', $this->_data->b)
115: );
116: }
117:
118: return sprintf(
119: _("Between '%s' and '%s'"),
120: gmstrftime('%x', $this->_data->b),
121: gmstrftime('%x', $this->_data->e)
122: );
123: }
124:
125: }
126: