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 { | |
00be9182 | 10 | public function run() { |
6a488035 TO |
11 | CRM_Core_Error::fatal('This is not a real page!'); |
12 | } | |
13 | ||
00be9182 | 14 | public static function registerProfileScripts() { |
6a488035 | 15 | static $loaded = FALSE; |
daf0f4f3 | 16 | if ($loaded || CRM_Core_Resources::isAjaxMode()) { |
6a488035 TO |
17 | return; |
18 | } | |
19 | $loaded = TRUE; | |
20 | ||
21 | CRM_Core_Resources::singleton() | |
353ffa53 | 22 | ->addSettingsFactory(function () { |
37375016 | 23 | $ufGroups = civicrm_api3('UFGroup', 'get', array( |
24 | 'sequential' => 1, | |
25 | 'is_active' => 1, | |
58770077 | 26 | 'options' => array('limit' => 0), |
37375016 | 27 | )); |
58770077 | 28 | //CRM-16915 - insert 'module' param for the profile used by CiviEvent. |
37375016 | 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)) { | |
33 | $ufGroups['values'][$key]['module'] = 'CiviEvent'; | |
34 | } | |
35 | } | |
36 | } | |
6a488035 TO |
37 | return array( |
38 | 'PseudoConstant' => array( | |
b2b0530a | 39 | 'locationType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), |
1da6c733 | 40 | 'websiteType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'), |
b4f964d9 | 41 | 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
6a488035 | 42 | ), |
37375016 | 43 | 'initialProfileList' => $ufGroups, |
21fced3b | 44 | 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(), |
6a488035 TO |
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) | |
6a488035 TO |
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 | * | |
5ce1712d TO |
77 | * @param array $entityTypes |
78 | * Strings, e.g. "IndividualModel", "ActivityModel". | |
6a488035 | 79 | */ |
00be9182 | 80 | public static function registerSchemas($entityTypes) { |
6a488035 TO |
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 | /** | |
fe482240 | 90 | * AJAX callback. |
6a488035 | 91 | */ |
00be9182 | 92 | public static function getSchemaJSON() { |
6a488035 | 93 | $entityTypes = explode(',', $_REQUEST['entityTypes']); |
ecdef330 | 94 | CRM_Utils_JSON::output(self::getSchema($entityTypes)); |
6a488035 TO |
95 | } |
96 | ||
97 | /** | |
98 | * Get a list of Backbone-Form models | |
99 | * | |
5ce1712d TO |
100 | * @param array $entityTypes |
101 | * Model names ("IndividualModel"). | |
77b97be7 EM |
102 | * |
103 | * @throws CRM_Core_Exception | |
6a488035 TO |
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 | */ | |
00be9182 | 108 | public static function getSchema($entityTypes) { |
6a488035 TO |
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; | |
0103eaf0 | 114 | $civiSchema = array(); |
6a488035 TO |
115 | foreach ($entityTypes as $entityType) { |
116 | if (!$availableFields) { | |
117 | $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); | |
6a488035 TO |
118 | } |
119 | switch ($entityType) { | |
120 | case 'IndividualModel': | |
121 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
122 | 'Individual', | |
123 | ts('Individual'), | |
124 | $availableFields | |
125 | ); | |
126 | break; | |
9147c186 | 127 | |
133e2c99 | 128 | case 'OrganizationModel': |
129 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
130 | 'Organization', | |
131 | ts('Organization'), | |
132 | $availableFields | |
133 | ); | |
bf86535e | 134 | break; |
9147c186 | 135 | |
bf86535e | 136 | case 'HouseholdModel': |
137 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
138 | 'Household', | |
139 | ts('Household'), | |
140 | $availableFields | |
141 | ); | |
142 | break; | |
9147c186 | 143 | |
6a488035 TO |
144 | case 'ActivityModel': |
145 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
146 | 'Activity', | |
147 | ts('Activity'), | |
148 | $availableFields | |
149 | ); | |
150 | break; | |
9147c186 | 151 | |
6a488035 TO |
152 | case 'ContributionModel': |
153 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
154 | 'Contribution', | |
155 | ts('Contribution'), | |
156 | $availableFields | |
157 | ); | |
158 | break; | |
9147c186 | 159 | |
6a488035 TO |
160 | case 'MembershipModel': |
161 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
162 | 'Membership', | |
163 | ts('Membership'), | |
164 | $availableFields | |
165 | ); | |
166 | break; | |
9147c186 | 167 | |
6a488035 TO |
168 | case 'ParticipantModel': |
169 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
170 | 'Participant', | |
171 | ts('Participant'), | |
172 | $availableFields | |
173 | ); | |
174 | break; | |
9147c186 | 175 | |
cf1182e6 N |
176 | case 'CaseModel': |
177 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
178 | 'Case', | |
179 | ts('Case'), | |
180 | $availableFields | |
181 | ); | |
182 | break; | |
9147c186 | 183 | |
6a488035 TO |
184 | default: |
185 | throw new CRM_Core_Exception("Unrecognized entity type: $entityType"); | |
186 | } | |
187 | } | |
188 | ||
0103eaf0 CW |
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 | ||
6a488035 TO |
209 | return $civiSchema; |
210 | } | |
211 | ||
212 | /** | |
213 | * FIXME: Move to somewhere more useful | |
214 | * FIXME: Do real mapping of "types" | |
215 | * | |
5ce1712d TO |
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']. | |
a6c01b45 CW |
222 | * @return array |
223 | * with keys 'sections' and 'schema' | |
6a488035 TO |
224 | * @see js/model/crm.core.js |
225 | * @see js/model/crm.mappedcore.js | |
226 | */ | |
00be9182 | 227 | public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) { |
6a488035 TO |
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': | |
21fced3b | 241 | if ($field['field_type'] != $extends && $field['field_type'] != 'Contact' |
242 | //CRM-15595 check if subtype | |
353ffa53 TO |
243 | && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends)) |
244 | ) { | |
6a488035 TO |
245 | continue 2; |
246 | } | |
247 | break; | |
9147c186 | 248 | |
6a488035 TO |
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 | } | |
1a228b4c | 262 | if ($fieldName == 'url') { |
1da6c733 | 263 | $result['schema'][$fieldName]['civiIsWebsite'] = TRUE; |
1a228b4c | 264 | } |
971d41b1 CW |
265 | if (in_array($fieldName, array('phone', 'phone_and_ext'))) { |
266 | // FIXME what about phone_ext? | |
6a488035 TO |
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)), | |
0249f58e | 285 | 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE, |
6a488035 TO |
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), | |
db7b725d | 289 | 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE, |
6a488035 TO |
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 | } | |
96025800 | 306 | |
6a488035 | 307 | } |