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') | |
64 | ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE) | |
65 | ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header') | |
66 | ->addStyleFile('civicrm', 'css/crm.designer.css', 140, 'html-header') | |
67 | ->addScriptFile('civicrm', 'js/crm.backbone.js', 150) | |
68 | ->addScriptFile('civicrm', 'js/model/crm.schema-mapped.js', 200) | |
69 | ->addScriptFile('civicrm', 'js/model/crm.uf.js', 200) | |
70 | ->addScriptFile('civicrm', 'js/model/crm.designer.js', 200) | |
71 | ->addScriptFile('civicrm', 'js/model/crm.profile-selector.js', 200) | |
72 | ->addScriptFile('civicrm', 'js/view/crm.designer.js', 200) | |
73 | ->addScriptFile('civicrm', 'js/view/crm.profile-selector.js', 200) | |
74 | ->addScriptFile('civicrm', 'js/jquery/jquery.crmProfileSelector.js', 250) | |
75 | ->addScriptFile('civicrm', 'js/crm.designerapp.js', 250); | |
76 | ||
77 | CRM_Core_Region::instance('page-header')->add(array( | |
78 | 'template' => 'CRM/UF/Page/ProfileTemplates.tpl', | |
79 | )); | |
80 | } | |
81 | ||
82 | /** | |
e8e8f3ad | 83 | * Register entity schemas for use in the editor's palette. |
6a488035 | 84 | * |
5ce1712d TO |
85 | * @param array $entityTypes |
86 | * Strings, e.g. "IndividualModel", "ActivityModel". | |
6a488035 | 87 | */ |
00be9182 | 88 | public static function registerSchemas($entityTypes) { |
6a488035 TO |
89 | // TODO in cases where registerSchemas is called multiple times for same entity, be more efficient |
90 | CRM_Core_Resources::singleton()->addSettingsFactory(function () use ($entityTypes) { | |
91 | return array( | |
92 | 'civiSchema' => CRM_UF_Page_ProfileEditor::getSchema($entityTypes), | |
93 | ); | |
94 | }); | |
95 | } | |
96 | ||
97 | /** | |
fe482240 | 98 | * AJAX callback. |
6a488035 | 99 | */ |
00be9182 | 100 | public static function getSchemaJSON() { |
6a488035 | 101 | $entityTypes = explode(',', $_REQUEST['entityTypes']); |
ecdef330 | 102 | CRM_Utils_JSON::output(self::getSchema($entityTypes)); |
6a488035 TO |
103 | } |
104 | ||
105 | /** | |
106 | * Get a list of Backbone-Form models | |
107 | * | |
5ce1712d TO |
108 | * @param array $entityTypes |
109 | * Model names ("IndividualModel"). | |
77b97be7 EM |
110 | * |
111 | * @throws CRM_Core_Exception | |
6a488035 TO |
112 | * @return array; keys are model names ("IndividualModel") and values describe 'sections' and 'schema' |
113 | * @see js/model/crm.core.js | |
114 | * @see js/model/crm.mappedcore.js | |
115 | */ | |
00be9182 | 116 | public static function getSchema($entityTypes) { |
6a488035 TO |
117 | // FIXME: Depending on context (eg civicrm/profile/create vs search-columns), it may be appropriate to |
118 | // pick importable or exportable fields | |
119 | ||
120 | $entityTypes = array_unique($entityTypes); | |
121 | $availableFields = NULL; | |
0103eaf0 | 122 | $civiSchema = array(); |
6a488035 TO |
123 | foreach ($entityTypes as $entityType) { |
124 | if (!$availableFields) { | |
125 | $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); | |
6a488035 TO |
126 | } |
127 | switch ($entityType) { | |
128 | case 'IndividualModel': | |
129 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
130 | 'Individual', | |
131 | ts('Individual'), | |
132 | $availableFields | |
133 | ); | |
134 | break; | |
9147c186 | 135 | |
133e2c99 | 136 | case 'OrganizationModel': |
137 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
138 | 'Organization', | |
139 | ts('Organization'), | |
140 | $availableFields | |
141 | ); | |
bf86535e | 142 | break; |
9147c186 | 143 | |
bf86535e | 144 | case 'HouseholdModel': |
145 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
146 | 'Household', | |
147 | ts('Household'), | |
148 | $availableFields | |
149 | ); | |
150 | break; | |
9147c186 | 151 | |
6a488035 TO |
152 | case 'ActivityModel': |
153 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
154 | 'Activity', | |
155 | ts('Activity'), | |
156 | $availableFields | |
157 | ); | |
158 | break; | |
9147c186 | 159 | |
6a488035 TO |
160 | case 'ContributionModel': |
161 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
162 | 'Contribution', | |
163 | ts('Contribution'), | |
164 | $availableFields | |
165 | ); | |
166 | break; | |
9147c186 | 167 | |
6a488035 TO |
168 | case 'MembershipModel': |
169 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
170 | 'Membership', | |
171 | ts('Membership'), | |
172 | $availableFields | |
173 | ); | |
174 | break; | |
9147c186 | 175 | |
6a488035 TO |
176 | case 'ParticipantModel': |
177 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
178 | 'Participant', | |
179 | ts('Participant'), | |
180 | $availableFields | |
181 | ); | |
182 | break; | |
9147c186 | 183 | |
cf1182e6 N |
184 | case 'CaseModel': |
185 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
186 | 'Case', | |
187 | ts('Case'), | |
188 | $availableFields | |
189 | ); | |
190 | break; | |
9147c186 | 191 | |
6a488035 TO |
192 | default: |
193 | throw new CRM_Core_Exception("Unrecognized entity type: $entityType"); | |
194 | } | |
195 | } | |
196 | ||
0103eaf0 CW |
197 | // Adding the oddball "formatting" field here because there's no other place to put it |
198 | foreach (array('Individual', 'Organization', 'Household') as $type) { | |
199 | if (isset($civiSchema[$type . 'Model'])) { | |
200 | $civiSchema[$type . 'Model']['schema'] += array( | |
201 | 'formatting' => array( | |
202 | 'type' => 'Markup', | |
203 | 'title' => ts('Free HTML'), | |
204 | 'civiFieldType' => 'Formatting', | |
205 | 'section' => 'formatting', | |
206 | ), | |
207 | ); | |
208 | $civiSchema[$type . 'Model']['sections'] += array( | |
209 | 'formatting' => array( | |
210 | 'title' => ts('Formatting'), | |
211 | 'is_addable' => FALSE, | |
212 | ), | |
213 | ); | |
214 | } | |
215 | } | |
216 | ||
6a488035 TO |
217 | return $civiSchema; |
218 | } | |
219 | ||
220 | /** | |
221 | * FIXME: Move to somewhere more useful | |
222 | * FIXME: Do real mapping of "types" | |
223 | * | |
5ce1712d TO |
224 | * @param string $extends |
225 | * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact". | |
226 | * @param string $title | |
227 | * A string to use in section headers. | |
228 | * @param array $availableFields | |
229 | * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type']. | |
a6c01b45 CW |
230 | * @return array |
231 | * with keys 'sections' and 'schema' | |
6a488035 TO |
232 | * @see js/model/crm.core.js |
233 | * @see js/model/crm.mappedcore.js | |
234 | */ | |
00be9182 | 235 | public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) { |
6a488035 TO |
236 | $locationFields = CRM_Core_BAO_UFGroup::getLocationFields(); |
237 | ||
c5c263ca AH |
238 | // schema in format array($fieldName => $fieldSchema) |
239 | // sections in format array($sectionName => $section) | |
6a488035 | 240 | $result = array( |
c5c263ca AH |
241 | 'schema' => array(), |
242 | 'sections' => array(), | |
6a488035 TO |
243 | ); |
244 | ||
245 | // build field list | |
246 | foreach ($availableFields as $fieldName => $field) { | |
247 | switch ($extends) { | |
248 | case 'Individual': | |
249 | case 'Organization': | |
250 | case 'Household': | |
21fced3b | 251 | if ($field['field_type'] != $extends && $field['field_type'] != 'Contact' |
252 | //CRM-15595 check if subtype | |
353ffa53 TO |
253 | && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends)) |
254 | ) { | |
6a488035 TO |
255 | continue 2; |
256 | } | |
257 | break; | |
9147c186 | 258 | |
6a488035 TO |
259 | default: |
260 | if ($field['field_type'] != $extends) { | |
261 | continue 2; | |
262 | } | |
263 | } | |
c5c263ca | 264 | // FIXME: type set to "Text" |
6a488035 | 265 | $result['schema'][$fieldName] = array( |
c5c263ca | 266 | 'type' => 'Text', |
6a488035 TO |
267 | 'title' => $field['title'], |
268 | 'civiFieldType' => $field['field_type'], | |
269 | ); | |
270 | if (in_array($fieldName, $locationFields)) { | |
271 | $result['schema'][$fieldName]['civiIsLocation'] = TRUE; | |
272 | } | |
1a228b4c | 273 | if ($fieldName == 'url') { |
1da6c733 | 274 | $result['schema'][$fieldName]['civiIsWebsite'] = TRUE; |
1a228b4c | 275 | } |
971d41b1 CW |
276 | if (in_array($fieldName, array('phone', 'phone_and_ext'))) { |
277 | // FIXME what about phone_ext? | |
6a488035 TO |
278 | $result['schema'][$fieldName]['civiIsPhone'] = TRUE; |
279 | } | |
280 | } | |
281 | ||
282 | // build section list | |
283 | $result['sections']['default'] = array( | |
284 | 'title' => $title, | |
285 | 'is_addable' => FALSE, | |
286 | ); | |
287 | ||
288 | $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); | |
289 | $customGroup->orderBy('weight'); | |
290 | $customGroup->is_active = 1; | |
291 | $customGroup->find(); | |
292 | while ($customGroup->fetch()) { | |
293 | $sectionName = 'cg_' . $customGroup->id; | |
294 | $section = array( | |
295 | 'title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)), | |
0249f58e | 296 | 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE, |
6a488035 TO |
297 | 'custom_group_id' => $customGroup->id, |
298 | 'extends_entity_column_id' => $customGroup->extends_entity_column_id, | |
299 | 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value), | |
db7b725d | 300 | 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE, |
6a488035 TO |
301 | ); |
302 | $result['sections'][$sectionName] = $section; | |
303 | } | |
304 | ||
305 | // put fields in their sections | |
306 | $fields = CRM_Core_BAO_CustomField::getFields($extends); | |
307 | foreach ($fields as $fieldId => $field) { | |
308 | $sectionName = 'cg_' . $field['custom_group_id']; | |
309 | $fieldName = 'custom_' . $fieldId; | |
310 | if (isset($result['schema'][$fieldName])) { | |
311 | $result['schema'][$fieldName]['section'] = $sectionName; | |
312 | $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId); | |
313 | } | |
314 | } | |
315 | return $result; | |
316 | } | |
96025800 | 317 | |
6a488035 | 318 | } |