1: <?php
2: /**
3: * This class handles within (date) search queries.
4: *
5: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (GPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/gpl.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/gpl GPL
13: * @package IMP
14: */
15: class IMP_Search_Element_Within extends IMP_Search_Element
16: {
17: /* Interval types. */
18: const INTERVAL_DAYS = 1;
19: const INTERVAL_MONTHS = 2;
20: const INTERVAL_YEARS = 3;
21:
22: /**
23: * Constructor.
24: *
25: * @param integer $interval Interval value.
26: * @param integer $type Interval type. Either:
27: * <pre>
28: * IMP_Search_Element_Within::INTERVAL_DAYS
29: * IMP_Search_Element_Within::INTERVAL_MONTHS
30: * IMP_Search_Element_Within::INTERVAL_YEARS
31: * </pre>
32: * @param boolean $older Do an older search?
33: */
34: public function __construct($interval, $type, $older = true)
35: {
36: /* Data element:
37: * o = (integer) Do an older search?
38: * t = (integer) Interval type.
39: * v = (integer) Interval value. */
40: $this->_data = new stdClass;
41: $this->_data->o = intval(!empty($older));
42: $this->_data->t = $type;
43: $this->_data->v = $interval;
44: }
45:
46: /**
47: */
48: public function createQuery($mbox, $queryob)
49: {
50: /* Limited to day granularity because that is the technical
51: * limit for IMAP servers without 'WITHIN' extension. */
52: $secs = $this->_data->v * 60 * 60 * 24;
53: switch ($this->_data->t) {
54: case self::INTERVAL_YEARS:
55: $secs *= 365;
56: break;
57:
58: case self::INTERVAL_MONTHS:
59: $secs *= 30;
60: break;
61: }
62:
63: $queryob->intervalSearch($secs, $this->_data->o ? Horde_Imap_Client_Search_Query::INTERVAL_OLDER : Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER);
64:
65: return $queryob;
66: }
67:
68: /**
69: */
70: public function queryText()
71: {
72: $label = $this->_data->o
73: ? _("Older Than")
74: : _("Younger Than");
75:
76: switch ($this->_data->t) {
77: case self::INTERVAL_YEARS:
78: $term = _("years");
79: break;
80:
81: case self::INTERVAL_MONTHS:
82: $term = _("months");
83: break;
84:
85: case self::INTERVAL_DAYS:
86: $term = _("days");
87: break;
88: }
89:
90: return sprintf("%s %u %s", $label, $this->_data->v, $term);
91: }
92:
93: }
94: