Skip to content

Commit e038933

Browse files
authored
[5.3] Move code to log model in new scheduler history view (#44587)
1 parent 8c67273 commit e038933

File tree

8 files changed

+105
-149
lines changed

8 files changed

+105
-149
lines changed

administrator/components/com_scheduler/forms/filter_logs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@
6161
class="js-select-submit-on-change"
6262
/>
6363
</fields>
64-
</form>
64+
</form>

administrator/components/com_scheduler/src/Controller/LogsController.php

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
namespace Joomla\Component\Scheduler\Administrator\Controller;
1212

13-
use Joomla\CMS\Access\Exception\NotAllowed;
1413
use Joomla\CMS\Language\Text;
1514
use Joomla\CMS\MVC\Controller\AdminController;
16-
use Joomla\Utilities\ArrayHelper;
1715

1816
// phpcs:disable PSR1.Files.SideEffects
1917
\defined('_JEXEC') or die;
@@ -46,7 +44,7 @@ class LogsController extends AdminController
4644
*
4745
* @since 5.3.0
4846
*/
49-
public function getModel($name = 'Logs', $prefix = 'Administrator', $config = ['ignore_request' => true])
47+
public function getModel($name = 'Log', $prefix = 'Administrator', $config = ['ignore_request' => true])
5048
{
5149
return parent::getModel($name, $prefix, $config);
5250
}
@@ -73,39 +71,4 @@ public function purge()
7371

7472
$this->setRedirect('index.php?option=com_scheduler&view=logs', $message);
7573
}
76-
77-
/**
78-
* Removes an item.
79-
*
80-
* Overrides Joomla\CMS\MVC\Controller\FormController::delete to check the core.admin permission.
81-
*
82-
* @return void
83-
*
84-
* @since 5.3.0
85-
*/
86-
public function delete(): void
87-
{
88-
$ids = $this->input->get('cid', [], 'array');
89-
90-
if (!$this->app->getIdentity()->authorise('core.admin', $this->option)) {
91-
throw new NotAllowed(Text::_('JERROR_ALERTNOAUTHOR'), 403);
92-
}
93-
94-
if (empty($ids)) {
95-
$this->setMessage(Text::_('COM_SCHEDULER_NO_LOGS_SELECTED'), 'warning');
96-
$this->setRedirect('index.php?option=com_scheduler&view=logs');
97-
return;
98-
}
99-
100-
// Get the model.
101-
$model = $this->getModel();
102-
$ids = ArrayHelper::toInteger($ids);
103-
104-
// Remove the items.
105-
if ($model->delete($ids)) {
106-
$this->setMessage(Text::plural('COM_SCHEDULER_N_ITEMS_DELETED', \count($ids)));
107-
}
108-
109-
$this->setRedirect('index.php?option=com_scheduler&view=logs');
110-
}
11174
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage com_scheduler
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Component\Scheduler\Administrator\Model;
12+
13+
use Joomla\CMS\Form\Form;
14+
use Joomla\CMS\MVC\Model\AdminModel;
15+
use Joomla\CMS\Table\Table;
16+
17+
// phpcs:disable PSR1.Files.SideEffects
18+
\defined('_JEXEC') or die;
19+
// phpcs:enable PSR1.Files.SideEffects
20+
21+
/**
22+
* MVC Model to interact with the Scheduler logs.
23+
*
24+
* @since __DPELOY_VERSION__
25+
*/
26+
class LogModel extends AdminModel
27+
{
28+
/**
29+
* There is no form for the log.
30+
*
31+
* @param array $data Data that needs to go into the form
32+
* @param bool $loadData Should the form load its data from the DB?
33+
*
34+
* @return Form|boolean A Form object on success, false on failure.
35+
*
36+
* @since __DEPLOY_VERSION__
37+
* @throws \Exception
38+
*/
39+
public function getForm($data = [], $loadData = true)
40+
{
41+
return false;
42+
}
43+
44+
/**
45+
* Method to get a table object, load it if necessary.
46+
*
47+
* @param string $name The table name. Optional.
48+
* @param string $prefix The class prefix. Optional.
49+
* @param array $options Configuration array for model. Optional.
50+
*
51+
* @return Table
52+
*
53+
* @since __DEPLOY_VERSION__
54+
* @throws \Exception
55+
*/
56+
public function getTable($name = 'Log', $prefix = 'Administrator', $options = []): Table
57+
{
58+
return parent::getTable($name, $prefix, $options);
59+
}
60+
}

