Ejemplo n.º 1
0
 public function postProcess()
 {
     $values = $this->controller->exportValues($this->_name);
     $groups = array();
     foreach (array('name', 'group_id', 'is_sms') as $n) {
         if (!empty($values[$n])) {
             $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;
     $ids = array();
     if ($this->get('mailing_id')) {
         // don't create a new mass sms 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
         foreach (array('groups', 'mailings') as $entity) {
             $mg = new CRM_Mailing_DAO_MailingGroup();
             $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');
     }
     $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
     $this->set('mailing_id', $mailing->id);
     // also compute the recipients and store them in the mailing recipients table
     CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, $mailing->id, TRUE, FALSE, 'sms');
     $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id);
     $this->set('count', $count);
     $this->assign('count', $count);
     $this->set('groups', $groups);
     $this->set('mailings', $mailings);
     if ($qf_Group_submit) {
         $status = ts("Your Mass SMS has been saved.");
         CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
         $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1');
         return $this->controller->setDestination($url);
     }
 }
Ejemplo n.º 2
0
function civicrm_api3_mailing_clone($params)
{
    $BLACKLIST = array('id', 'is_completed', 'created_id', 'created_date', 'scheduled_id', 'scheduled_date', 'approver_id', 'approval_date', 'approval_status_id', 'approval_note', 'is_archived', 'hash');
    $get = civicrm_api3('Mailing', 'getsingle', array('id' => $params['id']));
    $newParams = array();
    $newParams['debug'] = CRM_Utils_Array::value('debug', $params);
    $newParams['groups']['include'] = array();
    $newParams['groups']['exclude'] = array();
    $newParams['mailings']['include'] = array();
    $newParams['mailings']['exclude'] = array();
    foreach ($get as $field => $value) {
        if (!in_array($field, $BLACKLIST)) {
            $newParams[$field] = $value;
        }
    }
    $dao = new CRM_Mailing_DAO_MailingGroup();
    $dao->mailing_id = $params['id'];
    $dao->find();
    while ($dao->fetch()) {
        // CRM-11431; account for multi-lingual
        $entity = substr($dao->entity_table, 0, 15) == 'civicrm_mailing' ? 'mailings' : 'groups';
        $newParams[$entity][strtolower($dao->group_type)][] = $dao->entity_id;
    }
    return civicrm_api3('Mailing', 'create', $newParams);
}
 /**
  * This function sets the default values for the form.
  * the default values are retrieved from the database
  *
  * @access public
  *
  * @return None
  */
 function setDefaultValues()
 {
     // to continue the unscheduled or draft mailing
     $continue = $this->_continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL);
     $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
     $defaults = array();
     if ($this->_mailingID) {
         // check that the user has permission to access mailing id
         CRM_Mailing_BAO_Mailing::checkPermission($this->_mailingID);
         $mailing = new CRM_Mailing_DAO_Mailing();
         $mailing->id = $this->_mailingID;
         $mailing->addSelect('name', 'campaign_id');
         $mailing->find(TRUE);
         $defaults['name'] = $mailing->name;
         if (!$continue) {
             $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name));
         } else {
             // CRM-7590, reuse same mailing ID if we are continuing
             $this->set('mailing_id', $this->_mailingID);
         }
         $defaults['campaign_id'] = $mailing->campaign_id;
         $defaults['dedupe_email'] = $mailing->dedupe_email;
         $dao = new CRM_Mailing_DAO_MailingGroup();
         $mailingGroups = array('civicrm_group' => array(), 'civicrm_mailing' => array());
         $dao->mailing_id = $this->_mailingID;
         $dao->find();
         while ($dao->fetch()) {
             // account for multi-lingual
             // CRM-11431
             $entityTable = 'civicrm_group';
             if (substr($dao->entity_table, 0, 15) == 'civicrm_mailing') {
                 $entityTable = 'civicrm_mailing';
             }
             $mailingGroups[$entityTable][$dao->group_type][] = $dao->entity_id;
         }
         $defaults['includeGroups'] = $mailingGroups['civicrm_group']['include'];
         $defaults['excludeGroups'] = CRM_Utils_Array::value('exclude', $mailingGroups['civicrm_group']);
         if (!empty($mailingGroups['civicrm_mailing'])) {
             $defaults['includeMailings'] = CRM_Utils_Array::value('include', $mailingGroups['civicrm_mailing']);
             $defaults['excludeMailings'] = CRM_Utils_Array::value('exclude', $mailingGroups['civicrm_mailing']);
         }
     } else {
         $defaults['url_tracking'] = TRUE;
         $defaults['open_tracking'] = TRUE;
     }
     //set default message body
     $reuseMailing = FALSE;
     if ($mailingID) {
         $reuseMailing = TRUE;
     } else {
         $mailingID = $this->_mailingID;
     }
     $count = $this->get('count');
     $this->assign('count', $count);
     $this->set('skipTextFile', FALSE);
     $this->set('skipHtmlFile', FALSE);
     $htmlMessage = NULL;
     if ($mailingID) {
         $dao = new CRM_Mailing_DAO_Mailing();
         $dao->id = $mailingID;
         $dao->find(TRUE);
         $dao->storeValues($dao, $defaults);
         //we don't want to retrieve template details once it is
         //set in session
         $templateId = $this->get('template');
         $this->assign('templateSelected', $templateId ? $templateId : 0);
         if (isset($defaults['msg_template_id']) && !$templateId) {
             $defaults['template'] = $defaults['msg_template_id'];
             $messageTemplate = new CRM_Core_DAO_MessageTemplate();
             $messageTemplate->id = $defaults['msg_template_id'];
             $messageTemplate->selectAdd();
             $messageTemplate->selectAdd('msg_text, msg_html');
             $messageTemplate->find(TRUE);
             $defaults['text_message'] = $messageTemplate->msg_text;
             $htmlMessage = $messageTemplate->msg_html;
         }
         if (isset($defaults['body_text'])) {
             $defaults['text_message'] = $defaults['body_text'];
             $this->set('textFile', $defaults['body_text']);
             $this->set('skipTextFile', TRUE);
         }
         if (isset($defaults['body_html'])) {
             $htmlMessage = $defaults['body_html'];
             $this->set('htmlFile', $defaults['body_html']);
             $this->set('skipHtmlFile', TRUE);
         }
         //set default from email address.
         if (CRM_Utils_Array::value('from_name', $defaults) && CRM_Utils_Array::value('from_email', $defaults)) {
             $defaults['from_email_address'] = array_search('"' . $defaults['from_name'] . '" <' . $defaults['from_email'] . '>', CRM_Core_OptionGroup::values('from_email_address'));
         } else {
             //get the default from email address.
             $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
             foreach ($defaultAddress as $id => $value) {
                 $defaults['from_email_address'] = $id;
             }
         }
         if (CRM_Utils_Array::value('replyto_email', $defaults)) {
             $replyToEmail = CRM_Core_OptionGroup::values('from_email_address');
             foreach ($replyToEmail as $value) {
                 if (strstr($value, $defaults['replyto_email'])) {
                     $replyToEmailAddress = $value;
                     break;
                 }
             }
             $replyToEmailAddress = explode('<', $replyToEmailAddress);
             $replyToEmailAddress = $replyToEmailAddress[0] . '<' . $replyToEmailAddress[1];
             $this->replytoAddress = $defaults['reply_to_address'] = array_search($replyToEmailAddress, $replyToEmail);
         }
     }
     /*
     //set default from email address.
     if (CRM_Utils_Array::value('from_name', $defaults) && CRM_Utils_Array::value('from_email', $defaults)) {
       $defaults['from_email_address'] = array_search('"' . $defaults['from_name'] . '" <' . $defaults['from_email'] . '>',
         CRM_Core_OptionGroup::values('from_email_address')
       );
     }
     else {
       //get the default from email address.
       $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
       foreach ($defaultAddress as $id => $value) {
         $defaults['from_email_address'] = $id;
       }
     }
     
     if (CRM_Utils_Array::value('replyto_email', $defaults)) {
       $replyToEmail = CRM_Core_OptionGroup::values('from_email_address');
       foreach ($replyToEmail as $value) {
         if (strstr($value, $defaults['replyto_email'])) {
           $replyToEmailAddress = $value;
           break;
         }
       }
       $replyToEmailAddress = explode('<', $replyToEmailAddress);
       $replyToEmailAddress = $replyToEmailAddress[0] . '<' . $replyToEmailAddress[1];
       $this->replytoAddress = $defaults['reply_to_address'] = array_search($replyToEmailAddress, $replyToEmail);
     }
     */
     //fix for CRM-2873
     if (!$reuseMailing) {
         $textFilePath = $this->get('textFilePath');
         if ($textFilePath && file_exists($textFilePath)) {
             $defaults['text_message'] = file_get_contents($textFilePath);
             if (strlen($defaults['text_message']) > 0) {
                 $this->set('skipTextFile', TRUE);
             }
         }
         $htmlFilePath = $this->get('htmlFilePath');
         if ($htmlFilePath && file_exists($htmlFilePath)) {
             $defaults['html_message'] = file_get_contents($htmlFilePath);
             if (strlen($defaults['html_message']) > 0) {
                 $htmlMessage = $defaults['html_message'];
                 $this->set('skipHtmlFile', TRUE);
             }
         }
     }
     if ($this->get('html_message')) {
         $htmlMessage = $this->get('html_message');
     }
     $htmlMessage = str_replace(array("\n", "\r"), ' ', $htmlMessage);
     $htmlMessage = str_replace("'", "\\'", $htmlMessage);
     $this->assign('message_html', $htmlMessage);
     $defaults['upload_type'] = 1;
     if (isset($defaults['body_html'])) {
         $defaults['html_message'] = $defaults['body_html'];
     }
     if (!empty($defaults['html_message'])) {
         $this->assign('reuse_message_template', $defaults['html_message']);
     }
     //CRM-4678 setdefault to default component when composing new mailing.
     if (!$reuseMailing) {
         $componentFields = array('header_id' => 'Header', 'footer_id' => 'Footer');
         foreach ($componentFields as $componentVar => $componentType) {
             $defaults[$componentVar] = CRM_Mailing_PseudoConstant::defaultComponent($componentType, '');
         }
     }
     //end
     return $defaults;
 }
 /**
  * Set or update the category of the mailing.
  *
  * A mailing category is simply a mailing group of type 'Mailing Category', in addition to being of type 'Mailing
  * List'.
  *
  * A mailing can only ever be associated with one category (i.e. mailing group of type 'Mailing Category')
  *
  * @param $params
  *
  * @return $this
  */
 protected static function setMailingCategory($params)
 {
     if (!empty($params['category_id'])) {
         $group = new CRM_Mailing_DAO_MailingGroup();
         $group->reset();
         $group->selectAdd('*');
         $group->mailing_id = $params['crm_mailing_id'];
         $group->group_type = static::MAILING_GROUP_TYPE_BASE;
         $group->entity_table = 'civicrm_group';
         if ($group->find(TRUE)) {
             $group->entity_id = $params['category_id'];
             return $group->save();
         }
         $group->entity_id = $params['category_id'];
         return $group->save();
     }
 }
