3 require_once 'CRM/Core/Page.php';
6 * This class is not a real page -- it contains helpers for rendering the profile-selector and profile-editor
9 class CRM_UF_Page_ProfileEditor
extends CRM_Core_Page
{
16 public function run() {
17 throw new CRM_Core_Exception('This is not a real page!');
21 * Register profile scripts.
23 public static function registerProfileScripts() {
24 static $loaded = FALSE;
25 if ($loaded || CRM_Core_Resources
::isAjaxMode()) {
30 CRM_Core_Resources
::singleton()
31 ->addSettingsFactory(function () {
32 $ufGroups = civicrm_api3('UFGroup', 'get', [
35 'options' => ['limit' => 0],
37 //CRM-16915 - insert 'module' param for the profile used by CiviEvent.
38 if (CRM_Core_Permission
::check('manage event profiles') && !CRM_Core_Permission
::check('administer CiviCRM')) {
39 foreach ($ufGroups['values'] as $key => $value) {
40 $ufJoin = CRM_Core_BAO_UFGroup
::getUFJoinRecord($value['id']);
41 if (in_array('CiviEvent', $ufJoin) ||
in_array('CiviEvent_Additional', $ufJoin)) {
42 $ufGroups['values'][$key]['module'] = 'CiviEvent';
48 'locationType' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id'),
49 'websiteType' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id'),
50 'phoneType' => CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id'),
52 'initialProfileList' => $ufGroups,
53 'contactSubTypes' => CRM_Contact_BAO_ContactType
::subTypes(),
54 'profilePreviewKey' => CRM_Core_Key
::get('CRM_UF_Form_Inline_Preview', TRUE),
57 ->addScriptFile('civicrm.packages', 'backbone/json2.js', 100, 'html-header', FALSE)
58 ->addScriptFile('civicrm.packages', 'backbone/backbone.js', 120, 'html-header')
59 ->addScriptFile('civicrm.packages', 'backbone/backbone.marionette.js', 125, 'html-header', FALSE)
60 ->addScriptFile('civicrm.packages', 'backbone/backbone.collectionsubset.js', 125, 'html-header', FALSE)
61 ->addScriptFile('civicrm.packages', 'backbone-forms/distribution/backbone-forms.js', 130, 'html-header', FALSE)
62 ->addScriptFile('civicrm.packages', 'backbone-forms/distribution/adapters/backbone.bootstrap-modal.min.js', 140, 'html-header', FALSE)
63 ->addScriptFile('civicrm.packages', 'backbone-forms/distribution/editors/list.min.js', 140, 'html-header', FALSE)
64 ->addStyleFile('civicrm.packages', 'backbone-forms/distribution/templates/default.css', 140, 'html-header')
65 ->addScript('CRM.BB = Backbone.noConflict();', 300, 'html-header')
66 ->addScriptFile('civicrm.packages', 'jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
67 ->addStyleFile('civicrm.packages', 'jquery/plugins/jstree/themes/default/style.css', 0, 'html-header')
68 ->addStyleFile('civicrm', 'css/crm.designer.css', 140, 'html-header')
69 ->addScriptFile('civicrm', 'js/crm.backbone.js', 150)
70 ->addScriptFile('civicrm', 'js/model/crm.schema-mapped.js', 200)
71 ->addScriptFile('civicrm', 'js/model/crm.uf.js', 200)
72 ->addScriptFile('civicrm', 'js/model/crm.designer.js', 200)
73 ->addScriptFile('civicrm', 'js/model/crm.profile-selector.js', 200)
74 ->addScriptFile('civicrm', 'js/view/crm.designer.js', 200)
75 ->addScriptFile('civicrm', 'js/view/crm.profile-selector.js', 200)
76 ->addScriptFile('civicrm', 'js/jquery/jquery.crmProfileSelector.js', 250)
77 ->addScriptFile('civicrm', 'js/crm.designerapp.js', 250);
79 CRM_Core_Region
::instance('page-header')->add([
80 'template' => 'CRM/UF/Page/ProfileTemplates.tpl',
85 * Register entity schemas for use in the editor's palette.
87 * @param array $entityTypes
88 * Strings, e.g. "IndividualModel", "ActivityModel".
90 public static function registerSchemas($entityTypes) {
91 // TODO in cases where registerSchemas is called multiple times for same entity, be more efficient
92 CRM_Core_Resources
::singleton()->addSettingsFactory(function () use ($entityTypes) {
94 'civiSchema' => CRM_UF_Page_ProfileEditor
::getSchema($entityTypes),
102 public static function getSchemaJSON() {
103 $entityTypes = explode(',', $_REQUEST['entityTypes']);
104 CRM_Utils_JSON
::output(self
::getSchema($entityTypes));
108 * Get a list of Backbone-Form models
110 * @param array $entityTypes
111 * Model names ("IndividualModel").
113 * @throws CRM_Core_Exception
114 * @return array; keys are model names ("IndividualModel") and values describe 'sections' and 'schema'
115 * @see js/model/crm.core.js
116 * @see js/model/crm.mappedcore.js
118 public static function getSchema($entityTypes) {
119 // FIXME: Depending on context (eg civicrm/profile/create vs search-columns), it may be appropriate to
120 // pick importable or exportable fields
122 $entityTypes = array_unique($entityTypes);
123 $availableFields = NULL;
125 foreach ($entityTypes as $entityType) {
126 if (!$availableFields) {
127 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
129 switch ($entityType) {
130 case 'IndividualModel':
131 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
138 case 'OrganizationModel':
139 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
146 case 'HouseholdModel':
147 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
154 case 'ActivityModel':
155 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
162 case 'ContributionModel':
163 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
170 case 'MembershipModel':
171 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
178 case 'ParticipantModel':
179 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
187 $civiSchema[$entityType] = self
::convertCiviModelToBackboneModel(
195 if (strpos($entityType, 'Model') !== FALSE) {
196 $entity = str_replace('Model', '', $entityType);
197 $backboneModel = self
::convertCiviModelToBackboneModel(
199 ts('%1', [1 => $entity]),
202 if (!empty($backboneModel['schema'])) {
203 $civiSchema[$entityType] = $backboneModel;
206 if (!isset($civiSchema[$entityType])) {
207 throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
212 // Adding the oddball "formatting" field here because there's no other place to put it
213 foreach (['Individual', 'Organization', 'Household'] as $type) {
214 if (isset($civiSchema[$type . 'Model'])) {
215 $civiSchema[$type . 'Model']['schema'] +
= [
218 'title' => ts('Free HTML'),
219 'civiFieldType' => 'Formatting',
220 'section' => 'formatting',
223 $civiSchema[$type . 'Model']['sections'] +
= [
225 'title' => ts('Formatting'),
226 'is_addable' => FALSE,
236 * FIXME: Move to somewhere more useful
237 * FIXME: Do real mapping of "types"
239 * @param string $extends
240 * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact".
241 * @param string $title
242 * A string to use in section headers.
243 * @param array $availableFields
244 * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type'].
246 * with keys 'sections' and 'schema'
247 * @see js/model/crm.core.js
248 * @see js/model/crm.mappedcore.js
250 public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) {
251 $locationFields = CRM_Core_BAO_UFGroup
::getLocationFields();
253 // schema in format array($fieldName => $fieldSchema)
254 // sections in format array($sectionName => $section)
261 foreach ($availableFields as $fieldName => $field) {
266 if ($field['field_type'] != $extends && $field['field_type'] != 'Contact'
267 //CRM-15595 check if subtype
268 && !in_array($field['field_type'], CRM_Contact_BAO_ContactType
::subTypes($extends))
275 if ($field['field_type'] != $extends) {
279 // FIXME: type set to "Text"
280 $result['schema'][$fieldName] = [
282 'title' => $field['title'],
283 'civiFieldType' => $field['field_type'],
285 if (in_array($fieldName, $locationFields)) {
286 $result['schema'][$fieldName]['civiIsLocation'] = TRUE;
288 if ($fieldName == 'url') {
289 $result['schema'][$fieldName]['civiIsWebsite'] = TRUE;
291 if (in_array($fieldName, ['phone', 'phone_and_ext'])) {
292 // FIXME what about phone_ext?
293 $result['schema'][$fieldName]['civiIsPhone'] = TRUE;
297 // build section list
298 $result['sections']['default'] = [
300 'is_addable' => FALSE,
303 $customGroup = CRM_Core_BAO_CustomGroup
::getAllCustomGroupsByBaseEntity($extends);
304 $customGroup->orderBy('weight');
305 $customGroup->is_active
= 1;
306 $customGroup->find();
307 while ($customGroup->fetch()) {
308 $sectionName = 'cg_' . $customGroup->id
;
310 'title' => ts('%1: %2', [1 => $title, 2 => $customGroup->title
]),
311 'is_addable' => !$customGroup->is_reserved
,
312 'custom_group_id' => $customGroup->id
,
313 'extends_entity_column_id' => $customGroup->extends_entity_column_id
,
314 'extends_entity_column_value' => CRM_Utils_Array
::explodePadded($customGroup->extends_entity_column_value
),
315 'is_reserved' => (bool) $customGroup->is_reserved
,
317 $result['sections'][$sectionName] = $section;
320 // put fields in their sections
321 $fields = CRM_Core_BAO_CustomField
::getFields($extends);
322 foreach ($fields as $fieldId => $field) {
323 $sectionName = 'cg_' . $field['custom_group_id'];
324 $fieldName = 'custom_' . $fieldId;
325 if (isset($result['schema'][$fieldName])) {
326 $result['schema'][$fieldName]['section'] = $sectionName;
327 $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField
::isMultiRecordField($fieldId);