administrator/components/com_scheduler/src/Model/LogsModel.php

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// phpcs:enable PSR1.Files.SideEffects
1616

1717
use Joomla\CMS\Component\ComponentHelper;
18-
use Joomla\CMS\Factory;
1918
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
2019
use Joomla\CMS\MVC\Model\ListModel;
2120
use Joomla\Component\Scheduler\Administrator\Helper\SchedulerHelper;
@@ -185,91 +184,4 @@ protected function getListQuery()
185184

186185
return $query;
187186
}
188-
189-
/**
190-
* Delete rows.
191-
*
192-
* @param array $pks The ids of the items to delete.
193-
*
194-
* @return boolean Returns true on success, false on failure.
195-
*/
196-
public function delete($pks)
197-
{
198-
if ($this->canDelete($pks)) {
199-
// Delete logs from list
200-
$db = $this->getDatabase();
201-
$query = $db->getQuery(true)
202-
->delete($db->quoteName('#__scheduler_logs'))
203-
->whereIn($db->quoteName('id'), $pks);
204-
205-
$db->setQuery($query);
206-
$this->setError((string) $query);
207-
208-
try {
209-
$db->execute();
210-
} catch (\RuntimeException $e) {
211-
$this->setError($e->getMessage());
212-
213-
return false;
214-
}
215-
} else {
216-
Factory::getApplication()->enqueueMessage(Text::_('JERROR_CORE_DELETE_NOT_PERMITTED'), 'error');
217-
}
218-
219-
return true;
220-
}
221-
222-
/**
223-
* Determine whether a record may be deleted taking into consideration
224-
* the user's permissions over the record.
225-
*
226-
* @param object $record The database row/record in question
227-
*
228-
* @return boolean True if the record may be deleted
229-
*
230-
* @since 5.3.0
231-
* @throws \Exception
232-
*/
233-
protected function canDelete($record): bool
234-
{
235-
// Record doesn't exist, can't delete
236-
if (empty($record)) {
237-
return false;
238-
}
239-
240-
return Factory::getApplication()->getIdentity()->authorise('core.delete', 'com_scheduler');
241-
}
242-
243-
/**
244-
* @param array $data The task execution data.
245-
*
246-
* @return void
247-
*
248-
* @since 5.3.0
249-
* @throws Exception
250-
*/
251-
public function logTask(array $data): void
252-
{
253-
$model = Factory::getApplication()->bootComponent('com_scheduler')
254-
->getMVCFactory()->createModel('Task', 'Administrator', ['ignore_request' => true]);
255-
$taskInfo = $model->getItem($data['TASK_ID']);
256-
$taskOptions = SchedulerHelper::getTaskOptions();
257-
$safeTypeTitle = $taskOptions->findOption($taskInfo->type)->title ?? '';
258-
$duration = ($data['TASK_DURATION'] ?? 0);
259-
$created = Factory::getDate()->toSql();
260-
261-
/** @var \Joomla\Component\Schduler\Administrator\Table\LogsTable $table */
262-
$logsTable = $this->getTable();
263-
$logsTable->tasktype = $safeTypeTitle;
264-
$logsTable->taskname = $data['TASK_TITLE'];
265-
$logsTable->duration = $duration;
266-
$logsTable->jobid = $data['TASK_ID'];
267-
$logsTable->exitcode = $data['EXIT_CODE'];
268-
$logsTable->taskid = $data['TASK_TIMES'];
269-
$logsTable->lastdate = $created;
270-
$logsTable->nextdate = $taskInfo->next_execution;
271-
272-
// Log the execution of the task.
273-
$logsTable->store();
274-
}
275187
}

