Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JaJuMa committed Dec 5, 2023
0 parents commit 55008aa
Show file tree
Hide file tree
Showing 16 changed files with 881 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Block/QuickAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotQuickTranslation\Block;

use Jajuma\PotQuickTranslation\Helper\Config as HelperConfig;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class QuickAction extends \Jajuma\PowerToys\Block\PowerToys\QuickAction
{
/**
* @var HelperConfig
*/
protected HelperConfig $helperConfig;

/**
* @param Context $context
* @param HelperConfig $helperConfig
* @param array $data
*/
public function __construct(
Template\Context $context,
HelperConfig $helperConfig,
array $data = []
) {
parent::__construct($context, $data);
$this->helperConfig = $helperConfig;
}

/**
* Is enable
*
* @return bool
*/
public function isEnable(): bool
{
return $this->helperConfig->isEnable();
}
}
43 changes: 43 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotQuickTranslation\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Config extends AbstractHelper
{
public const XML_PATH_ENABLE = 'power_toys/pot_quick_translation/is_enabled';

public const XML_PATH_AUTO_FLUSH_TRANSLATE_CACHE = 'power_toys/pot_quick_translation/auto_flush_translate_cache';

/**
* @param Context $context
*/
public function __construct(
Context $context
) {
parent::__construct($context);
}

/**
* Is enable
*
* @return bool
*/
public function isEnable(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLE);
}

/**
* Auto flush translate cache
*
* @return bool
*/
public function isAutoFlushTranslateCache(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_AUTO_FLUSH_TRANSLATE_CACHE);
}
}
124 changes: 124 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotQuickTranslation\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Cache\TypeListInterface;

