Merge pull request #5680 from elinw/restauthenticationjoomla
[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 public function run() {
11 CRM_Core_Error::fatal('This is not a real page!');
12 }
13
14 public 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 'websiteType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'),
27 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'),
28 ),
29 'initialProfileList' => civicrm_api('UFGroup', 'get', array(
30 'version' => 3,
31 'sequential' => 1,
32 'is_active' => 1,
33 'rowCount' => 1000, // FIXME
34 )),
35 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(),
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)
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 *
68 * @param array $entityTypes
69 * Strings, e.g. "IndividualModel", "ActivityModel".
70 */
71 public static function registerSchemas($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 public 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
92 * Model names ("IndividualModel").
93 *
94 * @throws CRM_Core_Exception
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 */
99 public static function getSchema($entityTypes) {
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 $civiSchema = array();
106 foreach ($entityTypes as $entityType) {
107 if (!$availableFields) {
108 $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat();
109 }
110 switch ($entityType) {
111 case 'IndividualModel':
112 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
113 'Individual',
114 ts('Individual'),
115 $availableFields
116 );
117 break;
118
119 case 'OrganizationModel':
120 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
121 'Organization',
122 ts('Organization'),
123 $availableFields
124 );
125 break;
126
127 case 'HouseholdModel':
128 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
129 'Household',
130 ts('Household'),
131 $availableFields
132 );
133 break;
134
135 case 'ActivityModel':
136 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
137 'Activity',
138 ts('Activity'),
139 $availableFields
140 );
141 break;
142
143 case 'ContributionModel':
144 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
145 'Contribution',
146 ts('Contribution'),
147 $availableFields
148 );
149 break;
150
151 case 'MembershipModel':
152 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
153 'Membership',
154 ts('Membership'),
155 $availableFields
156 );
157 break;
158
159 case 'ParticipantModel':
160 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
161 'Participant',
162 ts('Participant'),
163 $availableFields
164 );
165 break;
166
167 case 'CaseModel':
168 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
169 'Case',
170 ts('Case'),
171 $availableFields
172 );
173 break;
174
175 default:
176 throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
177 }
178 }
179
180 // Adding the oddball "formatting" field here because there's no other place to put it
181 foreach (array('Individual', 'Organization', 'Household') as $type) {
182 if (isset($civiSchema[$type . 'Model'])) {
183 $civiSchema[$type . 'Model']['schema'] += array(
184 'formatting' => array(
185 'type' => 'Markup',
186 'title' => ts('Free HTML'),
187 'civiFieldType' => 'Formatting',
188 'section' => 'formatting',
189 ),
190 );
191 $civiSchema[$type . 'Model']['sections'] += array(
192 'formatting' => array(
193 'title' => ts('Formatting'),
194 'is_addable' => FALSE,
195 ),
196 );
197 }
198 }
199
200 return $civiSchema;
201 }
202
203 /**
204 * FIXME: Move to somewhere more useful
205 * FIXME: Do real mapping of "types"
206 *
207 * @param string $extends
208 * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact".
209 * @param string $title
210 * A string to use in section headers.
211 * @param array $availableFields
212 * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type'].
213 * @return array
214 * with keys 'sections' and 'schema'
215 * @see js/model/crm.core.js
216 * @see js/model/crm.mappedcore.js
217 */
218 public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) {
219 $locationFields = CRM_Core_BAO_UFGroup::getLocationFields();
220
221 $result = array(
222 'schema' => array(), // array($fieldName => $fieldSchema)
223 'sections' => array(), // array($sectionName => $section)
224 );
225
226 // build field list
227 foreach ($availableFields as $fieldName => $field) {
228 switch ($extends) {
229 case 'Individual':
230 case 'Organization':
231 case 'Household':
232 if ($field['field_type'] != $extends && $field['field_type'] != 'Contact'
233 //CRM-15595 check if subtype
234 && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))
235 ) {
236 continue 2;
237 }
238 break;
239
240 default:
241 if ($field['field_type'] != $extends) {
242 continue 2;
243 }
244 }
245 $result['schema'][$fieldName] = array(
246 'type' => 'Text', // FIXME,
247 'title' => $field['title'],
248 'civiFieldType' => $field['field_type'],
249 );
250 if (in_array($fieldName, $locationFields)) {
251 $result['schema'][$fieldName]['civiIsLocation'] = TRUE;
252 }
253 if ($fieldName == 'url') {
254 $result['schema'][$fieldName]['civiIsWebsite'] = TRUE;
255 }
256 if (in_array($fieldName, array('phone', 'phone_and_ext'))) {
257 // FIXME what about phone_ext?
258 $result['schema'][$fieldName]['civiIsPhone'] = TRUE;
259 }
260 }
261
262 // build section list
263 $result['sections']['default'] = array(
264 'title' => $title,
265 'is_addable' => FALSE,
266 );
267
268 $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends);
269 $customGroup->orderBy('weight');
270 $customGroup->is_active = 1;
271 $customGroup->find();
272 while ($customGroup->fetch()) {
273 $sectionName = 'cg_' . $customGroup->id;
274 $section = array(
275 'title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)),
276 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE,
277 'custom_group_id' => $customGroup->id,
278 'extends_entity_column_id' => $customGroup->extends_entity_column_id,
279 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value),
280 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE,
281 );
282 $result['sections'][$sectionName] = $section;
283 }
284
285 // put fields in their sections
286 $fields = CRM_Core_BAO_CustomField::getFields($extends);
287 foreach ($fields as $fieldId => $field) {
288 $sectionName = 'cg_' . $field['custom_group_id'];
289 $fieldName = 'custom_' . $fieldId;
290 if (isset($result['schema'][$fieldName])) {
291 $result['schema'][$fieldName]['section'] = $sectionName;
292 $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId);
293 }
294 }
295 return $result;
296 }
297
298 }