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) { |
be2fb01f | 76 | $permissions[] = ['id' => $id, 'label' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)]; |
2c8776f0 CW |
77 | } |
78 | $this->add('text', 'permission', ts('Permission'), | |
be2fb01f | 79 | ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'data-select-params' => json_encode(['data' => ['results' => $permissions, 'text' => 'label']])] |
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 | ||
126 | return $defaults; | |
127 | } | |
128 | ||
129 | /** | |
eceb18cc | 130 | * Process the form submission. |
6a488035 TO |
131 | */ |
132 | public function postProcess() { | |
133 | // get the submitted form values. | |
134 | $params = $this->controller->exportValues($this->_name); | |
135 | ||
136 | if (isset($this->_id)) { | |
137 | $params['id'] = $this->_id; | |
138 | $params['current_parent_id'] = $this->_currentParentID; | |
139 | } | |
140 | ||
a2a3cc46 CW |
141 | if (!empty($params['icon'])) { |
142 | $params['icon'] = 'crm-i ' . $params['icon']; | |
143 | } | |
144 | ||
6a488035 TO |
145 | $navigation = CRM_Core_BAO_Navigation::add($params); |
146 | ||
147 | // also reset navigation | |
148 | CRM_Core_BAO_Navigation::resetNavigation(); | |
149 | ||
150 | CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.', | |
be2fb01f | 151 | [1 => $navigation->label] |
353ffa53 | 152 | ), ts('Saved'), 'success'); |
6a488035 | 153 | } |
96025800 | 154 | |
6a488035 | 155 | } |