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