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
) {
105 return 'contribution_1';
107 return 'membership_1';
109 return 'participant_1';
113 if (!$.isEmptyObject(CRM
.contactSubTypes
) && ($.inArray(field_type
,CRM
.contactSubTypes
) > -1)) {
117 throw "Cannot guess entity name for field_type=" + field_type
;
123 * Represents a field in a customizable form.
125 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
127 * Backbone.Form description of the field to which this refers
133 * @var bool, non-persistent indication of whether this field is unique or duplicate
134 * within its UFFieldCollection
154 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
157 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
161 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
165 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
170 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
174 'is_multi_summary': {
175 title
: ts("Include in multi-record listing?"),
180 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
189 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
194 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
199 title
: ts('Field Label'),
202 'location_type_id': {
203 title
: ts('Location Type'),
205 options
: LOCATION_TYPES
208 title
: ts('Website Type'),
210 options
: WEBSITE_TYPES
213 title
: ts('Phone Type'),
218 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
226 initialize: function() {
227 if (this.get('field_name').indexOf('formatting') === 0) {
228 this.schema
.help_pre
.title
= ts('Markup');
230 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
231 this.on("rel:ufGroupModel", this.applyDefaults
, this);
232 this.on('change', watchChanges
);
234 applyDefaults: function() {
235 var fieldSchema
= this.getFieldSchema();
236 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
237 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
239 if (fieldSchema
&& fieldSchema
.civiIsWebsite
&& !this.get('website_type_id')) {
240 this.set('website_type_id', DEFAULT_WEBSITE_TYPE_ID
);
242 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
243 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
246 isInSelectorAllowed: function() {
247 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
249 return visibility
.isInSelectorAllowed
;
255 getFieldSchema: function() {
256 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
259 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
260 * have a unique signature.
264 getSignature: function() {
265 return this.get("entity_name") +
266 '::' + this.get("field_name") +
267 '::' + (this.get("location_type_id") ? this.get("location_type_id") : this.get("website_type_id") ? this.get("website_type_id") : '') +
268 '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
272 * This is like destroy(), but it only destroys the item on the client-side;
273 * it does not trigger REST or Backbone.sync() operations.
277 destroyLocal: function() {
278 this.trigger('destroy', this, this.collection
, {});
284 * Represents a list of fields in a customizable form
289 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
290 model
: CRM
.UF
.UFFieldModel
,
291 uf_group_id
: null, // int
292 initialize: function(models
, options
) {
293 options
= options
|| {};
294 this.uf_group_id
= options
.uf_group_id
;
295 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
296 this.on('add', this.watchDuplicates
, this);
297 this.on('remove', this.unwatchDuplicates
, this);
298 this.on('change', watchChanges
);
299 this.on('add', watchChanges
);
300 this.on('remove', watchChanges
);
302 getFieldsByName: function(entityName
, fieldName
) {
303 return this.filter(function(ufFieldModel
) {
304 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
307 toSortedJSON: function() {
308 var fields
= this.map(function(ufFieldModel
){
309 return ufFieldModel
.toStrictJSON();
311 return _
.sortBy(fields
, function(ufFieldJSON
){
312 return parseInt(ufFieldJSON
.weight
);
315 isAddable: function(ufFieldModel
) {
316 var entity_name
= ufFieldModel
.get('entity_name'),
317 field_name
= ufFieldModel
.get('field_name'),
318 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
319 if (field_name
.indexOf('formatting') === 0) {
325 var fields
= this.getFieldsByName(entity_name
, field_name
);
327 if (fieldSchema
.civiIsLocation
) {
328 limit
*= LOCATION_TYPES
.length
;
330 if (fieldSchema
.civiIsWebsite
) {
331 limit
*= WEBSITE_TYPES
.length
;
333 if (fieldSchema
.civiIsPhone
) {
334 limit
*= PHONE_TYPES
.length
;
336 return fields
.length
< limit
;
338 watchDuplicates: function(model
, collection
, options
) {
339 model
.on('change:location_type_id', this.markDuplicates
, this);
340 model
.on('change:website_type_id', this.markDuplicates
, this);
341 model
.on('change:phone_type_id', this.markDuplicates
, this);
342 this.markDuplicates();
344 unwatchDuplicates: function(model
, collection
, options
) {
345 model
.off('change:location_type_id', this.markDuplicates
, this);
346 model
.off('change:website_type_id', this.markDuplicates
, this);
347 model
.off('change:phone_type_id', this.markDuplicates
, this);
348 this.markDuplicates();
350 hasDuplicates: function() {
351 var firstDupe
= this.find(function(ufFieldModel
){
352 return ufFieldModel
.get('is_duplicate');
354 return firstDupe
? true : false;
359 markDuplicates: function() {
360 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
361 return ufFieldModel
.getSignature();
363 this.each(function(ufFieldModel
){
364 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
365 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
366 ufFieldModel
.set('is_duplicate', is_duplicate
);
373 * Represents an entity in a customizable form
375 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
382 title
: ts('Entity Name'),
383 help
: ts('Symbolic name which referenced in the fields'),
387 title
: ts('Entity Type'),
389 options
: ['IndividualModel', 'ActivityModel']
392 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
393 title
: ts('Sub Type'),
400 initialize: function() {
403 * Get a list of all fields that can be used with this entity.
405 * @return {Object} keys are field names; values are fieldSchemas
407 getFieldSchemas: function() {
408 var ufEntityModel
= this;
409 var modelClass
= this.getModelClass();
411 if (this.get('entity_sub_type') == '*') {
412 return _
.clone(modelClass
.prototype.schema
);
416 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
417 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
418 if (ufEntityModel
.isSectionEnabled(section
)) {
419 result
[fieldName
] = fieldSchema
;
424 isSectionEnabled: function(section
) {
426 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')) || this.get('entity_sub_type') == '*');
428 getSections: function() {
429 var ufEntityModel
= this;
431 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
432 if (ufEntityModel
.isSectionEnabled(section
)) {
433 result
[sectionKey
] = section
;
438 getModelClass: function() {
439 return CRM
.Schema
[this.get('entity_type')];
444 * Represents a list of entities in a customizable form
447 * - ufGroupModel: UFGroupModel
449 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
450 model
: CRM
.UF
.UFEntityModel
,
452 initialize: function(models
, options
) {
453 options
= options
|| {};
454 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
459 * @return {UFEntityModel} if found; otherwise, null
461 getByName: function(name
) {
462 // TODO consider indexing
463 return this.find(function(ufEntityModel
){
464 return ufEntityModel
.get('entity_name') == name
;
470 * Represents a customizable form
472 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
474 title
: ts('Unnamed Profile'),
487 title
: ts('Profile Name'),
490 validators
: ['required']
493 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
498 title
: ts('Include reCAPTCHA?'),
504 title
: ts('Add new contacts to a Group?'),
505 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.'),
509 title
: ts('Cancel Redirect URL'),
510 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.'),
522 title
: ts('Post-form Help'),
523 help
: ts('Explanatory text displayed at the end of the form.') +
524 ts('Note that this help text is displayed on profile create/edit screens only.'),
528 title
: ts('Pre-form Help'),
529 help
: ts('Explanatory text displayed at the beginning of the form.') +
530 ts('Note that this help text is displayed on profile create/edit screens only.'),
534 title
: ts('Is this CiviCRM Profile active?'),
539 title
: ts('Drupal user account registration option?'),// FIXME
542 options
: YESNO
// FIXME
545 title
: ts('Include profile edit links in search results?'),
546 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.'),
551 title
: ts('Enable mapping for this profile?'),
552 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.'),
556 'is_proximity_search': {
557 title
: ts('Proximity Search'),
560 options
: YESNO
// FIXME
568 title
: ts('Include Drupal user account information links in search results?'), // FIXME
574 title
: ts('What to do upon duplicate match'),
577 options
: YESNO
// FIXME
579 'limit_listings_group_id': {
580 title
: ts('Limit listings to a specific Group?'),
581 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.'),
585 title
: ts('Notify when profile form is submitted?'),
586 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.'),
590 title
: ts('Redirect URL'),
591 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.'"),
596 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.'),
598 // FIXME positive int
601 initialize: function() {
602 var ufGroupModel
= this;
604 if (!this.getRel('ufEntityCollection')) {
605 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
609 this.setRel('ufEntityCollection', ufEntityCollection
);
612 if (!this.getRel('ufFieldCollection')) {
613 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
614 uf_group_id
: this.id
,
617 this.setRel('ufFieldCollection', ufFieldCollection
);
620 if (!this.getRel('paletteFieldCollection')) {
621 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
624 paletteFieldCollection
.sync = function(method
, model
, options
) {
625 if (!options
) options
= {};
626 // console.log(method, model, options);
629 var success
= options
.success
;
630 options
.success = function(resp
, status
, xhr
) {
631 if (success
) success(resp
, status
, xhr
);
632 model
.trigger('sync', model
, resp
, options
);
634 success(ufGroupModel
.buildPaletteFields());
640 throw 'Unsupported method: ' + method
;
643 throw 'Unsupported method: ' + method
;
646 this.setRel('paletteFieldCollection', paletteFieldCollection
);
649 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
650 this.resetEntities();
652 this.on('change', watchChanges
);
655 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
656 * is suitable for a new, identical UFGroup.
658 * @return {CRM.UF.UFGroupModel}
660 deepCopy: function() {
661 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
662 copy
.getRel('ufEntityCollection').reset(
663 this.getRel('ufEntityCollection').toJSON()
664 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
666 copy
.getRel('ufFieldCollection').reset(
667 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
668 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
671 copy
.set('title', ts('%1 (Copy)', {
676 getModelClass: function(entity_name
) {
677 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
678 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
679 return ufEntity
.getModelClass();
681 getFieldSchema: function(entity_name
, field_name
) {
682 if (field_name
.indexOf('formatting') === 0) {
683 field_name
= 'formatting';
685 var modelClass
= this.getModelClass(entity_name
);
686 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
688 CRM
.console('warn', 'Failed to locate field: ' + entity_name
+ "." + field_name
);
694 * Check that the group_type contains *only* the types listed in validTypes
696 * @param string validTypesExpr
697 * @param bool allowAllSubtypes
701 checkGroupType: function(validTypesExpr
, allowAllSubtypes
) {
702 var allMatched
= true;
703 allowAllSubtypes
= allowAllSubtypes
|| false;
704 if (_
.isEmpty(this.get('group_type'))) {
708 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
709 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
711 // Every actual.coreType is a valid.coreType
712 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
713 if (! validTypes
.coreTypes
[actualCoreType
]) {
718 //CRM-15427 allow all subtypes
719 if (!$.isEmptyObject(validTypes
.subTypes
) && !allowAllSubtypes
) {
720 // Every actual.subType is a valid.subType
721 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
722 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
726 // actualSubTypeIds is a list of all subtypes which can be used by group,
727 // so it's sufficient to match any one of them
728 var subTypeMatched
= false;
729 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
730 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
731 subTypeMatched
= true;
734 allMatched
= allMatched
&& subTypeMatched
;
739 calculateContactEntityType: function() {
740 var ufGroupModel
= this;
742 // set proper entity model based on selected profile
743 var contactTypes
= ['Individual', 'Household', 'Organization'];
744 var profileType
= ufGroupModel
.get('group_type') || '';
746 // check if selected profile have subtype defined eg: ["Individual,Contact,Case", "caseType:7"]
747 if (_
.isArray(profileType
) && profileType
[0]) {
748 profileType
= profileType
[0];
750 profileType
= profileType
.split(',');
753 _
.each(profileType
, function (ptype
) {
754 if ($.inArray(ptype
, contactTypes
) > -1) {
755 ufEntityModel
= ptype
+ 'Model';
760 return ufEntityModel
;
762 setUFGroupModel: function(entityType
, allEntityModels
) {
763 var ufGroupModel
= this;
765 var newUfEntityModels
= [];
766 _
.each(allEntityModels
, function (values
) {
767 if (entityType
&& values
.entity_name
== 'contact_1') {
768 values
.entity_type
= entityType
;
770 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
773 ufGroupModel
.getRel('ufEntityCollection').reset(newUfEntityModels
);
775 resetEntities: function() {
776 var ufGroupModel
= this;
777 var deleteFieldList
= [];
778 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
779 if (!ufFieldModel
.getFieldSchema()) {
780 CRM
.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
781 1: ufFieldModel
.get('label')
782 }), '', 'alert', {expires
: false});
783 deleteFieldList
.push(ufFieldModel
);
787 _
.each(deleteFieldList
, function(ufFieldModel
) {
788 ufFieldModel
.destroyLocal();
791 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
793 // reset to redraw the cancel after entity type is updated.
794 ufGroupModel
.getRel('ufFieldCollection').reset(ufGroupModel
.getRel('ufFieldCollection').toJSON());
798 * @return {Array} of PaletteFieldModel
800 buildPaletteFields: function() {
801 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
803 // Note: The system as a whole is ill-defined in cases where we have an existing
804 // UFField that references a model field that disappears.
806 var ufGroupModel
= this;
808 var oldPaletteFieldModelsBySig
= {};
809 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
810 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
813 var newPaletteFieldModels
= [];
814 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
815 var modelClass
= ufEntityModel
.getModelClass();
816 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
817 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
819 model
= new CRM
.Designer
.PaletteFieldModel({
820 modelClass
: modelClass
,
821 entityName
: ufEntityModel
.get('entity_name'),
825 newPaletteFieldModels
.push(model
);
829 return newPaletteFieldModels
;
834 * Represents a list of customizable form
836 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
837 model
: CRM
.UF
.UFGroupModel