Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Jun 19, 2024
1 parent c1f1cec commit bd274a9
Show file tree
Hide file tree
Showing 26 changed files with 664 additions and 139 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"psr/container": "^1.0 || ^2.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"react/event-loop": "^1.5",
"symfony/process": "^6.0 || ^7.0"
"symfony/process": "^6.0 || ^7.0",
"alexanderpas/http-enum": "^1.0"
},
"replace": {
"panlatent/craft-action-abstract": "self.version"
Expand Down
50 changes: 41 additions & 9 deletions src/actions/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace panlatent\schedule\actions;

use Alexanderpas\Common\HTTP\Method;
use Craft;
use craft\helpers\Json;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
Expand All @@ -14,10 +16,7 @@

class HttpRequest extends Action
{
/**
* @var string
*/
public string $method = 'get';
public string $method = Method::GET->value;

/**
* @var string Request URL default is echo api.
Expand All @@ -44,11 +43,6 @@ class HttpRequest extends Action
*/
public string $body = '';

public static function run()
{

}

public function execute(ContextInterface $context): bool
{
try {
Expand All @@ -71,6 +65,44 @@ public function executeWithClient(ContextInterface $context, ClientInterface $cl
return $statusCode >= 200 && $statusCode < 400;
}

public function getMethods(): array
{
return [
Method::GET->value,
Method::HEAD->value,
Method::POST->value,
Method::PUT->value,
Method::PATCH->value,
Method::OPTIONS->value,
];
}

public function getContentTypes(): array
{
return [
'multipart/form-data',
'application/x-www-form-urlencoded',
'application/json',
'application/xml',
'text/html',
'text/xml',
];
}

public function getSettingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('schedule/_components/actions/HttpRequest/settings', [
'action' => $this,
// Craft editableTable not support custom suggestions
// 'httpHeaderSuggestions' => [
// [
// 'label' => '',
// 'data' => $this->_getHttpHeaderSuggestions(),
// ]
// ],
]);
}


protected function getOptions(): array
{
Expand Down
60 changes: 60 additions & 0 deletions src/actions/SendEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace panlatent\schedule\actions;

use Craft;
use craft\helpers\App;
use panlatent\craft\actions\abstract\Action;
use panlatent\craft\actions\abstract\ContextInterface;

class SendEmail extends Action
{
public static function displayName(): string
{
return Craft::t('schedule', 'Send Email');
}

public array $emails = [];

public string $subject = '';

public string $message = <<<TWIG
Hello {{name}},
TWIG;

public function execute(ContextInterface $context): bool
{
$emails = [];
foreach ($this->emails as $email) {
if ($email['name']) {
$emails[App::parseEnv($email['email'])] = $email['name'];
} else {
$emails[] = App::parseEnv($email['email']);
}
}

$subject = Craft::$app->view->renderString($this->subject, [
'action' => $this,
]);

$message = Craft::$app->view->renderString($this->message, [
'action' => $this,
]);

return Craft::$app->mailer
->compose()
->setTo($emails)
->setHtmlBody($message)
->setSubject($subject)
->send();
}

public function getSettingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('schedule/_components/actions/SendEmail/settings', [
'action' => $this,
]);
}
}
54 changes: 54 additions & 0 deletions src/controllers/ActionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,66 @@
namespace panlatent\schedule\controllers;

use Craft;
use craft\elements\Entry;
use craft\web\Controller;
use panlatent\craft\actions\abstract\ActionInterface;
use panlatent\schedule\actions\HttpRequest;
use panlatent\schedule\Plugin;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class ActionsController extends Controller
{
public function actionEdit(?int $actionId = null, ?ActionInterface $action = null): Response
{
if ($actionId !== null) {
if ($action === null) {
$action = Plugin::getInstance()->actions->getActionById($actionId);

if (!$action) {
throw new NotFoundHttpException('action not found');
}
}

$title = trim($action->name) ?: Craft::t('schedule', 'Edit Action');
} else {
if ($action === null) {
$action = new HttpRequest();
}

$title = Craft::t('schedule', 'Create a new action');
}

$allActionTypes = Plugin::getInstance()->actions->getAllActionTypes();
$actionTypeOptions = [];
foreach ($allActionTypes as $class) {
$actionTypeOptions[] = [
'label' => $class::displayName(),
'value' => $class,
];
}

return $this->asCpScreen()
->title($title)
->addCrumb(Craft::t('app', 'Settings'), 'settings')
->addCrumb(Craft::t('app', 'Entry Types'), 'settings/entry-types')
->action('schedule/actions/save')
->redirectUrl('schedule/actions')
->addAltAction(Craft::t('app', 'Save and continue editing'), [
'redirect' => 'settings/entry-types/{id}',
'shortcut' => true,
'retainScroll' => true,
])
->contentTemplate('schedule/actions/_edit.twig', [
'actionId' => $actionId,
'action' => $action,
'typeName' => $action::displayName(),
'actionTypes' => $allActionTypes,
'actionTypeOptions' => $actionTypeOptions,
]);
}


public function actionRenderSettings(): Response
{
$this->requirePostRequest();
Expand Down
26 changes: 2 additions & 24 deletions src/controllers/SchedulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,14 @@ public function actionEditSchedule(int $scheduleId = null, Schedule $schedule =
];
}

$actionInstances = [];
$actionTypeOptions = [];
foreach ($allActionTypes as $class) {
$actionInstances[$class] = new $class();
$actionTypeOptions[] = [
'label' => $class::displayName(),
'value' => $class,
];
}

$timerInstances = [];
$timerTypeOptions = [];
foreach ($allTimerTypes as $class) {
$timerInstances[$class] = new $class();
$timerTypeOptions[] = [
'label' => $class::displayName(),
'value' => $class,
];
}

return $this->renderTemplate('schedule/_edit', [
'isNewSchedule' => $isNewSchedule,
'groupOptions' => $groupOptions,
'schedule' => $schedule,
'actionInstances' => $actionInstances,
'actionTypes' => $allActionTypes,
'actionTypeOptions' => $actionTypeOptions,
'timerInstances' => $timerInstances,
'actionTypeOptions' => array_map(static fn($class) => ['label' => $class::displayName(), 'value' => $class], $allActionTypes),
'timerTypes' => $allTimerTypes,
'timerTypeOptions' => $timerTypeOptions,
'timerTypeOptions' => array_map(static fn($class) => ['label' => $class::displayName(), 'value' => $class], $allTimerTypes),
]);
}

Expand Down
1 change: 1 addition & 0 deletions src/controllers/TimersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*
* @package panlatent\schedule\controllers
* @author Panlatent <[email protected]>
* @deprecated since 1.0
*/
class TimersController extends Controller
{
Expand Down
2 changes: 1 addition & 1 deletion src/db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
abstract class Table
{
// public const ACTIONS = '{{%schedule_actions}}';
public const ACTIONS = '{{%schedule_actions}}';
public const SCHEDULES = '{{%schedule_schedules}}';
public const SCHEDULE_GROUPS = '{{%schedule_schedulegroups}}';
public const TIMERS = '{{%schedule_timers}}';
Expand Down
11 changes: 11 additions & 0 deletions src/events/ActionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace panlatent\schedule\events;

use craft\events\ModelEvent;
use panlatent\craft\actions\abstract\ActionInterface;

class ActionEvent extends ModelEvent
{
public ?ActionInterface $action = null;
}
1 change: 1 addition & 0 deletions src/events/ScheduleBuildEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
* @package panlatent\schedule\events
* @author Panlatent <[email protected]>
* @deprecated since 1.0
*/
class ScheduleBuildEvent extends Event
{
Expand Down
16 changes: 4 additions & 12 deletions src/events/ScheduleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@

namespace panlatent\schedule\events;

use panlatent\schedule\base\ScheduleInterface;
use yii\base\Event;
use craft\events\ModelEvent;
use panlatent\schedule\models\Schedule;

/**
* Class ScheduleEvent
*
* @package panlatent\schedule\events
* @author Panlatent <[email protected]>
*/
class ScheduleEvent extends Event
class ScheduleEvent extends ModelEvent
{
/**
* @var ScheduleInterface|null
*/
public ?ScheduleInterface $schedule = null;

/**
* @var bool
*/
public bool $isNew = false;
public ?Schedule $schedule = null;
}
9 changes: 2 additions & 7 deletions src/events/ScheduleGroupEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@

namespace panlatent\schedule\events;

use craft\events\ModelEvent;
use panlatent\schedule\models\ScheduleGroup;
use yii\base\Event;

/**
* Class ScheduleGroupEvent
*
* @package panlatent\schedule\events
* @author Panlatent <[email protected]>
*/
class ScheduleGroupEvent extends Event
class ScheduleGroupEvent extends ModelEvent
{
/**
* @var ScheduleGroup|null
*/
public ?ScheduleGroup $group = null;

/**
* @var bool
*/
public bool $isNew = false;
}
9 changes: 2 additions & 7 deletions src/events/TimerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@

namespace panlatent\schedule\events;

use craft\events\ModelEvent;
use panlatent\schedule\base\TimerInterface;
use yii\base\Event;

/**
* Class TimerEvent
*
* @package panlatent\schedule\events
* @author Panlatent <[email protected]>
*/
class TimerEvent extends Event
class TimerEvent extends ModelEvent
{
/**
* @var TimerInterface|null
*/
public ?TimerInterface $timer = null;

/**
* @var bool
*/
public bool $isNew = false;
}
10 changes: 10 additions & 0 deletions src/fields/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace panlatent\schedule\fields;

use craft\base\Field;

class Action extends Field
{

}
Loading

0 comments on commit bd274a9

Please sign in to comment.