3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Choose include / exclude groups and mailings
40 class CRM_Mailing_Form_Group
extends CRM_Contact_Form_Task
{
43 * The mailing ID of the mailing if we are resuming a mailing.
47 protected $_mailingID;
50 * Set variables up before form is built.
54 public function preProcess() {
55 if (CRM_Core_BAO_MailSettings
::defaultDomain() == "EXAMPLE.ORG") {
56 CRM_Core_Error
::fatal(ts('The <a href="%1">default mailbox</a> has not been configured. You will find <a href="%2">more info in our online user and administrator guide.</a>', array(
57 1 => CRM_Utils_System
::url('civicrm/admin/mailSettings', 'reset=1'),
58 2 => "http://book.civicrm.org/user/advanced-configuration/email-system-configuration/",
62 $this->_mailingID
= CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
64 // when user come from search context.
65 $this->_searchBasedMailing
= CRM_Contact_Form_Search
::isSearchContext($this->get('context'));
66 if ($this->_searchBasedMailing
) {
67 $searchParams = $this->controller
->exportValues();
68 // number of records that were selected - All or Few.
69 $this->_resultSelectOption
= $searchParams['radio_ts'];
70 if (CRM_Utils_Array
::value('task', $searchParams) == 20) {
75 $session = CRM_Core_Session
::singleton();
76 if ($this->_searchBasedMailing
) {
77 $config = CRM_Core_Config
::singleton();
78 $path = CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_GET);
79 $qfKey = CRM_Utils_Array
::value('qfKey', $_GET);
81 $session->pushUserContext(CRM_Utils_System
::url($path, "qfKey=$qfKey"));
84 $session->pushUserContext(CRM_Utils_System
::url('civicrm/mailing', 'reset=1'));
87 elseif (strpos($session->readUserContext(), 'civicrm/mailing') === FALSE) {
88 // use previous context unless mailing is not schedule, CRM-4290
89 $session->pushUserContext(CRM_Utils_System
::url('civicrm/mailing', 'reset=1'));
94 * Set default values for the form.
95 * the default values are retrieved from the database
100 public function setDefaultValues() {
101 $continue = CRM_Utils_Request
::retrieve('continue', 'String', $this, FALSE, NULL);
104 $defaults['dedupe_email'] = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
105 'dedupe_email_default', NULL, FALSE
107 if ($this->_mailingID
) {
108 // check that the user has permission to access mailing id
109 CRM_Mailing_BAO_Mailing
::checkPermission($this->_mailingID
);
111 $mailing = new CRM_Mailing_DAO_Mailing();
112 $mailing->id
= $this->_mailingID
;
113 $mailing->addSelect('name', 'campaign_id');
114 $mailing->find(TRUE);
116 $defaults['name'] = $mailing->name
;
118 $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name
));
121 // CRM-7590, reuse same mailing ID if we are continuing
122 $this->set('mailing_id', $this->_mailingID
);
125 $defaults['campaign_id'] = $mailing->campaign_id
;
126 $defaults['dedupe_email'] = $mailing->dedupe_email
;
127 $defaults['location_type_id'] = $mailing->location_type_id
;
128 $defaults['email_selection_method'] = $mailing->email_selection_method
;
130 $dao = new CRM_Mailing_DAO_MailingGroup();
132 $mailingGroups = array(
133 'civicrm_group' => array(),
134 'civicrm_mailing' => array(),
136 $dao->mailing_id
= $this->_mailingID
;
138 while ($dao->fetch()) {
139 // account for multi-lingual
141 $entityTable = 'civicrm_group';
142 if (substr($dao->entity_table
, 0, 15) == 'civicrm_mailing') {
143 $entityTable = 'civicrm_mailing';
145 $mailingGroups[$entityTable][$dao->group_type
][] = $dao->entity_id
;
148 $defaults['includeGroups'] = CRM_Utils_Array
::value('Include', $mailingGroups['civicrm_group']);
149 $defaults['excludeGroups'] = CRM_Utils_Array
::value('Exclude', $mailingGroups['civicrm_group']);
151 if (!empty($mailingGroups['civicrm_mailing'])) {
152 $defaults['includeMailings'] = CRM_Utils_Array
::value('Include', $mailingGroups['civicrm_mailing']);
153 $defaults['excludeMailings'] = CRM_Utils_Array
::value('Exclude', $mailingGroups['civicrm_mailing']);
157 //when the context is search hide the mailing recipients.
158 $showHide = new CRM_Core_ShowHideBlocks();
159 $showGroupSelector = TRUE;
160 if ($this->_searchBasedMailing
) {
161 $showGroupSelector = FALSE;
162 $formElements = array('includeGroups', 'excludeGroups', 'includeMailings', 'excludeMailings');
163 $formValues = $this->controller
->exportValues($this->_name
);
164 foreach ($formElements as $element) {
165 if (!empty($formValues[$element])) {
166 $showGroupSelector = TRUE;
172 if ($showGroupSelector) {
173 $showHide->addShow("id-additional");
174 $showHide->addHide("id-additional-show");
177 $showHide->addShow("id-additional-show");
178 $showHide->addHide("id-additional");
180 $showHide->addToTemplate();
186 * Build the form object.
190 public function buildQuickForm() {
193 $context = $this->get('context');
194 if ($this->_searchBasedMailing
) {
197 $this->assign('context', $context);
199 $this->add('text', 'name', ts('Name Your Mailing'),
200 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
204 $hiddenMailingGroup = NULL;
207 //CRM-7362 --add campaigns.
208 if ($this->_mailingID
) {
209 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID
, 'campaign_id');
210 $hiddenMailingGroup = CRM_Mailing_BAO_Mailing
::hiddenMailingGroup($this->_mailingID
);
212 CRM_Campaign_BAO_Campaign
::addCampaign($this, $campaignId);
214 //dedupe on email option
215 $this->addElement('checkbox', 'dedupe_email', ts('Remove duplicate emails?'));
218 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array('id' => 'display_name'));
219 $this->addElement('select', 'location_type_id', ts("Location Type"), array('' => ts('Automatic')) +
$locationTypes);
221 $methods = CRM_Core_SelectValues
::emailSelectMethods();
222 $this->addElement('select', 'email_selection_method', ts("Email Selection Method"), $methods);
224 //get the mailing groups.
225 $groups = CRM_Core_PseudoConstant
::nestedGroup('Mailing');
226 if ($hiddenMailingGroup) {
227 $groups[$hiddenMailingGroup]
228 = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $hiddenMailingGroup, 'title');
231 $mailings = CRM_Mailing_PseudoConstant
::completed();
236 // run the groups through a hook so users can trim it if needed
237 CRM_Utils_Hook
::mailingGroups($this, $groups, $mailings);
239 //when the context is search add base group's.
240 if ($this->_searchBasedMailing
) {
241 //get the static groups
242 $staticGroups = CRM_Core_PseudoConstant
::staticGroup(FALSE, 'Mailing');
243 $this->add('select', 'baseGroup',
244 ts('Unsubscription Group'),
246 '' => ts('- select -'),
247 ) + CRM_Contact_BAO_Group
::getGroupsHierarchy($staticGroups, NULL, ' ', TRUE),
249 array('class' => 'crm-select2 huge')
253 $select2style = array(
255 'style' => 'width: 100%; max-width: 60em;',
256 'class' => 'crm-select2',
257 'placeholder' => ts('- select -'),
260 $this->add('select', 'includeGroups',
261 ts('Include Group(s)'),
263 !$this->_searchBasedMailing
,
267 $this->add('select', 'excludeGroups',
268 ts('Exclude Group(s)'),
274 $this->add('select', 'includeMailings',
275 ts('INCLUDE Recipients of These Mailing(s)') . ' ',
280 $this->add('select', 'excludeMailings',
281 ts('EXCLUDE Recipients of These Mailing(s)') . ' ',
287 $this->addFormRule(array('CRM_Mailing_Form_Group', 'formRule'));
292 'name' => ts('Next'),
293 'spacing' => ' ',
298 'name' => ts('Save & Continue Later'),
302 'name' => ts('Cancel'),
306 $this->addButtons($buttons);
308 $this->assign('groupCount', count($groups));
309 $this->assign('mailingCount', count($mailings));
310 if (count($groups) == 0 && count($mailings) == 0 && !$this->_searchBasedMailing
) {
311 CRM_Core_Error
::statusBounce("To send a mailing, you must have a valid group of recipients - either at least one group that's a Mailing List or at least one previous mailing or start from a search");
315 public function postProcess() {
316 $values = $this->controller
->exportValues($this->_name
);
318 //build hidden smart group. when user want to send mailing
319 //through search contact-> more action -> send Mailing. CRM-3711
321 if ($this->_searchBasedMailing
&& $this->_contactIds
) {
322 $session = CRM_Core_Session
::singleton();
324 if ($this->_resultSelectOption
== 'ts_sel') {
325 // create a static grp if only a subset of result set was selected:
327 $randID = md5(time());
328 $grpTitle = "Hidden Group {$randID}";
329 $grpID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title');
332 $groupParams = array(
333 'title' => $grpTitle,
336 'group_type' => array('2' => 1),
339 $group = CRM_Contact_BAO_Group
::create($groupParams);
342 CRM_Contact_BAO_GroupContact
::addContactsToGroup($this->_contactIds
, $group->id
);
344 $newGroupTitle = "Hidden Group {$grpID}";
345 $groupParams = array(
347 'name' => CRM_Utils_String
::titleToVar($newGroupTitle),
348 'title' => $newGroupTitle,
349 'group_type' => array('2' => 1),
351 $group = CRM_Contact_BAO_Group
::create($groupParams);
354 // note at this point its a static group
355 $smartGroupId = $grpID;
358 //get the hidden smart group id.
359 $ssId = $this->get('ssID');
360 $hiddenSmartParams = array(
361 'group_type' => array('2' => 1),
362 'form_values' => $this->get('formValues'),
363 'saved_search_id' => $ssId,
364 'search_custom_id' => $this->get('customSearchID'),
365 'search_context' => $this->get('context'),
368 list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group
::createHiddenSmartGroup($hiddenSmartParams);
370 //set the saved search id.
372 if ($savedSearchId) {
373 $this->set('ssID', $savedSearchId);
376 CRM_Core_Error
::fatal();
381 //get the base group for this mailing, CRM-3711
382 $groups['base'] = array($values['baseGroup']);
383 $values['includeGroups'][] = $smartGroupId;
395 'email_selection_method',
398 if (!empty($values[$n])) {
399 $params[$n] = $values[$n];
403 $qf_Group_submit = $this->controller
->exportValue($this->_name
, '_qf_Group_submit');
404 $this->set('name', $params['name']);
406 $inGroups = $values['includeGroups'];
407 $outGroups = $values['excludeGroups'];
408 $inMailings = $values['includeMailings'];
409 $outMailings = $values['excludeMailings'];
411 if (is_array($inGroups)) {
412 foreach ($inGroups as $key => $id) {
414 $groups['include'][] = $id;
418 if (is_array($outGroups)) {
419 foreach ($outGroups as $key => $id) {
421 $groups['exclude'][] = $id;
427 if (is_array($inMailings)) {
428 foreach ($inMailings as $key => $id) {
430 $mailings['include'][] = $id;
434 if (is_array($outMailings)) {
435 foreach ($outMailings as $key => $id) {
437 $mailings['exclude'][] = $id;
442 $session = CRM_Core_Session
::singleton();
443 $params['groups'] = $groups;
444 $params['mailings'] = $mailings;
446 if ($this->get('mailing_id')) {
448 // don't create a new mailing if already exists
449 $ids['mailing_id'] = $this->get('mailing_id');
451 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
452 $mailingTableName = CRM_Mailing_BAO_Mailing
::getTableName();
454 // delete previous includes/excludes, if mailing already existed
455 foreach (array('groups', 'mailings') as $entity) {
456 $mg = new CRM_Mailing_DAO_MailingGroup();
457 $mg->mailing_id
= $ids['mailing_id'];
458 $mg->entity_table
= ($entity == 'groups') ?
$groupTableName : $mailingTableName;
460 while ($mg->fetch()) {
466 // new mailing, so lets set the created_id
467 $session = CRM_Core_Session
::singleton();
468 $params['created_id'] = $session->get('userID');
469 $params['created_date'] = date('YmdHis');
471 $mailing = CRM_Mailing_BAO_Mailing
::create($params, $ids);
472 $this->set('mailing_id', $mailing->id
);
474 $dedupeEmail = FALSE;
475 if (isset($params['dedupe_email'])) {
476 $dedupeEmail = $params['dedupe_email'];
479 // mailing id should be added to the form object
480 $this->_mailingID
= $mailing->id
;
482 // also compute the recipients and store them in the mailing recipients table
483 CRM_Mailing_BAO_Mailing
::getRecipients(
492 $count = CRM_Mailing_BAO_Recipients
::mailingSize($mailing->id
);
493 $this->set('count', $count);
494 $this->assign('count', $count);
495 $this->set('groups', $groups);
496 $this->set('mailings', $mailings);
498 if ($qf_Group_submit) {
499 //when user perform mailing from search context
500 //redirect it to search result CRM-3711.
501 $ssID = $this->get('ssID');
502 $context = $this->get('context');
503 if ($ssID && $this->_searchBasedMailing
) {
504 if ($this->_action
== CRM_Core_Action
::BASIC
) {
505 $fragment = 'search';
507 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
508 $fragment = 'search/builder';
510 elseif ($this->_action
== CRM_Core_Action
::ADVANCED
) {
511 $fragment = 'search/advanced';
514 $fragment = 'search/custom';
517 $context = $this->get('context');
518 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
521 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
523 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
524 if (CRM_Utils_Rule
::qfKey($qfKey)) {
525 $urlParams .= "&qfKey=$qfKey";
528 $draftURL = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
529 $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));
531 // Redirect user to search.
532 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
535 $status = ts("Click the 'Continue' action to resume working on it.");
536 $url = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
538 CRM_Core_Session
::setStatus($status, ts('Mailing Saved'), 'success');
539 return $this->controller
->setDestination($url);
544 * Display Name of the form.
549 public function getTitle() {
550 return ts('Select Recipients');
554 * Global validation rules for the form.
556 * @param array $fields
557 * Posted values of the form.
560 * list of errors to be posted back to the form
562 public static function formRule($fields) {
564 if (isset($fields['includeGroups']) &&
565 is_array($fields['includeGroups']) &&
566 isset($fields['excludeGroups']) &&
567 is_array($fields['excludeGroups'])
569 $checkGroups = array();
570 $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
571 if (!empty($checkGroups)) {
572 $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
576 if (isset($fields['includeMailings']) &&
577 is_array($fields['includeMailings']) &&
578 isset($fields['excludeMailings']) &&
579 is_array($fields['excludeMailings'])
581 $checkMailings = array();
582 $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
583 if (!empty($checkMailings)) {
584 $errors['excludeMailings'] = ts('Cannot have same mail in Include mailing(s) and Exclude mailing(s).');
588 if (!empty($fields['search_id']) &&
589 empty($fields['group_id'])
591 $errors['group_id'] = ts('You must select a group to filter on');
594 if (empty($fields['search_id']) &&
595 !empty($fields['group_id'])
597 $errors['search_id'] = ts('You must select a search to filter');
600 if (!empty($fields['location_type_id'])) {
601 if ($fields['email_selection_method'] == 'automatic') {
602 $errors['location_type_id'] = ts("If 'Email Selection Method' is automatic, you are not allowed to choose any 'Location Type'");
605 elseif ($fields['email_selection_method'] != 'automatic') {
606 $errors['email_selection_method'] = ts("If 'Location Type' is not selected, you must set the 'Email Selection Method' to automatic as well.");
609 return empty($errors) ?
TRUE : $errors;