3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to email a group of contacts.
21 class CRM_Activity_Form_Task_PickOption
extends CRM_Activity_Form_Task
{
24 * The title of the group.
31 * Maximum Activities that should be allowed to update.
34 protected $_maxActivities = 100;
37 * Variable to store redirect path.
40 protected $_userContext;
43 * Variable to store contact Ids.
49 * Build all the data structures needed to build the form.
51 public function preProcess() {
53 // initialize the task and row fields.
55 $session = CRM_Core_Session
::singleton();
56 $this->_userContext
= $session->readUserContext();
58 CRM_Utils_System
::setTitle(ts('Send Email to Contacts'));
62 if (count($this->_activityHolderIds
) > $this->_maxActivities
) {
63 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.", [
64 1 => $this->_maxActivities
,
65 2 => count($this->_activityHolderIds
),
66 ]), ts("Maximum Exceeded"), "error");
71 CRM_Utils_System
::redirect($this->_userContext
);
76 * Build the form object.
78 public function buildQuickForm() {
79 $this->addElement('checkbox', 'with_contact', ts('With Contact'));
80 $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact'));
81 $this->addElement('checkbox', 'created_by', ts('Created by'));
82 $this->setDefaults(['with_contact' => 1]);
83 $this->addDefaultButtons(ts('Continue'));
87 * Add local and global form rules.
89 public function addRules() {
90 $this->addFormRule(['CRM_Activity_Form_Task_PickOption', 'formRule']);
94 * Global validation rules for the form.
96 * @param array $fields
97 * Posted values of the form.
100 * list of errors to be posted back to the form
102 public static function formRule($fields) {
103 if (!isset($fields['with_contact']) &&
104 !isset($fields['assigned_to']) &&
105 !isset($fields['created_by'])
107 return ['with_contact' => ts('You must select at least one email recipient type.')];
113 * Process the form after the input has been submitted and validated.
115 public function postProcess() {
116 // Clear any formRule errors from Email form in case they came back here via Cancel button
117 $this->controller
->resetPage('Email');
118 $params = $this->exportValues();
119 $this->_contacts
= [];
121 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
122 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
123 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
124 // Get assignee contacts.
125 if (!empty($params['assigned_to'])) {
126 foreach ($this->_activityHolderIds
as $key => $id) {
127 $ids = array_keys(CRM_Activity_BAO_ActivityContact
::getNames($id, $assigneeID));
128 $this->_contacts
= array_merge($this->_contacts
, $ids);
131 // Get target contacts.
132 if (!empty($params['with_contact'])) {
133 foreach ($this->_activityHolderIds
as $key => $id) {
134 $ids = array_keys(CRM_Activity_BAO_ActivityContact
::getNames($id, $targetID));
135 $this->_contacts
= array_merge($this->_contacts
, $ids);
138 // Get 'Added by' contacts.
139 if (!empty($params['created_by'])) {
140 parent
::setContactIDs();
141 if (!empty($this->_contactIds
)) {
142 $this->_contacts
= array_merge($this->_contacts
, $this->_contactIds
);
145 $this->_contacts
= array_unique($this->_contacts
);
147 // Bounce to pick option if no contacts to send to.
148 if (empty($this->_contacts
)) {
149 $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}";
150 $urlRedirect = CRM_Utils_System
::url('civicrm/activity/search', $urlParams);
151 CRM_Core_Error
::statusBounce(
152 ts('It appears you have no contacts with email addresses from the selected recipients.'),
157 $this->set('contacts', $this->_contacts
);