/** Datei: plugins/cronjobs/library/ARUNOX/Plugins/Cronjobs/Running.php **/ [ "color" => REST\Generator\Config\Color::Default, "icon" => "bi bi-clipboard2-x", "label" => "None" ], 1 => [ "color" => REST\Generator\Config\Color::Warning, "icon" => "bi bi-clipboard2-pulse", "label" => "Running" ], 2 => [ "color" => REST\Generator\Config\Color::Success, "icon" => "bi bi-clipboard2-check", "label" => "Complete" ], ]; /* ### PUBLIC FUNCTIONS ######################################### # */ } /** Datei: plugins/cronjobs/library/ARUNOX/Plugins/Cronjobs/Model.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Cronjobs\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\Users; class Routes { public static function register() : void { /* ### MAIN ROUTES ############################## # */ Router::addAll( Controller::class ); /* ### MENU ROUTES ############################## # */ Hooks::add('router::get->api.v1.rest.routes', function( $routes ) { if( !Users\Model::isSuperadmin() ) return $routes; $routes[] = [ "path" => "/cronjobs", "label" => "Cronjobs", "icon" => "bi bi-clock", "menu" => "administration", ]; $routes[] = [ "path" => "/cronjobs/{id}", "label" => "Bearbeiten", ]; return $routes; }); } } /** Datei: plugins/cronjobs/library/ARUNOX/Plugins/Cronjobs/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Cronjobs\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Cronjobs; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Cronjob nicht gefunden", "saved" => "Cronjob wurde erfolgreich gespeichert", "deleted" => "Cronjob wurde erfolgreich gelöscht", "created" => "Cronjob wurde erfolgreich erstellt", "notcreated" => "Cronjob konnte nicht erstellt werden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /cronjobs * @route.description Listet alle Agents */ public static function getAll() : array { return Cronjobs\Model::getAll(); } /** * @api * @method GET * * @route.path /cronjobs/{hashid} * @route.description Gibt einen einzelnen Agent anhand der ID zurück */ public static function get( int|string $hashid ) : BaseEntity | HTTP\Response { if( $result = Cronjobs\Model::get( $hashid ) ) return $result; return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /cronjobs/{hashid} * @route.description Speichert Änderungen an einem bestehenden Cronjobs */ public static function save( int|string $hashid, array $data ) : HTTP\Response { if( $result = Cronjobs\Model::get( $hashid ) ) { if( $result->save( $data ) ) { return REST\Response::autoResponse( 200, static::$NOTIFY["saved"] ); } return REST\Response::autoResponse( 500, static::$NOTIFY["notsaved"] ); } return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method POST * * @route.path /cronjobs * @route.description Erstellt einen neuen Cronjob */ public static function create( array $data ) : HTTP\Response { if( $hashid = Cronjobs\Model::save( $data ) ) { $result = Cronjobs\Model::get( $hashid ); return REST\Response::autoResponse( 200, static::$NOTIFY["created"], [ "data" => $result ] ); } return REST\Response::autoResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method DELETE * * @route.path /cronjobs/{hashid} * @route.description Löscht einen Cronjob anhand der ID */ public static function delete( int|string $hashid ) : HTTP\Response { if( Cronjobs\Model::delete( $hashid ) ) { return REST\Response::autoResponse( 200, static::$NOTIFY["deleted"] ); } return REST\Response::autoResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method PATCH * * @route.path /cronjobs/{hashid}/status * @route.description Wechselt den Status (aktiv/inaktiv) eines Cronjobs */ public static function toggleStatus( int|string $hashid ) : HTTP\Response { if( $result = Cronjobs\Model::get( $hashid ) ) { $result->toggleStatus( TRUE ); return REST\Response::autoResponse( 200, static::$NOTIFY["statuschanged.{$result->status->getValue()}"], [ "data" => $result ] ); } return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /** * @api * @method POST * * @route.path /cronjobs/{hashid}/validate/uuid * @route.description Validiert eingegebene Ccronjobs UUID */ public static function validateUUID( int|string $hashid, array $data ) : HTTP\Response { if( isset( $data["value"] ) && $data["value"] ) { if( $result = Cronjobs\Model::getByUUID( $data["value"] ) ) { if( $result->id != $hashid && $result->hashid != $hashid ) { return REST\Response::jsonResponse( 200, [ "valid" => FALSE ] ); } } } return REST\Response::jsonResponse( 200, [ "valid" => TRUE ] ); } /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /cronjobs/{hashid} * @route.description REST-Antwort nach dem Speichern eines Cronjobs */ public static function restResponseSave( int|string $hashid, array $data ) : HTTP\Response { $result = static::save( $hashid, $data ); if( $result->isStatus( 200 ) ) { $data = Cronjobs\Model::get( $hashid ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /cronjobs * @route.description REST-Antwort nach dem Erstellen eines Cronjobs (inkl. Notifikation, Redirect etc.) */ public static function restResponseCreate( array $data ) : HTTP\Response { $result = static::create( $data ); if( $result->isStatus( 200 ) ) { REST\Response::closeModals(); REST\Response::redirect( "/agents/{$result->getBody("data", "hashid")}" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /cronjobs/{hashid} * @route.description REST-Antwort nach dem Löschen eines Cronjobs */ public static function restResponseDelete( int|string $hashid ) : HTTP\Response { REST\Response::closeModals(); $result = static::delete( $hashid ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/agents" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /cronjobs/{hashid}/status * @route.description REST-Antwort zur Status-Umschaltung eines Cronjobs (z. B. für Notifications) */ public static function restResponseToggleStatus( int|string $hashid ) : HTTP\Response { $result = static::toggleStatus( $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /cronjobs * @route.description REST-Konfiguration für Cronjob-Tabelle */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", "Cronjob" ) ->get() ->setApi( "/cronjobs" ) ->setAction( "/cronjobs/{hashid}" ) ->setOrder( "date_next", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "uuid", TRUE, TRUE, TRUE, "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], "cronjobs", "hashid" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [], "running", [], "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_execute", TRUE, FALSE, TRUE, "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_next", TRUE, FALSE, TRUE, "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "hook", TRUE, TRUE, TRUE, "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "interval", TRUE, TRUE, TRUE, "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "runtime", TRUE, TRUE, TRUE, "cronjobs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "cronjobs" ))->get() // ->addButton(...Hooks::filter( "rest.generator::action.name->execute", [], "cronjobs", "hashid" )) ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "cronjobs", "hashid" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "cronjobs", "hashid" )); } /** * @internal rest * @method GET * * @route.path /cronjobs/{hashid} * @route.description REST-Konfiguration für Detailansicht eines Cronjobs */ public static function restConfigDetails( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Cronjob bearbeiten" ) ->get() ->setApi( "/cronjobs/{$hashid}" ) ->setAction( "/cronjobs/{$hashid}", "PUT" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Speichern", "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "uuid", TRUE, "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "hook", TRUE, "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [], "interval", FALSE, TRUE, "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "runtime", FALSE, "cronjobs" )); } /** * @internal rest * @method GET * * @route.path /cronjobs/create * @route.description REST-Konfiguration für das Erstellen eines Cronjobss (Formular) */ public static function restConfigCreate() : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Cronjob erstellen" ) ->get() ->setAction( "/cronjobs", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "uuid", TRUE, "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "hook", TRUE, "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [], "interval", FALSE, TRUE, "cronjobs" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "runtime", FALSE, "cronjobs" )) ; } /** * @internal rest * @method GET * * @route.path /cronjobs/{hashid}/delete * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Cronjobss */ public static function restConfigDelete( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Cronjob Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/cronjobs/{$hashid}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$hashid}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "cronjobs" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "cronjobs" )); } } /** Datei: plugins/cronjobs/library/ARUNOX/Plugins/Cronjobs/Interval.php **/ [ "color" => REST\Generator\Config\Color::Default, "label" => "Minutely" ], 3600 => [ "color" => REST\Generator\Config\Color::Warning, "label" => "Hourly" ], 86400 => [ "color" => REST\Generator\Config\Color::Success, "label" => "Daily" ], ]; /* ### PUBLIC FUNCTIONS ######################################### # */ } /** Datei: plugins/cronjobs/library/ARUNOX/Plugins/Cronjobs/Entity.php **/ Attribute\Status::class, "date_create" => Attribute\DateTime::class, "date_edit" => Attribute\DateTime::class, "running" => Running::class, ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function toggleStatus( bool $save = FALSE ) { $this->status->toggle(); if( $save ) $this->save(); } }