diff --git a/include/library/Crunchbutton/Restaurant.php b/include/library/Crunchbutton/Restaurant.php index e208d81bd..84481a0f7 100644 --- a/include/library/Crunchbutton/Restaurant.php +++ b/include/library/Crunchbutton/Restaurant.php @@ -212,6 +212,8 @@ class Crunchbutton_Restaurant extends Cana_Table */ public function saveCategories($rawData) { + $originalCategories = $this->categories(); + $doNotDelete = []; if ($rawData) { foreach ($rawData as $data) { // if (!$data['name']) continue; @@ -220,8 +222,16 @@ class Crunchbutton_Restaurant extends Cana_Table $element->name = $data['name']; $element->sort = $data['sort']; $element->save(); + $doNotDelete[] = $element->id_category; } } + foreach($originalCategories as $toDelete) { + /* @var $toDelete Crunchbutton_Category */ + if (!in_array($toDelete->id_category, $doNotDelete)) { + $toDelete->delete(); + } + } + $this->_categories = null; $elements = $this->categories(); return $elements; diff --git a/include/views/default/crunchbutton/admin/orders/index.phtml b/include/views/default/crunchbutton/admin/orders/index.phtml index 44f25977c..b16f3c86b 100644 --- a/include/views/default/crunchbutton/admin/orders/index.phtml +++ b/include/views/default/crunchbutton/admin/orders/index.phtml @@ -93,7 +93,7 @@ +
diff --git a/www/assets/js/admin.js b/www/assets/js/admin.js index 3151584f9..90a19a870 100644 --- a/www/assets/js/admin.js +++ b/www/assets/js/admin.js @@ -19,6 +19,49 @@ var App = { _pageInit: false }; + +function _deleteCategoryDialog(){ + $('body').on('click', '.jqui-button.button-delete', function(){ + var category_id = $(this).parent('.ui-accordion-content').prev('h3').data().id_category; + var categories = App.restaurantObject.categories(); + var catOptions = ''; + for(var i = 0; i < categories.length; i++) { + var disabled = false + if (categories[i].id_category == category_id) { + disabled = ' disabled="disabled" '; + } + catOptions += ''; + } + + + var html = '
' + + '

You are about to delete this category. Existing dishes in the category will be moved to another category.

' + + '
' + + '

Are you sure you want to delete the category?

' + + '
'; + $(html).dialog({ + resizable: false, + height: 200, + width: 400, + modal: true, + buttons: { + Delete: function() { + var to_id = $('select', this).val(); + deleteCategory(category_id, to_id); + $(this).dialog('close'); + $(this).remove(); + }, + Cancel: function() { + $(this).dialog('close'); + $(this).remove(); + } + } + }); + }); +} + + /** * Populates notifications types with empty fields to be inserted * @@ -126,6 +169,7 @@ function _loadRestaurant() { * Swip all categories * * @todo Use the App.createCategory + * @todo Encapsulate the categories loading in a private _loadCategories() method */ for (var i in categories) { var dishes = categories[i].dishes(); @@ -133,6 +177,7 @@ function _loadRestaurant() { var sort = (categories[i].sort && (categories[i].sort != 'null')) ? categories[i].sort : 0; var $categoryTab = $('

'+ name+'

' + '
' + + '
'+ '
' + '' + '' + @@ -158,6 +203,8 @@ function _loadRestaurant() { }, 1.1 * speed); } }); + _deleteCategoryDialog(); + // end of loadingCategories; if (!isDishes) { $('input[name="dish_check"][value="0"]').prop('checked', true); @@ -340,7 +387,7 @@ function saveDishes (complete) { }); // Just to make sure that the name was typed and the user don't clicked at 'Add another dish?' by mistake. if( dish.name && dish.name != 'null' && dish.name != '' ){ - dishes[dishes.length] = dish; + dishes[dishes.length] = dish; } }); @@ -492,6 +539,16 @@ function saveHours (complete) { } }; +function deleteCategory(from_id, to_id) { + var $toDelete = $('h3[data-id_category="'+from_id+'"]'); + var dishes = $('.admin-food-item-wrap', $toDelete.next()); + $('select[name="dish-id_category"]').val(to_id); + $('h3[data-id_category="'+to_id+'"]').next().append(dishes); + $toDelete.next().remove(); + $toDelete.remove(); +} + + function getValues(selector, restaurant) { $(selector).each(function() { var name, value, group = false; @@ -1142,7 +1199,7 @@ $(function() { }, Cancel: function() { $(this).dialog('close'); - $(this).find('[admin-category-name"]').val(''); + $(this).find('[name="admin-category-name"]').val(''); } } });