3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Create a page for displaying CiviCRM Profile Fields.
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
26 class CRM_UF_Page_Field
extends CRM_Core_Page
{
28 public $useLivePageJS = TRUE;
31 * The group id of the field.
38 * The action links that we need to display for the browse screen.
42 private static $_actionLinks;
45 * Get the action links for this page.
49 public static function &actionLinks() {
50 if (!isset(self
::$_actionLinks)) {
51 self
::$_actionLinks = [
52 CRM_Core_Action
::UPDATE
=> [
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'),
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'),
64 CRM_Core_Action
::DISABLE
=> [
65 'name' => ts('Disable'),
66 'ref' => 'crm-enable-disable',
67 'title' => ts('Disable CiviCRM Profile Field'),
69 CRM_Core_Action
::ENABLE
=> [
70 'name' => ts('Enable'),
71 'ref' => 'crm-enable-disable',
72 'title' => ts('Enable CiviCRM Profile Field'),
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'),
82 return self
::$_actionLinks;
86 * Browse all CiviCRM Profile group fields.
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');
97 $ufFieldBAO = new CRM_Core_BAO_UFField();
100 $ufFieldBAO->uf_group_id
= $this->_gid
;
101 $ufFieldBAO->orderBy('weight', 'field_name');
104 $otherModules = CRM_Core_BAO_UFGroup
::getUFJoinRecord($this->_gid
);
105 $this->assign('otherModules', $otherModules);
107 $isGroupReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'is_reserved');
108 $this->assign('isGroupReserved', $isGroupReserved);
110 $isMixedProfile = CRM_Core_BAO_UFField
::checkProfileType($this->_gid
);
111 if ($isMixedProfile) {
112 $this->assign('skipCreate', TRUE);
116 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
118 $fields = CRM_Contact_BAO_Contact
::exportableFields('All', FALSE, TRUE);
119 $fields = array_merge(CRM_Contribute_BAO_Contribution
::getContributionFields(), $fields);
122 foreach ($fields as $name => $field) {
124 $select[$name] = $field['title'];
127 $select['group'] = ts('Group(s)');
128 $select['tag'] = ts('Tag(s)');
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
];
137 $ufField[$ufFieldBAO->id
]['label'] = $ufFieldBAO->label
;
139 $action = array_sum(array_keys(self
::actionLinks()));
140 if ($ufFieldBAO->is_active
) {
141 $action -= CRM_Core_Action
::ENABLE
;
144 $action -= CRM_Core_Action
::DISABLE
;
147 if ($ufFieldBAO->is_reserved
) {
148 $action -= CRM_Core_Action
::UPDATE
;
149 $action -= CRM_Core_Action
::DISABLE
;
150 $action -= CRM_Core_Action
::DELETE
;
152 $ufField[$ufFieldBAO->id
]['order'] = $ufField[$ufFieldBAO->id
]['weight'];
153 $ufField[$ufFieldBAO->id
]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(),
156 'id' => $ufFieldBAO->id
,
157 'gid' => $this->_gid
,
161 'ufField.row.actions',
167 $returnURL = CRM_Utils_System
::url('civicrm/admin/uf/group/field',
168 "reset=1&action=browse&gid={$this->_gid}"
170 $filter = "uf_group_id = {$this->_gid}";
171 CRM_Utils_Weight
::addOrder($ufField, 'CRM_Core_DAO_UFField',
172 'id', $returnURL, $filter
175 $this->assign('ufField', $ufField);
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);
185 * Edit CiviCRM Profile data.
187 * editing would involved modifying existing fields + adding data to new fields.
189 * @param string $action
190 * The action to be invoked.
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);
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
203 $controller->set('gid', $this->_gid
);
204 $controller->setEmbedded(TRUE);
205 $controller->process();
212 * This method is called after the page is created. It checks for the
213 * type of action and executes that action.
217 public function run() {
219 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
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]));
230 // get the requested action
231 $action = CRM_Utils_Request
::retrieve('action', 'String',
232 // default to 'browse'
233 $this, FALSE, 'browse'
236 // assign vars to templates
237 $this->assign('action', $action);
239 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
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);
248 elseif ($action & CRM_Core_Action
::PREVIEW
) {
249 $this->preview($id, $this->_gid
);
255 // Call the parents run method
256 return parent
::run();
260 * Preview custom field.
262 * @param int $fieldId
264 * @param int $groupId
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
274 $controller->set('fieldId', $fieldId);
275 $controller->set('id', $groupId);
276 $controller->setEmbedded(TRUE);
277 $controller->process();