3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Create a page for displaying Custom Fields.
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
44 class CRM_Custom_Page_Field
extends CRM_Core_Page
{
46 public $useLivePageJS = TRUE;
49 * The group id of the field
57 * The action links that we need to display for the browse screen
62 private static $_actionLinks;
65 * Get the action links for this page.
69 * @return array array of action links that we need to display for the browse screen
72 function &actionLinks() {
73 if (!isset(self
::$_actionLinks)) {
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'),
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'),
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'),
93 CRM_Core_Action
::DISABLE
=> array(
94 'name' => ts('Disable'),
95 'ref' => 'crm-enable-disable',
96 'title' => ts('Disable Custom Field'),
98 CRM_Core_Action
::ENABLE
=> array(
99 'name' => ts('Enable'),
100 'ref' => 'crm-enable-disable',
101 'title' => ts('Enable Custom Field'),
103 CRM_Core_Action
::EXPORT
=> array(
104 'name' => ts('Move'),
105 'url' => 'civicrm/admin/custom/group/field/move',
106 'class' => 'small-popup',
107 'qs' => 'reset=1&fid=%%id%%',
108 'title' => ts('Move Custom Field'),
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'),
118 return self
::$_actionLinks;
122 * Browse all custom group fields.
130 $customField = array();
131 $customFieldBAO = new CRM_Core_BAO_CustomField();
134 $customFieldBAO->custom_group_id
= $this->_gid
;
135 $customFieldBAO->orderBy('weight, label');
136 $customFieldBAO->find();
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
;
146 $action -= CRM_Core_Action
::DISABLE
;
149 switch ($customFieldBAO->data_type
) {
154 // if Multi Select field is selected in custom field
155 if ($customFieldBAO->html_type
== 'Text') {
156 $action -= CRM_Core_Action
::BROWSE
;
160 case "ContactReference":
164 case "StateProvince":
168 $action -= CRM_Core_Action
::BROWSE
;
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,
177 'id' => $customFieldBAO->id
,
178 'gid' => $this->_gid
,
182 'customField.row.actions',
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
194 $this->assign('customField', $customField);
200 * editing would involved modifying existing fields + adding data to new fields.
202 * @param string $action the action to be invoked
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);
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
));
215 $controller->set('gid', $this->_gid
);
216 $controller->setEmbedded(TRUE);
217 $controller->process();
224 * This method is called after the page is created. It checks for the
225 * type of action and executes that action.
235 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
240 $values = civicrm_api3('custom_field', 'getsingle', array('id' => $id));
241 $this->_gid
= $values['custom_group_id'];
245 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
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.");
254 $action = CRM_Utils_Request
::retrieve('action', 'String',
255 // default to 'browse'
256 $this, FALSE, 'browse'
259 if ($action & CRM_Core_Action
::DELETE
) {
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',
267 $controller->set('id', $id);
268 $controller->setEmbedded(TRUE);
269 $controller->process();
271 $fieldValues = array('custom_group_id' => $this->_gid
);
272 $wt = CRM_Utils_Weight
::delWeight('CRM_Core_DAO_CustomField', $id, $fieldValues);
276 $groupTitle = CRM_Core_BAO_CustomGroup
::getTitle($this->_gid
);
277 $this->assign('gid', $this->_gid
);
278 $this->assign('groupTitle', $groupTitle);
279 if ($action & CRM_Core_Action
::BROWSE
) {
280 CRM_Utils_System
::setTitle(ts('%1 - Custom Fields', array(1 => $groupTitle)));
284 // assign vars to templates
285 $this->assign('action', $action);
287 // what action to take ?
288 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
289 // no browse for edit/update/view
290 $this->edit($action);
292 elseif ($action & CRM_Core_Action
::PREVIEW
) {
299 // Call the parents run method
300 return parent
::run();
304 * Preview custom field
306 * @param int $id custom field id
311 function preview($id) {
312 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action
::PREVIEW
);
313 $session = CRM_Core_Session
::singleton();
314 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid
));
315 $controller->set('fieldId', $id);
316 $controller->set('groupId', $this->_gid
);
317 $controller->setEmbedded(TRUE);
318 $controller->process();