Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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. |
6a488035 TO |
41 | */ |
42 | protected $_currentParentID = NULL; | |
43 | ||
6a488035 | 44 | /** |
eceb18cc | 45 | * Build the form object. |
6a488035 TO |
46 | */ |
47 | public function buildQuickForm() { | |
48 | parent::buildQuickForm(); | |
49 | ||
2c8776f0 CW |
50 | $this->setPageTitle(ts('Menu Item')); |
51 | ||
6a488035 TO |
52 | if ($this->_action & CRM_Core_Action::DELETE) { |
53 | return; | |
54 | } | |
55 | ||
56 | if (isset($this->_id)) { | |
57 | $params = array('id' => $this->_id); | |
58 | CRM_Core_BAO_Navigation::retrieve($params, $this->_defaults); | |
59 | } | |
60 | ||
61 | $this->applyFilter('__ALL__', 'trim'); | |
62 | $this->add('text', | |
63 | 'label', | |
64 | ts('Title'), | |
65 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'label'), | |
66 | TRUE | |
67 | ); | |
68 | ||
69 | $this->add('text', 'url', ts('Url'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'url')); | |
4926ede8 | 70 | |
2b2c93f0 | 71 | $this->add('text', 'icon', ts('Icon'), array('class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE)); |
4926ede8 | 72 | |
2c8776f0 CW |
73 | $permissions = array(); |
74 | foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) { | |
75 | $permissions[] = array('id' => $id, 'label' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)); | |
76 | } | |
77 | $this->add('text', 'permission', ts('Permission'), | |
d78feb62 | 78 | array('placeholder' => ts('Unrestricted'), 'class' => 'huge', 'data-select-params' => json_encode(array('data' => array('results' => $permissions, 'text' => 'label')))) |
6a488035 TO |
79 | ); |
80 | ||
2c8776f0 CW |
81 | $operators = array('AND' => ts('AND'), 'OR' => ts('OR')); |
82 | $this->add('select', 'permission_operator', NULL, $operators); | |
6a488035 TO |
83 | |
84 | //make separator location configurable | |
2c8776f0 CW |
85 | $separator = array(ts('None'), ts('After menu element'), ts('Before menu element')); |
86 | $this->add('select', 'has_separator', ts('Separator'), $separator); | |
8ef12e64 | 87 | |
2c8776f0 | 88 | $active = $this->add('advcheckbox', 'is_active', ts('Enabled')); |
6a488035 TO |
89 | |
90 | if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') { | |
91 | $active->freeze(); | |
92 | } | |
93 | else { | |
94 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); | |
95 | ||
96 | if (isset($this->_id)) { | |
97 | unset($parentMenu[$this->_id]); | |
98 | } | |
99 | ||
100 | // also unset home. | |
101 | $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name'); | |
102 | unset($parentMenu[$homeMenuId]); | |
103 | ||
2c8776f0 | 104 | $this->add('select', 'parent_id', ts('Parent'), array('' => ts('Top level')) + $parentMenu, FALSE, array('class' => 'crm-select2')); |
6a488035 TO |
105 | } |
106 | } | |
107 | ||
e0ef6999 EM |
108 | /** |
109 | * @return array | |
110 | */ | |
6a488035 | 111 | public function setDefaultValues() { |
656e5c5b | 112 | $defaults = parent::setDefaultValues(); |
6a488035 | 113 | if (isset($this->_id)) { |
6a488035 TO |
114 | //Take parent id in object variable to calculate the menu |
115 | //weight if menu parent id changed | |
116 | $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults); | |
117 | } | |
118 | else { | |
119 | $defaults['permission'] = "access CiviCRM"; | |
120 | } | |
121 | ||
122 | // its ok if there is no element called is_active | |
123 | $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1; | |
124 | ||
125 | return $defaults; | |
126 | } | |
127 | ||
128 | /** | |
eceb18cc | 129 | * Process the form submission. |
6a488035 TO |
130 | */ |
131 | public function postProcess() { | |
132 | // get the submitted form values. | |
133 | $params = $this->controller->exportValues($this->_name); | |
134 | ||
135 | if (isset($this->_id)) { | |
136 | $params['id'] = $this->_id; | |
137 | $params['current_parent_id'] = $this->_currentParentID; | |
138 | } | |
139 | ||
a2a3cc46 CW |
140 | if (!empty($params['icon'])) { |
141 | $params['icon'] = 'crm-i ' . $params['icon']; | |
142 | } | |
143 | ||
6a488035 TO |
144 | $navigation = CRM_Core_BAO_Navigation::add($params); |
145 | ||
146 | // also reset navigation | |
147 | CRM_Core_BAO_Navigation::resetNavigation(); | |
148 | ||
149 | CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.', | |
353ffa53 TO |
150 | array(1 => $navigation->label) |
151 | ), ts('Saved'), 'success'); | |
6a488035 | 152 | } |
96025800 | 153 | |
6a488035 | 154 | } |