2015-06-29 15:51:29 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var mongoose = require('mongoose'),
|
|
|
|
FieldSchema = require('./form_field.server.model.js'),
|
|
|
|
Schema = mongoose.Schema,
|
|
|
|
pdfFiller = require('pdfFiller'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
config = require('../../config/config'),
|
|
|
|
path = require('path'),
|
2015-06-30 07:21:53 -07:00
|
|
|
fs = require('fs-extra'),
|
2015-07-02 17:49:23 -07:00
|
|
|
async = require('async'),
|
2015-06-30 07:21:53 -07:00
|
|
|
Field = mongoose.model('Field', FieldSchema);
|
2015-06-29 15:51:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form Schema
|
|
|
|
*/
|
|
|
|
var FormSchema = new Schema({
|
|
|
|
created: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
},
|
2015-07-01 20:50:57 -07:00
|
|
|
lastModified: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
title: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
trim: true,
|
|
|
|
unique: true,
|
|
|
|
required: 'Title cannot be blank'
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
2015-07-03 13:15:13 -07:00
|
|
|
form_fields: {
|
|
|
|
type: [Schema.Types.Mixed],
|
|
|
|
set: function(form_fields) {
|
|
|
|
this._previousFormFields = this.form_fields;
|
|
|
|
return form_fields;
|
|
|
|
}
|
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
|
2015-06-29 19:14:43 -07:00
|
|
|
submissions: [{
|
2015-06-29 15:51:29 -07:00
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
ref: 'FormSubmission'
|
|
|
|
}],
|
|
|
|
|
|
|
|
admin: {
|
|
|
|
type: Schema.Types.ObjectId,
|
|
|
|
ref: 'User'
|
|
|
|
},
|
|
|
|
|
|
|
|
pdf: {
|
|
|
|
type: Schema.Types.Mixed
|
|
|
|
},
|
|
|
|
pdfFieldMap: {
|
|
|
|
type: Schema.Types.Mixed
|
|
|
|
},
|
2015-06-30 04:05:44 -07:00
|
|
|
hideFooter: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
isGenerated: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
2015-07-01 19:49:35 -07:00
|
|
|
isLive: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2015-06-29 15:51:29 -07:00
|
|
|
autofillPDFs: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-07-02 17:49:23 -07:00
|
|
|
//Delete template PDF of current Form
|
|
|
|
FormSchema.pre('remove', function (next) {
|
|
|
|
if(this.pdf){
|
|
|
|
//Delete template form
|
|
|
|
fs.unlink(this.pdf.path, function(err){
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('successfully deleted', this.pdf.path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-02 16:34:00 -07:00
|
|
|
//Create folder for user's pdfs
|
|
|
|
FormSchema.pre('save', function (next) {
|
2015-07-02 17:49:23 -07:00
|
|
|
var newDestination = path.join(config.pdfUploadPath, this.admin.username.replace(/ /g,'')),
|
2015-07-02 16:34:00 -07:00
|
|
|
stat = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
stat = fs.statSync(newDestination);
|
|
|
|
} catch (err) {
|
|
|
|
fs.mkdirSync(newDestination);
|
|
|
|
}
|
|
|
|
if (stat && !stat.isDirectory()) {
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('Directory cannot be created');
|
2015-07-02 16:34:00 -07:00
|
|
|
next( new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"') );
|
|
|
|
}else{
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-02 17:49:23 -07:00
|
|
|
//Update lastModified and created everytime we save
|
2015-07-01 20:50:57 -07:00
|
|
|
FormSchema.pre('save', function (next) {
|
2015-07-02 17:49:23 -07:00
|
|
|
var now = new Date();
|
|
|
|
this.lastModified = now;
|
|
|
|
if( !this.created ){
|
|
|
|
this.created = now;
|
|
|
|
}
|
2015-07-01 20:50:57 -07:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
//Concatenate submission and form's form_fields
|
|
|
|
FormSchema.pre('save', function (next) {
|
|
|
|
if(this.isModified('form_fields')){
|
|
|
|
if(this.submissions.length){
|
|
|
|
for(var i=0; i<this.submissions.length; i++){
|
2015-07-03 16:47:14 -07:00
|
|
|
this.submissions[i].form_fields = submission.form_fields.concat(_.difference(this.form_fields, this._previousFormFields));
|
2015-07-03 13:15:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.form_fields = this._previousFormFields.concat(_.difference(this.form_fields, this._previousFormFields));
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2015-07-01 20:50:57 -07:00
|
|
|
//Move PDF to permanent location after new template is uploaded
|
2015-06-29 15:51:29 -07:00
|
|
|
FormSchema.pre('save', function (next) {
|
|
|
|
|
|
|
|
if(this.pdf){
|
2015-07-02 17:49:23 -07:00
|
|
|
var that = this;
|
|
|
|
async.series([
|
|
|
|
function(callback){
|
|
|
|
if(that.isModified('pdf')){
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('about to move PDF');
|
2015-07-02 17:49:23 -07:00
|
|
|
|
|
|
|
var new_filename = that.title.replace(/ /g,'')+'_template.pdf';
|
|
|
|
|
|
|
|
var newDestination = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,'')),
|
|
|
|
stat = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
stat = fs.statSync(newDestination);
|
|
|
|
} catch (err) {
|
|
|
|
fs.mkdirSync(newDestination);
|
|
|
|
}
|
|
|
|
if (stat && !stat.isDirectory()) {
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('Directory '+newDestination+' cannot be created');
|
2015-07-02 17:49:23 -07:00
|
|
|
callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') );
|
|
|
|
}
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('about to move PDF');
|
2015-07-02 17:49:23 -07:00
|
|
|
|
|
|
|
fs.move(that.pdf.path, path.join(newDestination, new_filename), function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
callback( new Error(err.message) );
|
|
|
|
}
|
|
|
|
that.pdf.path = path.join(newDestination, new_filename);
|
|
|
|
that.pdf.name = new_filename;
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path);
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
callback(null,'task1');
|
2015-07-02 17:49:23 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback(null,null);
|
|
|
|
},
|
|
|
|
function(callback){
|
|
|
|
if(that.isGenerated){
|
|
|
|
that.pdf.path = path.join(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'
|
|
|
|
};
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('autogenerating form');
|
|
|
|
// console.log(that.pdf.path);
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-03 12:25:02 -07:00
|
|
|
pdfFiller.generateFieldJson(that.pdf.path, function(err, _form_fields){
|
|
|
|
if(err){
|
|
|
|
next( new Error(err.message), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//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+'' ];
|
2015-07-02 17:49:23 -07:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:25:02 -07:00
|
|
|
field.fieldValue = '';
|
|
|
|
field.created = Date.now();
|
|
|
|
field.required = true;
|
|
|
|
field.disabled = false;
|
|
|
|
|
|
|
|
// field = new Field(field);
|
|
|
|
// field.save(function(err) {
|
|
|
|
// if (err) {
|
|
|
|
// console.error(err.message);
|
|
|
|
// throw new Error(err.message);
|
|
|
|
// });
|
|
|
|
// } else {
|
|
|
|
// _form_fields[i] = that;
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
_form_fields[i] = field;
|
|
|
|
}
|
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('NEW FORM_FIELDS: ');
|
|
|
|
// console.log(_form_fields);
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
// console.log('\n\nOLD FORM_FIELDS: ');
|
|
|
|
// console.log(that.form_fields);
|
2015-07-02 17:49:23 -07:00
|
|
|
|
2015-07-03 13:15:13 -07:00
|
|
|
that.form_fields.concat(_form_fields);
|
|
|
|
callback(null, 'task2');
|
2015-07-03 12:25:02 -07:00
|
|
|
});
|
2015-07-03 13:15:13 -07:00
|
|
|
}else{
|
|
|
|
callback(null, 'task2');
|
|
|
|
}
|
2015-07-02 17:49:23 -07:00
|
|
|
}
|
|
|
|
], function(err, results) {
|
|
|
|
if(err){
|
|
|
|
next(new Error({
|
|
|
|
message: err.message
|
|
|
|
}));
|
|
|
|
}
|
2015-07-03 13:15:13 -07:00
|
|
|
console.log('ending form save');
|
|
|
|
next();
|
|
|
|
|
2015-07-01 19:49:35 -07:00
|
|
|
});
|
2015-07-03 12:25:02 -07:00
|
|
|
}else{
|
|
|
|
next();
|
2015-07-01 19:49:35 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-02 17:49:23 -07:00
|
|
|
//Autogenerate Form_fields from PDF if 'isGenerated' flag is set
|
|
|
|
// FormSchema.pre('save', function (next) {
|
|
|
|
// var field, _form_fields;
|
2015-06-29 19:14:43 -07:00
|
|
|
|
2015-07-02 17:49:23 -07:00
|
|
|
// if(this.pdf){
|
|
|
|
// if(this.isGenerated){
|
|
|
|
|
|
|
|
// var _typeConvMap = {
|
|
|
|
// 'Multiline': 'textarea',
|
|
|
|
// 'Text': 'textfield',
|
|
|
|
// 'Button': 'checkbox',
|
|
|
|
// 'Choice': 'radio',
|
|
|
|
// 'Password': 'password',
|
|
|
|
// 'FileSelect': 'filefield',
|
|
|
|
// 'Radio': 'radio'
|
|
|
|
// };
|
|
|
|
|
|
|
|
// var that = this;
|
|
|
|
// console.log('autogenerating form');
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
|
|
|
|
|
|
|
|
// //Map PDF field names to FormField field names
|
|
|
|
// for(var i = 0; i < _form_fields.length; i++){
|
|
|
|
// field = _form_fields[i];
|
|
|
|
|
|
|
|
// //Convert types from FDF to 'FormField' types
|
|
|
|
// if(_typeConvMap[ field.fieldType+'' ]){
|
|
|
|
// field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
|
|
|
// }
|
|
|
|
|
|
|
|
// field.fieldValue = '';
|
|
|
|
// field.created = Date.now();
|
|
|
|
// field.required = true;
|
|
|
|
// field.disabled = false;
|
|
|
|
|
|
|
|
// // field = new Field(field);
|
|
|
|
// // field.save(function(err) {
|
|
|
|
// // if (err) {
|
|
|
|
// // console.error(err.message);
|
|
|
|
// // throw new Error(err.message);
|
|
|
|
// // });
|
|
|
|
// // } else {
|
|
|
|
// // _form_fields[i] = this;
|
|
|
|
// // }
|
|
|
|
// // });
|
|
|
|
// _form_fields[i] = field;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// console.log('NEW FORM_FIELDS: ');
|
|
|
|
// console.log(_form_fields);
|
|
|
|
|
|
|
|
// console.log('\n\nOLD FORM_FIELDS: ');
|
|
|
|
// console.log(that.form_fields);
|
|
|
|
|
|
|
|
// that.form_fields = _form_fields;
|
|
|
|
// next();
|
|
|
|
// });
|
|
|
|
// } catch(err){
|
|
|
|
// next( new Error(err.message) );
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// next();
|
|
|
|
// });
|
2015-06-29 15:51:29 -07:00
|
|
|
|
2015-07-03 12:25:02 -07:00
|
|
|
// FormSchema.methods.generateSubmissionsCSV = function (cb) {
|
|
|
|
// if(this.submissions.length){
|
|
|
|
// submissions = this.submissions
|
|
|
|
// }else{
|
|
|
|
// submissions =
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// _values.forEach(function(val){
|
|
|
|
// if(val === true){
|
|
|
|
// val = 'Yes';
|
|
|
|
// }else if(val === false) {
|
|
|
|
// val = 'Off';
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
|
|
|
// var jsonObj = _.zipObject(_keys, _values);
|
|
|
|
|
|
|
|
// return jsonObj;
|
|
|
|
// };
|
|
|
|
|
|
|
|
FormSchema.methods.generateFDFTemplate = function (cb) {
|
2015-06-29 15:51:29 -07:00
|
|
|
var _keys = _.pluck(this.form_fields, 'title'),
|
|
|
|
_values = _.pluck(this.form_fields, 'fieldValue');
|
|
|
|
|
|
|
|
_values.forEach(function(val){
|
|
|
|
if(val === true){
|
|
|
|
val = 'Yes';
|
|
|
|
}else if(val === false) {
|
|
|
|
val = 'Off';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var jsonObj = _.zipObject(_keys, _values);
|
|
|
|
|
|
|
|
return jsonObj;
|
|
|
|
};
|
|
|
|
|
2015-07-03 12:25:02 -07:00
|
|
|
|
2015-06-29 15:51:29 -07:00
|
|
|
mongoose.model('Form', FormSchema);
|