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
) {
82 return 'organization_1';
88 return 'contribution_1';
90 return 'membership_1';
92 return 'participant_1';
94 throw "Cannot guess entity name for field_type=" + field_type
;
99 * Represents a field in a customizable form.
101 CRM
.UF
.UFFieldModel
= CRM
.Backbone
.Model
.extend({
103 * Backbone.Form descripton of the field to which this refers
109 * @var bool, non-persistent indication of whether this field is unique or duplicate
110 * within its UFFieldCollection
130 options
: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity']
133 title
: addHelp(ts('Field Post Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
137 title
: addHelp(ts('Field Pre Help'), {id
: "help", file
:"CRM/UF/Form/Field"}),
141 title
: addHelp(ts('Results Columns?'), {id
: "in_selector", file
:"CRM/UF/Form/Field"}),
146 title
: addHelp(ts('Active?'), {id
: "is_active", file
:"CRM/UF/Form/Field"}),
150 'is_multi_summary': {
151 title
: ts("Include in multi-record listing?"),
156 title
: addHelp(ts('Required?'), {id
: "is_required", file
:"CRM/UF/Form/Field"}),
165 title
: addHelp(ts("Searchable"), {id
: "is_searchable", file
:"CRM/UF/Form/Field"}),
170 title
: addHelp(ts('View Only?'), {id
: "is_view", file
:"CRM/UF/Form/Field"}),
175 title
: ts('Field Label'),
178 'location_type_id': {
179 title
: ts('Location Type'),
181 options
: LOCATION_TYPES
184 title
: ts('Phone Type'),
189 title
: addHelp(ts('Visibility'), {id
: "visibility", file
:"CRM/UF/Form/Field"}),
197 initialize: function() {
198 this.set('entity_name', CRM
.UF
.guessEntityName(this.get('field_type')));
199 this.on("rel:ufGroupModel", this.applyDefaults
, this);
200 this.on('change', watchChanges
);
202 applyDefaults: function() {
203 var fieldSchema
= this.getFieldSchema();
204 if (fieldSchema
&& fieldSchema
.civiIsLocation
&& !this.get('location_type_id')) {
205 this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID
);
207 if (fieldSchema
&& fieldSchema
.civiIsPhone
&& !this.get('phone_type_id')) {
208 this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID
);
211 isInSelectorAllowed: function() {
212 var visibility
= _
.first(_
.where(VISIBILITY
, {val
: this.get('visibility')}));
214 return visibility
.isInSelectorAllowed
;
220 getFieldSchema: function() {
221 return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name'));
224 * Create a uniqueness signature. Ideally, each UFField in a UFGroup should
225 * have a unique signature.
229 getSignature: function() {
230 return this.get("entity_name")
231 + '::' + this.get("field_name")
232 + '::' + (this.get("location_type_id") ? this.get("location_type_id") : '')
233 + '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : '');
237 * This is like destroy(), but it only destroys the item on the client-side;
238 * it does not trigger REST or Backbone.sync() operations.
242 destroyLocal: function() {
243 this.trigger('destroy', this, this.collection
, {});
249 * Represents a list of fields in a customizable form
254 CRM
.UF
.UFFieldCollection
= CRM
.Backbone
.Collection
.extend({
255 model
: CRM
.UF
.UFFieldModel
,
256 uf_group_id
: null, // int
257 initialize: function(models
, options
) {
258 options
= options
|| {};
259 this.uf_group_id
= options
.uf_group_id
;
260 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
261 this.on('add', this.watchDuplicates
, this);
262 this.on('remove', this.unwatchDuplicates
, this);
263 this.on('change', watchChanges
);
264 this.on('add', watchChanges
);
265 this.on('remove', watchChanges
);
267 getFieldsByName: function(entityName
, fieldName
) {
268 return this.filter(function(ufFieldModel
) {
269 return (ufFieldModel
.get('entity_name') == entityName
&& ufFieldModel
.get('field_name') == fieldName
);
272 toSortedJSON: function() {
273 var fields
= this.map(function(ufFieldModel
){
274 return ufFieldModel
.toStrictJSON();
276 return _
.sortBy(fields
, function(ufFieldJSON
){
277 return parseInt(ufFieldJSON
.weight
);
280 isAddable: function(ufFieldModel
) {
281 var entity_name
= ufFieldModel
.get('entity_name'),
282 field_name
= ufFieldModel
.get('field_name'),
283 fieldSchema
= this.getRel('ufGroupModel').getFieldSchema(ufFieldModel
.get('entity_name'), ufFieldModel
.get('field_name'));
288 var fields
= this.getFieldsByName(entity_name
, field_name
);
290 if (fieldSchema
.civiIsLocation
) {
291 limit
*= LOCATION_TYPES
.length
;
293 if (fieldSchema
.civiIsPhone
) {
294 limit
*= PHONE_TYPES
.length
;
296 return fields
.length
< limit
;
298 watchDuplicates: function(model
, collection
, options
) {
299 model
.on('change:location_type_id', this.markDuplicates
, this);
300 model
.on('change:phone_type_id', this.markDuplicates
, this);
301 this.markDuplicates();
303 unwatchDuplicates: function(model
, collection
, options
) {
304 model
.off('change:location_type_id', this.markDuplicates
, this);
305 model
.off('change:phone_type_id', this.markDuplicates
, this);
306 this.markDuplicates();
308 hasDuplicates: function() {
309 var firstDupe
= this.find(function(ufFieldModel
){
310 return ufFieldModel
.get('is_duplicate');
312 return firstDupe
? true : false;
317 markDuplicates: function() {
318 var ufFieldModelsByKey
= this.groupBy(function(ufFieldModel
) {
319 return ufFieldModel
.getSignature();
321 this.each(function(ufFieldModel
){
322 var is_duplicate
= ufFieldModelsByKey
[ufFieldModel
.getSignature()].length
> 1;
323 if (is_duplicate
!= ufFieldModel
.get('is_duplicate')) {
324 ufFieldModel
.set('is_duplicate', is_duplicate
);
331 * Represents an entity in a customizable form
333 CRM
.UF
.UFEntityModel
= CRM
.Backbone
.Model
.extend({
340 title
: ts('Entity Name'),
341 help
: ts('Symbolic name which referenced in the fields'),
345 title
: ts('Entity Type'),
347 options
: ['IndividualModel', 'ActivityModel']
350 // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none
351 title
: ts('Sub Type'),
358 initialize: function() {
361 * Get a list of all fields that can be used with this entity.
363 * @return {Object} keys are field names; values are fieldSchemas
365 getFieldSchemas: function() {
366 var ufEntityModel
= this;
367 var modelClass
= this.getModelClass();
369 if (this.get('entity_sub_type') == '*') {
370 return _
.clone(modelClass
.prototype.schema
);
374 _
.each(modelClass
.prototype.schema
, function(fieldSchema
, fieldName
){
375 var section
= modelClass
.prototype.sections
[fieldSchema
.section
];
376 if (ufEntityModel
.isSectionEnabled(section
)) {
377 result
[fieldName
] = fieldSchema
;
382 isSectionEnabled: function(section
) {
383 return (!section
|| !section
.extends_entity_column_value
|| _
.contains(section
.extends_entity_column_value
, this.get('entity_sub_type')));
385 getSections: function() {
386 var ufEntityModel
= this;
388 _
.each(ufEntityModel
.getModelClass().prototype.sections
, function(section
, sectionKey
){
389 if (ufEntityModel
.isSectionEnabled(section
)) {
390 result
[sectionKey
] = section
;
395 getModelClass: function() {
396 return CRM
.Schema
[this.get('entity_type')];
401 * Represents a list of entities in a customizable form
404 * - ufGroupModel: UFGroupModel
406 CRM
.UF
.UFEntityCollection
= CRM
.Backbone
.Collection
.extend({
407 model
: CRM
.UF
.UFEntityModel
,
409 initialize: function(models
, options
) {
410 options
= options
|| {};
411 this.initializeCopyToChildrenRelation('ufGroupModel', options
.ufGroupModel
, models
);
416 * @return {UFEntityModel} if found; otherwise, null
418 getByName: function(name
) {
419 // TODO consider indexing
420 return this.find(function(ufEntityModel
){
421 return ufEntityModel
.get('entity_name') == name
;
427 * Represents a customizable form
429 CRM
.UF
.UFGroupModel
= CRM
.Backbone
.Model
.extend({
431 title
: ts('Unnamed Profile'),
444 title
: ts('Profile Name'),
447 validators
: ['required']
450 // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes
455 title
: ts('Include reCAPTCHA?'),
461 title
: ts('Add new contacts to a Group?'),
462 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.'),
466 title
: ts('Cancel Redirect URL'),
467 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.'),
479 title
: ts('Post-form Help'),
480 help
: ts('Explanatory text displayed at the end of the form.')
481 + ts('Note that this help text is displayed on profile create/edit screens only.'),
485 title
: ts('Pre-form Help '),
486 help
: ts('Explanatory text displayed at the beginning of the form.')
487 + ts('Note that this help text is displayed on profile create/edit screens only.'),
491 title
: ts('Is this CiviCRM Profile active?'),
496 title
: ts('Drupal user account registration option?'),// FIXME
499 options
: YESNO
// FIXME
502 title
: ts('Include profile edit links in search results?'),
503 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.'),
508 title
: ts('Enable mapping for this profile?'),
509 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.'),
513 'is_proximity_search': {
514 title
: ts('Proximity search'),
517 options
: YESNO
// FIXME
525 title
: ts('Include Drupal user account information links in search results?'), // FIXME
531 title
: ts('What to do upon duplicate match'),
534 options
: YESNO
// FIXME
536 'limit_listings_group_id': {
537 title
: ts('Limit listings to a specific Group?'),
538 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.'),
542 title
: ts('Notify when profile form is submitted?'),
543 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.'),
547 title
: ts('Redirect URL'),
548 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.'"),
553 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.'),
555 // FIXME positive int
558 initialize: function() {
559 var ufGroupModel
= this;
561 if (!this.getRel('ufEntityCollection')) {
562 var ufEntityCollection
= new CRM
.UF
.UFEntityCollection([], {
566 this.setRel('ufEntityCollection', ufEntityCollection
);
569 if (!this.getRel('ufFieldCollection')) {
570 var ufFieldCollection
= new CRM
.UF
.UFFieldCollection([], {
571 uf_group_id
: this.id
,
574 this.setRel('ufFieldCollection', ufFieldCollection
);
577 if (!this.getRel('paletteFieldCollection')) {
578 var paletteFieldCollection
= new CRM
.Designer
.PaletteFieldCollection([], {
581 paletteFieldCollection
.sync = function(method
, model
, options
) {
582 options
|| (options
= {});
583 // console.log(method, model, options);
586 var success
= options
.success
;
587 options
.success = function(resp
, status
, xhr
) {
588 if (success
) success(resp
, status
, xhr
);
589 model
.trigger('sync', model
, resp
, options
);
591 success(ufGroupModel
.buildPaletteFields());
598 throw 'Unsupported method: ' + method
;
601 this.setRel('paletteFieldCollection', paletteFieldCollection
);
604 this.getRel('ufEntityCollection').on('reset', this.resetEntities
, this);
605 this.resetEntities();
607 this.on('change', watchChanges
);
610 * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result
611 * is suitable for a new, identical UFGroup.
613 * @return {CRM.UF.UFGroupModel}
615 deepCopy: function() {
616 var copy
= new CRM
.UF
.UFGroupModel(_
.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type']));
617 copy
.getRel('ufEntityCollection').reset(
618 this.getRel('ufEntityCollection').toJSON()
619 // FIXME: for configurable entities, omit ['id', 'uf_group_id']
621 copy
.getRel('ufFieldCollection').reset(
622 this.getRel('ufFieldCollection').map(function(ufFieldModel
) {
623 return _
.omit(ufFieldModel
.toStrictJSON(), ['id', 'uf_group_id']);
626 copy
.set('title', ts('%1 (Copy)', {
631 getModelClass: function(entity_name
) {
632 var ufEntity
= this.getRel('ufEntityCollection').getByName(entity_name
);
633 if (!ufEntity
) throw 'Failed to locate entity: ' + entity_name
;
634 return ufEntity
.getModelClass();
636 getFieldSchema: function(entity_name
, field_name
) {
637 var modelClass
= this.getModelClass(entity_name
);
638 var fieldSchema
= modelClass
.prototype.schema
[field_name
];
641 console
.log('Failed to locate field: ' + entity_name
+ "." + field_name
);
648 * Check that the group_type contains *only* the types listed in validTypes
650 * @param string validTypesExpr
653 checkGroupType: function(validTypesExpr
) {
654 var allMatched
= true;
655 if (! this.get('group_type') || this.get('group_type') == '') {
659 var actualTypes
= CRM
.UF
.parseTypeList(this.get('group_type'));
660 var validTypes
= CRM
.UF
.parseTypeList(validTypesExpr
);
662 // Every actual.coreType is a valid.coreType
663 _
.each(actualTypes
.coreTypes
, function(ignore
, actualCoreType
) {
664 if (! validTypes
.coreTypes
[actualCoreType
]) {
669 // Every actual.subType is a valid.subType
670 _
.each(actualTypes
.subTypes
, function(actualSubTypeIds
, actualSubTypeKey
) {
671 if (!validTypes
.subTypes
[actualSubTypeKey
]) {
675 // actualSubTypeIds is a list of all subtypes which can be used by group,
676 // so it's sufficient to match any one of them
677 var subTypeMatched
= false;
678 _
.each(actualSubTypeIds
, function(ignore
, actualSubTypeId
) {
679 if (validTypes
.subTypes
[actualSubTypeKey
][actualSubTypeId
]) {
680 subTypeMatched
= true;
683 allMatched
= allMatched
&& subTypeMatched
;
687 resetEntities: function() {
688 var ufGroupModel
= this;
689 ufGroupModel
.getRel('ufFieldCollection').each(function(ufFieldModel
){
690 if (!ufFieldModel
.getFieldSchema()) {
691 CRM
.alert(ts('The data model no longer includes field "%1"! All references to the field have been removed.', {
692 1: ufFieldModel
.get('entity_name') + "." + ufFieldModel
.get('field_name')
693 }), '', 'alert', {expires
: false});
694 ufFieldModel
.destroyLocal();
697 this.getRel('paletteFieldCollection').reset(this.buildPaletteFields());
701 * @return {Array} of PaletteFieldModel
703 buildPaletteFields: function() {
704 // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones
706 // Note: The system as a whole is ill-defined in cases where we have an existing
707 // UFField that references a model field that disappears.
709 var ufGroupModel
= this;
711 var oldPaletteFieldModelsBySig
= {};
712 this.getRel('paletteFieldCollection').each(function(paletteFieldModel
){
713 oldPaletteFieldModelsBySig
[paletteFieldModel
.get("entityName") + '::' + paletteFieldModel
.get("fieldName")] = paletteFieldModel
;
716 var newPaletteFieldModels
= [];
717 this.getRel('ufEntityCollection').each(function(ufEntityModel
){
718 var modelClass
= ufEntityModel
.getModelClass();
719 _
.each(ufEntityModel
.getFieldSchemas(), function(value
, key
, list
) {
720 var model
= oldPaletteFieldModelsBySig
[ufEntityModel
.get('entity_name') + '::' + key
];
722 model
= new CRM
.Designer
.PaletteFieldModel({
723 modelClass
: modelClass
,
724 entityName
: ufEntityModel
.get('entity_name'),
728 newPaletteFieldModels
.push(model
);
732 return newPaletteFieldModels
;
737 * Represents a list of customizable form
739 CRM
.UF
.UFGroupCollection
= CRM
.Backbone
.Collection
.extend({
740 model
: CRM
.UF
.UFGroupModel