1: <?php
2: /**
3: * Copyright 2013-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 2013-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Iterator filter for the IMP_Search object.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2013-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Search_IteratorFilter extends FilterIterator
24: {
25: /* Bitmask filters for iterator. */
26: const FILTER = 1;
27: const QUERY = 2;
28: const VFOLDER = 4;
29: const ALL = 8;
30: const DISABLED = 16;
31:
32: /**
33: * Filtering mask.
34: *
35: * @var integer
36: */
37: protected $_mask = 0;
38:
39: /**
40: * Create the iterator and set the filter mask.
41: *
42: * @param integer $mask See setFilter().
43: *
44: * @return IMP_Search_IteratorFilter Iterator.
45: */
46: public static function create($mask = 0)
47: {
48: global $injector;
49:
50: $iterator = new self(
51: $injector->getInstance('IMP_Search')->getIterator()
52: );
53: $iterator->setFilter($mask);
54:
55: return $iterator;
56: }
57:
58: /**
59: * Set the iterator filter and reset the internal pointer.
60: *
61: * @param integer $mask A mask with the following possible elements:
62: * - self::DISABLED: List even if disabled.
63: * - self::FILTER: List filters.
64: * - self::QUERY: List search queries.
65: * - self::VFOLDER: List virtual folders.
66: * - self::ALL: List filter, search queries, and virtual folders.
67: */
68: public function setFilter($mask = 0)
69: {
70: $this->_mask = intval($mask);
71: if ($this->_mask & self::ALL) {
72: $this->_mask |= self::FILTER | self::QUERY | self::VFOLDER;
73: }
74: $this->rewind();
75: }
76:
77: /* FilterIterator method. */
78:
79: /**
80: */
81: public function accept()
82: {
83: $ob = $this->current();
84:
85: if ($ob->enabled || ($this->_mask & self::DISABLED)) {
86: if (($this->_mask & self::FILTER) &&
87: ($ob instanceof IMP_Search_Filter)) {
88: return true;
89: }
90:
91: if (($this->_mask & self::VFOLDER) &&
92: ($ob instanceof IMP_Search_Vfolder)) {
93: return true;
94: }
95:
96: if (($this->_mask & self::QUERY) &&
97: !($ob instanceof IMP_Search_Filter) &&
98: !($ob instanceof IMP_Search_Vfolder)) {
99: return true;
100: }
101: }
102:
103: return false;
104: }
105:
106: }
107: