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