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
};
24 var DEFAULT_PHONE_TYPE_ID
= PHONE_TYPES
[0].val
;
27 * Add a help link to a form label
29 function addHelp(title
, options
) {
30 return title
+ ' <a href="#" onclick=\'CRM.help("' + title
+ '", ' + JSON
.stringify(options
) + '); return false;\' title="' + ts('%1 Help', {1: title
}) + '" class="helpicon"></a>';
33 function watchChanges() {
34 CRM
.designerApp
.vent
.trigger('ufUnsaved', true);
38 * Parse a "group_type" expression
40 * @param string groupTypeExpr example: "Individual,Activity\0ActivityType:2:28"
41 * Note: I've seen problems where HTML "�" != JS '\0', so we support ';;' as an equivalent delimiter
42 * @return Object example: {coreTypes: {"Individual":true,"Activity":true}, subTypes: {"ActivityType":{2: true, 28:true}]}}
44 CRM
.UF
.parseTypeList = function(groupTypeExpr
) {
45 var typeList
= {coreTypes
: {}, subTypes
:{}};
46 // The API may have automatically converted a string with '\0' to an array
47 var parts
= _
.isArray(groupTypeExpr
) ? groupTypeExpr
: groupTypeExpr
.replace(';;','\0').split('\0');
48 var coreTypesExpr
= parts
[0];
49 var subTypesExpr
= parts
[1];
51 if (coreTypesExpr
&& coreTypesExpr
!= '') {
52 _
.each(coreTypesExpr
.split(','), function(coreType
){
53 typeList
.coreTypes
[coreType
] = true;
57 //CRM-15427 Allow Multiple subtype filtering
58 if (subTypesExpr
&& subTypesExpr
!= '') {
59 if (subTypesExpr
.indexOf(';;') !== -1) {
60 var subTypeparts
= subTypesExpr
.replace(/;;/g,'\0').split('\0');
61 _
.each(subTypeparts
, function(subTypepart
) {
62 var subTypes
= subTypepart
.split(':');
63 var subTypeKey
= subTypes
.shift();
64 typeList
.subTypes
[subTypeKey
] = {};
65 _
.each(subTypes
, function(subTypeId
) {
66 typeList
.subTypes
[subTypeKey
][subTypeId
] = true;
71 var subTypes
= subTypesExpr
.split(':');
72 var subTypeKey
= subTypes
.shift();
73 typeList
.subTypes
[subTypeKey
] = {};
74 _
.each(subTypes
, function(subTypeId
) {
75 typeList
.subTypes
[subTypeKey
][subTypeId
] = true;
83 * This function is a hack for generating simulated values of "entity_name"
84 * in the form-field model.
86 * @param {string} field_type
89 CRM
.UF
.guessEntityName = function(field_type
) {
99 return 'contribution_1';
101 return 'membership_1';
103 return 'participant_1';
107 throw "Cannot guess entity name for field_type=" + field_type
;
112 * Represents a field in a customizable form.
114 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
116 * Backbone.Form descripton of the field to which this refers
122 * @var bool, non-persistent indication of whether this field is unique or duplicate
123 * within its UFFieldCollection
143 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
146 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
150 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
154 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
159 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
163 'is_multi_summary': {
164 title
: ts("Include in multi-record listing?"),
169 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
178 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
183 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
188 title
: ts('Field Label'),
191 'location_type_id': {
192 title
: ts('Location Type'),
194 options
: LOCATION_TYPES
197 title
: ts('Phone Type'),
202 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
210 initialize: function() {
211 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
212 this.on("rel:ufGroupModel", this.applyDefaults
, this);
213 this.on('change', watchChanges
);
215 applyDefaults: function() {
216 var fieldSchema
= this.getFieldSchema();
217 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
218 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
220 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
221 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
224 isInSelectorAllowed: function() {
225 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
227 return visibility
.isInSelectorAllowed
;
233 getFieldSchema: function() {
234 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
237 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
238 * have a unique signature.
242 getSignature: function() {
243 return this.get("entity_name")
244 + '::' + this.get("field_name")
245 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
246 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
250 * This is like destroy(), but it only destroys the item on the client-side;
251 * it does not trigger REST or Backbone.sync() operations.
255 destroyLocal: function() {
256 this.trigger('destroy', this, this.collection
, {});
262 * Represents a list of fields in a customizable form
267 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
268 model
: CRM
.UF
.UFFieldModel
,
269 uf_group_id
: null, // int
270 initialize: function(models
, options
) {
271 options
= options
|| {};
272 this.uf_group_id
= options
.uf_group_id
;
273 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
274 this.on('add', this.watchDuplicates
, this);
275 this.on('remove', this.unwatchDuplicates
, this);
276 this.on('change', watchChanges
);
277 this.on('add', watchChanges
);
278 this.on('remove', watchChanges
);
280 getFieldsByName: function(entityName
, fieldName
) {
281 return this.filter(function(ufFieldModel
) {
282 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
285 toSortedJSON: function() {
286 var fields
= this.map(function(ufFieldModel
){
287 return ufFieldModel
.toStrictJSON();
289 return _
.sortBy(fields
, function(ufFieldJSON
){
290 return parseInt(ufFieldJSON
.weight
);
293 isAddable: function(ufFieldModel
) {
294 var entity_name
= ufFieldModel
.get('entity_name'),
295 field_name
= ufFieldModel
.get('field_name'),
296 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
301 var fields
= this.getFieldsByName(entity_name
, field_name
);
303 if (fieldSchema
.civiIsLocation
) {
304 limit
*= LOCATION_TYPES
.length
;
306 if (fieldSchema
.civiIsPhone
) {
307 limit
*= PHONE_TYPES
.length
;
309 return fields
.length
< limit
;
311 watchDuplicates: function(model
, collection
, options
) {
312 model
.on('change:location_type_id', this.markDuplicates
, this);
313 model
.on('change:phone_type_id', this.markDuplicates
, this);
314 this.markDuplicates();
316 unwatchDuplicates: function(model
, collection
, options
) {
317 model
.off('change:location_type_id', this.markDuplicates
, this);
318 model
.off('change:phone_type_id', this.markDuplicates
, this);
319 this.markDuplicates();
321 hasDuplicates: function() {
322 var firstDupe
= this.find(function(ufFieldModel
){
323 return ufFieldModel
.get('is_duplicate');
325 return firstDupe
? true : false;
330 markDuplicates: function() {
331 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
332 return ufFieldModel
.getSignature();
334 this.each(function(ufFieldModel
){
335 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
336 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
337 ufFieldModel
.set('is_duplicate', is_duplicate
);
344 * Represents an entity in a customizable form
346 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
353 title
: ts('Entity Name'),
354 help
: ts('Symbolic name which referenced in the fields'),
358 title
: ts('Entity Type'),
360 options
: ['IndividualModel', 'ActivityModel']
363 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
364 title
: ts('Sub Type'),
371 initialize: function() {
374 * Get a list of all fields that can be used with this entity.
376 * @return {Object} keys are field names; values are fieldSchemas
378 getFieldSchemas: function() {
379 var ufEntityModel
= this;
380 var modelClass
= this.getModelClass();
382 if (this.get('entity_sub_type') == '*') {
383 return _
.clone(modelClass
.prototype.schema
);
387 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
388 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
389 if (ufEntityModel
.isSectionEnabled(section
)) {
390 result
[fieldName
] = fieldSchema
;
395 isSectionEnabled: function(section
) {
397 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')) || this.get('entity_sub_type') == '*');
399 getSections: function() {
400 var ufEntityModel
= this;
402 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
403 if (ufEntityModel
.isSectionEnabled(section
)) {
404 result
[sectionKey
] = section
;
409 getModelClass: function() {
410 return CRM
.Schema
[this.get('entity_type')];
415 * Represents a list of entities in a customizable form
418 * - ufGroupModel: UFGroupModel
420 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
421 model
: CRM
.UF
.UFEntityModel
,
423 initialize: function(models
, options
) {
424 options
= options
|| {};
425 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
430 * @return {UFEntityModel} if found; otherwise, null
432 getByName: function(name
) {
433 // TODO consider indexing
434 return this.find(function(ufEntityModel
){
435 return ufEntityModel
.get('entity_name') == name
;
441 * Represents a customizable form
443 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
445 title
: ts('Unnamed Profile'),
458 title
: ts('Profile Name'),
461 validators
: ['required']
464 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
469 title
: ts('Include reCAPTCHA?'),
475 title
: ts('Add new contacts to a Group?'),
476 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.'),
480 title
: ts('Cancel Redirect URL'),
481 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.'),
493 title
: ts('Post-form Help'),
494 help
: ts('Explanatory text displayed at the end of the form.')
495 + ts('Note that this help text is displayed on profile create/edit screens only.'),
499 title
: ts('Pre-form Help '),
500 help
: ts('Explanatory text displayed at the beginning of the form.')
501 + ts('Note that this help text is displayed on profile create/edit screens only.'),
505 title
: ts('Is this CiviCRM Profile active?'),
510 title
: ts('Drupal user account registration option?'),// FIXME
513 options
: YESNO
// FIXME
516 title
: ts('Include profile edit links in search results?'),
517 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.'),
522 title
: ts('Enable mapping for this profile?'),
523 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.'),
527 'is_proximity_search': {
528 title
: ts('Proximity search'),
531 options
: YESNO
// FIXME
539 title
: ts('Include Drupal user account information links in search results?'), // FIXME
545 title
: ts('What to do upon duplicate match'),
548 options
: YESNO
// FIXME
550 'limit_listings_group_id': {
551 title
: ts('Limit listings to a specific Group?'),
552 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.'),
556 title
: ts('Notify when profile form is submitted?'),
557 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.'),
561 title
: ts('Redirect URL'),
562 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.'"),
567 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.'),
569 // FIXME positive int
572 initialize: function() {
573 var ufGroupModel
= this;
575 if (!this.getRel('ufEntityCollection')) {
576 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
580 this.setRel('ufEntityCollection', ufEntityCollection
);
583 if (!this.getRel('ufFieldCollection')) {
584 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
585 uf_group_id
: this.id
,
588 this.setRel('ufFieldCollection', ufFieldCollection
);
591 if (!this.getRel('paletteFieldCollection')) {
592 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
595 paletteFieldCollection
.sync = function(method
, model
, options
) {
596 options
|| (options
= {});
597 // console.log(method, model, options);
600 var success
= options
.success
;
601 options
.success = function(resp
, status
, xhr
) {
602 if (success
) success(resp
, status
, xhr
);
603 model
.trigger('sync', model
, resp
, options
);
605 success(ufGroupModel
.buildPaletteFields());
612 throw 'Unsupported method: ' + method
;
615 this.setRel('paletteFieldCollection', paletteFieldCollection
);
618 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
619 this.resetEntities();
621 this.on('change', watchChanges
);
624 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
625 * is suitable for a new, identical UFGroup.
627 * @return {CRM.UF.UFGroupModel}
629 deepCopy: function() {
630 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
631 copy
.getRel('ufEntityCollection').reset(
632 this.getRel('ufEntityCollection').toJSON()
633 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
635 copy
.getRel('ufFieldCollection').reset(
636 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
637 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
640 copy
.set('title', ts('%1 (Copy)', {
645 getModelClass: function(entity_name
) {
646 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
647 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
648 return ufEntity
.getModelClass();
650 getFieldSchema: function(entity_name
, field_name
) {
651 var modelClass
= this.getModelClass(entity_name
);
652 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
655 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
662 * Check that the group_type contains *only* the types listed in validTypes
664 * @param string validTypesExpr
665 * @param bool allowAllSubtypes
669 checkGroupType: function(validTypesExpr
, allowAllSubtypes
) {
670 var allMatched
= true;
671 allowAllSubtypes
= allowAllSubtypes
|| false;
672 if (! this.get('group_type') || this.get('group_type') == '') {
676 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
677 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
679 // Every actual.coreType is a valid.coreType
680 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
681 if (! validTypes
.coreTypes
[actualCoreType
]) {
686 //CRM-15427 allow all subtypes
687 if (!$.isEmptyObject(validTypes
.subTypes
) && !allowAllSubtypes
) {
688 // Every actual.subType is a valid.subType
689 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
690 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
694 // actualSubTypeIds is a list of all subtypes which can be used by group,
695 // so it's sufficient to match any one of them
696 var subTypeMatched
= false;
697 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
698 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
699 subTypeMatched
= true;
702 allMatched
= allMatched
&& subTypeMatched
;
707 calculateContactEntityType: function() {
708 var ufGroupModel
= this;
710 // set proper entity model based on selected profile
711 var contactTypes
= ['Individual', 'Household', 'Organization'];
712 var profileType
= ufGroupModel
.get('group_type') || '';
714 // check if selected profile have subtype defined eg: ["Individual,Contact,Case", "caseType:7"]
715 if (_
.isArray(profileType
) && profileType
[0]) {
716 profileType
= profileType
[0];
718 profileType
= profileType
.split(',');
721 _
.each(profileType
, function (ptype
) {
722 if ($.inArray(ptype
, contactTypes
) > -1) {
723 ufEntityModel
= ptype
+ 'Model';
728 return ufEntityModel
;
730 setUFGroupModel: function(entityType
, allEntityModels
) {
731 var ufGroupModel
= this;
733 var newUfEntityModels
= [];
734 _
.each(allEntityModels
, function (values
) {
735 if (entityType
&& values
.entity_name
== 'contact_1') {
736 values
.entity_type
= entityType
;
738 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
741 ufGroupModel
.getRel('ufEntityCollection').reset(newUfEntityModels
);
743 resetEntities: function() {
744 var ufGroupModel
= this;
745 var deleteFieldList
= [];
746 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
747 if (!ufFieldModel
.getFieldSchema()) {
748 CRM
.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
749 1: ufFieldModel
.get('label')
750 }), '', 'alert', {expires
: false});
751 deleteFieldList
.push(ufFieldModel
);
755 _
.each(deleteFieldList
, function(ufFieldModel
) {
756 ufFieldModel
.destroyLocal();
759 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
761 // reset to redraw the cancel after entity type is updated.
762 ufGroupModel
.getRel('ufFieldCollection').reset(ufGroupModel
.getRel('ufFieldCollection').toJSON());
766 * @return {Array} of PaletteFieldModel
768 buildPaletteFields: function() {
769 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
771 // Note: The system as a whole is ill-defined in cases where we have an existing
772 // UFField that references a model field that disappears.
774 var ufGroupModel
= this;
776 var oldPaletteFieldModelsBySig
= {};
777 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
778 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
781 var newPaletteFieldModels
= [];
782 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
783 var modelClass
= ufEntityModel
.getModelClass();
784 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
785 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
787 model
= new CRM
.Designer
.PaletteFieldModel({
788 modelClass
: modelClass
,
789 entityName
: ufEntityModel
.get('entity_name'),
793 newPaletteFieldModels
.push(model
);
797 return newPaletteFieldModels
;
802 * Represents a list of customizable form
804 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
805 model
: CRM
.UF
.UFGroupModel