| 1 | (function($, _) { |
| 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. |
| 28 | var matchingUfGroups, |
| 29 | $select = $(this).hide().addClass('rendered'); |
| 30 | |
| 31 | var validTypesId = []; |
| 32 | var usedByFilter = null; |
| 33 | if (options.groupTypeFilter) { |
| 34 | matchingUfGroups = ufGroupCollection.subcollection({ |
| 35 | filter: function(ufGroupModel) { |
| 36 | //CRM-16915 - filter with module used by the profile |
| 37 | if (options.usedByFilter && options.usedByFilter.length) { |
| 38 | usedByFilter = options.usedByFilter; |
| 39 | } |
| 40 | return ufGroupModel.checkGroupType(options.groupTypeFilter, options.allowAllSubtypes, usedByFilter); |
| 41 | } |
| 42 | }); |
| 43 | } else { |
| 44 | matchingUfGroups = ufGroupCollection; |
| 45 | } |
| 46 | |
| 47 | //CRM-15427 check for valid subtypes raise a warning if not valid |
| 48 | if (options.allowAllSubtypes && !validTypesId.length) { |
| 49 | validTypes = ufGroupCollection.subcollection({ |
| 50 | filter: function(ufGroupModel) { |
| 51 | return ufGroupModel.checkGroupType(options.groupTypeFilter); |
| 52 | } |
| 53 | }); |
| 54 | _.each(validTypes.models, function(validTypesattr) { |
| 55 | validTypesId.push(validTypesattr.id); |
| 56 | }); |
| 57 | } |
| 58 | if (validTypesId.length && $.inArray($select.val(), validTypesId) == -1) { |
| 59 | var civiComponent; |
| 60 | if (options.groupTypeFilter.indexOf('Membership') !== -1) { |
| 61 | civiComponent = 'Membership'; |
| 62 | } |
| 63 | else if (options.groupTypeFilter.indexOf('Participant') !== -1) { |
| 64 | civiComponent = 'Event'; |
| 65 | } |
| 66 | else { |
| 67 | civiComponent = 'Contribution'; |
| 68 | } |
| 69 | CRM.alert(ts('The selected profile is using a custom field which is not assigned to the "%1" being configured.', {1: civiComponent}), ts('Warning')); |
| 70 | } |
| 71 | var view = new CRM.ProfileSelector.View({ |
| 72 | ufGroupId: $select.val(), |
| 73 | ufGroupCollection: matchingUfGroups, |
| 74 | ufEntities: options.entities |
| 75 | }); |
| 76 | view.on('change:ufGroupId', function() { |
| 77 | $select.val(view.getUfGroupId()).change(); |
| 78 | }); |
| 79 | view.render(); |
| 80 | $select.after(view.el); |
| 81 | setTimeout(function() { |
| 82 | view.doPreview(); |
| 83 | }, 100); |
| 84 | }); |
| 85 | }; |
| 86 | |
| 87 | $('#crm-container').on('crmLoad', function() { |
| 88 | $('.crm-profile-selector:not(.rendered)', this).each(function() { |
| 89 | $(this).crmProfileSelector({ |
| 90 | groupTypeFilter: $(this).data('groupType'), |
| 91 | entities: $(this).data('entities'), |
| 92 | //CRM-15427 |
| 93 | allowAllSubtypes: $(this).data('default'), |
| 94 | usedByFilter: $(this).data('usedfor') |
| 95 | }); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | })(CRM.$, CRM._); |