Merge pull request #6553 from sudhabisht/C12658
[civicrm-core.git] / CRM / Mailing / Form / Group.php
index 0eabfee791fd2ed0633e225d1ce822cfd35a7886..dcca8afc628cb1b941050c7f877170465f823089 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
 
   /**
-   * the mailing ID of the mailing if we are resuming a mailing
+   * The mailing ID of the mailing if we are resuming a mailing.
    *
    * @var integer
    */
   protected $_mailingID;
 
   /**
-   * Function to set variables up before form is built
+   * Set variables up before form is built.
    *
    * @return void
-   * @access public
    */
   public function preProcess() {
     if (CRM_Core_BAO_MailSettings::defaultDomain() == "EXAMPLE.ORG") {
-      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(1 => CRM_Utils_System::url('civicrm/admin/mailSettings', 'reset=1'), 2 => "http://book.civicrm.org/user/advanced-configuration/email-system-configuration/")));
+      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(
+            1 => CRM_Utils_System::url('civicrm/admin/mailSettings', 'reset=1'),
+            2 => "http://book.civicrm.org/user/advanced-configuration/email-system-configuration/",
+          )));
     }
 
     $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
@@ -89,21 +91,19 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
   }
 
   /**
-   * This function sets the default values for the form.
+   * Set default values for the form.
    * the default values are retrieved from the database
    *
-   * @access public
    *
    * @return void
    */
