further comment fixes
[civicrm-core.git] / CRM / Admin / Form / Navigation.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 if ($this->_action & CRM_Core_Action::DELETE) {
51 return;
52 }
53
54 if (isset($this->_id)) {
55 $params = array('id' => $this->_id);
56 CRM_Core_BAO_Navigation::retrieve($params, $this->_defaults);
57 }
58
59 $this->applyFilter('__ALL__', 'trim');
60 $this->add('text',
61 'label',
62 ts('Title'),
63 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'label'),
64 TRUE
65 );
66
67 $this->add('text', 'url', ts('Url'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'url'));
68 $permissions = CRM_Core_Permission::basicPermissions(TRUE);
69 $include = &$this->addElement('advmultiselect', 'permission',
70 ts('Permission') . ' ', $permissions,
71 array(
72 'size' => 5,
73 'style' => 'width:auto',
74 'class' => 'advmultiselect',
75 )
76 );
77
78 $include->setButtonAttributes('add', array('value' => ts('Add >>')));
79 $include->setButtonAttributes('remove', array('value' => ts('<< Remove')));
80
81 $operators = array('AND' => 'AND', 'OR' => 'OR');
82 $this->add('select', 'permission_operator', ts('Operator'), $operators);
83
84 //make separator location configurable
85 $separator = array(0 => 'None', 1 => 'After Menu Element', 2 => 'Before Menu Element');
86 $this->add('select', 'has_separator', ts('Separator?'), $separator);
87
88 $active = $this->add('checkbox', 'is_active', ts('Enabled?'));
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
104 $parent = $this->add('select', 'parent_id', ts('Parent'), array('' => ts('- select -')) + $parentMenu);
105 }
106 }
107
108 /**
109 * @return array
110 */
111 public function setDefaultValues() {
112 $defaults = $this->_defaults;
113 if (isset($this->_id)) {
114 if (!empty($this->_defaults['permission'])) {
115 foreach (explode(',', $this->_defaults['permission']) as $value) {
116 $components[$value] = $value;
117 }
118 $defaults['permission'] = $components;
119 }
120 //Take parent id in object variable to calculate the menu
121 //weight if menu parent id changed
122 $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults);
123 }
124 else {
125 $defaults['permission'] = "access CiviCRM";
126 }
127
128 // its ok if there is no element called is_active
129 $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1;
130
131 return $defaults;
132 }
133
134 /**
135 * Process the form submission.
136 */
137 public function postProcess() {
138 // get the submitted form values.
139 $params = $this->controller->exportValues($this->_name);
140
141 if (isset($this->_id)) {
142 $params['id'] = $this->_id;
143 $params['current_parent_id'] = $this->_currentParentID;
144 }
145
146 $navigation = CRM_Core_BAO_Navigation::add($params);
147
148 // also reset navigation
149 CRM_Core_BAO_Navigation::resetNavigation();
150
151 CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.',
152 array(1 => $navigation->label)
153 ), ts('Saved'), 'success');
154 }
155
156 }