| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class provides the functionality to email a group of contacts. |
| 36 | */ |
| 37 | class CRM_Activity_Form_Task_PickOption extends CRM_Activity_Form_Task { |
| 38 | |
| 39 | /** |
| 40 | * The title of the group. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $_title; |
| 45 | |
| 46 | /** |
| 47 | * Maximum Activities that should be allowed to update. |
| 48 | */ |
| 49 | protected $_maxActivities = 100; |
| 50 | |
| 51 | /** |
| 52 | * Variable to store redirect path. |
| 53 | */ |
| 54 | protected $_userContext; |
| 55 | |
| 56 | /** |
| 57 | * Variable to store contact Ids. |
| 58 | */ |
| 59 | public $_contacts; |
| 60 | |
| 61 | /** |
| 62 | * Build all the data structures needed to build the form. |
| 63 | */ |
| 64 | public function preProcess() { |
| 65 | /* |
| 66 | * initialize the task and row fields |
| 67 | */ |
| 68 | |
| 69 | parent::preProcess(); |
| 70 | $session = CRM_Core_Session::singleton(); |
| 71 | $this->_userContext = $session->readUserContext(); |
| 72 | |
| 73 | CRM_Utils_System::setTitle(ts('Send Email to Contacts')); |
| 74 | |
| 75 | $validate = FALSE; |
| 76 | //validations |
| 77 | if (count($this->_activityHolderIds) > $this->_maxActivities) { |
| 78 | CRM_Core_Session::setStatus(ts("The maximum number of Activities you can select to send an email is %1. You have selected %2. Please select fewer Activities from your search results and try again.", array( |
| 79 | 1 => $this->_maxActivities, |
| 80 | 2 => count($this->_activityHolderIds), |
| 81 | )), ts("Maximum Exceeded"), "error"); |
| 82 | $validate = TRUE; |
| 83 | } |
| 84 | // then redirect |
| 85 | if ($validate) { |
| 86 | CRM_Utils_System::redirect($this->_userContext); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Build the form object. |
| 92 | */ |
| 93 | public function buildQuickForm() { |
| 94 | $this->addElement('checkbox', 'with_contact', ts('With Contact')); |
| 95 | $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact')); |
| 96 | $this->addElement('checkbox', 'created_by', ts('Created by')); |
| 97 | $this->setDefaults(array('with_contact' => 1)); |
| 98 | $this->addDefaultButtons(ts('Continue')); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Add local and global form rules. |
| 103 | */ |
| 104 | public function addRules() { |
| 105 | $this->addFormRule(array('CRM_Activity_Form_Task_PickOption', 'formRule')); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Global validation rules for the form. |
| 110 | * |
| 111 | * @param array $fields |
| 112 | * Posted values of the form. |
| 113 | * |
| 114 | * @return array |
| 115 | * list of errors to be posted back to the form |
| 116 | */ |
| 117 | public static function formRule($fields) { |
| 118 | if (!isset($fields['with_contact']) && |
| 119 | !isset($fields['assigned_to']) && |
| 120 | !isset($fields['created_by']) |
| 121 | ) { |
| 122 | return array('with_contact' => ts('You must select at least one email recipient type.')); |
| 123 | } |
| 124 | return TRUE; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Process the form after the input has been submitted and validated. |
| 129 | */ |
| 130 | public function postProcess() { |
| 131 | // Clear any formRule errors from Email form in case they came back here via Cancel button |
| 132 | $this->controller->resetPage('Email'); |
| 133 | $params = $this->exportValues(); |
| 134 | $this->_contacts = array(); |
| 135 | |
| 136 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 137 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 138 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 139 | //get assignee contacts |
| 140 | if (!empty($params['assigned_to'])) { |
| 141 | foreach ($this->_activityHolderIds as $key => $id) { |
| 142 | $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $assigneeID)); |
| 143 | $this->_contacts = array_merge($this->_contacts, $ids); |
| 144 | } |
| 145 | } |
| 146 | //get target contacts |
| 147 | if (!empty($params['with_contact'])) { |
| 148 | foreach ($this->_activityHolderIds as $key => $id) { |
| 149 | $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID)); |
| 150 | $this->_contacts = array_merge($this->_contacts, $ids); |
| 151 | } |
| 152 | } |
| 153 | //get 'Added by' contacts |
| 154 | if (!empty($params['created_by'])) { |
| 155 | parent::setContactIDs(); |
| 156 | if (!empty($this->_contactIds)) { |
| 157 | $this->_contacts = array_merge($this->_contacts, $this->_contactIds); |
| 158 | } |
| 159 | } |
| 160 | $this->_contacts = array_unique($this->_contacts); |
| 161 | |
| 162 | //bounce to pick option if no contacts to send to |
| 163 | if (empty($this->_contacts)) { |
| 164 | $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}"; |
| 165 | $urlRedirect = CRM_Utils_System::url('civicrm/activity/search', $urlParams); |
| 166 | CRM_Core_Error::statusBounce( |
| 167 | ts('It appears you have no contacts with email addresses from the selected recipients.'), |
| 168 | $urlRedirect |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | $this->set('contacts', $this->_contacts); |
| 173 | } |
| 174 | |
| 175 | } |