Commit | Line | Data |
---|---|---|
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 | * $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 | /** | |
66f9e52b | 28 | * The context that we are working on. |
bb7b01e6 SD |
29 | * |
30 | * @var string | |
31 | */ | |
32 | protected $_context; | |
33 | ||
34 | /** | |
66f9e52b | 35 | * The groupId retrieved from the GET vars. |
bb7b01e6 SD |
36 | * |
37 | * @var int | |
38 | */ | |
39 | protected $_id; | |
40 | ||
41 | /** | |
66f9e52b | 42 | * The title of the group. |
bb7b01e6 SD |
43 | * |
44 | * @var string | |
45 | */ | |
46 | protected $_title; | |
47 | ||
48 | /** | |
66f9e52b | 49 | * Build all the data structures needed to build the form. |
bb7b01e6 SD |
50 | * |
51 | * @return void | |
bb7b01e6 | 52 | */ |
00be9182 | 53 | public function preProcess() { |
c490a46a | 54 | // initialize the task and row fields |
bb7b01e6 SD |
55 | parent::preProcess(); |
56 | ||
57 | parent::setContactIDs(); | |
58 | $this->_context = $this->get('context'); | |
59 | $this->_id = $this->get('amtgID'); | |
60 | } | |
61 | ||
62 | /** | |
66f9e52b | 63 | * Build the form object. |
bb7b01e6 | 64 | * |
bb7b01e6 SD |
65 | * |
66 | * @return void | |
67 | */ | |
00be9182 | 68 | public function buildQuickForm() { |
bb7b01e6 SD |
69 | |
70 | //create radio buttons to select existing group or add a new group | |
be2fb01f | 71 | $options = [ts('Add Contact To Existing Group'), ts('Create New Group')]; |
bb7b01e6 SD |
72 | |
73 | if (!$this->_id) { | |
be2fb01f | 74 | $this->addRadio('group_option', ts('Group Options'), $options, ['onclick' => "return showElements();"]); |
bb7b01e6 SD |
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.'), | |
be2fb01f | 80 | 'objectExists', ['CRM_Contact_DAO_Group', $this->_id, 'title'] |
bb7b01e6 SD |
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, ' ' | |
104 | ); | |
105 | } | |
106 | } | |
107 | ||
108 | // add select for groups | |
be2fb01f | 109 | $group = ['' => ts('- select group -')] + CRM_Core_PseudoConstant::group(); |
bb7b01e6 SD |
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 | |
be2fb01f | 119 | $groupValues = ['id' => $this->_id, 'title' => $this->_title]; |
bb7b01e6 SD |
120 | $this->assign_by_ref('group', $groupValues); |
121 | } | |
122 | ||
123 | // Set dynamic page title for 'Add Members Group (confirm)' | |
124 | if ($this->_id) { | |
be2fb01f | 125 | CRM_Utils_System::setTitle(ts('Add Contacts: %1', [1 => $this->_title])); |
bb7b01e6 SD |
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 | /** | |
66f9e52b | 135 | * Set the default form values. |
bb7b01e6 | 136 | * |
bb7b01e6 | 137 | * |
a6c01b45 CW |
138 | * @return array |
139 | * the default array reference | |
bb7b01e6 | 140 | */ |
00be9182 | 141 | public function setDefaultValues() { |
be2fb01f | 142 | $defaults = []; |
bb7b01e6 SD |
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 | /** | |
66f9e52b | 153 | * Add local and global form rules. |
bb7b01e6 | 154 | * |
bb7b01e6 SD |
155 | * |
156 | * @return void | |
157 | */ | |
00be9182 | 158 | public function addRules() { |
be2fb01f | 159 | $this->addFormRule(['CRM_Event_Form_Task_AddToGroup', 'formRule']); |
bb7b01e6 SD |
160 | } |
161 | ||
162 | /** | |
66f9e52b | 163 | * Global validation rules for the form. |
bb7b01e6 | 164 | * |
408b79bf | 165 | * @param array $params |
d4dd1e85 | 166 | * Posted values of the form. |
bb7b01e6 | 167 | * |
a6c01b45 CW |
168 | * @return array |
169 | * list of errors to be posted back to the form | |
bb7b01e6 | 170 | */ |
00be9182 | 171 | public static function formRule($params) { |
be2fb01f | 172 | $errors = []; |
bb7b01e6 SD |
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 | /** | |
66f9e52b | 185 | * Process the form after the input has been submitted and validated. |
bb7b01e6 | 186 | * |
bb7b01e6 | 187 | * |
16b10e64 | 188 | * @return void |
bb7b01e6 SD |
189 | */ |
190 | public function postProcess() { | |
191 | $params = $this->controller->exportValues(); | |
192 | $groupOption = CRM_Utils_Array::value('group_option', $params, NULL); | |
193 | if ($groupOption) { | |
be2fb01f | 194 | $groupParams = []; |
bb7b01e6 SD |
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, | |
353ffa53 TO |
200 | array_keys($params['group_type']) |
201 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
bb7b01e6 SD |
202 | } |
203 | else { | |
204 | $groupParams['group_type'] = ''; | |
205 | } | |
206 | $groupParams['is_active'] = 1; | |
207 | ||
208 | $createdGroup = CRM_Contact_BAO_Group::create($groupParams); | |
353ffa53 TO |
209 | $groupID = $createdGroup->id; |
210 | $groupName = $groupParams['title']; | |
bb7b01e6 SD |
211 | } |
212 | else { | |
353ffa53 TO |
213 | $groupID = $params['group_id']; |
214 | $group = CRM_Core_PseudoConstant::group(); | |
bb7b01e6 SD |
215 | $groupName = $group[$groupID]; |
216 | } | |
217 | ||
218 | list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID); | |
219 | ||
be2fb01f CW |
220 | $status = [ |
221 | ts('%count contact added to group', [ | |
90b461f1 SL |
222 | 'count' => $added, |
223 | 'plural' => '%count contacts added to group', | |
be2fb01f CW |
224 | ]), |
225 | ]; | |
bb7b01e6 | 226 | if ($notAdded) { |
be2fb01f | 227 | $status[] = ts('%count contact was already in group', [ |
90b461f1 SL |
228 | 'count' => $notAdded, |
229 | 'plural' => '%count contacts were already in group', | |
230 | ]); | |
bb7b01e6 SD |
231 | } |
232 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
be2fb01f | 233 | CRM_Core_Session::setStatus($status, ts('Added Contact to %1', [ |
90b461f1 SL |
234 | 1 => $groupName, |
235 | 'count' => $added, | |
236 | 'plural' => 'Added Contacts to %1', | |
237 | ]), 'success', ['expires' => 0]); | |
bb7b01e6 | 238 | } |
96025800 | 239 | |
bb7b01e6 | 240 | } |