Move validator out of BAO into v3 api
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 17 Feb 2022 23:40:57 +0000 (12:40 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 18 Feb 2022 21:53:45 +0000 (10:53 +1300)
m

m

CRM/Mailing/BAO/Mailing.php
api/v3/Mailing.php
ext/flexmailer/src/Validator.php

index a3fc95dd6a8adb98a47582c000e53df6d067881e..b099f6beb645048b86dca8b0018775c80fb83df4 100644 (file)
@@ -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
    *
    * </twowrongsmakesaright>
    *
@@ -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.
index 83bc28b7412cbb2ab05c3826cc6e8dd9583dd7ac..22403a74444ad3422e4d222fac14e261ffcffbd2 100644 (file)
@@ -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);
 }
index f0818634d3b6f85c8191d138044bd04b1d7babca..455d6f0538815c7d820bae2b9e013a6eb92aae6e 100644 (file)
@@ -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),
     ));