Skip to content

Commit

Permalink
Merge pull request #9 from magefan/6731-permissions-issue
Browse files Browse the repository at this point in the history
Add check if file is writable
  • Loading branch information
magefan authored Jun 27, 2022
2 parents 5eb6f78 + f043d36 commit 6069bb3
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions Observer/Magefan/TranslationSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public function __construct(
*/
private function getThemePathByStoreId(int $storeId, string $basePath, string $localCode): string
{
$themeId = (int)$this->scopeConfig->getValue('design/theme/theme_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
$themeId = (int)$this->scopeConfig->getValue(
'design/theme/theme_id',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
) ?: 3;
$theme = $this->themeProvider->getThemeById($themeId);
$themeFullPath = $theme->getFullPath();
if (!($themeFullPath)) {
Expand All @@ -102,15 +106,15 @@ private function getThemePathByStoreId(int $storeId, string $basePath, string $l
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$basePath = $this->directoryList->getPath('static');
$basePath = $this->directoryList->getPath('static');
$translation = $observer->getTranslation();
$localCode = (string)$translation->getLocale();
$storeId = (int)$translation->getStoreId();
$original = $translation->getString();
$tranlate = $translation->getTranslate();
$translate = $translation->getTranslate();
$paths = [];
if ($storeId) {
$paths[]=$this->getThemePathByStoreId($storeId, $basePath, $localCode);
$paths[] = $this->getThemePathByStoreId($storeId, $basePath, $localCode);
} else {
$paths[] = $basePath . '/adminhtml/Magento/backend/' . $localCode . '/js-translation.json';
foreach ($this->storeManager->getStores() as $store) {
Expand All @@ -120,25 +124,27 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
}
}

if (!empty($paths)) {
$changed=false;
$changed = false;
$paths = array_unique($paths);
foreach ($paths as $path) {
$exists = $this->driverFile->isExists($path);
if ($exists) {
$fileContent = $this->driverFile->fileGetContents($path);
if ($fileContent) {
try {
$jsonDecoded = $this->json->unserialize($fileContent);
} catch (\InvalidArgumentException $e) {
$jsonDecoded = [];
}
//if (isset($jsonDecoded[$original])) {
$jsonDecoded[$original] = $tranlate;
$writable = $this->driverFile->isWritable($path);
if ($writable) {
$fileContent = $this->driverFile->fileGetContents($path);
if ($fileContent) {
try {
$jsonDecoded = $this->json->unserialize($fileContent);
} catch (\InvalidArgumentException $e) {
$jsonDecoded = [];
}
$jsonDecoded[$original] = $translate;
$jsonEncoded = $this->json->serialize($jsonDecoded);
$this->driverFile->filePutContents($path, $jsonEncoded);
$changed = true;
//}
}
}
}
}
Expand Down

0 comments on commit 6069bb3

Please sign in to comment.