/** * This function sets the default values for the form. * * @access public * @return None */ function setDefaultValues() { $defaults = array(); if ($this->_scheduleFormOnly) { $count = CRM_Mailing_BAO_Mailing::getRecipientsCount(true, false, $this->_mailingID); } else { $count = $this->get('count'); } $this->assign('count', $count); $defaults['now'] = 1; return $defaults; }
public function postProcess() { $values = $this->controller->exportValues($this->_name); //build hidden smart group. when user want to send mailing //through search contact-> more action -> send Mailing. CRM-3711 $groups = array(); if ($this->_searchBasedMailing && $this->_contactIds) { //get the hidden smart group id. $ssId = $this->get('ssID'); $session = CRM_Core_Session::singleton(); $hiddenSmartParams = array('group_type' => array('2' => 1), 'form_values' => $this->get('formValues'), 'saved_search_id' => $ssId, 'search_custom_id' => $this->get('customSearchID'), 'search_context' => $this->get('context')); require_once 'CRM/Contact/BAO/Group.php'; list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams); //set the saved search id. if (!$ssId) { if ($savedSearchId) { $this->set('ssID', $savedSearchId); } else { CRM_Core_Error::fatal(); } } //get the base group for this mailing, CRM-3711 $groups['base'] = array($values['baseGroup']); $values['includeGroups'][] = $smartGroupId; } foreach (array('name', 'group_id', 'search_id', 'search_args') as $n) { if (CRM_Utils_Array::value($n, $values)) { $params[$n] = $values[$n]; } } $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit'); $this->set('name', $params['name']); $inGroups = $values['includeGroups']; $outGroups = $values['excludeGroups']; $inMailings = $values['includeMailings']; $outMailings = $values['excludeMailings']; if (is_array($inGroups)) { foreach ($inGroups as $key => $id) { if ($id) { $groups['include'][] = $id; } } } if (is_array($outGroups)) { foreach ($outGroups as $key => $id) { if ($id) { $groups['exclude'][] = $id; } } } $mailings = array(); if (is_array($inMailings)) { foreach ($inMailings as $key => $id) { if ($id) { $mailings['include'][] = $id; } } } if (is_array($outMailings)) { foreach ($outMailings as $key => $id) { if ($id) { $mailings['exclude'][] = $id; } } } $session = CRM_Core_Session::singleton(); $params['groups'] = $groups; $params['mailings'] = $mailings; if ($this->get('mailing_id')) { $ids = array(); // don't create a new mailing if already exists $ids['mailing_id'] = $this->get('mailing_id'); $groupTableName = CRM_Contact_BAO_Group::getTableName(); $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); // delete previous includes/excludes, if mailing already existed require_once 'CRM/Contact/DAO/Group.php'; foreach (array('groups', 'mailings') as $entity) { $mg = new CRM_Mailing_DAO_Group(); $mg->mailing_id = $ids['mailing_id']; $mg->entity_table = $entity == 'groups' ? $groupTableName : $mailingTableName; $mg->find(); while ($mg->fetch()) { $mg->delete(); } } } else { // new mailing, so lets set the created_id $session = CRM_Core_Session::singleton(); $params['created_id'] = $session->get('userID'); $params['created_date'] = date('YmdHis'); } require_once 'CRM/Mailing/BAO/Mailing.php'; $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); $this->set('mailing_id', $mailing->id); $count = CRM_Mailing_BAO_Mailing::getRecipientsCount(true, false, $mailing->id); $this->set('count', $count); $this->assign('count', $count); $this->set('groups', $groups); $this->set('mailings', $mailings); if ($qf_Group_submit) { //when user perform mailing from search context //redirect it to search result CRM-3711. $ssID = $this->get('ssID'); $context = $this->get('context'); if ($ssID && $this->_searchBasedMailing) { if ($this->_action == CRM_Core_Action::BASIC) { $fragment = 'search'; } else { if ($this->_action == CRM_Core_Action::PROFILE) { $fragment = 'search/builder'; } else { if ($this->_action == CRM_Core_Action::ADVANCED) { $fragment = 'search/advanced'; } else { $fragment = 'search/custom'; } } } $context = $this->get('context'); if (!CRM_Contact_Form_Search::isSearchContext($context)) { $context = 'search'; } $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}"; $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); if (CRM_Utils_Rule::qfKey($qfKey)) { $urlParams .= "&qfKey={$qfKey}"; } $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); $status = ts("Your mailing has been saved. You can continue later by clicking the 'Continue' action to resume working on it.<br /> From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL)); CRM_Core_Session::setStatus($status); //replace user context to search. $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); CRM_Utils_System::redirect($url); } else { $status = ts("Your mailing has been saved. Click the 'Continue' action to resume working on it."); CRM_Core_Session::setStatus($status); $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); CRM_Utils_System::redirect($url); } } }