Merge pull request #7024 from colemanw/CRM-13823
[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 $ufGroups = civicrm_api3('UFGroup', 'get', array(
24 'sequential' => 1,
25 'is_active' => 1,
26 'options' => array('limit' => 0),
27 ));
28 //CRM-16915 - insert 'module' param for the profile used by CiviEvent.
29 if (CRM_Core_Permission::check('manage event profiles') && !CRM_Core_Permission::check('administer CiviCRM')) {
30 foreach ($ufGroups['values'] as $key => $value) {
31 $ufJoin = CRM_Core_BAO_UFGroup::getUFJoinRecord($value['id']);
32 if (in_array('CiviEvent', $ufJoin) || in_array('CiviEvent_Additional', $ufJoin)) {
33 $ufGroups['values'][$key]['module'] = 'CiviEvent';
34 }
35 }
36 }
37 return array(
38 'PseudoConstant' => array(
39 'locationType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
40 'websiteType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'),
41 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'),
42 ),
43 'initialProfileList' => $ufGroups,
44 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(),
45 'profilePreviewKey' => CRM_Core_Key::get('CRM_UF_Form_Inline_Preview', TRUE),
46 );
47 })
48 ->addScriptFile('civicrm', 'packages/backbone/json2.js', 100, 'html-header', FALSE)
49 ->addScriptFile('civicrm', 'packages/backbone/backbone.js', 120, 'html-header')
50 ->addScriptFile('civicrm', 'packages/backbone/backbone.marionette.js', 125, 'html-header', FALSE)
51 ->addScriptFile('civicrm', 'packages/backbone/backbone.collectionsubset.js', 125, 'html-header', FALSE)
52 ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/backbone-forms.js', 130, 'html-header', FALSE)
53 ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/adapters/backbone.bootstrap-modal.min.js', 140, 'html-header', FALSE)
54 ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/editors/list.min.js', 140, 'html-header', FALSE)
55 ->addStyleFile('civicrm', 'packages/backbone-forms/distribution/templates/default.css', 140, 'html-header')
56 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
57 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header')
58 ->addStyleFile('civicrm', 'css/crm.designer.css', 140, 'html-header')
59 ->addScriptFile('civicrm', 'js/crm.backbone.js', 150)
60 ->addScriptFile('civicrm', 'js/model/crm.schema-mapped.js', 200)
61 ->addScriptFile('civicrm', 'js/model/crm.uf.js', 200)
62 ->addScriptFile('civicrm', 'js/model/crm.designer.js', 200)
63 ->addScriptFile('civicrm', 'js/model/crm.profile-selector.js', 200)
64 ->addScriptFile('civicrm', 'js/view/crm.designer.js', 200)
65 ->addScriptFile('civicrm', 'js/view/crm.profile-selector.js', 200)
66 ->addScriptFile('civicrm', 'js/jquery/jquery.crmProfileSelector.js', 250)
67 ->addScriptFile('civicrm', 'js/crm.designerapp.js', 250);
68
69 CRM_Core_Region::instance('page-header')->add(array(
70 'template' => 'CRM/UF/Page/ProfileTemplates.tpl',
71 ));
72 }
73
74 /**
75 * Register entity schemas for use in the editor's palette
76 *
77 * @param array $entityTypes
78 * Strings, e.g. "IndividualModel", "ActivityModel".
79 */
80 public static function registerSchemas($entityTypes) {
81 // TODO in cases where registerSchemas is called multiple times for same entity, be more efficient
82 CRM_Core_Resources::singleton()->addSettingsFactory(function () use ($entityTypes) {
83 return array(
84 'civiSchema' => CRM_UF_Page_ProfileEditor::getSchema($entityTypes),
85 );
86 });
87 }
88
89 /**
90 * AJAX callback.
91 */
92 public static function getSchemaJSON() {
93 $entityTypes = explode(',', $_REQUEST['entityTypes']);
94 CRM_Utils_JSON::output(self::getSchema($entityTypes));
95 }
96
97 /**
98 * Get a list of Backbone-Form models
99 *
100 * @param array $entityTypes
101 * Model names ("IndividualModel").
102 *
103 * @throws CRM_Core_Exception
104 * @return array; keys are model names ("IndividualModel") and values describe 'sections' and 'schema'
105 * @see js/model/crm.core.js
106 * @see js/model/crm.mappedcore.js
107 */
108 public static function getSchema($entityTypes) {
109 // FIXME: Depending on context (eg civicrm/profile/create vs search-columns), it may be appropriate to
110 // pick importable or exportable fields
111
112 $entityTypes = array_unique($entityTypes);
113 $availableFields = NULL;
114 $civiSchema = array();
115 foreach ($entityTypes as $entityType) {
116 if (!$availableFields) {
117 $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat();
118 }
119 switch ($entityType) {
120 case 'IndividualModel':
121 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
122 'Individual',
123 ts('Individual'),
124 $availableFields
125 );
126 break;
127
128 case 'OrganizationModel':
129 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
130 'Organization',
131 ts('Organization'),
132 $availableFields
133 );
134 break;
135
136 case 'HouseholdModel':
137 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
138 'Household',
139 ts('Household'),
140 $availableFields
141 );
142 break;
143
144 case 'ActivityModel':
145 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
146 'Activity',
147 ts('Activity'),
148 $availableFields
149 );
150 break;
151
152 case 'ContributionModel':
153 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
154 'Contribution',
155 ts('Contribution'),
156 $availableFields
157 );
158 break;
159
160 case 'MembershipModel':
161 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
162 'Membership',
163 ts('Membership'),
164 $availableFields
165 );
166 break;
167
168 case 'ParticipantModel':
169 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
170 'Participant',
171 ts('Participant'),
172 $availableFields
173 );
174 break;
175
176 case 'CaseModel':
177 $civiSchema[$entityType] = self::convertCiviModelToBackboneModel(
178 'Case',
179 ts('Case'),
180 $availableFields
181 );
182 break;
183
184 default:
185 throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
186 }
187 }
188
189 // Adding the oddball "formatting" field here because there's no other place to put it
190 foreach (array('Individual', 'Organization', 'Household') as $type) {
191 if (isset($civiSchema[$type . 'Model'])) {
192 $civiSchema[$type . 'Model']['schema'] += array(
193 'formatting' => array(
194 'type' => 'Markup',
195 'title' => ts('Free HTML'),
196 'civiFieldType' => 'Formatting',
197 'section' => 'formatting',
198 ),
199 );
200 $civiSchema[$type . 'Model']['sections'] += array(
201 'formatting' => array(
202 'title' => ts('Formatting'),
203 'is_addable' => FALSE,
204 ),
205 );
206 }
207 }
208
209 return $civiSchema;
210 }
211
212 /**
213 * FIXME: Move to somewhere more useful
214 * FIXME: Do real mapping of "types"
215 *
216 * @param string $extends
217 * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact".
218 * @param string $title
219 * A string to use in section headers.
220 * @param array $availableFields
221 * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type'].
222 * @return array
223 * with keys 'sections' and 'schema'
224 * @see js/model/crm.core.js
225 * @see js/model/crm.mappedcore.js
226 */
227 public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) {
228 $locationFields = CRM_Core_BAO_UFGroup::getLocationFields();
229
230 $result = array(
231 'schema' => array(), // array($fieldName => $fieldSchema)
232 'sections' => array(), // array($sectionName => $section)
233 );
234
235 // build field list
236 foreach ($availableFields as $fieldName => $field) {
237 switch ($extends) {
238 case 'Individual':
239 case 'Organization':
240 case 'Household':
241 if ($field['field_type'] != $extends && $field['field_type'] != 'Contact'
242 //CRM-15595 check if subtype
243 && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))
244 ) {
245 continue 2;
246 }
247 break;
248
249 default:
250 if ($field['field_type'] != $extends) {
251 continue 2;
252 }
253 }
254 $result['schema'][$fieldName] = array(
255 'type' => 'Text', // FIXME,
256 'title' => $field['title'],
257 'civiFieldType' => $field['field_type'],
258 );
259 if (in_array($fieldName, $locationFields)) {
260 $result['schema'][$fieldName]['civiIsLocation'] = TRUE;
261 }
262 if ($fieldName == 'url') {
263 $result['schema'][$fieldName]['civiIsWebsite'] = TRUE;
264 }
265 if (in_array($fieldName, array('phone', 'phone_and_ext'))) {
266 // FIXME what about phone_ext?
267 $result['schema'][$fieldName]['civiIsPhone'] = TRUE;
268 }
269 }
270
271 // build section list
272 $result['sections']['default'] = array(
273 'title' => $title,
274 'is_addable' => FALSE,
275 );
276
277 $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends);
278 $customGroup->orderBy('weight');
279 $customGroup->is_active = 1;
280 $customGroup->find();
281 while ($customGroup->fetch()) {
282 $sectionName = 'cg_' . $customGroup->id;
283 $section = array(
284 'title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)),
285 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE,
286 'custom_group_id' => $customGroup->id,
287 'extends_entity_column_id' => $customGroup->extends_entity_column_id,
288 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value),
289 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE,
290 );
291 $result['sections'][$sectionName] = $section;
292 }
293
294 // put fields in their sections
295 $fields = CRM_Core_BAO_CustomField::getFields($extends);
296 foreach ($fields as $fieldId => $field) {
297 $sectionName = 'cg_' . $field['custom_group_id'];
298 $fieldName = 'custom_' . $fieldId;
299 if (isset($result['schema'][$fieldName])) {
300 $result['schema'][$fieldName]['section'] = $sectionName;
301 $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId);
302 }
303 }
304 return $result;
305 }
306
307 }