Skip to content

Commit

Permalink
Merge pull request #83 from AcclaroInc/release/1.7.2
Browse files Browse the repository at this point in the history
Release/1.7.2
  • Loading branch information
sidedwards authored Jul 24, 2020
2 parents c865567 + 438a95e commit b66d6cc
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"pluginName": "Translations for Craft",
"pluginDescription": "Easily launch and manage multilingual Craft websites without having to copy/paste content or manually track updates.",
"pluginVersion": "1.7.1",
"pluginVersion": "1.7.2",
"pluginAuthorName": "Acclaro",
"pluginVendorName": "Acclaro",
"pluginAuthorUrl": "http://www.acclaro.com/",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.7.2 - 2020-07-24

### Added
- Support for selecting Upload Volumes


## 1.7.1 - 2020-07-14

### Changed
Expand Down
137 changes: 92 additions & 45 deletions src/controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
use acclaro\translations\Translations;
use acclaro\translations\services\job\ImportFiles;
use acclaro\translations\services\repository\SiteRepository;
use craft\elements\Asset;
use craft\errors\UploadFailedException;
use craft\helpers\ArrayHelper;
use yii\base\ErrorException;

/**
* @author Acclaro
Expand Down Expand Up @@ -169,7 +173,6 @@ public function actionImportFile()
//Track error and success messages.
$message = "";

// Upload the file and drop it in the temporary folder
$file = UploadedFile::getInstanceByName('zip-upload');

//Get Order Data
Expand All @@ -187,72 +190,72 @@ public function actionImportFile()
$this->showUserMessages("Invalid extention: The plugin only support [ZIP, XML] files.");
}

$fileName = Assets::prepareAssetName($file->name, true, true);
$folderPath = Craft::$app->path->getTempAssetUploadsPath().'/';
FileHelper::clearDirectory($folderPath);

//If is a Zip File
if ($file->extension === 'zip') {
//Unzip File ZipArchive
$zip = new \ZipArchive();
if (move_uploaded_file($file->tempName, $folderPath.$fileName)) {

if ($zip->open($folderPath.$fileName)) {
$xmlPath = $folderPath.$orderId;
$assetPath = $file->saveAsTempFile();

if ($zip->open($assetPath)) {
$xmlPath = $assetPath.$orderId;

$zip->extractTo($xmlPath);

$fileName = preg_replace('/\\.[^.\\s]{3,4}$/', '', Assets::prepareAssetName($file->name));

$zip->extractTo($xmlPath);
$files = FileHelper::findFiles($assetPath.$orderId);

$fileName = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fileName);
$assetIds = [];

$files = FileHelper::findFiles($folderPath.$orderId);
foreach ($files as $key => $file) {
if (! is_bool(strpos($file, '__MACOSX'))) {
unlink($file);

foreach ($files as $key => $file) {
rename($file, $folderPath.$orderId.'/'.pathinfo($file)['basename']);
continue;
}

FileHelper::removeDirectory($folderPath.$orderId.'/'.$fileName);
$filename = Assets::prepareAssetName($file);

$uploadVolumeId = ArrayHelper::getValue(Translations::getInstance()->getSettings(), 'uploadVolume');

$folder = Craft::$app->getAssets()->getRootFolderByVolumeId($uploadVolumeId);

$pathInfo = pathinfo($file);

$zip->close();
$compatibleFilename = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.txt';

$job = Craft::$app->queue->push(new ImportFiles([
'description' => 'Updating translation drafts',
'orderId' => $orderId,
'totalFiles' => $total_files,
'xmlPath' => $xmlPath,
]));
rename($file, $compatibleFilename);

if ($job) {
$params = [
'id' => (int) $job,
'notice' => 'Done updating translation drafts',
'url' => 'translations/orders/detail/'. $orderId
];
Craft::$app->getView()->registerJs('$(function(){ Craft.Translations.trackJobProgressById(true, false, '. json_encode($params) .'); });');
$asset = new Asset();
$asset->tempFilePath = $compatibleFilename;
$asset->filename = $compatibleFilename;
$asset->newFolderId = $folder->id;
$asset->volumeId = $folder->volumeId;
$asset->avoidFilenameConflicts = true;
$asset->uploaderId = Craft::$app->getUser()->getId();
$asset->setScenario(Asset::SCENARIO_CREATE);

if (! Craft::$app->getElements()->saveElement($asset)) {
$errors = $asset->getFirstErrors();

return $this->asErrorJson(Craft::t('app', 'Failed to save the asset:') . ' ' . implode(";\n", $errors));
}
// $this->redirect('translations/orders/detail/'. $orderId, 302, true);
$this->showUserMessages("File uploaded successfully: $fileName", true);
} else {
$this->showUserMessages("Unable to unzip ". $file->name ." Operation not permitted or Decompression Failed ");

$assetIds[] = $asset->id;
}
} else {
$this->showUserMessages("Unable to upload file: $fileName");
}
} elseif ($file->extension === 'xml') {
$xmlPath = $folderPath.$orderId;

mkdir($xmlPath, 0777, true);
FileHelper::removeDirectory($assetPath.$orderId.'/'.$fileName);

//Upload File
if( move_uploaded_file($file->tempName, $xmlPath.'/'.$fileName)) {
$zip->close();

// This generally executes too fast for page to refresh
$job = Craft::$app->queue->push(new ImportFiles([
'description' => 'Updating translation drafts',
'orderId' => $orderId,
'totalFiles' => $total_files,
'xmlPath' => $xmlPath,
'assets' => $assetIds,
]));

if ($job) {
$params = [
'id' => (int) $job,
Expand All @@ -261,11 +264,55 @@ public function actionImportFile()
];
Craft::$app->getView()->registerJs('$(function(){ Craft.Translations.trackJobProgressById(true, false, '. json_encode($params) .'); });');
}

// $this->redirect('translations/orders/detail/'. $orderId, 302, true);
$this->showUserMessages("File uploaded successfully: $fileName", true);
} else {
$this->showUserMessages("Unable to upload file: $fileName");
$this->showUserMessages("Unable to unzip ". $file->name ." Operation not permitted or Decompression Failed ");
}
} elseif ($file->extension === 'xml') {
$filename = Assets::prepareAssetName($file->name);

$uploadVolumeId = ArrayHelper::getValue(Translations::getInstance()->getSettings(), 'uploadVolume');

$folder = Craft::$app->getAssets()->getRootFolderByVolumeId($uploadVolumeId);

$compatibleFilename = $file->tempName . '.txt';

rename($file->tempName, $compatibleFilename);

$asset = new Asset();
$asset->tempFilePath = $compatibleFilename;
$asset->filename = $compatibleFilename;
$asset->newFolderId = $folder->id;
$asset->volumeId = $folder->volumeId;
$asset->avoidFilenameConflicts = true;
$asset->uploaderId = Craft::$app->getUser()->getId();
$asset->setScenario(Asset::SCENARIO_CREATE);

if (! Craft::$app->getElements()->saveElement($asset)) {
$errors = $asset->getFirstErrors();

return $this->asErrorJson(Craft::t('app', 'Failed to save the asset:') . ' ' . implode(";\n", $errors));
}

// This generally executes too fast for page to refresh
$job = Craft::$app->queue->push(new ImportFiles([
'description' => 'Updating translation drafts',
'orderId' => $orderId,
'totalFiles' => $total_files,
'assets' => [$asset->id],
]));

if ($job) {
$params = [
'id' => (int) $job,
'notice' => 'Done updating translation drafts',
'url' => 'translations/orders/detail/'. $orderId
];
Craft::$app->getView()->registerJs('$(function(){ Craft.Translations.trackJobProgressById(true, false, '. json_encode($params) .'); });');
}

$this->showUserMessages("File uploaded successfully: {$file->name}", true);
} else {
$this->showUserMessages("Invalid extention: The plugin only support [ZIP, XML] files.");
}
Expand Down
20 changes: 19 additions & 1 deletion src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use craft\helpers\FileHelper;
use acclaro\translations\models\Settings;
use acclaro\translations\services\job\DeleteDrafts;
use craft\base\VolumeInterface;

/**
* @author Acclaro
Expand Down Expand Up @@ -232,6 +233,22 @@ public function actionConfigurationOptions()
}

$variables['chkDuplicateEntries'] = Translations::getInstance()->settings->chkDuplicateEntries;
$variables['uploadVolume'] = Translations::getInstance()->settings->uploadVolume;

$allVolumes = Craft::$app->getVolumes()->getAllVolumes();

$variables['volumeOptions'] = array_map(function (VolumeInterface $volume) {
return [
'label' => $volume->name,
'value' => $volume->id,
];
}, $allVolumes);

// Add default temp uploads option
array_unshift($variables['volumeOptions'], [
'label' => 'Temp Uploads',
'value' => 0,
]);

$this->renderTemplate('translations/settings/configuration-options', $variables);
}
Expand All @@ -245,12 +262,13 @@ public function actionSaveConfigurationOptions()

$request = Craft::$app->getRequest();
$duplicateEntries = $request->getParam('chkDuplicateEntries');
$selectedVolume = $request->getParam('uploadVolume');

try {

$pluginService = Craft::$app->getPlugins();
$plugin = $pluginService->getPlugin('translations');
if (!$pluginService->savePluginSettings($plugin, ['chkDuplicateEntries' => $duplicateEntries])) {
if (!$pluginService->savePluginSettings($plugin, ['chkDuplicateEntries' => $duplicateEntries, 'uploadVolume' => $selectedVolume])) {
Craft::$app->getSession()->setError(Translations::$plugin->translator->translate('app', 'Unable to save setting.'));
} else {
Craft::$app->getSession()->setNotice(Translations::$plugin->translator->translate('app', 'Setting saved.'));
Expand Down
3 changes: 3 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Settings extends Model
{
public $chkDuplicateEntries = true;

/** @var int The Volume ID where uploads will be saved */
public $uploadVolume = 0;

public function rules()
{
return [];
Expand Down
Loading

0 comments on commit b66d6cc

Please sign in to comment.