* Construct a new mailing object, along with job and mailing_group
* objects, from the form values of the create mailing wizard.
*
+ * This function is a bit evil. It not only merges $params and saves
+ * the mailing -- it also schedules the mailing and chooses the recipients.
+ * Since it merges $params, it's also the only place to correctly trigger
+ * multi-field validation. It should be broken up.
+ *
+ * In the mean time, use-cases which break under the weight of this
+ * evil may find reprieve in these extra evil params:
+ *
+ * - _skip_evil_bao_auto_recipients_: bool
+ * - _skip_evil_bao_auto_schedule_: bool
+ * - _evil_bao_validator_: string|callable
+ *
+ * </twowrongsmakesaright>
+ *
* @params array $params
* Form values.
*
*
* @return object
* $mailing The new mailing object
+ * @throws \Exception
*/
public static function create(&$params, $ids = array()) {
CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id);
// If we're going to autosend, then check validity before saving.
- if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') {
- $errors = static::checkSendable($mailing);
+ if (!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);
// Create parent job if not yet created.
// Condition on the existence of a scheduled date.
- if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') {
+ if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) {
$job = new CRM_Mailing_BAO_MailingJob();
$job->mailing_id = $mailing->id;
$job->status = 'Scheduled';