/** Datei: plugins/products/library/ARUNOX/Plugins/Products/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Products\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 ############################## # */ Hooks::add('router::get->api.v1.rest.routes', function( $routes ) { $routes[] = [ "path" => "#", "path" => "/products", "label" => "Artikelstamm", "icon" => "bi bi-box-seam", "menu" => "content", // "name" => "products", ]; $routes[] = [ "path" => "/products", "label" => "Produkte", "icon" => "bi bi-list-ul", "menu" => "products", ]; return $routes; }); } } /** Datei: plugins/products/library/ARUNOX/Plugins/Products/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Products\API\v1; use ARUNOX\HTTP; use ARUNOX\API\PHP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Products; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /products * @route.description Listet alle Produkte */ public static function getAllOwn( $args ) : array { if( isset( $args["type"] ) ) { return Products\Model::getAll([ "type" => $args["type"] ]); } return Products\Model::getAll(); } /** * @api * @method GET * * @route.path /products/{hashid} * @route.description Gibt einen einzelnen Produkts anhand der ID zurück */ public static function get( string $hashid ) : BaseEntity | HTTP\Response { if( $result = Products\Model::get( $hashid ) ) return $result; return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /products/{hashid} * @route.description Speichert Änderungen an einem bestehenden Produkts */ public static function save( string $hashid, array $data ) : HTTP\Response { if( $result = Products\Model::get( $hashid ) ) { if( $result->save( $data ) ) { return REST\Response::autoResponse( 200, static::getNotify("saved") ); } return REST\Response::autoResponse( 500, static::getNotify("notsaved") ); } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /** * @api * @method POST * * @route.path /products * @route.description Erstellt einen neues Produkt */ public static function create( array $data ) : HTTP\Response { if( $hashid = Products\Model::save( $data ) ) { $result = Products\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 /products/{hashid} * @route.description Löscht ein Produkt anhand der ID */ public static function delete( string $hashid ) : HTTP\Response { if( Products\Model::delete( $hashid ) ) { return REST\Response::autoResponse( 200, static::getNotify("deleted") ); } return REST\Response::autoResponse( 500, static::getNotify("notfound") ); } /** * @api * @method PATCH * * @route.path /products/{hashid}/status * @route.description Wechselt den Status (aktiv/inaktiv) eines Produkts */ public static function toggleStatus( string $hashid ) : HTTP\Response { if( $result = Products\Model::get( $hashid ) ) { $result->toggleStatus( TRUE ); return REST\Response::autoResponse( 200, static::getNotify("statuschanged.{$result->status->getValue()}" ), [ "data" => $result ] ); } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /** * @api * @method PATCH * * @route.path /products/{hashid}/invoice * @route.description Wechselt den Status (aktiv/inaktiv) eines Produkts */ public static function toggleInvoice( string $hashid ) : HTTP\Response { if( $result = Products\Model::get( $hashid ) ) { $result->toggleInvoice( TRUE ); return REST\Response::autoResponse( 200, static::getNotify("statuschanged.{$result->status->getValue()}" ), [ "data" => $result ] ); } return REST\Response::autoResponse( 404, static::getNotify("notfound") ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /products/{hashid} * @route.description REST-Antwort nach dem Speichern eines Produkts */ public static function restResponseSave( string $hashid, array $data ) : HTTP\Response { $result = static::save( $hashid, $data ); if( $result->isStatus( 200 ) ) { $data = Products\Model::get( $hashid ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /products * @route.description REST-Antwort nach dem Erstellen eines Produkts (inkl. Notifikation, Redirect etc.) */ public static function restResponseCreate( array $data ) : HTTP\Response { $result = static::create( $data ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); $type = Products\Type::get( $data->type ); REST\Response::closeModals(); REST\Response::redirect( "/products#{$type["plural"]}" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /products/{hashid} * @route.description REST-Antwort nach dem Löschen eines Produkts */ public static function restResponseDelete( string $hashid ) : HTTP\Response { REST\Response::closeModals(); $result = static::delete( $hashid ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/products" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /products/{hashid}/status * @route.description REST-Antwort zur Status-Umschaltung eines Produkts (z. B. für Notifications) */ public static function restResponseToggleStatus( string $hashid ) : HTTP\Response { $result = static::toggleStatus( $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /products/{hashid}/invoice * @route.description REST-Antwort zur Status-Umschaltung eines Produkts (z. B. für Notifications) */ public static function restResponseToggleInvoice( string $hashid ) : HTTP\Response { $result = static::toggleInvoice( $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /products * @route.description REST-Konfiguration für Produkt-Tabelle */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { $tabs = REST\Response::addWrapper( "details", "Artikelstamm" )->get(); foreach( Products\Type::getAll() as $type ) { $tabs = $tabs->addTab( $type["plural"] )->get() ->setIcon( $type["icon"] ) ->addContent( static::restConfigTable( $type["value"] ) ) ->parent(); } $tabs->addTab( "Sales prices", "sales" )->get() ->setIcon( "bi-currency-euro" ) ->addContent( Products\Sales\Types\API\v1\Controller::restConfigTable() ) ->parent(); return $tabs; } /** * @internal rest * @method GET * * @route.path /products/{hashid} * @route.description REST-Konfiguration für Detailansicht eines Produkts */ public static function restConfigDetails( string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "details", "Produkt bearbeiten" ) ->setBacklink( "/products" ) ->get() ->setApi( "/products/{$hashid}" ) ->setAction( "/products/{$hashid}", "PUT" ) ->addTab( "Produkt" )->get() ->addHeadline( "Produkt", 2 ) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "company_name", FALSE, "products" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "firstname", FALSE, "products" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "lastname", FALSE, "products" )) ->parent() ->addTab( "Ansprechpartner" )->get() ->addContent( // Contacts\Contacts\API\v1\Controller::restConfigTable( $hashid ) ) ->parent() ->addTab( "Adressen" )->get() ->addContent( // Contacts\Addresses\API\v1\Controller::restConfigTable( $hashid ) ) ->parent() ->addTab( "Speichern" )->get() ->setColor( REST\Generator\Config\Color::Primary ) ->setIcon( "bi bi-save" ) ->setType( "submit" ); } /** * @internal rest * @method GET * * @route.path /products/create * @route.description REST-Konfiguration für das Erstellen eines Produkts (Formular) */ public static function restConfigCreate( $args ) : REST\Generator\Base\BaseGenerator { $option = Products\Type::get( $args["type"] ); return REST\Response::addModal( "details", "{$option["label"]} erstellen" ) ->get() ->setAction( "/products", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "products" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "products" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "custom_id", FALSe, "products" )) ->addField(...Hooks::filter( "rest.generator::control.type->textarea", [], "description ", FALSE, "products" )) ->addField(...Hooks::filter( "rest.generator::control.type->hidden", [], "type", $args["type"], "products" )) ; } /** * @internal rest * @method GET * * @route.path /products/{hashid}/delete * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Produkts */ public static function restConfigDelete( string $hashid ) : REST\Generator\Base\BaseGenerator { $type = Products\Model::getValue( $hashid, "type" ); return REST\Response::addModal( "details", "{$type->getLabel()} Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/products/{$hashid}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$hashid}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "products" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "products" )); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTable( int $type ) : REST\Generator\Base\BaseGenerator { $table = REST\Generator\Data\Table::create() ->setApi( "/products?type={$type}" ) ->setAction( "/products/{hashid}" ) ->setOrder( "name", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [ "action" => "/products/create?type={$type}", ], "products" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], "products", "hashid" )) // ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [ // "label" => Products\Type::label(), // "icon" => Products\Type::icon(), // "options" => Products\Type::getAll(), // ], "type", [], "products", "hashid" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "name", TRUE, TRUE, TRUE, "products" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_edit", FALSE, FALSE, TRUE, "products" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "products" ))->get() ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "products", "hashid" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "products", "hashid" )) ; return $table; } }