From 934b1a86f67a0488196ef9f92a2b50a3ef03db13 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Mon, 14 Dec 2020 19:35:45 +0000 Subject: [PATCH] create seperate shoppinglist and shoppinglistprint views --- controllers/StockController.php | 9 +- localization/strings.pot | 3 + public/viewjs/components/shoppinglisttable.js | 41 ++++ public/viewjs/shoppinglist.js | 48 +---- public/viewjs/shoppinglistprint.js | 15 ++ views/components/shoppinglisttable.blade.php | 141 +++++++++++++ views/layout/default.blade.php | 33 ++- views/shoppinglist.blade.php | 189 ++---------------- views/shoppinglistprint.blade.php | 50 +++++ views/shoppinglistsettings.blade.php | 5 +- 10 files changed, 311 insertions(+), 223 deletions(-) create mode 100644 public/viewjs/components/shoppinglisttable.js create mode 100644 public/viewjs/shoppinglistprint.js create mode 100644 views/components/shoppinglisttable.blade.php create mode 100644 views/shoppinglistprint.blade.php diff --git a/controllers/StockController.php b/controllers/StockController.php index 2ff38b14..6b682ded 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -332,7 +332,14 @@ class StockController extends BaseController $listId = $request->getQueryParams()['list']; } - return $this->renderPage($response, 'shoppinglist', [ + $template = 'shoppinglist'; + + if (isset($request->getQueryParams()['print'])) + { + $template = 'shoppinglistprint'; + } + + return $this->renderPage($response, $template, [ 'listItems' => $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId), 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'), 'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), diff --git a/localization/strings.pot b/localization/strings.pot index b6951360..1d16a488 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -2008,3 +2008,6 @@ msgstr "" msgid "Show on stock overview page" msgstr "" + +msgid "Open print preview" +msgstr "" diff --git a/public/viewjs/components/shoppinglisttable.js b/public/viewjs/components/shoppinglisttable.js new file mode 100644 index 00000000..874a86bf --- /dev/null +++ b/public/viewjs/components/shoppinglisttable.js @@ -0,0 +1,41 @@ +var collapsedGroups = {}; + +//tableId is desclared in shoppinglisttable.blade.php + +var shoppingListTable = $(tableId).DataTable({ + 'order': [[3, 'asc']], + "orderFixed": [[1, 'asc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 }, + { 'visible': false, 'targets': 1 } + ].concat($.fn.dataTable.defaults.columnDefs), + 'rowGroup': { + dataSrc: 1, + startRender: function(rows, group) + { + var collapsed = !!collapsedGroups[group]; + var toggleClass = collapsed ? "fa-caret-right" : "fa-caret-down"; + + rows.nodes().each(function(row) + { + row.style.display = collapsed ? "none" : ""; + }); + + return $("") + .append('' + group + ' ') + .attr("data-name", group) + .toggleClass("collapsed", collapsed); + } + } +}); + +$(tableId + ' tbody').removeClass("d-none"); +shoppingListTable.columns.adjust().draw(); + +$(document).on("click", "tr.dtrg-group", function() +{ + var name = $(this).data('name'); + collapsedGroups[name] = !collapsedGroups[name]; + shoppingListTable.draw(); +}); diff --git a/public/viewjs/shoppinglist.js b/public/viewjs/shoppinglist.js index 35fb577b..5b52483c 100644 --- a/public/viewjs/shoppinglist.js +++ b/public/viewjs/shoppinglist.js @@ -1,43 +1,4 @@ -var collapsedGroups = {}; - -var shoppingListTable = $('#shoppinglist-table').DataTable({ - 'order': [[1, 'asc']], - "orderFixed": [[3, 'asc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 }, - { 'visible': false, 'targets': 3 } - ].concat($.fn.dataTable.defaults.columnDefs), - 'rowGroup': { - dataSrc: 3, - startRender: function(rows, group) - { - var collapsed = !!collapsedGroups[group]; - var toggleClass = collapsed ? "fa-caret-right" : "fa-caret-down"; - - rows.nodes().each(function(row) - { - row.style.display = collapsed ? "none" : ""; - }); - - return $("") - .append('' + group + ' ') - .attr("data-name", group) - .toggleClass("collapsed", collapsed); - } - } -}); -$('#shoppinglist-table tbody').removeClass("d-none"); -shoppingListTable.columns.adjust().draw(); - -$(document).on("click", "tr.dtrg-group", function() -{ - var name = $(this).data('name'); - collapsedGroups[name] = !collapsedGroups[name]; - shoppingListTable.draw(); -}); - -$("#search").on("keyup", Delay(function() +$("#search").on("keyup", Delay(function() { var value = $(this).val(); if (value === "all") @@ -383,13 +344,6 @@ function OnListItemRemoved() } OnListItemRemoved(); -$(document).on("click", "#print-shopping-list-button", function(e) -{ - $(".print-timestamp").text(moment().format("l LT")); - $("#description-for-print").html($("#description").val()); - window.print(); -}); - $("#description").on("summernote.change", function() { $("#save-description-button").removeClass("disabled"); diff --git a/public/viewjs/shoppinglistprint.js b/public/viewjs/shoppinglistprint.js new file mode 100644 index 00000000..1cea139d --- /dev/null +++ b/public/viewjs/shoppinglistprint.js @@ -0,0 +1,15 @@ +$(function() +{ + $(".print-timestamp").text(moment().format("l LT")); + + const url = new URL(window.location); + //open print dialog only if the parameter "preview" is not set + if (!url.searchParams.has("preview")) + { + window.print(); + + //redirect to the shoppinglist + url.searchParams.delete("print"); + window.location.replace(url); + } +}); \ No newline at end of file diff --git a/views/components/shoppinglisttable.blade.php b/views/components/shoppinglisttable.blade.php new file mode 100644 index 00000000..156c666a --- /dev/null +++ b/views/components/shoppinglisttable.blade.php @@ -0,0 +1,141 @@ +@php if(empty($listItems)) { $listItems = []; } @endphp +@php if(empty($products)) { $products = []; } @endphp +@php if(empty($quantityunits)) { $quantityunits = []; } @endphp +@php if(empty($missingProducts)) { $missingProducts = []; } @endphp +@php if(empty($productGroups)) { $productGroups = []; } @endphp +@php if(!isset($selectedShoppingListId)) { $selectedShoppingListId = 1; } @endphp +@php if(empty($quantityUnitConversionsResolved)) { $quantityUnitConversionsResolved = []; } @endphp +@php if(empty($productUserfields)) { $productUserfields = []; } @endphp +@php if(empty($productUserfieldValues)) { $productUserfieldValues = []; } @endphp +@php if(empty($userfields)) { $userfields = []; } @endphp +@php if(empty($userfieldValues)) { $userfieldValues = []; } @endphp +@php if(!isset($isPrint)) { $isPrint = false; } @endphp +@php $tableId = ($isPrint ? "shoppinglist-table-print" : "shoppinglist-table"); @endphp + +@push('componentStyles') + + +@endpush + +@push('componentScripts') + + + + +@endpush + + + + + + + + + + + @include('components.userfields_thead', array( + 'userfields' => $userfields + )) + @include('components.userfields_thead', array( + 'userfields' => $productUserfields + )) + + + + + @foreach($listItems as $listItem) + + + + + + + + + + + + @include('components.userfields_tbody', array( + 'userfields' => $userfields, + 'userfieldValues' => FindAllObjectsInArrayByPropertyValue($userfieldValues, 'object_id', $listItem->id) + )) + @include('components.userfields_tbody', array( + 'userfields' => $productUserfields, + 'userfieldValues' => FindAllObjectsInArrayByPropertyValue($userfieldValues, 'object_id', $listItem->product_id) + )) + + + @endforeach + +
+ Hidden product groupHidden status{{ $__t('Product') }} / {{ $__t('Note') }}{{ $__t('Amount') }}
+ @if(!$isPrint) + + + + + + + + + + product_id)) data-toggle="tooltip" title="{{ $__t('Add %1$s of %2$s to stock', $listItem->amount . ' ' . $__n($listItem->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name_plural), FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->name, $listItem->amount) }}" @endif> + + + @endif + + @if(!empty(FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->product_group_id)) {{ FindObjectInArrayByPropertyValue($productGroups, 'id', FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->product_group_id)->name }} @else {{ $__t('Ungrouped') }} @endif + + @if(FindObjectInArrayByPropertyValue($missingProducts, 'id', $listItem->product_id) !== null) belowminstockamount @endif + @if($listItem->done != 1) xxUNDONExx @endif + + @if(!empty($listItem->product_id)) + @php + $listItem->amount_origin_qu = $listItem->amount; + $product = FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id); + $productQuConversions = FindAllObjectsInArrayByPropertyValue($quantityUnitConversionsResolved, 'product_id', $product->id); + $productQuConversions = FindAllObjectsInArrayByPropertyValue($productQuConversions, 'from_qu_id', $product->qu_id_stock); + $productQuConversion = FindObjectInArrayByPropertyValue($productQuConversions, 'to_qu_id', $listItem->qu_id); + if ($productQuConversion) + { + $listItem->amount = $listItem->amount * $productQuConversion->factor; + } + @endphp + @endif + {{ $listItem->amount }} @if(!empty($listItem->product_id)){{ $__n($listItem->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name_plural) }}@endif +
\ No newline at end of file diff --git a/views/layout/default.blade.php b/views/layout/default.blade.php index 173b20f8..cbbc2fe8 100644 --- a/views/layout/default.blade.php +++ b/views/layout/default.blade.php @@ -83,6 +83,37 @@ rel="stylesheet"> @stack('pageStyles') + @php + // @stack('componentStyles') maybe contains the components CSS file reference multiple times + // if the component was included more than once in the view + // + // So this is a ugly hack to keep only unique CSS file references there + + // The property is normally protected, so change that + $reflection = new \ReflectionClass($__env); + $property = $reflection->getProperty('pushes'); + $property->setAccessible(true); + $env = $property->getValue($__env); + + if (array_key_exists('componentStyles', $env)) + { + // Take every line into a new array, one element per line + $filteredStack = array_map(function($value) + { + return explode("#SEP#", str_replace(array("\n", "\r", "\t"), '#SEP#', trim($value))); + }, $env['componentStyles']); + + // Flatten the array into a single one, only keep unique lines, remove empty lines, add a defined new line + $filteredStack = preg_filter('/$/', "\n", array_filter(array_unique(array_merge(...$filteredStack)))); + + // Write it back + $env['componentStyles'] = $filteredStack; + $property->setValue($__env, $env); + } + @endphp + + @stack('componentStyles') + @if(file_exists(GROCY_DATAPATH . '/custom_css.html')) @php include GROCY_DATAPATH . '/custom_css.html' @endphp @endif @@ -746,4 +777,4 @@ @endif - + \ No newline at end of file diff --git a/views/shoppinglist.blade.php b/views/shoppinglist.blade.php index 35b67cbe..106774af 100644 --- a/views/shoppinglist.blade.php +++ b/views/shoppinglist.blade.php @@ -5,23 +5,12 @@ @section('viewJsName', 'shoppinglist') @push('pageScripts') - - @endpush @push('pageStyles') - - - @endpush @section('content') @@ -71,7 +60,7 @@ + href="{{ $U('/shoppinglist?print&list=' . $selectedShoppingListId) }}"> {{ $__t('Print') }} @@ -171,108 +160,20 @@ href="#"> {{ $__t('Normal view') }} - - - - - - - - - - @include('components.userfields_thead', array( - 'userfields' => $userfields - )) - @include('components.userfields_thead', array( - 'userfields' => $productUserfields - )) - - - - - @foreach($listItems as $listItem) - - - - - - - - @include('components.userfields_tbody', array( - 'userfields' => $userfields, - 'userfieldValues' => FindAllObjectsInArrayByPropertyValue($userfieldValues, 'object_id', $listItem->id) - )) - @include('components.userfields_tbody', array( - 'userfields' => $productUserfields, - 'userfieldValues' => FindAllObjectsInArrayByPropertyValue($userfieldValues, 'object_id', $listItem->product_id) - )) - - - @endforeach - -
- {{ $__t('Product') }} / {{ $__t('Note') }}{{ $__t('Amount') }}Hidden product groupHidden status
- - - - - - - - - - product_id)) data-toggle="tooltip" title="{{ $__t('Add %1$s of %2$s to stock', $listItem->amount . ' ' . $__n($listItem->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name_plural), FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->name, $listItem->amount) }}" @endif> - - - - @if(!empty($listItem->product_id)) - @php - $listItem->amount_origin_qu = $listItem->amount; - $product = FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id); - $productQuConversions = FindAllObjectsInArrayByPropertyValue($quantityUnitConversionsResolved, 'product_id', $product->id); - $productQuConversions = FindAllObjectsInArrayByPropertyValue($productQuConversions, 'from_qu_id', $product->qu_id_stock); - $productQuConversion = FindObjectInArrayByPropertyValue($productQuConversions, 'to_qu_id', $listItem->qu_id); - if ($productQuConversion) - { - $listItem->amount = $listItem->amount * $productQuConversion->factor; - } - @endphp - @endif - {{ $listItem->amount }} @if(!empty($listItem->product_id)){{ $__n($listItem->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name_plural) }}@endif - - @if(!empty(FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->product_group_id)) {{ FindObjectInArrayByPropertyValue($productGroups, 'id', FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->product_group_id)->name }} @else {{ $__t('Ungrouped') }} @endif - - @if(FindObjectInArrayByPropertyValue($missingProducts, 'id', $listItem->product_id) !== null) belowminstockamount @endif - @if($listItem->done != 1) xxUNDONExx @endif -
+ @include('components.shoppinglisttable', array( + 'listItems' => $listItems, + 'products' => $products, + 'quantityunits' => $quantityunits, + 'missingProducts' => $missingProducts, + 'productGroups' => $productGroups, + 'selectedShoppingListId' => $selectedShoppingListId, + 'quantityUnitConversionsResolved' => $quantityUnitConversionsResolved, + 'productUserfields' => $productUserfields, + 'productUserfieldValues' => $productUserfieldValues, + 'userfields' => $userfields, + 'userfieldValues' => $userfieldValues, + 'isPrint' => false + )) @if(boolval($userSettings['shopping_list_show_calendar'])) @@ -322,64 +223,6 @@ -
-

