2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-02 23:34:00 +00:00
|
|
|
angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', '$http',
|
|
|
|
function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location, $http) {
|
|
|
|
$scope.form = {};
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.isNewForm = false;
|
2015-07-02 04:54:46 +00:00
|
|
|
$scope.log = '';
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.pdfLoading = false;
|
2015-07-03 00:49:23 +00:00
|
|
|
var _current_upload = null;
|
2015-07-02 04:54:46 +00:00
|
|
|
|
|
|
|
// Get current form if it exists, or create new one
|
|
|
|
if($stateParams.formId){
|
2015-07-02 23:34:00 +00:00
|
|
|
Forms.get({ formId: $stateParams.formId}, function(form){
|
|
|
|
$scope.form = angular.fromJson(angular.toJson(form));
|
|
|
|
console.log($scope.form);
|
2015-07-02 04:54:46 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$scope.form.form_fields = [];
|
|
|
|
$scope.isNewForm = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//PDF Functions
|
|
|
|
$scope.cancelUpload = function(){
|
|
|
|
_current_upload.abort();
|
2015-07-01 23:14:39 +00:00
|
|
|
$scope.pdfLoading = false;
|
2015-07-03 00:49:23 +00:00
|
|
|
$scope.removePDF();
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removePDF = function(){
|
|
|
|
$scope.form.pdf = null;
|
2015-07-03 00:49:23 +00:00
|
|
|
$scope.form.isGenerated = false;
|
|
|
|
$scope.form.autofillPDFs = false;
|
2015-07-02 04:54:46 +00:00
|
|
|
|
|
|
|
console.log('form.pdf: '+$scope.form.pdf+' REMOVED');
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.uploadPDF = function(files) {
|
|
|
|
|
|
|
|
if (files && files.length) {
|
|
|
|
// for (var i = 0; i < files.length; i++) {
|
|
|
|
var file = files[0];
|
|
|
|
_current_upload = Upload.upload({
|
|
|
|
url: '/upload/pdf',
|
|
|
|
fields: {
|
|
|
|
'user': $scope.user,
|
|
|
|
'form': $scope.form
|
|
|
|
},
|
|
|
|
file: file
|
|
|
|
}).progress(function (evt) {
|
|
|
|
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
|
|
|
|
$scope.log = 'progress: ' + progressPercentage + '% ' +
|
|
|
|
evt.config.file.name + '\n' + $scope.log;
|
|
|
|
$scope.pdfLoading = true;
|
|
|
|
}).success(function (data, status, headers, config) {
|
2015-07-02 23:34:00 +00:00
|
|
|
$scope.log = 'file ' + data.originalname + ' uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log;
|
|
|
|
console.log($scope.form.pdf);
|
|
|
|
$scope.form.pdf = angular.fromJson(angular.toJson(data));
|
2015-07-02 04:54:46 +00:00
|
|
|
$scope.pdfLoading = false;
|
|
|
|
|
|
|
|
console.log($scope.log);
|
|
|
|
console.log('$scope.pdf: '+$scope.form.pdf.name);
|
2015-07-02 23:34:00 +00:00
|
|
|
if(!$scope.$$phase){
|
2015-07-02 04:54:46 +00:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
}).error(function(err){
|
|
|
|
$scope.pdfLoading = false;
|
|
|
|
console.log('Error occured during upload.\n');
|
|
|
|
console.log(err);
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
2015-07-02 04:54:46 +00:00
|
|
|
// }
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
$rootScope.goToWithId = function(route, id) {
|
2015-07-02 04:54:46 +00:00
|
|
|
$state.go(route, {'formId': id}, {reload: true});
|
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
// Create new Form
|
2015-07-06 04:29:05 +00:00
|
|
|
$rootScope.createOrUpdate = function() {
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
if($scope.isNewForm){
|
|
|
|
// Create new Form object
|
|
|
|
var form = new Forms($scope.form);
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 23:34:00 +00:00
|
|
|
$http.post('/forms', {form: $scope.form})
|
|
|
|
.success(function(data, status, headers){
|
2015-07-02 04:54:46 +00:00
|
|
|
console.log('form created');
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
// Clear form fields
|
|
|
|
$scope.form = {};
|
|
|
|
// Redirect after save
|
2015-07-03 01:14:48 +00:00
|
|
|
$scope.goToWithId('viewForm', $scope.form._id);
|
|
|
|
}).error(function(errorResponse){
|
2015-07-07 01:21:43 +00:00
|
|
|
console.log(errorResponse);
|
|
|
|
$scope.error = errorResponse;
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
2015-07-02 04:54:46 +00:00
|
|
|
} else{
|
|
|
|
$scope.update();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update existing Form
|
2015-07-06 04:29:05 +00:00
|
|
|
$rootScope.update = function() {
|
2015-07-02 04:54:46 +00:00
|
|
|
var form = new Forms($scope.form);
|
2015-07-02 23:34:00 +00:00
|
|
|
console.log('update form');
|
|
|
|
console.log($scope.form);
|
|
|
|
|
|
|
|
$http.put('/forms/'+$scope.form._id, {form: $scope.form})
|
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form updated successfully');
|
|
|
|
$scope.goToWithId('viewForm', $scope.form._id);
|
|
|
|
}).error(function(err){
|
|
|
|
console.log('Error occured during form UPDATE.\n');
|
|
|
|
console.log(err);
|
2015-07-02 04:54:46 +00:00
|
|
|
});
|
2015-07-02 23:34:00 +00:00
|
|
|
// form.$update({formId: $scope.form._id}, function(response) {
|
|
|
|
// console.log('form successfully updated');
|
|
|
|
// $scope.goToWithId('viewForm', response._id);
|
|
|
|
// }, function(errorResponse) {
|
|
|
|
// console.log(errorResponse.data.message);
|
|
|
|
// $scope.error = errorResponse.data.message;
|
|
|
|
// });
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
2015-07-03 01:14:48 +00:00
|
|
|
]);
|