preliminary whitespace cleanup
[civicrm-core.git] / CRM / Custom / Page / Field.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * Create a page for displaying Custom Fields.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44class CRM_Custom_Page_Field extends CRM_Core_Page {
45
46 /**
47 * The group id of the field
48 *
49 * @var int
50 * @access protected
51 */
52 protected $_gid;
53
54 /**
55 * The action links that we need to display for the browse screen
56 *
57 * @var array
58 * @access private
59 */
60 private static $_actionLinks;
61
62 /**
63 * Get the action links for this page.
64 *
65 * @param null
66 *
67 * @return array array of action links that we need to display for the browse screen
68 * @access public
eea16664 69 */
6a488035
TO
70 function &actionLinks() {
71 if (!isset(self::$_actionLinks)) {
72 $deleteExtra = ts('Are you sure you want to delete this custom data field?');
73 self::$_actionLinks = array(
74 CRM_Core_Action::UPDATE => array(
75 'name' => ts('Edit Field'),
76 'url' => 'civicrm/admin/custom/group/field/update',
77 'qs' => 'action=update&reset=1&gid=%%gid%%&id=%%id%%',
78 'title' => ts('Edit Custom Field'),
79 ),
80 CRM_Core_Action::BROWSE => array(
81 'name' => ts('Edit Multiple Choice Options'),
82 'url' => 'civicrm/admin/custom/group/field/option',
83 'qs' => 'reset=1&action=browse&gid=%%gid%%&fid=%%id%%',
84 'title' => ts('List Custom Options'),
85 ),
86 CRM_Core_Action::PREVIEW => array(
87 'name' => ts('Preview Field Display'),
88 'url' => 'civicrm/admin/custom/group/field',
89 'qs' => 'action=preview&reset=1&gid=%%gid%%&id=%%id%%',
90 'title' => ts('Preview Custom Field'),
91 ),
92 CRM_Core_Action::DISABLE => array(
93 'name' => ts('Disable'),
94 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_CustomField' . '\',\'' . 'enable-disable' . '\',0,\'CustomField\' );"',
95 'ref' => 'disable-action',
96 'title' => ts('Disable Custom Field'),
97 ),
98 CRM_Core_Action::ENABLE => array(
99 'name' => ts('Enable'),
100 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_CustomField' . '\',\'' . 'disable-enable' . '\',0,\'CustomField\' );"',
101 'ref' => 'enable-action',
102 'title' => ts('Enable Custom Field'),
103 ),
104 CRM_Core_Action::EXPORT => array(
105 'name' => ts('Move'),
106 'url' => 'civicrm/admin/custom/group/field/move',
107 'qs' => 'reset=1&fid=%%id%%',
108 'title' => ts('Move Custom Field'),
109 ),
110 CRM_Core_Action::DELETE => array(
111 'name' => ts('Delete'),
112 'url' => 'civicrm/admin/custom/group/field',
113 'qs' => 'action=delete&reset=1&gid=%%gid%%&id=%%id%%',
114 'title' => ts('Delete Custom Field'),
115 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
116 ),
117 );
118 }
119 return self::$_actionLinks;
120 }
121
122 /**
123 * Browse all custom group fields.
124 *
125 * @param null
126 *
127 * @return void
128 * @access public
129 */
130 function browse() {
131 $customField = array();
132 $customFieldBAO = new CRM_Core_BAO_CustomField();
133
134 // fkey is gid
135 $customFieldBAO->custom_group_id = $this->_gid;
136 $customFieldBAO->orderBy('weight, label');
137 $customFieldBAO->find();
138
139 while ($customFieldBAO->fetch()) {
140 $customField[$customFieldBAO->id] = array();
141 CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]);
142 $action = array_sum(array_keys($this->actionLinks()));
143 if ($customFieldBAO->is_active) {
144 $action -= CRM_Core_Action::ENABLE;
145 }
146 else {
147 $action -= CRM_Core_Action::DISABLE;
148 }
149
150 switch ($customFieldBAO->data_type) {
151 case "String":
152 case "Int":
153 case "Float":
154 case "Money":
155 // if Multi Select field is selected in custom field
156 if ($customFieldBAO->html_type == 'Text') {
157 $action -= CRM_Core_Action::BROWSE;
158 }
159 break;
160
161 case "ContactReference":
162 case "Memo":
163 case "Date":
164 case "Boolean":
165 case "StateProvince":
166 case "Country":
167 case "File":
168 case "Link":
169 $action -= CRM_Core_Action::BROWSE;
170 break;
171 }
172
173 $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
174 $customField[$customFieldBAO->id]['data_type'] = $customFieldDataType[$customField[$customFieldBAO->id]['data_type']];
175 $customField[$customFieldBAO->id]['order'] = $customField[$customFieldBAO->id]['weight'];
176 $customField[$customFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
177 array(
178 'id' => $customFieldBAO->id,
179 'gid' => $this->_gid,
180 )
181 );
182 }
183
184 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_gid}");
185 $filter = "custom_group_id = {$this->_gid}";
186 CRM_Utils_Weight::addOrder($customField, 'CRM_Core_DAO_CustomField',
187 'id', $returnURL, $filter
188 );
189
190 $this->assign('customField', $customField);
191 }
192
193 /**
194 * edit custom data.
195 *
196 * editing would involved modifying existing fields + adding data to new fields.
197 *
198 * @param string $action the action to be invoked
199 *
200 * @return void
201 * @access public
202 */
203 function edit($action) {
204 // create a simple controller for editing custom dataCRM/Custom/Page/Field.php
205 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Field', ts('Custom Field'), $action);
206
207 // set the userContext stack
208 $session = CRM_Core_Session::singleton();
209 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
210
211 $controller->set('gid', $this->_gid);
212 $controller->setEmbedded(TRUE);
213 $controller->process();
214 $controller->run();
215 }
216
217 /**
218 * Run the page.
219 *
220 * This method is called after the page is created. It checks for the
221 * type of action and executes that action.
222 *
223 * @param null
224 *
225 * @return void
226 * @access public
227 */
228 function run() {
229
230 // get the group id
231 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
232 $this
233 );
234 $action = CRM_Utils_Request::retrieve('action', 'String',
235 // default to 'browse'
236 $this, FALSE, 'browse'
237 );
238
239 if ($action & CRM_Core_Action::DELETE) {
240
241 $session = CRM_Core_Session::singleton();
242 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
243 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteField', "Delete Custom Field", '');
244 $id = CRM_Utils_Request::retrieve('id', 'Positive',
245 $this, FALSE, 0
246 );
247 $controller->set('id', $id);
248 $controller->setEmbedded(TRUE);
249 $controller->process();
250 $controller->run();
251 $fieldValues = array('custom_group_id' => $this->_gid);
252 $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_CustomField', $id, $fieldValues);
253 }
254
255 if ($this->_gid) {
256 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_gid);
257 $this->assign('gid', $this->_gid);
258 $this->assign('groupTitle', $groupTitle);
259 CRM_Utils_System::setTitle(ts('%1 - Custom Fields', array(1 => $groupTitle)));
260 }
261
262 // assign vars to templates
263 $this->assign('action', $action);
264
265 $id = CRM_Utils_Request::retrieve('id', 'Positive',
266 $this, FALSE, 0
267 );
268
269 // what action to take ?
270 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
271 // no browse for edit/update/view
272 $this->edit($action);
273 }
274 elseif ($action & CRM_Core_Action::PREVIEW) {
275 $this->preview($id);
276 }
277 else {
278 $this->browse();
279 }
280
281 // Call the parents run method
282 return parent::run();
283 }
284
285 /**
286 * Preview custom field
287 *
288 * @param int $id custom field id
289 *
290 * @return void
291 * @access public
292 */
293 function preview($id) {
294 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
295 $session = CRM_Core_Session::singleton();
296 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
297 $controller->set('fieldId', $id);
298 $controller->set('groupId', $this->_gid);
299 $controller->setEmbedded(TRUE);
300 $controller->process();
301 $controller->run();
302 }
303}
304