Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * This class generates form components for Navigation. |
6a488035 TO |
36 | */ |
37 | class CRM_Admin_Form_Navigation extends CRM_Admin_Form { | |
38 | ||
39 | /** | |
eceb18cc | 40 | * The parent id of the navigation menu. |
62d3ee27 | 41 | * @var int |
6a488035 TO |
42 | */ |
43 | protected $_currentParentID = NULL; | |
44 | ||
6a488035 | 45 | /** |
eceb18cc | 46 | * Build the form object. |
6a488035 TO |
47 | */ |
48 | public function buildQuickForm() { | |
49 | parent::buildQuickForm(); | |
50 | ||
2c8776f0 CW |
51 | $this->setPageTitle(ts('Menu Item')); |
52 | ||
6a488035 TO |
53 | if ($this->_action & CRM_Core_Action::DELETE) { |
54 | return; | |
55 | } | |
56 | ||
57 | if (isset($this->_id)) { | |
be2fb01f | 58 | $params = ['id' => $this->_id]; |
6a488035 TO |
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')); | |
4926ede8 | 71 | |
be2fb01f | 72 | $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]); |
4926ede8 | 73 | |
be2fb01f | 74 | $permissions = []; |
2c8776f0 | 75 | foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) { |
11b9f280 | 76 | $permissions[] = ['id' => $id, 'text' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)]; |
2c8776f0 | 77 | } |
11b9f280 CW |
78 | $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE, |
79 | ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE] | |
6a488035 TO |
80 | ); |
81 | ||
be2fb01f | 82 | $operators = ['AND' => ts('AND'), 'OR' => ts('OR')]; |
2c8776f0 | 83 | $this->add('select', 'permission_operator', NULL, $operators); |
6a488035 TO |
84 | |
85 | //make separator location configurable | |
be2fb01f | 86 | $separator = [ts('None'), ts('After menu element'), ts('Before menu element')]; |
2c8776f0 | 87 | $this->add('select', 'has_separator', ts('Separator'), $separator); |
8ef12e64 | 88 | |
2c8776f0 | 89 | $active = $this->add('advcheckbox', 'is_active', ts('Enabled')); |
6a488035 TO |
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 | ||
be2fb01f | 105 | $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] + $parentMenu, FALSE, ['class' => 'crm-select2']); |
6a488035 TO |
106 | } |
107 | } | |
108 | ||
e0ef6999 EM |
109 | /** |
110 | * @return array | |
111 | */ | |
6a488035 | 112 | public function setDefaultValues() { |
656e5c5b | 113 | $defaults = parent::setDefaultValues(); |
6a488035 | 114 | if (isset($this->_id)) { |
6a488035 TO |
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 | ||
11b9f280 CW |
126 | if (!empty($defaults['icon'])) { |
127 | $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon'])); | |
128 | } | |
129 | ||
6a488035 TO |
130 | return $defaults; |
131 | } | |
132 | ||
133 | /** | |
eceb18cc | 134 | * Process the form submission. |
6a488035 TO |
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 | ||
a2a3cc46 CW |
145 | if (!empty($params['icon'])) { |
146 | $params['icon'] = 'crm-i ' . $params['icon']; | |
147 | } | |
148 | ||
6a488035 TO |
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.', | |
be2fb01f | 155 | [1 => $navigation->label] |
353ffa53 | 156 | ), ts('Saved'), 'success'); |
6a488035 | 157 | } |
96025800 | 158 | |
6a488035 | 159 | } |