Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
5a409b50 | 19 | * This class provides the functionality to group contacts. |
20 | * | |
21 | * This class provides functionality for the actual | |
6a488035 TO |
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 | /** | |
100fef9d | 34 | * The groupId retrieved from the GET vars |
6a488035 TO |
35 | * |
36 | * @var int | |
37 | */ | |
38 | protected $_id; | |
39 | ||
40 | /** | |
100fef9d | 41 | * The title of the group |
6a488035 TO |
42 | * |
43 | * @var string | |
44 | */ | |
45 | protected $_title; | |
46 | ||
47 | /** | |
fe482240 | 48 | * Build all the data structures needed to build the form. |
6a488035 | 49 | */ |
00be9182 | 50 | public function preProcess() { |
d424ffde | 51 | // initialize the task and row fields |
6a488035 TO |
52 | parent::preProcess(); |
53 | ||
54 | $this->_context = $this->get('context'); | |
55 | $this->_id = $this->get('amtgID'); | |
56 | } | |
57 | ||
58 | /** | |
fe482240 | 59 | * Build the form object. |
6a488035 | 60 | */ |
00be9182 | 61 | public function buildQuickForm() { |
6a488035 TO |
62 | |
63 | //create radio buttons to select existing group or add a new group | |
be2fb01f | 64 | $options = [ts('Add Contact To Existing Group'), ts('Create New Group')]; |
6a488035 TO |
65 | |
66 | if (!$this->_id) { | |
be2fb01f | 67 | $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]); |
6a488035 TO |
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.'), | |
be2fb01f | 73 | 'objectExists', ['CRM_Contact_DAO_Group', $this->_id, 'title'] |
6a488035 TO |
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, ' ' | |
97 | ); | |
98 | } | |
99 | } | |
100 | ||
101 | // add select for groups | |
be2fb01f | 102 | $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant::nestedGroup(); |
6a488035 | 103 | |
be2fb01f | 104 | $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group, FALSE, ['class' => 'crm-select2 huge']); |
6a488035 TO |
105 | |
106 | $this->_title = $group[$this->_id]; | |
107 | ||
108 | if ($this->_context === 'amtg') { | |
109 | $groupElement->freeze(); | |
110 | ||
111 | // also set the group title | |
be2fb01f | 112 | $groupValues = ['id' => $this->_id, 'title' => $this->_title]; |
6a488035 TO |
113 | $this->assign_by_ref('group', $groupValues); |
114 | } | |
115 | ||
116 | // Set dynamic page title for 'Add Members Group (confirm)' | |
117 | if ($this->_id) { | |
be2fb01f | 118 | CRM_Utils_System::setTitle(ts('Add Contacts: %1', [1 => $this->_title])); |
6a488035 TO |
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 | /** | |
fe482240 | 128 | * Set the default form values. |
6a488035 | 129 | * |
6a488035 | 130 | * |
a6c01b45 CW |
131 | * @return array |
132 | * the default array reference | |
6a488035 | 133 | */ |
00be9182 | 134 | public function setDefaultValues() { |
be2fb01f | 135 | $defaults = []; |
6a488035 TO |
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 | /** | |
fe482240 | 146 | * Add local and global form rules. |
6a488035 | 147 | */ |
00be9182 | 148 | public function addRules() { |
c91b34a5 | 149 | $this->addFormRule(['CRM_Contact_Form_Task_AddToGroup', 'formRule']); |
6a488035 TO |
150 | } |
151 | ||
152 | /** | |
fe482240 | 153 | * Global validation rules for the form. |
6a488035 | 154 | * |
c490a46a | 155 | * @param array $params |
6a488035 | 156 | * |
a6c01b45 CW |
157 | * @return array |
158 | * list of errors to be posted back to the form | |
6a488035 | 159 | */ |
00be9182 | 160 | public static function formRule($params) { |
be2fb01f | 161 | $errors = []; |
6a488035 TO |
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 | /** | |
fe482240 | 174 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
175 | */ |
176 | public function postProcess() { | |
177 | $params = $this->controller->exportValues(); | |
9c1bc317 | 178 | $groupOption = $params['group_option'] ?? NULL; |
6a488035 | 179 | if ($groupOption) { |
be2fb01f | 180 | $groupParams = []; |
6a488035 TO |
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, | |
353ffa53 TO |
186 | array_keys($params['group_type']) |
187 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
188 | } |
189 | else { | |
190 | $groupParams['group_type'] = ''; | |
191 | } | |
192 | $groupParams['is_active'] = 1; | |
193 | ||
194 | $createdGroup = CRM_Contact_BAO_Group::create($groupParams); | |
353ffa53 TO |
195 | $groupID = $createdGroup->id; |
196 | $groupName = $groupParams['title']; | |
6a488035 TO |
197 | } |
198 | else { | |
353ffa53 TO |
199 | $groupID = $params['group_id']; |
200 | $group = CRM_Core_PseudoConstant::group(); | |
6a488035 TO |
201 | $groupName = $group[$groupID]; |
202 | } | |
203 | ||
204 | list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID); | |
205 | ||
be2fb01f CW |
206 | $status = [ |
207 | ts('%count contact added to group', [ | |
69078420 SL |
208 | 'count' => $added, |
209 | 'plural' => '%count contacts added to group', | |
be2fb01f CW |
210 | ]), |
211 | ]; | |
6a488035 | 212 | if ($notAdded) { |
be2fb01f | 213 | $status[] = ts('%count contact was already in group', [ |
69078420 SL |
214 | 'count' => $notAdded, |
215 | 'plural' => '%count contacts were already in group', | |
216 | ]); | |
6a488035 TO |
217 | } |
218 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
be2fb01f | 219 | CRM_Core_Session::setStatus($status, ts('Added Contact to %1', [ |
69078420 SL |
220 | 1 => $groupName, |
221 | 'count' => $added, | |
222 | 'plural' => 'Added Contacts to %1', | |
223 | ]), 'success', ['expires' => 0]); | |
93e05f92 CW |
224 | |
225 | if ($this->_context === 'amtg') { | |
353ffa53 TO |
226 | CRM_Core_Session::singleton() |
227 | ->pushUserContext(CRM_Utils_System::url('civicrm/group/search', "reset=1&force=1&context=smog&gid=$groupID")); | |
93e05f92 | 228 | } |
6a488035 | 229 | } |
96025800 | 230 | |
6a488035 | 231 | } |