From d8360993cc038e9fdac6ec6a4079b7fd2023953b Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 21 Sep 2019 15:07:29 +0200 Subject: [PATCH] Internal change that the demo instances (stable and pre-release) can be served through a single instance for all localizations (references #241) --- README.md | 2 +- app.php | 14 +++++++++++++- changelog/18_1.9.0_2018-04-21.md | 2 +- changelog/25_1.13.0_2018-07-12.md | 2 +- changelog/26_1.13.1_2018-07-12.md | 2 +- changelog/27_1.14.0_2018-07-15.md | 4 ++-- changelog/39_1.23.0_2018-11-24.md | 2 +- changelog/43_2.0.0_2019-03-06.md | 8 ++++---- changelog/44_2.1.0_2019-03-09.md | 2 +- changelog/47_2.4.0_2019-05-10.md | 4 ++-- changelog/52_UNRELEASED_2019-xx-xx.md | 10 +++++++--- config-dist.php | 3 ++- helpers/extensions.php | 16 ++++++++++++++++ services/DatabaseService.php | 16 +++++++++++++--- 14 files changed, 65 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 9601d1d4..44f99e9e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ As the German translation will always be the most complete one, for maintaining ## Things worth to know ### REST API & data model documentation -See the integrated Swagger UI instance on [/api](https://demo-en.grocy.info/api). +See the integrated Swagger UI instance on [/api](https://demo.grocy.info/api). ### Barcode readers Some fields also allow to select a value by scanning a barcode. It works best when your barcode reader prefixes every barcode with a letter which is normally not part of a item name (I use a `$`) and sends a `TAB` after a scan. diff --git a/app.php b/app.php index 448058a8..d8760404 100644 --- a/app.php +++ b/app.php @@ -36,9 +36,21 @@ else // Load composer dependencies require_once __DIR__ . '/vendor/autoload.php'; +// Definitions for the official demo instances +// running on ".demo.grocy.info" and ".demo-prerelease.grocy.info +// The language should automatically be determined by the subdomain +if (string_ends_with(strtolower($_SERVER['HTTP_HOST']), 'grocy.info')) +{ + $cultureBySubdomain = explode('.', $_SERVER['HTTP_HOST'])[0]; + if (file_exists(__DIR__ . "/localization/$cultureBySubdomain") && is_dir(__DIR__ . "/localization/$cultureBySubdomain")) + { + define('GROCY_CULTURE', $cultureBySubdomain); + } +} + // Load config files require_once GROCY_DATAPATH . '/config.php'; -require_once __DIR__ . '/config-dist.php'; //For not in own config defined values we use the default ones +require_once __DIR__ . '/config-dist.php'; // For not in own config defined values we use the default ones // Definitions for disabled authentication mode if (GROCY_DISABLE_AUTH === true) diff --git a/changelog/18_1.9.0_2018-04-21.md b/changelog/18_1.9.0_2018-04-21.md index 71efa0e5..5b47d867 100644 --- a/changelog/18_1.9.0_2018-04-21.md +++ b/changelog/18_1.9.0_2018-04-21.md @@ -1 +1 @@ -- Documented the REST API and data model, see the integrated instance of Swagger UI at [/api](https://demo-en.grocy.info/api) +- Documented the REST API and data model, see the integrated instance of Swagger UI at [/api](https://demo.grocy.info/api) diff --git a/changelog/25_1.13.0_2018-07-12.md b/changelog/25_1.13.0_2018-07-12.md index 138ae890..2ea78724 100644 --- a/changelog/25_1.13.0_2018-07-12.md +++ b/changelog/25_1.13.0_2018-07-12.md @@ -1,3 +1,3 @@ - Upgraded Bootstrap and some other dependencies (grocy now looks even better!) - Added Italian translation (thanks @davidoskky) - - => Demo for this language available at: https://demo-it.grocy.info + - => Demo for this language available at: https://it.demo.grocy.info diff --git a/changelog/26_1.13.1_2018-07-12.md b/changelog/26_1.13.1_2018-07-12.md index cd851961..c8b97ff2 100644 --- a/changelog/26_1.13.1_2018-07-12.md +++ b/changelog/26_1.13.1_2018-07-12.md @@ -2,4 +2,4 @@ This was released shortly after the last release to fix a small regression bug, - Upgraded Bootstrap and some other dependencies (grocy now looks even better!) - Added Italian translation (thanks @davidoskky) - - => Demo for this language available at: https://demo-it.grocy.info + - => Demo for this language available at: https://it.demo.grocy.info diff --git a/changelog/27_1.14.0_2018-07-15.md b/changelog/27_1.14.0_2018-07-15.md index 3cab46a4..87e7c548 100644 --- a/changelog/27_1.14.0_2018-07-15.md +++ b/changelog/27_1.14.0_2018-07-15.md @@ -1,8 +1,8 @@ - New feature: **Recipes** - Organize a list of products, amounts and a description into recipes and see at a glance if everything needed is in stock or put the missing things with one click on the shopping list - - Try it live on the demo page: => https://demo-en.grocy.info/recipes + - Try it live on the demo page: => https://demo.grocy.info/recipes - Added norwegian translation (thanks @BlizzWave) - - Demo available at: => https://demo-no.grocy.info + - Demo available at: => https://no.demo.grocy.info - A lot of small UI improvements - Columns in tables can now be reordered - Show a calendar on the shopping list page (useful, at least for me) diff --git a/changelog/39_1.23.0_2018-11-24.md b/changelog/39_1.23.0_2018-11-24.md index b442385d..44844323 100644 --- a/changelog/39_1.23.0_2018-11-24.md +++ b/changelog/39_1.23.0_2018-11-24.md @@ -9,7 +9,7 @@ - New feature: Calendar - Shows all upcoming product expirations, due chores, due tasks and due battery charge cycles - New translation: French (thanks all the translators) - - As for all languages, a demo is available at: https://demo-fr.grocy.info + - As for all languages, a demo is available at: https://fr.demo.grocy.info - Small other improvements - Allow fraction numbers for recipe ingredients when not checked against stock and add an option to not check stock for a recipe position - The current time can now be shown in the header (see the settings menu next to the user icon) diff --git a/changelog/43_2.0.0_2019-03-06.md b/changelog/43_2.0.0_2019-03-06.md index eb3fc7d4..19289a4f 100644 --- a/changelog/43_2.0.0_2019-03-06.md +++ b/changelog/43_2.0.0_2019-03-06.md @@ -1,4 +1,4 @@ -- Breaking change: The API has been completely reworked, please review [the documentation](https://demo-en.grocy.info/api) before updating when you are using the API +- Breaking change: The API has been completely reworked, please review [the documentation](https://demo.grocy.info/api) before updating when you are using the API - New feature: Tare weight handling - An option per product - Imagine this: You have flour in jars, the jar weighs 500 grams, currently there are 1000 grams in stock, the new weight including the jar is 1100 grams - grocy can now calculate the used amount on consume/purchase/inventory automatically, you only have to enter the weighed amount including the jar (demo product to showcase this "Flour") @@ -9,13 +9,13 @@ - A new option per recipe to not check against the amount already on the shopping list when putting all missing ingredients on it (by default, only the amount not already on the shopping list is added, when this is enabled, always the whole missing amount will be put on the shopping list) - On consume, there can now be tracked for which recipe it was, this is also tracked automatically when using the "Consume all ingredients needed by this recipe" button (for future statistical purposes) - Recipes can now have pictures - - New "gallery view" for recipes (demo available at https://demo-en.grocy.info/recipes?tab=gallery) + - New "gallery view" for recipes (demo available at https://demo.grocy.info/recipes?tab=gallery) - Stock improvements - It is now optionally possible to have partial units in stock (option per product) - On purchase, a different location can now be assigned (imagine you have two freezers, by default you store your pizza there, but sometimes there) - New translations: (thanks all the translators) - - Spanish (demo available at https://demo-es.grocy.info) - - Turkish (demo available at https://demo-tr.grocy.info) + - Spanish (demo available at https://es.demo.grocy.info) + - Turkish (demo available at https://tr.demo.grocy.info) - Other improvements - The calendar can now be shared/integrated in iCal format (button in the header on the calendar page) - Added feature flags to hide/disable certain parts of grocy when you don't use them (for example hide "Chores" and all related UI elements, when you don't use it, see `config-dist.php`) diff --git a/changelog/44_2.1.0_2019-03-09.md b/changelog/44_2.1.0_2019-03-09.md index 327de06d..a4838796 100644 --- a/changelog/44_2.1.0_2019-03-09.md +++ b/changelog/44_2.1.0_2019-03-09.md @@ -7,4 +7,4 @@ - The changelog is now included as markdown files (in `/changelog` directory, one file per release with a filename in format `__.md`) and shown in the about dialog - Please review your `CURRENCY` setting in `data/config.php`, see also `config-dist.php` - this should be the ISO 4217 code of the currency to properly work with the JS `toLocaleString` function - New translation: (thanks all the translators) - - Russian (demo available at https://demo-ru.grocy.info) + - Russian (demo available at https://ru.demo.grocy.info) diff --git a/changelog/47_2.4.0_2019-05-10.md b/changelog/47_2.4.0_2019-05-10.md index 8f960733..432110dd 100644 --- a/changelog/47_2.4.0_2019-05-10.md +++ b/changelog/47_2.4.0_2019-05-10.md @@ -21,8 +21,8 @@ - Recipe improvements - It's now possible to enter a "variable amount" (e. g. if a recipe needs "1 - 2 cups"), the original amount is still used for stock fulfillment checking (if enabled for that recipe ingredient) - New translations: (thanks all the translators) - - Swedish (demo available at https://demo-sv.grocy.info) - - Polish (demo available at https://demo-pl.grocy.info) + - Swedish (demo available at https://sv_se.demo.grocy.info) + - Polish (demo available at https://pl.demo.grocy.info) - Internal improvement: Localizations are now handled via gettext, both on server and client side - Mainly to properly handle languages with more than 2 plural forms - This involved some string changes which results in a needed (re)translation of about 20 strings (excluding demo data) diff --git a/changelog/52_UNRELEASED_2019-xx-xx.md b/changelog/52_UNRELEASED_2019-xx-xx.md index 9335f024..7d3b2402 100644 --- a/changelog/52_UNRELEASED_2019-xx-xx.md +++ b/changelog/52_UNRELEASED_2019-xx-xx.md @@ -77,8 +77,12 @@ - It's now possible to test plural forms of quantity units (button on the quantity unit edit page, only visible if the current language requires more than 2 plural forms) - On the login page no menus and the sidebar is now hidden - New translations: (thanks all the translators) - - Danish (demo available at https://demo-da.grocy.info) - - Dutch (demo available at https://demo-nl.grocy.info) + - Danish (demo available at https://da.demo.grocy.info) + - Dutch (demo available at https://nl.demo.grocy.info) +- Internal change for how the localizations for the demo instances are handled + - Also for the pre-release demo now all currently supported languages are available + - The URLs have changed, I'll try to keep all existing URLs redirecting properly for a long time + - If you want to link to the demo, please only use https://demo.grocy.info (stable demo) or https://demo-prerelease.grocy.info (current master branch demo) ### API improvements & non-breaking changes - New endpoint `/objects/{entity}/search/{searchString}` to search for objects by name (contains search) @@ -90,7 +94,7 @@ - New endpoint `/stock/products/by-barcode/{barcode}/inventory` to inventory a product by its barcode - New endpoint `/stock/products/by-barcode/{barcode}/open` to mark a product as opened by its barcode - New endpoint `/stock/bookings/{bookingId}` to retrieve a single stock booking -- Endpoint `GET /files/{group}/{fileName}` can now also downscale pictures (see API documentation on [/api](https://demo-en.grocy.info/api)) +- Endpoint `GET /files/{group}/{fileName}` can now also downscale pictures (see API documentation on [/api](https://demo.grocy.info/api)) - When adding a product (through `stock/product/{productId}/add` or `stock/product/{productId}/inventory`) with omitted best before date and if the given product has "Default best before days" set, the best before date is calculated based on that (so far always today was used which is still the case when no date is supplied and also the product has no "Default best before days set) (thanks @Forceu) - Field `stock_amount` of endpoint `/stock/products/{productId}` now returns `0` instead of `null` when the given product is not in stock (thanks @Forceu) - Fixed that `/system/db-changed-time` always returned the current time (more or less) due to that that time is the database file modification time and the database is effectively changed on each request because of session information tracking - which now explicitly does not change the database file modification time, so this should work again to determine if any data changes happened diff --git a/config-dist.php b/config-dist.php index bb16506e..a8f34897 100644 --- a/config-dist.php +++ b/config-dist.php @@ -14,7 +14,8 @@ # The settings defined here below -# Either "production", "dev" or "prerelease" +# Either "production", "dev", "demo" or "prerelease" +# ("demo" and "prerelease" is reserved to be used only on the offical demo instances) Setting('MODE', 'production'); # Either "en" or "de" or the directory name of diff --git a/helpers/extensions.php b/helpers/extensions.php index f8a21f83..956d14c7 100644 --- a/helpers/extensions.php +++ b/helpers/extensions.php @@ -220,3 +220,19 @@ function IsJsonString($text) json_decode($text); return (json_last_error() == JSON_ERROR_NONE); } + +function string_starts_with($haystack, $needle) +{ + return (substr($haystack, 0, strlen($needle)) === $needle); +} + +function string_ends_with($haystack, $needle) +{ + $length = strlen($needle); + if ($length == 0) + { + return true; + } + + return (substr($haystack, -$length) === $needle); +} diff --git a/services/DatabaseService.php b/services/DatabaseService.php index bbfaa4ce..252aaec8 100644 --- a/services/DatabaseService.php +++ b/services/DatabaseService.php @@ -6,6 +6,16 @@ use \Grocy\Services\ApplicationService; class DatabaseService { + private function GetDbFilePath() + { + if (GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease') + { + return GROCY_DATAPATH . '/grocy_' . GROCY_CULTURE . '.db'; + } + + return GROCY_DATAPATH . '/grocy.db'; + } + private $DbConnectionRaw; /** * @return \PDO @@ -14,7 +24,7 @@ class DatabaseService { if ($this->DbConnectionRaw == null) { - $pdo = new \PDO('sqlite:' . GROCY_DATAPATH . '/grocy.db'); + $pdo = new \PDO('sqlite:' . $this->GetDbFilePath()); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->DbConnectionRaw = $pdo; } @@ -66,11 +76,11 @@ class DatabaseService public function GetDbChangedTime() { - return date('Y-m-d H:i:s', filemtime(GROCY_DATAPATH . '/grocy.db')); + return date('Y-m-d H:i:s', filemtime($this->GetDbFilePath())); } public function SetDbChangedTime($dateTime) { - touch(GROCY_DATAPATH . '/grocy.db', strtotime($dateTime)); + touch($this->GetDbFilePath(), strtotime($dateTime)); } }