Commit | Line | Data |
---|---|---|
4b513f23 | 1 | (function($, _) { |
6a488035 TO |
2 | var ufGroupCollection = new CRM.UF.UFGroupCollection(_.sortBy(CRM.initialProfileList.values, 'title')); |
3 | //var ufGroupCollection = new CRM.UF.UFGroupCollection(CRM.initialProfileList.values, { | |
4 | // comparator: 'title' // no point, this doesn't work with subcollections | |
5 | //}); | |
6 | ufGroupCollection.unshift(new CRM.UF.UFGroupModel({ | |
7 | id: '', | |
8 | title: ts('- select -') | |
9 | })); | |
10 | ||
11 | /** | |
12 | * Example: | |
13 | * <input type="text" value="{$profileId}" class="crm-profile-selector" /> | |
14 | * ... | |
15 | * cj('.crm-profile-selector').crmProfileSelector({ | |
16 | * groupTypeFilter: "Contact,Individual,Activity;;ActivityType:7", | |
17 | * entities: "contact_1:IndividualModel,activity_1:ActivityModel" | |
18 | * }); | |
19 | * | |
20 | * Note: The system does not currently support dynamic entities -- it only supports | |
21 | * a couple of entities named "contact_1" and "activity_1". See also | |
22 | * CRM.UF.guessEntityName(). | |
23 | */ | |
24 | $.fn.crmProfileSelector = function(options) { | |
25 | return this.each(function() { | |
26 | // Hide the existing <SELECT> and instead construct a ProfileSelector view. | |
27 | // Keep them synchronized. | |
8e515d57 CW |
28 | var matchingUfGroups, |
29 | $select = $(this).hide().addClass('rendered'); | |
6a488035 | 30 | |
99e239bc | 31 | var validTypesId = []; |
6a488035 TO |
32 | if (options.groupTypeFilter) { |
33 | matchingUfGroups = ufGroupCollection.subcollection({ | |
34 | filter: function(ufGroupModel) { | |
99e239bc | 35 | return ufGroupModel.checkGroupType(options.groupTypeFilter, options.allowAllSubtypes); |
6a488035 TO |
36 | } |
37 | }); | |
38 | } else { | |
39 | matchingUfGroups = ufGroupCollection; | |
40 | } | |
41 | ||
99e239bc | 42 | //CRM-15427 check for valid subtypes raise a warning if not valid |
43 | if (options.allowAllSubtypes && $.isEmptyObject(validTypesId)) { | |
44 | validTypes = ufGroupCollection.subcollection({ | |
45 | filter: function(ufGroupModel) { | |
46 | return ufGroupModel.checkGroupType(options.groupTypeFilter); | |
47 | } | |
48 | }); | |
49 | _.each(validTypes.models, function(validTypesattr) { | |
50 | validTypesId.push(validTypesattr.id); | |
51 | }); | |
52 | } | |
53 | if (!$.isEmptyObject(validTypesId) && $.inArray($select.val(), validTypesId) == -1) { | |
54 | var civiComponent; | |
55 | if (options.groupTypeFilter.indexOf('Membership') !== -1) { | |
56 | civiComponent = 'Membership'; | |
57 | } | |
58 | else if (options.groupTypeFilter.indexOf('Participant') !== -1) { | |
59 | civiComponent = 'Event'; | |
60 | } | |
61 | else { | |
62 | civiComponent = 'Contribution'; | |
63 | } | |
0c3fb122 | 64 | CRM.alert(ts('The selected profile is using a custom field which is not assigned to the "%1" being configured.', {1: civiComponent}), ts('Warning')); |
99e239bc | 65 | } |
6a488035 | 66 | var view = new CRM.ProfileSelector.View({ |
8e515d57 | 67 | ufGroupId: $select.val(), |
6a488035 TO |
68 | ufGroupCollection: matchingUfGroups, |
69 | ufEntities: options.entities | |
70 | }); | |
71 | view.on('change:ufGroupId', function() { | |
8e515d57 | 72 | $select.val(view.getUfGroupId()).change(); |
a572de36 | 73 | }); |
6a488035 | 74 | view.render(); |
8e515d57 | 75 | $select.after(view.el); |
6a488035 TO |
76 | setTimeout(function() { |
77 | view.doPreview(); | |
78 | }, 100); | |
79 | }); | |
80 | }; | |
81 | ||
8547369d | 82 | $('#crm-container').on('crmLoad', function() { |
8e515d57 | 83 | $('.crm-profile-selector:not(.rendered)', this).each(function() { |
6a488035 | 84 | $(this).crmProfileSelector({ |
8e515d57 | 85 | groupTypeFilter: $(this).data('groupType'), |
99e239bc | 86 | entities: $(this).data('entities'), |
87 | //CRM-15427 | |
88 | allowAllSubtypes: $(this).data('default') | |
6a488035 TO |
89 | }); |
90 | }); | |
8547369d | 91 | }); |
6a488035 | 92 | |
4b513f23 | 93 | })(CRM.$, CRM._); |