diff --git a/.bowerrc b/.bowerrc
index 1b842779..6a060253 100755
--- a/.bowerrc
+++ b/.bowerrc
@@ -1,3 +1,4 @@
{
- "directory": "public/lib"
+ "directory": "public/lib",
+ "analytics": false
}
diff --git a/app/controllers/forms.server.controller.js b/app/controllers/forms.server.controller.js
index f95d43d9..15d6bdef 100644
--- a/app/controllers/forms.server.controller.js
+++ b/app/controllers/forms.server.controller.js
@@ -71,7 +71,8 @@ exports.createSubmission = function(req, res) {
var submission = new FormSubmission(),
form = req.form,
fdfData,
- fdfTemplate;
+ fdfTemplate,
+ that = this;
submission.form = form;
submission.admin = req.user;
@@ -81,32 +82,54 @@ exports.createSubmission = function(req, res) {
console.log(req.body);
// submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
- if (form.isGenerated){
- fdfTemplate = form.convertToFDF();
- } else {
- try {
- fdfTemplate = pdfFiller.mapForm2PDF(form.convertToFDF(), form.pdfFieldMap);
- } catch(err){
- throw new Error(err.message);
- }
- }
-
if(form.autofillPDFs){
+ if (form.isGenerated){
+ fdfTemplate = form.generateFDFTemplate();
+ } else {
+ try {
+ fdfTemplate = pdfFiller.mapForm2PDF(form.generateFDFTemplate(), form.pdfFieldMap);
+ } catch(err){
+ res.status(400).send({
+ message: errorHandler.getErrorMessage(err)
+ });
+ }
+ }
fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null);
submission.fdfData = fdfData;
}
- submission.save(function(err){
- if (err) {
- console.error(err);
- res.status(400).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- console.log('Form Submission CREATED');
+ async.series([
+ function(callback){
+ submission.save(function(err){
+ if (err) {
+ callback(err);
+ } else {
+ callback(null);
+ }
+ });
+ },
+ function(callback){
+ //Add submission to Form.submissionns
+ form.submissions.push(submission);
+
+ form.save(function(err){
+ if (err) {
+ callback(err);
+ } else {
+ callback(null);
+ }
+ });
+ },
+ ], function(err, results) {
+ if(err){
+ res.status(400).send({
+ message: errorHandler.getErrorMessage(err)
+ });
+ }
+ console.log(results);
+ console.log(that.form_fields);
res.status(200).send('Form submission successfully saved');
- }
- });
+ });
};
/**
@@ -115,17 +138,21 @@ exports.createSubmission = function(req, res) {
exports.listSubmissions = function(req, res) {
var _form = req.form;
- FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) {
- if (err) {
- console.log(err);
- res.status(500).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- console.log('hello');
- res.json(submissions);
- }
- });
+ if(_form.submissions.length){
+ res.json(_form.submissions);
+ }else{
+ FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) {
+ if (err) {
+ console.log(err);
+ res.status(400).send({
+ message: errorHandler.getErrorMessage(err)
+ });
+ } else {
+ console.log('retrieved submissions for form');
+ res.json(submissions);
+ }
+ });
+ }
};
/**
@@ -188,7 +215,7 @@ exports.delete = function(req, res) {
Form.remove({_id: form._id}, function(err) {
if (err) {
res.status(500).send({
- message: err.message
+ message: errorHandler.getErrorMessage(err)
});
} else {
console.log('Form successfully deleted');
diff --git a/app/models/form.server.model.js b/app/models/form.server.model.js
index ad637fff..1fdf7e53 100644
--- a/app/models/form.server.model.js
+++ b/app/models/form.server.model.js
@@ -172,50 +172,49 @@ FormSchema.pre('save', function (next) {
console.log('autogenerating form');
console.log(that.pdf.path);
- try {
- pdfFiller.generateFieldJson(that.pdf.path, function(_form_fields){
+ 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];
+ //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;
+ //Convert types from FDF to 'FormField' types
+ if(_typeConvMap[ field.fieldType+'' ]){
+ field.fieldType = _typeConvMap[ field.fieldType+'' ];
}
- console.log('NEW FORM_FIELDS: ');
- console.log(_form_fields);
+ field.fieldValue = '';
+ field.created = Date.now();
+ field.required = true;
+ field.disabled = false;
- console.log('\n\nOLD FORM_FIELDS: ');
- console.log(that.form_fields);
+ // 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;
+ }
- that.form_fields = _form_fields;
- callback();
- });
- } catch(err){
- next( new Error(err.message) );
- }
+ 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(null, 'pdfFiller');
+ });
}
- callback(null,null);
+ callback(null, that);
}
], function(err, results) {
if(err){
@@ -223,10 +222,13 @@ FormSchema.pre('save', function (next) {
message: err.message
}));
}
- next();
+ console.log(results);
+ console.log(that.form_fields);
+ next(results[1]);
});
+ }else{
+ next();
}
- next();
});
//Autogenerate Form_fields from PDF if 'isGenerated' flag is set
@@ -297,7 +299,28 @@ FormSchema.pre('save', function (next) {
// next();
// });
-FormSchema.methods.convertToFDF = function (cb) {
+// 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) {
var _keys = _.pluck(this.form_fields, 'title'),
_values = _.pluck(this.form_fields, 'fieldValue');
@@ -314,4 +337,5 @@ FormSchema.methods.convertToFDF = function (cb) {
return jsonObj;
};
+
mongoose.model('Form', FormSchema);
diff --git a/app/models/form_submission.server.model.js b/app/models/form_submission.server.model.js
index ee71e2f0..fc97280b 100644
--- a/app/models/form_submission.server.model.js
+++ b/app/models/form_submission.server.model.js
@@ -81,9 +81,10 @@ FormSubmissionSchema.pre('save', function (next) {
// debugger;
var fdfData, dest_filename, dest_path;
var that = this;
+ var _form = this.form;
- Form.findById(that.form, function(err, _form){
- if(err) next( new Error(err.mesasge) );
+ // Form.findById(that.form, function(err, _form){
+ // if(err) next( new Error(err.mesasge) );
that.title = _form.title;
// console.log(_form);
@@ -96,7 +97,7 @@ FormSubmissionSchema.pre('save', function (next) {
this.pdfFilePath = dest_path;
- pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){
+ pdfFiller.fillForm(_form.pdf.path, dest_path, that.fdfData, function(err){
console.log('fdfData: \n');
console.log(that.fdfData);
@@ -114,7 +115,7 @@ FormSubmissionSchema.pre('save', function (next) {
next();
}
- });
+ // });
});
diff --git a/app/views/layout.server.view.html b/app/views/layout.server.view.html
index ced993bd..4e3779fe 100755
--- a/app/views/layout.server.view.html
+++ b/app/views/layout.server.view.html
@@ -34,11 +34,17 @@
+
+ {% for bowerCssFile in bowerCssFiles %}
+
+ {% endfor %}
+
+
{% for cssFile in cssFiles %}
{% endfor %}
-
+
@@ -60,25 +66,31 @@
var user = {{ user | json | safe }};
-
+
+ {% for bowerJSFile in bowerJSFiles %}
+
+ {% endfor %}
+
+
{% for jsFile in jsFiles %}
{% endfor %}
+
{% if process.env.NODE_ENV === 'development' %}
{% endif %}
-
+