- - {{ $__t("Shopping list") }} -

- @if (FindObjectInArrayByPropertyValue($shoppingLists, 'id', $selectedShoppingListId)->name != $__t("Shopping list")) -

- {{ FindObjectInArrayByPropertyValue($shoppingLists, 'id', $selectedShoppingListId)->name }} -

- @endif -
- {{ $__t('Time of printing') }}: - -
-
-
- - - - - - - @include('components.userfields_thead', array( - 'userfields' => $userfields - )) - - - - - @foreach($listItems as $listItem) - - - - - @include('components.userfields_tbody', array( - 'userfields' => $userfields, - 'userfieldValues' => FindAllObjectsInArrayByPropertyValue($userfieldValues, 'object_id', $listItem->product_id) - )) - - - @endforeach - -
{{ $__t('Product') }} / {{ $__t('Note') }}{{ $__t('Amount') }}
- @if(!empty($listItem->product_id)) {{ FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->name }}
@endif{!! nl2br($listItem->note) !!} -
- {{ $listItem->amount }} @if(!empty($listItem->product_id)){{ $__n($listItem->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', $listItem->qu_id)->name_plural) }}@endif -
-
-
-
-
-
{{ $__t('Notes') }}
-

-
-
-
-@stop +@stop \ No newline at end of file diff --git a/views/shoppinglistprint.blade.php b/views/shoppinglistprint.blade.php new file mode 100644 index 00000000..0a1ab520 --- /dev/null +++ b/views/shoppinglistprint.blade.php @@ -0,0 +1,50 @@ +@extends('layout.default') + +@section('title', $__t('Shopping list')) + +@section('viewJsName', 'shoppinglistprint') + +@section('content') +
+

+ + {{ $__t("Shopping list") }} +

+ @if (FindObjectInArrayByPropertyValue($shoppingLists, 'id', $selectedShoppingListId)->name != $__t("Shopping list")) +

+ {{ FindObjectInArrayByPropertyValue($shoppingLists, 'id', $selectedShoppingListId)->name }} +

+ @endif +
+ {{ $__t('Time of printing') }}: + +
+
+
+ @include('components.shoppinglisttable', array( + 'listItems' => $listItems, + 'products' => $products, + 'quantityunits' => $quantityunits, + 'missingProducts' => $missingProducts, + 'productGroups' => $productGroups, + 'selectedShoppingListId' => $selectedShoppingList->id, + 'quantityUnitConversionsResolved' => $quantityUnitConversionsResolved, + 'productUserfields' => $productUserfields, + 'productUserfieldValues' => $productUserfieldValues, + 'userfields' => $userfields, + 'userfieldValues' => $userfieldValues, + 'isPrint' => true + )) +
+
+
+
+
{{ $__t('Notes') }}
+

{!! FindObjectInArrayByPropertyValue($shoppingLists, 'id', $selectedShoppingListId)->description !!}

+
+
+
+ +@stop \ No newline at end of file diff --git a/views/shoppinglistsettings.blade.php b/views/shoppinglistsettings.blade.php index 7eb5b2c9..e90e88a3 100644 --- a/views/shoppinglistsettings.blade.php +++ b/views/shoppinglistsettings.blade.php @@ -43,8 +43,11 @@ + {{ $__t('Open print preview') }} + {{ $__t('OK') }} -@stop +@stop \ No newline at end of file