From 71d315a2d01a968a11b75811e8f619ca36e15e80 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Thu, 2 Jul 2015 17:49:23 -0700 Subject: [PATCH] got pdf generation working --- app/controllers/forms.server.controller.js | 5 +- app/models/form.server.model.js | 311 +++++++++++------- app/models/form_submission.server.model.js | 5 +- .../create-form.client.controller.js | 10 +- 4 files changed, 198 insertions(+), 133 deletions(-) diff --git a/app/controllers/forms.server.controller.js b/app/controllers/forms.server.controller.js index dc247a93..01351ffd 100644 --- a/app/controllers/forms.server.controller.js +++ b/app/controllers/forms.server.controller.js @@ -205,9 +205,10 @@ exports.listSubmissions = function(req, res) { * Update a form */ exports.update = function(req, res) { - + console.log('in form.update()'); + var form = req.form; - console.log(req.body.form); + // console.log(req.body.form); form = _.extend(form, req.body.form); form.admin = req.user; diff --git a/app/models/form.server.model.js b/app/models/form.server.model.js index 86212e07..ad637fff 100644 --- a/app/models/form.server.model.js +++ b/app/models/form.server.model.js @@ -11,6 +11,7 @@ var mongoose = require('mongoose'), config = require('../../config/config'), path = require('path'), fs = require('fs-extra'), + async = require('async'), Field = mongoose.model('Field', FieldSchema); @@ -73,9 +74,20 @@ var FormSchema = new Schema({ }, }); +//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); + }); + } +}); + //Create folder for user's pdfs FormSchema.pre('save', function (next) { - var newDestination = path.join(config.pdfUploadPath, this.admin.username.trim()), + var newDestination = path.join(config.pdfUploadPath, this.admin.username.replace(/ /g,'')), stat = null; try { @@ -91,149 +103,200 @@ FormSchema.pre('save', function (next) { } }); - -//Update lastModified everytime we save +//Update lastModified and created everytime we save FormSchema.pre('save', function (next) { - this.lastModified = Date.now(); + var now = new Date(); + this.lastModified = now; + if( !this.created ){ + this.created = now; + } next(); }); //Move PDF to permanent location after new template is uploaded FormSchema.pre('save', function (next) { - console.log(this.pdf); - console.log("isModified: "+this.isModified('pdf')); - var that = this; if(this.pdf){ - if(this.isModified('pdf')){ + var that = this; + async.series([ + function(callback){ + if(that.isModified('pdf')){ + console.log('about to move PDF'); - var new_filename = this.title.trim()+'_template.pdf'; + var new_filename = that.title.replace(/ /g,'')+'_template.pdf'; - var newDestination = path.join(config.pdfUploadPath, this.admin.username.trim(), this.title.trim()), - stat = null; + 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()) { - console.log('Directory '+newDestination+' cannot be created'); - next( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') ); - } + try { + stat = fs.statSync(newDestination); + } catch (err) { + fs.mkdirSync(newDestination); + } + if (stat && !stat.isDirectory()) { + console.log('Directory '+newDestination+' cannot be created'); + callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') ); + } - console.log('about to move PDF'); - //Move pdf to permanent location - fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) { - // if (err) { - // console.error(err); - // next( new Error(err.message) ); - // } + console.log('about to move PDF'); - // //Delete old pdf file - // fs.unlink(that.pdf.path, function(err){ - if (err) { - console.error(err); + 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; + + console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path); + + callback(null,null); + }); + } + 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' + }; + + console.log('autogenerating form'); + console.log(that.pdf.path); + + try { + pdfFiller.generateFieldJson(that.pdf.path, function(_form_fields){ + + //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+'' ]; + } + + 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; + } + + 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; + callback(); + }); + } catch(err){ next( new Error(err.message) ); } - - // console.log('successfully deleted', that.pdf.path); - - that.pdf.path = path.join(newDestination, new_filename); - that.pdf.name = new_filename; - - console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path); - - next(); - // }); - - }); - - } - }else { - next(); - } -}); - -//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); + } + callback(null,null); + } + ], function(err, results) { + if(err){ + next(new Error({ + message: err.message + })); + } + next(); }); } -}); - -//Autogenerate FORM from PDF if 'isGenerated' flag is 'true' -FormSchema.pre('save', function (next) { - var field, _form_fields; - - if(this.isGenerated && this.pdf){ - - 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) ); - } - - } - - //Throw error if we encounter form with invalid type next(); }); +//Autogenerate Form_fields from PDF if 'isGenerated' flag is set +// FormSchema.pre('save', function (next) { +// var field, _form_fields; + +// 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(); +// }); + FormSchema.methods.convertToFDF = function (cb) { var _keys = _.pluck(this.form_fields, 'title'), _values = _.pluck(this.form_fields, 'fieldValue'); diff --git a/app/models/form_submission.server.model.js b/app/models/form_submission.server.model.js index c4b6c7fb..40fe049e 100644 --- a/app/models/form_submission.server.model.js +++ b/app/models/form_submission.server.model.js @@ -90,12 +90,11 @@ FormSubmissionSchema.pre('save', function (next) { if(_form.autofillPDFs){ - dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf'; + dest_filename = _form.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf'; dest_path = path.join(config.pdfUploadPath, dest_filename); this.pdfFilePath = dest_path; - // console.log('autofillPDFs check'); pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){ console.log('fdfData: \n'); @@ -108,7 +107,7 @@ FormSubmissionSchema.pre('save', function (next) { console.log('\n err.message: '+err.message); next( new Error(err.message) ); } - console.log('Field data from Form: '+_form.title.trim()+' outputed to new PDF: '+dest_path); + console.log('Field data from Form: '+_form.title.replace(/ /g,'')+' outputed to new PDF: '+dest_path); next(); }); } else { diff --git a/public/modules/forms/controllers/create-form.client.controller.js b/public/modules/forms/controllers/create-form.client.controller.js index 91126c54..6c20f804 100644 --- a/public/modules/forms/controllers/create-form.client.controller.js +++ b/public/modules/forms/controllers/create-form.client.controller.js @@ -5,9 +5,10 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$ $scope.isNewForm = false; $scope.pdfLoading = false; - var _current_upload = null; - $scope.log = ''; + $scope.form = {}; + $scope.log = ''; + var _current_upload = null; // Get current form if it exists, or create new one if($stateParams.formId){ @@ -24,12 +25,13 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$ $scope.cancelUpload = function(){ _current_upload.abort(); $scope.pdfLoading = false; + $scope.removePDF(); }; $scope.removePDF = function(){ $scope.form.pdf = null; - $scope.isGenerated = false; - $scope.autofillPDFs = false; + $scope.form.isGenerated = false; + $scope.form.autofillPDFs = false; console.log('form.pdf: '+$scope.form.pdf+' REMOVED'); };