/** Datei: plugins/logs/library/ARUNOX/Plugins/Logs/Model.php **/ * @copyright 2025 ARUNOX * @version 1.0.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Logs; use ARUNOX\API\PHP; use ARUNOX\Database\BaseModel; #[PHP\Attributes\Database\Definitions\Table( "logs" )] #[PHP\Attributes\Database\Definitions\HashID( "log" )] #[PHP\Attributes\Database\Definitions\Entity( Entity::class )] class Model extends BaseModel { /* ### VARIABLES ################################################ # */ /* ### PUBLIC STATIC FUNCTIONS ################################## # */ public static function getByTarget( string $target ) : array { return static::getAll([[ "RELATION" => "OR", [ "source_hid" => $target, "target_hids__SQL" => "JSON_LENGTH( `target_hids` ) = 0", ], [ "source_hid__NOT" => $target, "target_hids__JSON" => $target, ] ]]); } public static function getBySource( string $source ) : array { return static::getAllByColumn( "source_hid", $source ); } } /** Datei: plugins/logs/library/ARUNOX/Plugins/Logs/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Logs\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; 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 ) { $routes[] = [ "path" => Controller::getRoute(), "label" => Controller::getLabelGroup(), "icon" => Controller::getIcon(), "menu" => "administration", ]; return $routes; }, 99 ); } } /** Datei: plugins/logs/library/ARUNOX/Plugins/Logs/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Logs\API\v1; use ARUNOX\HTTP; use ARUNOX\API\PHP; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\Logs; use ARUNOX\Plugins\REST; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; #[PHP\Attributes\Controller\Label( "Log", "Logs", "Logbuch" )] #[PHP\Attributes\Controller\Icon( REST\Generator\Config\Icon::JournalText )] #[PHP\Attributes\Controller\Route( "logs" )] class Controller extends BaseController { /* ### VARIABLES ################################################ # */ /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /logs * @route.description Listet alle Logeinträge */ public static function getAll() : array { return Logs\Model::getAll(); } /** * @api * @method GET * * @route.path /{*}/{target}/logs * @route.description Listet alle Logeinträge */ public static function getByTarget( string $route, string $target ) : array { return Logs\Model::getByTarget( $target ); } /** * @api * @method GET * * @route.path /logs/{hashid} * @route.description Gibt einen einzelnen Logeintrag anhand der ID zurück */ public static function get( string $hashid ) : BaseEntity | HTTP\Response { if( $result = Logs\Model::get( $hashid ) ) return $result; return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method DELETE * * @route.path /logs/{hashid} * @route.description Löscht ein Logeintrag anhand der ID */ public static function delete( string $hashid ) : HTTP\Response { if( Logs\Model::delete( $hashid ) ) { return REST\Response::autoResponse( 200, static::getNotify("deleted") ); } return REST\Response::autoResponse( 500, static::getNotify("notfound") ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method GET * * @route.path /logs/{hashid} * @route.description REST-Konfiguration für Detailansicht eines Logeintrags */ public static function restResponseDetails( string $hashid ) : mixed { $log = Logs\Model::get( $hashid ); $source = Logs\Model::resolveHashIDPrefix( $log->source_hid ); $route = $source->callControllerClass( "getRoute", $log->source_hid ); return Router::dispatch( HTTP\Request::method(), "/rest{$route}" ); } /** * @internal rest * @method DELETE * * @route.path /logs/{hashid} * @route.description REST-Antwort nach dem Löschen eines Logeintrag */ public static function restResponseDelete( string $hashid ) : HTTP\Response { REST\Response::closeModals(); $result = static::delete( $hashid ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/logs" ); } return static::restResponse( $result ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /logs * @route.description REST-Konfiguration für Logbuch-Tabelle */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", static::getLabelGroup() ) ->replaceContent( static::restConfigTable() ); } /** * @internal rest * @method GET * * @route.path /{*}/{target}/logs * @route.description REST-Konfiguration für Logbuch-Tabelle */ public static function restConfigLog( string $route, string $target ) : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", static::getLabelGroup() ) ->replaceContent( static::restConfigTable( $target ) ); } /** * @internal rest * @method GET * * @route.path /logs/{hashid}/delete * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Logeintrags */ public static function restConfigDelete( string $hashid ) : REST\Generator\Base\BaseGenerator { $type = Logs\Model::getValue( $hashid, "type" ); return REST\Response::addModal( "details", "{$type->getLabel()} Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/logs/{$hashid}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$hashid}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "logs" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "logs" )); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTab( REST\Generator\Base\BaseGenerator $rest, null|string $hashid = NULL ) : void { $rest->addTab( static::getLabelGroup(), static::getPlural() )->get() ->setIcon( static::getIcon() ) ->addContent( Logs\API\v1\Controller::restConfigTable( $hashid ) ) ; } public static function restConfigTable( null|string $hashid = NULL ) : REST\Generator\Base\BaseGenerator { $route = static::getRoute(); if( $hashid ) { $class = Logs\Model::resolveHashIDPrefix( $hashid ); $class = $class::getControllerClass(); $route = $class::getRoute( $hashid, $route ); } $table = REST\Generator\Data\Table::create() ->setApi( $route ) ->setAction( static::getRoute( "{hashid}" ) ) ->setOrder( "date_create", "desc" ) ->setSearch( TRUE ) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_create", TRUE, FALSE, TRUE, "logs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "message", TRUE, TRUE, TRUE, "logs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "creator_name", TRUE, TRUE, TRUE, "logs" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "source_hid", TRUE, TRUE, TRUE, "logs" )) ; return $table; } } /** Datei: plugins/logs/library/ARUNOX/Plugins/Logs/Entity.php **/ * @copyright 2025 ARUNOX * @version 1.0.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Logs; use ARUNOX\API\PHP; use ARUNOX\Plugins; use ARUNOX\Database\Attribute; use ARUNOX\Database\BaseEntity; #[PHP\Attributes\Database\Definitions\Model( Model::class )] class Entity extends BaseEntity { /* ### VARIABLES ################################################ # */ protected static array $ATTRIBUTES = [ "date_create" => Attribute\DateTime::class, "creator_name" => "creator_name", "creator" => "creator", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function getCreator() { if( $class = $this->getCreatorClass() ) { return $class::get( $this->created_by ); } } public function getCreatorClass() { if( !$this->created_by ) return NULL; $prefix = explode( "_", $this->created_by ); $prefix = current( $prefix ); if( $prefix == Plugins\Users\Model::getHashIDPrefix() ) { return Plugins\Users\Model::class; } return FALSE; } public function getCreatorName() { if( $class = $this->getCreatorClass() ) { return $class::getValue( $this->created_by, "username" ); } } }