administrator/components/com_scheduler/src/Model/TaskModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ protected function populateState(): void
254254
* @since 4.1.0
255255
* @throws \Exception
256256
*/
257-
public function getTable($name = 'Task', $prefix = 'Table', $options = []): Table
257+
public function getTable($name = 'Task', $prefix = 'Administrator', $options = []): Table
258258
{
259259
return parent::getTable($name, $prefix, $options);
260260
}

administrator/components/com_scheduler/src/Table/LogsTable.php renamed to administrator/components/com_scheduler/src/Table/LogTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @since 5.3.0
2525
*/
26-
class LogsTable extends Table
26+
class LogTable extends Table
2727
{
2828
/**
2929
* Constructor

administrator/language/en-GB/com_scheduler.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ COM_SCHEDULER_LAST_RUN_ASC="Last Run ascending"
9494
COM_SCHEDULER_LAST_RUN_DATE="Last Run Date"
9595
COM_SCHEDULER_LAST_RUN_DESC="Last Run descending"
9696
COM_SCHEDULER_LOGS_CLEAR="All execution history logs have been deleted."
97+
COM_SCHEDULER_LOGS_N_ITEMS_DELETED="%d logs deleted."
98+
COM_SCHEDULER_LOGS_N_ITEMS_DELETED_1="Log deleted."
9799
COM_SCHEDULER_MANAGER_TASKS="Scheduled Tasks"
98100
COM_SCHEDULER_MANAGER_TASK_EDIT="Edit Task"
99101
COM_SCHEDULER_MANAGER_TASK_NEW="New Task"
@@ -119,7 +121,6 @@ COM_SCHEDULER_N_ITEMS_UNPUBLISHED="%d tasks disabled."
119121
COM_SCHEDULER_N_ITEMS_UNPUBLISHED_1="Task disabled."
120122
COM_SCHEDULER_N_ITEMS_UNLOCKED="%d tasks unlocked."
121123
COM_SCHEDULER_N_ITEMS_UNLOCKED_1="Task unlocked."
122-
COM_SCHEDULER_NO_LOGS_SELECTED="No execution history logs selected."
123124
COM_SCHEDULER_OPTION_EXECUTION_MANUAL_LABEL="Manual Execution"
124125
COM_SCHEDULER_OPTION_ORPHANED_HIDE="Hide Orphaned"
125126
COM_SCHEDULER_OPTION_ORPHANED_ONLY="Only Orphaned"

plugins/system/tasknotification/src/Extension/TaskNotification.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
use Joomla\CMS\Event\Model;
1414
use Joomla\CMS\Factory;
15-
use Joomla\CMS\Form\Form;
1615
use Joomla\CMS\Log\Log;
1716
use Joomla\CMS\Mail\MailTemplate;
1817
use Joomla\CMS\Plugin\CMSPlugin;
1918
use Joomla\CMS\User\UserFactoryAwareTrait;
19+
use Joomla\Component\Scheduler\Administrator\Helper\SchedulerHelper;
2020
use Joomla\Component\Scheduler\Administrator\Task\Status;
2121
use Joomla\Component\Scheduler\Administrator\Task\Task;
2222
use Joomla\Database\DatabaseAwareTrait;
@@ -125,13 +125,8 @@ public function notifyFailure(Event $event): void
125125
{
126126
/** @var Task $task */
127127
$task = $event->getArgument('subject');
128-
129-
// @todo safety checks, multiple files [?]
130-
$outFile = $event->getArgument('subject')->snapshot['output_file'] ?? '';
131-
$data = $this->getDataFromTask($event->getArgument('subject'));
132-
$model = $this->getApplication()->bootComponent('com_scheduler')
133-
->getMVCFactory()->createModel('Task', 'Administrator', ['ignore_request' => true]);
134-
$model->logTask($data);
128+
$data = $this->getDataFromTask($event->getArgument('subject'));
129+
$this->saveLog($data);
135130

136131
if (!(int) $task->get('params.notifications.failure_mail', 1)) {
137132
return;
@@ -140,6 +135,8 @@ public function notifyFailure(Event $event): void
140135
// Load translations
141136
$this->loadLanguage();
142137

138+
// @todo safety checks, multiple files [?]
139+
$outFile = $event->getArgument('subject')->snapshot['output_file'] ?? '';
143140
$this->sendMail('plg_system_tasknotification.failure_mail', $data, $outFile);
144141
}
145142

@@ -185,20 +182,18 @@ public function notifySuccess(Event $event): void
185182
{
186183
/** @var Task $task */
187184
$task = $event->getArgument('subject');
188-
189-
// @todo safety checks, multiple files [?]
190-
$outFile = $event->getArgument('subject')->snapshot['output_file'] ?? '';
191-
$data = $this->getDataFromTask($event->getArgument('subject'));
192-
$model = $this->getApplication()->bootComponent('com_scheduler')
193-
->getMVCFactory()->createModel('Logs', 'Administrator', ['ignore_request' => true]);
194-
$model->logTask($data);
185+
$data = $this->getDataFromTask($event->getArgument('subject'));
186+
$this->saveLog($data);
195187

196188
if (!(int) $task->get('params.notifications.success_mail', 0)) {
197189
return;
198190
}
199191

200192
// Load translations
201193
$this->loadLanguage();
194+
195+
// @todo safety checks, multiple files [?]
196+
$outFile = $event->getArgument('subject')->snapshot['output_file'] ?? '';
202197
$this->sendMail('plg_system_tasknotification.success_mail', $data, $outFile);
203198
}
204199

@@ -214,10 +209,7 @@ public function notifySuccess(Event $event): void
214209
*/
215210
public function notifyWillResume(Event $event): void
216211
{
217-
$data = $this->getDataFromTask($event->getArgument('subject'));
218-
$model = $this->getApplication()->bootComponent('com_scheduler')
219-
->getMVCFactory()->createModel('Logs', 'Administrator', ['ignore_request' => true]);
220-
$model->logTask($data);
212+
$this->saveLog($this->getDataFromTask($event->getArgument('subject')));
221213
}
222214

223215
/**
@@ -341,4 +333,32 @@ private function sendMail(string $template, array $data, string $attachment = ''
341333
Log::add($this->getApplication()->getLanguage()->_('PLG_SYSTEM_TASK_NOTIFICATION_NO_MAIL_SENT'), Log::WARNING);
342334
}
343335
}
336+
337+
/**
338+
* @param array $data The form data
339+
*
340+
* @return void
341+
*
342+
* @since __DEPLOY_VERSION__
343+
* @throws \Exception
344+
*/
345+
private function saveLog(array $data): void
346+
{
347+
$model = $this->getApplication()->bootComponent('com_scheduler')->getMVCFactory()->createModel('Task', 'Administrator', ['ignore_request' => true]);
348+
$taskInfo = $model->getItem($data['TASK_ID']);
349+
350+
$obj = new \stdClass();
351+
$obj->tasktype = SchedulerHelper::getTaskOptions()->findOption($taskInfo->type)->title ?? '';
352+
$obj->taskname = $data['TASK_TITLE'];
353+
$obj->duration = $data['TASK_DURATION'] ?? 0;
354+
$obj->jobid = $data['TASK_ID'];
355+
$obj->exitcode = $data['EXIT_CODE'];
356+
$obj->taskid = $data['TASK_TIMES'];
357+
$obj->lastdate = Factory::getDate()->toSql();
358+
$obj->nextdate = $taskInfo->next_execution;
359+
360+
$model = $this->getApplication()->bootComponent('com_scheduler')
361+
->getMVCFactory()->createModel('Log', 'Administrator', ['ignore_request' => true]);
362+
$model->save((array)$obj);
363+
}
344364
}

0 commit comments

Comments
 (0)