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() | |
9b873358 | 22 | ->addSettingsFactory(function() { |
6a488035 TO |
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 | * | |
5ce1712d TO |
67 | * @param array $entityTypes |
68 | * Strings, e.g. "IndividualModel", "ActivityModel". | |
6a488035 | 69 | */ |
00be9182 | 70 | public static function registerSchemas($entityTypes) { |
6a488035 TO |
71 | // TODO in cases where registerSchemas is called multiple times for same entity, be more efficient |
72 | CRM_Core_Resources::singleton()->addSettingsFactory(function () use ($entityTypes) { | |
73 | return array( | |
74 | 'civiSchema' => CRM_UF_Page_ProfileEditor::getSchema($entityTypes), | |
75 | ); | |
76 | }); | |
77 | } | |
78 | ||
79 | /** | |
80 | * AJAX callback | |
81 | */ | |
00be9182 | 82 | public static function getSchemaJSON() { |
6a488035 | 83 | $entityTypes = explode(',', $_REQUEST['entityTypes']); |
ecdef330 | 84 | CRM_Utils_JSON::output(self::getSchema($entityTypes)); |
6a488035 TO |
85 | } |
86 | ||
87 | /** | |
88 | * Get a list of Backbone-Form models | |
89 | * | |
5ce1712d TO |
90 | * @param array $entityTypes |
91 | * Model names ("IndividualModel"). | |
77b97be7 EM |
92 | * |
93 | * @throws CRM_Core_Exception | |
6a488035 TO |
94 | * @return array; keys are model names ("IndividualModel") and values describe 'sections' and 'schema' |
95 | * @see js/model/crm.core.js | |
96 | * @see js/model/crm.mappedcore.js | |
97 | */ | |
00be9182 | 98 | public static function getSchema($entityTypes) { |
6a488035 TO |
99 | // FIXME: Depending on context (eg civicrm/profile/create vs search-columns), it may be appropriate to |
100 | // pick importable or exportable fields | |
101 | ||
102 | $entityTypes = array_unique($entityTypes); | |
103 | $availableFields = NULL; | |
104 | foreach ($entityTypes as $entityType) { | |
105 | if (!$availableFields) { | |
106 | $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); | |
107 | //dpm($availableFields); | |
108 | } | |
109 | switch ($entityType) { | |
110 | case 'IndividualModel': | |
111 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
112 | 'Individual', | |
113 | ts('Individual'), | |
114 | $availableFields | |
115 | ); | |
116 | break; | |
9147c186 | 117 | |
133e2c99 | 118 | case 'OrganizationModel': |
119 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
120 | 'Organization', | |
121 | ts('Organization'), | |
122 | $availableFields | |
123 | ); | |
bf86535e | 124 | break; |
9147c186 | 125 | |
bf86535e | 126 | case 'HouseholdModel': |
127 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
128 | 'Household', | |
129 | ts('Household'), | |
130 | $availableFields | |
131 | ); | |
132 | break; | |
9147c186 | 133 | |
6a488035 TO |
134 | case 'ActivityModel': |
135 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
136 | 'Activity', | |
137 | ts('Activity'), | |
138 | $availableFields | |
139 | ); | |
140 | break; | |
9147c186 | 141 | |
6a488035 TO |
142 | case 'ContributionModel': |
143 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
144 | 'Contribution', | |
145 | ts('Contribution'), | |
146 | $availableFields | |
147 | ); | |
148 | break; | |
9147c186 | 149 | |
6a488035 TO |
150 | case 'MembershipModel': |
151 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
152 | 'Membership', | |
153 | ts('Membership'), | |
154 | $availableFields | |
155 | ); | |
156 | break; | |
9147c186 | 157 | |
6a488035 TO |
158 | case 'ParticipantModel': |
159 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
160 | 'Participant', | |
161 | ts('Participant'), | |
162 | $availableFields | |
163 | ); | |
164 | break; | |
9147c186 | 165 | |
cf1182e6 N |
166 | case 'CaseModel': |
167 | $civiSchema[$entityType] = self::convertCiviModelToBackboneModel( | |
168 | 'Case', | |
169 | ts('Case'), | |
170 | $availableFields | |
171 | ); | |
172 | break; | |
9147c186 | 173 | |
6a488035 TO |
174 | default: |
175 | throw new CRM_Core_Exception("Unrecognized entity type: $entityType"); | |
176 | } | |
177 | } | |
178 | ||
179 | return $civiSchema; | |
180 | } | |
181 | ||
182 | /** | |
183 | * FIXME: Move to somewhere more useful | |
184 | * FIXME: Do real mapping of "types" | |
185 | * | |
5ce1712d TO |
186 | * @param string $extends |
187 | * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact". | |
188 | * @param string $title | |
189 | * A string to use in section headers. | |
190 | * @param array $availableFields | |
191 | * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type']. | |
a6c01b45 CW |
192 | * @return array |
193 | * with keys 'sections' and 'schema' | |
6a488035 TO |
194 | * @see js/model/crm.core.js |
195 | * @see js/model/crm.mappedcore.js | |
196 | */ | |
00be9182 | 197 | public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) { |
6a488035 TO |
198 | $locationFields = CRM_Core_BAO_UFGroup::getLocationFields(); |
199 | ||
200 | $result = array( | |
201 | 'schema' => array(), // array($fieldName => $fieldSchema) | |
202 | 'sections' => array(), // array($sectionName => $section) | |
203 | ); | |
204 | ||
205 | // build field list | |
206 | foreach ($availableFields as $fieldName => $field) { | |
207 | switch ($extends) { | |
208 | case 'Individual': | |
209 | case 'Organization': | |
210 | case 'Household': | |
21fced3b | 211 | if ($field['field_type'] != $extends && $field['field_type'] != 'Contact' |
212 | //CRM-15595 check if subtype | |
213 | && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))) { | |
6a488035 TO |
214 | continue 2; |
215 | } | |
216 | break; | |
9147c186 | 217 | |
6a488035 TO |
218 | default: |
219 | if ($field['field_type'] != $extends) { | |
220 | continue 2; | |
221 | } | |
222 | } | |
223 | $result['schema'][$fieldName] = array( | |
224 | 'type' => 'Text', // FIXME, | |
225 | 'title' => $field['title'], | |
226 | 'civiFieldType' => $field['field_type'], | |
227 | ); | |
228 | if (in_array($fieldName, $locationFields)) { | |
229 | $result['schema'][$fieldName]['civiIsLocation'] = TRUE; | |
230 | } | |
231 | if (in_array($fieldName, array('phone', 'phone_and_ext'))) { // FIXME what about phone_ext? | |
232 | $result['schema'][$fieldName]['civiIsPhone'] = TRUE; | |
233 | } | |
234 | } | |
235 | ||
236 | // build section list | |
237 | $result['sections']['default'] = array( | |
238 | 'title' => $title, | |
239 | 'is_addable' => FALSE, | |
240 | ); | |
241 | ||
242 | $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); | |
243 | $customGroup->orderBy('weight'); | |
244 | $customGroup->is_active = 1; | |
245 | $customGroup->find(); | |
246 | while ($customGroup->fetch()) { | |
247 | $sectionName = 'cg_' . $customGroup->id; | |
248 | $section = array( | |
249 | 'title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)), | |
0249f58e | 250 | 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE, |
6a488035 TO |
251 | 'custom_group_id' => $customGroup->id, |
252 | 'extends_entity_column_id' => $customGroup->extends_entity_column_id, | |
253 | 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value), | |
db7b725d | 254 | 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE, |
6a488035 TO |
255 | ); |
256 | $result['sections'][$sectionName] = $section; | |
257 | } | |
258 | ||
259 | // put fields in their sections | |
260 | $fields = CRM_Core_BAO_CustomField::getFields($extends); | |
261 | foreach ($fields as $fieldId => $field) { | |
262 | $sectionName = 'cg_' . $field['custom_group_id']; | |
263 | $fieldName = 'custom_' . $fieldId; | |
264 | if (isset($result['schema'][$fieldName])) { | |
265 | $result['schema'][$fieldName]['section'] = $sectionName; | |
266 | $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId); | |
267 | } | |
268 | } | |
269 | return $result; | |
270 | } | |
271 | } |