@@ -, +, @@
---
.../intranet-tmpl/prog/en/modules/admin/cities.tt | 176 ++++++++++++++-------
1 file changed, 118 insertions(+), 58 deletions(-)
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt
+++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt
@@ -1,3 +1,32 @@
+[% BLOCK city_form %]
+
+
+
+
+[% END %]
+
[% INCLUDE 'doc-head-open.inc' %]
Koha › Administration › [% IF op =='add_form' %]Cities › [% IF city.cityid %] Modify city[% ELSE %] New city[% END %][% ELSE %][% IF op == 'delete_confirm' %]Cities › Confirm deletion of city[% ELSE %] Cities[% END %][% END %]
[% INCLUDE 'doc-head-close.inc' %]
@@ -28,8 +57,8 @@
this.city_state,
this.city_zipcode,
this.city_country,
- "" + _("Edit") + "",
- "" + _("Delete") + ""
+ "" + _("Edit") + "",
+ "" + _("Delete") + ""
]);
});
json.iTotalRecords = data.length;
@@ -40,11 +69,6 @@
}
});
},
- 'fnRowCallback': function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
- $("td:eq(6)", nRow).find('a').on('click', function() {
- return delete_city($(this).attr('data-cityid'));
- });
- },
// Should add filter/search/pagination
// Not supported yet
'bAutoWidth': false,
@@ -64,53 +88,95 @@
};
// Double check on required fields should be avoided
if ( !city.city_name || !city.city_zipcode ) return;
- cityid ? update_city(city) : add_city(city);
+ var jqxhr = cityid ? update_city(city) : add_city(city);
+ jqxhr.done(function() {
+ $('#city-form').modal('hide');
+ $('#table_cities').dataTable().fnDraw();
+
+ var message;
+ if (cityid) {
+ message = _("The city was successfully updated");
+ } else {
+ message = _("The city was successfully added");
+ }
+ display_message(message);
+ });
+ jqxhr.fail(function(data) {
+ display_error(data);
+ });
+ });
+
+ $('#newcity').on('click', function(e) {
+ e.preventDefault();
+ $('#city-form').modal({show: true});
+ })
+
+ $('#table_cities').on('click', '.edit', function(e) {
+ e.preventDefault();
+ var row = $(this).parents('tr');
+ // FIXME There must be a better way to retrieve city data
+ var city = {
+ id: row.find('td:eq(0)').text(),
+ name: row.find('td:eq(1)').text(),
+ state: row.find('td:eq(2)').text(),
+ zipcode: row.find('td:eq(3)').text(),
+ country: row.find('td:eq(4)').text(),
+ };
+ var form = $('#city-form').find('form');
+ form.find('[name="cityid"]').val(city.id);
+ form.find('[name="city_name"]').val(city.name);
+ form.find('[name="city_state"]').val(city.state);
+ form.find('[name="city_zipcode"]').val(city.zipcode);
+ form.find('[name="city_country"]').val(city.country);
+ $('#city-form').modal({show: true});
+ });
+ $('#table_cities').on('click', '.delete', function(e) {
+ e.preventDefault();
+ delete_city($(this).attr('data-cityid'))
+ .done(function(data){
+ $('#table_cities').dataTable().fnDraw();
+
+ display_message(_("The city was successfully deleted"));
+ })
+ .fail(function(data){
+ display_error(data);
+ });
});
});
// The 3 functions should be extracted somewhere else
function update_city(city) {
- $.ajax({
+ return $.ajax({
type: 'PUT',
url: '/api/v1/cities/' + city.cityid,
data: JSON.stringify(city),
- success: function(data){
- // Need a better way to redirect, we would like to inform the user that the action succeeded
- window.location.href='/cgi-bin/koha/admin/cities.pl';
- },
- error: function(data){
- // Need a better way to inform the user that the action has failed
- alert(data);
- }
});
}
function add_city(city) {
- $.ajax({
+ return $.ajax({
type: 'PUT',
url: '/api/v1/cities',
data: JSON.stringify(city),
- success: function(data){
- window.location.href='/cgi-bin/koha/admin/cities.pl';
- },
- error: function(data){
- alert(data);
- }
});
}
function delete_city(cityid) {
if ( confirm(_("Are you sure you want to delete this city?")) ) {
- $.ajax({
+ return $.ajax({
type: 'DELETE',
url: '/api/v1/cities/' + cityid,
- success: function(data){
- window.location.href='/cgi-bin/koha/admin/cities.pl';
- },
- error: function(data){
- alert(data);
- }
- })
+ });
}
}
+ function display_message(msg) {
+ var message = $('').addClass('dialog message').html(msg);
+ setTimeout(function() {message.fadeOut();}, 5000);
+ $('#messages').append(message);
+ }
+ function display_error(msg) {
+ var message = $('').addClass('dialog alert').html(msg);
+ setTimeout(function() {message.fadeOut();}, 5000);
+ $('#messages').append(message);
+ }
//]]>
@@ -164,32 +230,7 @@
[% END %]