diff --git a/Block/Snowflake.php b/Block/Snowflake.php index c081af2..c8c8198 100644 --- a/Block/Snowflake.php +++ b/Block/Snowflake.php @@ -1,4 +1,8 @@ SnowflakeConfig = $snowflakeConfig; - $this->scopeConfig = $scopeConfig; + $this->snowflakeConfig = $snowflakeConfig; parent::__construct($context, $data); } public function getSnowflakeChar(): string { - return $this->SnowflakeConfig->getSnowflakeChar(); + return $this->snowflakeConfig->getSnowflakeChar(); + } + + public function getSnowflakeVSpeed(): string + { + return $this->snowflakeConfig->getSnowflakeVSpeed(); } - public function getSnowflakeVSpeed() : string + public function getSnowflakeHSpeed(): string { - return $this->SnowflakeConfig->getSnowflakeVSpeed(); + return $this->snowflakeConfig->getSnowflakeHSpeed(); } - public function getSnowflakeHSpeed() : string + public function getSnowflakeRotSpeed(): string { - return $this->SnowflakeConfig->getSnowflakeHSpeed(); + return $this->snowflakeConfig->getSnowflakeRotSpeed(); } - public function getSnowflakeRotSpeed() : string + public function getSnowflakeQty(): int { - return $this->SnowflakeConfig->getSnowflakeRotSpeed(); + return (int)$this->snowflakeConfig->getSnowflakeQty(); } - public function getSnowflakeQty() : int + public function getSnowflakeMinSize(): int { - return (int)$this->SnowflakeConfig->getSnowflakeQty(); + return (int)$this->snowflakeConfig->getSnowflakeMinSize(); } - public function getSnowflakeMinSize() : int + public function getSnowflakeMaxSize(): int { - return (int)$this->SnowflakeConfig->getSnowflakeMinSize(); + return (int)$this->snowflakeConfig->getSnowflakeMaxSize(); } - public function getSnowflakeMaxSize() : int + public function isForceSnow(): bool { - return (int)$this->SnowflakeConfig->getSnowflakeMaxSize(); + return $this->snowflakeConfig->isForceSnow(); } - public function getSnowflakeForce() : string + public function isApiEnable(): bool { - return $this->SnowflakeConfig->getSnowflakeForce(); + return $this->snowflakeConfig->isApiEnable(); } public function getAjaxUrl(): string { - return $this->SnowflakeConfig->getAjaxUrl(); + return $this->snowflakeConfig->getAjaxUrl(); } } diff --git a/Controller/Index/Meteo.php b/Controller/Api/Meteo.php similarity index 67% rename from Controller/Index/Meteo.php rename to Controller/Api/Meteo.php index 93a8e4a..c1ff448 100644 --- a/Controller/Index/Meteo.php +++ b/Controller/Api/Meteo.php @@ -5,7 +5,7 @@ */ declare(strict_types=1); -namespace Opengento\Snowflake\Controller\Index; +namespace Opengento\Snowflake\Controller\Api; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; @@ -32,11 +32,15 @@ public function __construct( */ public function execute() { - $lat = $this->getRequest()->getParam('lat'); - $lon = $this->getRequest()->getParam('lon'); + $isSnowing = ['is_snowing' => '0']; - return $this->resultJsonFactory->create()->setData( - ['is_snowing' => $this->api->isSnowing($lat, $lon)] - ); + $lat = $this->getRequest()->getParam('lat') ?? ''; + $lon = $this->getRequest()->getParam('lon') ?? ''; + + if ($lat && $lon) { + $isSnowing = ['is_snowing' => $this->api->isSnowing($lat, $lon)]; + } + + return $this->resultJsonFactory->create()->setData($isSnowing); } } diff --git a/Model/Config/Backend/EmojiConverter.php b/Model/Config/Backend/EmojiConverter.php index 06111af..889e411 100644 --- a/Model/Config/Backend/EmojiConverter.php +++ b/Model/Config/Backend/EmojiConverter.php @@ -1,11 +1,16 @@ scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_MAX_SIZE, ScopeInterface::SCOPE_STORE, $scopeId) ?? ''; } - public function getSnowflakeForce(?int $scopeId = null): string + public function isForceSnow(?int $scopeId = null): bool { - return (string)$this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_FORCE, ScopeInterface::SCOPE_STORE, $scopeId); + return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_SNOWFLAKE_FORCE, ScopeInterface::SCOPE_STORE, $scopeId); + } + + public function isApiEnable(?int $scopeId = null): bool + { + return $this->scopeConfig->isSetFlag( + self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLE, + ScopeInterface::SCOPE_STORE, + $scopeId + ); } public function getApiKey(?int $scopeId = null): string diff --git a/README.md b/README.md index 7b32399..18e2eeb 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ [![Packagist](https://img.shields.io/packagist/dt/opengento/module-snowflake.svg?style=flat-square)](https://packagist.org/packages/opengento/module-snowflake/stats) [![Packagist](https://img.shields.io/packagist/dm/opengento/module-snowflake.svg?style=flat-square)](https://packagist.org/packages/opengento/module-snowflake/stats) -This module adds ❄️️ based on local meteo. +This fun module adds ❄️️ on your website all the time or based on your visitor outside weather. - [Setup](#setup) - [Composer installation](#composer-installation) - - [Setup the module](#setup-the-snowflake) + - [Setup the module](#setup-the-module) - [Features](#features) - [Settings](#settings) - [Documentation](#documentation) @@ -21,11 +21,11 @@ This module adds ❄️️ based on local meteo. Magento 2 Open Source or Commerce edition is required. -### Composer installation +### Composer installation Run the following composer command: -``` +```shell composer require opengento/module-snowflake ``` @@ -33,7 +33,7 @@ composer require opengento/module-snowflake Run the following magento command: -``` +```shell bin/magento setup:upgrade ``` @@ -45,13 +45,19 @@ bin/magento setup:upgrade Do you like the snow? Do you like to feel the same weather on your favorite website? Try this plugin and get the snow along your local meteo! -## Settings +Require: [OpenWeatherMap API key](https://openweathermap.org/). + +### Force Snowflake -The configuration for this module is available in `Stores > Configuration > General > Snowflake`. +It doesn't snow everywhere, so you can force snow. -## Documentation +### Hacking Snowflake + +You don't like snow, no problem, Snowflake can make it rain [cookies](https://twitter.com/MehdiChch/status/1505649692755079169), [hot-dog](https://twitter.com/MehdiChch/status/1505258061249884160) or whatever you want. + +## Settings -Enable the module in the configuration panel to enable the snowflake based on the local meteo. +The configuration for this module is available in `Stores > Configuration > General > ❄️ Snowflake`. ## Support @@ -59,7 +65,7 @@ Raise a new [request](https://github.com/opengento/magento2-snowflake/issues) to ## Authors -- **Opengento Community** - *Lead* - [![Twitter Follow](https://img.shields.io/twitter/follow/opengento.svg?style=social)](https://twitter.com/opengento) +- **OpenGento Community** - *Lead* - [![Twitter Follow](https://img.shields.io/twitter/follow/opengento.svg?style=social)](https://twitter.com/opengento) - **Contributors** - *Contributor* - [![GitHub contributors](https://img.shields.io/github/contributors/opengento/magento2-snowflake.svg?style=flat-square)](https://github.com/opengento/magento2-snowflake/graphs/contributors) ## License diff --git a/Service/OpenWeatherMapApi.php b/Service/OpenWeatherMapApi.php index 35e48f5..a5ee8b4 100644 --- a/Service/OpenWeatherMapApi.php +++ b/Service/OpenWeatherMapApi.php @@ -14,6 +14,10 @@ class OpenWeatherMapApi { + // Manage all Group 6xx: Snow + // https://openweathermap.org/weather-conditions + public const WEATHER_CONDITION = 'snow'; + // Language of data (try your own language here!): protected string $lang = 'en'; @@ -31,7 +35,7 @@ public function __construct( /** * @throws OpenWeatherMap\Exception */ - public function isSnowing($lat, $lon): bool + public function isSnowing(string $lat, string $lon): bool { $apiKey = $this->config->getApiKey(); @@ -42,6 +46,6 @@ public function isSnowing($lat, $lon): bool $weather = $owm->getWeather(['lat' => $lat, 'lon' => $lon], $this->lang, $this->units); - return 'snow' === $weather->weather->description; + return static::WEATHER_CONDITION === $weather->weather->description; } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 99a0421..920d0cc 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -6,97 +6,106 @@ */ --> - - - - -
- separator-top - - opengento - Opengento_Snowflake::config - - - - - Magento\Config\Model\Config\Source\Yesno - - - - Icon to display as a snowflake - Opengento\Snowflake\Model\Config\Backend\EmojiConverter - - 1 - - - - - Quantity of icons displayed - validate-digits - - 1 - - - - - Minimum size of icon in pixels - validate-digits - - 1 - - - - - Maximum size of icon in pixels - validate-digits - - 1 - - - - - Positive value to go right, negatives values to go left - - 1 - - - - - Positive value to fall down, negatives values to ascend - - 1 - - - - - Duration in seconds to do a complete turn on itself. Set to 0 to keep icon still - - 1 - - - + + + + +
+ separator-top + + general + Opengento_Snowflake::config + + + + + snowflake/general/enable + Magento\Config\Model\Config\Source\Yesno + + + + Icon to display as a snowflake ❄️. + Opengento\Snowflake\Model\Config\Backend\EmojiConverter + + 1 + + + + + snowflake/general/force + Magento\Config\Model\Config\Source\Yesno + ⚠️ Display snow whatever is the weather (this makes OpenWeatherMap API useless). + + 1 + + + + + Quantity of icons displayed. + validate-digits + + 1 + + + + + Minimum size of icon in pixels. + validate-digits + + 1 + + + + + Maximum size of icon in pixels. + validate-digits + + 1 + + + + + Positive value to go right, negatives values to go left. + + 1 + + + + + Positive value to fall down, negatives values to ascend. + + 1 + + + + + Duration in seconds to do a complete turn on itself. Set to 0 to keep icon still. + + 1 + + + - + 0 + Force Display.]]> + + snowflake/general/enable_api Magento\Config\Model\Config\Source\Yesno + + 1 + - + - opengento/snowflake/api_key + snowflake/general/api_api_key - 1 + 1 - - openweathermap.org to get the API Key. You will not be able to use the integration without both the API Key.]]> - - - - Magento\Config\Model\Config\Source\Yesno - Display snow whatever is the weather + openweathermap.org to get the API Key. You will not be able to use the integration without the API Key.]]> diff --git a/etc/config.xml b/etc/config.xml index a29ad82..d1d79a8 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -10,7 +10,7 @@ 0 - + sss 70 8 36 diff --git a/view/frontend/templates/snowflake.phtml b/view/frontend/templates/snowflake.phtml index e479dac..feaf096 100644 --- a/view/frontend/templates/snowflake.phtml +++ b/view/frontend/templates/snowflake.phtml @@ -22,12 +22,17 @@ declare(strict_types=1); "timeout": 0 }; - let lat, lon; - if (!getSnowflakeForce() ?>) { + // Force Snowflake + if (isForceSnow() ?>) { + letSnow(); + + // Api Enable + } else if (isApiEnable() ?>) { + let lat, lon; $.ajax(settings).done(function (response) { [lat, lon] = response.loc.split(','); $.ajax({ - url: 'getUrl($block->getAjaxUrl()) ?>lat/' + lat + '/lon/' + lon, + url: 'getUrl($block->getAjaxUrl()) ?>lat/' + lat + '/lon/' + lon, type: "POST", }).done(function (data) { if (data.is_snowing) { @@ -35,40 +40,38 @@ declare(strict_types=1); } }); }); - } else { - letSnow(); } function letSnow() { - var snowMax = getSnowflakeQty(); ?>; - var snowColor = ["#DDD", "#BBB", "#AAA", "#EEE", "#CCC"]; - var snowEntity = "getSnowflakeChar(); ?>"; - var snowVSpeed = getSnowflakeVSpeed(); ?>; - var snowHSpeed = getSnowflakeHSpeed(); ?>; - var snowRotSpeed = getSnowflakeRotSpeed(); ?>; - var snowMinSize = getSnowflakeMinSize(); ?>; - var snowMaxSize = getSnowflakeMaxSize(); ?>; - var snowRefresh = 50; - var snowMaxBlur = 3; - var snowStyles = "cursor: default; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none;"; - - var snow = [], + let snowMax = getSnowflakeQty(); ?>; + let snowColor = ["#DDD", "#BBB", "#AAA", "#EEE", "#CCC"]; + let snowEntity = "getSnowflakeChar(); ?>"; + let snowVSpeed = getSnowflakeVSpeed(); ?>; + let snowHSpeed = getSnowflakeHSpeed(); ?>; + let snowRotSpeed = getSnowflakeRotSpeed(); ?>; + let snowMinSize = getSnowflakeMinSize(); ?>; + const snowMaxSize = getSnowflakeMaxSize(); ?>; + const snowRefresh = 50; + const snowMaxBlur = 3; + const snowStyles = "cursor: default; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none;"; + + let snow = [], pos = [], coords = [], left = [], marginBottom, marginRight; - let newPostitions = ''; + let newPositions = ''; for (let i = 0; i <= snowMax; i++) { - newPostitions += ("" + snowEntity + ""); } - $('#snowflake_container').html(newPostitions); + $('#snowflake_container').html(newPositions); - var snowSize = snowMaxSize - snowMinSize; + let snowSize = snowMaxSize - snowMinSize; marginBottom = document.body.scrollHeight - 5; marginRight = document.body.clientWidth - 15; @@ -97,15 +100,14 @@ declare(strict_types=1); snow[i].rotationAngleStep = 0; snow[i].rotationAngActual = 0; if (snowRotSpeed > 0) { - let minimunRotSpeed = 1; - snow[i].rotationAngleStep = Math.floor((snowRotSpeed - minimunRotSpeed) * Math.random()) + minimunRotSpeed * ((i % 2) ? 1 : -1); + let minimumRotSpeed = 1; + snow[i].rotationAngleStep = Math.floor((snowRotSpeed - minimumRotSpeed) * Math.random()) + minimumRotSpeed * ((i % 2) ? 1 : -1); snow[i].rotationAngActual = snow[i].rotationAngleStep; } } function randomise(range) { - let rand = Math.floor(range * Math.random()); - return rand; + return Math.floor(range * Math.random()); } function resize() {