Ejemplo n.º 5
0
 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) {
         $session = CRM_Core_Session::singleton();
         if ($this->_resultSelectOption == 'ts_sel') {
             // create a static grp if only a subset of result set was selected:
             $randID = md5(time());
             $grpTitle = "Hidden Group {$randID}";
             $grpID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title');
             if (!$grpID) {
                 $groupParams = array('title' => $grpTitle, 'is_active' => 1, 'is_hidden' => 1, 'group_type' => array('2' => 1));
                 $group = CRM_Contact_BAO_Group::create($groupParams);
                 $grpID = $group->id;
                 CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $group->id);
                 $newGroupTitle = "Hidden Group {$grpID}";
                 $groupParams = array('id' => $grpID, 'name' => CRM_Utils_String::titleToVar($newGroupTitle), 'title' => $newGroupTitle, 'group_type' => array('2' => 1));
                 $group = CRM_Contact_BAO_Group::create($groupParams);
             }
             // note at this point its a static group
             $smartGroupId = $grpID;
         } else {
             //get the hidden smart group id.
             $ssId = $this->get('ssID');
             $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'));
             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', 'campaign_id', 'dedupe_email', 'location_type_id', 'email_selection_method') as $n) {
         if (!empty($values[$n])) {
             $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;
     $ids = array();
     if ($this->get('mailing_id')) {
         // 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
         foreach (array('groups', 'mailings') as $entity) {
             $mg = new CRM_Mailing_DAO_MailingGroup();
             $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');
     }
     $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
     $this->set('mailing_id', $mailing->id);
     $dedupeEmail = FALSE;
     if (isset($params['dedupe_email'])) {
         $dedupeEmail = $params['dedupe_email'];
     }
     // mailing id should be added to the form object
     $this->_mailingID = $mailing->id;
     // also compute the recipients and store them in the mailing recipients table
     CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, $mailing->id, NULL, NULL, TRUE, $dedupeEmail);
     $count = CRM_Mailing_BAO_Recipients::mailingSize($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';
             } elseif ($this->_action == CRM_Core_Action::PROFILE) {
                 $fragment = 'search/builder';
             } elseif ($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("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));
             // Redirect user to search.
             $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
         } else {
             $status = ts("Click the 'Continue' action to resume working on it.");
             $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
         }
         CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
         return $this->controller->setDestination($url);
     }
 }