Merge pull request #11495 from yashodha/CRM-21637
[civicrm-core.git] / CRM / UF / Page / ProfileEditor.php
1 <?php
2
3 require_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 */
9 class CRM_UF_Page_ProfileEditor extends CRM_Core_Page {
10 /**
11 * Run page.
12 *
13 * @throws \Exception
14 */
15 public function run() {
16 CRM_Core_Error::fatal('This is not a real page!');
17 }
18
19 /**
20 * Register profile scripts.
21 */
22 public static function registerProfileScripts() {
23 static $loaded = FALSE;
24 if ($loaded || CRM_Core_Resources::isAjaxMode()) {
25 return;
26 }
27 $loaded = TRUE;
28
29 CRM_Core_Resources::singleton()
30 ->addSettingsFactory(function () {
31 $ufGroups = civicrm_api3('UFGroup', 'get', array(
32 'sequential' => 1,
33 'is_active' => 1,
34 'options' => array('limit' => 0),
35 ));
36 //CRM-16915 - insert 'module' param for the profile used by CiviEvent.
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']);
40 if (in_array('CiviEvent', $ufJoin) || in_array('CiviEvent_Additional', $ufJoin)) {
41 $ufGroups['values'][$key]['module'] = 'CiviEvent';
42 }
43 }
44 }
45 return array(
46 'PseudoConstant' => array(
47 'locationType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
48 'websiteType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'),
49 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'),
50 ),
51 'initialProfileList' => $ufGroups,
52 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(),
53 'profilePreviewKey' => CRM_Core_Key::get('CRM_UF_Form_Inline_Preview', TRUE),
54 );
55 })
56 ->addScriptFile('civicrm', 'packages/backbone/json2.js', 100, 'html-header', FALSE)
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')
64 ->addScript('CRM.BB = Backbone.noConflict();', 300, 'html-header')
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
78 CRM_Core_Region::instance('page-header')->add(array(
79 'template' => 'CRM/UF/Page/ProfileTemplates.tpl',
80 ));
81 }
82
83 /**
84 * Register entity schemas for use in the editor's palette.
85 *
86 * @param array $entityTypes
87 * Strings, e.g. "IndividualModel", "ActivityModel".
88 */
89 public static function registerSchemas($entityTypes) {
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) {
92 return array(
93 'civiSchema' => CRM_UF_Page_ProfileEditor::getSchema($entityTypes),
94 );
95 });
96 }
97
98 /**
99 * AJAX callback.
100 */
101 public static function getSchemaJSON() {
102 $entityTypes = explode(',', $_REQUEST['entityTypes']);
103 CRM_Utils_JSON::output(self::getSchema($entityTypes));
104 }
105
106 /**
107 * Get a list of Backbone-Form models
108 *
109 * @param array $entityTypes
110 * Model names ("IndividualModel").
111 *
112 * @throws CRM_Core_Exception
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 */
117 public static function getSchema($entityTypes) {
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;
123 $civiSchema = array();
124 foreach ($entityTypes as $entityType) {
125 if (!$availableFields) {
126 $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat();
127 }
128 switch ($entityType) {
129 case 'IndividualModel':
130 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
131 'Individual',
132 ts('Individual'),
133 $availableFields
134 );
135 break;
136
137 case 'OrganizationModel':
138 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
139 'Organization',
140 ts('Organization'),
141 $availableFields
142 );
143 break;
144
145 case 'HouseholdModel':
146 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
147 'Household',
148 ts('Household'),
149 $availableFields
150 );
151 break;
152
153 case 'ActivityModel':
154 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
155 'Activity',
156 ts('Activity'),
157 $availableFields
158 );
159 break;
160
161 case 'ContributionModel':
162 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
163 'Contribution',
164 ts('Contribution'),
165 $availableFields
166 );
167 break;
168
169 case 'MembershipModel':
170 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
171 'Membership',
172 ts('Membership'),
173 $availableFields
174 );
175 break;
176
177 case 'ParticipantModel':
178 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
179 'Participant',
180 ts('Participant'),
181 $availableFields
182 );
183 break;
184
185 case 'CaseModel':
186 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
187 'Case',
188 ts('Case'),
189 $availableFields
190 );
191 break;
192
193 default:
194 throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
195 }
196 }
197
198 // Adding the oddball "formatting" field here because there's no other place to put it
199 foreach (array('Individual', 'Organization', 'Household') as $type) {
200 if (isset($civiSchema[$type . 'Model'])) {
201 $civiSchema[$type . 'Model']['schema'] += array(
202 'formatting' => array(
203 'type' => 'Markup',
204 'title' => ts('Free HTML'),
205 'civiFieldType' => 'Formatting',
206 'section' => 'formatting',
207 ),
208 );
209 $civiSchema[$type . 'Model']['sections'] += array(
210 'formatting' => array(
211 'title' => ts('Formatting'),
212 'is_addable' => FALSE,
213 ),
214 );
215 }
216 }
217
218 return $civiSchema;
219 }
220
221 /**
222 * FIXME: Move to somewhere more useful
223 * FIXME: Do real mapping of "types"
224 *
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'].
231 * @return array
232 * with keys 'sections' and 'schema'
233 * @see js/model/crm.core.js
234 * @see js/model/crm.mappedcore.js
235 */
236 public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) {
237 $locationFields = CRM_Core_BAO_UFGroup::getLocationFields();
238
239 // schema in format array($fieldName => $fieldSchema)
240 // sections in format array($sectionName => $section)
241 $result = array(
242 'schema' => array(),
243 'sections' => array(),
244 );
245
246 // build field list
247 foreach ($availableFields as $fieldName => $field) {
248 switch ($extends) {
249 case 'Individual':
250 case 'Organization':
251 case 'Household':
252 if ($field['field_type'] != $extends && $field['field_type'] != 'Contact'
253 //CRM-15595 check if subtype
254 && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))
255 ) {
256 continue 2;
257 }
258 break;
259
260 default:
261 if ($field['field_type'] != $extends) {
262 continue 2;
263 }
264 }
265 // FIXME: type set to "Text"
266 $result['schema'][$fieldName] = array(
267 'type' => 'Text',
268 'title' => $field['title'],
269 'civiFieldType' => $field['field_type'],
270 );
271 if (in_array($fieldName, $locationFields)) {
272 $result['schema'][$fieldName]['civiIsLocation'] = TRUE;
273 }
274 if ($fieldName == 'url') {
275 $result['schema'][$fieldName]['civiIsWebsite'] = TRUE;
276 }
277 if (in_array($fieldName, array('phone', 'phone_and_ext'))) {
278 // FIXME what about phone_ext?
279 $result['schema'][$fieldName]['civiIsPhone'] = TRUE;
280 }
281 }
282
283 // build section list
284 $result['sections']['default'] = array(
285 'title' => $title,
286 'is_addable' => FALSE,
287 );
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;
295 $section = array(
296 'title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)),
297 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE,
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),
301 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE,
302 );
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 }
318
319 }