-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmgmt_lilt.module
241 lines (205 loc) · 8.13 KB
/
tmgmt_lilt.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* @file
* Module file of the Lilt Translator module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt_lilt\Plugin\tmgmt\Translator\LiltTranslator;
/**
* Implements hook_entity_operation().
*/
function tmgmt_lilt_entity_operation(EntityInterface $entity) {
// Add Job Ops:
/** @var Drupal\tmgmt\Entity\Job $entity */
if ($entity->bundle() == 'tmgmt_job' && $entity->hasTranslator() && $entity->getTranslatorId() == 'lilt') {
$project_id = LiltTranslator::getJobProjectId($entity);
if (!$project_id) {
return;
}
$operations['lilt_view'] = [
'url' => Url::fromUri(LiltTranslator::getLiltAppUrl($entity->getTranslator()) . 'projects/details/' . $project_id, [
'attributes' => [
'target' => '_blank',
],
'query' => [],
]),
'title' => t('View on Lilt'),
'weight' => 10,
];
return $operations;
}
/** @var Drupal\tmgmt\Entity\JobItem $entity */
// Add Job Item Ops:
if ($entity->bundle() == 'tmgmt_job_item') {
$remote = LiltTranslator::getJobItemMapping($entity);
list('module_name' => $module_name, 'document_id' => $document_id, 'project_id' => $project_id) = $remote;
if (!$project_id || $module_name != 'tmgmt_lilt') {
return;
}
$operations['lilt_view'] = [
'url' => Url::fromUri(LiltTranslator::getLiltAppUrl($entity->getTranslator()) . 'projects/details/' . $project_id . '/edit-document/' . $document_id, [
'attributes' => [
'target' => '_blank',
],
'query' => [],
]),
'title' => t('View on Lilt'),
'weight' => 10,
];
return $operations;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tmgmt_lilt_form_tmgmt_job_abort_form_alter(&$form, $form_state) {
/** @var Drupal\tmgmt\Entity\Job $job */
$job = \Drupal::routeMatch()->getParameter('tmgmt_job');
if (empty($job) || !$job->hasTranslator() || $job->getTranslatorId() != 'lilt') {
return;
}
$confirmation_message = t("This will send a request to Lilt to abort the job.");
$form['description']['#markup'] = $confirmation_message->render();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tmgmt_lilt_form_tmgmt_job_delete_form_alter(&$form, $form_state) {
/** @var Drupal\tmgmt\Entity\Job $job */
$job = $form_state->getFormObject()->getEntity();
if ($job->getState() == Job::STATE_UNPROCESSED) {
return;
}
if (!$job->hasTranslator() || !$job->getTranslatorId() == 'lilt') {
return;
}
$jobs_items = $job->getItems();
if (!empty($jobs_items)) {
$remote = LiltTranslator::getJobItemMapping(reset($jobs_items));
$form['tmgmt_lilt_project_id'] = [
'#type' => 'hidden',
'#value' => $remote['project_id'],
];
$form['actions']['submit']['#submit'][] = 'tmgmt_lilt_form_tmgmt_job_delete_form_delete_submit';
}
}
/**
* Handle job deletion confirmation by archiving project on Lilt.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function tmgmt_lilt_form_tmgmt_job_delete_form_delete_submit(array $form, FormStateInterface $form_state) {
/** @var Drupal\tmgmt\Entity\Job $job */
$job = $form_state->getFormObject()->getEntity();
if ($project_id = $form_state->getValue('tmgmt_lilt_project_id')) {
$translator = $job->getTranslatorPlugin();
$translator->setTranslator($job->getTranslator());
$translator->archiveLiltProject($project_id);
$job->addMessage('Job deleted. Archived Lilt Project @project_id.', ['@project_id' => $project_id], 'debug');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tmgmt_lilt_form_tmgmt_job_item_edit_form_alter(&$form, $form_state) {
// Retrieve job item data:
/** @var Drupal\tmgmt\Entity\JobItem $job_item */
$job_item = $form_state->getFormObject()->getEntity();
/** @var Drupal\tmgmt\Entity\Job $job */
$job = $job_item->getJob();
if (!$job->hasTranslator() || !$job->getTranslatorId() == 'lilt' || $job_item->bundle() != 'tmgmt_job_item') {
return;
}
if (!$job_item->isState(JobItemInterface::STATE_REVIEW)) {
return;
}
// Add submission handler to check for completed projects:
$form['actions']['accept']['#submit'][] = 'tmgmt_lilt_form_tmgmt_job_item_edit_form_accept_submit';
/** @var Drupal\tmgmt_lilt\LiltTranslator $plugin */
$plugin = $job_item->getTranslatorPlugin();
$plugin->setTranslator($job_item->getTranslator());
$remote = LiltTranslator::getJobItemMapping($job_item);
list('document_id' => $document_id) = $remote;
/** @var array $lilt_doc */
$lilt_doc = $plugin->getLiltDocument($document_id);
if (empty($lilt_doc) || !array_key_exists('status', $lilt_doc)) {
return;
}
// Warnings:
if (!$form_state->isRebuilding()) {
\Drupal::messenger()->addWarning(t('Please share any QA feedback with your services manager so the updates can be made in Lilt.'));
if ($lilt_doc['status'] === 'inReview') {
\Drupal::messenger()->addWarning(t('If content is not accepted within 7 days, it will be automatically validated by Lilt with no possible revision request.'));
}
elseif ($lilt_doc['status'] === 'inProgress') {
\Drupal::messenger()->addWarning(t('Please note that Lilt Document for this job item is in status "incomplete" and you wont be able to accept the translation until Lilt author finishes his work.'));
}
elseif ($lilt_doc['status'] === 'done') {
\Drupal::messenger()->addWarning(t('Please note that Lilt Document for this job item is already completed.'));
}
}
}
/**
* Archive Lilt projects on accepting all translation segments.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function tmgmt_lilt_form_tmgmt_job_item_edit_form_accept_submit(array $form, FormStateInterface $form_state) {
/** @var Drupal\tmgmt\Entity\JobItem $job_item */
$job_item = $form_state->getFormObject()->getEntity();
/** @var Drupal\tmgmt\Entity\Job $job */
$job = $job_item->getJob();
if (!$job->hasTranslator() || !$job->getTranslatorId() == 'lilt') {
return;
}
if ($job->getState() == Job::STATE_FINISHED) {
$remote = LiltTranslator::getJobItemMapping($job_item);
$translator = $job->getTranslatorPlugin();
$translator->setTranslator($job->getTranslator());
$translator->archiveLiltProject($remote['project_id']);
$job->addMessage('Job finished. Archived Lilt Project @project_id.', ['@project_id' => $remote['project_id']], 'debug');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tmgmt_lilt_form_tmgmt_job_edit_form_alter(&$form, $form_state) {
/** @var Drupal\tmgmt\Entity\Job $job */
$job = $form_state->getFormObject()->getEntity();
if (!$job->hasTranslator() || !$job->getTranslatorId() == 'lilt') {
return;
}
// If we have multiple jobs/target langs, the Lilt TMs can't be the same.
if (isset($form['translator_wrapper']['submit_all'])) {
unset($form['translator_wrapper']['submit_all']);
}
// If job is part of a queue re-use the last used label.
$job = $form_state->getFormObject()->getEntity();
$job_queue = \Drupal::service('tmgmt.queue');
if ($job->isSubmittable() && $job_queue->isJobInQueue($job) && ($job_queue->count() + $job_queue->getProcessed()) > 1) {
if (isset($form['label']['widget'][0]['value'])) {
$last_label = isset($_SESSION['tmgmt_lilt']['last_job_label']) ? $_SESSION['tmgmt_lilt']['last_job_label'] : '';
$label_widget = &$form['label']['widget'][0]['value'];
$label_widget['#default_value'] = ($label_widget['#default_value'] == '') ? $last_label : $label_widget['#default_value'];
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tmgmt_lilt_form_views_exposed_form_alter(&$form, $form_state) {
$view = $form_state->get('view');
if (!empty($view) && $view->id() == 'tmgmt_job_overview') {
\Drupal::messenger()->addWarning(t('NOTE: Drupal word count may differ from Lilt.'));
}
}