Skip to content

Commit

Permalink
Allow removal of category only when no items
Browse files Browse the repository at this point in the history
Only remove category when there are no items inside the category
  • Loading branch information
jessedobbelaere committed Feb 16, 2015
1 parent f96e5eb commit 5ac1806
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Backend/Modules/Menu/Actions/EditCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ protected function parse()

// assign the data
$this->tpl->assign('item', $this->record);

if (BackendMenuModel::getItemsInCategory($this->record['id']) == 0) {
$this->tpl->assign('emptyCategory', true);
}
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/Backend/Modules/Menu/Engine/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public static function deleteAlacarte($id)
public static function deleteCategory($id)
{
$db = BackendModel::get('database');
$item = self::getCategory($id);
$item = self::existsCategory($id);

if (!empty($item)) {
$db->delete('meta', 'id = ?', array($item['meta_id']));
$db->delete('menu_categories', 'id = ?', array((int) $id));
$db->update('menu', array('category_id' => null), 'category_id = ?', array((int) $id));
$db->update('menu_alacarte', array('category_id' => null), 'category_id = ?', array((int) $id));
}
}

Expand Down Expand Up @@ -460,4 +460,20 @@ public static function updateCategory(array $item)
'menu_categories', $item, 'id = ?', array($item['id'])
);
}

/**
* Get the number of items in a category
*
* @param $categoryId
* @return int Number of items in category
*/
public static function getItemsInCategory($categoryId)
{
return (int) BackendModel::get('database')->getVar(
'SELECT COUNT(*)
FROM menu_alacarte
WHERE category_id = ?',
array((int) $categoryId)
);
}
}
4 changes: 4 additions & 0 deletions src/Backend/Modules/Menu/Installer/Data/locale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
<translation language="en"><![CDATA[no categories]]></translation>
<translation language="nl"><![CDATA[geen categorieën]]></translation>
</item>
<item type="message" name="EmptyCategoriesFirst">
<translation language="en"><![CDATA[Verwijder alle gerechten in de categorie voordat u deze kunt verwijderen.]]></translation>
<translation language="nl"><![CDATA[Remove all the dishes in the category before you can remove this.]]></translation>
</item>
</Core>
</Backend>
<Frontend>
Expand Down
2 changes: 1 addition & 1 deletion src/Backend/Modules/Menu/Layout/Templates/Categories.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{$dataGrid}
</div>
{/option:dataGrid}
{option:!dataGrid}{$msgNoCategories}{/option:!dataGrid}
{option:!dataGrid}{$msgNoCategories|ucfirst}{/option:!dataGrid}

{include:{$BACKEND_CORE_PATH}/Layout/Templates/StructureEndModule.tpl}
{include:{$BACKEND_CORE_PATH}/Layout/Templates/Footer.tpl}
5 changes: 5 additions & 0 deletions src/Backend/Modules/Menu/Layout/Templates/EditCategory.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</div>

<div class="fullwidthOptions">
{option:emptyCategory}
<a href="{$var|geturl:'delete_category'}&amp;id={$item.id}" data-message-id="confirmDelete" class="askConfirmation button linkButton icon iconDelete">
<span>{$lblDelete|ucfirst}</span>
</a>
Expand All @@ -37,6 +38,10 @@
{$msgConfirmDeleteCategory|sprintf:{$item.title}}
</p>
</div>
{/option:emptyCategory}
{option:!emptyCategory}
{$msgEmptyCategoriesFirst|ucfirst}
{/option:!emptyCategory}

<div class="buttonHolderRight">
<input id="editButton" class="inputButton button mainButton" type="submit" name="edit" value="{$lblSave|ucfirst}" />
Expand Down

0 comments on commit 5ac1806

Please sign in to comment.