Merge pull request #19125 from eileenmcnaughton/ev533
[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 */
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 */
23 class CRM_Event_Form_Task_AddToGroup extends CRM_Event_Form_Task {
24
25 /**
26 * The context that we are working on.
27 *
28 * @var string
29 */
30 protected $_context;
31
32 /**
33 * The groupId retrieved from the GET vars.
34 *
35 * @var int
36 */
37 protected $_id;
38
39 /**
40 * The title of the group.
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
47 * Build all the data structures needed to build the form.
48 *
49 * @return void
50 */
51 public function preProcess() {
52 // initialize the task and row fields
53 parent::preProcess();
54
55 parent::setContactIDs();
56 $this->_context = $this->get('context');
57 $this->_id = $this->get('amtgID');
58 }
59
60 /**
61 * Build the form object.
62 *
63 *
64 * @return void
65 */
66 public function buildQuickForm() {
67
68 //create radio buttons to select existing group or add a new group
69 $options = [ts('Add Contact To Existing Group'), ts('Create New Group')];
70
71 if (!$this->_id) {
72 $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]);
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.'),
78 'objectExists', ['CRM_Contact_DAO_Group', $this->_id, 'title']
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
107 $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant::group();
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
117 $groupValues = ['id' => $this->_id, 'title' => $this->_title];
118 $this->assign_by_ref('group', $groupValues);
119 }
120
121 // Set dynamic page title for 'Add Members Group (confirm)'
122 if ($this->_id) {
123 CRM_Utils_System::setTitle(ts('Add Contacts: %1', [1 => $this->_title]));
124 }
125 else {
126 CRM_Utils_System::setTitle(ts('Add Contacts to A Group'));
127 }
128
129 $this->addDefaultButtons(ts('Add to Group'));
130 }
131
132 /**
133 * Set the default form values.
134 *
135 *
136 * @return array
137 * the default array reference
138 */
139 public function setDefaultValues() {
140 $defaults = [];
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 /**
151 * Add local and global form rules.
152 *
153 *
154 * @return void
155 */
156 public function addRules() {
157 $this->addFormRule(['CRM_Event_Form_Task_AddToGroup', 'formRule']);
158 }
159
160 /**
161 * Global validation rules for the form.
162 *
163 * @param array $params
164 * Posted values of the form.
165 *
166 * @return array
167 * list of errors to be posted back to the form
168 */
169 public static function formRule($params) {
170 $errors = [];
171
172 if (!empty($params['group_option']) && empty($params['title'])) {
173 $errors['title'] = "Group Name is a required field";
174 }
175 elseif (empty($params['group_option']) && empty($params['group_id'])) {
176 $errors['group_id'] = "Select Group is a required field.";
177 }
178
179 return empty($errors) ? TRUE : $errors;
180 }
181
182 /**
183 * Process the form after the input has been submitted and validated.
184 *
185 *
186 * @return void
187 */
188 public function postProcess() {
189 $params = $this->controller->exportValues();
190 $groupOption = $params['group_option'] ?? NULL;
191 if ($groupOption) {
192 $groupParams = [];
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,
198 array_keys($params['group_type'])
199 ) . CRM_Core_DAO::VALUE_SEPARATOR;
200 }
201 else {
202 $groupParams['group_type'] = '';
203 }
204 $groupParams['is_active'] = 1;
205
206 $createdGroup = CRM_Contact_BAO_Group::create($groupParams);
207 $groupID = $createdGroup->id;
208 $groupName = $groupParams['title'];
209 }
210 else {
211 $groupID = $params['group_id'];
212 $group = CRM_Core_PseudoConstant::group();
213 $groupName = $group[$groupID];
214 }
215
216 list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID);
217
218 $status = [
219 ts('%count contact added to group', [
220 'count' => $added,
221 'plural' => '%count contacts added to group',
222 ]),
223 ];
224 if ($notAdded) {
225 $status[] = ts('%count contact was already in group', [
226 'count' => $notAdded,
227 'plural' => '%count contacts were already in group',
228 ]);
229 }
230 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
231 CRM_Core_Session::setStatus($status, ts('Added Contact to %1', [
232 1 => $groupName,
233 'count' => $added,
234 'plural' => 'Added Contacts to %1',
235 ]), 'success', ['expires' => 0]);
236 }
237
238 }