/** Datei: plugins/cards/library/ARUNOX/Plugins/Cards/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Cards\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Tools\Cardnumber; 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::getPlural(), "icon" => Controller::getIcon(), "menu" => "management", ]; return $routes; }, 50 ); } public static function init() : void { Cardnumber::setLevelClass( 1, Cardnumber\v1::class ); Cardnumber::setLevelClass( 2, Cardnumber\v2::class ); Cardnumber::setDefaultLevel( 2 ); } public static function redirect( $route ) : array { [ $uri, $method ] = $route; if( $uri === Controller::getRoute( "0:rest", "verify" ) ) { Hooks::remove( "LOGIN_REDIRECT" ); } return $route; } } /** Datei: plugins/cards/library/ARUNOX/Plugins/Cards/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Cards\API\v1; use ARUNOX\HTTP; use ARUNOX\Plugins; use ARUNOX\API\PHP; use ARUNOX\Core\Hooks; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; use ARUNOX\Plugins\REST\Generator\Base\BaseGenerator; #[PHP\Attributes\Controller\Label( "Karte", "Karten" )] #[PHP\Attributes\Controller\Icon( Plugins\REST\Generator\Config\Icon::CreditCard2Front )] #[PHP\Attributes\Controller\Route( "cards" )] #[PHP\Attributes\Controller\Notifications( "statuschanged.0", '{$LABEL.singular} wurde erfolgreich erstellt' )] #[PHP\Attributes\Controller\Notifications( "statuschanged.1", '{$LABEL.singular} wurde erfolgreich beschrieben' )] #[PHP\Attributes\Controller\Notifications( "statuschanged.2", '{$LABEL.singular} wurde erfolgreich aktiviert' )] #[PHP\Attributes\Controller\Notifications( "statuschanged.3", '{$LABEL.singular} wurde erfolgreich deaktiviert' )] #[PHP\Attributes\Controller\Notifications( "NOTFOUND", 'Card not Found' )] #[PHP\Attributes\Controller\Notifications( "FORBIDDEN", 'Card not allowed' )] #[PHP\Attributes\Controller\Notifications( "CHECKSUM", 'LOGIN: by CHECKSUM' )] #[PHP\Attributes\Controller\Notifications( "SERIAL", 'LOGIN: by SERIAL' )] #[PHP\Attributes\Controller\Notifications( "CVC", 'LOGIN: by CVC' )] #[PHP\Attributes\Controller\Notifications( "INVALID", 'LOGIN: FAILED' )] #[PHP\Attributes\Database\Definitions\Model( Plugins\Cards\Model::class )] class Controller extends BaseController { /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /cards * @route.description Listet alle Logeinträge */ public static function getAllPrepared() : array { return parent::getAllPrepared(); } /** * @api * @method GET * * @route.path /cards/{hashid} * @route.description Gibt einen einzelnen Logeintrag anhand der ID zurück */ public static function getPrepared( string $hashid ) : BaseEntity | HTTP\Response { return parent::getPrepared( $hashid ); } /** * @api * @method POST * * @route.path /cards/verify * @route.description Gibt einen einzelnen Logeintrag anhand der ID zurück */ public static function verify( $data ) : HTTP\Response { [ $code, $error, $number ] = Plugins\Cards\Model::verify( $data["cardid"], $data["serial"] ); if( $code == 200 ) { if( $data["action"] == "login" ) { if( $card = Plugins\Cards\Model::getByUUID( $number ) ) { if( $card->status->isActive() && $card->type->isLogin() ) { Plugins\Users\Model::setCurrent( $card->user ); } else [ $code, $error ] = [ "403", "FORBIDDEN" ]; } else [ $code, $error ] = [ "404", "NOTFOUND" ]; } } return Plugins\REST\Response::autoResponse( $code, static::getNotify( $error ), NULL, "NFC Scan" ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /cards/{hashid} * @route.description Speichert Änderungen an einem bestehenden Termin */ public static function savePrepared( string $hashid, array $data ) : HTTP\Response { return parent::savePrepared( $hashid, $data ); } /** * @api * @method POST * * @route.path /cards * @route.description Erstellt einen neuen Kalender */ public static function createPrepared( array $data ) : HTTP\Response { return parent::createPrepared( $data ); } /** * @api * @method DELETE * * @route.path /cards/{hashid} * @route.description Löscht ein Logeintrag anhand der ID */ public static function deletePrepared( string $hashid ) : HTTP\Response { return parent::deletePrepared( $hashid ); } /** * @api * @method PATCH * * @route.path /cards/{hashid}/status * @route.description Wechselt den Status (aktiv/inaktiv) eines Kalenders */ public static function toggleStatusPrepared( string $hashid ) : HTTP\Response { return parent::toggleStatusPrepared( $hashid ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /cards/{hashid} * @route.description REST-Antwort nach dem Speichern eines Termins */ public static function restResponseSavePrepared( string $hashid, array $data ) : HTTP\Response { return parent::restResponseSavePrepared( $hashid, $data ); } /** * @internal rest * @method POST * * @route.path /cards * @route.description REST-Antwort nach dem Erstellen eines Kalenders */ public static function restResponseCreate( array $data ) : HTTP\Response { $result = static::createPrepared( $data ); if( $result->isStatus( 200 ) ) { Plugins\REST\Response::closeModals(); Plugins\REST\Response::redirect( "/cards/{$result->getBody("data", "hashid")}" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /cards/{hashid} * @route.description REST-Antwort nach dem Löschen eines Logeintrag */ public static function restResponseDelete( string $hashid ) : HTTP\Response { Plugins\REST\Response::closeModals(); $result = static::deletePrepared( $hashid ); if( $result->isStatus( 200 ) ) { Plugins\REST\Response::redirect( "/cards" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /cards/{hashid}/status * @route.description REST-Antwort zur Status-Umschaltung eines Kalenders */ public static function restResponseToggleStatusPrepared( string $hashid ) : HTTP\Response { return parent::restResponseToggleStatusPrepared( $hashid ); } /** * @internal rest * @method POST * * @route.path /cards/verify * @route.description REST-Konfiguration für Benutzer Lougoin */ public static function restResponseVerify( array $data ) : HTTP\Response { return static::restResponse( static::verify( $data ), function(){ $referer = HTTP\Request::referer(); redirect( "/api/v1/rest/{$referer["path"]}" ); }); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /cards * @route.description REST-Konfiguration für Logbuch-Tabelle */ public static function restConfigOverview() : BaseGenerator { return Plugins\REST\Response::addWrapper( "details", static::getPlural() )->get() ->addTab( "Übersicht", FALSE )->get() ->setIcon( static::getIcon() ) ->addHeadline( static::getPlural(), 2 ) ->addCallable( [ static::class, "restConfigTable" ] ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "cards" )) ; } /** * @internal rest * @method GET * * @route.path /cards/{hashid} * @route.description REST-Konfiguration für Detailansicht eines Logeintrags */ public static function restConfigDetails( string $hashid ) : BaseGenerator { return Plugins\REST\Response::addWrapper( "details", static::getSingular( "%t bearbeiten" ) ) ->setBacklink( static::getRoute() ) ->get() ->setApi( static::getRoute( $hashid ) ) ->setAction( static::getRoute( $hashid ), "PUT" ) ->addTab( static::getSingular() )->get() ->addHeadline( static::getSingular(), 2 ) ->addCallable( [ static::class, "restConfigInputs" ], $hashid ) ->parent() ->addCallable( [ Plugins\Logs\API\v1\Controller::class, "restConfigTab" ], $hashid ) ->addTab( "Speichern" )->get() ->setColor( Plugins\REST\Generator\Config\Color::Primary ) ->setIcon( "bi bi-save" ) ->setType( "submit" ); } /** * @internal rest * @method GET * * @route.path /cards/create * @route.description REST-Konfiguration für das Erstellen eines Kalenders (Formular) */ public static function restConfigCreatePrepared() : BaseGenerator { return parent::restConfigCreatePrepared(); } /** * @internal rest * @method GET * * @route.path /cards/{hashid}/delete * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Logeintrags */ public static function restConfigDeletePrepared( string $hashid ) : BaseGenerator { return parent::restConfigDeletePrepared( $hashid ); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTable( BaseGenerator $rest ) : BaseGenerator { return $rest ->addTable()->get() ->setApi( static::getRoute() ) ->setAction( static::getRoute("{hashid}") ) ->setOrder( "uuid", "asc" ) ->setSearch( TRUE ) ->addCallable( [ static::class, "restConfigColumns" ] ) ; } public static function restConfigColumns( BaseGenerator $rest ) : BaseGenerator { return $rest ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [ "label" => Plugins\Cards\Status::getSingular(), "icon" => Plugins\Cards\Status::getIcon(), "options" => Plugins\Cards\Status::getAll(), ], "status", [], "cards", "hashid" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [ "label" => Plugins\Cards\Type::getSingular(), "icon" => Plugins\Cards\Type::getIcon(), "options" => Plugins\Cards\Type::getAll(), ], "type", [], "cards", "hashid" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "uuid", TRUE, TRUE, TRUE, "cards" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [ "label" => "Standort", ], "location.name", TRUE, TRUE, TRUE, "cards" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [ "label" => "Benutzer", ], "user.username", TRUE, TRUE, TRUE, "cards" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_edit", TRUE, FALSE, TRUE, "cards" )) ; } public static function restConfigInputs( BaseGenerator $rest ) : BaseGenerator { return $rest ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "cards" )) // ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "additional", FALSE, "cards" )) // ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "address", FALSE, "cards" )) // ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "zipcode", FALSE, "cards" )) // ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "city", FALSE, "cards" )) ; } }