Refactor form bulk action to docrine
[MAILPOET-3630]
This commit is contained in:
@ -8,6 +8,7 @@ use MailPoet\API\JSON\Error;
|
|||||||
use MailPoet\API\JSON\Error as APIError;
|
use MailPoet\API\JSON\Error as APIError;
|
||||||
use MailPoet\API\JSON\Response;
|
use MailPoet\API\JSON\Response;
|
||||||
use MailPoet\API\JSON\ResponseBuilders\FormsResponseBuilder;
|
use MailPoet\API\JSON\ResponseBuilders\FormsResponseBuilder;
|
||||||
|
use MailPoet\API\JSON\SuccessResponse;
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
use MailPoet\Entities\FormEntity;
|
use MailPoet\Entities\FormEntity;
|
||||||
use MailPoet\Form\ApiDataSanitizer;
|
use MailPoet\Form\ApiDataSanitizer;
|
||||||
@ -19,19 +20,15 @@ use MailPoet\Form\Listing\FormListingRepository;
|
|||||||
use MailPoet\Form\PreviewPage;
|
use MailPoet\Form\PreviewPage;
|
||||||
use MailPoet\Listing;
|
use MailPoet\Listing;
|
||||||
use MailPoet\Settings\UserFlagsController;
|
use MailPoet\Settings\UserFlagsController;
|
||||||
|
use MailPoet\UnexpectedValueException;
|
||||||
use MailPoet\WP\Emoji;
|
use MailPoet\WP\Emoji;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Forms extends APIEndpoint {
|
class Forms extends APIEndpoint {
|
||||||
|
|
||||||
|
|
||||||
public $permissions = [
|
public $permissions = [
|
||||||
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
|
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var Listing\BulkActionController */
|
|
||||||
private $bulkAction;
|
|
||||||
|
|
||||||
/** @var Listing\Handler */
|
/** @var Listing\Handler */
|
||||||
private $listingHandler;
|
private $listingHandler;
|
||||||
|
|
||||||
@ -63,7 +60,6 @@ class Forms extends APIEndpoint {
|
|||||||
private $formSaveController;
|
private $formSaveController;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Listing\BulkActionController $bulkAction,
|
|
||||||
Listing\Handler $listingHandler,
|
Listing\Handler $listingHandler,
|
||||||
UserFlagsController $userFlags,
|
UserFlagsController $userFlags,
|
||||||
FormFactory $formFactory,
|
FormFactory $formFactory,
|
||||||
@ -75,7 +71,6 @@ class Forms extends APIEndpoint {
|
|||||||
ApiDataSanitizer $dataSanitizer,
|
ApiDataSanitizer $dataSanitizer,
|
||||||
FormSaveController $formSaveController
|
FormSaveController $formSaveController
|
||||||
) {
|
) {
|
||||||
$this->bulkAction = $bulkAction;
|
|
||||||
$this->listingHandler = $listingHandler;
|
$this->listingHandler = $listingHandler;
|
||||||
$this->userFlags = $userFlags;
|
$this->userFlags = $userFlags;
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
@ -330,15 +325,20 @@ class Forms extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bulkAction($data = []) {
|
public function bulkAction($data = []): SuccessResponse {
|
||||||
try {
|
$definition = $this->listingHandler->getListingDefinition($data['listing']);
|
||||||
$meta = $this->bulkAction->apply('\MailPoet\Models\Form', $data);
|
$ids = $this->formListingRepository->getActionableIds($definition);
|
||||||
return $this->successResponse(null, $meta);
|
if ($data['action'] === 'trash') {
|
||||||
} catch (\Exception $e) {
|
$this->formsRepository->bulkTrash($ids);
|
||||||
return $this->errorResponse([
|
} elseif ($data['action'] === 'restore') {
|
||||||
$e->getCode() => $e->getMessage(),
|
$this->formsRepository->bulkRestore($ids);
|
||||||
]);
|
} elseif ($data['action'] === 'delete') {
|
||||||
|
$this->formsRepository->bulkDelete($ids);
|
||||||
|
} else {
|
||||||
|
throw UnexpectedValueException::create()
|
||||||
|
->withErrors([APIError::BAD_REQUEST => "Invalid bulk action '{$data['action']}' provided."]);
|
||||||
}
|
}
|
||||||
|
return $this->successResponse(null, ['count' => count($ids)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getForm(array $data): ?FormEntity {
|
private function getForm(array $data): ?FormEntity {
|
||||||
|
@ -4,7 +4,6 @@ namespace MailPoet\Form;
|
|||||||
|
|
||||||
use MailPoet\Doctrine\Repository;
|
use MailPoet\Doctrine\Repository;
|
||||||
use MailPoet\Entities\FormEntity;
|
use MailPoet\Entities\FormEntity;
|
||||||
use MailPoetVendor\Carbon\Carbon;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends Repository<FormEntity>
|
* @extends Repository<FormEntity>
|
||||||
@ -37,21 +36,56 @@ class FormsRepository extends Repository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function delete(FormEntity $form) {
|
public function delete(FormEntity $form) {
|
||||||
$this->remove($form);
|
$this->entityManager->remove($form);
|
||||||
$this->flush();
|
$this->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function trash(FormEntity $form) {
|
public function trash(FormEntity $form) {
|
||||||
$this->updateDeletedAt($form, Carbon::now());
|
$this->bulkTrash([$form->getId()]);
|
||||||
|
$this->entityManager->refresh($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function restore(FormEntity $form) {
|
public function restore(FormEntity $form) {
|
||||||
$this->updateDeletedAt($form, null);
|
$this->bulkRestore([$form->getId()]);
|
||||||
|
$this->entityManager->refresh($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateDeletedAt(FormEntity $form, ?Carbon $value) {
|
public function bulkTrash(array $ids): int {
|
||||||
$form->setDeletedAt($value);
|
if (empty($ids)) {
|
||||||
$this->persist($form);
|
return 0;
|
||||||
$this->flush();
|
}
|
||||||
|
|
||||||
|
return $this->entityManager->createQueryBuilder()
|
||||||
|
->update(FormEntity::class, 'f')
|
||||||
|
->set('f.deletedAt', 'CURRENT_TIMESTAMP()')
|
||||||
|
->where('f.id IN (:ids)')
|
||||||
|
->setParameter('ids', $ids)
|
||||||
|
->getQuery()->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function bulkRestore(array $ids): int {
|
||||||
|
if (empty($ids)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->entityManager->createQueryBuilder()
|
||||||
|
->update(FormEntity::class, 'f')
|
||||||
|
->set('f.deletedAt', ':deletedAt')
|
||||||
|
->where('f.id IN (:ids)')
|
||||||
|
->setParameter('deletedAt', null)
|
||||||
|
->setParameter('ids', $ids)
|
||||||
|
->getQuery()->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function bulkDelete(array $ids): int {
|
||||||
|
if (empty($ids)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->entityManager->createQueryBuilder()
|
||||||
|
->delete(FormEntity::class, 'f')
|
||||||
|
->where('f.id IN (:ids)')
|
||||||
|
->setParameter('ids', $ids)
|
||||||
|
->getQuery()->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user