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