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