2 if (!CRM
.UF
) CRM
.UF
= {};
5 {val
: 0, label
: ts('No')},
6 {val
: 1, label
: ts('Yes')}
10 {val
: 'User and User Admin Only', label
: ts('User and User Admin Only'), isInSelectorAllowed
: false},
11 {val
: 'Public Pages', label
: ts('Public Pages'), isInSelectorAllowed
: true},
12 {val
: 'Public Pages and Listings', label
: ts('Public Pages and Listings'), isInSelectorAllowed
: true}
15 var LOCATION_TYPES
= _
.map(CRM
.PseudoConstant
.locationType
, function(value
, key
) {
16 return {val
: key
, label
: value
};
18 LOCATION_TYPES
.unshift({val
: '', label
: ts('Primary')});
19 var DEFAULT_LOCATION_TYPE_ID
= '';
21 var PHONE_TYPES
= _
.map(CRM
.PseudoConstant
.phoneType
, function(value
, key
) {
22 return {val
: key
, label
: value
};
25 var WEBSITE_TYPES
= _
.map(CRM
.PseudoConstant
.websiteType
, function(value
, key
) {
26 return {val
: key
, label
: value
};
28 var DEFAULT_PHONE_TYPE_ID
= PHONE_TYPES
[0].val
;
29 var DEFAULT_WEBSITE_TYPE_ID
= WEBSITE_TYPES
[0].val
;
32 * Add a help link to a form label
34 function addHelp(title
, options
) {
35 return title
+ ' <a href="#" onclick=\'CRM.help("' + title
+ '", ' + JSON
.stringify(options
) + '); return false;\' title="' + ts('%1 Help', {1: title
}) + '" class="helpicon"></a>';
38 function watchChanges() {
39 CRM
.designerApp
.vent
.trigger('ufUnsaved', true);
43 * Parse a "group_type" expression
45 * @param string groupTypeExpr example: "Individual,Activity\0ActivityType:2:28"
46 * Note: I've seen problems where HTML "�" != JS '\0', so we support ';;' as an equivalent delimiter
47 * @return Object example: {coreTypes: {"Individual":true,"Activity":true}, subTypes: {"ActivityType":{2: true, 28:true}]}}
49 CRM
.UF
.parseTypeList = function(groupTypeExpr
) {
50 var typeList
= {coreTypes
: {}, subTypes
:{}};
51 // The API may have automatically converted a string with '\0' to an array
52 var parts
= _
.isArray(groupTypeExpr
) ? groupTypeExpr
: groupTypeExpr
.replace(';;','\0').split('\0');
53 var coreTypesExpr
= parts
[0];
54 var subTypesExpr
= parts
[1];
56 if (!_
.isEmpty(coreTypesExpr
)) {
57 _
.each(coreTypesExpr
.split(','), function(coreType
){
58 typeList
.coreTypes
[coreType
] = true;
62 //CRM-15427 Allow Multiple subtype filtering
63 if (!_
.isEmpty(subTypesExpr
)) {
64 if (subTypesExpr
.indexOf(';;') !== -1) {
65 var subTypeparts
= subTypesExpr
.replace(/;;/g,'\0').split('\0');
66 _
.each(subTypeparts
, function(subTypepart
) {
67 var subTypes
= subTypepart
.split(':');
68 var subTypeKey
= subTypes
.shift();
69 typeList
.subTypes
[subTypeKey
] = {};
70 _
.each(subTypes
, function(subTypeId
) {
71 typeList
.subTypes
[subTypeKey
][subTypeId
] = true;
76 var subTypes
= subTypesExpr
.split(':');
77 var subTypeKey
= subTypes
.shift();
78 typeList
.subTypes
[subTypeKey
] = {};
79 _
.each(subTypes
, function(subTypeId
) {
80 typeList
.subTypes
[subTypeKey
][subTypeId
] = true;
88 * This function is a hack for generating simulated values of "entity_name"
89 * in the form-field model.
91 * @param {string} field_type
94 CRM
.UF
.guessEntityName = function(field_type
) {
104 return 'contribution_1';
106 return 'membership_1';
108 return 'participant_1';
112 if (!$.isEmptyObject(CRM
.contactSubTypes
) && ($.inArray(field_type
,CRM
.contactSubTypes
) > -1)) {
116 throw "Cannot guess entity name for field_type=" + field_type
;
122 * Represents a field in a customizable form.
124 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
126 * Backbone.Form descripton of the field to which this refers
132 * @var bool, non-persistent indication of whether this field is unique or duplicate
133 * within its UFFieldCollection
153 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
156 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
160 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
164 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
169 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
173 'is_multi_summary': {
174 title
: ts("Include in multi-record listing?"),
179 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
188 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
193 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
198 title
: ts('Field Label'),
201 'location_type_id': {
202 title
: ts('Location Type'),
204 options
: LOCATION_TYPES
207 title
: ts('Website Type'),
209 options
: WEBSITE_TYPES
212 title
: ts('Phone Type'),
217 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
225 initialize: function() {
226 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
227 this.on("rel:ufGroupModel", this.applyDefaults
, this);
228 this.on('change', watchChanges
);
230 applyDefaults: function() {
231 var fieldSchema
= this.getFieldSchema();
232 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
233 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
235 if (fieldSchema
&& fieldSchema
.civiIsWebsite
&& !this.get('website_type_id')) {
236 this.set('website_type_id', DEFAULT_WEBSITE_TYPE_ID
);
238 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
239 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
242 isInSelectorAllowed: function() {
243 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
245 return visibility
.isInSelectorAllowed
;
251 getFieldSchema: function() {
252 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
255 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
256 * have a unique signature.
260 getSignature: function() {
261 return this.get("entity_name") +
262 '::' + this.get("field_name") +
263 '::' + (this.get("location_type_id") ? this.get("location_type_id") : this.get("website_type_id") ? this.get("website_type_id") : '') +
264 '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
268 * This is like destroy(), but it only destroys the item on the client-side;
269 * it does not trigger REST or Backbone.sync() operations.
273 destroyLocal: function() {
274 this.trigger('destroy', this, this.collection
, {});
280 * Represents a list of fields in a customizable form
285 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
286 model
: CRM
.UF
.UFFieldModel
,
287 uf_group_id
: null, // int
288 initialize: function(models
, options
) {
289 options
= options
|| {};
290 this.uf_group_id
= options
.uf_group_id
;
291 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
292 this.on('add', this.watchDuplicates
, this);
293 this.on('remove', this.unwatchDuplicates
, this);
294 this.on('change', watchChanges
);
295 this.on('add', watchChanges
);
296 this.on('remove', watchChanges
);
298 getFieldsByName: function(entityName
, fieldName
) {
299 return this.filter(function(ufFieldModel
) {
300 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
303 toSortedJSON: function() {
304 var fields
= this.map(function(ufFieldModel
){
305 return ufFieldModel
.toStrictJSON();
307 return _
.sortBy(fields
, function(ufFieldJSON
){
308 return parseInt(ufFieldJSON
.weight
);
311 isAddable: function(ufFieldModel
) {
312 var entity_name
= ufFieldModel
.get('entity_name'),
313 field_name
= ufFieldModel
.get('field_name'),
314 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
319 var fields
= this.getFieldsByName(entity_name
, field_name
);
321 if (fieldSchema
.civiIsLocation
) {
322 limit
*= LOCATION_TYPES
.length
;
324 if (fieldSchema
.civiIsWebsite
) {
325 limit
*= WEBSITE_TYPES
.length
;
327 if (fieldSchema
.civiIsPhone
) {
328 limit
*= PHONE_TYPES
.length
;
330 return fields
.length
< limit
;
332 watchDuplicates: function(model
, collection
, options
) {
333 model
.on('change:location_type_id', this.markDuplicates
, this);
334 model
.on('change:website_type_id', this.markDuplicates
, this);
335 model
.on('change:phone_type_id', this.markDuplicates
, this);
336 this.markDuplicates();
338 unwatchDuplicates: function(model
, collection
, options
) {
339 model
.off('change:location_type_id', this.markDuplicates
, this);
340 model
.off('change:website_type_id', this.markDuplicates
, this);
341 model
.off('change:phone_type_id', this.markDuplicates
, this);
342 this.markDuplicates();
344 hasDuplicates: function() {
345 var firstDupe
= this.find(function(ufFieldModel
){
346 return ufFieldModel
.get('is_duplicate');
348 return firstDupe
? true : false;
353 markDuplicates: function() {
354 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
355 return ufFieldModel
.getSignature();
357 this.each(function(ufFieldModel
){
358 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
359 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
360 ufFieldModel
.set('is_duplicate', is_duplicate
);
367 * Represents an entity in a customizable form
369 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
376 title
: ts('Entity Name'),
377 help
: ts('Symbolic name which referenced in the fields'),
381 title
: ts('Entity Type'),
383 options
: ['IndividualModel', 'ActivityModel']
386 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
387 title
: ts('Sub Type'),
394 initialize: function() {
397 * Get a list of all fields that can be used with this entity.
399 * @return {Object} keys are field names; values are fieldSchemas
401 getFieldSchemas: function() {
402 var ufEntityModel
= this;
403 var modelClass
= this.getModelClass();
405 if (this.get('entity_sub_type') == '*') {
406 return _
.clone(modelClass
.prototype.schema
);
410 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
411 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
412 if (ufEntityModel
.isSectionEnabled(section
)) {
413 result
[fieldName
] = fieldSchema
;
418 isSectionEnabled: function(section
) {
420 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')) || this.get('entity_sub_type') == '*');
422 getSections: function() {
423 var ufEntityModel
= this;
425 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
426 if (ufEntityModel
.isSectionEnabled(section
)) {
427 result
[sectionKey
] = section
;
432 getModelClass: function() {
433 return CRM
.Schema
[this.get('entity_type')];
438 * Represents a list of entities in a customizable form
441 * - ufGroupModel: UFGroupModel
443 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
444 model
: CRM
.UF
.UFEntityModel
,
446 initialize: function(models
, options
) {
447 options
= options
|| {};
448 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
453 * @return {UFEntityModel} if found; otherwise, null
455 getByName: function(name
) {
456 // TODO consider indexing
457 return this.find(function(ufEntityModel
){
458 return ufEntityModel
.get('entity_name') == name
;
464 * Represents a customizable form
466 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
468 title
: ts('Unnamed Profile'),
481 title
: ts('Profile Name'),
484 validators
: ['required']
487 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
492 title
: ts('Include reCAPTCHA?'),
498 title
: ts('Add new contacts to a Group?'),
499 help
: ts('Select a group if you are using this profile for adding new contacts, AND you want the new contacts to be automatically assigned to a group.'),
503 title
: ts('Cancel Redirect URL'),
504 help
: ts('If you are using this profile as a contact signup or edit form, and want to redirect the user to a static URL if they click the Cancel button - enter the complete URL here. If this field is left blank, the built-in Profile form will be redisplayed.'),
516 title
: ts('Post-form Help'),
517 help
: ts('Explanatory text displayed at the end of the form.') +
518 ts('Note that this help text is displayed on profile create/edit screens only.'),
522 title
: ts('Pre-form Help'),
523 help
: ts('Explanatory text displayed at the beginning of the form.') +
524 ts('Note that this help text is displayed on profile create/edit screens only.'),
528 title
: ts('Is this CiviCRM Profile active?'),
533 title
: ts('Drupal user account registration option?'),// FIXME
536 options
: YESNO
// FIXME
539 title
: ts('Include profile edit links in search results?'),
540 help
: ts('Check this box if you want to include a link in the listings to Edit profile fields. Only users with permission to edit the contact will see this link.'),
545 title
: ts('Enable mapping for this profile?'),
546 help
: ts('If enabled, a Map link is included on the profile listings rows and detail screens for any contacts whose records include sufficient location data for your mapping provider.'),
550 'is_proximity_search': {
551 title
: ts('Proximity Search'),
554 options
: YESNO
// FIXME
562 title
: ts('Include Drupal user account information links in search results?'), // FIXME
568 title
: ts('What to do upon duplicate match'),
571 options
: YESNO
// FIXME
573 'limit_listings_group_id': {
574 title
: ts('Limit listings to a specific Group?'),
575 help
: ts('Select a group if you are using this profile for search and listings, AND you want to limit the listings to members of a specific group.'),
579 title
: ts('Notify when profile form is submitted?'),
580 help
: ts('If you want member(s) of your organization to receive a notification email whenever this Profile form is used to enter or update contact information, enter one or more email addresses here. Multiple email addresses should be separated by a comma (e.g. jane@example.org, paula@example.org). The first email address listed will be used as the FROM address in the notifications.'),
584 title
: ts('Redirect URL'),
585 help
: ts("If you are using this profile as a contact signup or edit form, and want to redirect the user to a static URL after they've submitted the form, you can also use contact tokens in URL - enter the complete URL here. If this field is left blank, the built-in Profile form will be redisplayed with a generic status message - 'Your contact information has been saved.'"),
590 help
: ts('Weight controls the order in which profiles are presented when more than one profile is included in User Registration or My Account screens. Enter a positive or negative integer - lower numbers are displayed ahead of higher numbers.'),
592 // FIXME positive int
595 initialize: function() {
596 var ufGroupModel
= this;
598 if (!this.getRel('ufEntityCollection')) {
599 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
603 this.setRel('ufEntityCollection', ufEntityCollection
);
606 if (!this.getRel('ufFieldCollection')) {
607 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
608 uf_group_id
: this.id
,
611 this.setRel('ufFieldCollection', ufFieldCollection
);
614 if (!this.getRel('paletteFieldCollection')) {
615 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
618 paletteFieldCollection
.sync = function(method
, model
, options
) {
619 if (!options
) options
= {};
620 // console.log(method, model, options);
623 var success
= options
.success
;
624 options
.success = function(resp
, status
, xhr
) {
625 if (success
) success(resp
, status
, xhr
);
626 model
.trigger('sync', model
, resp
, options
);
628 success(ufGroupModel
.buildPaletteFields());
634 throw 'Unsupported method: ' + method
;
637 throw 'Unsupported method: ' + method
;
640 this.setRel('paletteFieldCollection', paletteFieldCollection
);
643 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
644 this.resetEntities();
646 this.on('change', watchChanges
);
649 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
650 * is suitable for a new, identical UFGroup.
652 * @return {CRM.UF.UFGroupModel}
654 deepCopy: function() {
655 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
656 copy
.getRel('ufEntityCollection').reset(
657 this.getRel('ufEntityCollection').toJSON()
658 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
660 copy
.getRel('ufFieldCollection').reset(
661 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
662 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
665 copy
.set('title', ts('%1 (Copy)', {
670 getModelClass: function(entity_name
) {
671 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
672 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
673 return ufEntity
.getModelClass();
675 getFieldSchema: function(entity_name
, field_name
) {
676 var modelClass
= this.getModelClass(entity_name
);
677 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
680 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
687 * Check that the group_type contains *only* the types listed in validTypes
689 * @param string validTypesExpr
690 * @param bool allowAllSubtypes
694 checkGroupType: function(validTypesExpr
, allowAllSubtypes
) {
695 var allMatched
= true;
696 allowAllSubtypes
= allowAllSubtypes
|| false;
697 if (_
.isEmpty(this.get('group_type'))) {
701 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
702 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
704 // Every actual.coreType is a valid.coreType
705 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
706 if (! validTypes
.coreTypes
[actualCoreType
]) {
711 //CRM-15427 allow all subtypes
712 if (!$.isEmptyObject(validTypes
.subTypes
) && !allowAllSubtypes
) {
713 // Every actual.subType is a valid.subType
714 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
715 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
719 // actualSubTypeIds is a list of all subtypes which can be used by group,
720 // so it's sufficient to match any one of them
721 var subTypeMatched
= false;
722 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
723 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
724 subTypeMatched
= true;
727 allMatched
= allMatched
&& subTypeMatched
;
732 calculateContactEntityType: function() {
733 var ufGroupModel
= this;
735 // set proper entity model based on selected profile
736 var contactTypes
= ['Individual', 'Household', 'Organization'];
737 var profileType
= ufGroupModel
.get('group_type') || '';
739 // check if selected profile have subtype defined eg: ["Individual,Contact,Case", "caseType:7"]
740 if (_
.isArray(profileType
) && profileType
[0]) {
741 profileType
= profileType
[0];
743 profileType
= profileType
.split(',');
746 _
.each(profileType
, function (ptype
) {
747 if ($.inArray(ptype
, contactTypes
) > -1) {
748 ufEntityModel
= ptype
+ 'Model';
753 return ufEntityModel
;
755 setUFGroupModel: function(entityType
, allEntityModels
) {
756 var ufGroupModel
= this;
758 var newUfEntityModels
= [];
759 _
.each(allEntityModels
, function (values
) {
760 if (entityType
&& values
.entity_name
== 'contact_1') {
761 values
.entity_type
= entityType
;
763 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
766 ufGroupModel
.getRel('ufEntityCollection').reset(newUfEntityModels
);
768 resetEntities: function() {
769 var ufGroupModel
= this;
770 var deleteFieldList
= [];
771 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
772 if (!ufFieldModel
.getFieldSchema()) {
773 CRM
.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
774 1: ufFieldModel
.get('label')
775 }), '', 'alert', {expires
: false});
776 deleteFieldList
.push(ufFieldModel
);
780 _
.each(deleteFieldList
, function(ufFieldModel
) {
781 ufFieldModel
.destroyLocal();
784 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
786 // reset to redraw the cancel after entity type is updated.
787 ufGroupModel
.getRel('ufFieldCollection').reset(ufGroupModel
.getRel('ufFieldCollection').toJSON());
791 * @return {Array} of PaletteFieldModel
793 buildPaletteFields: function() {
794 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
796 // Note: The system as a whole is ill-defined in cases where we have an existing
797 // UFField that references a model field that disappears.
799 var ufGroupModel
= this;
801 var oldPaletteFieldModelsBySig
= {};
802 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
803 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
806 var newPaletteFieldModels
= [];
807 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
808 var modelClass
= ufEntityModel
.getModelClass();
809 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
810 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
812 model
= new CRM
.Designer
.PaletteFieldModel({
813 modelClass
: modelClass
,
814 entityName
: ufEntityModel
.get('entity_name'),
818 newPaletteFieldModels
.push(model
);
822 return newPaletteFieldModels
;
827 * Represents a list of customizable form
829 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
830 model
: CRM
.UF
.UFGroupModel