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 if (subTypesExpr
&& subTypesExpr
!= '') {
58 var subTypes
= subTypesExpr
.split(':');
59 var subTypeKey
= subTypes
.shift();
60 typeList
.subTypes
[subTypeKey
] = {};
61 _
.each(subTypes
, function(subTypeId
){
62 typeList
.subTypes
[subTypeKey
][subTypeId
] = true;
69 * This function is a hack for generating simulated values of "entity_name"
70 * in the form-field model.
72 * @param {string} field_type
75 CRM
.UF
.guessEntityName = function(field_type
) {
85 return 'contribution_1';
87 return 'membership_1';
89 return 'participant_1';
93 throw "Cannot guess entity name for field_type=" + field_type
;
98 * Represents a field in a customizable form.
100 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
102 * Backbone.Form descripton of the field to which this refers
108 * @var bool, non-persistent indication of whether this field is unique or duplicate
109 * within its UFFieldCollection
129 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
132 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
136 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
140 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
145 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
149 'is_multi_summary': {
150 title
: ts("Include in multi-record listing?"),
155 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
164 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
169 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
174 title
: ts('Field Label'),
177 'location_type_id': {
178 title
: ts('Location Type'),
180 options
: LOCATION_TYPES
183 title
: ts('Phone Type'),
188 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
196 initialize: function() {
197 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
198 this.on("rel:ufGroupModel", this.applyDefaults
, this);
199 this.on('change', watchChanges
);
201 applyDefaults: function() {
202 var fieldSchema
= this.getFieldSchema();
203 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
204 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
206 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
207 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
210 isInSelectorAllowed: function() {
211 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
213 return visibility
.isInSelectorAllowed
;
219 getFieldSchema: function() {
220 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
223 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
224 * have a unique signature.
228 getSignature: function() {
229 return this.get("entity_name")
230 + '::' + this.get("field_name")
231 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
232 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
236 * This is like destroy(), but it only destroys the item on the client-side;
237 * it does not trigger REST or Backbone.sync() operations.
241 destroyLocal: function() {
242 this.trigger('destroy', this, this.collection
, {});
248 * Represents a list of fields in a customizable form
253 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
254 model
: CRM
.UF
.UFFieldModel
,
255 uf_group_id
: null, // int
256 initialize: function(models
, options
) {
257 options
= options
|| {};
258 this.uf_group_id
= options
.uf_group_id
;
259 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
260 this.on('add', this.watchDuplicates
, this);
261 this.on('remove', this.unwatchDuplicates
, this);
262 this.on('change', watchChanges
);
263 this.on('add', watchChanges
);
264 this.on('remove', watchChanges
);
266 getFieldsByName: function(entityName
, fieldName
) {
267 return this.filter(function(ufFieldModel
) {
268 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
271 toSortedJSON: function() {
272 var fields
= this.map(function(ufFieldModel
){
273 return ufFieldModel
.toStrictJSON();
275 return _
.sortBy(fields
, function(ufFieldJSON
){
276 return parseInt(ufFieldJSON
.weight
);
279 isAddable: function(ufFieldModel
) {
280 var entity_name
= ufFieldModel
.get('entity_name'),
281 field_name
= ufFieldModel
.get('field_name'),
282 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
287 var fields
= this.getFieldsByName(entity_name
, field_name
);
289 if (fieldSchema
.civiIsLocation
) {
290 limit
*= LOCATION_TYPES
.length
;
292 if (fieldSchema
.civiIsPhone
) {
293 limit
*= PHONE_TYPES
.length
;
295 return fields
.length
< limit
;
297 watchDuplicates: function(model
, collection
, options
) {
298 model
.on('change:location_type_id', this.markDuplicates
, this);
299 model
.on('change:phone_type_id', this.markDuplicates
, this);
300 this.markDuplicates();
302 unwatchDuplicates: function(model
, collection
, options
) {
303 model
.off('change:location_type_id', this.markDuplicates
, this);
304 model
.off('change:phone_type_id', this.markDuplicates
, this);
305 this.markDuplicates();
307 hasDuplicates: function() {
308 var firstDupe
= this.find(function(ufFieldModel
){
309 return ufFieldModel
.get('is_duplicate');
311 return firstDupe
? true : false;
316 markDuplicates: function() {
317 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
318 return ufFieldModel
.getSignature();
320 this.each(function(ufFieldModel
){
321 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
322 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
323 ufFieldModel
.set('is_duplicate', is_duplicate
);
330 * Represents an entity in a customizable form
332 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
339 title
: ts('Entity Name'),
340 help
: ts('Symbolic name which referenced in the fields'),
344 title
: ts('Entity Type'),
346 options
: ['IndividualModel', 'ActivityModel']
349 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
350 title
: ts('Sub Type'),
357 initialize: function() {
360 * Get a list of all fields that can be used with this entity.
362 * @return {Object} keys are field names; values are fieldSchemas
364 getFieldSchemas: function() {
365 var ufEntityModel
= this;
366 var modelClass
= this.getModelClass();
368 if (this.get('entity_sub_type') == '*') {
369 return _
.clone(modelClass
.prototype.schema
);
373 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
374 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
375 if (ufEntityModel
.isSectionEnabled(section
)) {
376 result
[fieldName
] = fieldSchema
;
381 isSectionEnabled: function(section
) {
382 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')));
384 getSections: function() {
385 var ufEntityModel
= this;
387 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
388 if (ufEntityModel
.isSectionEnabled(section
)) {
389 result
[sectionKey
] = section
;
394 getModelClass: function() {
395 return CRM
.Schema
[this.get('entity_type')];
400 * Represents a list of entities in a customizable form
403 * - ufGroupModel: UFGroupModel
405 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
406 model
: CRM
.UF
.UFEntityModel
,
408 initialize: function(models
, options
) {
409 options
= options
|| {};
410 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
415 * @return {UFEntityModel} if found; otherwise, null
417 getByName: function(name
) {
418 // TODO consider indexing
419 return this.find(function(ufEntityModel
){
420 return ufEntityModel
.get('entity_name') == name
;
426 * Represents a customizable form
428 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
430 title
: ts('Unnamed Profile'),
443 title
: ts('Profile Name'),
446 validators
: ['required']
449 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
454 title
: ts('Include reCAPTCHA?'),
460 title
: ts('Add new contacts to a Group?'),
461 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.'),
465 title
: ts('Cancel Redirect URL'),
466 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.'),
478 title
: ts('Post-form Help'),
479 help
: ts('Explanatory text displayed at the end of the form.')
480 + ts('Note that this help text is displayed on profile create/edit screens only.'),
484 title
: ts('Pre-form Help '),
485 help
: ts('Explanatory text displayed at the beginning of the form.')
486 + ts('Note that this help text is displayed on profile create/edit screens only.'),
490 title
: ts('Is this CiviCRM Profile active?'),
495 title
: ts('Drupal user account registration option?'),// FIXME
498 options
: YESNO
// FIXME
501 title
: ts('Include profile edit links in search results?'),
502 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.'),
507 title
: ts('Enable mapping for this profile?'),
508 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.'),
512 'is_proximity_search': {
513 title
: ts('Proximity search'),
516 options
: YESNO
// FIXME
524 title
: ts('Include Drupal user account information links in search results?'), // FIXME
530 title
: ts('What to do upon duplicate match'),
533 options
: YESNO
// FIXME
535 'limit_listings_group_id': {
536 title
: ts('Limit listings to a specific Group?'),
537 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.'),
541 title
: ts('Notify when profile form is submitted?'),
542 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.'),
546 title
: ts('Redirect URL'),
547 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.'"),
552 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.'),
554 // FIXME positive int
557 initialize: function() {
558 var ufGroupModel
= this;
560 if (!this.getRel('ufEntityCollection')) {
561 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
565 this.setRel('ufEntityCollection', ufEntityCollection
);
568 if (!this.getRel('ufFieldCollection')) {
569 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
570 uf_group_id
: this.id
,
573 this.setRel('ufFieldCollection', ufFieldCollection
);
576 if (!this.getRel('paletteFieldCollection')) {
577 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
580 paletteFieldCollection
.sync = function(method
, model
, options
) {
581 options
|| (options
= {});
582 // console.log(method, model, options);
585 var success
= options
.success
;
586 options
.success = function(resp
, status
, xhr
) {
587 if (success
) success(resp
, status
, xhr
);
588 model
.trigger('sync', model
, resp
, options
);
590 success(ufGroupModel
.buildPaletteFields());
597 throw 'Unsupported method: ' + method
;
600 this.setRel('paletteFieldCollection', paletteFieldCollection
);
603 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
604 this.resetEntities();
606 this.on('change', watchChanges
);
609 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
610 * is suitable for a new, identical UFGroup.
612 * @return {CRM.UF.UFGroupModel}
614 deepCopy: function() {
615 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
616 copy
.getRel('ufEntityCollection').reset(
617 this.getRel('ufEntityCollection').toJSON()
618 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
620 copy
.getRel('ufFieldCollection').reset(
621 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
622 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
625 copy
.set('title', ts('%1 (Copy)', {
630 getModelClass: function(entity_name
) {
631 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
632 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
633 return ufEntity
.getModelClass();
635 getFieldSchema: function(entity_name
, field_name
) {
636 var modelClass
= this.getModelClass(entity_name
);
637 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
640 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
647 * Check that the group_type contains *only* the types listed in validTypes
649 * @param string validTypesExpr
652 checkGroupType: function(validTypesExpr
) {
653 var allMatched
= true;
654 if (! this.get('group_type') || this.get('group_type') == '') {
658 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
659 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
661 // Every actual.coreType is a valid.coreType
662 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
663 if (! validTypes
.coreTypes
[actualCoreType
]) {
668 // Every actual.subType is a valid.subType
669 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
670 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
674 // actualSubTypeIds is a list of all subtypes which can be used by group,
675 // so it's sufficient to match any one of them
676 var subTypeMatched
= false;
677 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
678 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
679 subTypeMatched
= true;
682 allMatched
= allMatched
&& subTypeMatched
;
686 calculateContactEntityType: function() {
687 var ufGroupModel
= this;
689 // set proper entity model based on selected profile
690 var contactTypes
= ['Individual', 'Household', 'Organization'];
691 var profileType
= ufGroupModel
.get('group_type') || '';
693 // check if selected profile have subtype defined eg: ["Individual,Contact,Case", "caseType:7"]
694 if (_
.isArray(profileType
) && profileType
[0]) {
695 profileType
= profileType
[0];
697 profileType
= profileType
.split(',');
700 _
.each(profileType
, function (ptype
) {
701 if ($.inArray(ptype
, contactTypes
) > -1) {
702 ufEntityModel
= ptype
+ 'Model';
707 return ufEntityModel
;
709 setUFGroupModel: function(entityType
, allEntityModels
) {
710 var ufGroupModel
= this;
712 var newUfEntityModels
= [];
713 _
.each(allEntityModels
, function (values
) {
714 if (entityType
&& values
.entity_name
== 'contact_1') {
715 values
.entity_type
= entityType
;
717 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
720 ufGroupModel
.getRel('ufEntityCollection').reset(newUfEntityModels
);
722 resetEntities: function() {
723 var ufGroupModel
= this;
724 var deleteFieldList
= [];
725 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
726 if (!ufFieldModel
.getFieldSchema()) {
727 CRM
.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
728 1: ufFieldModel
.get('label')
729 }), '', 'alert', {expires
: false});
730 deleteFieldList
.push(ufFieldModel
);
734 _
.each(deleteFieldList
, function(ufFieldModel
) {
735 ufFieldModel
.destroyLocal();
738 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
740 // reset to redraw the cancel after entity type is updated.
741 ufGroupModel
.getRel('ufFieldCollection').reset(ufGroupModel
.getRel('ufFieldCollection').toJSON());
745 * @return {Array} of PaletteFieldModel
747 buildPaletteFields: function() {
748 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
750 // Note: The system as a whole is ill-defined in cases where we have an existing
751 // UFField that references a model field that disappears.
753 var ufGroupModel
= this;
755 var oldPaletteFieldModelsBySig
= {};
756 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
757 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
760 var newPaletteFieldModels
= [];
761 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
762 var modelClass
= ufEntityModel
.getModelClass();
763 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
764 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
766 model
= new CRM
.Designer
.PaletteFieldModel({
767 modelClass
: modelClass
,
768 entityName
: ufEntityModel
.get('entity_name'),
772 newPaletteFieldModels
.push(model
);
776 return newPaletteFieldModels
;
781 * Represents a list of customizable form
783 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
784 model
: CRM
.UF
.UFGroupModel