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