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: * Within (date) 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_Within extends IMP_Search_Element
24: {
25: /* Interval types. */
26: const INTERVAL_DAYS = 1;
27: const INTERVAL_MONTHS = 2;
28: const INTERVAL_YEARS = 3;
29:
30: /**
31: * Constructor.
32: *
33: * @param integer $interval Interval value.
34: * @param integer $type Interval type. Either:
35: * - IMP_Search_Element_Within::INTERVAL_DAYS
36: * - IMP_Search_Element_Within::INTERVAL_MONTHS
37: * - IMP_Search_Element_Within::INTERVAL_YEARS
38: * @param boolean $older Do an older search?
39: */
40: public function __construct($interval, $type, $older = true)
41: {
42: /* Data element:
43: * o = (integer) Do an older search?
44: * t = (integer) Interval type.
45: * v = (integer) Interval value. */
46: $this->_data = new stdClass;
47: $this->_data->o = intval(!empty($older));
48: $this->_data->t = $type;
49: $this->_data->v = $interval;
50: }
51:
52: /**
53: */
54: public function createQuery($mbox, $queryob)
55: {
56: /* Limited to day granularity because that is the technical
57: * limit for IMAP servers without 'WITHIN' extension. */
58: $secs = $this->_data->v * 60 * 60 * 24;
59: switch ($this->_data->t) {
60: case self::INTERVAL_YEARS:
61: $secs *= 365;
62: break;
63:
64: case self::INTERVAL_MONTHS:
65: $secs *= 30;
66: break;
67: }
68:
69: $queryob->intervalSearch($secs, $this->_data->o ? Horde_Imap_Client_Search_Query::INTERVAL_OLDER : Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER);
70:
71: return $queryob;
72: }
73:
74: /**
75: */
76: public function queryText()
77: {
78: $label = $this->_data->o
79: ? _("Older Than")
80: : _("Younger Than");
81:
82: switch ($this->_data->t) {
83: case self::INTERVAL_YEARS:
84: $term = _("years");
85: break;
86:
87: case self::INTERVAL_MONTHS:
88: $term = _("months");
89: break;
90:
91: case self::INTERVAL_DAYS:
92: $term = _("days");
93: break;
94: }
95:
96: return sprintf("%s %u %s", $label, $this->_data->v, $term);
97: }
98:
99: }
100: