. */ namespace SP\Modules\Web\Controllers\AccountManager; use Exception; use SP\Core\Acl\ActionsInterface; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Account\Ports\AccountServiceInterface; use SP\Domain\CustomField\Ports\CustomFieldServiceInterface; use SP\Http\JsonResponse; 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 JsonTrait, ItemTrait; private AccountServiceInterface $accountService; private CustomFieldServiceInterface $customFieldService; public function __construct( Application $application, WebControllerHelper $webControllerHelper, AccountServiceInterface $accountService, CustomFieldServiceInterface $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(ActionsInterface::ACCOUNT, $id, $this->customFieldService); $this->eventDispatcher->notifyEvent( 'delete.account.selection', new Event($this, EventMessage::factory()->addDescription(__u('Accounts removed'))) ); return $this->returnJsonResponseData(JsonResponse::JSON_SUCCESS, __u('Accounts removed')); } $accountDetails = $this->accountService->getById($id)->getAccountVData(); $this->accountService->delete($id); $this->deleteCustomFieldsForItem(ActionsInterface::ACCOUNT, $id, $this->customFieldService); $this->eventDispatcher->notifyEvent( 'delete.account', new Event( $this, EventMessage::factory() ->addDescription(__u('Account removed')) ->addDetail(__u('Account'), $accountDetails->getName()) ->addDetail(__u('Client'), $accountDetails->getClientName()) ) ); return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Account removed')); } catch (Exception $e) { processException($e); return $this->returnJsonResponseException($e); } } }