3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Navigation.
21 class CRM_Admin_Form_Navigation
extends CRM_Admin_Form
{
24 * The parent id of the navigation menu.
27 protected $_currentParentID = NULL;
30 * Build the form object.
32 public function buildQuickForm() {
33 parent
::buildQuickForm();
35 $this->setPageTitle(ts('Menu Item'));
37 if ($this->_action
& CRM_Core_Action
::DELETE
) {
41 if (isset($this->_id
)) {
42 $params = ['id' => $this->_id
];
43 CRM_Core_BAO_Navigation
::retrieve($params, $this->_defaults
);
46 $this->applyFilter('__ALL__', 'trim');
50 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Navigation', 'label'),
54 $this->add('text', 'url', ts('Url'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Navigation', 'url'));
56 $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);
58 $getPerms = (array) \Civi\Api4\Permission
::get(0)
59 ->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
60 ->setOrderBy(['group' => 'ASC', 'name' => 'ASC'])
63 foreach ($getPerms as $perm) {
64 $permissions[] = ['id' => $perm['name'], 'text' => $perm['title'], 'description' => $perm['description'] ??
''];
66 $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
67 ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
70 $operators = ['AND' => ts('AND'), 'OR' => ts('OR')];
71 $this->add('select', 'permission_operator', NULL, $operators);
73 //make separator location configurable
74 $separator = CRM_Core_SelectValues
::navigationMenuSeparator();
75 $this->add('select', 'has_separator', ts('Separator'), $separator);
77 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
79 if (CRM_Utils_Array
::value('name', $this->_defaults
) == 'Home') {
83 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
85 if (isset($this->_id
)) {
86 unset($parentMenu[$this->_id
]);
90 $homeMenuId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
91 unset($parentMenu[$homeMenuId]);
93 $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] +
$parentMenu, FALSE, ['class' => 'crm-select2']);
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;
108 $defaults['permission'] = "access CiviCRM";
111 // its ok if there is no element called is_active
112 $defaults['is_active'] = ($this->_id
) ?
$this->_defaults
['is_active'] : 1;
114 if (!empty($defaults['icon'])) {
115 $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon']));
122 * Process the form submission.
124 public function postProcess() {
125 // get the submitted form values.
126 $params = $this->controller
->exportValues($this->_name
);
128 if (isset($this->_id
)) {
129 $params['id'] = $this->_id
;
130 $params['current_parent_id'] = $this->_currentParentID
;
133 if (!empty($params['icon'])) {
134 $params['icon'] = 'crm-i ' . $params['icon'];
137 $navigation = CRM_Core_BAO_Navigation
::add($params);
139 // also reset navigation
140 CRM_Core_BAO_Navigation
::resetNavigation();
142 CRM_Core_Session
::setStatus(ts('Menu \'%1\' has been saved.',
143 [1 => $navigation->label
]
144 ), ts('Saved'), 'success');