Merge pull request #15566 from civicrm/5.19
[civicrm-core.git] / CRM / Admin / Form / Navigation.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for Navigation.
36 */
37 class CRM_Admin_Form_Navigation extends CRM_Admin_Form {
38
39 /**
40 * The parent id of the navigation menu.
41 * @var int
42 */
43 protected $_currentParentID = NULL;
44
45 /**
46 * Build the form object.
47 */
48 public function buildQuickForm() {
49 parent::buildQuickForm();
50
51 $this->setPageTitle(ts('Menu Item'));
52
53 if ($this->_action & CRM_Core_Action::DELETE) {
54 return;
55 }
56
57 if (isset($this->_id)) {
58 $params = ['id' => $this->_id];
59 CRM_Core_BAO_Navigation::retrieve($params, $this->_defaults);
60 }
61
62 $this->applyFilter('__ALL__', 'trim');
63 $this->add('text',
64 'label',
65 ts('Title'),
66 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'label'),
67 TRUE
68 );
69
70 $this->add('text', 'url', ts('Url'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'url'));
71
72 $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);
73
74 $permissions = [];
75 foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) {
76 $permissions[] = ['id' => $id, 'text' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)];
77 }
78 $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
79 ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
80 );
81
82 $operators = ['AND' => ts('AND'), 'OR' => ts('OR')];
83 $this->add('select', 'permission_operator', NULL, $operators);
84
85 //make separator location configurable
86 $separator = [ts('None'), ts('After menu element'), ts('Before menu element')];
87 $this->add('select', 'has_separator', ts('Separator'), $separator);
88
89 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
90
91 if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') {
92 $active->freeze();
93 }
94 else {
95 $parentMenu = CRM_Core_BAO_Navigation::getNavigationList();
96
97 if (isset($this->_id)) {
98 unset($parentMenu[$this->_id]);
99 }
100
101 // also unset home.
102 $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
103 unset($parentMenu[$homeMenuId]);
104
105 $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] + $parentMenu, FALSE, ['class' => 'crm-select2']);
106 }
107 }
108
109 /**
110 * @return array
111 */
112 public function setDefaultValues() {
113 $defaults = parent::setDefaultValues();
114 if (isset($this->_id)) {
115 //Take parent id in object variable to calculate the menu
116 //weight if menu parent id changed
117 $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults);
118 }
119 else {
120 $defaults['permission'] = "access CiviCRM";
121 }
122
123 // its ok if there is no element called is_active
124 $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1;
125
126 if (!empty($defaults['icon'])) {
127 $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon']));
128 }
129
130 return $defaults;
131 }
132
133 /**
134 * Process the form submission.
135 */
136 public function postProcess() {
137 // get the submitted form values.
138 $params = $this->controller->exportValues($this->_name);
139
140 if (isset($this->_id)) {
141 $params['id'] = $this->_id;
142 $params['current_parent_id'] = $this->_currentParentID;
143 }
144
145 if (!empty($params['icon'])) {
146 $params['icon'] = 'crm-i ' . $params['icon'];
147 }
148
149 $navigation = CRM_Core_BAO_Navigation::add($params);
150
151 // also reset navigation
152 CRM_Core_BAO_Navigation::resetNavigation();
153
154 CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.',
155 [1 => $navigation->label]
156 ), ts('Saved'), 'success');
157 }
158
159 }