/** Datei: plugins/contacts/library/ARUNOX/Plugins/Contacts/Contacts/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Contacts\Contacts\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\REST; class Routes { public static function register() : void { /* ### MAIN ROUTES ############################## # */ Router::addAll( Controller::class ); /* ### MENU ROUTES ############################## # */ } } /** Datei: plugins/contacts/library/ARUNOX/Plugins/Contacts/Contacts/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Contacts\Contacts\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Logs; use ARUNOX\Plugins\Contacts; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Kontakt nicht gefunden", "saved" => "Kontakt wurde erfolgreich gespeichert", "deleted" => "Kontakt wurde erfolgreich gelöscht", "created" => "Kontakt wurde erfolgreich erstellt", "statuschanged.0" => "Kontakt wurde erfolgreich deaktiviert", "statuschanged.1" => "Kontakt wurde erfolgreich aktiviert", "notcreated" => "Kontakt konnte nicht erstellt werden", "notsaved" => "Kontakt konnte nicht gespeichert werden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /contacts/{hashid}/contacts * @route.description Listet alle Kontakte */ public static function getAllOwn( string $origin ) : array { return Contacts\Model::getByContact( $origin ); } /** * @api * @method GET * * @route.path /contacts/{hashid}/contacts/{hashid} * @route.description Gibt einen einzelnen Kontakts anhand der ID zurück */ public static function get( string $origin, string $hashid ) : BaseEntity | HTTP\Response { if( Contacts\Model::isSubContact( $hashid, $origin ) ) { if( $result = Contacts\Model::get( $hashid ) ) return $result; } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /contacts/{hashid}/contacts/{hashid} * @route.description Speichert Änderungen an einem bestehenden Kontakts */ public static function save( string $origin, string $hashid, array $data ) : HTTP\Response { if( Contacts\Model::isSubContact( $hashid, $origin ) ) { return Contacts\API\v1\Controller::save( $hashid, $data ); } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /** * @api * @method POST * * @route.path /contacts/{hashid}/contacts * @route.description Erstellt einen neuen Kontakt */ public static function create( string $origin, array $data ) : HTTP\Response { if( $hashid = Contacts\Model::save( $data ) ) { $result = Contacts\Model::get( $hashid ); return REST\Response::autoResponse( 200, static::getNotify("created"), [ "data" => $result ] ); } return REST\Response::autoResponse( 500, static::getNotify("notfound") ); } /** * @api * @method DELETE * * @route.path /contacts/{hashid}/contacts/{hashid} * @route.description Löscht einen Kontakt anhand der ID */ public static function delete( string $origin, string $hashid ) : HTTP\Response { if( Contacts\Model::isSubContact( $hashid, $origin ) ) { return Contacts\API\v1\Controller::delete( $hashid ); } return REST\Response::autoResponse( 500, static::getNotify("notfound") ); } /** * @api * @method PATCH * * @route.path /contacts/{hashid}/contacts/{hashid}/status * @route.description Wechselt den Status (aktiv/inaktiv) eines Kontakts */ public static function toggleStatus( int|string $origin, int|string $hashid ) : HTTP\Response { if( Contacts\Model::isSubContact( $hashid, $origin ) ) { return Contacts\API\v1\Controller::toggleStatus( $hashid ); } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /** * @api * @method PATCH * * @route.path /contacts/{hashid}/contacts/{hashid}/invoice * @route.description Wechselt den Status (aktiv/inaktiv) eines Kontakts */ public static function toggleInvoice( int|string $origin, int|string $hashid ) : HTTP\Response { if( Contacts\Model::isSubContact( $hashid, $origin ) ) { return Contacts\API\v1\Controller::toggleInvoice( $hashid ); } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /contacts/{hashid}/contacts/{hashid} * @route.description REST-Antwort nach dem Speichern eines Kontakts */ public static function restResponseSave( int|string $origin, int|string $hashid, array $data ) : HTTP\Response { $result = static::save( $origin, $hashid, $data ); if( $result->isStatus( 200 ) ) { $data = Contacts\Model::get( $hashid ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", "/api/v1/contacts/{hashid}/contacts", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /contacts/{hashid}/contacts * @route.description REST-Antwort nach dem Erstellen eines Kontakts (inkl. Notifikation, Redirect etc.) */ public static function restResponseCreate( int|string $origin, array $data ) : HTTP\Response { $result = static::create( $origin, $data ); if( $result->isStatus( 200 ) ) { REST\Response::closeModals(); REST\Response::redirect( "/contacts/{hashid}" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /contacts/{hashid}/contacts/{hashid} * @route.description REST-Antwort nach dem Löschen eines Kontakts */ public static function restResponseDelete( int|string $origin, int|string $hashid ) : HTTP\Response { REST\Response::closeModals(); $result = static::delete( $origin, $hashid ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/contacts/{hashid}" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /contacts/{hashid}/contacts/{hashid}/status * @route.description REST-Antwort zur Status-Umschaltung eines Kontakts (z. B. für Notifications) */ public static function restResponseToggleStatus( int|string $origin, int|string $hashid ) : HTTP\Response { $result = static::toggleStatus( $origin, $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", "/api/v1/contacts/{$origin}/contacts", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /contacts/{hashid}/contacts/{hashid}/invoice * @route.description REST-Antwort zur Status-Umschaltung eines Kontakts (z. B. für Notifications) */ public static function restResponseToggleInvoice( int|string $origin, int|string $hashid ) : HTTP\Response { $result = static::toggleInvoice( $origin, $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", "/api/v1/contacts/{$origin}/contacts", $hashid ], $data ); } return static::restResponse( $result ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /contacts/{hashid}/contacts * @route.description REST-Konfiguration für Kontakt-Tabelle */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", "Kontakte" ) ->replaceContent( static::restConfigTable() ); } /** * @internal rest * @method GET * * @route.path /contacts/{hashid}/contacts/{hashid} * @route.description REST-Konfiguration für Detailansicht eines Kontakts */ public static function restConfigDetails( int|string $origin, int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Kontakt bearbeiten" ) ->get() ->setApi( "/contacts/{$origin}/contacts/{$hashid}" ) ->setAction( "/contacts/{$origin}/contacts/{$hashid}", "PUT" ) ->addTab( "Kontaktdaten", "contactdata" )->get() ->setIcon( "bi-person-vcard" ) ->addHeadline( "Kontakt", 2 ) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "company_name", FALSE, "contacts" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "firstname", FALSE, "contacts" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "lastname", FALSE, "contacts" )) ->parent() ->addCallable( [ Logs\API\v1\Controller::class, "restConfigTab" ], $hashid ) ->addTab( "Speichern" )->get() ->setColor( REST\Generator\Config\Color::Primary ) ->setIcon( "bi bi-save" ) ->setType( "submit" ); } /** * @internal rest * @method GET * * @route.path /contacts/{hashid}/contacts/create * @route.description REST-Konfiguration für das Erstellen eines Kontakts (Formular) */ public static function restConfigCreate() : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Kontakt erstellen" ) ->get() ->setAction( "/contacts/{hashid}/contacts", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "contacts" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "contacts" )); } /** * @internal rest * @method GET * * @route.path /contacts/{hashid}/contacts/{hashid}/delete * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Kontakts */ public static function restConfigDeleteOwn( int|string $origin, int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Kontakt Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/contacts/{hashid}/contacts/{$hashid}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$hashid}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "contacts" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "contacts" )); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTable( int|string $origin ) : REST\Generator\Base\BaseGenerator { $table = Contacts\API\v1\Controller::restConfigTable()->top() ->setApi( "/contacts/{$origin}/contacts" ) ->setAction( "/contacts/{$origin}/contacts/{hashid}" ) ->getColumn( "status")->setAction( "/contacts/{$origin}/contacts/{hashid}/status", "PATCH" )->parent() ->getColumn( "invoice")->setAction( "/contacts/{$origin}/contacts/{hashid}/invoice", "PATCH" )->parent() ; $actions = $table->top()->getColumn("actions")->getConfig("buttons"); foreach( $actions as $button ) { $action = $button->getAction(); if( preg_match( "/^\/contacts\/\{hashid\}/", $action["route"] ) ) { $action["route"] = preg_replace( "/^\/contacts\/\{hashid\}/", "/contacts/{$origin}/contacts/{hashid}", $action["route"] ); $button->setAction( ...$action ); } // show( $action, FALSE ); } return $table; } }