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