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';
91 throw "Cannot guess entity name for field_type=" + field_type
;
96 * Represents a field in a customizable form.
98 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
100 * Backbone.Form descripton of the field to which this refers
106 * @var bool, non-persistent indication of whether this field is unique or duplicate
107 * within its UFFieldCollection
127 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
130 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
134 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
138 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
143 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
147 'is_multi_summary': {
148 title
: ts("Include in multi-record listing?"),
153 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
162 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
167 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
172 title
: ts('Field Label'),
175 'location_type_id': {
176 title
: ts('Location Type'),
178 options
: LOCATION_TYPES
181 title
: ts('Phone Type'),
186 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
194 initialize: function() {
195 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
196 this.on("rel:ufGroupModel", this.applyDefaults
, this);
197 this.on('change', watchChanges
);
199 applyDefaults: function() {
200 var fieldSchema
= this.getFieldSchema();
201 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
202 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
204 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
205 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
208 isInSelectorAllowed: function() {
209 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
211 return visibility
.isInSelectorAllowed
;
217 getFieldSchema: function() {
218 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
221 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
222 * have a unique signature.
226 getSignature: function() {
227 return this.get("entity_name")
228 + '::' + this.get("field_name")
229 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
230 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
234 * This is like destroy(), but it only destroys the item on the client-side;
235 * it does not trigger REST or Backbone.sync() operations.
239 destroyLocal: function() {
240 this.trigger('destroy', this, this.collection
, {});
246 * Represents a list of fields in a customizable form
251 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
252 model
: CRM
.UF
.UFFieldModel
,
253 uf_group_id
: null, // int
254 initialize: function(models
, options
) {
255 options
= options
|| {};
256 this.uf_group_id
= options
.uf_group_id
;
257 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
258 this.on('add', this.watchDuplicates
, this);
259 this.on('remove', this.unwatchDuplicates
, this);
260 this.on('change', watchChanges
);
261 this.on('add', watchChanges
);
262 this.on('remove', watchChanges
);
264 getFieldsByName: function(entityName
, fieldName
) {
265 return this.filter(function(ufFieldModel
) {
266 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
269 toSortedJSON: function() {
270 var fields
= this.map(function(ufFieldModel
){
271 return ufFieldModel
.toStrictJSON();
273 return _
.sortBy(fields
, function(ufFieldJSON
){
274 return parseInt(ufFieldJSON
.weight
);
277 isAddable: function(ufFieldModel
) {
278 var entity_name
= ufFieldModel
.get('entity_name'),
279 field_name
= ufFieldModel
.get('field_name'),
280 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
285 var fields
= this.getFieldsByName(entity_name
, field_name
);
287 if (fieldSchema
.civiIsLocation
) {
288 limit
*= LOCATION_TYPES
.length
;
290 if (fieldSchema
.civiIsPhone
) {
291 limit
*= PHONE_TYPES
.length
;
293 return fields
.length
< limit
;
295 watchDuplicates: function(model
, collection
, options
) {
296 model
.on('change:location_type_id', this.markDuplicates
, this);
297 model
.on('change:phone_type_id', this.markDuplicates
, this);
298 this.markDuplicates();
300 unwatchDuplicates: function(model
, collection
, options
) {
301 model
.off('change:location_type_id', this.markDuplicates
, this);
302 model
.off('change:phone_type_id', this.markDuplicates
, this);
303 this.markDuplicates();
305 hasDuplicates: function() {
306 var firstDupe
= this.find(function(ufFieldModel
){
307 return ufFieldModel
.get('is_duplicate');
309 return firstDupe
? true : false;
314 markDuplicates: function() {
315 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
316 return ufFieldModel
.getSignature();
318 this.each(function(ufFieldModel
){
319 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
320 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
321 ufFieldModel
.set('is_duplicate', is_duplicate
);
328 * Represents an entity in a customizable form
330 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
337 title
: ts('Entity Name'),
338 help
: ts('Symbolic name which referenced in the fields'),
342 title
: ts('Entity Type'),
344 options
: ['IndividualModel', 'ActivityModel']
347 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
348 title
: ts('Sub Type'),
355 initialize: function() {
358 * Get a list of all fields that can be used with this entity.
360 * @return {Object} keys are field names; values are fieldSchemas
362 getFieldSchemas: function() {
363 var ufEntityModel
= this;
364 var modelClass
= this.getModelClass();
366 if (this.get('entity_sub_type') == '*') {
367 return _
.clone(modelClass
.prototype.schema
);
371 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
372 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
373 if (ufEntityModel
.isSectionEnabled(section
)) {
374 result
[fieldName
] = fieldSchema
;
379 isSectionEnabled: function(section
) {
380 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')));
382 getSections: function() {
383 var ufEntityModel
= this;
385 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
386 if (ufEntityModel
.isSectionEnabled(section
)) {
387 result
[sectionKey
] = section
;
392 getModelClass: function() {
393 return CRM
.Schema
[this.get('entity_type')];
398 * Represents a list of entities in a customizable form
401 * - ufGroupModel: UFGroupModel
403 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
404 model
: CRM
.UF
.UFEntityModel
,
406 initialize: function(models
, options
) {
407 options
= options
|| {};
408 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
413 * @return {UFEntityModel} if found; otherwise, null
415 getByName: function(name
) {
416 // TODO consider indexing
417 return this.find(function(ufEntityModel
){
418 return ufEntityModel
.get('entity_name') == name
;
424 * Represents a customizable form
426 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
428 title
: ts('Unnamed Profile'),
441 title
: ts('Profile Name'),
444 validators
: ['required']
447 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
452 title
: ts('Include reCAPTCHA?'),
458 title
: ts('Add new contacts to a Group?'),
459 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.'),
463 title
: ts('Cancel Redirect URL'),
464 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.'),
476 title
: ts('Post-form Help'),
477 help
: ts('Explanatory text displayed at the end of the form.')
478 + ts('Note that this help text is displayed on profile create/edit screens only.'),
482 title
: ts('Pre-form Help '),
483 help
: ts('Explanatory text displayed at the beginning of the form.')
484 + ts('Note that this help text is displayed on profile create/edit screens only.'),
488 title
: ts('Is this CiviCRM Profile active?'),
493 title
: ts('Drupal user account registration option?'),// FIXME
496 options
: YESNO
// FIXME
499 title
: ts('Include profile edit links in search results?'),
500 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.'),
505 title
: ts('Enable mapping for this profile?'),
506 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.'),
510 'is_proximity_search': {
511 title
: ts('Proximity search'),
514 options
: YESNO
// FIXME
522 title
: ts('Include Drupal user account information links in search results?'), // FIXME
528 title
: ts('What to do upon duplicate match'),
531 options
: YESNO
// FIXME
533 'limit_listings_group_id': {
534 title
: ts('Limit listings to a specific Group?'),
535 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.'),
539 title
: ts('Notify when profile form is submitted?'),
540 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.'),
544 title
: ts('Redirect URL'),
545 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.'"),
550 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.'),
552 // FIXME positive int
555 initialize: function() {
556 var ufGroupModel
= this;
558 if (!this.getRel('ufEntityCollection')) {
559 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
563 this.setRel('ufEntityCollection', ufEntityCollection
);
566 if (!this.getRel('ufFieldCollection')) {
567 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
568 uf_group_id
: this.id
,
571 this.setRel('ufFieldCollection', ufFieldCollection
);
574 if (!this.getRel('paletteFieldCollection')) {
575 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
578 paletteFieldCollection
.sync = function(method
, model
, options
) {
579 options
|| (options
= {});
580 // console.log(method, model, options);
583 var success
= options
.success
;
584 options
.success = function(resp
, status
, xhr
) {
585 if (success
) success(resp
, status
, xhr
);
586 model
.trigger('sync', model
, resp
, options
);
588 success(ufGroupModel
.buildPaletteFields());
595 throw 'Unsupported method: ' + method
;
598 this.setRel('paletteFieldCollection', paletteFieldCollection
);
601 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
602 this.resetEntities();
604 this.on('change', watchChanges
);
607 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
608 * is suitable for a new, identical UFGroup.
610 * @return {CRM.UF.UFGroupModel}
612 deepCopy: function() {
613 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
614 copy
.getRel('ufEntityCollection').reset(
615 this.getRel('ufEntityCollection').toJSON()
616 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
618 copy
.getRel('ufFieldCollection').reset(
619 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
620 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
623 copy
.set('title', ts('%1 (Copy)', {
628 getModelClass: function(entity_name
) {
629 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
630 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
631 return ufEntity
.getModelClass();
633 getFieldSchema: function(entity_name
, field_name
) {
634 var modelClass
= this.getModelClass(entity_name
);
635 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
638 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
645 * Check that the group_type contains *only* the types listed in validTypes
647 * @param string validTypesExpr
650 checkGroupType: function(validTypesExpr
) {
651 var allMatched
= true;
652 if (! this.get('group_type') || this.get('group_type') == '') {
656 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
657 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
659 // Every actual.coreType is a valid.coreType
660 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
661 if (! validTypes
.coreTypes
[actualCoreType
]) {
666 // Every actual.subType is a valid.subType
667 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
668 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
672 // actualSubTypeIds is a list of all subtypes which can be used by group,
673 // so it's sufficient to match any one of them
674 var subTypeMatched
= false;
675 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
676 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
677 subTypeMatched
= true;
680 allMatched
= allMatched
&& subTypeMatched
;
684 calculateContactEntityType: function() {
685 var ufGroupModel
= this;
687 // set proper entity model based on selected profile
688 var contactTypes
= ['Individual', 'Household', 'Organization'];
689 var profileType
= ufGroupModel
.get('group_type') || '';
690 profileType
= profileType
.split(',');
692 _
.each(profileType
, function (ptype
) {
693 if ($.inArray(ptype
, contactTypes
) > -1) {
694 ufEntityModel
= ptype
+ 'Model';
699 return ufEntityModel
;
701 setUFGroupModel: function(entityType
, allEntityModels
) {
702 var ufGroupModel
= this;
704 var newUfEntityModels
= [];
705 _
.each(allEntityModels
, function (values
) {
706 if (values
.entity_name
== 'contact_1') {
707 values
.entity_type
= entityType
;
709 newUfEntityModels
.push(new CRM
.UF
.UFEntityModel(values
));
712 ufGroupModel
.getRel('ufEntityCollection').reset(newUfEntityModels
);
714 resetEntities: function() {
715 var ufGroupModel
= this;
716 var deleteFieldList
= [];
717 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
718 if (!ufFieldModel
.getFieldSchema()) {
719 CRM
.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', {
720 1: ufFieldModel
.get('label')
721 }), '', 'alert', {expires
: false});
722 deleteFieldList
.push(ufFieldModel
);
726 _
.each(deleteFieldList
, function(ufFieldModel
) {
727 ufFieldModel
.destroyLocal();
730 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
732 // reset to redraw the cancel after entity type is updated.
733 ufGroupModel
.getRel('ufFieldCollection').reset(ufGroupModel
.getRel('ufFieldCollection').toJSON());
737 * @return {Array} of PaletteFieldModel
739 buildPaletteFields: function() {
740 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
742 // Note: The system as a whole is ill-defined in cases where we have an existing
743 // UFField that references a model field that disappears.
745 var ufGroupModel
= this;
747 var oldPaletteFieldModelsBySig
= {};
748 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
749 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
752 var newPaletteFieldModels
= [];
753 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
754 var modelClass
= ufEntityModel
.getModelClass();
755 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
756 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
758 model
= new CRM
.Designer
.PaletteFieldModel({
759 modelClass
: modelClass
,
760 entityName
: ufEntityModel
.get('entity_name'),
764 newPaletteFieldModels
.push(model
);
768 return newPaletteFieldModels
;
773 * Represents a list of customizable form
775 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
776 model
: CRM
.UF
.UFGroupModel