Commit | Line | Data |
---|---|---|
6a488035 TO |
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 { | |
e8e8f3ad | 10 | /** |
11 | * Run page. | |
12 | * | |
13 | * @throws \Exception | |
14 | */ | |
00be9182 | 15 | public function run() { |
6a488035 TO |
16 | CRM_Core_Error::fatal('This is not a real page!'); |
17 | } | |
18 | ||
e8e8f3ad | 19 | /** |
20 | * Register profile scripts. | |
21 | */ | |
00be9182 | 22 | public static function registerProfileScripts() { |
6a488035 | 23 | static $loaded = FALSE; |
daf0f4f3 | 24 | if ($loaded || CRM_Core_Resources::isAjaxMode()) { |
6a488035 TO |
25 | return; |
26 | } | |
27 | $loaded = TRUE; | |
28 | ||
29 | CRM_Core_Resources::singleton() | |
353ffa53 | 30 | ->addSettingsFactory(function () { |
37375016 | 31 | $ufGroups = civicrm_api3('UFGroup', 'get', array( |
32 | 'sequential' => 1, | |
33 | 'is_active' => 1, | |
58770077 | 34 | 'options' => array('limit' => 0), |
37375016 | 35 | )); |
58770077 | 36 | //CRM-16915 - insert 'module' param for the profile used by CiviEvent. |
37375016 | 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']); | |
b7fafaec | 40 | if (in_array('CiviEvent', $ufJoin) || in_array('CiviEvent_Additional', $ufJoin)) { |
37375016 | 41 | $ufGroups['values'][$key]['module'] = 'CiviEvent'; |
42 | } | |
43 | } | |
44 | } | |
6a488035 TO |
45 | return array( |
46 | 'PseudoConstant' => array( | |
b2b0530a | 47 | 'locationType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), |
1da6c733 | 48 | 'websiteType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'), |
b4f964d9 | 49 | 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
6a488035 | 50 | ), |
37375016 | 51 | 'initialProfileList' => $ufGroups, |
21fced3b | 52 | 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(), |
6a488035 TO |
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) | |
6a488035 TO |
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') | |
e7c93dd4 | 64 | ->addScript('CRM.BB = Backbone.noConflict();', 300, 'html-header') |
6a488035 TO |
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 | /** | |
e8e8f3ad | 84 | * Register entity schemas for use in the editor's palette. |
6a488035 | 85 | * |
5ce1712d TO |
86 | * @param array $entityTypes |
87 | * Strings, e.g. "IndividualModel", "ActivityModel". | |
6a488035 | 88 | */ |
00be9182 | 89 | public static function registerSchemas($entityTypes) { |
6a488035 TO |
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 | /** | |
fe482240 | 99 | * AJAX callback. |
6a488035 | 100 | */ |
00be9182 | 101 | public static function getSchemaJSON() { |
6a488035 | 102 | $entityTypes = explode(',', $_REQUEST['entityTypes']); |
ecdef330 | 103 | CRM_Utils_JSON::output(self::getSchema($entityTypes)); |
6a488035 TO |
104 | } |
105 | ||
106 | /** | |
107 | * Get a list of Backbone-Form models | |
108 | * | |
5ce1712d TO |
109 | * @param array $entityTypes |
110 | * Model names ("IndividualModel"). | |
77b97be7 EM |
111 | * |
112 | * @throws CRM_Core_Exception | |
6a488035 TO |
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 | */ | |
00be9182 | 117 | public static function getSchema($entityTypes) { |
6a488035 TO |
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; | |
0103eaf0 | 123 | $civiSchema = array(); |
6a488035 TO |
124 | foreach ($entityTypes as $entityType) { |
125 | if (!$availableFields) { | |
126 | $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); | |
6a488035 TO |
127 | } |
128 | switch ($entityType) { | |
129 | case 'IndividualModel': | |
130 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
131 | 'Individual', | |
132 | ts('Individual'), | |
133 | $availableFields | |
134 | ); | |
135 | break; | |
9147c186 | 136 | |
133e2c99 | 137 | case 'OrganizationModel': |
138 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
139 | 'Organization', | |
140 | ts('Organization'), | |
141 | $availableFields | |
142 | ); | |
bf86535e | 143 | break; |
9147c186 | 144 | |
bf86535e | 145 | case 'HouseholdModel': |
146 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
147 | 'Household', | |
148 | ts('Household'), | |
149 | $availableFields | |
150 | ); | |
151 | break; | |
9147c186 | 152 | |
6a488035 TO |
153 | case 'ActivityModel': |
154 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
155 | 'Activity', | |
156 | ts('Activity'), | |
157 | $availableFields | |
158 | ); | |
159 | break; | |
9147c186 | 160 | |
6a488035 TO |
161 | case 'ContributionModel': |
162 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
163 | 'Contribution', | |
164 | ts('Contribution'), | |
165 | $availableFields | |
166 | ); | |
167 | break; | |
9147c186 | 168 | |
6a488035 TO |
169 | case 'MembershipModel': |
170 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
171 | 'Membership', | |
172 | ts('Membership'), | |
173 | $availableFields | |
174 | ); | |
175 | break; | |
9147c186 | 176 | |
6a488035 TO |
177 | case 'ParticipantModel': |
178 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
179 | 'Participant', | |
180 | ts('Participant'), | |
181 | $availableFields | |
182 | ); | |
183 | break; | |
9147c186 | 184 | |
cf1182e6 N |
185 | case 'CaseModel': |
186 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
187 | 'Case', | |
188 | ts('Case'), | |
189 | $availableFields | |
190 | ); | |
191 | break; | |
9147c186 | 192 | |
6a488035 TO |
193 | default: |
194 | throw new CRM_Core_Exception("Unrecognized entity type: $entityType"); | |
195 | } | |
196 | } | |
197 | ||
0103eaf0 CW |
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 | ||
6a488035 TO |
218 | return $civiSchema; |
219 | } | |
220 | ||
221 | /** | |
222 | * FIXME: Move to somewhere more useful | |
223 | * FIXME: Do real mapping of "types" | |
224 | * | |
5ce1712d TO |
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']. | |
a6c01b45 CW |
231 | * @return array |
232 | * with keys 'sections' and 'schema' | |
6a488035 TO |
233 | * @see js/model/crm.core.js |
234 | * @see js/model/crm.mappedcore.js | |
235 | */ | |
00be9182 | 236 | public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) { |
6a488035 TO |
237 | $locationFields = CRM_Core_BAO_UFGroup::getLocationFields(); |
238 | ||
c5c263ca AH |
239 | // schema in format array($fieldName => $fieldSchema) |
240 | // sections in format array($sectionName => $section) | |
6a488035 | 241 | $result = array( |
c5c263ca AH |
242 | 'schema' => array(), |
243 | 'sections' => array(), | |
6a488035 TO |
244 | ); |
245 | ||
246 | // build field list | |
247 | foreach ($availableFields as $fieldName => $field) { | |
248 | switch ($extends) { | |
249 | case 'Individual': | |
250 | case 'Organization': | |
251 | case 'Household': | |
21fced3b | 252 | if ($field['field_type'] != $extends && $field['field_type'] != 'Contact' |
253 | //CRM-15595 check if subtype | |
353ffa53 TO |
254 | && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends)) |
255 | ) { | |
6a488035 TO |
256 | continue 2; |
257 | } | |
258 | break; | |
9147c186 | 259 | |
6a488035 TO |
260 | default: |
261 | if ($field['field_type'] != $extends) { | |
262 | continue 2; | |
263 | } | |
264 | } | |
c5c263ca | 265 | // FIXME: type set to "Text" |
6a488035 | 266 | $result['schema'][$fieldName] = array( |
c5c263ca | 267 | 'type' => 'Text', |
6a488035 TO |
268 | 'title' => $field['title'], |
269 | 'civiFieldType' => $field['field_type'], | |
270 | ); | |
271 | if (in_array($fieldName, $locationFields)) { | |
272 | $result['schema'][$fieldName]['civiIsLocation'] = TRUE; | |
273 | } | |
1a228b4c | 274 | if ($fieldName == 'url') { |
1da6c733 | 275 | $result['schema'][$fieldName]['civiIsWebsite'] = TRUE; |
1a228b4c | 276 | } |
971d41b1 CW |
277 | if (in_array($fieldName, array('phone', 'phone_and_ext'))) { |
278 | // FIXME what about phone_ext? | |
6a488035 TO |
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)), | |
0249f58e | 297 | 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE, |
6a488035 TO |
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), | |
db7b725d | 301 | 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE, |
6a488035 TO |
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 | } | |
96025800 | 318 | |
6a488035 | 319 | } |