/** * 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; }
private static function getMailingJobs($params) { if (is_array(static::$jobs)) { return static::$jobs; } static::$jobs = array(); if (empty($params['crm_mailing_id'])) { return static::$jobs; } $jobBao = new CRM_Mailing_BAO_MailingJob(); // This needs to be done as there can only be one query per DAO (see docs for reset()) - without this, the DAO // object was keeping residual data from previous operations elsewhere on the same DAO, making the below operations // unpredictable and fail $jobBao->reset(); // This is needed as the reset() above clears the query completely $jobBao->selectAdd('*'); $jobBao->mailing_id = $params['crm_mailing_id']; $jobBao->is_test = 0; if ($jobBao->find()) { while ($jobBao->fetch()) { static::$jobs[] = $jobBao->toArray(); } } $jobBao->free(); return static::$jobs; }
function postProcess() { $params = array(); $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID; if (empty($params['mailing_id'])) { CRM_Core_Error::fatal(ts('Could not find a mailing id')); } foreach (array('now', 'start_date', 'start_date_time') as $parameter) { $params[$parameter] = $this->controller->exportValue($this->_name, $parameter); } $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->id = $ids['mailing_id']; if ($mailing->find(TRUE)) { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; $job->is_test = 0; if ($job->find(TRUE)) { CRM_Core_Error::fatal(ts('A job for this mailing already exists')); } if (empty($mailing->is_template)) { $job->status = 'Scheduled'; if ($params['now']) { $job->scheduled_date = date('YmdHis'); } else { $job->scheduled_date = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']); } $job->save(); } // set approval details if workflow is not enabled if (!CRM_Mailing_Info::workflowEnabled()) { $session = CRM_Core_Session::singleton(); $mailing->approver_id = $session->get('userID'); $mailing->approval_date = date('YmdHis'); $mailing->approval_status_id = 1; } else { // reset them in case this mailing was rejected $mailing->approver_id = 'null'; $mailing->approval_date = 'null'; $mailing->approval_status_id = 'null'; } if ($mailing->approval_date) { $mailing->approval_date = CRM_Utils_Date::isoToMysql($mailing->approval_date); } // also set the scheduled_id $session = CRM_Core_Session::singleton(); $mailing->scheduled_id = $session->get('userID'); $mailing->scheduled_date = date('YmdHis'); $mailing->created_date = CRM_Utils_Date::isoToMysql($mailing->created_date); $mailing->save(); } $status = ts("Your mailing has been saved."); CRM_Core_Session::setStatus($status); $url = CRM_Utils_System::url('civicrm/view/quickbulkemail'); return $this->controller->setDestination($url); }