Add API method deleteList
[MAILPOET-4752]
This commit is contained in:
@ -80,6 +80,10 @@ class API {
|
||||
return $this->segments->addList($list);
|
||||
}
|
||||
|
||||
public function deleteList(string $listId): bool {
|
||||
return $this->segments->deleteList($listId);
|
||||
}
|
||||
|
||||
public function updateList(array $list): array {
|
||||
return $this->segments->updateList($list);
|
||||
}
|
||||
|
@ -20,4 +20,7 @@ class APIException extends \Exception {
|
||||
const WELCOME_FAILED_TO_SEND = 17;
|
||||
const LIST_ID_REQUIRED = 18;
|
||||
const FAILED_TO_UPDATE_LIST = 19;
|
||||
const LIST_USED_IN_EMAIL = 20;
|
||||
const LIST_USED_IN_FORM = 21;
|
||||
const FAILED_TO_DELETE_LIST = 22;
|
||||
}
|
||||
|
@ -3,17 +3,29 @@
|
||||
namespace MailPoet\API\MP\v1;
|
||||
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Form\FormsRepository;
|
||||
use MailPoet\Newsletter\Segment\NewsletterSegmentRepository;
|
||||
use MailPoet\Segments\SegmentsRepository;
|
||||
|
||||
class Segments {
|
||||
private const DATE_FORMAT = 'Y-m-d H:i:s';
|
||||
|
||||
/** @var NewsletterSegmentRepository */
|
||||
private $newsletterSegmentRepository;
|
||||
|
||||
/** @var FormsRepository */
|
||||
private $formsRepository;
|
||||
|
||||
/** @var SegmentsRepository */
|
||||
private $segmentsRepository;
|
||||
|
||||
public function __construct (
|
||||
NewsletterSegmentRepository $newsletterSegmentRepository,
|
||||
FormsRepository $formsRepository,
|
||||
SegmentsRepository $segmentsRepository
|
||||
) {
|
||||
$this->newsletterSegmentRepository = $newsletterSegmentRepository;
|
||||
$this->formsRepository = $formsRepository;
|
||||
$this->segmentsRepository = $segmentsRepository;
|
||||
}
|
||||
|
||||
@ -66,6 +78,52 @@ class Segments {
|
||||
return $this->buildItem($segment);
|
||||
}
|
||||
|
||||
public function deleteList(string $listId): bool {
|
||||
$this->validateSegmentId($listId);
|
||||
|
||||
$activelyUsedNewslettersSubjects = $this->newsletterSegmentRepository->getSubjectsOfActivelyUsedEmailsForSegments([$listId]);
|
||||
if (isset($activelyUsedNewslettersSubjects[$listId])) {
|
||||
throw new APIException(
|
||||
str_replace(
|
||||
'%1$s',
|
||||
"'" . join("', '", $activelyUsedNewslettersSubjects[$listId] ) . "'",
|
||||
// translators: %1$s is a comma-seperated list of emails for which the segment is used.
|
||||
_x('List cannot be deleted because it’s used for %1$s email', 'Alert shown when trying to delete segment, which is assigned to any automatic emails.', 'mailpoet')
|
||||
),
|
||||
APIException::LIST_USED_IN_EMAIL
|
||||
);
|
||||
}
|
||||
|
||||
$activelyUsedFormNames = $this->formsRepository->getNamesOfFormsForSegments();
|
||||
if (isset($activelyUsedFormNames[$listId])) {
|
||||
throw new APIException(
|
||||
str_replace(
|
||||
'%1$s',
|
||||
"'" . join("', '", $activelyUsedFormNames[$listId] ) . "'",
|
||||
// translators: %1$s is a comma-seperated list of forms for which the segment is used.
|
||||
_nx(
|
||||
'List cannot be deleted because it’s used for %1$s form',
|
||||
'List cannot be deleted because it’s used for %1$s forms',
|
||||
count($activelyUsedFormNames[$listId]),
|
||||
'Alert shown when trying to delete segment, when it is assigned to a form.',
|
||||
'mailpoet'
|
||||
)
|
||||
),
|
||||
APIException::LIST_USED_IN_FORM
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->segmentsRepository->bulkDelete([$listId]);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
throw new APIException(
|
||||
__('The list couldn’t be deleted from the database', 'mailpoet'),
|
||||
APIException::FAILED_TO_DELETE_LIST
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateSegmentId(string $segmentId): void {
|
||||
if (empty($segmentId)) {
|
||||
throw new APIException(
|
||||
|
Reference in New Issue
Block a user