commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / UF / Form / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is for UF Group
38 */
39 class CRM_UF_Form_Group extends CRM_Core_Form {
40
41 /**
42 * The form id saved to the session for an update.
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * The title for group.
50 *
51 * @var int
52 */
53 protected $_title;
54 protected $_groupElement;
55 protected $_group;
56 protected $_allPanes;
57
58 /**
59 * Set variables up before form is built.
60 *
61 * @return void
62 */
63 public function preProcess() {
64 // current form id
65 $this->_id = $this->get('id');
66 if (!$this->_id) {
67 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
68 }
69 $this->assign('gid', $this->_id);
70 $this->_group = CRM_Core_PseudoConstant::group();
71
72 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
73 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
74 $this->assign('profileTitle', $title);
75 }
76
77 // setting title for html page
78 if ($this->_action & CRM_Core_Action::UPDATE) {
79 CRM_Utils_System::setTitle(ts('Profile Settings') . " - $title");
80 }
81 elseif ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) {
82 $ufGroup['module'] = implode(' , ', CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE));
83 $status = 0;
84 $status = CRM_Core_BAO_UFGroup::usedByModule($this->_id);
85 if ($this->_action & (CRM_Core_Action::DISABLE)) {
86 if ($status) {
87 $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you disable the profile - it will be removed from these forms and/or modules. Do you want to continue?';
88 }
89 else {
90 $message = 'Are you sure you want to disable this Profile?';
91 }
92 }
93 else {
94 if ($status) {
95 $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you delete the profile - it will be removed from these forms and/or modules. This action cannot be undone. Do you want to continue?';
96 }
97 else {
98 $message = 'Are you sure you want to delete this Profile? This action cannot be undone.';
99 }
100 }
101 $this->assign('message', $message);
102 }
103 else {
104 CRM_Utils_System::setTitle(ts('New CiviCRM Profile'));
105 }
106 }
107
108 /**
109 * Build the form object.
110 *
111 * @return void
112 */
113 public function buildQuickForm() {
114 if ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) {
115 if ($this->_action & (CRM_Core_Action::DISABLE)) {
116 $display = 'Disable Profile';
117 }
118 else {
119 $display = 'Delete Profile';
120 }
121 $this->addButtons(array(
122 array(
123 'type' => 'next',
124 'name' => $display,
125 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
126 'isDefault' => TRUE,
127 ),
128 array(
129 'type' => 'cancel',
130 'name' => ts('Cancel'),
131 ),
132 )
133 );
134 return;
135 }
136 $this->applyFilter('__ALL__', 'trim');
137
138 // title
139 $this->add('text', 'title', ts('Profile Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'title'), TRUE);
140 $this->add('textarea', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'description'));
141
142 //add checkboxes
143 $uf_group_type = array();
144 $UFGroupType = CRM_Core_SelectValues::ufGroupTypes();
145 foreach ($UFGroupType as $key => $value) {
146 $uf_group_type[] = $this->createElement('checkbox', $key, NULL, $value);
147 }
148 $this->addGroup($uf_group_type, 'uf_group_type', ts('Used For'), '&nbsp;');
149
150 // help text
151 $this->addWysiwyg('help_pre', ts('Pre-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post'));
152 $this->addWysiwyg('help_post', ts('Post-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post'));
153
154 // weight
155 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE);
156 $this->addRule('weight', ts('is a numeric field'), 'numeric');
157
158 // is this group active ?
159 $this->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?'));
160
161 $paneNames = array('Advanced Settings' => 'buildAdvanceSetting');
162
163 foreach ($paneNames as $name => $type) {
164 if ($this->_id) {
165 $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}";
166 }
167 else {
168 $dataURL = "&reset=1&action=add&snippet=4&formType={$type}";
169 }
170
171 $allPanes[$name] = array(
172 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting',
173 $dataURL
174 ),
175 'open' => 'false',
176 'id' => $type,
177 );
178
179 CRM_UF_Form_AdvanceSetting::$type($this);
180 }
181
182 $this->addButtons(array(
183 array(
184 'type' => 'next',
185 'name' => ts('Save'),
186 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
187 'isDefault' => TRUE,
188 ),
189 array(
190 'type' => 'cancel',
191 'name' => ts('Cancel'),
192 ),
193 )
194 );
195
196 // views are implemented as frozen form
197 if ($this->_action & CRM_Core_Action::VIEW) {
198 $this->freeze();
199 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'"));
200 }
201
202 $this->addFormRule(array('CRM_UF_Form_Group', 'formRule'), $this);
203 }
204
205 /**
206 * Set default values for the form. Note that in edit/view mode
207 * the default values are retrieved from the database
208 *
209 *
210 * @return void
211 */
212 public function setDefaultValues() {
213 $defaults = array();
214 $showHide = new CRM_Core_ShowHideBlocks();
215
216 if ($this->_action == CRM_Core_Action::ADD) {
217 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin');
218 }
219
220 //id fetched for Dojo Pane
221 $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
222 if (isset($pId)) {
223 $this->_id = $pId;
224 }
225
226 if ((isset($this->_id))) {
227
228 $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id);
229
230 $params = array('id' => $this->_id);
231 CRM_Core_BAO_UFGroup::retrieve($params, $defaults);
232 $defaults['group'] = CRM_Utils_Array::value('limit_listings_group_id', $defaults);
233 $defaults['add_contact_to_group'] = CRM_Utils_Array::value('add_to_group_id', $defaults);
234 //get the uf join records for current uf group
235 $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id);
236 foreach ($ufJoinRecords as $key => $value) {
237 $checked[$value] = 1;
238 }
239 $defaults['uf_group_type'] = isset($checked) ? $checked : "";
240
241 //get the uf join records for current uf group other than default modules
242 $otherModules = array();
243 $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE, TRUE);
244 if (!empty($otherModules)) {
245 $otherModuleString = NULL;
246 foreach ($otherModules as $key) {
247 $otherModuleString .= " [ x ] <label>" . $key . "</label>";
248 }
249 $this->assign('otherModuleString', $otherModuleString);
250 }
251
252 $showAdvanced = 0;
253 $advFields = array(
254 'group',
255 'post_URL',
256 'cancel_URL',
257 'add_captcha',
258 'is_map',
259 'is_uf_link',
260 'is_edit_link',
261 'is_update_dupe',
262 'is_cms_user',
263 'is_proximity_search',
264 );
265 foreach ($advFields as $key) {
266 if (!empty($defaults[$key])) {
267 $showAdvanced = 1;
268 $this->_allPanes['Advanced Settings']['open'] = 'true';
269 break;
270 }
271 }
272 }
273 else {
274 $defaults['is_active'] = 1;
275 $defaults['is_map'] = 0;
276 $defaults['is_update_dupe'] = 0;
277 $defaults['is_proximity_search'] = 0;
278 }
279 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
280 if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) {
281 $showHide->addToTemplate();
282 }
283 $this->assign('allPanes', $this->_allPanes);
284 return $defaults;
285 }
286
287 /**
288 * Global form rule.
289 *
290 * @param array $fields
291 * The input form values.
292 * @param array $files
293 * The uploaded files if any.
294 * @param array $self
295 * Current form object.
296 *
297 * @return bool|array
298 * true if no errors, else array of errors
299 */
300 public static function formRule($fields, $files, $self) {
301 $errors = array();
302
303 //validate profile title as well as name.
304 $title = $fields['title'];
305 $name = CRM_Utils_String::munge($title, '_', 56);
306 $name .= $self->_id ? '_' . $self->_id : '';
307 $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2';
308 $pCnt = CRM_Core_DAO::singleValueQuery($query, array(
309 1 => array($name, 'String'),
310 2 => array((int) $self->_id, 'Integer'),
311 ));
312 if ($pCnt) {
313 $errors['title'] = ts('Profile \'%1\' already exists in Database.', array(1 => $title));
314 }
315
316 return empty($errors) ? TRUE : $errors;
317 }
318
319 /**
320 * Process the form.
321 *
322 * @return void
323 */
324 public function postProcess() {
325 if ($this->_action & CRM_Core_Action::DELETE) {
326 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
327 CRM_Core_BAO_UFGroup::del($this->_id);
328 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", array(1 => $title)), ts('Profile Deleted'), 'success');
329 }
330 elseif ($this->_action & CRM_Core_Action::DISABLE) {
331 $ufJoinParams = array('uf_group_id' => $this->_id);
332 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
333
334 CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0);
335 }
336 else {
337 // get the submitted form values.
338 $params = $ids = array();
339 $params = $this->controller->exportValues($this->_name);
340
341 if (!array_key_exists('is_active', $params)) {
342 $params['is_active'] = 0;
343 }
344
345 if ($this->_action & (CRM_Core_Action::UPDATE)) {
346 $ids['ufgroup'] = $this->_id;
347 // CRM-5284
348 // lets skip trying to mess around with profile weights and allow the user to do as needed.
349 }
350 elseif ($this->_action & CRM_Core_Action::ADD) {
351 $session = CRM_Core_Session::singleton();
352 $params['created_id'] = $session->get('userID');
353 $params['created_date'] = date('YmdHis');
354 }
355
356 // create uf group
357 $ufGroup = CRM_Core_BAO_UFGroup::add($params, $ids);
358
359 if (!empty($params['is_active'])) {
360 //make entry in uf join table
361 CRM_Core_BAO_UFGroup::createUFJoin($params, $ufGroup->id);
362 }
363 elseif ($this->_id) {
364 // this profile has been set to inactive, delete all corresponding UF Join's
365 $ufJoinParams = array('uf_group_id' => $this->_id);
366 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
367 }
368
369 if ($this->_action & CRM_Core_Action::UPDATE) {
370 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
371 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", array(1 => $ufGroup->title)), ts('Profile Saved'), 'success');
372 }
373 else {
374 // Jump directly to adding a field if popups are disabled
375 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add';
376 $url = CRM_Utils_System::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id . '&action=' . ($action ? 'add' : 'browse'));
377 CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.',
378 array(1 => $ufGroup->title)
379 ), ts('Profile Added'), 'success');
380 }
381 $session = CRM_Core_Session::singleton();
382 $session->replaceUserContext($url);
383 }
384
385 // update cms integration with registration / my account
386 CRM_Utils_System::updateCategories();
387 }
388
389 }