1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class IMP_LoginTasks_Task_RenameSentmailMonthly extends Horde_LoginTasks_Task
16: {
17: 18: 19:
20: public function __construct()
21: {
22: $this->active = $GLOBALS['prefs']->getValue('rename_sentmail_monthly');
23: if ($this->active &&
24: $GLOBALS['prefs']->isLocked('rename_sentmail_monthly')) {
25: $this->display = Horde_LoginTasks::DISPLAY_NONE;
26: }
27: }
28:
29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
43: public function execute()
44: {
45: global $injector, $notification;
46:
47: $date_format = (substr($GLOBALS['language'], 0, 2) == 'en')
48: ? 'M-Y'
49: : 'm-Y';
50:
51: $datetime = new DateTime();
52: $now = $datetime->format($date_format);
53:
54: $imp_imap = $injector->getInstance('IMP_Factory_Imap')->create();
55:
56: foreach ($this->_getSentmail() as $sent) {
57: 58:
59: if ($sent->exists) {
60: $notification->push(sprintf(_("%s mailbox being renamed at the start of the month."), $sent->display), 'horde.message');
61:
62: $query = new Horde_Imap_Client_Fetch_Query();
63: $query->imapDate();
64: $query->uid();
65:
66: $res = $imp_imap->fetch($sent, $query);
67:
68: $msgs = array();
69: foreach ($res as $val) {
70: $date_string = $val->getImapDate()->format($date_format);
71: if (!isset($msgs[$date_string])) {
72: $msgs[$date_string] = $imp_imap->getIdsOb();
73: }
74: $msgs[$date_string]->add($val->getUid());
75: }
76:
77: unset($msgs[$now]);
78: foreach ($msgs as $key => $val) {
79: $new_mbox = IMP_Mailbox::get(strval($sent) . '-' . Horde_String::convertCharset(Horde_String::lower($key), 'UTF-8', 'UTF7-IMAP'));
80:
81: $imp_imap->copy($sent, $new_mbox, array(
82: 'create' => true,
83: 'ids' => $val,
84: 'move' => true
85: ));
86: }
87: }
88: }
89:
90: return true;
91: }
92:
93: 94: 95: 96: 97: 98:
99: public function describe()
100: {
101: $mbox_list = array();
102:
103: foreach ($this->_getSentmail() as $mbox) {
104: $mbox_list[] = $mbox->display_html;
105: }
106:
107: return sprintf(_("The current sent-mail mailbox(es) \"%s\" will be renamed."), implode(', ', $mbox_list));
108: }
109:
110: 111: 112: 113: 114:
115: protected function _getSentmail()
116: {
117: return IMP_Mailbox::get($GLOBALS['injector']->getInstance('IMP_Identity')->getAllSentmailfolders());
118: }
119:
120: }
121: