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