class Data extends AbstractHelper
{
protected const XML_PATH_DEV_TRANSLATE_INLINE_ACTIVE = 'dev/translate_inline/active';

protected const XML_PATH_DEV_TRANSLATE_INLINE_ACTIVE_ADMIN = 'dev/translate_inline/active_admin';

protected const XML_PATH_DEV_RESTRICT_ALLOW_IPS = 'dev/restrict/allow_ips';

protected const XML_PATH_JAJUMA_TRANSLATE_IS_ENABLED = 'jajuma_translation/general/is_enabled';

protected const XML_PATH_JAJUMA_TRANSLATE_INLINE_ACTIVE = 'jajuma_translation/general/translate_inline';

protected const XML_PATH_JAJUMA_RESTRICT_ALLOW_IPS = 'jajuma_translation/general/allow_ips';

/**
* @var WriterInterface
*/
protected WriterInterface $writer;

/**
* @var TypeListInterface
*/
protected TypeListInterface $typeList;

/**
* @param Context $context
* @param WriterInterface $writer
* @param TypeListInterface $typeList
*/
public function __construct(
Context $context,
WriterInterface $writer,
TypeListInterface $typeList
) {
parent::__construct($context);
$this->writer = $writer;
$this->typeList = $typeList;
}

/**
* Get translate inline config
*
* @return mixed
*/
public function getTranslateInlineConfig($isHyvaTranslation = false)
{
$translateInlineActive = $this->scopeConfig->getValue(self::XML_PATH_DEV_TRANSLATE_INLINE_ACTIVE);
if ($isHyvaTranslation) {
$hyvaTranslationEnabled = $this->scopeConfig->getValue(self::XML_PATH_JAJUMA_TRANSLATE_IS_ENABLED);
$hyvaTranslationInlineActive = $this->scopeConfig->getValue(self::XML_PATH_JAJUMA_TRANSLATE_INLINE_ACTIVE);
return $translateInlineActive && $hyvaTranslationEnabled && $hyvaTranslationInlineActive;
}
return $translateInlineActive;
}

/**
* Get translate inline config backend
*
* @return mixed
*/
public function getTranslateInlineConfigBackend()
{
return $this->scopeConfig->getValue(self::XML_PATH_DEV_TRANSLATE_INLINE_ACTIVE_ADMIN);
}

/**
* Set translate inline config
*
* @param $value
* @param bool $isHyvaTranslation
* @return void
*/
public function setTranslateInlineConfig($value, bool $isHyvaTranslation = false)
{
if ($isHyvaTranslation) {
$this->writer->save(self::XML_PATH_JAJUMA_TRANSLATE_IS_ENABLED, $value);
$this->writer->save(self::XML_PATH_JAJUMA_TRANSLATE_INLINE_ACTIVE, $value);
}
$this->writer->save(self::XML_PATH_DEV_TRANSLATE_INLINE_ACTIVE, $value);

$types = array_keys($this->typeList->getTypes());
foreach ($types as $type) {
$this->typeList->cleanType($type);
}
}

/**
* Set translate inline config backend
*
* @param $value
* @return void
*/
public function setTranslateInlineConfigBackend($value)
{
$this->writer->save(self::XML_PATH_DEV_TRANSLATE_INLINE_ACTIVE_ADMIN, $value);
$this->typeList->cleanType('config');
$this->typeList->cleanType('block_html');
}

/**
* Set translate allow ips
*
* @param $value
* @param bool $isHyvaTranslation
* @return void
*/
public function setTranslateAllowIps($value, bool $isHyvaTranslation = false)
{
if ($isHyvaTranslation) {
$this->writer->save(self::XML_PATH_JAJUMA_RESTRICT_ALLOW_IPS, $value);
}
$this->writer->save(self::XML_PATH_DEV_RESTRICT_ALLOW_IPS, $value);
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 JaJuMa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
141 changes: 141 additions & 0 deletions Magewire/QuickAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
declare(strict_types=1);

namespace Jajuma\PotQuickTranslation\Magewire;

use Magewirephp\Magewire\Component;
use Jajuma\PotQuickTranslation\Helper\Data;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\View\DesignInterface;
use Magento\Framework\Module\Manager;

class QuickAction extends Component
{
protected $loader = true;

protected $status = '';

/**
* @var Data
*/
protected Data $helper;

/**
* @var RemoteAddress
*/
protected RemoteAddress $remoteAddress;

/**
* @var DesignInterface
*/
protected DesignInterface $design;

/**
* @var Manager
*/
protected Manager $moduleManager;

/**
* @param Data $helper
* @param RemoteAddress $remoteAddress
* @param DesignInterface $design
* @param Manager $moduleManager
*/
public function __construct(
Data $helper,
RemoteAddress $remoteAddress,
DesignInterface $design,
Manager $moduleManager
) {
$this->helper = $helper;
$this->remoteAddress = $remoteAddress;
$this->design = $design;
$this->moduleManager = $moduleManager;
}

/**
* Save coupon
*
* @return void
*/
public function execute()
{
if (!$this->validateHyvaTranslation()) {
$message = 'Please enable Jajuma_HyvaTranslation extension';
$this->dispatchBrowserEvent('finish-quick-translation', ['message' => $message]);
return;
}

$value = '1';
$currentIp = $this->remoteAddress->getRemoteAddress();
if ($this->helper->getTranslateInlineConfig() == '1') {
$value = '0';
}
$isHyvaTranslation = $this->moduleManager->isEnabled('Jajuma_HyvaTranslation');
$this->helper->setTranslateAllowIps($currentIp, $isHyvaTranslation);
$this->helper->setTranslateInlineConfig($value, $isHyvaTranslation);
$this->status = $value;
$this->dispatchBrowserEvent('finish-quick-translation', ['status' => $value]);
}

/**
* Backend execute
*
* @return void
*/
public function backendExecute()
{
$value = '1';
$currentIp = $this->remoteAddress->getRemoteAddress();
if ($this->helper->getTranslateInlineConfigBackend() == '1') {
$value = '0';
}

$this->helper->setTranslateAllowIps($currentIp);
$this->helper->setTranslateInlineConfigBackend($value);
$this->dispatchBrowserEvent('finish-quick-translation');
}

/**
* Get current status
*
* @return bool|mixed
*/
public function getCurrentStatus()
{
if ($this->status !== '') {
return $this->status;
}

$isHyvaTranslation = $this->moduleManager->isEnabled('Jajuma_HyvaTranslation');
return $this->helper->getTranslateInlineConfig($isHyvaTranslation);
}

/**
* Get current status backend
*
* @return mixed
*/
public function getCurrentStatusBackend()
{
return $this->helper->getTranslateInlineConfigBackend();
}

/**
* Validate hyva translation
*
* @return bool
*/
protected function validateHyvaTranslation(): bool
{
$theme = $this->design->getDesignTheme();
while ($theme) {
if (strpos($theme->getCode(), 'Hyva/') === 0) {
return $this->moduleManager->isEnabled('Jajuma_HyvaTranslation');
}
$theme = $theme->getParentTheme();
}

return true;
}
}
Loading

0 comments on commit 55008aa

Please sign in to comment.