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: * Manage the mailbox poll list.
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_Ftree_Prefs_Poll extends IMP_Ftree_Prefs
24: {
25: /**
26: * Tree object.
27: *
28: * @var IMP_Ftree
29: */
30: private $_ftree;
31:
32: /**
33: * Constructor.
34: *
35: * @param IMP_Ftree $ftree Tree object.
36: */
37: public function __construct(IMP_Ftree $ftree)
38: {
39: global $prefs;
40:
41: $this->_ftree = $ftree;
42:
43: if ($prefs->getValue('nav_poll_all')) {
44: $this->_data = $this->_locked = true;
45: } else {
46: /* We ALWAYS poll the INBOX. */
47: $this->_data = array('INBOX' => 1);
48:
49: /* Add the list of polled mailboxes from the prefs. */
50: if ($nav_poll = @unserialize($prefs->getValue('nav_poll'))) {
51: $this->_data += $nav_poll;
52: }
53:
54: $this->_locked = $prefs->isLocked('nav_poll');
55: }
56: }
57:
58: /**
59: */
60: public function shutdown()
61: {
62: $GLOBALS['prefs']->setValue('nav_poll', serialize($this->_data));
63: }
64:
65: /**
66: * Returns the list of mailboxes to poll.
67: *
68: * @param boolean $sort Sort the directory list?
69: *
70: * @return array The list of mailboxes to poll (IMP_Mailbox objects).
71: */
72: public function getPollList($sort = false)
73: {
74: global $injector;
75:
76: $iterator = new IMP_Ftree_IteratorFilter(
77: $injector->getInstance('IMP_Ftree')
78: );
79: $iterator->add(array($iterator::CONTAINERS, $iterator::NONIMAP));
80: if ($this->_data !== true) {
81: $iterator->add($iterator::POLLED);
82: }
83:
84: $plist = array_map('strval', iterator_to_array($iterator, false));
85:
86: if ($sort) {
87: $this->_ftree->sortList($plist, $this->_ftree[IMP_Ftree::BASE_ELT]);
88: }
89:
90: return IMP_Mailbox::get($plist);
91: }
92:
93: /**
94: * Add elements to the poll list.
95: *
96: * @param mixed $id The element name or a list of element names to add.
97: */
98: public function addPollList($id)
99: {
100: if ($this->locked) {
101: return;
102: }
103:
104: foreach ((is_array($id) ? $id : array($id)) as $val) {
105: if (($elt = $this->_ftree[$val]) &&
106: !$elt->polled &&
107: !$elt->nonimap &&
108: !$elt->container) {
109: if (!$elt->subscribed) {
110: $elt->subscribed = true;
111: }
112: $this[$elt] = true;
113: $elt->polled = true;
114: }
115: }
116: }
117:
118: /**
119: * Remove elements from the poll list.
120: *
121: * @param mixed $id The element name or a list of element names to
122: * remove.
123: */
124: public function removePollList($id)
125: {
126: if (!$this->locked) {
127: foreach ((is_array($id) ? $id : array($id)) as $val) {
128: if ($elt = $this->_ftree[$val]) {
129: if (!$elt->inbox) {
130: unset($this[$val]);
131: $elt->polled = false;
132: }
133: } elseif (!IMP_Mailbox::get($val)->inbox) {
134: unset($this[$val]);
135: }
136: }
137: }
138: }
139:
140: /**
141: * Prune non-existent mailboxes from poll list.
142: */
143: public function prunePollList()
144: {
145: if (!$this->locked) {
146: foreach (IMP_Mailbox::get($this->_data) as $val) {
147: if (!$val->exists) {
148: $this->removePollList($val);
149: }
150: }
151: }
152: }
153:
154: /**
155: */
156: public function offsetGet($offset)
157: {
158: return ($this->_data === true)
159: ? (($elt = $this->_ftree[$offset]) && !$elt->nonimap && !$elt->container)
160: : parent::offsetGet($offset);
161: }
162:
163: }
164: