2013-07-17 19:29:51 +03:00
|
|
|
/**
|
|
|
|
* This namespace handles the js functionality of the backend services page.
|
|
|
|
*
|
|
|
|
* @namespace BackendServices
|
|
|
|
*/
|
|
|
|
var BackendServices = {
|
|
|
|
/**
|
|
|
|
* Contains the basic record methods for the page.
|
|
|
|
*
|
|
|
|
* @type ServicesHelper|CategoriesHelper
|
|
|
|
*/
|
|
|
|
helper: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default initialize method of the page.
|
|
|
|
*
|
|
|
|
* @param {bool} bindEventHandlers (OPTIONAL) Determines whether to bind the
|
|
|
|
* default event handlers (default: true).
|
|
|
|
*/
|
|
|
|
initialize: function(bindEventHandlers) {
|
|
|
|
if (bindEventHandlers === undefined) bindEventHandlers = true;
|
|
|
|
|
|
|
|
// Fill available service categories listbox.
|
|
|
|
$.each(GlobalVariables.categories, function(index, category) {
|
2013-07-18 00:10:42 +03:00
|
|
|
var option = new Option(category.name, category.id);
|
2013-07-17 19:29:51 +03:00
|
|
|
$('#service-category').append(option);
|
|
|
|
});
|
|
|
|
$('#service-category').append(new Option('- No Category -', null)).val('null');
|
|
|
|
|
|
|
|
// Instantiate helper object (service helper by default).
|
|
|
|
BackendServices.helper = new ServicesHelper();
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter('');
|
|
|
|
|
|
|
|
$('#service-duration').spinner({
|
|
|
|
'min': 0,
|
2013-07-18 00:10:42 +03:00
|
|
|
'disabled': true //default
|
2013-07-17 19:29:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (bindEventHandlers) BackendServices.bindEventHandlers();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the default event handlers of the backend services page. Do not use this method
|
|
|
|
* if you include the "BackendServices" namespace on another page.
|
|
|
|
*/
|
|
|
|
bindEventHandlers: function() {
|
|
|
|
/**
|
|
|
|
* Event: Page Tab Button "Click"
|
|
|
|
*
|
|
|
|
* Changes the displayed tab.
|
|
|
|
*/
|
|
|
|
$('.tab').click(function() {
|
|
|
|
$('.active').removeClass('active');
|
|
|
|
$(this).addClass('active');
|
|
|
|
$('.tab-content').hide();
|
|
|
|
|
|
|
|
if ($(this).hasClass('services-tab')) { // display services tab
|
|
|
|
$('#services').show();
|
|
|
|
BackendServices.helper = new ServicesHelper();
|
|
|
|
} else if ($(this).hasClass('categories-tab')) { // display categories tab
|
|
|
|
$('#categories').show();
|
|
|
|
BackendServices.helper = new CategoriesHelper();
|
|
|
|
}
|
2013-07-19 18:29:59 +03:00
|
|
|
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter('');
|
|
|
|
$('.filter-key').val('');
|
2013-07-17 19:29:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Filter Services Button "Click"
|
|
|
|
*/
|
2013-07-18 00:10:42 +03:00
|
|
|
$('.filter-services').click(function() {
|
2013-07-17 19:29:51 +03:00
|
|
|
var key = $('#services .filter-key').val();
|
|
|
|
$('.selected-row').removeClass('selected-row');
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter(key);
|
|
|
|
});
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Categories Button "Click"
|
|
|
|
*/
|
|
|
|
$('.filter-categories').click(function() {
|
|
|
|
var key = $('#categories .filter-key').val();
|
|
|
|
$('.selected-row').removeClass('selected-row');
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter(key);
|
|
|
|
});
|
|
|
|
|
2013-07-17 19:29:51 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Service Row "Click"
|
|
|
|
*
|
|
|
|
* Display the selected service data to the user.
|
|
|
|
*/
|
|
|
|
$(document).on('click', '.service-row', function() {
|
2013-07-19 18:29:59 +03:00
|
|
|
if ($('#services .filter-services').prop('disabled')) {
|
|
|
|
$('#services .filter-results').css('color', '#AAA');
|
|
|
|
return; // exit because we are on edit mode
|
|
|
|
}
|
2013-07-17 19:29:51 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
var service = { 'id': $(this).attr('data-id') };
|
2013-07-17 19:29:51 +03:00
|
|
|
$.each(BackendServices.helper.filterResults, function(index, item) {
|
|
|
|
if (item.id === service.id) {
|
|
|
|
service = item;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
BackendServices.helper.display(service);
|
|
|
|
$('.selected-row').removeClass('selected-row');
|
|
|
|
$(this).addClass('selected-row');
|
2013-07-18 00:10:42 +03:00
|
|
|
$('#edit-service, #delete-service').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Categories Row "Click"
|
|
|
|
*
|
|
|
|
* Displays the selected row data on the right side of the page.
|
|
|
|
*/
|
|
|
|
$(document).on('click', '.category-row', function() {
|
|
|
|
if ($('#categories .filter-categories').prop('disabled')) {
|
|
|
|
$('#categories .filter-results').css('color', '#AAA');
|
|
|
|
return; // exit because we are on edit mode
|
|
|
|
}
|
|
|
|
var category = { 'id': $(this).attr('data-id') };
|
|
|
|
$.each(BackendServices.helper.filterResults, function(index, item) {
|
|
|
|
if (item.id === category.id) {
|
|
|
|
category = item;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
BackendServices.helper.display(category);
|
|
|
|
$('.selected-row').removeClass('selected-row');
|
|
|
|
$(this).addClass('selected-row');
|
|
|
|
$('#edit-category, #delete-category').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
/**
|
|
|
|
* Event: Add New Service Button "Click"
|
|
|
|
*/
|
|
|
|
$('#add-service').click(function() {
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
$('#services .add-edit-delete-group').hide();
|
|
|
|
$('#services .save-cancel-group').show();
|
|
|
|
$('#services .details').find('input, textarea').prop('readonly', false);
|
|
|
|
$('#services .details').find('select').prop('disabled', false);
|
|
|
|
$('#service-duration').spinner('enable');
|
2013-07-19 18:29:59 +03:00
|
|
|
$('#services .filter-services').prop('disabled', true);
|
|
|
|
$('#services .filter-results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Add Category Button "Click"
|
|
|
|
*/
|
|
|
|
$('#add-category').click(function() {
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
$('#categories .add-edit-delete-group').hide();
|
|
|
|
$('#categories .save-cancel-group').show();
|
|
|
|
$('#categories .details').find('input, textarea').prop('readonly', false);
|
|
|
|
$('#categories .filter-categories').prop('disabled', true);
|
|
|
|
$('#categories .filter-results').css('color', '#AAA');
|
2013-07-18 00:10:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Cancel Service Button "Click"
|
|
|
|
*
|
|
|
|
* Cancel add or edit of a service record.
|
|
|
|
*/
|
|
|
|
$('#cancel-service').click(function() {
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
});
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* Event: Cancel Category Button "Click"
|
|
|
|
*/
|
|
|
|
$('#cancel-category').click(function() {
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
});
|
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
/**
|
|
|
|
* Event: Save Service Button "Click"
|
|
|
|
*/
|
|
|
|
$('#save-service').click(function() {
|
|
|
|
var service = {
|
|
|
|
'name': $('#service-name').val(),
|
|
|
|
'duration': $('#service-duration').val(),
|
|
|
|
'price': $('#service-price').val(),
|
|
|
|
'currency': $('#service-currency').val(),
|
|
|
|
'description': $('#service-description').val()
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#service-category').val() !== 'null') {
|
|
|
|
service.id_service_categories = $('#service-category').val();
|
|
|
|
} else {
|
|
|
|
service.id_service_categories = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($('#service-id').val() !== '') {
|
|
|
|
service.id = $('#service-id').val();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BackendServices.helper.validate(service)) return;
|
|
|
|
|
|
|
|
BackendServices.helper.save(service);
|
|
|
|
});
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* Event: Categories Save Button "Click"
|
|
|
|
*/
|
|
|
|
$('#save-category').click(function() {
|
|
|
|
var category = {
|
|
|
|
'name': $('#category-name').val(),
|
|
|
|
'description': $('#category-description').val()
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#category-id').val() !== '') {
|
|
|
|
category.id = $('#category-id').val();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BackendServices.helper.validate(category)) return;
|
|
|
|
|
|
|
|
BackendServices.helper.save(category);
|
|
|
|
});
|
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
/**
|
|
|
|
* Event: Edit Service Button "Click"
|
|
|
|
*/
|
|
|
|
$('#edit-service').click(function() {
|
|
|
|
$('#services .add-edit-delete-group').hide();
|
|
|
|
$('#services .save-cancel-group').show();
|
|
|
|
$('.filter-services').prop('disabled', true);
|
2013-07-19 18:29:59 +03:00
|
|
|
$('#services .filter-results').css('color', '#AAA');
|
2013-07-18 00:10:42 +03:00
|
|
|
$('#services .details').find('input, textarea').prop('readonly', false);
|
|
|
|
$('#services .details select').prop('disabled', false);
|
|
|
|
$('#service-duration').spinner('enable');
|
|
|
|
});
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* Event: Edit Category Button "Click"
|
|
|
|
*/
|
|
|
|
$('#edit-category').click(function() {
|
|
|
|
$('#categories .add-edit-delete-group').hide();
|
|
|
|
$('#categories .save-cancel-group').show();
|
|
|
|
$('.filter-categories').prop('disabled', true);
|
|
|
|
$('#categories .filter-results').css('color', '#AAA');
|
|
|
|
$('#categories .details').find('input, textarea').prop('readonly', false);
|
|
|
|
});
|
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
/**
|
|
|
|
* Event: Delete Service Button "Click"
|
|
|
|
*/
|
|
|
|
$('#delete-service').click(function() {
|
|
|
|
var serviceId = $('#service-id').val();
|
|
|
|
|
|
|
|
var messageBtns = {
|
|
|
|
'Delete': function() {
|
|
|
|
BackendServices.helper.delete(serviceId);
|
|
|
|
$('#message_box').dialog('close');
|
|
|
|
},
|
|
|
|
'Cancel': function() {
|
|
|
|
$('#message_box').dialog('close');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GeneralFunctions.displayMessageBox('Delete Service', 'Are you sure that you want '
|
|
|
|
+ 'to delete this record? This action cannot be undone.', messageBtns);
|
2013-07-17 19:29:51 +03:00
|
|
|
});
|
2013-07-19 18:29:59 +03:00
|
|
|
|
|
|
|
$('#delete-category').click(function() {
|
|
|
|
var categoryId = $('#category-id').val();
|
|
|
|
|
|
|
|
var messageBtns = {
|
|
|
|
'Delete': function() {
|
|
|
|
BackendServices.helper.delete(categoryId);
|
|
|
|
$('#message_box').dialog('close');
|
|
|
|
},
|
|
|
|
'Cancel': function() {
|
|
|
|
$('#message_box').dialog('close');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GeneralFunctions.displayMessageBox('Delete Category', 'Are you sure that you want '
|
|
|
|
+ 'to delete this record? This action cannot be undone.', messageBtns);
|
|
|
|
});
|
2013-07-17 19:29:51 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class contains the methods that will be used by the "Services" tab of the page.
|
|
|
|
* @class ServicesHelper
|
|
|
|
*/
|
|
|
|
var ServicesHelper = function() {
|
|
|
|
this.filterResults = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save service record to database.
|
|
|
|
*
|
|
|
|
* @param {object} service Contains the service record data. If an 'id' value is provided
|
|
|
|
* then the update operation is going to be executed.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.save = function(service) {
|
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_save_service';
|
|
|
|
var postData = { 'service': JSON.stringify(service) };
|
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
console.log('Save Service Response:', response);
|
|
|
|
if (!Backend.handleAjaxExceptions(response)) return;
|
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
Backend.displayNotification('Service saved successfully!');
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter($('#services .filter-key').val());
|
|
|
|
}, 'json');
|
2013-07-17 19:29:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a service records from database.
|
|
|
|
*
|
|
|
|
* @param {int} id Record id to be deleted.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.delete = function(id) {
|
2013-07-18 00:10:42 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_delete_service';
|
|
|
|
var postData = { 'service_id': id };
|
2013-07-17 19:29:51 +03:00
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
console.log('Delete service response:', response);
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if (!Backend.handleAjaxExceptions(response)) return;
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
Backend.displayNotification('Service deleted successfully!');
|
2013-07-18 00:10:42 +03:00
|
|
|
|
|
|
|
BackendServices.helper.resetForm();
|
2013-07-19 18:29:59 +03:00
|
|
|
BackendServices.helper.filter($('#services .filter-key').val());
|
2013-07-18 00:10:42 +03:00
|
|
|
});
|
2013-07-17 19:29:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a service record.
|
|
|
|
*
|
|
|
|
* @param {object} service Contains the service data.
|
|
|
|
* @returns {bool} Returns the validation result.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.validate = function(service) {
|
2013-07-18 00:10:42 +03:00
|
|
|
$('#services .required').css('border', '');
|
2013-07-17 19:29:51 +03:00
|
|
|
|
2013-07-18 00:10:42 +03:00
|
|
|
try {
|
|
|
|
// validate required fields.
|
|
|
|
var missingRequired = false;
|
|
|
|
$('#services .required').each(function() {
|
|
|
|
if ($(this).val() == '' || $(this).val() == undefined) {
|
|
|
|
$(this).css('border', '2px solid red');
|
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (missingRequired) {
|
|
|
|
throw 'Fields with * are required.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch(exc) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 19:29:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the service tab form back to its initial state.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.resetForm = function() {
|
|
|
|
$('#services .details').find('input, textarea').val('');
|
|
|
|
$('#service-category').val('null');
|
|
|
|
$('#services .add-edit-delete-group').show();
|
|
|
|
$('#services .save-cancel-group').hide();
|
|
|
|
$('#edit-service, #delete-service').prop('disabled', true);
|
|
|
|
$('#services .details').find('input, textarea').prop('readonly', true);
|
|
|
|
$('#service-category').prop('disabled', true);
|
2013-07-18 00:10:42 +03:00
|
|
|
$('.filter-services').prop('disabled', false);
|
2013-07-19 18:29:59 +03:00
|
|
|
$('#services .filter-results').css('color', '');
|
2013-07-17 19:29:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a service record into the service form.
|
|
|
|
*
|
|
|
|
* @param {object} service Contains the service record data.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.display = function(service) {
|
|
|
|
$('#service-id').val(service.id);
|
|
|
|
$('#service-name').val(service.name);
|
2013-07-18 00:10:42 +03:00
|
|
|
$('#service-duration').val(service.duration);
|
2013-07-17 19:29:51 +03:00
|
|
|
$('#service-price').val(service.price);
|
|
|
|
$('#service-currency').val(service.currency);
|
|
|
|
$('#service-description').val(service.description);
|
2013-07-18 00:10:42 +03:00
|
|
|
|
|
|
|
var categoryId = (service.id_service_categories != null) ? service.id_service_categories : 'null';
|
|
|
|
$('#service-category').val(categoryId);
|
2013-07-17 19:29:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters service records depending a string key.
|
|
|
|
*
|
|
|
|
* @param {string} key This is used to filter the service records of the database.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.filter = function(key) {
|
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_filter_services';
|
|
|
|
var postData = { 'key': key };
|
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
console.log('Filter services response:', response);
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if (!Backend.handleAjaxExceptions(response)) return;
|
|
|
|
|
|
|
|
BackendServices.helper.filterResults = response;
|
|
|
|
$('#services .filter-results').html('');
|
|
|
|
|
|
|
|
$.each(response, function(index, service) {
|
|
|
|
var html = ServicesHelper.prototype.getFilterHtml(service);
|
|
|
|
$('#services .filter-results').append(html);
|
|
|
|
});
|
|
|
|
}, 'json');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a service row html code that is going to be displayed on the filter results list.
|
|
|
|
*
|
|
|
|
* @param {object} service Contains the service record data.
|
|
|
|
* @returns {string} The html code that represents the record on the filter results list.
|
|
|
|
*/
|
|
|
|
ServicesHelper.prototype.getFilterHtml = function(service) {
|
|
|
|
var html =
|
|
|
|
'<div class="service-row" data-id="' + service.id + '">' +
|
|
|
|
'<strong>' + service.name + '</strong><br>' +
|
|
|
|
service.duration + ' min - ' +
|
|
|
|
service.price + ' ' + service.currency + '<br>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class contains the core method implementations that belong to the categories tab
|
|
|
|
* of the backend services page.
|
|
|
|
*
|
|
|
|
* @class CategoriesHelper
|
|
|
|
*/
|
|
|
|
var CategoriesHelper = function() {
|
|
|
|
this.filterResults = {};
|
2013-07-18 00:10:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter service categories records.
|
|
|
|
*
|
|
|
|
* @param {string} key This key string is used to filter the category records.
|
|
|
|
*/
|
|
|
|
CategoriesHelper.prototype.filter = function(key) {
|
2013-07-19 18:29:59 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_filter_service_categories';
|
2013-07-18 00:10:42 +03:00
|
|
|
var postData = { 'key': key };
|
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
console.log('Filter Categories Response:', response);
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if (!Backend.handleAjaxExceptions(response)) return;
|
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
BackendServices.helper.filterResults = response;
|
2013-07-18 00:10:42 +03:00
|
|
|
$('#categories .filter-results').html('');
|
|
|
|
$.each(response, function(index, category) {
|
|
|
|
var html = BackendServices.helper.getFilterHtml(category);
|
|
|
|
$('#categories .filter-results').append(html);
|
|
|
|
});
|
|
|
|
|
|
|
|
}, 'json');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a category record to the database (via ajax post).
|
|
|
|
*
|
|
|
|
* @param {object} category Contains the category data.
|
|
|
|
*/
|
|
|
|
CategoriesHelper.prototype.save = function(category) {
|
2013-07-19 18:29:59 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_save_service_category';
|
|
|
|
var postData = { 'category': JSON.stringify(category) };
|
2013-07-18 00:10:42 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
console.log('Save Service Category Response:', response);
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if (!Backend.handleAjaxExceptions(response)) return;
|
|
|
|
|
|
|
|
Backend.displayNotification('Service saved successfully!');
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter($('#categories .filter-key').val());
|
|
|
|
});
|
2013-07-18 00:10:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete category record.
|
|
|
|
*
|
|
|
|
* @param {int} id Record id to be deleted.
|
|
|
|
*/
|
|
|
|
CategoriesHelper.prototype.delete = function(id) {
|
2013-07-19 18:29:59 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_delete_service_category';
|
|
|
|
var postData = { 'category_id': id };
|
2013-07-18 00:10:42 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
console.log('Delete category response:', response);
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
if (!Backend.handleAjaxExceptions(response)) return;
|
|
|
|
|
|
|
|
Backend.displayNotification('Category deleted successfully!');
|
|
|
|
|
|
|
|
BackendServices.helper.resetForm();
|
|
|
|
BackendServices.helper.filter($('#categories .filter-key').val());
|
|
|
|
});
|
2013-07-18 00:10:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a category record on the form.
|
|
|
|
*
|
|
|
|
* @param {object} category Contains the category data.
|
|
|
|
*/
|
|
|
|
CategoriesHelper.prototype.display = function(category) {
|
2013-07-19 18:29:59 +03:00
|
|
|
$('#category-id').val(category.id);
|
|
|
|
$('#category-name').val(category.name);
|
|
|
|
$('#category-description').val(category.description);
|
2013-07-18 00:10:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate category data before save (insert or update).
|
|
|
|
*
|
|
|
|
* @param {object} category Contains the category data.
|
|
|
|
*/
|
|
|
|
CategoriesHelper.prototype.validate = function(category) {
|
2013-07-19 18:29:59 +03:00
|
|
|
$('#categories .details').find('input, textarea').css('border', '');
|
2013-07-18 00:10:42 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
try {
|
|
|
|
var missingRequired = false;
|
|
|
|
$('#categories .required').each(function() {
|
|
|
|
if ($(this).val() == '' || $(this).val() == undefined) {
|
|
|
|
$(this).css('border', '2px solid red');
|
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (missingRequired) throw 'Required field is missing.';
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch(exc) {
|
|
|
|
console.log('Category Record Validation Exc:', exc);
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-18 00:10:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bring the category form back to its initial state.
|
|
|
|
*/
|
|
|
|
CategoriesHelper.prototype.resetForm = function() {
|
2013-07-19 18:29:59 +03:00
|
|
|
$('#categories .add-edit-delete-group').show();
|
|
|
|
$('#categories .save-cancel-group').hide();
|
|
|
|
$('#categories .details').find('input, textarea').val('');
|
|
|
|
$('#categories .details').find('input, textarea').prop('readonly', true);
|
|
|
|
$('#edit-category, #delete-category').prop('disabled', true);
|
|
|
|
$('#categories .filter-results').css('color', '');
|
|
|
|
$('#categories .filter-categories').prop('disabled', false);
|
2013-07-18 00:10:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the filter results row html code.
|
|
|
|
*
|
|
|
|
* @param {object} category Contains the category data.
|
|
|
|
* @return {string} Returns the record html code.
|
|
|
|
*/
|
2013-07-19 18:29:59 +03:00
|
|
|
CategoriesHelper.prototype.getFilterHtml = function(category) {
|
2013-07-18 00:10:42 +03:00
|
|
|
var html =
|
|
|
|
'<div class="category-row" data-id="' + category.id + '">' +
|
2013-07-19 18:29:59 +03:00
|
|
|
'<strong>' + category.name + '</strong>' +
|
2013-07-18 00:10:42 +03:00
|
|
|
'</div>';
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|