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