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 = []; |
37375016 | 32 | var usedByFilter = null; |
6a488035 TO |
33 | if (options.groupTypeFilter) { |
34 | matchingUfGroups = ufGroupCollection.subcollection({ | |
35 | filter: function(ufGroupModel) { | |
37375016 | 36 | if(!$.isEmptyObject(options.usedByFilter)) { |
37 | usedByFilter = options.usedByFilter; | |
38 | } | |
39 | return ufGroupModel.checkGroupType(options.groupTypeFilter, options.allowAllSubtypes, usedByFilter); | |
6a488035 TO |
40 | } |
41 | }); | |
42 | } else { | |
43 | matchingUfGroups = ufGroupCollection; | |
44 | } | |
45 | ||
99e239bc | 46 | //CRM-15427 check for valid subtypes raise a warning if not valid |
47 | if (options.allowAllSubtypes && $.isEmptyObject(validTypesId)) { | |
48 | validTypes = ufGroupCollection.subcollection({ | |
49 | filter: function(ufGroupModel) { | |
50 | return ufGroupModel.checkGroupType(options.groupTypeFilter); | |
51 | } | |
52 | }); | |
53 | _.each(validTypes.models, function(validTypesattr) { | |
54 | validTypesId.push(validTypesattr.id); | |
55 | }); | |
56 | } | |
57 | if (!$.isEmptyObject(validTypesId) && $.inArray($select.val(), validTypesId) == -1) { | |
58 | var civiComponent; | |
59 | if (options.groupTypeFilter.indexOf('Membership') !== -1) { | |
60 | civiComponent = 'Membership'; | |
61 | } | |
62 | else if (options.groupTypeFilter.indexOf('Participant') !== -1) { | |
63 | civiComponent = 'Event'; | |
64 | } | |
65 | else { | |
66 | civiComponent = 'Contribution'; | |
67 | } | |
0c3fb122 | 68 | 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 | 69 | } |
6a488035 | 70 | var view = new CRM.ProfileSelector.View({ |
8e515d57 | 71 | ufGroupId: $select.val(), |
6a488035 TO |
72 | ufGroupCollection: matchingUfGroups, |
73 | ufEntities: options.entities | |
74 | }); | |
75 | view.on('change:ufGroupId', function() { | |
8e515d57 | 76 | $select.val(view.getUfGroupId()).change(); |
a572de36 | 77 | }); |
6a488035 | 78 | view.render(); |
8e515d57 | 79 | $select.after(view.el); |
6a488035 TO |
80 | setTimeout(function() { |
81 | view.doPreview(); | |
82 | }, 100); | |
83 | }); | |
84 | }; | |
85 | ||
8547369d | 86 | $('#crm-container').on('crmLoad', function() { |
8e515d57 | 87 | $('.crm-profile-selector:not(.rendered)', this).each(function() { |
6a488035 | 88 | $(this).crmProfileSelector({ |
8e515d57 | 89 | groupTypeFilter: $(this).data('groupType'), |
99e239bc | 90 | entities: $(this).data('entities'), |
91 | //CRM-15427 | |
37375016 | 92 | allowAllSubtypes: $(this).data('default'), |
93 | usedByFilter: $(this).data('usedfor') | |
6a488035 TO |
94 | }); |
95 | }); | |
8547369d | 96 | }); |
6a488035 | 97 | |
4b513f23 | 98 | })(CRM.$, CRM._); |