/** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\Systems\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\Users; class Routes { public static function register() : void { Router::addAll( Controller::class ); // Router::get('/api/v1/tenants/{id}/systems', [ Controller::class, 'getAll' ]); // // Router::get('/api/v1/tenants/{id}/systems/create', [ Controller::class, 'restConfigCreate' ]); // Router::post('/api/v1/tenants/{id}/systems', [ Controller::class, 'create' ]); // // Router::get('/api/v1/tenants/{id}/systems/{id}', [ Controller::class, 'get' ]); // Router::put('/api/v1/tenants/{id}/systems/{id}', [ Controller::class, 'save' ]); // Router::patch('/api/v1/tenants/{id}/systems/{id}/status', [ Controller::class, 'toggleStatus' ]); // Router::patch('/api/v1/tenants/{id}/systems/{id}/db_status', [ Controller::class, 'resolveDBStatus' ]); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\Systems\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Systems; use ARUNOX\Plugins\Tenants; use ARUNOX\API\BaseController; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "System nicht gefunden", "saved" => "System wurde erfolgreich gespeichert", "added" => "System wurde erfolgreich hinzugefügt", "notadded" => "System konnte nicht hinzugefügt werden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ public function getAll( int $tenant_id ) { $results = Tenants\Systems\Model::getAll([ "tenant_id" => $tenant_id ]); return $this->jsonResponse( $results ); } public function get( int $tenant_id, int $id ) { if( $result = Tenants\Systems\Model::get( $id ) ) { static::restConfigDetails( $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ public function save( int $tenant_id, int $id, array $data ) { if( $result = Tenants\Systems\Model::get( $id ) ) { $result->save( $data ); REST\Response::addNotify( "success", static::$NOTIFY["saved"] ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", "/api/v1/tenants/{$tenant_id}/systems", $id ], $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } public function create( int $tenant_id, array $data ) { if( $id = Tenants\Systems\Model::save( $data ) ) { REST\Response::addNotify( "success", static::$NOTIFY["added"] ); REST\Response::closeModals(); REST\Response::redirect( "/tenants/{$tenant_id}#systems" ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notadded"] ); return REST\Response::jsonResponse(); } public function toggleStatus( int $tenant_id, int $id ) { if( $result = Tenants\Systems\Model::get( $id ) ) { $result->toggleStatus( TRUE ); REST\Response::addUpdater( [ "data/table/row", "/api/v1/tenants/{$tenant_id}/systems", $id ], $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } public function resolveDBStatus( int $tenant_id, int $id ) { if( $result = Tenants\Systems\Model::get( $id ) ) { $result->resolveDBStatus(); REST\Response::addUpdater( [ "data/table/row", "/api/v1/tenants/{$tenant_id}/systems", $id ], $result ); REST\Response::addNotify( $result->db_status->getColor()->value, $result->db_status->getLabel(), $result->db_status->getIcon() ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } /* ### PUBLIC REST FUNCTIONS #################################### # */ public static function restConfigDetails( Tenants\Systems\Entity $data ) { $form = REST\Response::addModal( "details", "System bearbeiten" ) ->setData( $data ) ->get() ->setAction( "/tenants/{tenant_id}/systems/{id}", "PUT" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Speichern", "tenants/{tenant_id}/systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Systems\Model::getAsOptions(), ], "system_id", TRUE, FALSE, "tenants/{tenant_id}/systems" )); if( $data->type->isTenantIsolated() ) { $form->addField(...Hooks::filter( "rest.generator::control.type->select", [ "label" => "Datenbankname", "options" => $data->getDatabasesAsOptions() ], "db_name", TRUE, TRUE, "tenants/{tenant_id}/systems" )); } return $form; } public static function restConfigCreate( int $tenant_id ) { return REST\Response::addModal( "details", "System hinzufügen" ) ->setData([ "tenant_id" => $tenant_id ]) ->get() ->setAction( "/tenants/{tenant_id}/systems", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "tenants/{tenant_id}/systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->hidden", [], "tenant_id", $tenant_id, "tenants/{tenant_id}/systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Systems\Model::getAsOptions(), ], "system_id", TRUE, FALSE, "tenants/{tenant_id}/systems" )); } public static function restConfigTable( int $tenant_id ) { return Systems\API\v1\Controller::restConfigTable("tenants/{$tenant_id}") ->setData([ "tenant_id" => $tenant_id ]) ->setAction( "/tenants/{tenant_id}/systems/{id}" ) ->getColumn("actions") ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "tenants/{tenant_id}/systems" )) ->beforeColumn( ...Hooks::filter( "rest.generator::column.type->text", [], "db_name", TRUE, TRUE, TRUE, "tenants/{tenant_id}" )); } }