Merge pull request #16028 from civicrm/5.20
[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
6a488035 29 /**
eceb18cc 30 * Build the form object.
6a488035
TO
31 */
32 public function buildQuickForm() {
33 parent::buildQuickForm();
34
2c8776f0
CW
35 $this->setPageTitle(ts('Menu Item'));
36
6a488035
TO
37 if ($this->_action & CRM_Core_Action::DELETE) {
38 return;
39 }
40
41 if (isset($this->_id)) {
be2fb01f 42 $params = ['id' => $this->_id];
6a488035
TO
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'));
4926ede8 55
be2fb01f 56 $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);
4926ede8 57
be2fb01f 58 $permissions = [];
2c8776f0 59 foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) {
11b9f280 60 $permissions[] = ['id' => $id, 'text' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)];
2c8776f0 61 }
11b9f280
CW
62 $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
63 ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
6a488035
TO
64 );
65
be2fb01f 66 $operators = ['AND' => ts('AND'), 'OR' => ts('OR')];
2c8776f0 67 $this->add('select', 'permission_operator', NULL, $operators);
6a488035
TO
68
69 //make separator location configurable
be2fb01f 70 $separator = [ts('None'), ts('After menu element'), ts('Before menu element')];
2c8776f0 71 $this->add('select', 'has_separator', ts('Separator'), $separator);
8ef12e64 72
2c8776f0 73 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
6a488035
TO
74
75 if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') {
76 $active->freeze();
77 }
78 else {
79 $parentMenu = CRM_Core_BAO_Navigation::getNavigationList();
80
81 if (isset($this->_id)) {
82 unset($parentMenu[$this->_id]);
83 }
84
85 // also unset home.
86 $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
87 unset($parentMenu[$homeMenuId]);
88
be2fb01f 89 $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] + $parentMenu, FALSE, ['class' => 'crm-select2']);
6a488035
TO
90 }
91 }
92
e0ef6999
EM
93 /**
94 * @return array
95 */
6a488035 96 public function setDefaultValues() {
656e5c5b 97 $defaults = parent::setDefaultValues();
6a488035 98 if (isset($this->_id)) {
6a488035
TO
99 //Take parent id in object variable to calculate the menu
100 //weight if menu parent id changed
101 $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults);
102 }
103 else {
104 $defaults['permission'] = "access CiviCRM";
105 }
106
107 // its ok if there is no element called is_active
108 $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1;
109
11b9f280
CW
110 if (!empty($defaults['icon'])) {
111 $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon']));
112 }
113
6a488035
TO
114 return $defaults;
115 }
116
117 /**
eceb18cc 118 * Process the form submission.
6a488035
TO
119 */
120 public function postProcess() {
121 // get the submitted form values.
122 $params = $this->controller->exportValues($this->_name);
123
124 if (isset($this->_id)) {
125 $params['id'] = $this->_id;
126 $params['current_parent_id'] = $this->_currentParentID;
127 }
128
a2a3cc46
CW
129 if (!empty($params['icon'])) {
130 $params['icon'] = 'crm-i ' . $params['icon'];
131 }
132
6a488035
TO
133 $navigation = CRM_Core_BAO_Navigation::add($params);
134
135 // also reset navigation
136 CRM_Core_BAO_Navigation::resetNavigation();
137
138 CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.',
be2fb01f 139 [1 => $navigation->label]
353ffa53 140 ), ts('Saved'), 'success');
6a488035 141 }
96025800 142
6a488035 143}