2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Forms controller
|
2015-07-06 04:29:05 +00:00
|
|
|
angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http',
|
|
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) {
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope = $rootScope;
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myform = CurrentForm.getForm();
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope.saveInProgress = false;
|
2015-07-02 04:54:46 +00:00
|
|
|
$scope.viewSubmissions = false;
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.showCreateModal = false;
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.table = {
|
|
|
|
masterChecker: true,
|
|
|
|
rows: []
|
|
|
|
};
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// Return all user's Forms
|
|
|
|
$scope.findAll = function() {
|
|
|
|
$scope.myforms = Forms.query();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find a specific Form
|
|
|
|
$scope.findOne = function() {
|
|
|
|
$scope.myform = Forms.get({
|
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
|
|
|
CurrentForm.setForm($scope.myform);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.goToWithId = function(route, id) {
|
|
|
|
$state.go(route, {'formId': id}, {reload: true});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.setForm = function (form) {
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope.myform = form;
|
2015-07-07 01:21:43 +00:00
|
|
|
};
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
//Modal functions
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.openCreateModal = function(){
|
|
|
|
if(!$scope.showCreateModal){
|
|
|
|
$scope.showCreateModal = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.closeCreateModal = function(){
|
|
|
|
if($scope.showCreateModal){
|
|
|
|
$scope.showCreateModal = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
/*
|
|
|
|
* Table Functions
|
|
|
|
*/
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope.isAtLeastOneChecked = function(){
|
2015-07-27 18:36:44 +00:00
|
|
|
console.log('isAtLeastOneChecked');
|
2015-07-27 18:11:43 +00:00
|
|
|
for(var i=0; i<$scope.table.rows.length; i++){
|
|
|
|
if($scope.table.rows[i].selected) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.toggleAllCheckers = function(){
|
|
|
|
console.log('toggleAllCheckers');
|
|
|
|
for(var i=0; i<$scope.table.rows.length; i++){
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.table.rows[i].selected = $scope.table.masterChecker;
|
2015-07-03 23:47:14 +00:00
|
|
|
}
|
2015-07-07 01:21:43 +00:00
|
|
|
};
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.toggleObjSelection = function($event, description) {
|
|
|
|
$event.stopPropagation();
|
2015-07-21 23:25:45 +00:00
|
|
|
};
|
|
|
|
$scope.rowClicked = function(obj) {
|
2015-07-03 23:47:14 +00:00
|
|
|
obj.selected = !obj.selected;
|
2015-07-21 23:25:45 +00:00
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
/*
|
|
|
|
* Form Submission Methods
|
|
|
|
*/
|
|
|
|
//Delete selected submissions of Form
|
|
|
|
$scope.deleteSelectedSubmissions = function(){
|
|
|
|
// console.log('deleteSelectedSubmissions');
|
2015-07-27 18:36:44 +00:00
|
|
|
var delete_ids = _.chain($scope.table.rows).filter(function(row){
|
2015-07-27 18:11:43 +00:00
|
|
|
return !!row.selected;
|
|
|
|
}).pluck('_id').value();
|
|
|
|
console.log(delete_ids);
|
|
|
|
|
|
|
|
$http({ url: '/forms/'+$scope.myform._id+'/submissions',
|
|
|
|
method: 'DELETE',
|
|
|
|
data: {deleted_submissions: delete_ids},
|
|
|
|
headers: {"Content-Type": "application/json;charset=utf-8"}
|
|
|
|
}).success(function(data, status, headers){
|
|
|
|
//Remove deleted ids from table
|
|
|
|
for(var i=0; i<$scope.table.rows.length; i++){
|
|
|
|
if($scope.table.rows[i].selected){
|
|
|
|
$scope.table.rows.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function(err){
|
|
|
|
console.log('Could not delete form submissions.\nError: ');
|
|
|
|
console.log(err);
|
|
|
|
console.error = err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
//Fetch and display submissions of Form
|
2015-07-02 04:54:46 +00:00
|
|
|
$scope.showSubmissions = function(){
|
|
|
|
$scope.viewSubmissions = true;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
$http.get('/forms/'+$scope.myform._id+'/submissions')
|
|
|
|
.success(function(data, status, headers){
|
2015-07-27 18:11:43 +00:00
|
|
|
// console.log(data[0].form_fields);
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
var _data = [];
|
2015-07-21 23:25:45 +00:00
|
|
|
for(var i=0; i<data.length; i++){
|
|
|
|
|
|
|
|
var _tmpSubFormFields = JSON.parse(JSON.stringify($scope.myform.form_fields));
|
|
|
|
|
|
|
|
for(var x=0; x<_tmpSubFormFields.length; x++){
|
|
|
|
|
|
|
|
var currField__id = _tmpSubFormFields[x]._id,
|
2015-07-27 18:11:43 +00:00
|
|
|
currField;
|
2015-07-21 23:25:45 +00:00
|
|
|
|
|
|
|
_.find(data[i].form_fields, function(fieldItem, fieldIdx){
|
|
|
|
if(fieldItem._id === currField__id){
|
|
|
|
currField = fieldItem;
|
2015-07-27 18:11:43 +00:00
|
|
|
// console.log(fieldItem.fieldValue);
|
2015-07-21 23:25:45 +00:00
|
|
|
return true;
|
2015-07-27 18:11:43 +00:00
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if(currField !== undefined){
|
|
|
|
_tmpSubFormFields[x].fieldValue = currField.fieldValue;
|
|
|
|
_tmpSubFormFields[x].$$hashKey = currField.$$hashKey;
|
|
|
|
}else {
|
|
|
|
_tmpSubFormFields[x].fieldValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_data[i] = data[i];
|
|
|
|
_data[i].form_fields = _tmpSubFormFields;
|
2015-07-27 18:11:43 +00:00
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
// console.log(JSON.stringify(_data));
|
2015-07-21 23:25:45 +00:00
|
|
|
$scope.submissions = _data;
|
|
|
|
$scope.table.rows = _data;
|
2015-07-27 18:11:43 +00:00
|
|
|
if(!$scope.$$phase && !$scope.$digest){
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log('form submissions successfully fetched');
|
|
|
|
// console.log( JSON.parse(JSON.stringify($scope.submissions)) ) ;
|
|
|
|
// console.log( JSON.parse(JSON.stringify($scope.myform.form_fields)) );
|
|
|
|
})
|
|
|
|
.error(function(err){
|
|
|
|
console.log('Could not fetch form submissions.\nError: '+err);
|
|
|
|
});
|
|
|
|
};
|
2015-07-02 04:54:46 +00:00
|
|
|
//hide submissions of Form
|
|
|
|
$scope.hideSubmissions = function(){
|
|
|
|
$scope.viewSubmissions = false;
|
2015-07-03 01:14:48 +00:00
|
|
|
};
|
2015-07-02 04:54:46 +00:00
|
|
|
|
|
|
|
// Remove existing Form
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.remove = function(form_id) {
|
|
|
|
var form = {};
|
|
|
|
if(!form_id){
|
|
|
|
form = CurrentForm.getForm();
|
|
|
|
if(!form) form = $scope.myform;
|
|
|
|
}else {
|
|
|
|
form._id = form_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$http.delete('/forms/'+form._id)
|
2015-07-02 04:54:46 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form deleted successfully');
|
2015-07-07 01:21:43 +00:00
|
|
|
|
|
|
|
if(!form_id){
|
|
|
|
$state.go('listForms');
|
|
|
|
}
|
|
|
|
if($scope.myforms.length > 0){
|
|
|
|
$scope.myforms = _.filter($scope.myforms, function(myform){
|
|
|
|
return myform._id !== form._id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
}).error(function(error){
|
|
|
|
console.log('ERROR: Form could not be deleted.');
|
|
|
|
console.error(error);
|
|
|
|
});
|
2015-07-06 04:29:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create new Form
|
2015-07-21 23:25:45 +00:00
|
|
|
$scope.createNew = function(){
|
|
|
|
var form = {};
|
2015-07-27 18:11:43 +00:00
|
|
|
form.title = $scope.myform.name.$modelValue;
|
|
|
|
form.language = $scope.myform.language.$modelValue;
|
2015-07-21 23:25:45 +00:00
|
|
|
console.log(form);
|
|
|
|
$scope.showCreateModal = true;
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
console.log($scope.myform);
|
|
|
|
if($scope.myform.$valid && $scope.myform.$dirty){
|
2015-07-21 23:25:45 +00:00
|
|
|
$http.post('/forms', {form: form})
|
2015-07-06 04:29:05 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form created');
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
// Clear form fields
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope.myform = {};
|
2015-07-06 04:29:05 +00:00
|
|
|
// Redirect after save
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope.goToWithId('viewForm', data._id+'');
|
2015-07-06 04:29:05 +00:00
|
|
|
}).error(function(errorResponse){
|
2015-07-21 23:25:45 +00:00
|
|
|
console.log(errorResponse);
|
2015-07-27 18:11:43 +00:00
|
|
|
$scope.error = errorResponse.data.message;
|
2015-07-06 04:29:05 +00:00
|
|
|
});
|
|
|
|
}
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// Update existing Form
|
|
|
|
$scope.update = $rootScope.update = function(cb) {
|
2015-07-27 18:11:43 +00:00
|
|
|
if(!$rootScope.saveInProgress){
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
$rootScope.saveInProgress = true;
|
|
|
|
// console.log('begin updating form');
|
|
|
|
var err = null;
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
$http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
|
|
|
.then(function(response){
|
2015-07-27 18:11:43 +00:00
|
|
|
$rootScope.myform = $scope.myform = response.data;
|
|
|
|
console.log(response.data);
|
|
|
|
if(!$scope.$digest){
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
}).catch(function(response){
|
|
|
|
console.log('Error occured during form UPDATE.\n');
|
|
|
|
console.log(response.data);
|
|
|
|
err = response.data;
|
|
|
|
}).finally(function() {
|
|
|
|
// console.log('finished updating');
|
2015-07-27 18:11:43 +00:00
|
|
|
$rootScope.saveInProgress = false;
|
2015-07-21 23:25:45 +00:00
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
2015-07-06 04:29:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$rootScope.resetForm = function(){
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myform = Forms.get({
|
2015-07-06 04:29:05 +00:00
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
2015-07-07 01:21:43 +00:00
|
|
|
};
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
]);
|