diff --git a/Extension.php b/Extension.php index a1f7cf5..5546fc6 100644 --- a/Extension.php +++ b/Extension.php @@ -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([ @@ -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([ @@ -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([ @@ -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.*', ], ], diff --git a/controllers/Categories.php b/controllers/Categories.php index aae2f2f..5c1dbe2 100644 --- a/controllers/Categories.php +++ b/controllers/Categories.php @@ -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) diff --git a/controllers/Menuitems.php b/controllers/Menuoptions.php similarity index 66% rename from controllers/Menuitems.php rename to controllers/Menuoptions.php index cf61fae..29de9cc 100644 --- a/controllers/Menuitems.php +++ b/controllers/Menuoptions.php @@ -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', @@ -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, ], ]; @@ -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(); @@ -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(); } @@ -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(); }); @@ -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(), ]; @@ -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); diff --git a/controllers/Menus.php b/controllers/Menus.php index 0a921da..e6b9a6a 100644 --- a/controllers/Menus.php +++ b/controllers/Menus.php @@ -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) diff --git a/database/migrations/2021_06_02_setup_stock_tables.php b/database/migrations/2021_06_02_setup_stock_tables.php index 7d1d35b..855ca1e 100644 --- a/database/migrations/2021_06_02_setup_stock_tables.php +++ b/database/migrations/2021_06_02_setup_stock_tables.php @@ -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(); }); } diff --git a/language/en/default.php b/language/en/default.php index 4b51d7d..3c445e7 100644 --- a/language/en/default.php +++ b/language/en/default.php @@ -1,10 +1,8 @@ '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', @@ -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', diff --git a/models/config/categories.php b/models/config/categories.php deleted file mode 100644 index ce8468a..0000000 --- a/models/config/categories.php +++ /dev/null @@ -1,59 +0,0 @@ - 'categories', - 'location_id' => \AdminLocation::getId(), -])->get()->pluck('type_id'); - -return [ - 'list' => [ - 'toolbar' => [ - 'buttons' => [ - 'menus' => [ - 'label' => 'lang:thoughtco.outofstock::default.button_menuitems', - 'class' => 'btn btn-secondary', - 'href' => 'thoughtco/outofstock/menus', - ], - 'menuitems' => [ - 'label' => 'lang:thoughtco.outofstock::default.button_menuoptions', - 'class' => 'btn btn-secondary', - 'href' => 'thoughtco/outofstock/menuitems', - ], - ], - ], - 'filter' => [ - 'search' => [ - 'prompt' => 'lang:thoughtco.outofstock::default.label_filter_search', - 'mode' => 'all', - ], - ], - 'columns' => [ - 'name' => [ - 'label' => 'lang:thoughtco.outofstock::default.column_name', - 'type' => 'text', - 'sortable' => TRUE, - ], - 'category_id' => [ - 'label' => '', - 'type' => 'text', - 'sortable' => FALSE, - 'formatter' => function ($record, $column, $value) use ($cache_menus) { - if ($cache_menus->contains($value)) - return ''.__('lang:thoughtco.outofstock::default.button_stock').''; - - return ' -
'; - } - ], - - ], - ], -]; diff --git a/models/config/menuitems.php b/models/config/menuitems.php deleted file mode 100644 index acec73e..0000000 --- a/models/config/menuitems.php +++ /dev/null @@ -1,66 +0,0 @@ - 'menuitems', - 'location_id' => \AdminLocation::getId(), -])->get()->pluck('type_id'); - -return [ - 'list' => [ - 'toolbar' => [ - 'buttons' => [ - 'menuitems' => [ - 'label' => 'lang:thoughtco.outofstock::default.button_menuitems', - 'class' => 'btn btn-secondary', - 'href' => 'thoughtco/outofstock/menus', - ], - 'categories' => [ - 'label' => 'lang:thoughtco.outofstock::default.button_categories', - 'class' => 'btn btn-secondary', - 'href' => 'thoughtco/outofstock/categories', - ], - ], - ], - 'filter' => [ - 'search' => [ - 'prompt' => 'lang:thoughtco.outofstock::default.label_filter_search_options', - 'mode' => 'all', - ], - ], - 'columns' => [ - 'option' => [ - 'label' => 'lang:thoughtco.outofstock::default.column_option', - 'type' => 'text', - 'sortable' => TRUE, - 'relation' => 'option', - 'select' => 'option_name', - ], - 'value' => [ - 'label' => 'lang:thoughtco.outofstock::default.column_name', - 'type' => 'text', - 'sortable' => TRUE, - ], - 'option_value_id' => [ - 'label' => '', - 'type' => 'text', - 'sortable' => FALSE, - 'formatter' => function ($record, $column, $value) use ($cache_menus) { - if ($cache_menus->contains($value)) - return ''.__('lang:thoughtco.outofstock::default.button_stock').''; - - return ' - '; - } - ], - - ], - ], -]; diff --git a/models/config/menuoptions.php b/models/config/menuoptions.php new file mode 100644 index 0000000..493202b --- /dev/null +++ b/models/config/menuoptions.php @@ -0,0 +1,46 @@ + [ + 'filter' => [ + 'search' => [ + 'prompt' => 'lang:thoughtco.outofstock::default.label_filter_search_options', + 'mode' => 'all', + ], + ], + 'columns' => [ + 'option' => [ + 'label' => 'lang:thoughtco.outofstock::default.column_option', + 'type' => 'text', + 'sortable' => TRUE, + 'relation' => 'option', + 'select' => 'option_name', + ], + 'value' => [ + 'label' => 'lang:thoughtco.outofstock::default.column_name', + 'type' => 'text', + 'sortable' => TRUE, + ], + 'option_value_id' => [ + 'label' => '', + 'type' => 'partial', + 'sortable' => FALSE, + 'path' => 'form/stock_column', +// 'formatter' => function ($record, $column, $value) use ($cache_menus) { +// if ($cache_menus->contains($value)) +// return ''.__('lang:thoughtco.outofstock::default.button_stock').''; +// +// return ' +// '; +// } + ], + + ], + ], +]; diff --git a/models/config/menus.php b/models/config/menus.php deleted file mode 100644 index 60d8c4c..0000000 --- a/models/config/menus.php +++ /dev/null @@ -1,59 +0,0 @@ - 'menus', - 'location_id' => \AdminLocation::getId(), -])->get()->pluck('type_id'); - -return [ - 'list' => [ - 'toolbar' => [ - 'buttons' => [ - 'menuitems' => [ - 'label' => 'lang:thoughtco.outofstock::default.button_menuoptions', - 'class' => 'btn btn-secondary', - 'href' => 'thoughtco/outofstock/menuitems', - ], - 'categories' => [ - 'label' => 'lang:thoughtco.outofstock::default.button_categories', - 'class' => 'btn btn-secondary', - 'href' => 'thoughtco/outofstock/categories', - ], - ], - ], - 'filter' => [ - 'search' => [ - 'prompt' => 'lang:thoughtco.outofstock::default.label_filter_search', - 'mode' => 'all', - ], - ], - 'columns' => [ - 'menu_name' => [ - 'label' => 'lang:thoughtco.outofstock::default.column_name', - 'type' => 'text', - 'sortable' => TRUE, - ], - 'menu_id' => [ - 'label' => '', - 'type' => 'text', - 'sortable' => FALSE, - 'formatter' => function ($record, $column, $value) use ($cache_menus) { - if ($cache_menus->contains($value)) - return ''.__('lang:thoughtco.outofstock::default.button_stock').''; - - return ' - '; - } - ], - - ], - ], -]; diff --git a/views/form/stock_column.blade.php b/views/form/stock_column.blade.php new file mode 100644 index 0000000..ee2997e --- /dev/null +++ b/views/form/stock_column.blade.php @@ -0,0 +1,12 @@ + @if ($out_of_stock->contains($value)) + @lang('thoughtco.outofstock::default.button_stock') + @else + + + @endif diff --git a/views/menuitems/index.blade.php b/views/menuitems/index.blade.php deleted file mode 100644 index fa34fa9..0000000 --- a/views/menuitems/index.blade.php +++ /dev/null @@ -1,7 +0,0 @@ -@lang('thoughtco.outofstock::default.text_select_location')
- @else - {!! $this->renderList() !!} - @endif -