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() | |
22 | ->addSettingsFactory(function(){ | |
23 | return array( | |
24 | 'PseudoConstant' => array( | |
b2b0530a | 25 | 'locationType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), |
b4f964d9 | 26 | 'phoneType' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
6a488035 TO |
27 | ), |
28 | 'initialProfileList' => civicrm_api('UFGroup', 'get', array( | |
29 | 'version' => 3, | |
30 | 'sequential' => 1, | |
342936e2 | 31 | 'is_active' => 1, |
6a488035 TO |
32 | 'rowCount' => 1000, // FIXME |
33 | )), | |
21fced3b | 34 | 'contactSubTypes' => CRM_Contact_BAO_ContactType::subTypes(), |
6a488035 TO |
35 | 'profilePreviewKey' => CRM_Core_Key::get('CRM_UF_Form_Inline_Preview', TRUE), |
36 | ); | |
37 | }) | |
38 | ->addScriptFile('civicrm', 'packages/backbone/json2.js', 100, 'html-header', FALSE) | |
6a488035 TO |
39 | ->addScriptFile('civicrm', 'packages/backbone/backbone.js', 120, 'html-header') |
40 | ->addScriptFile('civicrm', 'packages/backbone/backbone.marionette.js', 125, 'html-header', FALSE) | |
41 | ->addScriptFile('civicrm', 'packages/backbone/backbone.collectionsubset.js', 125, 'html-header', FALSE) | |
42 | ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/backbone-forms.js', 130, 'html-header', FALSE) | |
43 | ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/adapters/backbone.bootstrap-modal.min.js', 140, 'html-header', FALSE) | |
44 | ->addScriptFile('civicrm', 'packages/backbone-forms/distribution/editors/list.min.js', 140, 'html-header', FALSE) | |
45 | ->addStyleFile('civicrm', 'packages/backbone-forms/distribution/templates/default.css', 140, 'html-header') | |
46 | ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE) | |
47 | ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header') | |
48 | ->addStyleFile('civicrm', 'css/crm.designer.css', 140, 'html-header') | |
49 | ->addScriptFile('civicrm', 'js/crm.backbone.js', 150) | |
50 | ->addScriptFile('civicrm', 'js/model/crm.schema-mapped.js', 200) | |
51 | ->addScriptFile('civicrm', 'js/model/crm.uf.js', 200) | |
52 | ->addScriptFile('civicrm', 'js/model/crm.designer.js', 200) | |
53 | ->addScriptFile('civicrm', 'js/model/crm.profile-selector.js', 200) | |
54 | ->addScriptFile('civicrm', 'js/view/crm.designer.js', 200) | |
55 | ->addScriptFile('civicrm', 'js/view/crm.profile-selector.js', 200) | |
56 | ->addScriptFile('civicrm', 'js/jquery/jquery.crmProfileSelector.js', 250) | |
57 | ->addScriptFile('civicrm', 'js/crm.designerapp.js', 250); | |
58 | ||
59 | CRM_Core_Region::instance('page-header')->add(array( | |
60 | 'template' => 'CRM/UF/Page/ProfileTemplates.tpl', | |
61 | )); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Register entity schemas for use in the editor's palette | |
66 | * | |
67 | * @param array $entityTypes strings, e.g. "IndividualModel", "ActivityModel" | |
68 | */ | |
00be9182 | 69 | public static function registerSchemas($entityTypes) { |
6a488035 TO |
70 | // TODO in cases where registerSchemas is called multiple times for same entity, be more efficient |
71 | CRM_Core_Resources::singleton()->addSettingsFactory(function () use ($entityTypes) { | |
72 | return array( | |
73 | 'civiSchema' => CRM_UF_Page_ProfileEditor::getSchema($entityTypes), | |
74 | ); | |
75 | }); | |
76 | } | |
77 | ||
78 | /** | |
79 | * AJAX callback | |
80 | */ | |
00be9182 | 81 | public static function getSchemaJSON() { |
6a488035 | 82 | $entityTypes = explode(',', $_REQUEST['entityTypes']); |
ecdef330 | 83 | CRM_Utils_JSON::output(self::getSchema($entityTypes)); |
6a488035 TO |
84 | } |
85 | ||
86 | /** | |
87 | * Get a list of Backbone-Form models | |
88 | * | |
89 | * @param array $entityTypes model names ("IndividualModel") | |
77b97be7 EM |
90 | * |
91 | * @throws CRM_Core_Exception | |
6a488035 TO |
92 | * @return array; keys are model names ("IndividualModel") and values describe 'sections' and 'schema' |
93 | * @see js/model/crm.core.js | |
94 | * @see js/model/crm.mappedcore.js | |
95 | */ | |
00be9182 | 96 | public static function getSchema($entityTypes) { |
6a488035 TO |
97 | // FIXME: Depending on context (eg civicrm/profile/create vs search-columns), it may be appropriate to |
98 | // pick importable or exportable fields | |
99 | ||
100 | $entityTypes = array_unique($entityTypes); | |
101 | $availableFields = NULL; | |
102 | foreach ($entityTypes as $entityType) { | |
103 | if (!$availableFields) { | |
104 | $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); | |
105 | //dpm($availableFields); | |
106 | } | |
107 | switch ($entityType) { | |
108 | case 'IndividualModel': | |
109 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
110 | 'Individual', | |
111 | ts('Individual'), | |
112 | $availableFields | |
113 | ); | |
114 | break; | |
133e2c99 | 115 | case 'OrganizationModel': |
116 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
117 | 'Organization', | |
118 | ts('Organization'), | |
119 | $availableFields | |
120 | ); | |
bf86535e | 121 | break; |
122 | case 'HouseholdModel': | |
123 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
124 | 'Household', | |
125 | ts('Household'), | |
126 | $availableFields | |
127 | ); | |
128 | break; | |
6a488035 TO |
129 | case 'ActivityModel': |
130 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
131 | 'Activity', | |
132 | ts('Activity'), | |
133 | $availableFields | |
134 | ); | |
135 | break; | |
136 | case 'ContributionModel': | |
137 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
138 | 'Contribution', | |
139 | ts('Contribution'), | |
140 | $availableFields | |
141 | ); | |
142 | break; | |
143 | case 'MembershipModel': | |
144 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
145 | 'Membership', | |
146 | ts('Membership'), | |
147 | $availableFields | |
148 | ); | |
149 | break; | |
150 | case 'ParticipantModel': | |
151 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
152 | 'Participant', | |
153 | ts('Participant'), | |
154 | $availableFields | |
155 | ); | |
156 | break; | |
cf1182e6 N |
157 | case 'CaseModel': |
158 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
159 | 'Case', | |
160 | ts('Case'), | |
161 | $availableFields | |
162 | ); | |
163 | break; | |
6a488035 TO |
164 | default: |
165 | throw new CRM_Core_Exception("Unrecognized entity type: $entityType"); | |
166 | } | |
167 | } | |
168 | ||
169 | return $civiSchema; | |
170 | } | |
171 | ||
172 | /** | |
173 | * FIXME: Move to somewhere more useful | |
174 | * FIXME: Do real mapping of "types" | |
175 | * | |
176 | * @param string $extends entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact" | |
177 | * @param string $title a string to use in section headers | |
178 | * @param array $availableFields list of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type'] | |
179 | * @return array with keys 'sections' and 'schema' | |
180 | * @see js/model/crm.core.js | |
181 | * @see js/model/crm.mappedcore.js | |
182 | */ | |
00be9182 | 183 | public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) { |
6a488035 TO |
184 | $locationFields = CRM_Core_BAO_UFGroup::getLocationFields(); |
185 | ||
186 | $result = array( | |
187 | 'schema' => array(), // array($fieldName => $fieldSchema) | |
188 | 'sections' => array(), // array($sectionName => $section) | |
189 | ); | |
190 | ||
191 | // build field list | |
192 | foreach ($availableFields as $fieldName => $field) { | |
193 | switch ($extends) { | |
194 | case 'Individual': | |
195 | case 'Organization': | |
196 | case 'Household': | |
21fced3b | 197 | if ($field['field_type'] != $extends && $field['field_type'] != 'Contact' |
198 | //CRM-15595 check if subtype | |
199 | && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))) { | |
6a488035 TO |
200 | continue 2; |
201 | } | |
202 | break; | |
203 | default: | |
204 | if ($field['field_type'] != $extends) { | |
205 | continue 2; | |
206 | } | |
207 | } | |
208 | $result['schema'][$fieldName] = array( | |
209 | 'type' => 'Text', // FIXME, | |
210 | 'title' => $field['title'], | |
211 | 'civiFieldType' => $field['field_type'], | |
212 | ); | |
213 | if (in_array($fieldName, $locationFields)) { | |
214 | $result['schema'][$fieldName]['civiIsLocation'] = TRUE; | |
215 | } | |
216 | if (in_array($fieldName, array('phone', 'phone_and_ext'))) { // FIXME what about phone_ext? | |
217 | $result['schema'][$fieldName]['civiIsPhone'] = TRUE; | |
218 | } | |
219 | } | |
220 | ||
221 | // build section list | |
222 | $result['sections']['default'] = array( | |
223 | 'title' => $title, | |
224 | 'is_addable' => FALSE, | |
225 | ); | |
226 | ||
227 | $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); | |
228 | $customGroup->orderBy('weight'); | |
229 | $customGroup->is_active = 1; | |
230 | $customGroup->find(); | |
231 | while ($customGroup->fetch()) { | |
232 | $sectionName = 'cg_' . $customGroup->id; | |
233 | $section = array( | |
234 | 'title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)), | |
0249f58e | 235 | 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE, |
6a488035 TO |
236 | 'custom_group_id' => $customGroup->id, |
237 | 'extends_entity_column_id' => $customGroup->extends_entity_column_id, | |
238 | 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value), | |
db7b725d | 239 | 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE, |
6a488035 TO |
240 | ); |
241 | $result['sections'][$sectionName] = $section; | |
242 | } | |
243 | ||
244 | // put fields in their sections | |
245 | $fields = CRM_Core_BAO_CustomField::getFields($extends); | |
246 | foreach ($fields as $fieldId => $field) { | |
247 | $sectionName = 'cg_' . $field['custom_group_id']; | |
248 | $fieldName = 'custom_' . $fieldId; | |
249 | if (isset($result['schema'][$fieldName])) { | |
250 | $result['schema'][$fieldName]['section'] = $sectionName; | |
251 | $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId); | |
252 | } | |
253 | } | |
254 | return $result; | |
255 | } | |
256 | } |