/** Datei: plugins/systems/library/ARUNOX/Plugins/Systems/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Systems\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\REST; 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" => "/systems", "label" => "Systems", "icon" => "bi bi-gear-wide-connected", "menu" => "administration", ]; return $routes; }); } } /** Datei: plugins/systems/library/ARUNOX/Plugins/Systems/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Systems\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Systems; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static string $PLUGIN = "systems"; protected static array $NOTIFY = [ "notfound" => "System nicht gefunden", "saved" => "System wurde erfolgreich gespeichert", "deleted" => "System wurde erfolgreich gelöscht", "created" => "System wurde erfolgreich erstellt", "statuschanged.0" => "System wurde erfolgreich deaktiviert", "statuschanged.1" => "System wurde erfolgreich aktiviert", "notcreated" => "System konnte nicht erstellt werden", "notsaved" => "System konnte nicht gespeichert werden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /systems * @route.description Listet alle Systeme */ public static function getAll() : array { return Systems\Model::getAll(); } /** * @api * @method GET * * @route.path /systems/{hashid}/plugins * @route.description Listet die Plugins eines Systems */ public static function getPlugins( int|string $hashid ) : array | HTTP\Response { if( $result = Systems\Model::get( $hashid ) ) return $result->getPlugins(); return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method GET * * @route.path /systems/{hashid} * @route.description Gibt Details zu einem bestimmten System zurück */ public static function get( int|string $hashid ) : BaseEntity | HTTP\Response { if( $result = Systems\Model::get( $hashid ) ) return $result; return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /systems/{hashid} * @route.description Speichert Änderungen an einem System */ public static function save( int|string $hashid, array $data ) : HTTP\Response { if( $result = Systems\Model::get( $hashid ) ) { if( $result->save( $data ) ) { return REST\Response::jsonResponse( 200, static::$NOTIFY["saved"] ); } return REST\Response::jsonResponse( 500, static::$NOTIFY["notsaved"] ); } return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method POST * * @route.path /systems * @route.description Erstellt ein neues System */ public static function create( array $data ) : HTTP\Response { if( $hashid = Systems\Model::save( $data ) ) { $result = Systems\Model::get( $hashid ); return REST\Response::jsonResponse( 200, static::$NOTIFY["created"], [ "data" => $result ] ); } return REST\Response::jsonResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method DELETE * * @route.path /systems/{hashid} * @route.description Löscht ein System */ public static function delete( int|string $hashid ) : HTTP\Response { if( Systems\Model::delete( $hashid ) ) { return REST\Response::jsonResponse( 200, static::$NOTIFY["deleted"] ); } return REST\Response::jsonResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method PATCH * * @route.path /systems/{hashid}/status * @route.description Aktualisiert den Aktivitätsstatus eines Systems */ public static function toggleStatus( int|string $hashid ) : HTTP\Response { if( $result = Systems\Model::get( $hashid ) ) { $result->toggleStatus( TRUE ); return REST\Response::jsonResponse( 200, static::$NOTIFY["statuschanged.{$result->status->getValue()}"], [ "data" => $result ] ); } return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method PATCH * * @route.path /systems/{hashid}/db_status * @route.description Überprüft und setzt den Datenbankstatus eines Systems */ public static function resolveDBStatus( int|string $hashid ) : HTTP\Response { if( $result = Systems\Model::get( $hashid ) ) { $result->resolveDBStatus(); return REST\Response::jsonResponse( 200, static::$NOTIFY["statuschanged.{$result->status->getValue()}"], [ "data" => $result ] ); } return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /systems/{hashid} * @route.description REST-Antwort nach dem Speichern eines Systems */ public static function restResponseSave( int|string $hashid, array $data ) : HTTP\Response { $result = static::save( $hashid, $data ); if( $result->isStatus( 200 ) ) { $data = Systems\Model::get( $hashid ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /systems * @route.alternative.0 /systems/{hashid}#plugins * @route.description REST-Antwort nach dem Erstellen eines Systems */ public static function restResponseCreate( array $data ) : HTTP\Response { $result = static::create( $data ); if( $result->isStatus( 200 ) ) { REST\Response::closeModals(); REST\Response::redirect( "/systems" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /systems/{hashid} * @route.description REST-Antwort nach dem Löschen eines Systems */ public static function restResponseDelete( int|string $hashid ) : HTTP\Response { REST\Response::closeModals(); $result = static::delete( $hashid ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/systems" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /systems/{hashid}/status * @route.description REST-Antwort zur Statusänderung eines Systems */ 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 ); } /** * @internal rest * @method PATCH * * @route.path /systems/{hashid}/db_status * @route.description REST-Antwort zur Aktualisierung des DB-Status eines Systems */ public static function restResponseResolveDBStatus( int|string $hashid ) : HTTP\Response { $result = static::resolveDBStatus( $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); $status = Systems\DBStatus::get( $data->db_status ); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); REST\Response::addNotify( $status["color"]->value, $status["label"], $status["icon"] ); return REST\Response::jsonResponse(); } return static::restResponse( $result ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /systems * @route.description REST-Konfiguration für System-Tabelle */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", "Systems" ) ->replaceContent( static::restConfigTable() ->setApi( "/systems" ) ->setAction( "/systems/{hashid}" ) ->getColumn() ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "systems", "hashid" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "systems", "hashid" )) ); } /** * @internal rest * @method GET * * @route.path /systems/{hashid} * @route.description REST-Konfiguration für Detailansicht eines Systems */ public static function restConfigDetails( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "details", "System bearbeiten" ) ->setBacklink( "/systems" ) ->get() ->setApi( "/systems/{$hashid}" ) ->setAction( "/systems/{$hashid}", "PUT" ) ->addTab( "System" )->get() ->setActive( TRUE ) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Systems\Type::getAll(), ], "type", TRUE, FALSE, "systems" )) ->parent() ->addTab( "Plugins" )->get() ->addContent( Systems\Plugins\API\v1\Controller::restConfigTable( $hashid ?? 0 ) ) ->parent() ->addTab( "Speichern" )->get() ->setColor( REST\Generator\Config\Color::Primary ) ->setIcon( "bi bi-save" ) ->setType( "submit" ); } /** * @internal rest * @method GET * * @route.path /systems/create * @route.description REST-Formularkonfiguration für neue Systeme */ public static function restConfigCreate() : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "System erstellen" ) ->setData([ "id" => 0 ]) ->get() ->setAction( "/systems", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Systems\Type::getAll(), ], "type", TRUE, FALSE, "systems" )) ->top(); } /** * @internal rest * @method GET * * @route.path /systems/{hashid}/delete * @route.description REST-Bestätigungsdialog zum Löschen eines Systems */ public static function restConfigDelete( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "System Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/systems/{$hashid}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$hashid}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "systems" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "systems" )) ->top(); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTable( ?string $prefix = NULL ) : REST\Generator\Base\BaseGenerator { $plugin = static::getPluginName( $prefix ); return REST\Generator\Data\Table::create() ->setApi( $prefix ? $plugin : FALSE ) ->setOrder( "name", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], $plugin )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], $plugin )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [ "icon" => "bi-diagram-3", "options" => Systems\Type::getAll(), ], "type", [], $plugin )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [ "label" => "Datenbank", "icon" => "bi-database-gear", "options" => Systems\DBStatus::getAll(), ], "db_status", [], $plugin )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "name", TRUE, TRUE, TRUE, $plugin )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "domain", TRUE, TRUE, TRUE, $plugin )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, $plugin )); } }