-  function setDefaultValues() {
+  public function setDefaultValues() {
     $continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL);
 
     $defaults = array();
     $defaults['dedupe_email'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
       'dedupe_email_default', NULL, FALSE
     );
-
     if ($this->_mailingID) {
       // check that the user has permission to access mailing id
       CRM_Mailing_BAO_Mailing::checkPermission($this->_mailingID);
@@ -124,12 +124,14 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
 
       $defaults['campaign_id'] = $mailing->campaign_id;
       $defaults['dedupe_email'] = $mailing->dedupe_email;
+      $defaults['location_type_id'] = $mailing->location_type_id;
+      $defaults['email_selection_method'] = $mailing->email_selection_method;
 
       $dao = new CRM_Mailing_DAO_MailingGroup();
 
       $mailingGroups = array(
-        'civicrm_group' => array( ),
-        'civicrm_mailing' => array( )
+        'civicrm_group' => array(),
+        'civicrm_mailing' => array(),
       );
       $dao->mailing_id = $this->_mailingID;
       $dao->find();
@@ -157,8 +159,8 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
     $showGroupSelector = TRUE;
     if ($this->_searchBasedMailing) {
       $showGroupSelector = FALSE;
-      $formElements      = array('includeGroups', 'excludeGroups', 'includeMailings', 'excludeMailings');
-      $formValues        = $this->controller->exportValues($this->_name);
+      $formElements = array('includeGroups', 'excludeGroups', 'includeMailings', 'excludeMailings');
+      $formValues = $this->controller->exportValues($this->_name);
       foreach ($formElements as $element) {
         if (!empty($formValues[$element])) {
           $showGroupSelector = TRUE;
@@ -181,10 +183,9 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Function to actually build the form
+   * Build the form object.
    *
    * @return void
-   * @access public
    */
   public function buildQuickForm() {
 
@@ -213,11 +214,18 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
     //dedupe on email option
     $this->addElement('checkbox', 'dedupe_email', ts('Remove duplicate emails?'));
 
+    // location types
+    $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('id' => 'display_name'));
+    $this->addElement('select', 'location_type_id', ts("Location Type"), array('' => ts('Automatic')) + $locationTypes);
+
+    $methods = CRM_Core_SelectValues::emailSelectMethods();
+    $this->addElement('select', 'email_selection_method', ts("Email Selection Method"), $methods);
+
     //get the mailing groups.
     $groups = CRM_Core_PseudoConstant::nestedGroup('Mailing');
     if ($hiddenMailingGroup) {
-      $groups[$hiddenMailingGroup] =
-        CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $hiddenMailingGroup, 'title');
+      $groups[$hiddenMailingGroup]
+        CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $hiddenMailingGroup, 'title');
     }
 
     $mailings = CRM_Mailing_PseudoConstant::completed();
@@ -230,12 +238,13 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
 
     //when the context is search add base group's.
     if ($this->_searchBasedMailing) {
-      //get the static groups
-      $staticGroups = CRM_Core_PseudoConstant::staticGroup(FALSE, 'Mailing');
+      //CRM-16600 Include Smart Groups in Unsubscribe list as that matches
+      //all other practices in CiviMail
       $this->add('select', 'baseGroup',
         ts('Unsubscription Group'),
         array(
-          '' => ts('- select -')) + CRM_Contact_BAO_Group::getGroupsHierarchy($staticGroups, NULL, '&nbsp;&nbsp;', TRUE),
+          '' => ts('- select -'),
+        ) + $groups,
         TRUE,
         array('class' => 'crm-select2 huge')
       );
@@ -278,8 +287,9 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
     $this->addFormRule(array('CRM_Mailing_Form_Group', 'formRule'));
 
     $buttons = array(
-      array('type' => 'next',
-        'name' => ts('Next >>'),
+      array(
+        'type' => 'next',
+        'name' => ts('Next'),
         'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
         'isDefault' => TRUE,
       ),
@@ -297,7 +307,7 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
 
     $this->assign('groupCount', count($groups));
     $this->assign('mailingCount', count($mailings));
-    if(count($groups) == 0 && count($mailings) == 0 && !$this->_searchBasedMailing) {
+    if (count($groups) == 0 && count($mailings) == 0 && !$this->_searchBasedMailing) {
       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");
     }
   }
@@ -314,9 +324,9 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
       if ($this->_resultSelectOption == 'ts_sel') {
         // create a static grp if only a subset of result set was selected:
 
-        $randID   = md5(time());
+        $randID = md5(time());
         $grpTitle = "Hidden Group {$randID}";
-        $grpID    = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title');
+        $grpID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title');
 
         if (!$grpID) {
           $groupParams = array(
@@ -333,8 +343,8 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
 
           $newGroupTitle = "Hidden Group {$grpID}";
           $groupParams = array(
-            'id'    => $grpID,
-            'name'  => CRM_Utils_String::titleToVar($newGroupTitle),
+            'id' => $grpID,
+            'name' => CRM_Utils_String::titleToVar($newGroupTitle),
             'title' => $newGroupTitle,
             'group_type' => array('2' => 1),
           );
@@ -347,7 +357,8 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
       else {
         //get the hidden smart group id.
         $ssId = $this->get('ssID');
-        $hiddenSmartParams = array('group_type' => array('2' => 1),
+        $hiddenSmartParams = array(
+          'group_type' => array('2' => 1),
           'form_values' => $this->get('formValues'),
           'saved_search_id' => $ssId,
           'search_custom_id' => $this->get('customSearchID'),
@@ -373,20 +384,28 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
     }
 
     foreach (
-      array('name', 'group_id', 'search_id', 'search_args', 'campaign_id', 'dedupe_email') as $n
+      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'];
+    $inGroups = $values['includeGroups'];
+    $outGroups = $values['excludeGroups'];
+    $inMailings = $values['includeMailings'];
     $outMailings = $values['excludeMailings'];
 
     if (is_array($inGroups)) {
@@ -420,8 +439,8 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
       }
     }
 
-    $session            = CRM_Core_Session::singleton();
-    $params['groups']   = $groups;
+    $session = CRM_Core_Session::singleton();
+    $params['groups'] = $groups;
     $params['mailings'] = $mailings;
     $ids = array();
     if ($this->get('mailing_id')) {
@@ -434,8 +453,8 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
 
       // 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 = new CRM_Mailing_DAO_MailingGroup();
+        $mg->mailing_id = $ids['mailing_id'];
         $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName;
         $mg->find();
         while ($mg->fetch()) {
@@ -449,7 +468,6 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
       $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);
 
@@ -465,8 +483,6 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
     CRM_Mailing_BAO_Mailing::getRecipients(
       $mailing->id,
       $mailing->id,
-      NULL,
-      NULL,
       TRUE,
       $dedupeEmail
     );
@@ -523,9 +539,8 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Display Name of the form
+   * Display Name of the form.
    *
-   * @access public
    *
    * @return string
    */
@@ -534,15 +549,15 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
   }
 
   /**
-   * global validation rules for the form
+   * Global validation rules for the form.
    *
-   * @param array $fields posted values of the form
+   * @param array $fields
+   *   Posted values of the form.
    *
-   * @return array list of errors to be posted back to the form
-   * @static
-   * @access public
+   * @return array
+   *   list of errors to be posted back to the form
    */
-  static function formRule($fields) {
+  public static function formRule($fields) {
     $errors = array();
     if (isset($fields['includeGroups']) &&
       is_array($fields['includeGroups']) &&
@@ -580,7 +595,16 @@ class CRM_Mailing_Form_Group extends CRM_Contact_Form_Task {
       $errors['search_id'] = ts('You must select a search to filter');
     }
 
+    if (!empty($fields['location_type_id'])) {
+      if ($fields['email_selection_method'] == 'automatic') {
+        $errors['location_type_id'] = ts("If 'Email Selection Method' is automatic, you are not allowed to choose any 'Location Type'");
+      }
+    }
+    elseif ($fields['email_selection_method'] != 'automatic') {
+      $errors['email_selection_method'] = ts("If 'Location Type' is not selected, you must set the 'Email Selection Method' to automatic as well.");
+    }
+
     return empty($errors) ? TRUE : $errors;
   }
-}
 
+}