Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Theme Selection setting & auto theme switch feature #2

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/module-magespices-core/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</tab>
<section id="mage2dark" translate="label" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label><![CDATA[Mage2Dark Theme Feedback]]></label>
<label><![CDATA[Mage2Dark Theme]]></label>
<tab>magespices</tab>
<resource>MageSpices_Core::config</resource>
<group id="settings" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1"
Expand Down
133 changes: 133 additions & 0 deletions src/module-magespices-mage2dark/Block/Adminhtml/Config/TimeSlider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
namespace MageSpices\Mage2Dark\Block\Adminhtml\Config;


use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\Form\Element\Fieldset;
use Magento\Framework\Data\Form\Element\Renderer\RendererInterface;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;

/**
* Class TimeSlider
*/
class TimeSlider extends Template implements RendererInterface, BlockInterface
{
const BASE_CONFIG_PATH = "mage2dark/settings/";
const TIME_NAME_FROM = 'light_theme_time_from';
const TIME_NAME_TO = 'light_theme_time_to';

/**
* Form element which re-rendering
*
* @var Fieldset
*/
protected $element;

/**
* @var string
*/
protected $_template = 'MageSpices_Mage2Dark::config/renderer/time_slider.phtml';

/**
* @var string
*/
protected $_htmlId = 'time-range';

/**
* Retrieve an element
*
* @return Fieldset
*/
public function getElement()
{
return $this->element;
}

/**
* Set an element
*
* @param AbstractElement $element
* @return $this
*/
public function setElement(AbstractElement $element)
{
$this->element = $element;

return $this;
}

/**
* Render element
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$this->element = $element;

return $this->toHtml();
}

/**
* @return string
*/
public function getHtmlId()
{
return $this->_htmlId;
}

/**
* @return string
*/
public function getNameFrom()
{
return str_replace("/", "_", self::BASE_CONFIG_PATH).self::TIME_NAME_FROM;
}

/**
* @return string
*/
public function getNameTo()
{
return str_replace("/", "_", self::BASE_CONFIG_PATH).self::TIME_NAME_TO;
}

public function getTimeFrom()
{
return $this->_scopeConfig->getValue(self::BASE_CONFIG_PATH.self::TIME_NAME_FROM);
}

public function getTimeTo()
{
return $this->_scopeConfig->getValue(self::BASE_CONFIG_PATH.self::TIME_NAME_TO);
}

/**
* @param int $minutes
* @return string
*/
public function minutesToTime($minutes)
{
$hours = floor($minutes / 60);
$minutes = $minutes % 60;
$part = $hours >= 12 ? 'PM' : 'AM';

return sprintf('%02d:%02d %s', $hours, $minutes, $part);
}

/**
* @return string
* @throws ValidatorException
*/
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}

