Skip to content

Commit

Permalink
Use existing lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Sep 8, 2021
1 parent 1580c0b commit 0857e0a
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 261 deletions.
80 changes: 75 additions & 5 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,85 @@

namespace Thoughtco\Outofstock;

use Admin\Controllers\Categories;
use Admin\Controllers\Menus;
use Admin\Facades\AdminLocation;
use Carbon\Carbon;
use Event;
use Igniter\Local\Facades\Location;
use System\Classes\BaseExtension;
use Thoughtco\Outofstock\Models\Outofstock;
use Thoughtco\OutOfStock\Models\Settings;

class Extension extends BaseExtension
{
public function boot()
{
$this->extendListColumns();
$this->listenForMenuIsAvailableEvent();
}

private function extendListColumns()
{
Event::listen('admin.list.extendColumns', function ($widget) {

$type = '';

if ($widget->getController() instanceof Menus) {
$type = 'menus';
$primary_key = 'menu_id';
}

if ($widget->getController() instanceof Categories) {
$type = 'categories';
$primary_key = 'category_id';
}

if ($type == '')
return;

$widget->vars['out_of_stock'] = Outofstock::where([
'type' => $type,
])->where(function($subquery) {
$location_id = AdminLocation::getId();
$subquery->where(['location_id' => $location_id]);
if (!is_null($location_id))
$subquery->orWhereNull('location_id');
})
->get()
->pluck('type_id');

$widget->vars['out_of_stock_delays'] = collect(Settings::get('delay_times', []));
$widget->vars['out_of_stock_url'] = admin_url('thoughtco/outofstock/'.$type);
$widget->vars['out_of_stock_location'] = AdminLocation::getId();

$widget->addColumns([
$primary_key => [
'label' => '',
'type' => 'partial',
'sortable' => FALSE,
'path' => 'extensions/thoughtco/outofstock/views/form/stock_column',
],
]);

});
}

private function listenForMenuIsAvailableEvent()
{
$cache_menus = NULL;
$cache_menu_items = NULL;
$cache_categories = NULL;
Event::listen('admin.menu.isAvailable', function (&$model, $dateTime, $isAvailable) use (&$cache_menus, &$cache_menu_items, &$cache_categories) {

$location_id = Location::getId();

if (is_null($cache_menus))
$cache_menus = Outofstock::where(['location_id' => Location::getId(), 'type' => 'menus'])
$cache_menus = Outofstock::where(['type' => 'menus'])
->where(function($subquery) use ($location_id) {
$subquery->where(['location_id' => $location_id])
->orWhereNull('location_id');
})
->where(function ($subquery) {
return $subquery->whereNull('timeout')
->orWhere([
Expand All @@ -29,7 +91,11 @@ public function boot()
->pluck('type_id');

if (is_null($cache_menu_items))
$cache_menu_items = Outofstock::where(['location_id' => Location::getId(), 'type' => 'menuitems'])
$cache_menu_items = Outofstock::where(['type' => 'menuoptions'])
->where(function($subquery) use ($location_id) {
$subquery->where(['location_id' => $location_id])
->orWhereNull('location_id');
})
->where(function ($subquery) {
return $subquery->whereNull('timeout')
->orWhere([
Expand All @@ -40,7 +106,11 @@ public function boot()
->pluck('type_id');

if (is_null($cache_categories))
$cache_categories = Outofstock::where(['location_id' => Location::getId(), 'type' => 'categories'])
$cache_categories = Outofstock::where(['type' => 'categories'])
->where(function($subquery) use ($location_id) {
$subquery->where(['location_id' => $location_id])
->orWhereNull('location_id');
})
->where(function ($subquery) {
return $subquery->whereNull('timeout')
->orWhere([
Expand Down Expand Up @@ -78,8 +148,8 @@ public function registerNavigation()
'outofstock' => [
'priority' => 25,
'class' => 'pages',
'href' => admin_url('thoughtco/outofstock/menus'),
'title' => lang('lang:thoughtco.outofstock::default.text_title'),
'href' => admin_url('thoughtco/outofstock/menuoptions'),
'title' => lang('lang:thoughtco.outofstock::default.button_menuoptions'),
'permission' => 'Thoughtco.Outofstock.*',
],
],
Expand Down
23 changes: 0 additions & 23 deletions controllers/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,6 @@ class Categories extends \Admin\Classes\AdminController

protected $requiredPermissions = 'Thoughtco.Outofstock.*';

public function __construct()
{
parent::__construct();

AdminMenu::setContext('restaurant', 'outofstock');
Template::setTitle(lang('lang:thoughtco.outofstock::default.text_title'));
}

public function index()
{
$this->vars['noLocation'] = false;

if (!AdminLocation::getId())
$this->vars['noLocation'] = true;

Outofstock::where([
['type', '=', 'menuitems'],
['timeout', '<', Carbon::now()->format('Y-m-d H:i:s')]
])->delete();

$this->asExtension('ListController')->index();
}

public function stock($context, $id = null)
{
if (!$id)
Expand Down
47 changes: 32 additions & 15 deletions controllers/Menuitems.php → controllers/Menuoptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
use Redirect;
use Template;
use Thoughtco\Outofstock\Models\Outofstock;
use Thoughtco\OutOfStock\Models\Settings;

class Menuitems extends \Admin\Classes\AdminController
class Menuoptions extends \Admin\Classes\AdminController
{
public $implement = [
'Admin\Actions\ListController',
Expand All @@ -25,7 +26,7 @@ class Menuitems extends \Admin\Classes\AdminController
'title' => 'lang:thoughtco.outofstock::default.text_title',
'emptyMessage' => 'lang:thoughtco.outofstock::default.text_empty',
'defaultSort' => ['value', 'ASC'],
'configFile' => 'menuitems',
'configFile' => 'menuoptions',
'showCheckboxes' => FALSE,
],
];
Expand All @@ -42,13 +43,8 @@ public function __construct()

public function index()
{
$this->vars['noLocation'] = false;

if (!AdminLocation::getId())
$this->vars['noLocation'] = true;

Outofstock::where([
['type', '=', 'menuitems'],
['type', '=', 'menuoptions'],
['timeout', '<', Carbon::now()->format('Y-m-d H:i:s')]
])->delete();

Expand All @@ -58,6 +54,21 @@ public function index()
});
});

$this->vars['out_of_stock'] = Outofstock::where([
'type' => 'menuoptions',
])->where(function($subquery) {
$location_id = AdminLocation::getId();
$subquery->where(['location_id' => $location_id]);
if (!is_null($location_id))
$subquery->orWhereNull('location_id');
})
->get()
->pluck('type_id');

$this->vars['out_of_stock_delays'] = collect(Settings::get('delay_times', []));
$this->vars['out_of_stock_url'] = admin_url('thoughtco/outofstock/menuoptions');
$this->vars['out_of_stock_location'] = AdminLocation::getId();

$this->asExtension('ListController')->index();
}

Expand All @@ -66,15 +77,21 @@ public function stock($context, $id = null)
if (!$id)
abort(404);

$this->checkMenuItemExists($id);
$this->checkMenuOptionExists($id);

$params = [
'type' => 'menuitems',
'type' => 'menuoptions',
'type_id' => $id,
'location_id' => AdminLocation::getId(),
];

Outofstock::where($params)->each(function($model) {
Outofstock::where($params)
->where(function($subquery) {
$location_id = AdminLocation::getId();
$subquery->where(['location_id' => $location_id]);
if (!is_null($location_id))
$subquery->orWhereNull('location_id');
})
->each(function($model) {
$model->delete();
});

Expand All @@ -86,10 +103,10 @@ public function nostock($context, $id = null)
if (!$id)
abort(404);

$this->checkMenuItemExists($id);
$this->checkMenuOptionExists($id);

$params = [
'type' => 'menuitems',
'type' => 'menuoptions',
'type_id' => $id,
'location_id' => AdminLocation::getId(),
];
Expand All @@ -112,7 +129,7 @@ public function nostock($context, $id = null)
return Redirect::back();
}

private function checkMenuItemExists($id)
private function checkMenuOptionExists($id)
{
if (!Menu_option_values_model::find($id))
abort(404);
Expand Down
23 changes: 0 additions & 23 deletions controllers/Menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,6 @@ class Menus extends \Admin\Classes\AdminController

protected $requiredPermissions = 'Thoughtco.Outofstock.*';

public function __construct()
{
parent::__construct();

AdminMenu::setContext('restaurant', 'outofstock');
Template::setTitle(lang('lang:thoughtco.outofstock::default.text_title'));
}

public function index()
{
$this->vars['noLocation'] = false;

if (!AdminLocation::getId())
$this->vars['noLocation'] = true;

Outofstock::where([
['type', '=', 'menus'],
['timeout', '<', Carbon::now()->format('Y-m-d H:i:s')]
])->delete();

$this->asExtension('ListController')->index();
}

public function stock($context, $id = null)
{
if (!$id)
Expand Down
2 changes: 1 addition & 1 deletion database/migrations/2021_06_02_setup_stock_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up()
$table->increments('id');
$table->string('type', 15);
$table->integer('type_id');
$table->integer('location_id');
$table->integer('location_id')->nullable();
$table->dateTime('timeout')->nullable();
});
}
Expand Down
3 changes: 0 additions & 3 deletions language/en/default.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

return [
'button_categories' => 'Categories',
'button_closing' => 'Until closing',
'button_forever' => 'Forever',
'button_menuitems' => 'Menu items',
'button_menuoptions' => 'Menu options',
'button_stock' => 'Back in stock',
'button_nostock' => 'Out of stock',
Expand All @@ -21,7 +19,6 @@
'text_empty' => 'No results found',
'text_permissions' => 'Use Out Of Stock views',
'text_permissions_settings' => 'Manage Out Of Stock Settings',
'text_select_location' => 'Select a location',
'text_settings_label' => 'Out of stock Settings',
'text_settings_description' => 'Configure out of stock settings',
'text_title' => 'Out of stock',
Expand Down
59 changes: 0 additions & 59 deletions models/config/categories.php

This file was deleted.

Loading

0 comments on commit 0857e0a

Please sign in to comment.