Merge pull request #24148 from civicrm/5.52
[civicrm-core.git] / CRM / Admin / Form / Navigation.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 */
17
18 /**
19 * This class generates form components for Navigation.
20 */
21 class CRM_Admin_Form_Navigation extends CRM_Admin_Form {
22
23 /**
24 * The parent id of the navigation menu.
25 * @var int
26 */
27 protected $_currentParentID = NULL;
28
29 /**
30 * @var bool
31 */
32 public $submitOnce = TRUE;
33
34 /**
35 * Build the form object.
36 */
37 public function buildQuickForm() {
38 parent::buildQuickForm();
39
40 $this->setPageTitle(ts('Menu Item'));
41
42 if ($this->_action & CRM_Core_Action::DELETE) {
43 return;
44 }
45
46 if (isset($this->_id)) {
47 $params = ['id' => $this->_id];
48 CRM_Core_BAO_Navigation::retrieve($params, $this->_defaults);
49 }
50
51 $this->applyFilter('__ALL__', 'trim');
52 $this->add('text',
53 'label',
54 ts('Title'),
55 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'label'),
56 TRUE
57 );
58
59 $this->add('text', 'url', ts('Url'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'url'));
60
61 $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);
62
63 $getPerms = (array) \Civi\Api4\Permission::get(0)
64 ->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
65 ->setOrderBy(['title' => 'ASC'])
66 ->execute();
67 $permissions = [];
68 foreach ($getPerms as $perm) {
69 $permissions[] = ['id' => $perm['name'], 'text' => $perm['title'], 'description' => $perm['description'] ?? ''];
70 }
71 $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
72 ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
73 );
74
75 $operators = ['AND' => ts('AND'), 'OR' => ts('OR')];
76 $this->add('select', 'permission_operator', NULL, $operators);
77
78 //make separator location configurable
79 $separator = CRM_Core_SelectValues::navigationMenuSeparator();
80 $this->add('select', 'has_separator', ts('Separator'), $separator);
81
82 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
83
84 if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') {
85 $active->freeze();
86 }
87 else {
88 $parentMenu = CRM_Core_BAO_Navigation::getNavigationList();
89
90 if (isset($this->_id)) {
91 unset($parentMenu[$this->_id]);
92 }
93
94 // also unset home.
95 $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
96 unset($parentMenu[$homeMenuId]);
97
98 $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] + $parentMenu, FALSE, ['class' => 'crm-select2']);
99 }
100 }
101
102 /**
103 * @return array
104 */
105 public function setDefaultValues() {
106 $defaults = parent::setDefaultValues();
107 if (isset($this->_id)) {
108 //Take parent id in object variable to calculate the menu
109 //weight if menu parent id changed
110 $this->_currentParentID = $this->_defaults['parent_id'] ?? NULL;
111 }
112 else {
113 $defaults['permission'] = "access CiviCRM";
114 }
115
116 // its ok if there is no element called is_active
117 $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1;
118
119 if (!empty($defaults['icon'])) {
120 $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon']));
121 }
122
123 return $defaults;
124 }
125
126 /**
127 * Process the form submission.
128 */
129 public function postProcess() {
130 // get the submitted form values.
131 $params = $this->controller->exportValues($this->_name);
132
133 if (isset($this->_id)) {
134 $params['id'] = $this->_id;
135 $params['current_parent_id'] = $this->_currentParentID;
136 }
137
138 if (!empty($params['icon'])) {
139 $params['icon'] = 'crm-i ' . $params['icon'];
140 }
141
142 $navigation = CRM_Core_BAO_Navigation::add($params);
143
144 // also reset navigation
145 CRM_Core_BAO_Navigation::resetNavigation();
146
147 CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.',
148 [1 => $navigation->label]
149 ), ts('Saved'), 'success');
150 }
151
152 }