Merge pull request #9769 from scardinius/crm-19958
[civicrm-core.git] / CRM / Admin / Form / Navigation.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 */
42 protected $_currentParentID = NULL;
43
44 /**
45 * Build the form object.
46 */
47 public function buildQuickForm() {
48 parent::buildQuickForm();
49
50 $this->setPageTitle(ts('Menu Item'));
51
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'));
70 $permissions = array();
71 foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) {
72 $permissions[] = array('id' => $id, 'label' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals));
73 }
74 $this->add('text', 'permission', ts('Permission'),
75 array('placeholder' => ts('Unrestricted'), 'class' => 'huge', 'data-select-params' => json_encode(array('data' => array('results' => $permissions, 'text' => 'label'))))
76 );
77
78 $operators = array('AND' => ts('AND'), 'OR' => ts('OR'));
79 $this->add('select', 'permission_operator', NULL, $operators);
80
81 //make separator location configurable
82 $separator = array(ts('None'), ts('After menu element'), ts('Before menu element'));
83 $this->add('select', 'has_separator', ts('Separator'), $separator);
84
85 $active = $this->add('advcheckbox', 'is_active', ts('Enabled'));
86
87 if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') {
88 $active->freeze();
89 }
90 else {
91 $parentMenu = CRM_Core_BAO_Navigation::getNavigationList();
92
93 if (isset($this->_id)) {
94 unset($parentMenu[$this->_id]);
95 }
96
97 // also unset home.
98 $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
99 unset($parentMenu[$homeMenuId]);
100
101 $this->add('select', 'parent_id', ts('Parent'), array('' => ts('Top level')) + $parentMenu, FALSE, array('class' => 'crm-select2'));
102 }
103 }
104
105 /**
106 * @return array
107 */
108 public function setDefaultValues() {
109 $defaults = parent::setDefaultValues();
110 if (isset($this->_id)) {
111 //Take parent id in object variable to calculate the menu
112 //weight if menu parent id changed
113 $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults);
114 }
115 else {
116 $defaults['permission'] = "access CiviCRM";
117 }
118
119 // its ok if there is no element called is_active
120 $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1;
121
122 return $defaults;
123 }
124
125 /**
126 * Process the form submission.
127 */
128 public function postProcess() {
129 // get the submitted form values.
130 $params = $this->controller->exportValues($this->_name);
131
132 if (isset($this->_id)) {
133 $params['id'] = $this->_id;
134 $params['current_parent_id'] = $this->_currentParentID;
135 }
136
137 $navigation = CRM_Core_BAO_Navigation::add($params);
138
139 // also reset navigation
140 CRM_Core_BAO_Navigation::resetNavigation();
141
142 CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.',
143 array(1 => $navigation->label)
144 ), ts('Saved'), 'success');
145 }
146
147 }