3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * This class provides the functionality to email a group of contacts.
37 class CRM_Activity_Form_Task_PickOption
extends CRM_Activity_Form_Task
{
40 * The title of the group.
47 * Maximum Activities that should be allowed to update.
50 protected $_maxActivities = 100;
53 * Variable to store redirect path.
56 protected $_userContext;
59 * Variable to store contact Ids.
65 * Build all the data structures needed to build the form.
67 public function preProcess() {
69 // initialize the task and row fields.
71 $session = CRM_Core_Session
::singleton();
72 $this->_userContext
= $session->readUserContext();
74 CRM_Utils_System
::setTitle(ts('Send Email to Contacts'));
78 if (count($this->_activityHolderIds
) > $this->_maxActivities
) {
79 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.", [
80 1 => $this->_maxActivities
,
81 2 => count($this->_activityHolderIds
),
82 ]), ts("Maximum Exceeded"), "error");
87 CRM_Utils_System
::redirect($this->_userContext
);
92 * Build the form object.
94 public function buildQuickForm() {
95 $this->addElement('checkbox', 'with_contact', ts('With Contact'));
96 $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact'));
97 $this->addElement('checkbox', 'created_by', ts('Created by'));
98 $this->setDefaults(['with_contact' => 1]);
99 $this->addDefaultButtons(ts('Continue'));
103 * Add local and global form rules.
105 public function addRules() {
106 $this->addFormRule(['CRM_Activity_Form_Task_PickOption', 'formRule']);
110 * Global validation rules for the form.
112 * @param array $fields
113 * Posted values of the form.
116 * list of errors to be posted back to the form
118 public static function formRule($fields) {
119 if (!isset($fields['with_contact']) &&
120 !isset($fields['assigned_to']) &&
121 !isset($fields['created_by'])
123 return ['with_contact' => ts('You must select at least one email recipient type.')];
129 * Process the form after the input has been submitted and validated.
131 public function postProcess() {
132 // Clear any formRule errors from Email form in case they came back here via Cancel button
133 $this->controller
->resetPage('Email');
134 $params = $this->exportValues();
135 $this->_contacts
= [];
137 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
138 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
139 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
140 // Get assignee contacts.
141 if (!empty($params['assigned_to'])) {
142 foreach ($this->_activityHolderIds
as $key => $id) {
143 $ids = array_keys(CRM_Activity_BAO_ActivityContact
::getNames($id, $assigneeID));
144 $this->_contacts
= array_merge($this->_contacts
, $ids);
147 // Get target contacts.
148 if (!empty($params['with_contact'])) {
149 foreach ($this->_activityHolderIds
as $key => $id) {
150 $ids = array_keys(CRM_Activity_BAO_ActivityContact
::getNames($id, $targetID));
151 $this->_contacts
= array_merge($this->_contacts
, $ids);
154 // Get 'Added by' contacts.
155 if (!empty($params['created_by'])) {
156 parent
::setContactIDs();
157 if (!empty($this->_contactIds
)) {
158 $this->_contacts
= array_merge($this->_contacts
, $this->_contactIds
);
161 $this->_contacts
= array_unique($this->_contacts
);
163 // Bounce to pick option if no contacts to send to.
164 if (empty($this->_contacts
)) {
165 $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}";
166 $urlRedirect = CRM_Utils_System
::url('civicrm/activity/search', $urlParams);
167 CRM_Core_Error
::statusBounce(
168 ts('It appears you have no contacts with email addresses from the selected recipients.'),
173 $this->set('contacts', $this->_contacts
);