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