Merge pull request #15764 from civicrm/5.20
[civicrm-core.git] / CRM / UF / 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 */
33
34/**
35 * Create a page for displaying CiviCRM Profile Fields.
36 *
37 * Heart of this class is the run method which checks
38 * for action type and then displays the appropriate
39 * page.
40 *
41 */
42class CRM_UF_Page_Field extends CRM_Core_Page {
43
96f50de2
CW
44 public $useLivePageJS = TRUE;
45
6a488035 46 /**
fe482240 47 * The group id of the field.
6a488035
TO
48 *
49 * @var int
6a488035
TO
50 */
51 protected $_gid;
52
53 /**
fe482240 54 * The action links that we need to display for the browse screen.
6a488035
TO
55 *
56 * @var array
6a488035
TO
57 */
58 private static $_actionLinks;
59
60 /**
61 * Get the action links for this page.
62 *
a6c01b45 63 * @return array
95ea96be 64 */
d1424f8f 65 public static function &actionLinks() {
6a488035 66 if (!isset(self::$_actionLinks)) {
be2fb01f
CW
67 self::$_actionLinks = [
68 CRM_Core_Action::UPDATE => [
6a488035
TO
69 'name' => ts('Edit'),
70 'url' => 'civicrm/admin/uf/group/field/update',
71 'qs' => 'reset=1&action=update&id=%%id%%&gid=%%gid%%',
72 'title' => ts('Edit CiviCRM Profile Field'),
be2fb01f
CW
73 ],
74 CRM_Core_Action::PREVIEW => [
6a488035
TO
75 'name' => ts('Preview'),
76 'url' => 'civicrm/admin/uf/group/field',
77 'qs' => 'action=preview&id=%%id%%&field=1',
78 'title' => ts('Preview CiviCRM Profile Field'),
be2fb01f
CW
79 ],
80 CRM_Core_Action::DISABLE => [
6a488035 81 'name' => ts('Disable'),
4d17a233 82 'ref' => 'crm-enable-disable',
6a488035 83 'title' => ts('Disable CiviCRM Profile Field'),
be2fb01f
CW
84 ],
85 CRM_Core_Action::ENABLE => [
6a488035 86 'name' => ts('Enable'),
4d17a233 87 'ref' => 'crm-enable-disable',
6a488035 88 'title' => ts('Enable CiviCRM Profile Field'),
be2fb01f
CW
89 ],
90 CRM_Core_Action::DELETE => [
6a488035
TO
91 'name' => ts('Delete'),
92 'url' => 'civicrm/admin/uf/group/field',
93 'qs' => 'action=delete&id=%%id%%',
94 'title' => ts('Enable CiviCRM Profile Field'),
be2fb01f
CW
95 ],
96 ];
6a488035
TO
97 }
98 return self::$_actionLinks;
99 }
100
101 /**
102 * Browse all CiviCRM Profile group fields.
103 *
104 * @return void
6a488035 105 */
00be9182 106 public function browse() {
6f231148
CW
107 $resourceManager = CRM_Core_Resources::singleton();
108 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
96ed17aa 109 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
6f231148
CW
110 }
111
be2fb01f 112 $ufField = [];
6a488035
TO
113 $ufFieldBAO = new CRM_Core_BAO_UFField();
114
115 // fkey is gid
116 $ufFieldBAO->uf_group_id = $this->_gid;
117 $ufFieldBAO->orderBy('weight', 'field_name');
118 $ufFieldBAO->find();
119
120 $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid);
121 $this->assign('otherModules', $otherModules);
122
123 $isGroupReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_reserved');
124 $this->assign('isGroupReserved', $isGroupReserved);
125
7e50650d
PN
126 $isMixedProfile = CRM_Core_BAO_UFField::checkProfileType($this->_gid);
127 if ($isMixedProfile) {
6a488035
TO
128 $this->assign('skipCreate', TRUE);
129 }
130
be2fb01f 131 $locationType = [];
b2b0530a 132 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
133
134 $fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
135 $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields);
136
be2fb01f 137 $select = [];
6a488035
TO
138 foreach ($fields as $name => $field) {
139 if ($name) {
140 $select[$name] = $field['title'];
141 }
142 }
143 $select['group'] = ts('Group(s)');
144 $select['tag'] = ts('Tag(s)');
145
77d0b1f8 146 $visibility = CRM_Core_SelectValues::ufVisibility();
6a488035 147 while ($ufFieldBAO->fetch()) {
be2fb01f 148 $ufField[$ufFieldBAO->id] = [];
6a488035
TO
149 $phoneType = $locType = '';
150 CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]);
77d0b1f8 151 $ufField[$ufFieldBAO->id]['visibility_display'] = $visibility[$ufFieldBAO->visibility];
6a488035
TO
152
153 $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label;
154
d1424f8f 155 $action = array_sum(array_keys(self::actionLinks()));
6a488035
TO
156 if ($ufFieldBAO->is_active) {
157 $action -= CRM_Core_Action::ENABLE;
158 }
159 else {
160 $action -= CRM_Core_Action::DISABLE;
161 }
162
163 if ($ufFieldBAO->is_reserved) {
164 $action -= CRM_Core_Action::UPDATE;
165 $action -= CRM_Core_Action::DISABLE;
166 $action -= CRM_Core_Action::DELETE;
167 }
168 $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight'];
169 $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
170 $action,
be2fb01f 171 [
6a488035
TO
172 'id' => $ufFieldBAO->id,
173 'gid' => $this->_gid,
be2fb01f 174 ],
87dab4a4
AH
175 ts('more'),
176 FALSE,
177 'ufField.row.actions',
178 'UFField',
179 $ufFieldBAO->id
6a488035
TO
180 );
181 }
182
183 $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field',
184 "reset=1&action=browse&gid={$this->_gid}"
185 );
186 $filter = "uf_group_id = {$this->_gid}";
187 CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField',
188 'id', $returnURL, $filter
189 );
190
191 $this->assign('ufField', $ufField);
192
193 // retrieve showBestResult from session
194 $session = CRM_Core_Session::singleton();
195 $showBestResult = $session->get('showBestResult');
196 $this->assign('showBestResult', $showBestResult);
197 $session->set('showBestResult', 0);
198 }
199
200 /**
100fef9d 201 * Edit CiviCRM Profile data.
6a488035
TO
202 *
203 * editing would involved modifying existing fields + adding data to new fields.
204 *
5ce1712d
TO
205 * @param string $action
206 * The action to be invoked.
6a488035
TO
207 *
208 * @return void
6a488035 209 */
00be9182 210 public function edit($action) {
6a488035
TO
211 // create a simple controller for editing CiviCRM Profile data
212 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action);
213
214 // set the userContext stack
215 $session = CRM_Core_Session::singleton();
216 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
353ffa53
TO
217 'reset=1&action=browse&gid=' . $this->_gid
218 ));
6a488035
TO
219 $controller->set('gid', $this->_gid);
220 $controller->setEmbedded(TRUE);
221 $controller->process();
222 $controller->run();
223 }
224
225 /**
226 * Run the page.
227 *
228 * This method is called after the page is created. It checks for the
229 * type of action and executes that action.
230 *
231 * @return void
6a488035 232 */
00be9182 233 public function run() {
6a488035
TO
234 // get the group id
235 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
236 $this, FALSE, 0
237 );
238
239 if ($this->_gid) {
240 $groupTitle = CRM_Core_BAO_UFGroup::getTitle($this->_gid);
241 $this->assign('gid', $this->_gid);
242 $this->assign('groupTitle', $groupTitle);
be2fb01f 243 CRM_Utils_System::setTitle(ts('%1 - CiviCRM Profile Fields', [1 => $groupTitle]));
6a488035
TO
244 }
245
246 // get the requested action
247 $action = CRM_Utils_Request::retrieve('action', 'String',
248 // default to 'browse'
249 $this, FALSE, 'browse'
250 );
251
252 // assign vars to templates
253 $this->assign('action', $action);
254
255 $id = CRM_Utils_Request::retrieve('id', 'Positive',
256 $this, FALSE, 0
257 );
258
259 // what action to take ?
260 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE)) {
261 // no browse for edit/update/view/delete
262 $this->edit($action);
263 }
264 elseif ($action & CRM_Core_Action::PREVIEW) {
265 $this->preview($id, $this->_gid);
266 }
267 else {
268 $this->browse();
269 }
270
271 // Call the parents run method
272 return parent::run();
273 }
274
275 /**
fe482240 276 * Preview custom field.
6a488035 277 *
5ce1712d
TO
278 * @param int $fieldId
279 * Custom field id.
c490a46a 280 * @param int $groupId
6a488035
TO
281 *
282 * @return void
6a488035 283 */
00be9182 284 public function preview($fieldId, $groupId) {
6a488035
TO
285 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
286 $session = CRM_Core_Session::singleton();
287 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
353ffa53
TO
288 'reset=1&action=browse&gid=' . $this->_gid
289 ));
6a488035
TO
290 $controller->set('fieldId', $fieldId);
291 $controller->set('id', $groupId);
292 $controller->setEmbedded(TRUE);
293 $controller->process();
294 $controller->run();
295 }
96025800 296
6a488035 297}