Merge pull request #15760 from colemanw/iconPicker
[civicrm-core.git] / CRM / Event / Form / Task / AddToGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class provides the functionality to group
22 * contacts. This class provides functionality for the actual
23 * addition of contacts to groups.
24 */
25 class CRM_Event_Form_Task_AddToGroup extends CRM_Event_Form_Task {
26
27 /**
28 * The context that we are working on.
29 *
30 * @var string
31 */
32 protected $_context;
33
34 /**
35 * The groupId retrieved from the GET vars.
36 *
37 * @var int
38 */
39 protected $_id;
40
41 /**
42 * The title of the group.
43 *
44 * @var string
45 */
46 protected $_title;
47
48 /**
49 * Build all the data structures needed to build the form.
50 *
51 * @return void
52 */
53 public function preProcess() {
54 // initialize the task and row fields
55 parent::preProcess();
56
57 parent::setContactIDs();
58 $this->_context = $this->get('context');
59 $this->_id = $this->get('amtgID');
60 }
61
62 /**
63 * Build the form object.
64 *
65 *
66 * @return void
67 */
68 public function buildQuickForm() {
69
70 //create radio buttons to select existing group or add a new group
71 $options = [ts('Add Contact To Existing Group'), ts('Create New Group')];
72
73 if (!$this->_id) {
74 $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]);
75
76 $this->add('text', 'title', ts('Group Name:') . ' ',
77 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title')
78 );
79 $this->addRule('title', ts('Name already exists in Database.'),
80 'objectExists', ['CRM_Contact_DAO_Group', $this->_id, 'title']
81 );
82
83 $this->add('textarea', 'description', ts('Description:') . ' ',
84 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
85 );
86
87 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
88 if (!CRM_Core_Permission::access('CiviMail')) {
89 $isWorkFlowEnabled = CRM_Mailing_Info::workflowEnabled();
90 if ($isWorkFlowEnabled &&
91 !CRM_Core_Permission::check('create mailings') &&
92 !CRM_Core_Permission::check('schedule mailings') &&
93 !CRM_Core_Permission::check('approve mailings')
94 ) {
95 unset($groupTypes['Mailing List']);
96 }
97 }
98
99 if (!empty($groupTypes)) {
100 $this->addCheckBox('group_type',
101 ts('Group Type'),
102 $groupTypes,
103 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
104 );
105 }
106 }
107
108 // add select for groups
109 $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant::group();
110
111 $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group);
112
113 $this->_title = $group[$this->_id];
114
115 if ($this->_context === 'amtg') {
116 $groupElement->freeze();
117
118 // also set the group title
119 $groupValues = ['id' => $this->_id, 'title' => $this->_title];
120 $this->assign_by_ref('group', $groupValues);
121 }
122
123 // Set dynamic page title for 'Add Members Group (confirm)'
124 if ($this->_id) {
125 CRM_Utils_System::setTitle(ts('Add Contacts: %1', [1 => $this->_title]));
126 }
127 else {
128 CRM_Utils_System::setTitle(ts('Add Contacts to A Group'));
129 }
130
131 $this->addDefaultButtons(ts('Add to Group'));
132 }
133
134 /**
135 * Set the default form values.
136 *
137 *
138 * @return array
139 * the default array reference
140 */
141 public function setDefaultValues() {
142 $defaults = [];
143
144 if ($this->_context === 'amtg') {
145 $defaults['group_id'] = $this->_id;
146 }
147
148 $defaults['group_option'] = 0;
149 return $defaults;
150 }
151
152 /**
153 * Add local and global form rules.
154 *
155 *
156 * @return void
157 */
158 public function addRules() {
159 $this->addFormRule(['CRM_Event_Form_Task_AddToGroup', 'formRule']);
160 }
161
162 /**
163 * Global validation rules for the form.
164 *
165 * @param array $params
166 * Posted values of the form.
167 *
168 * @return array
169 * list of errors to be posted back to the form
170 */
171 public static function formRule($params) {
172 $errors = [];
173
174 if (!empty($params['group_option']) && empty($params['title'])) {
175 $errors['title'] = "Group Name is a required field";
176 }
177 elseif (empty($params['group_option']) && empty($params['group_id'])) {
178 $errors['group_id'] = "Select Group is a required field.";
179 }
180
181 return empty($errors) ? TRUE : $errors;
182 }
183
184 /**
185 * Process the form after the input has been submitted and validated.
186 *
187 *
188 * @return void
189 */
190 public function postProcess() {
191 $params = $this->controller->exportValues();
192 $groupOption = CRM_Utils_Array::value('group_option', $params, NULL);
193 if ($groupOption) {
194 $groupParams = [];
195 $groupParams['title'] = $params['title'];
196 $groupParams['description'] = $params['description'];
197 $groupParams['visibility'] = "User and User Admin Only";
198 if (array_key_exists('group_type', $params) && is_array($params['group_type'])) {
199 $groupParams['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
200 array_keys($params['group_type'])
201 ) . CRM_Core_DAO::VALUE_SEPARATOR;
202 }
203 else {
204 $groupParams['group_type'] = '';
205 }
206 $groupParams['is_active'] = 1;
207
208 $createdGroup = CRM_Contact_BAO_Group::create($groupParams);
209 $groupID = $createdGroup->id;
210 $groupName = $groupParams['title'];
211 }
212 else {
213 $groupID = $params['group_id'];
214 $group = CRM_Core_PseudoConstant::group();
215 $groupName = $group[$groupID];
216 }
217
218 list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID);
219
220 $status = [
221 ts('%count contact added to group', [
222 'count' => $added,
223 'plural' => '%count contacts added to group',
224 ]),
225 ];
226 if ($notAdded) {
227 $status[] = ts('%count contact was already in group', [
228 'count' => $notAdded,
229 'plural' => '%count contacts were already in group',
230 ]);
231 }
232 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
233 CRM_Core_Session::setStatus($status, ts('Added Contact to %1', [
234 1 => $groupName,
235 'count' => $added,
236 'plural' => 'Added Contacts to %1',
237 ]), 'success', ['expires' => 0]);
238 }
239
240 }