Completed
Push — master ( b35b0f...acc097 )
by Greg
05:21
created

Scheduler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * /classes/DomainMOD/Scheduler.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class Scheduler
25
{
26
    public $system;
27
    public $time;
28
29
    public function __construct()
30
    {
31
        $this->system = new System();
32
        $this->time = new Time();
33
    }
34
35
    public function isRunning($task_id)
36
    {
37
        $tmpq = $this->system->db()->prepare("
38
            UPDATE scheduler
39
            SET is_running = '1'
40
            WHERE id = :task_id");
41
        $tmpq->execute(array('task_id' => $task_id));
42
    }
43
44
    public function isFinished($task_id)
45
    {
46
        $tmpq = $this->system->db()->prepare("
47
            UPDATE scheduler
48
            SET is_running = '0'
49
            WHERE id = :task_id");
50
        $tmpq->execute(array('task_id' => $task_id));
51
    }
52
53
    public function updateTime($task_id, $timestamp, $next_run)
54
    {
55
        $current_time = $this->time->stamp();
56
        $duration = $this->getTimeDifference($timestamp, $current_time);
57
58
        $tmpq = $this->system->db()->prepare("
59
            UPDATE scheduler
60
            SET last_run = :last_run,
61
                last_duration = :last_duration,
62
                next_run = :next_run
63
            WHERE id = :task_id");
64
        $tmpq->execute(array(
65
                       'last_run' => $current_time,
66
                       'last_duration' => $duration,
67
                       'next_run' => $next_run,
68
                       'task_id' => $task_id));
69
    }
70
71
    public function getTimeDifference($start_time, $end_time)
72
    {
73
        $difference = (strtotime($end_time) - strtotime($start_time));
74
        $minutes = intval($difference / 60);
75
        $seconds = $difference - ($minutes * 60);
76
        if ($minutes != '0') {
77
            $result = " (<em>" . $minutes . "m " . $seconds . "s</em>)";
78
        } else {
79
            $result = " (<em>" . $seconds . "s</em>)";
80
        }
81
        return $result;
82
    }
83
84
    public function getTask($task_id)
85
    {
86
        $tmpq = $this->system->db()->prepare("
87
            SELECT id, `name`, description, `interval`, expression, last_run, last_duration, next_run, active
88
            FROM scheduler
89
            WHERE id = :task_id
90
            ORDER BY sort_order ASC");
91
        $tmpq->execute(array('task_id' => $task_id));
92
        return $tmpq->fetch();
93
    }
94
95
    public function createActive($active, $task_id)
96
    {
97
        $result = '<strong><font color=\'green\'>Active</font></strong> [<a href=\'update.php?a=d&id=' . $task_id .
98
            '\'>disable</a>] [<a href=\'run.php?id=' . $task_id . '\'>run now</a>]';
99
        if ($active == '0') {
100
            $result = '<strong><font color=\'red\'>Inactive</font></strong> [<a href=\'update.php?a=e&id=' . $task_id .
101
                '\'>enable</a>] [<a href=\'run.php?id=' . $task_id . '\'>run now</a>]';
102
        }
103
        return $result;
104
    }
105
106
    public function getDateOutput($next_run)
107
    {
108
        if ($next_run == '1978-01-23 00:00:00') {
109
            return 'n/a';
110
        } else {
111
            return $next_run;
112
        }
113
    }
114
115
    public function hourSelect($hour)
116
    {
117
        $hours = array('00' => '00:00', '01' => '01:00', '02' => '02:00', '03' => '03:00', '04' => '04:00',
118
                       '05' => '05:00', '06' => '06:00', '07' => '07:00', '08' => '08:00', '09' => '09:00',
119
                       '10' => '10:00', '11' => '11:00', '12' => '12:00', '13' => '13:00', '14' => '14:00',
120
                       '15' => '15:00', '16' => '16:00', '17' => '17:00', '18' => '18:00', '19' => '19:00',
121
                       '20' => '20:00', '21' => '21:00', '22' => '22:00', '23' => '23:00');
122
        ob_start();
123
        foreach ($hours as $key => $value) { ?>
124
            <option value="<?php echo $key; ?>"<?php if ($hour == $key) echo ' selected'; ?>><?php echo $value; ?></option><?php
125
        }
126
        return ob_get_clean();
127
    }
128
129
} //@formatter:on
130