/** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/Model.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}" )); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/Plugins/Model.php **/ DBStatus::class, "status" => Attribute\Status::class, "date_create" => Attribute\DateTime::class, "date_edit" => Attribute\DateTime::class, "type" => "system->type", "name" => "system->name", "domain" => "system->domain", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function toggleStatus( bool $save = FALSE ) { $this->status->toggle(); if( $save ) $this->save(); } public function connect() { return $this->system->connect( $this->db_name ); } public function getDBConn() { return $this->system->db_conn; } // public function getPlugins() { return $this->system->plugins; } public function checkDBStatus() { return $this->system->checkDBStatus( $this ); } public function resolveDBStatus() { return $this->system->resolveDBStatus( $this ); } public function getSystem() { if( $this->system_id ) { return Systems\Model::get( $this->system_id ); } return NULL; } public function getDatabasesAsOptions() { $system = strtolower( $this->name ); $results = Database::getDatabase(); $options = []; foreach( $results as $item ) { if( preg_match( "/_schema$/", $item ) ) continue; if( !preg_match( "/(^|_){$system}(_|$)/", $item ) ) continue; $options[] = [ "value" => $item, "label" => $item, ]; } return $options; } public function getPlugins() { $plugins = $this->system->plugins; $plugins = array_filter( $plugins, fn( $item ) => $item->status->isActive() ); foreach( $plugins as $plugin ) { $check = Plugins\Model::get([ "tenant_id" => $this->tenant_id, "system_id" => $this->system_id, "plugin" => $plugin->plugin, ]); if( $check ) continue; Plugins\Model::save([ "tenant_id" => $this->tenant_id, "system_id" => $this->system_id, "plugin" => $plugin->plugin, ]); } $plugins = Plugins\Model::getAll([ "tenant_id" => $this->tenant_id, "system_id" => $this->system_id, ]); return $plugins; } /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct() { if( !$this->system->type->isTenantIsolated() ) { $this->db_status = $this->system->db_status->getValue(); } parent::__construct(); } }