Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Jun 21, 2024
1 parent bd274a9 commit 30507f5
Showing 22 changed files with 400 additions and 149 deletions.
2 changes: 1 addition & 1 deletion UPGRADE-1.x.md
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ This is a 0.x - 1.0 update note. This article assumes that you have already used

## Documentation Promise

The new version will promise a relatively complete documentation.py
The new version will promise a relatively complete documentation

## Schedule

2 changes: 1 addition & 1 deletion abstract/Action.php
Original file line number Diff line number Diff line change
@@ -6,5 +6,5 @@

abstract class Action extends SavableComponent implements ActionInterface
{

public ?string $uid = null;
}
4 changes: 3 additions & 1 deletion abstract/ActionInterface.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,9 @@

namespace panlatent\craft\actions\abstract;

interface ActionInterface
use craft\base\SavableComponentInterface;

interface ActionInterface extends SavableComponentInterface
{
public function execute(ContextInterface $context): bool;
}
34 changes: 34 additions & 0 deletions abstract/ConditionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace panlatent\craft\actions\abstract;

class ConditionAction extends Action
{
public function __construct(private readonly ActionInterface $action, $config = [])
{
parent::__construct($config);
}

public function getConditions(): array
{
return [];
}

public function getAction(): ActionInterface
{
return $this->action;
}

public function validateConditions(): bool
{
return true;
}

public function execute(ContextInterface $context): bool
{



return $this->action->execute($context);
}
}
1 change: 0 additions & 1 deletion abstract/TriggerInterface.php
Original file line number Diff line number Diff line change
@@ -4,5 +4,4 @@

interface TriggerInterface
{
public function handle(): bool;
}
6 changes: 2 additions & 4 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
@@ -87,10 +87,8 @@ public function runSchedule(Schedule $schedule): bool
*/
public function getTriggerTimers(): array
{
$timers = Plugin::getInstance()->timers->getActiveTimers();
$now = new \DateTime('now', $this->timezone);
return array_filter($timers, static function (TimerInterface $timer) use($now) {
return (new CronExpression($timer->getCronExpression()))->isDue($now);
return array_filter(Plugin::getInstance()->timers->getActiveTimers(), static function (TimerInterface $timer) {
return $timer->isDue();
});
}

61 changes: 61 additions & 0 deletions src/actions/ElementAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace panlatent\schedule\actions;

use Craft;
use craft\base\ElementInterface;
use craft\elements\Entry;
use panlatent\craft\actions\abstract\Action;
use panlatent\craft\actions\abstract\ContextInterface;

class ElementAction extends Action
{
public string $elementType = Entry::class;

public ?string $elementAction = null;

public $query;

public function execute(ContextInterface $context): bool
{
return true;
}

public function getSettingsHtml(): ?string
{
$elementTypes = Craft::$app->getElements()->getAllElementTypes();

$elementTypeOptions = [];
$allElementActionOptions = [];
$allElementSourceOptions = [];
foreach ($elementTypes as $elementType) {
/** @var ElementInterface|string $elementType */
$elementTypeOptions[] = ['label' => $elementType::displayName(), 'value' => $elementType];
foreach ($elementType::actions('*') as $action) {
$allElementActionOptions[$elementType][] = [
'label' => $action['label'] ?? $action::displayName(),
'value' => $action['type'] ?? $action,
];
}

$allElementSourceOptions[$elementType] = [];
foreach($elementType::sources('index') as $source) {
if (isset($source['heading'])) {
continue;
}
$allElementSourceOptions[$elementType][] = [
'label' => $source['label'],
'value' => $source['key'],
'enabled' => false,
];
}
}

return Craft::$app->getView()->renderTemplate('schedule/_components/actions/ElementAction/settings', [
'action' => $this,
'elementTypeOptions' => $elementTypeOptions,
'allElementActionOptions' => $allElementActionOptions,
'allElementSourceOptions' => $allElementSourceOptions,
]);
}
}
20 changes: 2 additions & 18 deletions src/base/Timer.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@

use Craft;
use craft\base\SavableComponent;
use panlatent\schedule\helpers\CronHelper;
use panlatent\schedule\models\Schedule;
use panlatent\schedule\Plugin;
use yii\base\InvalidConfigException;
@@ -39,16 +38,6 @@ abstract class Timer extends SavableComponent implements TimerInterface
// Public Methods
// =========================================================================

/**
* @return string
*/
public function __toString()
{
return Craft::t('schedule', '# {order}' , [
'order' => (int)$this->sortOrder
]);
}

/**
* @return array
*/
@@ -61,17 +50,12 @@ public function rules(): array
return $rules;
}

public function handle(): bool
{
return $this->getSchedule()->run();
}

/**
* @inheritdoc
* @deprecated
*/
public function isValid(): bool
{
return $this->getSchedule()->isValid() && $this->enabled;
return true;
}

/**
6 changes: 3 additions & 3 deletions src/base/TimerInterface.php
Original file line number Diff line number Diff line change
@@ -22,11 +22,11 @@ interface TimerInterface extends TriggerInterface
* @see \panlatent\schedule\services\Timers::getAllTimers()
*
* @return bool whether to run the timer.
* @deprecated since 1.0.0
*/
public function isValid(): bool;

/**
* @return Schedule
*/
public function isDue(): bool;

public function getSchedule(): Schedule;
}
18 changes: 0 additions & 18 deletions src/base/TimerTrait.php
Original file line number Diff line number Diff line change
@@ -15,26 +15,8 @@
*/
trait TimerTrait
{
// Properties
// =========================================================================

/**
* @var int|null
*/
public ?int $scheduleId = null;

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

/**
* @var int|null
*/
public ?int $sortOrder = null;

/**
* @var string|null
*/
public ?string $uid = null;
}
6 changes: 6 additions & 0 deletions src/helpers/CronHelper.php
Original file line number Diff line number Diff line change
@@ -65,4 +65,10 @@ public static function toDescription(array|string $expression): string

