Merge pull request #13984 from seamuslee001/nfc_comment_fix_ang
[civicrm-core.git] / CRM / UF / Page / ProfileEditor.php
CommitLineData
6a488035
TO
1<?php
2
3require_once 'CRM/Core/Page.php';
4
5/**
6 * This class is not a real page -- it contains helpers for rendering the profile-selector and profile-editor
7 * widgets
8 */
9class CRM_UF_Page_ProfileEditor extends CRM_Core_Page {
e8e8f3ad 10 /**
11 * Run page.
12 *
13 * @throws \Exception
14 */
00be9182 15 public function run() {
6a488035
TO
16 CRM_Core_Error::fatal('This is not a real page!');
17 }
18
e8e8f3ad 19 /**
20 * Register profile scripts.
21 */
00be9182 22 public static function registerProfileScripts() {
6a488035 23 static $loaded = FALSE;
daf0f4f3 24 if ($loaded || CRM_Core_Resources::isAjaxMode()) {
6a488035
TO
25 return;
26 }
27 $loaded = TRUE;
28
29 CRM_Core_Resources::singleton()
353ffa53 30 ->addSettingsFactory(function () {
be2fb01f 31 $ufGroups = civicrm_api3('UFGroup', 'get', [
37375016 32 'sequential' => 1,
33 'is_active' => 1,
be2fb01f
CW
34 'options' => ['limit' => 0],
35 ]);
58770077 36 //CRM-16915 - insert 'module' param for the profile used by CiviEvent.
37375016 37 if (CRM_Core_Permission::check('manage event profiles') && !CRM_Core_Permission::check('administer CiviCRM')) {
38 foreach ($ufGroups['values'] as $key => $value) {
39 $ufJoin = CRM_Core_BAO_UFGroup::getUFJoinRecord($value['id']);
b7fafaec 40 if (in_array('CiviEvent', $ufJoin) || in_array('CiviEvent_Additional', $ufJoin)) {
37375016 41 $ufGroups['values'][$key]['module'] = 'CiviEvent';
42 }
43 }
44 }
be2fb01f
CW
45 return [
46 'PseudoConstant' => [
b2b0530a 47 'locationType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
1da6c733 48 'websiteType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'),
b4f964d9 49 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'),
be2fb01f 50 ],
37375016 51 'initialProfileList' => $ufGroups,
21fced3b 52 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(),
6a488035 53 'profilePreviewKey' => CRM_Core_Key::get('CRM_UF_Form_Inline_Preview', TRUE),
be2fb01f 54 ];
6a488035
TO
55 })
56 ->addScriptFile('civicrm', 'packages/backbone/json2.js', 100, 'html-header', FALSE)
6a488035
TO
57 ->addScriptFile('civicrm', 'packages/backbone/backbone.js', 120, 'html-header')
58 ->addScriptFile('civicrm', 'packages/backbone/backbone.marionette.js', 125, 'html-header', FALSE)
59 ->addScriptFile('civicrm', 'packages/backbone/backbone.collectionsubset.js', 125, 'html-header', FALSE)
60 ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/backbone-forms.js', 130, 'html-header', FALSE)
61 ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/adapters/backbone.bootstrap-modal.min.js', 140, 'html-header', FALSE)
62 ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/editors/list.min.js', 140, 'html-header', FALSE)
63 ->addStyleFile('civicrm', 'packages/backbone-forms/distribution/templates/default.css', 140, 'html-header')
e7c93dd4 64 ->addScript('CRM.BB = Backbone.noConflict();', 300, 'html-header')
6a488035
TO
65 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
66 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header')
67 ->addStyleFile('civicrm', 'css/crm.designer.css', 140, 'html-header')
68 ->addScriptFile('civicrm', 'js/crm.backbone.js', 150)
69 ->addScriptFile('civicrm', 'js/model/crm.schema-mapped.js', 200)
70 ->addScriptFile('civicrm', 'js/model/crm.uf.js', 200)
71 ->addScriptFile('civicrm', 'js/model/crm.designer.js', 200)
72 ->addScriptFile('civicrm', 'js/model/crm.profile-selector.js', 200)
73 ->addScriptFile('civicrm', 'js/view/crm.designer.js', 200)
74 ->addScriptFile('civicrm', 'js/view/crm.profile-selector.js', 200)
75 ->addScriptFile('civicrm', 'js/jquery/jquery.crmProfileSelector.js', 250)
76 ->addScriptFile('civicrm', 'js/crm.designerapp.js', 250);
77
be2fb01f 78 CRM_Core_Region::instance('page-header')->add([
6a488035 79 'template' => 'CRM/UF/Page/ProfileTemplates.tpl',
be2fb01f 80 ]);
6a488035
TO
81 }
82
83 /**
e8e8f3ad 84 * Register entity schemas for use in the editor's palette.
6a488035 85 *
5ce1712d
TO
86 * @param array $entityTypes
87 * Strings, e.g. "IndividualModel", "ActivityModel".
6a488035 88 */
00be9182 89 public static function registerSchemas($entityTypes) {
6a488035
TO
90 // TODO in cases where registerSchemas is called multiple times for same entity, be more efficient
91 CRM_Core_Resources::singleton()->addSettingsFactory(function () use ($entityTypes) {
be2fb01f 92 return [
6a488035 93 'civiSchema' => CRM_UF_Page_ProfileEditor::getSchema($entityTypes),
be2fb01f 94 ];
6a488035
TO
95 });
96 }
97
98 /**
fe482240 99 * AJAX callback.
6a488035 100 */
00be9182 101 public static function getSchemaJSON() {
6a488035 102 $entityTypes = explode(',', $_REQUEST['entityTypes']);
ecdef330 103 CRM_Utils_JSON::output(self::getSchema($entityTypes));
6a488035
TO
104 }
105
106 /**
107 * Get a list of Backbone-Form models
108 *
5ce1712d
TO
109 * @param array $entityTypes
110 * Model names ("IndividualModel").
77b97be7
EM
111 *
112 * @throws CRM_Core_Exception
6a488035
TO
113 * @return array; keys are model names ("IndividualModel") and values describe 'sections' and 'schema'
114 * @see js/model/crm.core.js
115 * @see js/model/crm.mappedcore.js
116 */
00be9182 117 public static function getSchema($entityTypes) {
6a488035
TO
118 // FIXME: Depending on context (eg civicrm/profile/create vs search-columns), it may be appropriate to
119 // pick importable or exportable fields
120
121 $entityTypes = array_unique($entityTypes);
122 $availableFields = NULL;
be2fb01f 123 $civiSchema = [];
6a488035
TO
124 foreach ($entityTypes as $entityType) {
125 if (!$availableFields) {
126 $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat();
6a488035
TO
127 }
128 switch ($entityType) {
129 case 'IndividualModel':
130 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
131 'Individual',
132 ts('Individual'),
133 $availableFields
134 );
135 break;
9147c186 136
133e2c99 137 case 'OrganizationModel':
138 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
139 'Organization',
140 ts('Organization'),
141 $availableFields
142 );
bf86535e 143 break;
9147c186 144
bf86535e 145 case 'HouseholdModel':
146 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
147 'Household',
148 ts('Household'),
149 $availableFields
150 );
151 break;
9147c186 152
6a488035
TO
153 case 'ActivityModel':
154 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
155 'Activity',
156 ts('Activity'),
157 $availableFields
158 );
159 break;
9147c186 160
6a488035
TO
161 case 'ContributionModel':
162 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
163 'Contribution',
164 ts('Contribution'),
165 $availableFields
166 );
167 break;
9147c186 168
6a488035
TO
169 case 'MembershipModel':
170 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
171 'Membership',
172 ts('Membership'),
173 $availableFields
174 );
175 break;
9147c186 176
6a488035
TO
177 case 'ParticipantModel':
178 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
179 'Participant',
180 ts('Participant'),
181 $availableFields
182 );
183 break;
9147c186 184
cf1182e6
N
185 case 'CaseModel':
186 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
187 'Case',
188 ts('Case'),
189 $availableFields
190 );
191 break;
9147c186 192
6a488035
TO
193 default:
194 throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
195 }
196 }
197
0103eaf0 198 // Adding the oddball "formatting" field here because there's no other place to put it
be2fb01f 199 foreach (['Individual', 'Organization', 'Household'] as $type) {
0103eaf0 200 if (isset($civiSchema[$type . 'Model'])) {
be2fb01f
CW
201 $civiSchema[$type . 'Model']['schema'] += [
202 'formatting' => [
0103eaf0
CW
203 'type' => 'Markup',
204 'title' => ts('Free HTML'),
205 'civiFieldType' => 'Formatting',
206 'section' => 'formatting',
be2fb01f
CW
207 ],
208 ];
209 $civiSchema[$type . 'Model']['sections'] += [
210 'formatting' => [
0103eaf0
CW
211 'title' => ts('Formatting'),
212 'is_addable' => FALSE,
be2fb01f
CW
213 ],
214 ];
0103eaf0
CW
215 }
216 }
217
6a488035
TO
218 return $civiSchema;
219 }
220
221 /**
222 * FIXME: Move to somewhere more useful
223 * FIXME: Do real mapping of "types"
224 *
5ce1712d
TO
225 * @param string $extends
226 * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact".
227 * @param string $title
228 * A string to use in section headers.
229 * @param array $availableFields
230 * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type'].
a6c01b45
CW
231 * @return array
232 * with keys 'sections' and 'schema'
6a488035
TO
233 * @see js/model/crm.core.js
234 * @see js/model/crm.mappedcore.js
235 */
00be9182 236 public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) {
6a488035
TO
237 $locationFields = CRM_Core_BAO_UFGroup::getLocationFields();
238
c5c263ca
AH
239 // schema in format array($fieldName => $fieldSchema)
240 // sections in format array($sectionName => $section)
be2fb01f
CW
241 $result = [
242 'schema' => [],
243 'sections' => [],
244 ];
6a488035
TO
245
246 // build field list
247 foreach ($availableFields as $fieldName => $field) {
248 switch ($extends) {
249 case 'Individual':
250 case 'Organization':
251 case 'Household':
21fced3b 252 if ($field['field_type'] != $extends && $field['field_type'] != 'Contact'
253 //CRM-15595 check if subtype
353ffa53
TO
254 && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))
255 ) {
6a488035
TO
256 continue 2;
257 }
258 break;
9147c186 259
6a488035
TO
260 default:
261 if ($field['field_type'] != $extends) {
262 continue 2;
263 }
264 }
c5c263ca 265 // FIXME: type set to "Text"
be2fb01f 266 $result['schema'][$fieldName] = [
c5c263ca 267 'type' => 'Text',
6a488035
TO
268 'title' => $field['title'],
269 'civiFieldType' => $field['field_type'],
be2fb01f 270 ];
6a488035
TO
271 if (in_array($fieldName, $locationFields)) {
272 $result['schema'][$fieldName]['civiIsLocation'] = TRUE;
273 }
1a228b4c 274 if ($fieldName == 'url') {
1da6c733 275 $result['schema'][$fieldName]['civiIsWebsite'] = TRUE;
1a228b4c 276 }
be2fb01f 277 if (in_array($fieldName, ['phone', 'phone_and_ext'])) {
971d41b1 278 // FIXME what about phone_ext?
6a488035
TO
279 $result['schema'][$fieldName]['civiIsPhone'] = TRUE;
280 }
281 }
282
283 // build section list
be2fb01f 284 $result['sections']['default'] = [
6a488035
TO
285 'title' => $title,
286 'is_addable' => FALSE,
be2fb01f 287 ];
6a488035
TO
288
289 $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends);
290 $customGroup->orderBy('weight');
291 $customGroup->is_active = 1;
292 $customGroup->find();
293 while ($customGroup->fetch()) {
294 $sectionName = 'cg_' . $customGroup->id;
be2fb01f
CW
295 $section = [
296 'title' => ts('%1: %2', [1 => $title, 2 => $customGroup->title]),
0249f58e 297 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE,
6a488035
TO
298 'custom_group_id' => $customGroup->id,
299 'extends_entity_column_id' => $customGroup->extends_entity_column_id,
300 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value),
db7b725d 301 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE,
be2fb01f 302 ];
6a488035
TO
303 $result['sections'][$sectionName] = $section;
304 }
305
306 // put fields in their sections
307 $fields = CRM_Core_BAO_CustomField::getFields($extends);
308 foreach ($fields as $fieldId => $field) {
309 $sectionName = 'cg_' . $field['custom_group_id'];
310 $fieldName = 'custom_' . $fieldId;
311 if (isset($result['schema'][$fieldName])) {
312 $result['schema'][$fieldName]['section'] = $sectionName;
313 $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId);
314 }
315 }
316 return $result;
317 }
96025800 318
6a488035 319}