1: <?php
2: /**
3: * Copyright 2001-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 2001-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Login tasks module that renames sent-mail mailboxes every month.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2001-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_LoginTasks_Task_RenameSentmailMonthly extends Horde_LoginTasks_Task
24: {
25: /**
26: * Constructor.
27: */
28: public function __construct()
29: {
30: $this->active = $GLOBALS['prefs']->getValue('rename_sentmail_monthly');
31: if ($this->active &&
32: $GLOBALS['prefs']->isLocked('rename_sentmail_monthly')) {
33: $this->display = Horde_LoginTasks::DISPLAY_NONE;
34: }
35: }
36:
37: /**
38: * Renames the old sent-mail mailboxes.
39: *
40: * Mailbox name: sent-mail-month-year
41: * month = English: 3 letter abbreviation
42: * Other Languages: Month value (01-12)
43: * year = 4 digit year
44: *
45: * The mailbox name needs to be in this specific format (as opposed to a
46: * user-defined one) to ensure that 'delete_sentmail_monthly' processing
47: * can accurately find all the old sent-mail mailboxes.
48: *
49: * @return boolean Whether all sent-mail mailboxes were renamed.
50: */
51: public function execute()
52: {
53: global $notification;
54:
55: $date_format = (substr($GLOBALS['language'], 0, 2) == 'en')
56: ? 'M-Y'
57: : 'm-Y';
58:
59: $datetime = new DateTime();
60: $now = $datetime->format($date_format);
61:
62: foreach ($this->_getSentmail() as $sent) {
63: /* Display a message to the user and rename the mailbox.
64: * Only do this if sent-mail mailbox currently exists. */
65: if ($sent->exists) {
66: $notification->push(sprintf(_("\"%s\" mailbox being renamed at the start of the month."), $sent->display), 'horde.message');
67:
68: $query = new Horde_Imap_Client_Fetch_Query();
69: $query->imapDate();
70: $query->uid();
71:
72: $imp_imap = $sent->imp_imap;
73: $res = $imp_imap->fetch($sent, $query);
74:
75: $msgs = array();
76: foreach ($res as $val) {
77: $date_string = $val->getImapDate()->format($date_format);
78: if (!isset($msgs[$date_string])) {
79: $msgs[$date_string] = $imp_imap->getIdsOb();
80: }
81: $msgs[$date_string]->add($val->getUid());
82: }
83:
84: unset($msgs[$now]);
85: foreach ($msgs as $key => $val) {
86: $new_mbox = IMP_Mailbox::get(strval($sent) . '-' . Horde_String::lower($key));
87:
88: $imp_imap->copy($sent, $new_mbox, array(
89: 'create' => true,
90: 'ids' => $val,
91: 'move' => true
92: ));
93: }
94: }
95: }
96:
97: return true;
98: }
99:
100: /**
101: * Returns information for the login task.
102: *
103: * @return string Description of what the operation is going to do during
104: * this login.
105: */
106: public function describe()
107: {
108: $mbox_list = array();
109:
110: foreach ($this->_getSentmail() as $mbox) {
111: $mbox_list[] = $mbox->display_html;
112: }
113:
114: return sprintf(_("The current sent-mail mailbox(es) \"%s\" will be renamed."), implode(', ', $mbox_list));
115: }
116:
117: /**
118: * Returns the list of sent-mail mailboxes.
119: *
120: * @return array All sent-mail mailboxes (IMP_Mailbox objects).
121: */
122: protected function _getSentmail()
123: {
124: return $GLOBALS['injector']->getInstance('IMP_Identity')->getAllSentmail();
125: }
126:
127: }
128: