. */ namespace SP\Modules\Web\Controllers\AccountManager; use Exception; use JsonException; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Account\Ports\AccountService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\Http\Dtos\JsonMessage; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\ItemTrait; use SP\Mvc\Controller\WebControllerHelper; /** * Class AccountManagerController * * @package SP\Modules\Web\Controllers */ final class DeleteController extends ControllerBase { use ItemTrait; use JsonTrait; private AccountService $accountService; private CustomFieldDataService $customFieldService; public function __construct( Application $application, WebControllerHelper $webControllerHelper, AccountService $accountService, CustomFieldDataService $customFieldService ) { parent::__construct($application, $webControllerHelper); $this->accountService = $accountService; $this->customFieldService = $customFieldService; $this->checkLoggedIn(); } /** * Delete action * * @param int|null $id * * @return bool * @throws JsonException */ public function deleteAction(?int $id = null): bool { try { if ($id === null) { $this->accountService->deleteByIdBatch($this->getItemsIdFromRequest($this->request)); $this->deleteCustomFieldsForItem(AclActionsInterface::ACCOUNT, $id, $this->customFieldService); $this->eventDispatcher->notify( 'delete.account.selection', new Event($this, EventMessage::build()->addDescription(__u('Accounts removed'))) ); return $this->returnJsonResponseData(JsonMessage::JSON_SUCCESS, __u('Accounts removed')); } $accountDetails = $this->accountService->getByIdEnriched($id)->getAccountVData(); $this->accountService->delete($id); $this->deleteCustomFieldsForItem(AclActionsInterface::ACCOUNT, $id, $this->customFieldService); $this->eventDispatcher->notify( 'delete.account', new Event( $this, EventMessage::build() ->addDescription(__u('Account removed')) ->addDetail(__u('Account'), $accountDetails->getName()) ->addDetail(__u('Client'), $accountDetails->getClientName()) ) ); return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Account removed')); } catch (Exception $e) { processException($e); return $this->returnJsonResponseException($e); } } }