Merge pull request #15834 from francescbassas/patch-16
[civicrm-core.git] / CRM / Custom / Form / DeleteGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class is to build the form for Deleting Group
22 */
23 class CRM_Custom_Form_DeleteGroup extends CRM_Core_Form {
24
25 /**
26 * The group id.
27 *
28 * @var int
29 */
30 protected $_id;
31
32 /**
33 * The title of the group being deleted.
34 *
35 * @var string
36 */
37 protected $_title;
38
39 /**
40 * Set up variables to build the form.
41 *
42 * @return void
43 * @access protected
44 */
45 public function preProcess() {
46 $this->_id = $this->get('id');
47
48 $defaults = [];
49 $params = ['id' => $this->_id];
50 CRM_Core_BAO_CustomGroup::retrieve($params, $defaults);
51 $this->_title = $defaults['title'];
52
53 //check wheter this contain any custom fields
54 $customField = new CRM_Core_DAO_CustomField();
55 $customField->custom_group_id = $this->_id;
56
57 if ($customField->find(TRUE)) {
58 CRM_Core_Session::setStatus(ts("The Group '%1' cannot be deleted! You must Delete all custom fields in this group prior to deleting the group.", [1 => $this->_title]), ts('Deletion Error'), 'error');
59 $url = CRM_Utils_System::url('civicrm/admin/custom/group', "reset=1");
60 CRM_Utils_System::redirect($url);
61 return TRUE;
62 }
63 $this->assign('title', $this->_title);
64
65 CRM_Utils_System::setTitle(ts('Confirm Custom Group Delete'));
66 }
67
68 /**
69 * Build the form object.
70 *
71 * @return void
72 */
73 public function buildQuickForm() {
74
75 $this->addButtons([
76 [
77 'type' => 'next',
78 'name' => ts('Delete Custom Group'),
79 'isDefault' => TRUE,
80 ],
81 [
82 'type' => 'cancel',
83 'name' => ts('Cancel'),
84 ],
85 ]);
86 }
87
88 /**
89 * Process the form when submitted.
90 *
91 * @return void
92 */
93 public function postProcess() {
94 $group = new CRM_Core_DAO_CustomGroup();
95 $group->id = $this->_id;
96 $group->find(TRUE);
97
98 $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_CustomGroup', $this->_id);
99 CRM_Core_BAO_CustomGroup::deleteGroup($group);
100 CRM_Core_Session::setStatus(ts("The Group '%1' has been deleted.", [1 => $group->title]), '', 'success');
101 }
102
103 }