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 if (!$.isEmptyObject(CRM
.contactSubTypes
) && ($.inArray(field_type
,CRM
.contactSubTypes
) > -1)) {
111 throw "Cannot guess entity name for field_type=" + field_type
;
117 * Represents a field in a customizable form.
119 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
121 * Backbone.Form descripton of the field to which this refers
127 * @var bool, non-persistent indication of whether this field is unique or duplicate
128 * within its UFFieldCollection
148 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
151 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
155 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
159 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
164 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
168 'is_multi_summary': {
169 title
: ts("Include in multi-record listing?"),
174 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
183 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
188 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
193 title
: ts('Field Label'),
196 'location_type_id': {
197 title
: ts('Location Type'),
199 options
: LOCATION_TYPES
202 title
: ts('Phone Type'),
207 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
215 initialize: function() {
216 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
217 this.on("rel:ufGroupModel", this.applyDefaults
, this);
218 this.on('change', watchChanges
);
220 applyDefaults: function() {
221 var fieldSchema
= this.getFieldSchema();
222 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
223 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
225 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
226 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
229 isInSelectorAllowed: function() {
230 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
232 return visibility
.isInSelectorAllowed
;
238 getFieldSchema: function() {
239 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
242 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
243 * have a unique signature.
247 getSignature: function() {
248 return this.get("entity_name")
249 + '::' + this.get("field_name")
250 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
251 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
255 * This is like destroy(), but it only destroys the item on the client-side;
256 * it does not trigger REST or Backbone.sync() operations.
260 destroyLocal: function() {
261 this.trigger('destroy', this, this.collection
, {});
267 * Represents a list of fields in a customizable form
272 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
273 model
: CRM
.UF
.UFFieldModel
,
274 uf_group_id
: null, // int
275 initialize: function(models
, options
) {
276 options
= options
|| {};
277 this.uf_group_id
= options
.uf_group_id
;
278 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
279 this.on('add', this.watchDuplicates
, this);
280 this.on('remove', this.unwatchDuplicates
, this);
281 this.on('change', watchChanges
);
282 this.on('add', watchChanges
);
283 this.on('remove', watchChanges
);
285 getFieldsByName: function(entityName
, fieldName
) {
286 return this.filter(function(ufFieldModel
) {
287 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
290 toSortedJSON: function() {
291 var fields
= this.map(function(ufFieldModel
){
292 return ufFieldModel
.toStrictJSON();
294 return _
.sortBy(fields
, function(ufFieldJSON
){
295 return parseInt(ufFieldJSON
.weight
);
298 isAddable: function(ufFieldModel
) {
299 var entity_name
= ufFieldModel
.get('entity_name'),
300 field_name
= ufFieldModel
.get('field_name'),
301 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
306 var fields
= this.getFieldsByName(entity_name
, field_name
);
308 if (fieldSchema
.civiIsLocation
) {
309 limit
*= LOCATION_TYPES
.length
;
311 if (fieldSchema
.civiIsPhone
) {
312 limit
*= PHONE_TYPES
.length
;
314 return fields
.length
< limit
;
316 watchDuplicates: function(model
, collection
, options
) {
317 model
.on('change:location_type_id', this.markDuplicates
, this);
318 model
.on('change:phone_type_id', this.markDuplicates
, this);
319 this.markDuplicates();
321 unwatchDuplicates: function(model
, collection
, options
) {
322 model
.off('change:location_type_id', this.markDuplicates
, this);
323 model
.off('change:phone_type_id', this.markDuplicates
, this);
324 this.markDuplicates();
326 hasDuplicates: function() {
327 var firstDupe
= this.find(function(ufFieldModel
){
328 return ufFieldModel
.get('is_duplicate');
330 return firstDupe
? true : false;
335 markDuplicates: function() {
336 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
337 return ufFieldModel
.getSignature();
339 this.each(function(ufFieldModel
){
340 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
341 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
342 ufFieldModel
.set('is_duplicate', is_duplicate
);
349 * Represents an entity in a customizable form
351 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
358 title
: ts('Entity Name'),
359 help
: ts('Symbolic name which referenced in the fields'),
363 title
: ts('Entity Type'),
365 options
: ['IndividualModel', 'ActivityModel']
368 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
369 title
: ts('Sub Type'),
376 initialize: function() {
379 * Get a list of all fields that can be used with this entity.
381 * @return {Object} keys are field names; values are fieldSchemas
383 getFieldSchemas: function() {
384 var ufEntityModel
= this;
385 var modelClass
= this.getModelClass();
387 if (this.get('entity_sub_type') == '*') {
388 return _
.clone(modelClass
.prototype.schema
);
392 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
393 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
394 if (ufEntityModel
.isSectionEnabled(section
)) {
395 result
[fieldName
] = fieldSchema
;
400 isSectionEnabled: function(section
) {
402 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')) || this.get('entity_sub_type') == '*');
404 getSections: function() {
405 var ufEntityModel
= this;
407 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
408 if (ufEntityModel
.isSectionEnabled(section
)) {
409 result
[sectionKey
] = section
;
414 getModelClass: function() {
415 return CRM
.Schema
[this.get('entity_type')];
420 * Represents a list of entities in a customizable form
423 * - ufGroupModel: UFGroupModel
425 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
426 model
: CRM
.UF
.UFEntityModel
,
428 initialize: function(models
, options
) {
429 options
= options
|| {};
430 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
435 * @return {UFEntityModel} if found; otherwise, null
437 getByName: function(name
) {
438 // TODO consider indexing
439 return this.find(function(ufEntityModel
){
440 return ufEntityModel
.get('entity_name') == name
;
446 * Represents a customizable form
448 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
450 title
: ts('Unnamed Profile'),
463 title
: ts('Profile Name'),
466 validators
: ['required']
469 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
474 title
: ts('Include reCAPTCHA?'),
480 title
: ts('Add new contacts to a Group?'),
481 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.'),
485 title
: ts('Cancel Redirect URL'),
486 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.'),
498 title
: ts('Post-form Help'),
499 help
: ts('Explanatory text displayed at the end of the form.')
500 + ts('Note that this help text is displayed on profile create/edit screens only.'),
504 title
: ts('Pre-form Help '),
505 help
: ts('Explanatory text displayed at the beginning of the form.')
506 + ts('Note that this help text is displayed on profile create/edit screens only.'),
510 title
: ts('Is this CiviCRM Profile active?'),
515 title
: ts('Drupal user account registration option?'),// FIXME
518 options
: YESNO
// FIXME
521 title
: ts('Include profile edit links in search results?'),
522 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.'),
527 title
: ts('Enable mapping for this profile?'),
528 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.'),
532 'is_proximity_search': {
533 title
: ts('Proximity search'),
536 options
: YESNO
// FIXME
544 title
: ts('Include Drupal user account information links in search results?'), // FIXME
550 title
: ts('What to do upon duplicate match'),
553 options
: YESNO
// FIXME
555 'limit_listings_group_id': {
556 title
: ts('Limit listings to a specific Group?'),
557 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.'),
561 title
: ts('Notify when profile form is submitted?'),
562 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.'),
566 title
: ts('Redirect URL'),
567 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.'"),
572 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.'),
574 // FIXME positive int
577 initialize: function() {
578 var ufGroupModel
= this;
580 if (!this.getRel('ufEntityCollection')) {
581 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
585 this.setRel('ufEntityCollection', ufEntityCollection
);
588 if (!this.getRel('ufFieldCollection')) {
589 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
590 uf_group_id
: this.id
,
593 this.setRel('ufFieldCollection', ufFieldCollection
);
596 if (!this.getRel('paletteFieldCollection')) {
597 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
600 paletteFieldCollection
.sync = function(method
, model
, options
) {
601 options
|| (options
= {});
602 // console.log(method, model, options);
605 var success
= options
.success
;
606 options
.success = function(resp
, status
, xhr
) {
607 if (success
) success(resp
, status
, xhr
);
608 model
.trigger('sync', model
, resp
, options
);
610 success(ufGroupModel
.buildPaletteFields());
617 throw 'Unsupported method: ' + method
;
620 this.setRel('paletteFieldCollection', paletteFieldCollection
);
623 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
624 this.resetEntities();
626 this.on('change', watchChanges
);
629 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
630 * is suitable for a new, identical UFGroup.
632 * @return {CRM.UF.UFGroupModel}
634 deepCopy: function() {
635 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
636 copy
.getRel('ufEntityCollection').reset(
637 this.getRel('ufEntityCollection').toJSON()
638 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
640 copy
.getRel('ufFieldCollection').reset(
641 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
642 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
645 copy
.set('title', ts('%1 (Copy)', {
650 getModelClass: function(entity_name
) {
651 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
652 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
653 return ufEntity
.getModelClass();
655 getFieldSchema: function(entity_name
, field_name
) {
656 var modelClass
= this.getModelClass(entity_name
);
657 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
660 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
667 * Check that the group_type contains *only* the types listed in validTypes
669 * @param string validTypesExpr
670 * @param bool allowAllSubtypes
674 checkGroupType: function(validTypesExpr
, allowAllSubtypes
) {
675 var allMatched
= true;
676 allowAllSubtypes
= allowAllSubtypes
|| false;
677 if (! this.get('group_type') || this.get('group_type') == '') {
681 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
682 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
684 // Every actual.coreType is a valid.coreType
685 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
686 if (! validTypes
.coreTypes
[actualCoreType
]) {
691 //CRM-15427 allow all subtypes
692 if (!$.isEmptyObject(validTypes
.subTypes
) && !allowAllSubtypes
) {
693 // Every actual.subType is a valid.subType
694 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
695 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
699 // actualSubTypeIds is a list of all subtypes which can be used by group,
700 // so it's sufficient to match any one of them
701 var subTypeMatched
= false;
702 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
703 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
704 subTypeMatched
= true;
707 allMatched
= allMatched
&& subTypeMatched
;
712 calculateContactEntityType: function() {
713 var ufGroupModel
= this;
715 // set proper entity model based on selected profile
716 var contactTypes
= ['Individual', 'Household', 'Organization'];
717 var profileType
= ufGroupModel
.get('group_type') || '';
719 // check if selected profile have subtype defined eg: ["Individual,Contact,Case", "caseType:7"]
720 if (_
.isArray(profileType
) && profileType
[0]) {
721 profileType
= profileType
[0];
723 profileType
= profileType
.split(',');
726 _
.each(profileType
, function (ptype
) {
727 if ($.inArray(ptype
, contactTypes
) > -1) {
728 ufEntityModel
= ptype
+ 'Model';
733 return ufEntityModel
;
735 setUFGroupModel: function(entityType
, allEntityModels
) {
736 var ufGroupModel
= this;
738 var newUfEntityModels
= [];
739 _
.each(allEntityModels
, function (values
) {
740 if (entityType
&& values
.entity_name
== 'contact_1') {
741 values
.entity_type
= entityType
;
743 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
746 ufGroupModel
.getRel('ufEntityCollection').reset(newUfEntityModels
);
748 resetEntities: function() {
749 var ufGroupModel
= this;
750 var deleteFieldList
= [];
751 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
752 if (!ufFieldModel
.getFieldSchema()) {
753 CRM
.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
754 1: ufFieldModel
.get('label')
755 }), '', 'alert', {expires
: false});
756 deleteFieldList
.push(ufFieldModel
);
760 _
.each(deleteFieldList
, function(ufFieldModel
) {
761 ufFieldModel
.destroyLocal();
764 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
766 // reset to redraw the cancel after entity type is updated.
767 ufGroupModel
.getRel('ufFieldCollection').reset(ufGroupModel
.getRel('ufFieldCollection').toJSON());
771 * @return {Array} of PaletteFieldModel
773 buildPaletteFields: function() {
774 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
776 // Note: The system as a whole is ill-defined in cases where we have an existing
777 // UFField that references a model field that disappears.
779 var ufGroupModel
= this;
781 var oldPaletteFieldModelsBySig
= {};
782 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
783 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
786 var newPaletteFieldModels
= [];
787 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
788 var modelClass
= ufEntityModel
.getModelClass();
789 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
790 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
792 model
= new CRM
.Designer
.PaletteFieldModel({
793 modelClass
: modelClass
,
794 entityName
: ufEntityModel
.get('entity_name'),
798 newPaletteFieldModels
.push(model
);
802 return newPaletteFieldModels
;
807 * Represents a list of customizable form
809 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
810 model
: CRM
.UF
.UFGroupModel