Commit | Line | Data |
---|---|---|
4b513f23 | 1 | (function($, _) { |
6a488035 TO |
2 | if (!CRM.UF) CRM.UF = {}; |
3 | ||
4 | var YESNO = [ | |
5 | {val: 0, label: ts('No')}, | |
6 | {val: 1, label: ts('Yes')} | |
7 | ]; | |
8 | ||
9 | var VISIBILITY = [ | |
10 | {val: 'User and User Admin Only', label: ts('User and User Admin Only'), isInSelectorAllowed: false}, | |
fac01cb1 DG |
11 | {val: 'Public Pages', label: ts('Expose Publicly'), isInSelectorAllowed: true}, |
12 | {val: 'Public Pages and Listings', label: ts('Expose Publicly and for Listings'), isInSelectorAllowed: true} | |
6a488035 TO |
13 | ]; |
14 | ||
15 | var LOCATION_TYPES = _.map(CRM.PseudoConstant.locationType, function(value, key) { | |
16 | return {val: key, label: value}; | |
17 | }); | |
18 | LOCATION_TYPES.unshift({val: '', label: ts('Primary')}); | |
19 | var DEFAULT_LOCATION_TYPE_ID = ''; | |
20 | ||
21 | var PHONE_TYPES = _.map(CRM.PseudoConstant.phoneType, function(value, key) { | |
22 | return {val: key, label: value}; | |
23 | }); | |
1a228b4c | 24 | |
25 | var WEBSITE_TYPES = _.map(CRM.PseudoConstant.websiteType, function(value, key) { | |
26 | return {val: key, label: value}; | |
27 | }); | |
6a488035 | 28 | var DEFAULT_PHONE_TYPE_ID = PHONE_TYPES[0].val; |
1a228b4c | 29 | var DEFAULT_WEBSITE_TYPE_ID = WEBSITE_TYPES[0].val; |
6a488035 TO |
30 | |
31 | /** | |
32 | * Add a help link to a form label | |
33 | */ | |
34 | function addHelp(title, options) { | |
085db364 | 35 | return title + ' <a href="#" onclick=\'CRM.help("' + title + '", ' + JSON.stringify(options) + '); return false;\' title="' + ts('%1 Help', {1: title}) + '" class="helpicon"></a>'; |
6a488035 TO |
36 | } |
37 | ||
38 | function watchChanges() { | |
39 | CRM.designerApp.vent.trigger('ufUnsaved', true); | |
40 | } | |
41 | ||
42 | /** | |
43 | * Parse a "group_type" expression | |
44 | * | |
45 | * @param string groupTypeExpr example: "Individual,Activity\0ActivityType:2:28" | |
46 | * Note: I've seen problems where HTML "�" != JS '\0', so we support ';;' as an equivalent delimiter | |
47 | * @return Object example: {coreTypes: {"Individual":true,"Activity":true}, subTypes: {"ActivityType":{2: true, 28:true}]}} | |
48 | */ | |
49 | CRM.UF.parseTypeList = function(groupTypeExpr) { | |
50 | var typeList = {coreTypes: {}, subTypes:{}}; | |
51 | // The API may have automatically converted a string with '\0' to an array | |
52 | var parts = _.isArray(groupTypeExpr) ? groupTypeExpr : groupTypeExpr.replace(';;','\0').split('\0'); | |
53 | var coreTypesExpr = parts[0]; | |
54 | var subTypesExpr = parts[1]; | |
55 | ||
fb079bbf | 56 | if (!_.isEmpty(coreTypesExpr)) { |
6a488035 TO |
57 | _.each(coreTypesExpr.split(','), function(coreType){ |
58 | typeList.coreTypes[coreType] = true; | |
59 | }); | |
60 | } | |
61 | ||
99e239bc | 62 | //CRM-15427 Allow Multiple subtype filtering |
fb079bbf | 63 | if (!_.isEmpty(subTypesExpr)) { |
99e239bc | 64 | if (subTypesExpr.indexOf(';;') !== -1) { |
65 | var subTypeparts = subTypesExpr.replace(/;;/g,'\0').split('\0'); | |
66 | _.each(subTypeparts, function(subTypepart) { | |
67 | var subTypes = subTypepart.split(':'); | |
68 | var subTypeKey = subTypes.shift(); | |
69 | typeList.subTypes[subTypeKey] = {}; | |
70 | _.each(subTypes, function(subTypeId) { | |
71 | typeList.subTypes[subTypeKey][subTypeId] = true; | |
72 | }); | |
73 | }); | |
74 | } | |
75 | else { | |
76 | var subTypes = subTypesExpr.split(':'); | |
77 | var subTypeKey = subTypes.shift(); | |
78 | typeList.subTypes[subTypeKey] = {}; | |
79 | _.each(subTypes, function(subTypeId) { | |
80 | typeList.subTypes[subTypeKey][subTypeId] = true; | |
81 | }); | |
82 | } | |
6a488035 TO |
83 | } |
84 | return typeList; | |
85 | }; | |
86 | ||
87 | /** | |
88 | * This function is a hack for generating simulated values of "entity_name" | |
89 | * in the form-field model. | |
90 | * | |
91 | * @param {string} field_type | |
92 | * @return {string} | |
93 | */ | |
94 | CRM.UF.guessEntityName = function(field_type) { | |
95 | switch (field_type) { | |
96 | case 'Contact': | |
13e4fbdd | 97 | case 'Individual': |
bf86535e | 98 | case 'Organization': |
bf86535e | 99 | case 'Household': |
0103eaf0 | 100 | case 'Formatting': |
41fd8e26 | 101 | return 'contact_1'; |
6a488035 TO |
102 | case 'Activity': |
103 | return 'activity_1'; | |
104 | case 'Contribution': | |
105 | return 'contribution_1'; | |
106 | case 'Membership': | |
107 | return 'membership_1'; | |
108 | case 'Participant': | |
109 | return 'participant_1'; | |
cf1182e6 N |
110 | case 'Case': |
111 | return 'case_1'; | |
6a488035 | 112 | default: |
0c3fb122 | 113 | if (!$.isEmptyObject(CRM.contactSubTypes) && ($.inArray(field_type,CRM.contactSubTypes) > -1)) { |
21fced3b | 114 | return 'contact_1'; |
115 | } | |
116 | else { | |
117 | throw "Cannot guess entity name for field_type=" + field_type; | |
118 | } | |
6a488035 | 119 | } |
3e64165a | 120 | }; |
6a488035 TO |
121 | |
122 | /** | |
123 | * Represents a field in a customizable form. | |
124 | */ | |
125 | CRM.UF.UFFieldModel = CRM.Backbone.Model.extend({ | |
126 | /** | |
e4f46be0 | 127 | * Backbone.Form description of the field to which this refers |
6a488035 TO |
128 | */ |
129 | defaults: { | |
130 | help_pre: '', | |
131 | help_post: '', | |
132 | /** | |
133 | * @var bool, non-persistent indication of whether this field is unique or duplicate | |
134 | * within its UFFieldCollection | |
135 | */ | |
136 | is_duplicate: false | |
137 | }, | |
138 | schema: { | |
139 | 'id': { | |
140 | type: 'Number' | |
141 | }, | |
142 | 'uf_group_id': { | |
143 | type: 'Number' | |
144 | }, | |
145 | 'entity_name': { | |
146 | // pseudo-field | |
147 | type: 'Text' | |
148 | }, | |
149 | 'field_name': { | |
150 | type: 'Text' | |
151 | }, | |
152 | 'field_type': { | |
153 | type: 'Select', | |
154 | options: ['Contact', 'Individual', 'Organization', 'Contribution', 'Membership', 'Participant', 'Activity'] | |
155 | }, | |
156 | 'help_post': { | |
157 | title: addHelp(ts('Field Post Help'), {id: "help", file:"CRM/UF/Form/Field"}), | |
158 | type: 'TextArea' | |
159 | }, | |
160 | 'help_pre': { | |
161 | title: addHelp(ts('Field Pre Help'), {id: "help", file:"CRM/UF/Form/Field"}), | |
162 | type: 'TextArea' | |
163 | }, | |
164 | 'in_selector': { | |
165 | title: addHelp(ts('Results Columns?'), {id: "in_selector", file:"CRM/UF/Form/Field"}), | |
166 | type: 'Select', | |
167 | options: YESNO | |
168 | }, | |
169 | 'is_active': { | |
170 | title: addHelp(ts('Active?'), {id: "is_active", file:"CRM/UF/Form/Field"}), | |
171 | type: 'Select', | |
172 | options: YESNO | |
173 | }, | |
174 | 'is_multi_summary': { | |
175 | title: ts("Include in multi-record listing?"), | |
176 | type: 'Select', | |
177 | options: YESNO | |
178 | }, | |
179 | 'is_required': { | |
180 | title: addHelp(ts('Required?'), {id: "is_required", file:"CRM/UF/Form/Field"}), | |
181 | type: 'Select', | |
182 | options: YESNO | |
183 | }, | |
184 | 'is_reserved': { | |
185 | type: 'Select', | |
186 | options: YESNO | |
187 | }, | |
188 | 'is_searchable': { | |
189 | title: addHelp(ts("Searchable"), {id: "is_searchable", file:"CRM/UF/Form/Field"}), | |
190 | type: 'Select', | |
191 | options: YESNO | |
192 | }, | |
193 | 'is_view': { | |
194 | title: addHelp(ts('View Only?'), {id: "is_view", file:"CRM/UF/Form/Field"}), | |
195 | type: 'Select', | |
196 | options: YESNO | |
197 | }, | |
198 | 'label': { | |
199 | title: ts('Field Label'), | |
3a3663a4 CW |
200 | type: 'Text', |
201 | editorAttrs: {maxlength: 255} | |
6a488035 TO |
202 | }, |
203 | 'location_type_id': { | |
204 | title: ts('Location Type'), | |
205 | type: 'Select', | |
206 | options: LOCATION_TYPES | |
207 | }, | |
1a228b4c | 208 | 'website_type_id': { |
209 | title: ts('Website Type'), | |
210 | type: 'Select', | |
211 | options: WEBSITE_TYPES | |
212 | }, | |
6a488035 TO |
213 | 'phone_type_id': { |
214 | title: ts('Phone Type'), | |
215 | type: 'Select', | |
216 | options: PHONE_TYPES | |
217 | }, | |
218 | 'visibility': { | |
219 | title: addHelp(ts('Visibility'), {id: "visibility", file:"CRM/UF/Form/Field"}), | |
220 | type: 'Select', | |
221 | options: VISIBILITY | |
222 | }, | |
223 | 'weight': { | |
224 | type: 'Number' | |
225 | } | |
226 | }, | |
227 | initialize: function() { | |
0103eaf0 CW |
228 | if (this.get('field_name').indexOf('formatting') === 0) { |
229 | this.schema.help_pre.title = ts('Markup'); | |
230 | } | |
6a488035 TO |
231 | this.set('entity_name', CRM.UF.guessEntityName(this.get('field_type'))); |
232 | this.on("rel:ufGroupModel", this.applyDefaults, this); | |
233 | this.on('change', watchChanges); | |
234 | }, | |
235 | applyDefaults: function() { | |
236 | var fieldSchema = this.getFieldSchema(); | |
237 | if (fieldSchema && fieldSchema.civiIsLocation && !this.get('location_type_id')) { | |
238 | this.set('location_type_id', DEFAULT_LOCATION_TYPE_ID); | |
239 | } | |
1a228b4c | 240 | if (fieldSchema && fieldSchema.civiIsWebsite && !this.get('website_type_id')) { |
241 | this.set('website_type_id', DEFAULT_WEBSITE_TYPE_ID); | |
242 | } | |
6a488035 TO |
243 | if (fieldSchema && fieldSchema.civiIsPhone && !this.get('phone_type_id')) { |
244 | this.set('phone_type_id', DEFAULT_PHONE_TYPE_ID); | |
245 | } | |
246 | }, | |
247 | isInSelectorAllowed: function() { | |
248 | var visibility = _.first(_.where(VISIBILITY, {val: this.get('visibility')})); | |
c1358f4e | 249 | if (visibility) { |
250 | return visibility.isInSelectorAllowed; | |
251 | } | |
252 | else { | |
253 | return false; | |
254 | } | |
6a488035 TO |
255 | }, |
256 | getFieldSchema: function() { | |
257 | return this.getRel('ufGroupModel').getFieldSchema(this.get('entity_name'), this.get('field_name')); | |
258 | }, | |
259 | /** | |
260 | * Create a uniqueness signature. Ideally, each UFField in a UFGroup should | |
261 | * have a unique signature. | |
262 | * | |
263 | * @return {String} | |
264 | */ | |
265 | getSignature: function() { | |
3e64165a TO |
266 | return this.get("entity_name") + |
267 | '::' + this.get("field_name") + | |
1a228b4c | 268 | '::' + (this.get("location_type_id") ? this.get("location_type_id") : this.get("website_type_id") ? this.get("website_type_id") : '') + |
3e64165a | 269 | '::' + (this.get("phone_type_id") ? this.get("phone_type_id") : ''); |
6a488035 TO |
270 | }, |
271 | ||
272 | /** | |
273 | * This is like destroy(), but it only destroys the item on the client-side; | |
274 | * it does not trigger REST or Backbone.sync() operations. | |
275 | * | |
276 | * @return {Boolean} | |
277 | */ | |
278 | destroyLocal: function() { | |
279 | this.trigger('destroy', this, this.collection, {}); | |
280 | return false; | |
281 | } | |
282 | }); | |
283 | ||
284 | /** | |
285 | * Represents a list of fields in a customizable form | |
286 | * | |
287 | * options: | |
288 | * - uf_group_id: int | |
289 | */ | |
290 | CRM.UF.UFFieldCollection = CRM.Backbone.Collection.extend({ | |
291 | model: CRM.UF.UFFieldModel, | |
292 | uf_group_id: null, // int | |
293 | initialize: function(models, options) { | |
294 | options = options || {}; | |
295 | this.uf_group_id = options.uf_group_id; | |
296 | this.initializeCopyToChildrenRelation('ufGroupModel', options.ufGroupModel, models); | |
297 | this.on('add', this.watchDuplicates, this); | |
298 | this.on('remove', this.unwatchDuplicates, this); | |
299 | this.on('change', watchChanges); | |
300 | this.on('add', watchChanges); | |
301 | this.on('remove', watchChanges); | |
302 | }, | |
303 | getFieldsByName: function(entityName, fieldName) { | |
304 | return this.filter(function(ufFieldModel) { | |
305 | return (ufFieldModel.get('entity_name') == entityName && ufFieldModel.get('field_name') == fieldName); | |
306 | }); | |
307 | }, | |
308 | toSortedJSON: function() { | |
309 | var fields = this.map(function(ufFieldModel){ | |
310 | return ufFieldModel.toStrictJSON(); | |
311 | }); | |
312 | return _.sortBy(fields, function(ufFieldJSON){ | |
313 | return parseInt(ufFieldJSON.weight); | |
314 | }); | |
315 | }, | |
316 | isAddable: function(ufFieldModel) { | |
317 | var entity_name = ufFieldModel.get('entity_name'), | |
318 | field_name = ufFieldModel.get('field_name'), | |
319 | fieldSchema = this.getRel('ufGroupModel').getFieldSchema(ufFieldModel.get('entity_name'), ufFieldModel.get('field_name')); | |
0103eaf0 CW |
320 | if (field_name.indexOf('formatting') === 0) { |
321 | return true; | |
322 | } | |
6a488035 TO |
323 | if (! fieldSchema) { |
324 | return false; | |
325 | } | |
326 | var fields = this.getFieldsByName(entity_name, field_name); | |
327 | var limit = 1; | |
328 | if (fieldSchema.civiIsLocation) { | |
329 | limit *= LOCATION_TYPES.length; | |
330 | } | |
1a228b4c | 331 | if (fieldSchema.civiIsWebsite) { |
332 | limit *= WEBSITE_TYPES.length; | |
333 | } | |
6a488035 TO |
334 | if (fieldSchema.civiIsPhone) { |
335 | limit *= PHONE_TYPES.length; | |
336 | } | |
337 | return fields.length < limit; | |
338 | }, | |
339 | watchDuplicates: function(model, collection, options) { | |
340 | model.on('change:location_type_id', this.markDuplicates, this); | |
1a228b4c | 341 | model.on('change:website_type_id', this.markDuplicates, this); |
6a488035 TO |
342 | model.on('change:phone_type_id', this.markDuplicates, this); |
343 | this.markDuplicates(); | |
344 | }, | |
345 | unwatchDuplicates: function(model, collection, options) { | |
346 | model.off('change:location_type_id', this.markDuplicates, this); | |
1a228b4c | 347 | model.off('change:website_type_id', this.markDuplicates, this); |
6a488035 TO |
348 | model.off('change:phone_type_id', this.markDuplicates, this); |
349 | this.markDuplicates(); | |
350 | }, | |
351 | hasDuplicates: function() { | |
352 | var firstDupe = this.find(function(ufFieldModel){ | |
353 | return ufFieldModel.get('is_duplicate'); | |
354 | }); | |
355 | return firstDupe ? true : false; | |
356 | }, | |
357 | /** | |
358 | * | |
359 | */ | |
360 | markDuplicates: function() { | |
361 | var ufFieldModelsByKey = this.groupBy(function(ufFieldModel) { | |
362 | return ufFieldModel.getSignature(); | |
363 | }); | |
364 | this.each(function(ufFieldModel){ | |
365 | var is_duplicate = ufFieldModelsByKey[ufFieldModel.getSignature()].length > 1; | |
366 | if (is_duplicate != ufFieldModel.get('is_duplicate')) { | |
367 | ufFieldModel.set('is_duplicate', is_duplicate); | |
368 | } | |
369 | }); | |
370 | } | |
371 | }); | |
372 | ||
373 | /** | |
374 | * Represents an entity in a customizable form | |
375 | */ | |
376 | CRM.UF.UFEntityModel = CRM.Backbone.Model.extend({ | |
377 | schema: { | |
378 | 'id': { | |
379 | // title: ts(''), | |
380 | type: 'Number' | |
381 | }, | |
382 | 'entity_name': { | |
383 | title: ts('Entity Name'), | |
384 | help: ts('Symbolic name which referenced in the fields'), | |
385 | type: 'Text' | |
386 | }, | |
387 | 'entity_type': { | |
388 | title: ts('Entity Type'), | |
389 | type: 'Select', | |
390 | options: ['IndividualModel', 'ActivityModel'] | |
391 | }, | |
392 | 'entity_sub_type': { | |
393 | // Use '*' to match all subtypes; use an int to match a specific type id; use empty-string to match none | |
394 | title: ts('Sub Type'), | |
395 | type: 'Text' | |
396 | } | |
397 | }, | |
398 | defaults: { | |
399 | entity_sub_type: '*' | |
400 | }, | |
401 | initialize: function() { | |
402 | }, | |
403 | /** | |
404 | * Get a list of all fields that can be used with this entity. | |
405 | * | |
406 | * @return {Object} keys are field names; values are fieldSchemas | |
407 | */ | |
408 | getFieldSchemas: function() { | |
409 | var ufEntityModel = this; | |
410 | var modelClass= this.getModelClass(); | |
411 | ||
412 | if (this.get('entity_sub_type') == '*') { | |
413 | return _.clone(modelClass.prototype.schema); | |
414 | } | |
415 | ||
416 | var result = {}; | |
417 | _.each(modelClass.prototype.schema, function(fieldSchema, fieldName){ | |
418 | var section = modelClass.prototype.sections[fieldSchema.section]; | |
419 | if (ufEntityModel.isSectionEnabled(section)) { | |
420 | result[fieldName] = fieldSchema; | |
421 | } | |
422 | }); | |
423 | return result; | |
424 | }, | |
425 | isSectionEnabled: function(section) { | |
99e239bc | 426 | //CRM-15427 |
427 | return (!section || !section.extends_entity_column_value || _.contains(section.extends_entity_column_value, this.get('entity_sub_type')) || this.get('entity_sub_type') == '*'); | |
6a488035 TO |
428 | }, |
429 | getSections: function() { | |
430 | var ufEntityModel = this; | |
431 | var result = {}; | |
432 | _.each(ufEntityModel.getModelClass().prototype.sections, function(section, sectionKey){ | |
433 | if (ufEntityModel.isSectionEnabled(section)) { | |
434 | result[sectionKey] = section; | |
435 | } | |
436 | }); | |
437 | return result; | |
438 | }, | |
439 | getModelClass: function() { | |
440 | return CRM.Schema[this.get('entity_type')]; | |
441 | } | |
442 | }); | |
443 | ||
444 | /** | |
445 | * Represents a list of entities in a customizable form | |
446 | * | |
447 | * options: | |
448 | * - ufGroupModel: UFGroupModel | |
449 | */ | |
450 | CRM.UF.UFEntityCollection = CRM.Backbone.Collection.extend({ | |
451 | model: CRM.UF.UFEntityModel, | |
452 | byName: {}, | |
453 | initialize: function(models, options) { | |
454 | options = options || {}; | |
455 | this.initializeCopyToChildrenRelation('ufGroupModel', options.ufGroupModel, models); | |
456 | }, | |
457 | /** | |
458 | * | |
459 | * @param name | |
460 | * @return {UFEntityModel} if found; otherwise, null | |
461 | */ | |
462 | getByName: function(name) { | |
463 | // TODO consider indexing | |
464 | return this.find(function(ufEntityModel){ | |
465 | return ufEntityModel.get('entity_name') == name; | |
466 | }); | |
467 | } | |
468 | }); | |
469 | ||
470 | /** | |
471 | * Represents a customizable form | |
472 | */ | |
473 | CRM.UF.UFGroupModel = CRM.Backbone.Model.extend({ | |
474 | defaults: { | |
d13aab87 | 475 | title: ts('Unnamed Profile'), |
6a488035 TO |
476 | is_active: 1 |
477 | }, | |
478 | schema: { | |
479 | 'id': { | |
480 | // title: ts(''), | |
481 | type: 'Number' | |
482 | }, | |
483 | 'name': { | |
484 | // title: ts(''), | |
485 | type: 'Text' | |
486 | }, | |
487 | 'title': { | |
488 | title: ts('Profile Name'), | |
489 | help: ts(''), | |
490 | type: 'Text', | |
3a3663a4 | 491 | editorAttrs: {maxlength: 64}, |
6a488035 TO |
492 | validators: ['required'] |
493 | }, | |
494 | 'group_type': { | |
495 | // For a description of group_type, see CRM_Core_BAO_UFGroup::updateGroupTypes | |
496 | // title: ts(''), | |
497 | type: 'Text' | |
498 | }, | |
499 | 'add_captcha': { | |
500 | title: ts('Include reCAPTCHA?'), | |
501 | help: ts('FIXME'), | |
502 | type: 'Select', | |
503 | options: YESNO | |
504 | }, | |
505 | 'add_to_group_id': { | |
506 | title: ts('Add new contacts to a Group?'), | |
507 | 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.'), | |
508 | type: 'Number' | |
509 | }, | |
510 | 'cancel_URL': { | |
511 | title: ts('Cancel Redirect URL'), | |
512 | 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.'), | |
513 | type: 'Text' | |
514 | }, | |
515 | 'created_date': { | |
516 | //title: ts(''), | |
517 | type: 'Text'// FIXME | |
518 | }, | |
519 | 'created_id': { | |
520 | //title: ts(''), | |
521 | type: 'Number' | |
522 | }, | |
523 | 'help_post': { | |
524 | title: ts('Post-form Help'), | |
3e64165a TO |
525 | help: ts('Explanatory text displayed at the end of the form.') + |
526 | ts('Note that this help text is displayed on profile create/edit screens only.'), | |
6a488035 TO |
527 | type: 'TextArea' |
528 | }, | |
529 | 'help_pre': { | |
47775bc9 | 530 | title: ts('Pre-form Help'), |
3e64165a TO |
531 | help: ts('Explanatory text displayed at the beginning of the form.') + |
532 | ts('Note that this help text is displayed on profile create/edit screens only.'), | |
6a488035 TO |
533 | type: 'TextArea' |
534 | }, | |
535 | 'is_active': { | |
536 | title: ts('Is this CiviCRM Profile active?'), | |
537 | type: 'Select', | |
538 | options: YESNO | |
539 | }, | |
540 | 'is_cms_user': { | |
541 | title: ts('Drupal user account registration option?'),// FIXME | |
542 | help: ts('FIXME'), | |
543 | type: 'Select', | |
544 | options: YESNO // FIXME | |
545 | }, | |
546 | 'is_edit_link': { | |
547 | title: ts('Include profile edit links in search results?'), | |
548 | 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.'), | |
549 | type: 'Select', | |
550 | options: YESNO | |
551 | }, | |
552 | 'is_map': { | |
553 | title: ts('Enable mapping for this profile?'), | |
554 | 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.'), | |
555 | type: 'Select', | |
556 | options: YESNO | |
557 | }, | |
558 | 'is_proximity_search': { | |
8e3d52a4 | 559 | title: ts('Proximity Search'), |
6a488035 TO |
560 | help: ts('FIXME'), |
561 | type: 'Select', | |
562 | options: YESNO // FIXME | |
563 | }, | |
564 | 'is_reserved': { | |
565 | // title: ts(''), | |
566 | type: 'Select', | |
567 | options: YESNO | |
568 | }, | |
569 | 'is_uf_link': { | |
570 | title: ts('Include Drupal user account information links in search results?'), // FIXME | |
571 | help: ts('FIXME'), | |
572 | type: 'Select', | |
573 | options: YESNO | |
574 | }, | |
575 | 'is_update_dupe': { | |
576 | title: ts('What to do upon duplicate match'), | |
577 | help: ts('FIXME'), | |
578 | type: 'Select', | |
579 | options: YESNO // FIXME | |
580 | }, | |
581 | 'limit_listings_group_id': { | |
582 | title: ts('Limit listings to a specific Group?'), | |
583 | 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.'), | |
584 | type: 'Number' | |
585 | }, | |
586 | 'notify': { | |
587 | title: ts('Notify when profile form is submitted?'), | |
588 | 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.'), | |
589 | type: 'TextArea' | |
590 | }, | |
591 | 'post_URL': { | |
592 | title: ts('Redirect URL'), | |
593 | 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.'"), | |
594 | type: 'Text' | |
595 | }, | |
596 | 'weight': { | |
597 | title: ts('Order'), | |
598 | 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.'), | |
599 | type: 'Number' | |
600 | // FIXME positive int | |
601 | } | |
602 | }, | |
603 | initialize: function() { | |
604 | var ufGroupModel = this; | |
605 | ||
606 | if (!this.getRel('ufEntityCollection')) { | |
607 | var ufEntityCollection = new CRM.UF.UFEntityCollection([], { | |
608 | ufGroupModel: this, | |
609 | silent: false | |
610 | }); | |
611 | this.setRel('ufEntityCollection', ufEntityCollection); | |
612 | } | |
613 | ||
614 | if (!this.getRel('ufFieldCollection')) { | |
615 | var ufFieldCollection = new CRM.UF.UFFieldCollection([], { | |
616 | uf_group_id: this.id, | |
617 | ufGroupModel: this | |
618 | }); | |
619 | this.setRel('ufFieldCollection', ufFieldCollection); | |
620 | } | |
621 | ||
622 | if (!this.getRel('paletteFieldCollection')) { | |
623 | var paletteFieldCollection = new CRM.Designer.PaletteFieldCollection([], { | |
624 | ufGroupModel: this | |
625 | }); | |
626 | paletteFieldCollection.sync = function(method, model, options) { | |
3e64165a | 627 | if (!options) options = {}; |
6a488035 TO |
628 | // console.log(method, model, options); |
629 | switch (method) { | |
630 | case 'read': | |
631 | var success = options.success; | |
632 | options.success = function(resp, status, xhr) { | |
633 | if (success) success(resp, status, xhr); | |
634 | model.trigger('sync', model, resp, options); | |
635 | }; | |
636 | success(ufGroupModel.buildPaletteFields()); | |
637 | ||
638 | break; | |
639 | case 'create': | |
640 | case 'update': | |
641 | case 'delete': | |
fb079bbf TO |
642 | throw 'Unsupported method: ' + method; |
643 | ||
6a488035 TO |
644 | default: |
645 | throw 'Unsupported method: ' + method; | |
646 | } | |
647 | }; | |
648 | this.setRel('paletteFieldCollection', paletteFieldCollection); | |
649 | } | |
650 | ||
651 | this.getRel('ufEntityCollection').on('reset', this.resetEntities, this); | |
652 | this.resetEntities(); | |
653 | ||
654 | this.on('change', watchChanges); | |
655 | }, | |
656 | /** | |
657 | * Generate a copy of this UFGroupModel and its fields, with all ID's removed. The result | |
658 | * is suitable for a new, identical UFGroup. | |
659 | * | |
660 | * @return {CRM.UF.UFGroupModel} | |
661 | */ | |
662 | deepCopy: function() { | |
663 | var copy = new CRM.UF.UFGroupModel(_.omit(this.toStrictJSON(), ['id','created_id','created_date','is_reserved','group_type'])); | |
664 | copy.getRel('ufEntityCollection').reset( | |
665 | this.getRel('ufEntityCollection').toJSON() | |
666 | // FIXME: for configurable entities, omit ['id', 'uf_group_id'] | |
667 | ); | |
668 | copy.getRel('ufFieldCollection').reset( | |
669 | this.getRel('ufFieldCollection').map(function(ufFieldModel) { | |
670 | return _.omit(ufFieldModel.toStrictJSON(), ['id', 'uf_group_id']); | |
671 | }) | |
672 | ); | |
3a3663a4 CW |
673 | var copyLabel = ' ' + ts('(Copy)'); |
674 | copy.set('title', copy.get('title').slice(0, 64 - copyLabel.length) + copyLabel); | |
6a488035 TO |
675 | return copy; |
676 | }, | |
677 | getModelClass: function(entity_name) { | |
678 | var ufEntity = this.getRel('ufEntityCollection').getByName(entity_name); | |
679 | if (!ufEntity) throw 'Failed to locate entity: ' + entity_name; | |
680 | return ufEntity.getModelClass(); | |
681 | }, | |
682 | getFieldSchema: function(entity_name, field_name) { | |
0103eaf0 CW |
683 | if (field_name.indexOf('formatting') === 0) { |
684 | field_name = 'formatting'; | |
685 | } | |
6a488035 TO |
686 | var modelClass = this.getModelClass(entity_name); |
687 | var fieldSchema = modelClass.prototype.schema[field_name]; | |
688 | if (!fieldSchema) { | |
0103eaf0 | 689 | CRM.console('warn', 'Failed to locate field: ' + entity_name + "." + field_name); |
6a488035 TO |
690 | return null; |
691 | } | |
692 | return fieldSchema; | |
693 | }, | |
694 | /** | |
695 | * Check that the group_type contains *only* the types listed in validTypes | |
696 | * | |
697 | * @param string validTypesExpr | |
99e239bc | 698 | * @param bool allowAllSubtypes |
6a488035 TO |
699 | * @return {Boolean} |
700 | */ | |
99e239bc | 701 | //CRM-15427 |
37375016 | 702 | checkGroupType: function(validTypesExpr, allowAllSubtypes, usedByFilter) { |
6a488035 | 703 | var allMatched = true; |
f80ef0e2 | 704 | allowAllSubtypes = allowAllSubtypes || false; |
37375016 | 705 | usedByFilter = usedByFilter || null; |
fb079bbf | 706 | if (_.isEmpty(this.get('group_type'))) { |
6a488035 TO |
707 | return true; |
708 | } | |
37375016 | 709 | if (usedByFilter && _.isEmpty(this.get('module'))) { |
710 | return false; | |
711 | } | |
6a488035 TO |
712 | |
713 | var actualTypes = CRM.UF.parseTypeList(this.get('group_type')); | |
714 | var validTypes = CRM.UF.parseTypeList(validTypesExpr); | |
715 | ||
716 | // Every actual.coreType is a valid.coreType | |
717 | _.each(actualTypes.coreTypes, function(ignore, actualCoreType) { | |
718 | if (! validTypes.coreTypes[actualCoreType]) { | |
719 | allMatched = false; | |
720 | } | |
721 | }); | |
722 | ||
58770077 | 723 | // CRM-16915 - filter with usedBy module if specified. |
37375016 | 724 | if (usedByFilter && this.get('module') != usedByFilter) { |
725 | allMatched = false; | |
726 | } | |
99e239bc | 727 | //CRM-15427 allow all subtypes |
728 | if (!$.isEmptyObject(validTypes.subTypes) && !allowAllSubtypes) { | |
729 | // Every actual.subType is a valid.subType | |
730 | _.each(actualTypes.subTypes, function(actualSubTypeIds, actualSubTypeKey) { | |
731 | if (!validTypes.subTypes[actualSubTypeKey]) { | |
732 | allMatched = false; | |
733 | return; | |
6a488035 | 734 | } |
99e239bc | 735 | // actualSubTypeIds is a list of all subtypes which can be used by group, |
736 | // so it's sufficient to match any one of them | |
737 | var subTypeMatched = false; | |
738 | _.each(actualSubTypeIds, function(ignore, actualSubTypeId) { | |
739 | if (validTypes.subTypes[actualSubTypeKey][actualSubTypeId]) { | |
740 | subTypeMatched = true; | |
741 | } | |
742 | }); | |
743 | allMatched = allMatched && subTypeMatched; | |
6a488035 | 744 | }); |
99e239bc | 745 | } |
6a488035 TO |
746 | return allMatched; |
747 | }, | |
d35c2913 | 748 | calculateContactEntityType: function() { |
06575e57 | 749 | var ufGroupModel = this; |
750 | ||
751 | // set proper entity model based on selected profile | |
752 | var contactTypes = ['Individual', 'Household', 'Organization']; | |
45609fb1 | 753 | var profileType = ufGroupModel.get('group_type') || ''; |
752ddf35 N |
754 | |
755 | // check if selected profile have subtype defined eg: ["Individual,Contact,Case", "caseType:7"] | |
756 | if (_.isArray(profileType) && profileType[0]) { | |
757 | profileType = profileType[0]; | |
758 | } | |
06575e57 | 759 | profileType = profileType.split(','); |
cf1182e6 | 760 | |
06575e57 | 761 | var ufEntityModel; |
762 | _.each(profileType, function (ptype) { | |
763 | if ($.inArray(ptype, contactTypes) > -1) { | |
f34dad55 | 764 | ufEntityModel = ptype + 'Model'; |
06575e57 | 765 | return true; |
766 | } | |
767 | }); | |
768 | ||
769 | return ufEntityModel; | |
770 | }, | |
f34dad55 | 771 | setUFGroupModel: function(entityType, allEntityModels) { |
772 | var ufGroupModel = this; | |
773 | ||
774 | var newUfEntityModels = []; | |
775 | _.each(allEntityModels, function (values) { | |
752ddf35 | 776 | if (entityType && values.entity_name == 'contact_1') { |
f34dad55 | 777 | values.entity_type = entityType; |
778 | } | |
779 | newUfEntityModels.push(new CRM.UF.UFEntityModel(values)); | |
780 | }); | |
781 | ||
782 | ufGroupModel.getRel('ufEntityCollection').reset(newUfEntityModels); | |
783 | }, | |
6a488035 TO |
784 | resetEntities: function() { |
785 | var ufGroupModel = this; | |
41fd8e26 | 786 | var deleteFieldList = []; |
6a488035 TO |
787 | ufGroupModel.getRel('ufFieldCollection').each(function(ufFieldModel){ |
788 | if (!ufFieldModel.getFieldSchema()) { | |
41fd8e26 | 789 | CRM.alert(ts('This profile no longer includes field "%1"! All references to the field have been removed.', { |
790 | 1: ufFieldModel.get('label') | |
6a488035 | 791 | }), '', 'alert', {expires: false}); |
41fd8e26 | 792 | deleteFieldList.push(ufFieldModel); |
6a488035 TO |
793 | } |
794 | }); | |
41fd8e26 | 795 | |
796 | _.each(deleteFieldList, function(ufFieldModel) { | |
797 | ufFieldModel.destroyLocal(); | |
798 | }); | |
799 | ||
6a488035 | 800 | this.getRel('paletteFieldCollection').reset(this.buildPaletteFields()); |
41fd8e26 | 801 | |
802 | // reset to redraw the cancel after entity type is updated. | |
803 | ufGroupModel.getRel('ufFieldCollection').reset(ufGroupModel.getRel('ufFieldCollection').toJSON()); | |
6a488035 TO |
804 | }, |
805 | /** | |
806 | * | |
807 | * @return {Array} of PaletteFieldModel | |
808 | */ | |
809 | buildPaletteFields: function() { | |
810 | // rebuild list of fields; reuse old instances of PaletteFieldModel and create new ones | |
811 | // as appropriate | |
812 | // Note: The system as a whole is ill-defined in cases where we have an existing | |
813 | // UFField that references a model field that disappears. | |
814 | ||
815 | var ufGroupModel = this; | |
816 | ||
817 | var oldPaletteFieldModelsBySig = {}; | |
818 | this.getRel('paletteFieldCollection').each(function(paletteFieldModel){ | |
819 | oldPaletteFieldModelsBySig[paletteFieldModel.get("entityName") + '::' + paletteFieldModel.get("fieldName")] = paletteFieldModel; | |
820 | }); | |
821 | ||
822 | var newPaletteFieldModels = []; | |
823 | this.getRel('ufEntityCollection').each(function(ufEntityModel){ | |
824 | var modelClass = ufEntityModel.getModelClass(); | |
825 | _.each(ufEntityModel.getFieldSchemas(), function(value, key, list) { | |
826 | var model = oldPaletteFieldModelsBySig[ufEntityModel.get('entity_name') + '::' + key]; | |
827 | if (!model) { | |
828 | model = new CRM.Designer.PaletteFieldModel({ | |
829 | modelClass: modelClass, | |
830 | entityName: ufEntityModel.get('entity_name'), | |
831 | fieldName: key | |
832 | }); | |
833 | } | |
834 | newPaletteFieldModels.push(model); | |
835 | }); | |
836 | }); | |
837 | ||
838 | return newPaletteFieldModels; | |
839 | } | |
840 | }); | |
841 | ||
842 | /** | |
843 | * Represents a list of customizable form | |
844 | */ | |
845 | CRM.UF.UFGroupCollection = CRM.Backbone.Collection.extend({ | |
846 | model: CRM.UF.UFGroupModel | |
847 | }); | |
4b513f23 | 848 | })(CRM.$, CRM._); |