Set version to 5.51.alpha1
[civicrm-core.git] / CRM / Admin / Form / Navigation.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
ce064e4f 19 * This class generates form components for Navigation.
6a488035
TO
20 */
21class CRM_Admin_Form_Navigation extends CRM_Admin_Form {
22
23 /**
eceb18cc 24 * The parent id of the navigation menu.
62d3ee27 25 * @var int
6a488035
TO
26 */
27 protected $_currentParentID = NULL;
28
88aae6d4
A
29 /**
30 * @var bool
31 */
32 public $submitOnce = TRUE;
33
6a488035 34 /**
eceb18cc 35 * Build the form object.
6a488035
TO
36 */
37 public function buildQuickForm() {
38 parent::buildQuickForm();
39
2c8776f0
CW
40 $this->setPageTitle(ts('Menu Item'));
41
6a488035
TO
42 if ($this->_action & CRM_Core_Action::DELETE) {
43 return;
44 }
45
46 if (isset($this->_id)) {
be2fb01f 47 $params = ['id' => $this->_id];
6a488035
TO
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'));
4926ede8 60
be2fb01f 61 $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);
4926ede8 62
15ee4ffd
TO
63 $getPerms = (array) \Civi\Api4\Permission::get(0)
64 ->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
7efb95e7 65 ->setOrderBy(['title' => 'ASC'])
15ee4ffd 66 ->execute();
be2fb01f 67 $permissions = [];
15ee4ffd
TO
68 foreach ($getPerms as $perm) {
69 $permissions[] = ['id' => $perm['name'], 'text' => $perm['title'], 'description' => $perm['description'] ?? ''];
2c8776f0 70 }
11b9f280
CW
71 $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
72 ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
6a488035
TO
73 );
74
be2fb01f 75 $operators = ['AND' => ts('AND'), 'OR' => ts('OR')];
2c8776f0 76 $this->add('select', 'permission_operator', NULL, $operators);
6a488035
TO
77
78 //make separator location configurable
e0674c37 79 $separator = CRM_Core_SelectValues::navigationMenuSeparator();
2c8776f0 80 $this->add('select', 'has_separator', ts('Separator'), $separator);
8ef12e64 81
2c8776f0 82 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
6a488035
TO
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
be2fb01f 98 $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] + $parentMenu, FALSE, ['class' => 'crm-select2']);
6a488035
TO
99 }
100 }
101
e0ef6999
EM
102 /**
103 * @return array
104 */
6a488035 105 public function setDefaultValues() {
656e5c5b 106 $defaults = parent::setDefaultValues();
6a488035 107 if (isset($this->_id)) {
6a488035
TO
108 //Take parent id in object variable to calculate the menu
109 //weight if menu parent id changed
9c1bc317 110 $this->_currentParentID = $this->_defaults['parent_id'] ?? NULL;
6a488035
TO
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
11b9f280
CW
119 if (!empty($defaults['icon'])) {
120 $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon']));
121 }
122
6a488035
TO
123 return $defaults;
124 }
125
126 /**
eceb18cc 127 * Process the form submission.
6a488035
TO
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
a2a3cc46
CW
138 if (!empty($params['icon'])) {
139 $params['icon'] = 'crm-i ' . $params['icon'];
140 }
141
6a488035
TO
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.',
be2fb01f 148 [1 => $navigation->label]
353ffa53 149 ), ts('Saved'), 'success');
6a488035 150 }
96025800 151
6a488035 152}