Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Jun 22, 2024
1 parent f453497 commit f72493a
Show file tree
Hide file tree
Showing 23 changed files with 456 additions and 241 deletions.
46 changes: 17 additions & 29 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Craft;
use craft\base\Model;
use craft\events\RebuildConfigEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\helpers\UrlHelper;
use craft\services\ProjectConfig;
use craft\services\UserPermissions;
use craft\services\Utilities;
use craft\web\Response;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
Expand All @@ -25,6 +27,7 @@
use panlatent\schedule\services\Schedules;
use panlatent\schedule\services\Timers;
use panlatent\schedule\user\Permissions;
use panlatent\schedule\utilities\ActionRunner;
use panlatent\schedule\web\twig\CraftVariableBehavior;
use yii\base\Event;
use yii\console\Application as ConsoleApplication;
Expand Down Expand Up @@ -97,6 +100,7 @@ public function init(): void
$this->_registerProjectConfigEvents();
$this->_registerUserPermissions();
$this->_registerVariables();
$this->_registerUtilities();

if ($this->settings->enabledWebCron) {
$this->_registerWebCron();
Expand All @@ -111,29 +115,6 @@ public function getCpNavItem(): ?array
{
$ret = parent::getCpNavItem();
$ret['label'] = $this->getSettings()->getCustomCpNavName() ?? $this->name;

$user = Craft::$app->getUser();
if ($user->checkPermission(Permissions::MANAGE_SCHEDULES)) {
$ret['subnav']['schedules'] = [
'label' => Craft::t('schedule', 'Schedules'),
'url' => 'schedule',
];
}

if ($user->checkPermission(Permissions::MANAGE_LOGS)) {
$ret['subnav']['logs'] = [
'label' => Craft::t('schedule', 'Logs'),
'url' => 'schedule/logs',
];
}

if (Craft::$app->getConfig()->getGeneral()->allowAdminChanges && $user->getIsAdmin()) {
$ret['subnav']['settings'] = [
'label' => Craft::t('schedule', 'Settings'),
'url' => 'schedule/settings',
];
}

return $ret;
}

Expand Down Expand Up @@ -216,11 +197,11 @@ public function _registerCpRoutes(): void
'schedule/groups/<groupId:\d+|static>' => ['template' => 'schedule'],
'schedule/new' => 'schedule/schedules/edit-schedule',
'schedule/<scheduleId:\d+>' => 'schedule/schedules/edit-schedule',
'schedule/<scheduleId:\d+>/timers' => ['template' => 'schedule/timers'],
'schedule/<scheduleId:\d+>/timers/new' => 'schedule/timers/edit-timer',
'schedule/<scheduleId:\d+>/timers/<timerId:\d+>' => 'schedule/timers/edit-timer',
'schedule/<scheduleId:\d+>/logs' => ['template' => 'schedule/_logs'],
'schedule/<scheduleId:\d+>/logs/<logId:\d+>' => ['template' => 'schedule/logs/_view'],
// 'schedule/<scheduleId:\d+>/timers' => ['template' => 'schedule/timers'],
// 'schedule/<scheduleId:\d+>/timers/new' => 'schedule/timers/edit-timer',
// 'schedule/<scheduleId:\d+>/timers/<timerId:\d+>' => 'schedule/timers/edit-timer',
// 'schedule/<scheduleId:\d+>/logs' => ['template' => 'schedule/_logs'],
// 'schedule/<scheduleId:\d+>/logs/<logId:\d+>' => ['template' => 'schedule/logs/_view'],
]);
});
}
Expand All @@ -237,10 +218,17 @@ private function _registerVariables(): void
});
}

private function _registerWebCron()
private function _registerWebCron(): void
{
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function (RegisterUrlRulesEvent $event) {
$event->rules[$this->settings->endpoint] = 'schedule/web-cron/trigger';
});
}

