Newer
Older
<?php
/**
* Admin_Cronjobs_LogsController
*
* Controller class for the logs of cronjobs
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
* @category Stud.IP
* @since 2.4
*/
class Admin_Cronjobs_LogsController extends AuthenticatedController
{
protected $_autobind = true;
/**
* Set up this controller.
*
* @param String $action Name of the action to be invoked
* @param Array $args Arguments to be passed to the action method
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
Navigation::activateItem('/admin/config/cronjobs');
PageLayout::setTitle(_('Cronjob-Verwaltung') . ' - ' . _('Log-Einträge'));
if (empty($_SESSION['cronlog-filter'])) {
$_SESSION['cronlog-filter'] = [
'where' => '1',
'values' => array_fill_keys(['status', 'schedule_id', 'task_id'], null),
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
];
}
// Setup sidebar
$sidebar = Sidebar::Get();
$actions = $sidebar->addWidget(new ViewsWidget());
$actions->addLink(
_('Cronjobs verwalten'),
$this->url_for('admin/cronjobs/schedules')
);
$actions->addLink(
_('Aufgaben verwalten'),
$this->url_for('admin/cronjobs/tasks')
);
$actions->addLink(
_('Logs anzeigen'),
$this->url_for('admin/cronjobs/logs')
)->setActive(true);
}
/**
* Displays all available log entries according to the set filters.
*
* @param int $page Which page to display
*/
public function index_action($page = 0)
{
$filter = $_SESSION['cronlog-filter'];
$this->total = CronjobLog::countBySql();
$this->pagination = Pagination::create(
CronjobLog::countBySql($filter['where']),
$page
);
$this->logs = $this->pagination->loadSORMCollection(
CronjobLog::class,
"{$filter['where']} ORDER BY executed DESC"
);
// Filters
$this->schedules = CronjobSchedule::findBySql('1');
$this->tasks = CronjobTask::findBySql('1');
$this->filter = $filter['values'];
}
/**
* Sets the filters for the log view.
* Filters are stored in the session.
*/
public function filter_action()
{
$filter = array_filter(Request::optionArray('filter'));
$conditions = [];
if (!empty($filter['status'])) {
$conditions[] = ($filter['status'] === 'passed')
? "exception IS NULL"
: "exception IS NOT NULL";
}
if (!empty($filter['schedule_id'])) {
$conditions[] = "schedule_id = " . DBManager::get()->quote($filter['schedule_id']);
}
if (!empty($filter['task_id'])) {
$temp = CronjobSchedule::findByTask_id($filter['task_id']);
$temp = SimpleORMapCollection::createFromArray($temp);
$schedule_ids = $temp->pluck('schedule_id') ?: null;
$conditions[] = "schedule_id IN (" . DBManager::get()->quote($schedule_ids). ")";
}
$_SESSION['cronlog-filter'] = [
'where' => implode(" AND " , $conditions) ?: '1',
'values' => $filter,
];
$this->redirect('admin/cronjobs/logs');
}
/**
* Sets the filters for the schedule view to a specific schedule id.
*
* @param String $schedule_id Id of the schedule in question
*/
public function schedule_action($schedule_id)
{
$this->redirect('admin/cronjobs/logs/filter?filter[schedule_id]=' . $schedule_id);
}
/**
* Sets the filters for the schedule view to a specific task id.
*
* @param String $task_id Id of the task in question
*/
public function task_action($task_id)
{
$this->redirect('admin/cronjobs/logs/filter?filter[task_id]=' . $task_id);
}
/**
* Displays a log entry.
*
* @param CronjobLog $log Log entry to display
*/
public function display_action(CronjobLog $log)
{
PageLayout::setTitle(sprintf(
_('Logeintrag für Cronjob "%s" anzeigen'),
$log->schedule->title
));
}
/**
* Deletes a log entry.
*
* @param CronjobLog $log Log entry to delete
*/
public function delete_action(CronjobLog $log, $page = 0)
{
$log->delete();
PageLayout::postSuccess(_('Der Logeintrag wurde gelöscht.'));
$this->redirect("admin/cronjobs/logs/index/{$page}");
}
/**
* Performs a bulk operation on a set of log entries. The only supported
* operation at the moment is deleting.
*
* @param int $page Return to this page afterwarsd (optional)
*/
public function bulk_action($page = 1)
{
$action = Request::option('action');
$ids = Request::optionArray('ids');
$logs = CronjobLog::findMany($ids);
if ($action === 'delete') {
foreach ($logs as $log) {
$log->delete();
}
$n = count($logs);
$message = sprintf(ngettext('%u Logeintrag wurde gelöscht.', '%u Logeinträge wurden gelöscht.', $n), $n);
PageLayout::postSuccess($message);
}
$this->redirect("admin/cronjobs/logs/index/{$page}");
}
}