~ubuntu-branches/ubuntu/vivid/php-horde-nag/vivid

« back to all changes in this revision

Viewing changes to nag-4.1.3/lib/LoginTasks/Task/PurgeCompleted.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-10-29 22:01:43 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20131029220143-k16crjhccwr56jxq
Tags: 4.1.3-1
New upstream version 4.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Login tasks module that purges completed tasks.
 
4
 *
 
5
 * Copyright 2012-2013 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 J Rubinsky <mrubinsk@horde.org>
 
11
 * @category Horde
 
12
 * @license  http://www.horde.org/licenses/gpl GPL
 
13
 * @package  Nag
 
14
 */
 
15
class Nag_LoginTasks_Task_PurgeCompleted extends Horde_LoginTasks_Task
 
16
{
 
17
    /**
 
18
     * Constructor.
 
19
     */
 
20
    public function __construct()
 
21
    {
 
22
        if ($this->interval = $GLOBALS['prefs']->getValue('purge_completed_interval')) {
 
23
            if ($GLOBALS['prefs']->isLocked('purge_completed_interval')) {
 
24
                $this->display = Horde_LoginTasks::DISPLAY_NONE;
 
25
            }
 
26
        } else {
 
27
            $this->active = false;
 
28
        }
 
29
    }
 
30
 
 
31
    /**
 
32
     * Purge completed tasks that were completed before the configured date.
 
33
     *
 
34
     * @return boolean  Whether any messages were purged from the mailbox.
 
35
     */
 
36
    public function execute()
 
37
    {
 
38
        global $injector, $prefs;
 
39
 
 
40
        /* Get the current UNIX timestamp minus the number of days specified
 
41
         * in 'purge_completed_keep'.  If a message has a timestamp prior to
 
42
         * this value, it will be deleted. */
 
43
        $del_time = new Horde_Date(time() - ($prefs->getValue('purge_completed_keep') * 86400));
 
44
        $del_time = $del_time->timestamp();
 
45
        $tasklists = Nag::listTasklists(true, Horde_Perms::DELETE, false);
 
46
        $tasks = Nag::listTasks(array(
 
47
            'completed' => Nag::VIEW_COMPLETE,
 
48
            'tasklists' => array_keys($tasklists),
 
49
            'include_history' => false)
 
50
        );
 
51
        $factory = $GLOBALS['injector']->getInstance('Nag_Factory_Driver');
 
52
        $count = 0;
 
53
        $tasks->reset();
 
54
        while ($task = $tasks->each()) {
 
55
            if (($task->completed_date) && $task->completed_date < $del_time) {
 
56
                try {
 
57
                    $factory->create($task->tasklist)->delete($task->id);
 
58
                    ++$count;
 
59
                } catch (Nag_Exception $e) {
 
60
                    Horde::logMessage($e->getMessage(), 'ERR');
 
61
                }
 
62
            }
 
63
        }
 
64
 
 
65
        $GLOBALS['notification']->push(
 
66
            sprintf(ngettext("Purging %d completed task.", "Purging %d completed tasks.", $count), $count), 'horde.message');
 
67
 
 
68
        return true;
 
69
    }
 
70
 
 
71
    /**
 
72
     * Return information for the login task.
 
73
     *
 
74
     * @return string  Description of what the operation is going to do during
 
75
     *                 this login.
 
76
     */
 
77
    public function describe()
 
78
    {
 
79
 
 
80
        return sprintf(
 
81
            _("All completed tasks older than %d days will be permanently deleted."),
 
82
            $GLOBALS['prefs']->getValue('purge_completed_keep')
 
83
        );
 
84
    }
 
85
 
 
86
}