/**
  * @param $mailingGroupId
  */
 private static function deleteMailingGroup($mailingGroupId)
 {
     $group = new CRM_Mailing_DAO_MailingGroup();
     $group->reset();
     $group->id = $mailingGroupId;
     $group->delete();
 }
 /**
  * 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.
  *
  * @param array $params
  * @param array $ids
  *
  * @return object
  *   $mailing      The new mailing object
  * @throws \Exception
  */
 public static function create(&$params, $ids = array())
 {
     // WTH $ids
     if (empty($ids) && isset($params['id'])) {
         $ids['mailing_id'] = $ids['id'] = $params['id'];
     }
     // CRM-12430
     // Do the below only for an insert
     // for an update, we should not set the defaults
     if (!isset($ids['id']) && !isset($ids['mailing_id'])) {
         // Retrieve domain email and name for default sender
         $domain = civicrm_api('Domain', 'getsingle', array('version' => 3, 'current_domain' => 1, 'sequential' => 1));
         if (isset($domain['from_email'])) {
             $domain_email = $domain['from_email'];
             $domain_name = $domain['from_name'];
         } else {
             $domain_email = '*****@*****.**';
             $domain_name = 'EXAMPLE.ORG';
         }
         if (!isset($params['created_id'])) {
             $session =& CRM_Core_Session::singleton();
             $params['created_id'] = $session->get('userID');
         }
         $defaults = array('override_verp' => TRUE, 'forward_replies' => FALSE, 'open_tracking' => TRUE, 'url_tracking' => TRUE, 'visibility' => 'Public Pages', 'replyto_email' => $domain_email, 'header_id' => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''), 'footer_id' => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''), 'from_email' => $domain_email, 'from_name' => $domain_name, 'msg_template_id' => NULL, 'created_id' => $params['created_id'], 'approver_id' => NULL, 'auto_responder' => 0, 'created_date' => date('YmdHis'), 'scheduled_date' => NULL, 'approval_date' => NULL);
         // Get the default from email address, if not provided.
         if (empty($defaults['from_email'])) {
             $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
             foreach ($defaultAddress as $id => $value) {
                 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
                     $defaults['from_email'] = $match[2];
                     $defaults['from_name'] = $match[1];
                 }
             }
         }
         $params = array_merge($defaults, $params);
     }
     /**
      * Could check and warn for the following cases:
      *
      * - groups OR mailings should be populated.
      * - body html OR body text should be populated.
      */
     $transaction = new CRM_Core_Transaction();
     $mailing = self::add($params, $ids);
     if (is_a($mailing, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $mailing;
     }
     // update mailings with hash values
     CRM_Contact_BAO_Contact_Utils::generateChecksum($mailing->id, NULL, NULL, NULL, 'mailing', 16);
     $groupTableName = CRM_Contact_BAO_Group::getTableName();
     $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
     /* Create the mailing group record */
     $mg = new CRM_Mailing_DAO_MailingGroup();
     $groupTypes = array('include' => 'Include', 'exclude' => 'Exclude', 'base' => 'Base');
     foreach (array('groups', 'mailings') as $entity) {
         foreach (array('include', 'exclude', 'base') as $type) {
             if (isset($params[$entity][$type])) {
                 self::replaceGroups($mailing->id, $groupTypes[$type], $entity, $params[$entity][$type]);
             }
         }
     }
     if (!empty($params['search_id']) && !empty($params['group_id'])) {
         $mg->reset();
         $mg->mailing_id = $mailing->id;
         $mg->entity_table = $groupTableName;
         $mg->entity_id = $params['group_id'];
         $mg->search_id = $params['search_id'];
         $mg->search_args = $params['search_args'];
         $mg->group_type = 'Include';
         $mg->save();
     }
     // 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['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.
     // Condition on the existence of a scheduled date.
     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';
         $job->is_test = 0;
         if (!$job->find(TRUE)) {
             $job->scheduled_date = $params['scheduled_date'];
             $job->save();
         }
         // Populate the recipients.
         if (empty($params['_skip_evil_bao_auto_recipients_'])) {
             self::getRecipients($job->id, $mailing->id, TRUE, $mailing->dedupe_email);
         }
     }
     return $mailing;
 }
 /**
  * Construct a new mailing object, along with job and mailing_group
  * objects, from the form values of the create mailing wizard.
  *
  * @params array $params        Form values
  *
  * @return object $mailing      The new mailing object
  * @access public
  * @static
  */
 public static function create(&$params, $ids = array())
 {
     // CRM-12430
     // Do the below only for an insert
     // for an update, we should not set the defaults
     if (!isset($ids['id']) && !isset($ids['mailing_id'])) {
         // Retrieve domain email and name for default sender
         $domain = civicrm_api('Domain', 'getsingle', array('version' => 3, 'current_domain' => 1, 'sequential' => 1));
         if (isset($domain['from_email'])) {
             $domain_email = $domain['from_email'];
             $domain_name = $domain['from_name'];
         } else {
             $domain_email = '*****@*****.**';
             $domain_name = 'EXAMPLE.ORG';
         }
         if (!isset($params['created_id'])) {
             $session =& CRM_Core_Session::singleton();
             $params['created_id'] = $session->get('userID');
         }
         $defaults = array('override_verp' => TRUE, 'forward_replies' => FALSE, 'open_tracking' => TRUE, 'url_tracking' => TRUE, 'visibility' => 'User and User Admin Only', 'replyto_email' => $domain_email, 'header_id' => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''), 'footer_id' => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''), 'from_email' => $domain_email, 'from_name' => $domain_name, 'msg_template_id' => NULL, 'created_id' => $params['created_id'], 'approver_id' => NULL, 'auto_responder' => 0, 'created_date' => date('YmdHis'), 'scheduled_date' => NULL, 'approval_date' => NULL);
         // Get the default from email address, if not provided.
         if (empty($defaults['from_email'])) {
             $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
             foreach ($defaultAddress as $id => $value) {
                 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
                     $defaults['from_email'] = $match[2];
                     $defaults['from_name'] = $match[1];
                 }
             }
         }
         $params = array_merge($defaults, $params);
     }
     /**
      * Could check and warn for the following cases:
      *
      * - groups OR mailings should be populated.
      * - body html OR body text should be populated.
      */
     $transaction = new CRM_Core_Transaction();
     $mailing = self::add($params, $ids);
     if (is_a($mailing, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $mailing;
     }
     $groupTableName = CRM_Contact_BAO_Group::getTableName();
     $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
     /* Create the mailing group record */
     $mg = new CRM_Mailing_DAO_MailingGroup();
     foreach (array('groups', 'mailings') as $entity) {
         foreach (array('include', 'exclude', 'base') as $type) {
             if (isset($params[$entity]) && CRM_Utils_Array::value($type, $params[$entity]) && is_array($params[$entity][$type])) {
                 foreach ($params[$entity][$type] as $entityId) {
                     $mg->reset();
                     $mg->mailing_id = $mailing->id;
                     $mg->entity_table = $entity == 'groups' ? $groupTableName : $mailingTableName;
                     $mg->entity_id = $entityId;
                     $mg->group_type = $type;
                     $mg->save();
                 }
             }
         }
     }
     if (!empty($params['search_id']) && !empty($params['group_id'])) {
         $mg->reset();
         $mg->mailing_id = $mailing->id;
         $mg->entity_table = $groupTableName;
         $mg->entity_id = $params['group_id'];
         $mg->search_id = $params['search_id'];
         $mg->search_args = $params['search_args'];
         $mg->group_type = 'Include';
         $mg->save();
     }
     // check and attach and files as needed
     CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id);
     $transaction->commit();
     /**
      * create parent job if not yet created
      * condition on the existence of a scheduled date
      */
     if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') {
         $job = new CRM_Mailing_BAO_MailingJob();
         $job->mailing_id = $mailing->id;
         $job->status = 'Scheduled';
         $job->is_test = 0;
         if (!$job->find(TRUE)) {
             $job->scheduled_date = $params['scheduled_date'];
             $job->save();
         }
         // Populate the recipients.
         $mailing->getRecipients($job->id, $mailing->id, NULL, NULL, TRUE, FALSE);
     }
     return $mailing;
 }