| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - E4J srl |
| 6 |
* @copyright Copyright (C) 2024 E4J srl. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* Class handler for admin widget "Background tasks". |
| 15 |
* |
| 16 |
* @since 1.17 (J) - 1.7 (WP) |
| 17 |
*/ |
| 18 |
class VikBookingAdminWidgetBackgroundTasks extends VikBookingAdminWidget |
| 19 |
{ |
| 20 |
/** @var VBOCrontabSimulator */ |
| 21 |
private $crontab; |
| 22 |
|
| 23 |
/** |
| 24 |
* @inheritDOc |
| 25 |
*/ |
| 26 |
public function __construct() |
| 27 |
{ |
| 28 |
// call parent constructor |
| 29 |
parent::__construct(); |
| 30 |
|
| 31 |
$this->widgetName = 'Background Tasks'; |
| 32 |
$this->widgetDescr = 'Displays all the scheduled background tasks.'; |
| 33 |
$this->widgetId = basename(__FILE__, '.php'); |
| 34 |
|
| 35 |
$this->widgetIcon = '<i class="' . VikBookingIcons::i('server') . '"></i>'; |
| 36 |
$this->widgetStyleName = 'red'; |
| 37 |
|
| 38 |
try { |
| 39 |
$this->crontab = VBOFactory::getCrontabSimulator(); |
| 40 |
} catch (Throwable $e) { |
| 41 |
$this->crontab = []; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @inheritDoc |
| 47 |
*/ |
| 48 |
public function getPriority() |
| 49 |
{ |
| 50 |
// always display as last widget |
| 51 |
return 1; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @inheritDoc |
| 56 |
*/ |
| 57 |
public function preflight() |
| 58 |
{ |
| 59 |
// can be used only if we have at least a scheduled task |
| 60 |
return (bool) count($this->crontab); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @inheritDoc |
| 65 |
*/ |
| 66 |
public function render(?VBOMultitaskData $data = null) |
| 67 |
{ |
| 68 |
$schedules = []; |
| 69 |
|
| 70 |
foreach ($this->crontab as $runner) { |
| 71 |
$lastExecution = $this->crontab->getSemaphore()->getLastExecution($runner); |
| 72 |
$nextExecution = $runner->getNextExecution($lastExecution); |
| 73 |
|
| 74 |
$schedules[] = [ |
| 75 |
'name' => ucwords(str_replace('_', ' ', $runner->getID())), |
| 76 |
'last_execution' => JHtml::fetch('date', $lastExecution, 'Y-m-d H:i:s'), |
| 77 |
'next_execution' => JHtml::fetch('date', $nextExecution, 'Y-m-d H:i:s'), |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
$log = ''; |
| 82 |
|
| 83 |
try { |
| 84 |
// assume we are using a file logger |
| 85 |
$path = $this->crontab->getLogger()->getPath(); |
| 86 |
|
| 87 |
if (!JFile::exists($path)) { |
| 88 |
throw new Exception('Log file not found', 404); |
| 89 |
} |
| 90 |
|
| 91 |
$fp = fopen($path, 'r'); |
| 92 |
|
| 93 |
while (!feof($fp)) { |
| 94 |
$log .= fread($fp, 8192); |
| 95 |
} |
| 96 |
|
| 97 |
fclose($fp); |
| 98 |
} catch (Throwable $error) { |
| 99 |
// ignore log buffer |
| 100 |
} |
| 101 |
|
| 102 |
// display by using a specific layout provided by VCM |
| 103 |
echo JLayoutHelper::render( |
| 104 |
'sidepanel.widgets.crontab', |
| 105 |
[ |
| 106 |
'schedules' => $schedules, |
| 107 |
'log' => htmlentities($log), |
| 108 |
] |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|