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