return $this->fetchView($this->getTemplateFile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace MageSpices\Mage2Dark\Model\Config\FrontendModel;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Exception\LocalizedException;

class TimeSlider extends Field
{

/**
* Retrieve element HTML markup
*
* @param AbstractElement $element
* @return string
* @throws LocalizedException
*/
protected function _getElementHtml(AbstractElement $element)
{
$renderer = $this->getLayout()->createBlock(
\MageSpices\Mage2Dark\Block\Adminhtml\Config\TimeSlider::class
);
$renderer->setElement($element);

return $renderer->toHtml();
}
}
25 changes: 25 additions & 0 deletions src/module-magespices-mage2dark/Model/Config/Source/Selection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace MageSpices\Mage2Dark\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class Selection implements OptionSourceInterface
{
public function toOptionArray()
{
return [
[
"value" => "auto",
"label" => __("Auto")
],
[
"value" => "dark",
"label" => __("Dark")
],
[
"value" => "light",
"label" => __("Light")
]
];
}
}
69 changes: 69 additions & 0 deletions src/module-magespices-mage2dark/Plugin/Model/View/Design.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace MageSpices\Mage2Dark\Plugin\Model\View;

use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class Design
{
const BASE_CONFIG_PATH = "mage2dark/settings/";

protected $scopeConfig;

protected $timeZone;

protected $themeSelectionConfig;

public function __construct(
ScopeConfigInterface $scopeConfig,
TimezoneInterface $timezone
) {
$this->scopeConfig = $scopeConfig;
$this->timeZone = $timezone;
}

/**
* @param \Magento\Theme\Model\View\Design $subject
* @param string|null $area
* @param $result
* @return string
*/
public function afterGetConfigurationDesignTheme(
\Magento\Theme\Model\View\Design $subject,
$result,
$area = null
) {
if (!$area) {
$area = $subject->getArea();
}
if ($area == Area::AREA_ADMINHTML && $this->getThemeSelectionConfig() == "dark") {
return "MageSpices/Mage2Dark";
}
return $result;
}

protected function getThemeSelectionConfig()
{
if (!$this->themeSelectionConfig) {
$this->themeSelectionConfig = $this->getConfig('theme_selection');
if ($this->themeSelectionConfig == "auto") {
$currentDate = $this->timeZone->date();
$currentTime = ($currentDate->format("H")*60)+$currentDate->format("i");
$lightThemeStartTime = $this->getConfig("light_theme_time_from");
$lightThemeEndTime = $this->getConfig("light_theme_time_to");
if ($currentTime >= $lightThemeStartTime && $currentTime <= $lightThemeEndTime) {
$this->themeSelectionConfig = "light";
} else {
$this->themeSelectionConfig = "dark";
}
}
}
return $this->themeSelectionConfig;
}

protected function getConfig($path)
{
return $this->scopeConfig->getValue(self::BASE_CONFIG_PATH.$path);
}
}
6 changes: 6 additions & 0 deletions src/module-magespices-mage2dark/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Theme\Model\View\Design">
<plugin name="mageSpicesMage2DarkDesign" type="MageSpices\Mage2Dark\Plugin\Model\View\Design"/>
</type>
</config>
43 changes: 43 additions & 0 deletions src/module-magespices-mage2dark/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
/**
* Created by Q-Solutions Studio
*
* @category MageSpices
* @package MageSpices_Core
* @author Sebastian Strojwas <sebastian@qsolutionsstudio.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="mage2dark">
<group id="settings">
<field id="theme_selection" translate="label comment" type="select" sortOrder="20" showInDefault="1">
<label>Theme Selection</label>
<source_model>MageSpices\Mage2Dark\Model\Config\Source\Selection</source_model>
<comment>If "Auto" is selected, then please select time slot for light theme from next setting and outside of that time slot Dark theme will be in use.</comment>
</field>
<field id="light_theme_time" translate="label comment" type="text" sortOrder="30" showInDefault="1">
<label>Light Theme</label>
<frontend_model>MageSpices\Mage2Dark\Model\Config\FrontendModel\TimeSlider</frontend_model>
<depends>
<field id="theme_selection">auto</field>
</depends>
</field>
<field id="light_theme_time_from" translate="label comment" type="hidden" sortOrder="40" showInDefault="1">
<label>Light Theme Time From</label>
<depends>
<field id="theme_selection">auto</field>
</depends>
</field>
<field id="light_theme_time_to" translate="label comment" type="hidden" sortOrder="50" showInDefault="1">
<label>Light Theme Time To</label>
<depends>
<field id="theme_selection">auto</field>
</depends>
</field>
</group>
</section>
</system>
</config>
12 changes: 12 additions & 0 deletions src/module-magespices-mage2dark/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<mage2dark>
<settings>
<theme_selection>auto</theme_selection>
<light_theme_time_from>420</light_theme_time_from>
<light_theme_time_to>1140</light_theme_time_to>
</settings>
</mage2dark>
</default>
</config>
20 changes: 0 additions & 20 deletions src/module-magespices-mage2dark/etc/di.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="MageSpices_Mage2Dark::css/config.css"/>
</head>
</page>
Loading