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