/** Datei: plugins/systems/library/ARUNOX/Plugins/Systems/Plugins/Model.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Systems\Plugins\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 ); } } /** Datei: plugins/systems/library/ARUNOX/Plugins/Systems/Plugins/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Systems\Plugins\API\v1; use ARUNOX\HTTP; use ARUNOX\API\PHP; use ARUNOX\Core\Hooks; use ARUNOX\API\Method; use ARUNOX\Core\Router; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Systems; use ARUNOX\Database\Attribute; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; #[PHP\Attributes\Controller\Label( "Plugin", "Plugins" )] #[PHP\Attributes\Database\Definitions\Model( Systems\Plugins\Model::class )] class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Plugin nicht gefunden", "saved" => "Plugin wurde erfolgreich gespeichert", "deleted" => "Plugin wurde erfolgreich gelöscht", "created" => "Plugin wurde erfolgreich erstellt", "statuschanged.0" => "Plugin wurde erfolgreich deaktiviert", "statuschanged.1" => "Plugin wurde erfolgreich aktiviert", "statuschanged.2" => "Plugin wurde als notwendig markiert", "notcreated" => "Plugin konnte nicht erstellt werden", "notsaved" => "Plugin konnte nicht gespeichert werden", "systemneeded" => "Plugin wird vom System benötigt", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /systems/plugins * @route.description Gibt eine Liste aller verfügbaren System-Plugins zurück */ public static function getAll() : array { return Systems\Plugins\Model::getAll([ "status" => "1", "system_id" => SYSTEM->id, ]); } /** * @api * @method GET * * @route.path /systems/plugins/routes * @route.description Gibt alle verfügbaren Routen der Plugins zurück */ public static function getAllRoutes() : array { $routes = Router::getRoutes(); $routes = array_filter( $routes, fn( $item ) => $item["method"] == Method::GET ); $routes = array_filter( $routes, fn( $item ) => $item["config"] ?? FALSE ); $routes = array_map( fn( $item ) => [ "name" => "{$item["method"]} {$item["path"]}", "method" => $item["method"], "route" => $item["path"], "api" => $item["config"]["api"] ?? $item["config"]["internal"] ?? FALSE, "link" => $item["config"]["route"]["link"] ?? false, "query" => $item["config"]["route"]["query"] ?? false, "alternatives" => $item["config"]["route"]["alternative"] ?? false, "description" => $item["config"]["route"]["description"], ], $routes ); // Aktualisiere Dich! Ich brauche keine Auswertung $routes = array_filter( $routes, fn( $item ) => $item["api"] ); // show( $rest ); // show( $routes ); return $routes; } /** * @api * @method GET * * @route.path /systems/plugins/{id} * @route.description Gibt die Details eines einzelnen System-Plugins zurück */ public static function get( int $id ) : BaseEntity | HTTP\Response { if( $result = Systems\Plugins\Model::get( $id ) ) return $result; return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PATCH * * @route.path /systems/plugins/{id}/status * @route.description Aktiviert oder deaktiviert ein Plugin (Status-Umschaltung) */ public static function toggleStatus( int $id ) : HTTP\Response { if( $result = Systems\Plugins\Model::get( $id ) ) { if( $result->system_need ) { return REST\Response::autoResponse( "409", static::$NOTIFY["systemneeded"] ); } $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 ################################ # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PATCH * * @route.path /systems/plugins/{id}/status * @route.description Gibt die REST-Antwort zur Status-Umschaltung eines Plugins zurück (z. B. für Notifications) */ public static function restResponseToggleStatusPrepared( string $hashid ) : HTTP\Response { return parent::restResponseToggleStatusPrepared( $hashid ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /systems/plugins * @route.description Liefert die UI-Konfiguration für die Plugin-Tabelle im REST-Frontend */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", "Setting" ) ->get() ->setApi( "/systems/plugins" ) ->setAction( "/systems/plugins/{id}" ) ->setOrder( "plugin", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "plugin", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [], "type", [], "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "setting", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "systems/plugins" ))->get() ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "systems/plugins" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "systems/plugins" )) ->top(); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTable( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Generator\Data\Table::create() ->setApi( "/api/v1/systems/{$hashid}/plugins" ) ->setOrder( "plugin", "asc" ) ->setSearch( TRUE ) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [ "options" => Systems\Plugins\Status::getAll(), ], "status", [], "systems/plugins", "hashid" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [ "icon" => "bi-lightning-charge-fill", "options" => Attribute\Status::getAll(), ], "preload", [], "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "plugin", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "description", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "version", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "author", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "dependencies", TRUE, TRUE, TRUE, "systems/plugins" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_edit", FALSE, FALSE, TRUE, "systems/plugins" )) ->top(); } } /** Datei: plugins/systems/library/ARUNOX/Plugins/Systems/Plugins/Entity.php **/ Status::class, "preload" => Attribute\Status::class, "date_create" => Attribute\DateTime::class, "date_edit" => Attribute\DateTime::class, "system_hashid" => "system->hashid", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function toggleStatus( bool $save = FALSE ) { $this->status->toggle(); if( $save ) $this->save([ "status" => $this->status->getValue() ]); } public function getSystem() { if( $this->system_id ) { return Systems\Model::get( $this->system_id ); } return NULL; } public function getSchema() { return include "{$this->path}/config/schema/v{$this->fileVersion}.php"; } public function checkSchema( bool $onlyTables = FALSE ) { foreach( $this->schema as $table => $schema ) { if( !$this->db->existsTable( $table ) ) { return NULL; } if( $onlyTables ) continue; $current = $this->db->getSchema( $table ); if( $current !== $schema ) { return FALSE; } } return TRUE; } public function getSchemaIssues( ?Database $db = NULL ) { if( $db ) $this->setDB( $db ); $issues = []; foreach( $this->schema as $table => $schema ) { if( !$this->db->existsTable( $table ) ) { $issues[ $table ] = DBStatus::TablesMissing; continue; } $current = $this->db->getSchema( $table ); if( $current !== $schema ) { $issues[ $table ] = DBStatus::SchemaMismatch; } } return $issues; } public function repairSchema( ?Database $db = NULL ) { if( $db ) $this->setDB( $db ); $issues = $this->getSchemaIssues(); foreach( $issues as $table => &$issue ) { $schema = $this->getSchema(); if( $issue === DBStatus::TablesMissing ) { $issue = $this->db->installSchema( $schema ); } if( $issue === DBStatus::SchemaMismatch ) { $issue = $this->db->updateSchema( $schema ); } } $issues = array_filter( $issues, fn( $item ) => !is_bool( $item ) ); return !$issues; } public function installSchema() { } public function updateSchema() { } /* ### PROTECTED FUNCTIONS ###################################### # */ /* ### PRIVATE FUNCTIONS ######################################## # */ public function getDB() { return $this->db_conn ?? Connections::current(); } public function setDB( ?Database $db = NULL ) { return $this->db_conn = $db; } /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct() { $plugin = Core\Plugins::get( $this->plugin ); if( $plugin ) { $this->preload = (int)$plugin->preload; $this->path = $plugin->path; $this->author = $plugin->author; $this->fileVersion = $plugin->version; $this->description = $plugin->description; $this->dependencies = $plugin->dependencies; $this->system_need = FALSE; $this->system_show = TRUE; // if( $plugin->name == "cronjobs" ) show("X"); if( $plugin->system["only"] ?? FALSE ) { $types = preg_split( "/,|\s/", $plugin->system["need"] ); $types = array_map( "trim", $types ); $types = array_filter( $types ); $types = array_unique( $types ); $types = array_map( fn( $type ) => Systems\Type::getByConstant( $type ), $types ); $this->system_show = in_array( $this->system->type, $types ); // show( $this->system->type, $types, $plugin, $this ); } if( $plugin->system["need"] ?? FALSE ) { if( strtoupper( $plugin->system["need"] ) != "ALL" ) { $types = preg_split( "/,|\s/", $plugin->system["need"] ); $types = array_map( "trim", $types ); $types = array_filter( $types ); $types = array_unique( $types ); $types = array_map( fn( $type ) => Systems\Type::getByConstant( $type ), $types ); } else $types = array_column( Systems\Type::getAll(), "value" ); $this->system_need = in_array( $this->system->type, $types ); // if( $plugin->name == "logs" ) show( $this->system->type, $types ); // show( $this->system->type, $types, $plugin, $this ); } // if( $plugin->name == "logs" ) show( $this ); } // show( $plugin, $this ); } } /** Datei: plugins/systems/library/ARUNOX/Plugins/Systems/Plugins/Status.php **/ [ "color" => "info", "label" => "Mandatory", "icon" => "bi bi-shield-lock" ], ]; /* ### PUBLIC FUNCTIONS ######################################### # */ /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct( mixed $value, $parent = NULL ) { static::$OPTIONS = array_replace( parent::$OPTIONS, static::$OPTIONS ); parent::__construct( $value, $parent ); } }