diff --git a/app/controllers/forms.server.controller.js b/app/controllers/forms.server.controller.js index 0830d063..90fa92e1 100644 --- a/app/controllers/forms.server.controller.js +++ b/app/controllers/forms.server.controller.js @@ -1,4 +1,4 @@ - +'use strict'; /** * Module dependencies. @@ -38,7 +38,7 @@ exports.create = function(req, res) { /** * Upload PDF */ -exports.uploadPDF = function(req, res) { +exports.uploadPDF = function(req, res, next) { var parser = new PDFParser(), pdfFile = req.files.file; @@ -54,21 +54,21 @@ exports.uploadPDF = function(req, res) { } fs.exists(pdfFile.path, function(exists) { if(exists) { - console.log("UPLOADING FILE \N\N"); - // res.end('Got your file!'); - // next() - return res.status(200); + console.log('UPLOADING FILE \N\N'); + res.end('Got your file!'); + next(); + // return res.status(200); } else { - // res.end('DID NOT get your file!'); - // next() - return res.status(400); + res.end('DID NOT get your file!'); + next(); + // return res.status(400); } }); } - // next(new Error('FILE NOT UPLOADED')); + next(new Error('FILE NOT UPLOADED')); - return res.status(400); + // return res.status(400); // res.json(pdfFile); }; @@ -77,6 +77,7 @@ exports.uploadPDF = function(req, res) { */ exports.read = function(req, res) { // console.log(req.form); + console.log(req.form.form_fields[7]); res.json(req.form); }; @@ -86,36 +87,38 @@ exports.read = function(req, res) { exports.createSubmission = function(req, res) { var submission = new FormSubmission(), - form = req.form, fdfData; + form = req.form, + fdfData, + fdfTemplate; submission.form = form; submission.admin = req.user; submission.form_fields = req.body.form_fields; submission.title = req.body.title; submission.timeElapsed = req.body.timeElapsed; + // submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress; + if (form.isGenerated){ - fdfData = form.convertToJSON(); + fdfTemplate = form.convertToFDF(); } else { try { - fdfData = pdfFiller.mapForm2PDF(form.convertToJSON(), form.pdfFieldMap); + fdfTemplate = pdfFiller.mapForm2PDF(form.convertToFDF(), form.pdfFieldMap); } catch(err){ throw new Error(err.message); } } + fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null); + submission.fdfData = fdfData; - - - + //Create new file - pdfFiller.fillForm( form.pdf.path, config.pdfUploadPath+form.title+"/"+form.title+"_"+Date.now()+"_submission.pdf", fdfData, function() { - console.log("\n\n\n fdfData"); - console.log(fdfData); - }); - - - // submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress; + // pdfFiller.fillForm( form.pdf.path, config.pdfUploadPath+form.title+'/'+form.title+'_'+Date.now()+'_submission.pdf', fdfData, function() { + // console.log('\n\n\n fdfData'); + // console.log(fdfData); + // console.log('\n\n\n :\n'); + // console.log(req.body); submission.save(function(err){ if (err) { @@ -127,6 +130,7 @@ exports.createSubmission = function(req, res) { return res.status(200); } }); + // }); }; diff --git a/app/models/form.server.model.js b/app/models/form.server.model.js index e27e16bb..08d48b79 100644 --- a/app/models/form.server.model.js +++ b/app/models/form.server.model.js @@ -41,7 +41,7 @@ var FormSchema = new Schema({ }, form_fields: [Schema.Types.Mixed], - submission: [{ + submissions: [{ type: Schema.Types.ObjectId, ref: 'FormSubmission' }], @@ -67,18 +67,17 @@ var FormSchema = new Schema({ }, }); +//Move PDF to permanent location after first save FormSchema.pre('save', function (next) { // console.log(this.pdf); // debugger; - //Move PDF to permanent location after first save if(this.pdf){ if(this.pdf.modified){ var new_filename = this.pdf.title.trim()+'_template.pdf'; - // TODO: DAVID - need to remove dependence on relative paths - var newDestination = path.join(config.pdfUploadPath+this.title+"/", this.pdf.title.trim()), + var newDestination = path.join(config.pdfUploadPath, this.pdf.title.trim()), stat = null; try { @@ -92,18 +91,17 @@ FormSchema.pre('save', function (next) { } console.log('about to move PDF'); - - fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) { if (err) { console.error(err); next( new Error(err.message) ); } - console.log('PDF file successfully moved'); this.pdf.path = path.join(newDestination, new_filename); this.pdf.name = new_filename; + console.log('PDF file:'+this.pdf.name+' successfully moved to: '+this.pdf.path); + next(); }); @@ -113,55 +111,63 @@ FormSchema.pre('save', function (next) { } }); -// FormSchema.pre('save', function (next) { -// //Autogenerate FORM from PDF -// if(this.isGenerated && this.pdf && this.autofillPDFs){ -// this.autofillPDFs = false; -// var _pdfConvMap = { -// 'Text': 'textfield', -// 'Button': 'checkbox' -// }; -// var that = this; -// console.log('autogenerating form'); +//Autogenerate FORM from PDF if 'isGenerated' flag is 'true' +FormSchema.pre('save', function (next) { + var field; + + if(this.isGenerated && this.pdf){ -// try { -// pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){ + var _typeConvMap = { + 'Text': 'textfield', + 'Button': 'checkbox' + }; -// _form_fields.forEach(function(field){ -// if(_pdfConvMap[ field.fieldType+'' ]){ -// field.fieldType = _pdfConvMap[ field.fieldType+'' ]; -// } -// field.created = Date.now(); -// field.fieldValue = ''; -// field.required = true; -// field.disabled = false; + var that = this; + console.log('autogenerating form'); -// // field = new Field(field); -// // field.save() -// }); + try { + pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){ -// // console.log('NEW FORM_FIELDS: '); -// // console.log(_form_fields); + //Map PDF field names to FormField field names + for(var i = 0; i < _form_fields.length; i++){ + field = _form_fields[i]; -// // console.log('\n\nOLD FORM_FIELDS: '); -// // console.log(that.form_fields); + //Convert types from FDF to 'FormField' types + if(_typeConvMap[ field.fieldType+'' ]){ + field.fieldType = _pdfConvMap[ field.fieldType+'' ]; + } -// that.form_fields = _form_fields; -// next(); -// }); -// } catch(err){ -// next( new Error(err.message) ); -// } + field.created = Date.now(); + field.fieldValue = ''; + field.required = true; + field.disabled = false; -// } + // field = new Field(field); + // field.save() + } -// //Throw error if we encounter form with invalid type -// next(); + console.log('NEW FORM_FIELDS: '); + console.log(_form_fields); -// }); + console.log('\n\nOLD FORM_FIELDS: '); + console.log(that.form_fields); -FormSchema.methods.convertToJSON = function (cb) { + that.form_fields = _form_fields; + next(); + }); + } catch(err){ + next( new Error(err.message) ); + } + + } + + //Throw error if we encounter form with invalid type + 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 777d7c8b..9eabbc21 100644 --- a/app/models/form_submission.server.model.js +++ b/app/models/form_submission.server.model.js @@ -70,10 +70,11 @@ FormSubmissionSchema.pre('save', function (next){ }); } } - console.log('ipAddr check'); + // console.log('ipAddr check'); next(); }); +//Generate autofilled PDF if flags are set FormSubmissionSchema.pre('save', function (next) { // debugger; var fdfData, dest_filename, dest_path; @@ -82,23 +83,31 @@ FormSubmissionSchema.pre('save', function (next) { Form.findById(that.form, function(err, _form){ if(err) next( new Error(err.mesasge) ); - this.title = _form.title; - console.log(_form); - //Create filled-out PDF, if there is a pdf template - if(_form.autofillPDFs){ + // that.title = _form.title; + // console.log(_form); - dest_filename = this.title.trim()+'_submission_'+Date.now()+'.pdf'; - dest_path = path.join(config.pdfUploadPath, this.title.trim(), dest_filename); + if(true){ //_form.autofillPDFs){ + + dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf'; + dest_path = path.join(config.pdfUploadPath, dest_filename); this.pdfFilePath = dest_path; - console.log('autofillPDFs check'); + // console.log('autofillPDFs check'); + pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){ + console.log("fdfData: \n"); + console.log(that.fdfData); - if(err) next( new Error(err.message) ); + // console.log("_form.pdf.path: "+_form.pdf.path); + // console.log("dest_path: "+dest_path); - console.log('Field data from Form: '+this.title.trim()+' outputed to new PDF: '+dest_path); + if(err) { + 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); next(); }); } else { diff --git a/public/modules/forms/directives/form.client.directive.js b/public/modules/forms/directives/form.client.directive.js index b7ef2deb..4a8fc965 100644 --- a/public/modules/forms/directives/form.client.directive.js +++ b/public/modules/forms/directives/form.client.directive.js @@ -13,6 +13,8 @@ angular.module('forms').directive('formDirective', ['$http', '$timeout', 'timeCo console.log($scope.form.timeElapsed); + // console.log($scope.form.form_fields[7]); + $http.post('/forms/'+$scope.form._id,$scope.form). success(function(data, status, headers){ console.log('form submitted successfully'); diff --git a/public/modules/forms/services/time-counter.client.service.js b/public/modules/forms/services/time-counter.client.service.js index cbd19de0..a6ba2ff4 100644 --- a/public/modules/forms/services/time-counter.client.service.js +++ b/public/modules/forms/services/time-counter.client.service.js @@ -4,7 +4,7 @@ angular.module('forms').service('timeCounter', [ function(){ var _startTime, _endTime, that=this; - this.timeSpent; + this.timeSpent = 0; this.startClock = function(){ _startTime = Date.now(); diff --git a/public/modules/forms/views/directiveViews/field/checkbox.html b/public/modules/forms/views/directiveViews/field/checkbox.html index 76370bc0..59ffc803 100755 --- a/public/modules/forms/views/directiveViews/field/checkbox.html +++ b/public/modules/forms/views/directiveViews/field/checkbox.html @@ -1,6 +1,6 @@

- + (* required)
diff --git a/uploads/pdfs/snthsnthsnth_submission_1435626867783.pdf b/uploads/pdfs/snthsnthsnth_submission_1435626867783.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435626867783.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435626906006.pdf b/uploads/pdfs/snthsnthsnth_submission_1435626906006.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435626906006.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627024811.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627024811.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627024811.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627149925.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627149925.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627149925.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627259301.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627259301.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627259301.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627563300.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627563300.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627563300.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627608467.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627608467.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627608467.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627689598.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627689598.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627689598.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627728743.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627728743.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627728743.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627853923.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627853923.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627853923.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627904065.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627904065.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627904065.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435627932463.pdf b/uploads/pdfs/snthsnthsnth_submission_1435627932463.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435627932463.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435628052453.pdf b/uploads/pdfs/snthsnthsnth_submission_1435628052453.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435628052453.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435630188095.pdf b/uploads/pdfs/snthsnthsnth_submission_1435630188095.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435630188095.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435630300812.pdf b/uploads/pdfs/snthsnthsnth_submission_1435630300812.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435630300812.pdf differ diff --git a/uploads/pdfs/snthsnthsnth_submission_1435630420602.pdf b/uploads/pdfs/snthsnthsnth_submission_1435630420602.pdf new file mode 100644 index 00000000..23460f8e Binary files /dev/null and b/uploads/pdfs/snthsnthsnth_submission_1435630420602.pdf differ diff --git a/uploads/tmp/1432171572285.pdf b/uploads/tmp/1432171572285.pdf new file mode 100644 index 00000000..b93e7316 Binary files /dev/null and b/uploads/tmp/1432171572285.pdf differ