return $descriptor->getDescription();
}

public function nextTime(string $expression, int $total = 5): int
{
$cron = new \Cron\CronExpression($expression);
return $cron->getNextRunDate($time)->getTimeStamp();
}
}
26 changes: 11 additions & 15 deletions src/models/Schedule.php
Original file line number Diff line number Diff line change
@@ -12,29 +12,15 @@

/**
* @property-read ScheduleGroup $group
* @property-read ScheduleInfo $info
* @since 1.0.0
*/
class Schedule extends Model
{
public ?int $id = null;
/**
* @var int|null
*/
public ?int $groupId = null;

/**
* @var string|null
*/
public ?string $name = null;

/**
* @var string|null
*/
public ?string $handle = null;

/**
* @var string|null
*/
public ?string $description = null;

/**
@@ -70,6 +56,8 @@ class Schedule extends Model

public ?DateTime $dateUpdated = null;

private ?ScheduleInfo $_info = null;

/**
* @return array
* @todo
@@ -79,6 +67,14 @@ public function getConditions(): array
return [];
}

public function getInfo(): ScheduleInfo
{
if ($this->_info === null) {
$this->_info = new ScheduleInfo();
}
return $this->_info;
}

public function canRun(): bool
{
return true;
11 changes: 11 additions & 0 deletions src/models/ScheduleInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace panlatent\schedule\models;

class ScheduleInfo
{
public function getLastFinishedTime(): ?\DateTime
{

}
}
10 changes: 10 additions & 0 deletions src/records/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace panlatent\schedule\records;

use craft\db\ActiveRecord;

class Action extends ActiveRecord
{

}
10 changes: 10 additions & 0 deletions src/services/Actions.php
Original file line number Diff line number Diff line change
@@ -8,9 +8,12 @@
use craft\events\RegisterComponentTypesEvent;
use craft\helpers\ArrayHelper;
use craft\helpers\Component as ComponentHelper;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use panlatent\craft\actions\abstract\ActionInterface;
use panlatent\schedule\actions\Command;
use panlatent\schedule\actions\Console;
use panlatent\schedule\actions\ElementAction;
use panlatent\schedule\actions\HttpRequest;
use panlatent\schedule\actions\SendEmail;
use panlatent\schedule\db\Table;
@@ -55,6 +58,7 @@ public function getAllActionTypes(): array
'types' => [
Command::class,
Console::class,
ElementAction::class,
HttpRequest::class,
SendEmail::class,
]
@@ -119,6 +123,12 @@ public function saveAction(ActionInterface $action, bool $runValidation = true):
return false;
}

if ($isNew) {
$action->uid = StringHelper::UUID();
} elseif ($action->uid === null) {
$action->uid = Db::uidById(Table::ACTIONS, $action->id);
}

if ($runValidation && !$action->validate()) {
Craft::info('Action not saved due to validation error.', __METHOD__);
return false;
15 changes: 1 addition & 14 deletions src/services/Schedules.php
Original file line number Diff line number Diff line change
@@ -508,20 +508,7 @@ public function saveSchedule(Schedule $schedule, bool $runValidation = true): bo
$record = new ScheduleRecord();
}

if (!$schedule->action->id) {
Craft::$app->getDb()
->createCommand()
->upsert(Table::ACTIONS, [

], [
'type' => get_class($schedule->action),
'settings' => $action->getSettings(),
'dateUpdated' => $action->dateUpdated,
'dateCreated' => $action->dateCreated,
'uid' => $action->uid,
])
->execute();
}
Plugin::getInstance()->actions->saveAction($schedule->action);

$record->groupId = $schedule->groupId;
$record->name = $schedule->name;
Loading

0 comments on commit 30507f5

Please sign in to comment.