private function _registerUtilities(): void
{
Event::on(Utilities::class, Utilities::EVENT_REGISTER_UTILITIES, function(RegisterComponentTypesEvent $event) {
$event->types[] = ActionRunner::class;
});
}
}
38 changes: 38 additions & 0 deletions src/actions/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use panlatent\craft\actions\abstract\Action;
use panlatent\craft\actions\abstract\ContextInterface;
use panlatent\schedule\Plugin;
use Symfony\Component\Process\Process;

class Command extends Action
Expand Down Expand Up @@ -52,4 +53,41 @@ public function execute(ContextInterface $context): bool

return $process->isSuccessful();
}

public function getSettingsHtml(): ?string
{
$suggestions = [];

$process = new Process([Plugin::getInstance()->getSettings()->getCliPath(), 'craft', 'help/list'], Craft::getAlias('@root'));
$process->run();

if ($process->isSuccessful()) {
$lines = explode("\n", mb_convert_encoding($process->getOutput(), mb_internal_encoding()));

$data = [];
foreach ($lines as $line) {
if (($pos = strpos($line, '/')) === false) {
$data[$line] = [];
continue;
}

$data[substr($line, 0, $pos)][] = [
'name' => $line,
'hint' => $line,
];
}

foreach ($data as $label => $commandSuggestions) {
$suggestions[] = [
'label' => $label,
'data' => $commandSuggestions,
];
}
}

return Craft::$app->getView()->renderTemplate('schedule/_components/actions/Command/settings', [
'schedule' => $this,
'suggestions' => $suggestions,
]);
}
}
41 changes: 19 additions & 22 deletions src/actions/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,35 @@ public function execute(ContextInterface $context): bool

public function getSettingsHtml(): ?string
{
$suggestions = [];
return Craft::$app->getView()->renderTemplate('schedule/_components/actions/Console/settings', [
'schedule' => $this,
]);
}

$process = new Process([Plugin::getInstance()->getSettings()->getCliPath(), 'craft', 'help/list'], Craft::getAlias('@root'));
public function getCommandOptions(): array
{
$options = [];

$process = new Process([Plugin::getInstance()->getSettings()->getCliPath(), 'craft', 'help'], Craft::getAlias('@root'));
$process->run();

if ($process->isSuccessful()) {
$lines = explode("\n", mb_convert_encoding($process->getOutput(), mb_internal_encoding()));

$data = [];
foreach ($lines as $line) {
if (($pos = strpos($line, '/')) === false) {
$data[$line] = [];
if (str_starts_with($line, '-')) {
$options[] = ['optgroup' => substr($line, 2)];
continue;
}

$data[substr($line, 0, $pos)][] = [
'name' => $line,
'hint' => $line,
];
}

foreach ($data as $label => $commandSuggestions) {
$suggestions[] = [
'label' => $label,
'data' => $commandSuggestions,
];
if (preg_match('#^\s*(\w+/\w+)\s*(?:\(\w+\)|)\s+(.+)\s*$#', $line, $match)) {
$options[] = [
'label' => $match[1] . ' - ' . $match[2], //substr($line, 0, $pos),
'value' => $match[1],
];
}
}
}

return Craft::$app->getView()->renderTemplate('schedule/_components/schedules/Console/settings', [
'schedule' => $this,
'suggestions' => $suggestions,
]);
return $options;
}

}
2 changes: 1 addition & 1 deletion src/base/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class Timer extends SavableComponent implements TimerInterface
public function rules(): array
{
$rules = parent::rules();
$rules[] = [['scheduleId', 'enabled'], 'required'];
$rules[] = [['enabled'], 'required'];
$rules[] = [['scheduleId', 'sortOrder'], 'integer'];
$rules[] = [['enabled'], 'boolean'];
return $rules;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/SchedulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function actionEditSchedule(int $scheduleId = null, Schedule $schedule =
}
}

$isNewSchedule = !$schedule;
$isNewSchedule = !$schedule->id;

$allGroups = $schedules->getAllGroups();
$allActionTypes = Plugin::getInstance()->actions->getAllActionTypes();
Expand Down
10 changes: 10 additions & 0 deletions src/errors/ActionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace panlatent\schedule\errors;

use yii\base\Exception;

class ActionException extends Exception
{

}
64 changes: 58 additions & 6 deletions src/models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use Craft;
use craft\base\Model;
use craft\validators\HandleValidator;
use craft\validators\UniqueValidator;
use DateTime;
use panlatent\craft\actions\abstract\ActionInterface;
use panlatent\schedule\base\TimerInterface;
use panlatent\schedule\log\LogAdapter;
use panlatent\schedule\Plugin;
use panlatent\schedule\records\Schedule as ScheduleRecord;
use Psr\Log\LoggerInterface;
use function Arrayy\array_first;

/**
* @property-read ScheduleGroup $group
Expand All @@ -31,19 +36,29 @@ class Schedule extends Model

public ?ActionInterface $action = null;

public ?TimerInterface $timer = null;
// public ?TimerInterface $timer = null;

public ?int $timeout = null;

public ?int $retry = null;

public $onSuccess = null;

public $onFailed = null;

/**
* @var bool
*/
public bool $enabled = true;

/**
* @var bool|null
*/
public ?bool $enabledLog = null;
// /**
// * @var bool|null
// */
//public ?bool $enabledLog = null;




// public $timeout;

/**
* @var int|null
Expand Down Expand Up @@ -75,6 +90,21 @@ public function getInfo(): ScheduleInfo
return $this->_info;
}

private ?TimerInterface $_timer = null;

public function getTimer(): TimerInterface
{
if ($this->_timer === null) {
$this->_timer = Plugin::getInstance()->timers->getTimerByScheduleId($this->id);
}
return $this->_timer;
}

public function setTimer(TimerInterface $timer): void
{

}

public function canRun(): bool
{
return true;
Expand Down Expand Up @@ -115,4 +145,26 @@ protected function getLogger(): LoggerInterface
{
return new LogAdapter(Craft::$app->getLog()->getLogger(), 'schedule');
}

protected function defineRules(): array
{
return [
[['name', 'handle'], 'required'],
[['id', 'groupId'], 'integer'],
[['name', 'handle', 'description'], 'string'],
[['handle'], UniqueValidator::class, 'targetClass' => ScheduleRecord::class, 'targetAttribute' => 'handle'],
[['handle'], HandleValidator::class],
[['static'], 'boolean'],
[['action'], function($attribute) {if (!$this->$attribute->validate()) {
$this->addError($attribute, array_first($this->$attribute->getFirstErrors()));
}}],

[['timer'], function($attribute) {
if (!$this->$attribute->validate()) {
$errors = $this->$attribute->getFirstErrors();
$this->addError($attribute, reset($errors));
}
}],
];
}
}
11 changes: 10 additions & 1 deletion src/records/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
namespace panlatent\schedule\records;

use craft\db\ActiveRecord;
use panlatent\schedule\db\Table;

/**
* @property int $id
* @property string $type
* @property string $settings
*/
class Action extends ActiveRecord
{

public static function tableName(): string
{
return Table::ACTIONS;
}
}
1 change: 0 additions & 1 deletion src/records/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @property int $onSuccess
* @property int $onFailed
* @property bool $enabled
* @property bool $enabledLog
* @property int $sortOrder
* @author Panlatent <[email protected]>
*/
Expand Down
7 changes: 1 addition & 6 deletions src/records/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* @property int $id
* @property int $scheduleId
* @property string $type
* @property string $minute
* @property string $hour
* @property string $day
* @property string $month
* @property string $week
* @property string $settings
* @property bool $enabled
* @property int $sortOrder
Expand All @@ -34,6 +29,6 @@ class Timer extends ActiveRecord
*/
public static function tableName(): string
{
return Table::SCHEDULETIMERS;
return Table::TIMERS;
}
}
Loading

0 comments on commit f72493a

Please sign in to comment.