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