Commit c021693d authored by Loparev's avatar Loparev
Browse files

Set batch operation per job + job item

parent e4e36b4f
......@@ -4,7 +4,7 @@ dependencies:
module:
- tmgmt
id: tmgmt_smartling_download_by_job_items_job_action
label: 'Download Translation by TMGMT Job Items'
label: 'Download Translation (by job items)'
type: tmgmt_job
plugin: tmgmt_smartling_download_by_job_items_job_action
configuration: { }
......@@ -81,4 +81,4 @@ action.configuration.tmgmt_smartling_send_context_action:
action.configuration.tmgmt_smartling_download_by_job_items_job_action:
type: action_configuration_default
label: 'Download Translation by TMGMT Job Items'
label: 'Download Translation (by job items)'
......@@ -3,6 +3,9 @@
namespace Drupal\tmgmt_smartling\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;
use Drupal\tmgmt_extension_suit\Form\BaseTmgmtActionApproveForm;
......@@ -11,6 +14,13 @@ use Drupal\tmgmt_extension_suit\Form\BaseTmgmtActionApproveForm;
*/
class DownloadByJobItemsTmgmtActionApproveForm extends BaseTmgmtActionApproveForm {
/**
* Temp storage name we are saving entity_ids to.
*
* @var string
*/
protected $tempStorageName = 'tmgmt_smartling_tmgmt_job_operations_download_by_job_items';
/**
* {@inheritdoc}
*/
......@@ -22,7 +32,7 @@ class DownloadByJobItemsTmgmtActionApproveForm extends BaseTmgmtActionApproveFor
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Download Translation by TMGMT Job Items');
return $this->t('Download Translation (by job items)');
}
/**
......@@ -39,17 +49,87 @@ class DownloadByJobItemsTmgmtActionApproveForm extends BaseTmgmtActionApproveFor
return $this->t('Downloading can take some time, do not close the browser');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_user_id = $this->currentUser()->id();
$temp_storage_name = $this->getTempStorageName();
// Clear out the accounts from the temp store.
$this->tempStoreFactory->get($temp_storage_name)->delete($current_user_id);
if (!$form_state->getValue('confirm')) {
return;
}
$operations = [];
foreach ($this->entityIds as $id => $entity_type) {
$job = Job::load($id);
if (empty($job)) {
continue;
}
foreach ($job->getItems() as $item) {
$batch_item_data = [
'tjid' => $job->id(),
'tjiid' => $item->id()
];
$operations[] = [
[get_class($this), 'processBatch'],
[$batch_item_data],
];
}
}
if (!empty($operations)) {
$batch = [
'title' => $this->getConfirmText(),
'operations' => $operations,
'finished' => [get_class($this), 'finishBatch'],
];
batch_set($batch);
}
else {
$form_state->setRedirect('system.admin_content');
}
}
/**
* {@inheritdoc}
*/
public static function processBatch(array $data, array &$context) {
$job = parent::processBatch($data, $context);
if (!isset($context['results']['errors'])) {
$context['results']['errors'] = [];
$context['results']['count'] = 0;
}
if (!empty($job) && $translator = $job->getTranslator()) {
$job = Job::load($data["tjid"]);
$job_item = JobItem::load($data["tjiid"]);
if (!$job) {
$context['results']['errors'][] = t('TMGMT Job @id not found', [
'@id' => $data["tjid"]
]);
}
if (!$job_item) {
$context['results']['errors'][] = t('TMGMT Job Item @id not found', [
'@id' => $data["tjiid"]
]);
}
if ($translator = $job->getTranslator()) {
$plugin = $translator->getPlugin();
if ($plugin instanceof ExtendedTranslatorPluginInterface &&
$plugin->downloadTranslation($job)) {
if (
$plugin instanceof ExtendedTranslatorPluginInterface &&
$plugin->downloadTranslation($job, $job_item)
) {
$context['results']['count']++;
}
else {
......@@ -66,7 +146,7 @@ class DownloadByJobItemsTmgmtActionApproveForm extends BaseTmgmtActionApproveFor
}
else {
$context['message'] = new FormattableMarkup('Skipped %name.', [
'%name' => $data['entity_type'],
'%name' => $job->label(),
]);
}
}
......
......@@ -9,7 +9,7 @@ use Drupal\tmgmt_extension_suit\Plugin\Action\BaseJobAction;
*
* @Action(
* id = "tmgmt_smartling_download_by_job_items_job_action",
* label = @Translation("Download Translation by TMGMT Job Items"),
* label = @Translation("Download Translation (by job items)"),
* type = "tmgmt_job",
* confirm_form_route_name = "tmgmt_smartling.download_by_job_items_approve_action"
* )
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment