2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var mongoose = require('mongoose'),
|
|
|
|
Schema = mongoose.Schema,
|
2015-11-01 00:32:37 +00:00
|
|
|
pdfFiller = require('pdffiller'),
|
2015-06-29 22:51:29 +00:00
|
|
|
_ = require('lodash'),
|
|
|
|
config = require('../../config/config'),
|
|
|
|
path = require('path'),
|
2015-10-06 20:14:38 +00:00
|
|
|
mUtilities = require('mongoose-utilities'),
|
2015-06-30 14:21:53 +00:00
|
|
|
fs = require('fs-extra'),
|
2015-07-27 18:11:43 +00:00
|
|
|
async = require('async'),
|
2016-04-29 02:48:02 +00:00
|
|
|
mkdirp = require('mkdirp'),
|
|
|
|
Random = require('random-js'),
|
|
|
|
mt = Random.engines.mt19937(),
|
2015-08-04 21:06:16 +00:00
|
|
|
util = require('util');
|
|
|
|
|
2016-04-29 02:48:02 +00:00
|
|
|
mt.autoSeed();
|
|
|
|
|
2015-08-04 21:06:16 +00:00
|
|
|
//Mongoose Models
|
2015-09-18 16:32:17 +00:00
|
|
|
var FieldSchema = require('./form_field.server.model.js');
|
|
|
|
var Field = mongoose.model('Field');
|
|
|
|
|
2015-09-10 22:06:28 +00:00
|
|
|
var FormSubmissionSchema = require('./form_submission.server.model.js'),
|
|
|
|
FormSubmission = mongoose.model('FormSubmission', FormSubmissionSchema);
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-09-18 16:32:17 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
var ButtonSchema = new Schema({
|
|
|
|
url: {
|
|
|
|
type: String,
|
|
|
|
match: [/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/],
|
|
|
|
},
|
|
|
|
action: String,
|
|
|
|
text: String,
|
|
|
|
bgColor: {
|
|
|
|
type: String,
|
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#5bc0de'
|
|
|
|
},
|
|
|
|
color: {
|
|
|
|
type: String,
|
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#ffffff'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
var VisitorDataSchema = new Schema({
|
|
|
|
referrer: {
|
2016-06-17 21:51:17 +00:00
|
|
|
type: String
|
2016-06-07 00:37:09 +00:00
|
|
|
},
|
|
|
|
lastActiveField: {
|
|
|
|
type: Schema.Types.ObjectId
|
|
|
|
},
|
|
|
|
timeElapsed: {
|
2016-06-17 21:33:33 +00:00
|
|
|
type: Number,
|
|
|
|
required: true
|
2016-06-07 00:37:09 +00:00
|
|
|
},
|
|
|
|
isSubmitted: {
|
2016-06-17 21:33:33 +00:00
|
|
|
type: Boolean,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
language: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
ipAddr: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
deviceType: {
|
|
|
|
type: String,
|
|
|
|
enum: ['desktop', 'phone', 'tablet', 'other'],
|
2016-06-17 21:51:17 +00:00
|
|
|
default: 'other'
|
2016-06-17 21:33:33 +00:00
|
|
|
},
|
|
|
|
userAgent: {
|
|
|
|
type: String
|
2016-06-07 00:37:09 +00:00
|
|
|
}
|
2016-06-17 21:33:33 +00:00
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
});
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
/**
|
|
|
|
* Form Schema
|
|
|
|
*/
|
2015-09-10 22:06:28 +00:00
|
|
|
var FormSchema = new Schema({
|
2015-06-29 22:51:29 +00:00
|
|
|
title: {
|
|
|
|
type: String,
|
|
|
|
trim: true,
|
2016-04-29 02:48:02 +00:00
|
|
|
required: 'Form Title cannot be blank'
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
2015-07-07 01:21:43 +00:00
|
|
|
language: {
|
|
|
|
type: String,
|
2016-06-16 00:38:22 +00:00
|
|
|
enum: ['en', 'fr', 'es', 'it', 'de'],
|
|
|
|
default: 'en',
|
|
|
|
required: 'Form must have a language'
|
2015-07-07 01:21:43 +00:00
|
|
|
},
|
2015-06-29 22:51:29 +00:00
|
|
|
description: {
|
|
|
|
type: String,
|
2016-03-30 03:45:16 +00:00
|
|
|
default: ''
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
analytics:{
|
|
|
|
gaCode: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
visitors: [VisitorDataSchema]
|
|
|
|
},
|
|
|
|
|
2016-06-01 01:06:45 +00:00
|
|
|
form_fields: [FieldSchema],
|
2015-06-30 02:14:43 +00:00
|
|
|
submissions: [{
|
2015-06-29 22:51:29 +00:00
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
ref: 'FormSubmission'
|
|
|
|
}],
|
|
|
|
|
|
|
|
admin: {
|
|
|
|
type: Schema.Types.ObjectId,
|
2015-07-21 23:25:45 +00:00
|
|
|
ref: 'User',
|
|
|
|
required: 'Form must have an Admin'
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pdf: {
|
|
|
|
type: Schema.Types.Mixed
|
|
|
|
},
|
|
|
|
pdfFieldMap: {
|
|
|
|
type: Schema.Types.Mixed
|
|
|
|
},
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-08-07 21:02:44 +00:00
|
|
|
startPage: {
|
|
|
|
showStart:{
|
|
|
|
type: Boolean,
|
2016-04-29 02:48:02 +00:00
|
|
|
default: false
|
2015-08-07 21:02:44 +00:00
|
|
|
},
|
2015-08-18 21:44:36 +00:00
|
|
|
introTitle:{
|
2015-08-07 21:02:44 +00:00
|
|
|
type: String,
|
2015-08-18 21:44:36 +00:00
|
|
|
default: 'Welcome to Form'
|
2015-08-07 21:02:44 +00:00
|
|
|
},
|
2015-08-18 21:44:36 +00:00
|
|
|
introParagraph:{
|
2016-03-30 03:45:16 +00:00
|
|
|
type: String
|
2015-08-18 21:44:36 +00:00
|
|
|
},
|
2016-04-21 21:44:15 +00:00
|
|
|
introButtonText:{
|
|
|
|
type: String,
|
|
|
|
default: 'Start'
|
|
|
|
},
|
2015-08-18 21:44:36 +00:00
|
|
|
buttons:[ButtonSchema]
|
2015-08-06 05:52:59 +00:00
|
|
|
},
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-06-30 11:05:44 +00:00
|
|
|
hideFooter: {
|
|
|
|
type: Boolean,
|
2016-03-30 03:45:16 +00:00
|
|
|
default: false
|
2015-06-30 11:05:44 +00:00
|
|
|
},
|
2015-06-29 22:51:29 +00:00
|
|
|
isGenerated: {
|
|
|
|
type: Boolean,
|
2016-03-30 03:45:16 +00:00
|
|
|
default: false
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
2015-07-02 02:49:35 +00:00
|
|
|
isLive: {
|
|
|
|
type: Boolean,
|
2016-03-30 03:45:16 +00:00
|
|
|
default: false
|
2015-07-02 02:49:35 +00:00
|
|
|
},
|
2015-06-29 22:51:29 +00:00
|
|
|
autofillPDFs: {
|
|
|
|
type: Boolean,
|
2016-03-30 03:45:16 +00:00
|
|
|
default: false
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
2015-08-07 21:02:44 +00:00
|
|
|
|
2015-07-30 00:22:53 +00:00
|
|
|
design: {
|
|
|
|
colors:{
|
2016-03-30 03:45:16 +00:00
|
|
|
backgroundColor: {
|
2015-08-07 22:30:41 +00:00
|
|
|
type: String,
|
2015-10-30 21:40:22 +00:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#fff'
|
2015-08-07 22:30:41 +00:00
|
|
|
},
|
2016-03-30 03:45:16 +00:00
|
|
|
questionColor: {
|
2015-08-07 22:30:41 +00:00
|
|
|
type: String,
|
2015-10-30 21:40:22 +00:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
2016-04-21 23:48:59 +00:00
|
|
|
default: '#333'
|
2015-08-07 22:30:41 +00:00
|
|
|
},
|
2016-03-30 03:45:16 +00:00
|
|
|
answerColor: {
|
2015-08-07 22:30:41 +00:00
|
|
|
type: String,
|
2015-10-30 21:40:22 +00:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
2016-04-21 23:48:59 +00:00
|
|
|
default: '#333'
|
2015-08-07 22:30:41 +00:00
|
|
|
},
|
2016-03-30 03:45:16 +00:00
|
|
|
buttonColor: {
|
2015-08-07 22:30:41 +00:00
|
|
|
type: String,
|
2016-04-21 23:48:59 +00:00
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#fff'
|
2016-04-21 22:25:22 +00:00
|
|
|
},
|
|
|
|
buttonTextColor: {
|
|
|
|
type: String,
|
|
|
|
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
|
|
|
|
default: '#333'
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
2015-07-30 00:22:53 +00:00
|
|
|
},
|
|
|
|
font: String,
|
|
|
|
backgroundImage: { type: Schema.Types.Mixed }
|
2015-09-10 22:06:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
plugins: {
|
|
|
|
oscarhost: {
|
|
|
|
baseUrl: {
|
2016-04-29 02:48:02 +00:00
|
|
|
type: String
|
2015-09-10 22:06:28 +00:00
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
lookupField: {
|
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
ref: 'Field'
|
|
|
|
},
|
|
|
|
updateType: {
|
|
|
|
type: String,
|
|
|
|
enum: ['upsert', 'force_add', 'force_update', 'fetch'],
|
|
|
|
},
|
|
|
|
fieldMap: {
|
|
|
|
type: Schema.Types.Mixed,
|
2015-10-30 18:40:02 +00:00
|
|
|
},
|
|
|
|
validUpdateTypes: {
|
|
|
|
type: [String]
|
|
|
|
},
|
|
|
|
validFields : {
|
|
|
|
type: [String],
|
|
|
|
default: [
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'email',
|
|
|
|
'firstName',
|
|
|
|
'hin',
|
|
|
|
'lastName',
|
|
|
|
'phone',
|
|
|
|
'postal',
|
|
|
|
'province',
|
|
|
|
'sex',
|
|
|
|
'spokenLanguage',
|
|
|
|
'title',
|
|
|
|
'DOB']
|
2015-09-10 22:06:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
user: {
|
2016-06-07 00:37:09 +00:00
|
|
|
type: String
|
2015-09-10 22:06:28 +00:00
|
|
|
},
|
|
|
|
pass: {
|
2016-06-07 00:37:09 +00:00
|
|
|
type: String
|
2015-09-10 22:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 22:29:07 +00:00
|
|
|
}
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
/*
|
|
|
|
** In-Form Analytics Virtual Attributes
|
|
|
|
*/
|
|
|
|
FormSchema.virtual('analytics.views').get(function () {
|
|
|
|
return this.analytics.visitors.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
FormSchema.virtual('analytics.submissions').get(function () {
|
|
|
|
return this.submissions.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
FormSchema.virtual('analytics.conversionRate').get(function () {
|
|
|
|
return this.submissions.length/this.analytics.visitors.length*100;
|
|
|
|
});
|
|
|
|
|
|
|
|
FormSchema.virtual('analytics.fields').get(function () {
|
|
|
|
var fieldDropoffs = [];
|
|
|
|
var visitors = this.analytics.visitors;
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
for(var i=0; i<this.form_fields.length; i++){
|
|
|
|
var field = this.form_fields[i];
|
|
|
|
|
|
|
|
if(!field.deletePreserved){
|
|
|
|
|
|
|
|
var dropoffViews = _.reduce(visitors, function(sum, visitorObj){
|
|
|
|
|
|
|
|
if(visitorObj.lastActiveField+'' === field._id+'' && !visitorObj.isSubmitted){
|
|
|
|
return sum + 1;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
var continueViews, nextIndex;
|
|
|
|
|
|
|
|
if(i !== this.form_fields.length-1){
|
|
|
|
continueViews = _.reduce(visitors, function(sum, visitorObj){
|
|
|
|
nextIndex = that.form_fields.indexOf(_.find(that.form_fields, function(o) {
|
|
|
|
return o._id+'' === visitorObj.lastActiveField+'';
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(nextIndex > i){
|
|
|
|
return sum + 1;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}, 0);
|
|
|
|
}else {
|
|
|
|
continueViews = _.reduce(visitors, function(sum, visitorObj){
|
|
|
|
if(visitorObj.lastActiveField+'' === field._id+'' && visitorObj.isSubmitted){
|
|
|
|
return sum + 1;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalViews = dropoffViews+continueViews;
|
2016-06-17 22:11:02 +00:00
|
|
|
var responses = continueViews;
|
2016-06-07 00:37:09 +00:00
|
|
|
var continueRate = continueViews/totalViews*100;
|
|
|
|
var dropoffRate = dropoffViews/totalViews*100;
|
|
|
|
|
|
|
|
fieldDropoffs[i] = {
|
|
|
|
dropoffViews: dropoffViews,
|
2016-06-17 22:11:02 +00:00
|
|
|
responses: continueViews,
|
2016-06-07 00:37:09 +00:00
|
|
|
totalViews: totalViews,
|
|
|
|
continueRate: continueRate,
|
|
|
|
dropoffRate: dropoffRate,
|
|
|
|
field: field
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldDropoffs;
|
|
|
|
});
|
|
|
|
|
2015-10-06 20:14:38 +00:00
|
|
|
FormSchema.plugin(mUtilities.timestamp, {
|
|
|
|
createdPath: 'created',
|
|
|
|
modifiedPath: 'lastModified',
|
|
|
|
useVirtual: false
|
|
|
|
});
|
2015-12-12 20:08:48 +00:00
|
|
|
|
2015-07-03 00:49:23 +00:00
|
|
|
//Delete template PDF of current Form
|
2015-09-10 22:06:28 +00:00
|
|
|
FormSchema.pre('remove', function (next) {
|
2015-07-30 00:32:46 +00:00
|
|
|
if(this.pdf && process.env.NODE_ENV === 'development'){
|
2015-07-03 00:49:23 +00:00
|
|
|
//Delete template form
|
|
|
|
fs.unlink(this.pdf.path, function(err){
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('successfully deleted', this.pdf.path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-08-14 17:24:14 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
var _original;
|
2015-07-03 00:49:23 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
function getDeletedIndexes(needle, haystack){
|
|
|
|
var deletedIndexes = [];
|
|
|
|
|
|
|
|
if(haystack.length > 0){
|
|
|
|
for(var i = 0; i < needle.length; i++){
|
2015-07-27 18:11:43 +00:00
|
|
|
if(haystack.indexOf(needle[i]) === -1){
|
2015-07-21 23:25:45 +00:00
|
|
|
deletedIndexes.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deletedIndexes;
|
|
|
|
}
|
|
|
|
|
2015-07-02 03:50:57 +00:00
|
|
|
//Move PDF to permanent location after new template is uploaded
|
2015-09-10 22:06:28 +00:00
|
|
|
FormSchema.pre('save', function (next) {
|
2016-04-29 02:48:02 +00:00
|
|
|
var that = this;
|
|
|
|
|
2016-04-29 06:22:47 +00:00
|
|
|
async.series([function(cb) {
|
2016-04-29 02:48:02 +00:00
|
|
|
that.constructor
|
|
|
|
.findOne({_id: that._id}).exec(function (err, original) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb(err);
|
2016-04-29 02:48:02 +00:00
|
|
|
} else {
|
|
|
|
_original = original;
|
|
|
|
//console.log('_original');
|
|
|
|
//console.log(_original);
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb(null);
|
2015-07-03 00:49:23 +00:00
|
|
|
}
|
2015-07-02 02:49:35 +00:00
|
|
|
});
|
2016-04-29 06:22:47 +00:00
|
|
|
}, function(cb) {
|
2016-04-29 02:48:02 +00:00
|
|
|
//DAVID: TODO: Make this so we don't have to update the validFields property ever save
|
|
|
|
if (that.plugins.oscarhost.hasOwnProperty('baseUrl')) {
|
|
|
|
var validUpdateTypes = mongoose.model('Form').schema.path('plugins.oscarhost.settings.updateType').enumValues;
|
|
|
|
that.plugins.oscarhost.settings.validUpdateTypes = validUpdateTypes;
|
2015-11-23 21:06:02 +00:00
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb(null);
|
2016-04-29 02:48:02 +00:00
|
|
|
},
|
2016-04-29 06:22:47 +00:00
|
|
|
function(cb) {
|
2016-04-29 02:48:02 +00:00
|
|
|
if (that.pdf) {
|
|
|
|
async.series([
|
2016-04-29 06:22:47 +00:00
|
|
|
function (callback) {
|
2016-04-29 02:48:02 +00:00
|
|
|
if (that.isModified('pdf') && that.pdf.path) {
|
2015-07-27 18:11:43 +00:00
|
|
|
|
2016-04-29 02:48:02 +00:00
|
|
|
var new_filename = that.title.replace(/ /g, '') + '_template.pdf';
|
2016-03-30 03:45:16 +00:00
|
|
|
|
2016-04-29 02:48:02 +00:00
|
|
|
var newDestination = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g, ''), that.title.replace(/ /g, '')),
|
|
|
|
stat = null;
|
2015-07-27 18:11:43 +00:00
|
|
|
|
2016-04-29 02:48:02 +00:00
|
|
|
try {
|
|
|
|
stat = fs.statSync(newDestination);
|
|
|
|
} catch (err) {
|
|
|
|
mkdirp.sync(newDestination);
|
2015-07-27 18:11:43 +00:00
|
|
|
}
|
2016-04-29 02:48:02 +00:00
|
|
|
if (stat && !stat.isDirectory()) {
|
2016-04-29 06:22:47 +00:00
|
|
|
return callback(new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"'), null);
|
2015-10-06 20:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 02:48:02 +00:00
|
|
|
var old_path = that.pdf.path;
|
|
|
|
fs.move(old_path, path.join(newDestination, new_filename), {clobber: true}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(new Error(err.message), 'task1');
|
2016-04-29 02:48:02 +00:00
|
|
|
} else {
|
|
|
|
that.pdf.path = path.join(newDestination, new_filename);
|
|
|
|
that.pdf.name = new_filename;
|
|
|
|
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(null, 'task1');
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(null, 'task1');
|
2015-07-27 18:11:43 +00:00
|
|
|
}
|
2016-04-29 02:48:02 +00:00
|
|
|
},
|
2016-04-29 06:22:47 +00:00
|
|
|
function (callback) {
|
2016-04-29 02:48:02 +00:00
|
|
|
if (that.isGenerated) {
|
|
|
|
that.pdf.path = config.pdfUploadPath + that.admin.username.replace(/ /g, '') + '/' + that.title.replace(/ /g, '') + '/' + that.title.replace(/ /g, '') + '_template.pdf';
|
|
|
|
that.pdf.name = that.title.replace(/ /g, '') + '_template.pdf';
|
|
|
|
var _typeConvMap = {
|
|
|
|
'Multiline': 'textarea',
|
|
|
|
'Text': 'textfield',
|
|
|
|
'Button': 'checkbox',
|
|
|
|
'Choice': 'radio',
|
|
|
|
'Password': 'password',
|
|
|
|
'FileSelect': 'filefield',
|
|
|
|
'Radio': 'radio'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pdfFiller.generateFieldJson(that.pdf.path, '', function (err, _form_fields) {
|
|
|
|
|
|
|
|
//console.log(that.pdf.path);
|
|
|
|
|
|
|
|
if (err) {
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(new Error(err.message), null);
|
2016-04-29 02:48:02 +00:00
|
|
|
} else if (!_form_fields.length || _form_fields === undefined || _form_fields === null) {
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(new Error('Generated formfields is empty'), null);
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('autogenerating form');
|
|
|
|
|
|
|
|
//Map PDF field names to FormField field names
|
|
|
|
for (var i = 0; i < _form_fields.length; i++) {
|
|
|
|
var _field = _form_fields[i];
|
|
|
|
|
|
|
|
//Convert types from FDF to 'FormField' types
|
|
|
|
if (_typeConvMap[_field.fieldType + '']) {
|
|
|
|
_field.fieldType = _typeConvMap[_field.fieldType + ''];
|
|
|
|
}
|
|
|
|
|
|
|
|
var new_field = {};
|
|
|
|
new_field.title = _field.fieldType + ' ' + Math.abs(mt());
|
|
|
|
new_field.fieldValue = '';
|
|
|
|
new_field.disabled = false;
|
|
|
|
new_field.fieldType = _field.fieldType;
|
|
|
|
new_field.deletePreserved = false;
|
|
|
|
new_field.required = false;
|
|
|
|
_form_fields[i] = new_field;
|
|
|
|
}
|
|
|
|
|
|
|
|
that.form_fields = _form_fields;
|
|
|
|
|
|
|
|
that.isGenerated = false;
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(null, 'task2');
|
2016-04-29 02:48:02 +00:00
|
|
|
});
|
|
|
|
} else {
|
2016-04-29 06:00:41 +00:00
|
|
|
return callback(null, 'task2');
|
2015-07-27 18:11:43 +00:00
|
|
|
}
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
|
|
|
], function (err, results) {
|
|
|
|
if (err) {
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb(new Error({
|
2016-04-29 02:48:02 +00:00
|
|
|
message: err.message
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
//console.log('ending form save1');
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb();
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (_original) {
|
|
|
|
if (_original.hasOwnProperty('pdf')) {
|
|
|
|
fs.remove(_original.pdf.path, function (err) {
|
2016-04-29 06:00:41 +00:00
|
|
|
if (err) return cb(err);
|
2016-04-29 02:48:02 +00:00
|
|
|
console.log('file at ' + _original.pdf.path + ' successfully deleted');
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb();
|
2015-07-27 18:11:43 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
else return cb();
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
else return cb();
|
2016-04-29 02:48:02 +00:00
|
|
|
},
|
2016-04-29 06:22:47 +00:00
|
|
|
function(cb) {
|
2016-04-29 02:48:02 +00:00
|
|
|
|
|
|
|
if(that.isModified('form_fields') && that.form_fields && _original){
|
|
|
|
|
|
|
|
var old_form_fields = _original.form_fields,
|
|
|
|
new_ids = _.map(_.pluck(that.form_fields, '_id'), function(id){ return ''+id;}),
|
|
|
|
old_ids = _.map(_.pluck(old_form_fields, '_id'), function(id){ return ''+id;}),
|
|
|
|
deletedIds = getDeletedIndexes(old_ids, new_ids);
|
|
|
|
|
|
|
|
//Preserve fields that have at least one submission
|
|
|
|
if( deletedIds.length > 0 ){
|
|
|
|
|
|
|
|
var modifiedSubmissions = [];
|
|
|
|
|
|
|
|
async.forEachOfSeries(deletedIds,
|
2016-04-29 06:22:47 +00:00
|
|
|
function (deletedIdIndex, key, cb_id) {
|
2016-04-29 02:48:02 +00:00
|
|
|
|
|
|
|
var deleted_id = old_ids[deletedIdIndex];
|
|
|
|
|
|
|
|
//Find FormSubmissions that contain field with _id equal to 'deleted_id'
|
|
|
|
FormSubmission.
|
2016-06-05 03:13:42 +00:00
|
|
|
find({ form: that._id, admin: that.admin, form_fields: {$elemMatch: {submissionId: deleted_id} } }).
|
2016-04-29 02:48:02 +00:00
|
|
|
exec(function(err, submissions){
|
|
|
|
if(err) {
|
|
|
|
console.error(err);
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb_id(err);
|
2016-04-29 02:48:02 +00:00
|
|
|
} else {
|
|
|
|
//Delete field if there are no submission(s) found
|
|
|
|
if (submissions.length) {
|
|
|
|
//Add submissions
|
|
|
|
modifiedSubmissions.push.apply(modifiedSubmissions, submissions);
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb_id(null);
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if(err){
|
|
|
|
console.error(err.message);
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb(err);
|
2016-04-29 02:48:02 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
//Iterate through all submissions with modified form_fields
|
2016-04-29 06:22:47 +00:00
|
|
|
async.forEachOfSeries(modifiedSubmissions, function (submission, key, callback) {
|
2016-04-29 02:48:02 +00:00
|
|
|
|
|
|
|
//Iterate through ids of deleted fields
|
|
|
|
for (var i = 0; i < deletedIds.length; i++) {
|
|
|
|
|
|
|
|
var index = _.findIndex(submission.form_fields, function (field) {
|
|
|
|
var tmp_id = field._id + '';
|
|
|
|
return tmp_id === old_ids[deletedIds[i]];
|
|
|
|
});
|
|
|
|
|
|
|
|
var deletedField = submission.form_fields[index];
|
|
|
|
|
|
|
|
//Hide field if it exists
|
|
|
|
if (deletedField) {
|
|
|
|
// console.log('deletedField\n-------\n\n');
|
|
|
|
// console.log(deletedField);
|
|
|
|
//Delete old form_field
|
|
|
|
submission.form_fields.splice(index, 1);
|
|
|
|
|
|
|
|
deletedField.deletePreserved = true;
|
|
|
|
|
|
|
|
//Move deleted form_field to start
|
|
|
|
submission.form_fields.unshift(deletedField);
|
|
|
|
that.form_fields.unshift(deletedField);
|
|
|
|
// console.log('form.form_fields\n--------\n\n');
|
|
|
|
// console.log(that.form_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
submission.save(function (err) {
|
2016-04-29 06:00:41 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
else return callback(null);
|
2016-04-29 02:48:02 +00:00
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
2016-04-29 06:00:41 +00:00
|
|
|
return cb(err);
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
else return cb();
|
2016-04-29 02:48:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
else return cb(null);
|
2016-04-29 02:48:02 +00:00
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
else return cb(null);
|
2016-04-29 02:48:02 +00:00
|
|
|
}],
|
|
|
|
function(err, results){
|
2016-04-29 06:00:41 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
return next();
|
2016-04-29 02:48:02 +00:00
|
|
|
});
|
2015-07-27 18:11:43 +00:00
|
|
|
});
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-09-10 22:06:28 +00:00
|
|
|
mongoose.model('Form', FormSchema);
|
2015-06-29 22:51:29 +00:00
|
|
|
|