Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-10-14-11-16-10
[civicrm-core.git] / CRM / Admin / Form / Navigation.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Navigation
38 *
39 */
40 class CRM_Admin_Form_Navigation extends CRM_Admin_Form {
41
42 /**
43 * The parent id of the navigation menu
44 */
45 protected $_currentParentID = NULL;
46
47 /**
48 * Default values
49 */
50 protected $_defaults = array();
51
52 /**
53 * Function to build the form
54 *
55 * @return void
56 * @access public
57 */
58 public function buildQuickForm() {
59 parent::buildQuickForm();
60
61 if ($this->_action & CRM_Core_Action::DELETE) {
62 return;
63 }
64
65 if (isset($this->_id)) {
66 $params = array('id' => $this->_id);
67 CRM_Core_BAO_Navigation::retrieve($params, $this->_defaults);
68 }
69
70 $this->applyFilter('__ALL__', 'trim');
71 $this->add('text',
72 'label',
73 ts('Title'),
74 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'label'),
75 TRUE
76 );
77
78 $this->add('text', 'url', ts('Url'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'url'));
79 $permissions = CRM_Core_Permission::basicPermissions(TRUE);
80 $include = &$this->addElement('advmultiselect', 'permission',
81 ts('Permission') . ' ', $permissions,
82 array(
83 'size' => 5,
84 'style' => 'width:auto',
85 'class' => 'advmultiselect',
86 )
87 );
88
89 $include->setButtonAttributes('add', array('value' => ts('Add >>')));
90 $include->setButtonAttributes('remove', array('value' => ts('<< Remove')));
91
92 $operators = array('AND' => 'AND', 'OR' => 'OR');
93 $this->add('select', 'permission_operator', ts('Operator'), $operators);
94
95 //make separator location configurable
96 $separator = array(0 => 'None', 1 => 'After Menu Element', 2 => 'Before Menu Element');
97 $this->add('select', 'has_separator', ts('Separator?'), $separator);
98
99 $active = $this->add('checkbox', 'is_active', ts('Enabled?'));
100
101 if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') {
102 $active->freeze();
103 }
104 else {
105 $parentMenu = CRM_Core_BAO_Navigation::getNavigationList();
106
107 if (isset($this->_id)) {
108 unset($parentMenu[$this->_id]);
109 }
110
111 // also unset home.
112 $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name');
113 unset($parentMenu[$homeMenuId]);
114
115 $parent = $this->add('select', 'parent_id', ts('Parent'), array('' => ts('-- select --')) + $parentMenu);
116 }
117 }
118
119 /**
120 * @return array
121 */
122 public function setDefaultValues() {
123 $defaults = $this->_defaults;
124 if (isset($this->_id)) {
125 if (!empty($this->_defaults['permission'])) {
126 foreach (explode(',', $this->_defaults['permission']) as $value) {
127 $components[$value] = $value;
128 }
129 $defaults['permission'] = $components;
130 }
131 //Take parent id in object variable to calculate the menu
132 //weight if menu parent id changed
133 $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults);
134 }
135 else {
136 $defaults['permission'] = "access CiviCRM";
137 }
138
139 // its ok if there is no element called is_active
140 $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1;
141
142 return $defaults;
143 }
144
145 /**
146 * Function to process the form
147 *
148 * @access public
149 *
150 * @return void
151 */
152 public function postProcess() {
153 // get the submitted form values.
154 $params = $this->controller->exportValues($this->_name);
155
156 if (isset($this->_id)) {
157 $params['id'] = $this->_id;
158 $params['current_parent_id'] = $this->_currentParentID;
159 }
160
161 $navigation = CRM_Core_BAO_Navigation::add($params);
162
163 // also reset navigation
164 CRM_Core_BAO_Navigation::resetNavigation();
165
166 CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.',
167 array(1 => $navigation->label)
168 ), ts('Saved'), 'success');
169 }
170 }