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