From 7e0fcd9d1073493a953b46caad225498e470f546 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 18 Feb 2022 12:40:57 +1300 Subject: [PATCH] Move validator out of BAO into v3 api m m --- CRM/Mailing/BAO/Mailing.php | 12 ------------ api/v3/Mailing.php | 17 +++++++++++++++-- ext/flexmailer/src/Validator.php | 14 ++++++++++---- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index a3fc95dd6a..b099f6beb6 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -1478,7 +1478,6 @@ ORDER BY civicrm_email.is_bulkmail DESC * * - _skip_evil_bao_auto_recipients_: bool * - _skip_evil_bao_auto_schedule_: bool - * - _evil_bao_validator_: string|callable * * * @@ -1612,17 +1611,6 @@ ORDER BY civicrm_email.is_bulkmail DESC // check and attach and files as needed CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id); - // If we're going to autosend, then check validity before saving. - if (empty($params['is_completed']) && !empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) { - $cb = Civi\Core\Resolver::singleton() - ->get($params['_evil_bao_validator_']); - $errors = call_user_func($cb, $mailing); - if (!empty($errors)) { - $fields = implode(',', array_keys($errors)); - throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors); - } - } - $transaction->commit(); // Create parent job if not yet created. diff --git a/api/v3/Mailing.php b/api/v3/Mailing.php index 83bc28b741..22403a7444 100644 --- a/api/v3/Mailing.php +++ b/api/v3/Mailing.php @@ -36,9 +36,22 @@ function civicrm_api3_mailing_create($params) { if (!$timestampCheck) { throw new API_Exception("Mailing has not been saved, Content maybe out of date, please refresh the page and try again"); } + // If we're going to autosend, then check validity before saving. + if (empty($params['is_completed']) && !empty($params['scheduled_date']) + && $params['scheduled_date'] !== 'null' + // This might have been passed in as empty to prevent us validating, is set skip. + && !isset($params['_evil_bao_validator_'])) { + + // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail. + $function = \CRM_Utils_Constant::value('CIVICRM_FLEXMAILER_HACK_SENDABLE', 'CRM_Mailing_BAO_Mailing::checkSendable'); + $validationFunction = Civi\Core\Resolver::singleton()->get($function); + $errors = call_user_func($validationFunction, $params); + if (!empty($errors)) { + $fields = implode(',', array_keys($errors)); + throw new CiviCRM_API3_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors); + } + } - // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail. - $safeParams['_evil_bao_validator_'] = \CRM_Utils_Constant::value('CIVICRM_FLEXMAILER_HACK_SENDABLE', 'CRM_Mailing_BAO_Mailing::checkSendable'); $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $safeParams, 'Mailing'); return _civicrm_api3_mailing_get_formatResult($result); } diff --git a/ext/flexmailer/src/Validator.php b/ext/flexmailer/src/Validator.php index f0818634d3..455d6f0538 100644 --- a/ext/flexmailer/src/Validator.php +++ b/ext/flexmailer/src/Validator.php @@ -25,14 +25,20 @@ class Validator { const EVENT_CHECK_SENDABLE = 'civi.flexmailer.checkSendable'; /** - * @param \CRM_Mailing_DAO_Mailing $mailing + * @param array $params * The mailing which may or may not be sendable. * @return array * List of error messages. */ - public static function createAndRun($mailing) { - $validator = new \Civi\FlexMailer\Validator(); - return $validator->run(array( + public static function createAndRun(array $params): array { + $mailing = new \CRM_Mailing_BAO_Mailing(); + $mailing->id = $params['id'] ?? NULL; + if ($mailing->id) { + $mailing->find(TRUE); + } + $mailing->copyValues($params); + + return (new Validator())->run(array( 'mailing' => $mailing, 'attachments' => \CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id), )); -- 2.25.1