public function preProcess()
 {
     parent::preProcess();
     // TODO: These would probably not be needed if the logic in \CRM_Simplemail_BAO_SimpleMail::createSmartContactGroupForSearchContacts() is moved into this class' postProcess() method
     simplemail_civicrm_addToSessionScope('createdFromSearch', TRUE);
     simplemail_civicrm_addToSessionScope('contactCountFromSearch', count($this->_contactIds));
     simplemail_civicrm_addToSessionScope('contactIds', $this->_contactIds);
     simplemail_civicrm_addToSessionScope('searchParams', $this->controller->exportValues());
     simplemail_civicrm_addToSessionScope('formValues', $this->get('formValues'));
     simplemail_civicrm_addToSessionScope('customSearchId', $this->get('customSearchID'));
     simplemail_civicrm_addToSessionScope('context', $this->get('context'));
     CRM_Utils_System::redirect('/civicrm/simple-mail#/mailings/new');
 }
 /**
  * @return $this
  */
 private function addContextToSession()
 {
     simplemail_civicrm_addToSessionScope('context', $this->get('context'));
     return $this;
 }
 /**
  * TODO: Find a way to move this into CRM_Simplemail_Form_Task_SimpleMail::postProcess()
  *
  * Note: A lot of the logic in this method (for creating hidden and smart groups) is taken from
  * CRM_Mailing_Form_Group::postProcess()
  *
  * @return null|string
  * @throws Exception
  */
 private static function createSmartContactGroupForSearchContacts()
 {
     $searchParams = simplemail_civicrm_getFromSessionScope('searchParams');
     $contactIds = simplemail_civicrm_getFromSessionScope('contactIds');
     $smartGroupId = NULL;
     if ($contactIds) {
         $resultSelectOption = $searchParams['radio_ts'];
         // Only the ticked contacts in the search result need to be sent mailing - create a hidden group for them
         if ($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($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 saved search ID
             $ssId = simplemail_civicrm_getFromSessionScope('ssId');
             $formValues = simplemail_civicrm_getFromSessionScope('formValues');
             $customSearchId = simplemail_civicrm_getFromSessionScope('customSearchId');
             $context = simplemail_civicrm_getFromSessionScope('context');
             $hiddenSmartParams = array('group_type' => array('2' => 1), 'form_values' => $formValues, 'saved_search_id' => $ssId, 'search_custom_id' => $customSearchId, 'search_context' => $context);
             list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams);
             // Set the saved search ID
             if (!$ssId) {
                 if ($savedSearchId) {
                     simplemail_civicrm_addToSessionScope('ssId', $savedSearchId);
                 } else {
                     CRM_Core_Error::fatal();
                 }
             }
         }
     }
     simplemail_civicrm_addToSessionScope('smartGroupId', $smartGroupId);
 }