2 var CRM
= (window
.CRM
) ? (window
.CRM
) : (window
.CRM
= {});
3 if (!CRM
.UF
) CRM
.UF
= {};
6 {val
: 0, label
: ts('No')},
7 {val
: 1, label
: ts('Yes')}
11 {val
: 'User and User Admin Only', label
: ts('User and User Admin Only'), isInSelectorAllowed
: false},
12 {val
: 'Public Pages', label
: ts('Public Pages'), isInSelectorAllowed
: true},
13 {val
: 'Public Pages and Listings', label
: ts('Public Pages and Listings'), isInSelectorAllowed
: true}
16 var LOCATION_TYPES
= _
.map(CRM
.PseudoConstant
.locationType
, function(value
, key
) {
17 return {val
: key
, label
: value
};
19 LOCATION_TYPES
.unshift({val
: '', label
: ts('Primary')});
20 var DEFAULT_LOCATION_TYPE_ID
= '';
22 var PHONE_TYPES
= _
.map(CRM
.PseudoConstant
.phoneType
, function(value
, key
) {
23 return {val
: key
, label
: value
};
25 var DEFAULT_PHONE_TYPE_ID
= PHONE_TYPES
[0].val
;
28 * Add a help link to a form label
30 function addHelp(title
, options
) {
31 return title
+ ' <a href="#" onclick=\'CRM.help("' + title
+ '", ' + JSON
.stringify(options
) + '); return false;\' title="' + ts('%1 Help', {1: title
}) + '" class="helpicon"></a>';
34 function watchChanges() {
35 CRM
.designerApp
.vent
.trigger('ufUnsaved', true);
39 * Parse a "group_type" expression
41 * @param string groupTypeExpr example: "Individual,Activity\0ActivityType:2:28"
42 * Note: I've seen problems where HTML "�" != JS '\0', so we support ';;' as an equivalent delimiter
43 * @return Object example: {coreTypes: {"Individual":true,"Activity":true}, subTypes: {"ActivityType":{2: true, 28:true}]}}
45 CRM
.UF
.parseTypeList = function(groupTypeExpr
) {
46 var typeList
= {coreTypes
: {}, subTypes
:{}};
47 // The API may have automatically converted a string with '\0' to an array
48 var parts
= _
.isArray(groupTypeExpr
) ? groupTypeExpr
: groupTypeExpr
.replace(';;','\0').split('\0');
49 var coreTypesExpr
= parts
[0];
50 var subTypesExpr
= parts
[1];
52 if (coreTypesExpr
&& coreTypesExpr
!= '') {
53 _
.each(coreTypesExpr
.split(','), function(coreType
){
54 typeList
.coreTypes
[coreType
] = true;
58 if (subTypesExpr
&& subTypesExpr
!= '') {
59 var subTypes
= subTypesExpr
.split(':');
60 var subTypeKey
= subTypes
.shift();
61 typeList
.subTypes
[subTypeKey
] = {};
62 _
.each(subTypes
, function(subTypeId
){
63 typeList
.subTypes
[subTypeKey
][subTypeId
] = true;
70 * This function is a hack for generating simulated values of "entity_name"
71 * in the form-field model.
73 * @param {string} field_type
76 CRM
.UF
.guessEntityName = function(field_type
) {
81 return 'individual_1';
83 return 'organization_1';
89 return 'contribution_1';
91 return 'membership_1';
93 return 'participant_1';
95 throw "Cannot guess entity name for field_type=" + field_type
;
100 * Represents a field in a customizable form.
102 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
104 * Backbone.Form descripton of the field to which this refers
110 * @var bool, non-persistent indication of whether this field is unique or duplicate
111 * within its UFFieldCollection
131 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
134 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
138 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
142 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
147 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
151 'is_multi_summary': {
152 title
: ts("Include in multi-record listing?"),
157 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
166 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
171 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
176 title
: ts('Field Label'),
179 'location_type_id': {
180 title
: ts('Location Type'),
182 options
: LOCATION_TYPES
185 title
: ts('Phone Type'),
190 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
198 initialize: function() {
199 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
200 this.on("rel:ufGroupModel", this.applyDefaults
, this);
201 this.on('change', watchChanges
);
203 applyDefaults: function() {
204 var fieldSchema
= this.getFieldSchema();
205 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
206 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
208 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
209 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
212 isInSelectorAllowed: function() {
213 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
214 return visibility
.isInSelectorAllowed
;
216 getFieldSchema: function() {
217 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
220 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
221 * have a unique signature.
225 getSignature: function() {
226 return this.get("entity_name")
227 + '::' + this.get("field_name")
228 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
229 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
233 * This is like destroy(), but it only destroys the item on the client-side;
234 * it does not trigger REST or Backbone.sync() operations.
238 destroyLocal: function() {
239 this.trigger('destroy', this, this.collection
, {});
245 * Represents a list of fields in a customizable form
250 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
251 model
: CRM
.UF
.UFFieldModel
,
252 uf_group_id
: null, // int
253 initialize: function(models
, options
) {
254 options
= options
|| {};
255 this.uf_group_id
= options
.uf_group_id
;
256 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
257 this.on('add', this.watchDuplicates
, this);
258 this.on('remove', this.unwatchDuplicates
, this);
259 this.on('change', watchChanges
);
260 this.on('add', watchChanges
);
261 this.on('remove', watchChanges
);
263 getFieldsByName: function(entityName
, fieldName
) {
264 return this.filter(function(ufFieldModel
) {
265 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
268 toSortedJSON: function() {
269 var fields
= this.map(function(ufFieldModel
){
270 return ufFieldModel
.toStrictJSON();
272 return _
.sortBy(fields
, function(ufFieldJSON
){
273 return parseInt(ufFieldJSON
.weight
);
276 isAddable: function(ufFieldModel
) {
277 var entity_name
= ufFieldModel
.get('entity_name'),
278 field_name
= ufFieldModel
.get('field_name'),
279 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
284 var fields
= this.getFieldsByName(entity_name
, field_name
);
286 if (fieldSchema
.civiIsLocation
) {
287 limit
*= LOCATION_TYPES
.length
;
289 if (fieldSchema
.civiIsPhone
) {
290 limit
*= PHONE_TYPES
.length
;
292 return fields
.length
< limit
;
294 watchDuplicates: function(model
, collection
, options
) {
295 model
.on('change:location_type_id', this.markDuplicates
, this);
296 model
.on('change:phone_type_id', this.markDuplicates
, this);
297 this.markDuplicates();
299 unwatchDuplicates: function(model
, collection
, options
) {
300 model
.off('change:location_type_id', this.markDuplicates
, this);
301 model
.off('change:phone_type_id', this.markDuplicates
, this);
302 this.markDuplicates();
304 hasDuplicates: function() {
305 var firstDupe
= this.find(function(ufFieldModel
){
306 return ufFieldModel
.get('is_duplicate');
308 return firstDupe
? true : false;
313 markDuplicates: function() {
314 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
315 return ufFieldModel
.getSignature();
317 this.each(function(ufFieldModel
){
318 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
319 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
320 ufFieldModel
.set('is_duplicate', is_duplicate
);
327 * Represents an entity in a customizable form
329 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
336 title
: ts('Entity Name'),
337 help
: ts('Symbolic name which referenced in the fields'),
341 title
: ts('Entity Type'),
343 options
: ['IndividualModel', 'ActivityModel']
346 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
347 title
: ts('Sub Type'),
354 initialize: function() {
357 * Get a list of all fields that can be used with this entity.
359 * @return {Object} keys are field names; values are fieldSchemas
361 getFieldSchemas: function() {
362 var ufEntityModel
= this;
363 var modelClass
= this.getModelClass();
365 if (this.get('entity_sub_type') == '*') {
366 return _
.clone(modelClass
.prototype.schema
);
370 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
371 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
372 if (ufEntityModel
.isSectionEnabled(section
)) {
373 result
[fieldName
] = fieldSchema
;
378 isSectionEnabled: function(section
) {
379 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')));
381 getSections: function() {
382 var ufEntityModel
= this;
384 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
385 if (ufEntityModel
.isSectionEnabled(section
)) {
386 result
[sectionKey
] = section
;
391 getModelClass: function() {
392 return CRM
.Schema
[this.get('entity_type')];
397 * Represents a list of entities in a customizable form
400 * - ufGroupModel: UFGroupModel
402 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
403 model
: CRM
.UF
.UFEntityModel
,
405 initialize: function(models
, options
) {
406 options
= options
|| {};
407 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
412 * @return {UFEntityModel} if found; otherwise, null
414 getByName: function(name
) {
415 // TODO consider indexing
416 return this.find(function(ufEntityModel
){
417 return ufEntityModel
.get('entity_name') == name
;
423 * Represents a customizable form
425 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
427 title
: ts('Unnamed Profile'),
440 title
: ts('Profile Name'),
443 validators
: ['required']
446 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
451 title
: ts('Include reCAPTCHA?'),
457 title
: ts('Add new contacts to a Group?'),
458 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.'),
462 title
: ts('Cancel Redirect URL'),
463 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.'),
475 title
: ts('Post-form Help'),
476 help
: ts('Explanatory text displayed at the end of the form.')
477 + ts('Note that this help text is displayed on profile create/edit screens only.'),
481 title
: ts('Pre-form Help '),
482 help
: ts('Explanatory text displayed at the beginning of the form.')
483 + ts('Note that this help text is displayed on profile create/edit screens only.'),
487 title
: ts('Is this CiviCRM Profile active?'),
492 title
: ts('Drupal user account registration option?'),// FIXME
495 options
: YESNO
// FIXME
498 title
: ts('Include profile edit links in search results?'),
499 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.'),
504 title
: ts('Enable mapping for this profile?'),
505 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.'),
509 'is_proximity_search': {
510 title
: ts('Proximity search'),
513 options
: YESNO
// FIXME
521 title
: ts('Include Drupal user account information links in search results?'), // FIXME
527 title
: ts('What to do upon duplicate match'),
530 options
: YESNO
// FIXME
532 'limit_listings_group_id': {
533 title
: ts('Limit listings to a specific Group?'),
534 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.'),
538 title
: ts('Notify when profile form is submitted?'),
539 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.'),
543 title
: ts('Redirect URL'),
544 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.'"),
549 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.'),
551 // FIXME positive int
554 initialize: function() {
555 var ufGroupModel
= this;
557 if (!this.getRel('ufEntityCollection')) {
558 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
562 this.setRel('ufEntityCollection', ufEntityCollection
);
565 if (!this.getRel('ufFieldCollection')) {
566 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
567 uf_group_id
: this.id
,
570 this.setRel('ufFieldCollection', ufFieldCollection
);
573 if (!this.getRel('paletteFieldCollection')) {
574 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
577 paletteFieldCollection
.sync = function(method
, model
, options
) {
578 options
|| (options
= {});
579 // console.log(method, model, options);
582 var success
= options
.success
;
583 options
.success = function(resp
, status
, xhr
) {
584 if (success
) success(resp
, status
, xhr
);
585 model
.trigger('sync', model
, resp
, options
);
587 success(ufGroupModel
.buildPaletteFields());
594 throw 'Unsupported method: ' + method
;
597 this.setRel('paletteFieldCollection', paletteFieldCollection
);
600 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
601 this.resetEntities();
603 this.on('change', watchChanges
);
606 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
607 * is suitable for a new, identical UFGroup.
609 * @return {CRM.UF.UFGroupModel}
611 deepCopy: function() {
612 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
613 copy
.getRel('ufEntityCollection').reset(
614 this.getRel('ufEntityCollection').toJSON()
615 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
617 copy
.getRel('ufFieldCollection').reset(
618 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
619 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
622 copy
.set('title', ts('%1 (Copy)', {
627 getModelClass: function(entity_name
) {
628 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
629 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
630 return ufEntity
.getModelClass();
632 getFieldSchema: function(entity_name
, field_name
) {
633 var modelClass
= this.getModelClass(entity_name
);
634 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
637 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
644 * Check that the group_type contains *only* the types listed in validTypes
646 * @param string validTypesExpr
649 checkGroupType: function(validTypesExpr
) {
650 var allMatched
= true;
651 if (! this.get('group_type') || this.get('group_type') == '') {
655 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
656 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
658 // Every actual.coreType is a valid.coreType
659 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
660 if (! validTypes
.coreTypes
[actualCoreType
]) {
665 // Every actual.subType is a valid.subType
666 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
667 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
671 // actualSubTypeIds is a list of all subtypes which can be used by group,
672 // so it's sufficient to match any one of them
673 var subTypeMatched
= false;
674 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
675 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
676 subTypeMatched
= true;
679 allMatched
= allMatched
&& subTypeMatched
;
683 resetEntities: function() {
684 var ufGroupModel
= this;
685 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
686 if (!ufFieldModel
.getFieldSchema()) {
687 CRM
.alert(ts('The data model no longer includes field "%1"! All references to the field have been removed.', {
688 1: ufFieldModel
.get('entity_name') + "." + ufFieldModel
.get('field_name')
689 }), '', 'alert', {expires
: false});
690 ufFieldModel
.destroyLocal();
693 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
697 * @return {Array} of PaletteFieldModel
699 buildPaletteFields: function() {
700 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
702 // Note: The system as a whole is ill-defined in cases where we have an existing
703 // UFField that references a model field that disappears.
705 var ufGroupModel
= this;
707 var oldPaletteFieldModelsBySig
= {};
708 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
709 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
712 var newPaletteFieldModels
= [];
713 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
714 var modelClass
= ufEntityModel
.getModelClass();
715 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
716 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
718 model
= new CRM
.Designer
.PaletteFieldModel({
719 modelClass
: modelClass
,
720 entityName
: ufEntityModel
.get('entity_name'),
724 newPaletteFieldModels
.push(model
);
728 return newPaletteFieldModels
;
733 * Represents a list of customizable form
735 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
736 model
: CRM
.UF
.UFGroupModel