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