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;
32 public $submitOnce = TRUE;
35 * Build the form object.
37 public function buildQuickForm() {
38 parent
::buildQuickForm();
40 $this->setPageTitle(ts('Menu Item'));
42 if ($this->_action
& CRM_Core_Action
::DELETE
) {
46 if (isset($this->_id
)) {
47 $params = ['id' => $this->_id
];
48 CRM_Core_BAO_Navigation
::retrieve($params, $this->_defaults
);
51 $this->applyFilter('__ALL__', 'trim');
55 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Navigation', 'label'),
59 $this->add('text', 'url', ts('Url'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Navigation', 'url'));
61 $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);
63 $getPerms = (array) \Civi\Api4\Permission
::get(0)
64 ->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
65 ->setOrderBy(['title' => 'ASC'])
68 foreach ($getPerms as $perm) {
69 $permissions[] = ['id' => $perm['name'], 'text' => $perm['title'], 'description' => $perm['description'] ??
''];
71 $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
72 ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
75 $operators = ['AND' => ts('AND'), 'OR' => ts('OR')];
76 $this->add('select', 'permission_operator', NULL, $operators);
78 //make separator location configurable
79 $separator = CRM_Core_SelectValues
::navigationMenuSeparator();
80 $this->add('select', 'has_separator', ts('Separator'), $separator);
82 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
84 if (CRM_Utils_Array
::value('name', $this->_defaults
) == 'Home') {
88 $parentMenu = CRM_Core_BAO_Navigation
::getNavigationList();
90 if (isset($this->_id
)) {
91 unset($parentMenu[$this->_id
]);
95 $homeMenuId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
96 unset($parentMenu[$homeMenuId]);
98 $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] +
$parentMenu, FALSE, ['class' => 'crm-select2']);
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;
113 $defaults['permission'] = "access CiviCRM";
116 // its ok if there is no element called is_active
117 $defaults['is_active'] = ($this->_id
) ?
$this->_defaults
['is_active'] : 1;
119 if (!empty($defaults['icon'])) {
120 $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon']));
127 * Process the form submission.
129 public function postProcess() {
130 // get the submitted form values.
131 $params = $this->controller
->exportValues($this->_name
);
133 if (isset($this->_id
)) {
134 $params['id'] = $this->_id
;
135 $params['current_parent_id'] = $this->_currentParentID
;
138 if (!empty($params['icon'])) {
139 $params['icon'] = 'crm-i ' . $params['icon'];
142 $navigation = CRM_Core_BAO_Navigation
::add($params);
144 // also reset navigation
145 CRM_Core_BAO_Navigation
::resetNavigation();
147 CRM_Core_Session
::setStatus(ts('Menu \'%1\' has been saved.',
148 [1 => $navigation->label
]
149 ), ts('Saved'), 'success');