summaryrefslogtreecommitdiffstats
path: root/lingotek.page.inc
blob: 4d64e5b74e72bef6148ea8c3c9a7fc63202330be (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
 * @file
 * Lingotek Tab for Nodes
 */

/**
 * Page callback for the Lingotek local task on node detail pages.
 *
 * Construct the table summarizing information a Product Manager would want
 * to know about the progress of the translations.
 *
 * @return array
 *   A Drupal render array of the page contents.
 */
function lingotek_pm($node) {
  $output = array();

  drupal_add_js(drupal_get_path('module', 'lingotek') . '/js/lingotek.pm.js');

  if (class_exists('LingotekDocument')) {
    $checkbox = '<input type="checkbox" onchange="lingotek.pm.toggle_checkboxes(this);" />';
    $headers = array(
      $checkbox,
      t('Target Language'),
      t('Document Progress'),
      t('Phase'),
      t('Phase Progress'),
      t('Phase Complete'),
    );

    $document = LingotekDocument::load(lingotek_lingonode($node->nid, 'document_id_' . $node->language));

    $progress = lingotek_get_document_targets($document->document_id, TRUE);

    $rows = array();
    foreach ($progress as $language => $target) {
      $current_phase = $document->currentPhase($target->id);
      $phase_complete = ($current_phase->isMarkedComplete) ? TRUE : FALSE;
      $phase_complete_percent = $current_phase->percentComplete;
      if (empty($phase_complete_percent)) {
        $phase_complete_percent = 0;
      }
      $row = array(
        '<input type="checkbox" tag="lingotek_pm_row" language="' . $language . '" />',
        lingotek_language_native($language) . ' (' . lingotek_language_name($language) . ')',
        $target->percentComplete . '%',
        lingotek_get_workbench_url($node, $language, TRUE),
        $phase_complete_percent . '%',
        ($phase_complete) ? '<img src="' . url(drupal_get_path('module', 'lingotek') . '/images/ico_tick_17x.png') . '" />' : '',
      );
      lingotek_trace("lingotek_pm table row", array('language' => $language));
      $rows[] = $row;
    }
    $table = theme('table', array('header' => $headers, 'rows' => $rows));

    $actions = '<input type="button" id="lingotek-update-button" value="' . t('Update') . '"/>';

    $vars = '<input type="hidden" id="lingotek_nid" value="' . $node->nid . '" />';

    $token_element = array(
      '#type' => 'hidden',
      '#attributes' => array('id' => 'submit-token'),
      '#value' => drupal_get_token(),
    );
    $vars .= drupal_render($token_element);

    $pull_translations = array();

    $pull_translations['fieldset'] = array(
      '#type' => 'fieldset',
      '#title' => t('Pull translations from Lingotek'),
      '#description' => t('Pull the latest translations from Lingotek in the selected languages.'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    $pull_translations['fieldset']['contents'] = array(
      '#markup' => $table . $actions . $vars,
    );

    if (lingotek_supported_type($node->type) && lingotek_supported_language($node->language)) {
      $output['content_push'] = array(
        '#markup' => (lingotek_lingonode($node->nid, 'create_lingotek_document')) ? '' : drupal_render(drupal_get_form('lingotek_content_push_form', $node)),
      );

      if (!empty($rows)) {
        $output['content_pull'] = array(
          '#markup' => drupal_render($pull_translations),
        );
      }
    }
    else {
      $output['content_pull'] = array(
        '#markup' => '<p class="help">' . t('This node is not compatible with Lingotek translation. Either it is not a Lingotek tranlsation-enabled content type. Or the node does not have a defined language.') . '</p>'
      );
    }

    // Add the mark as complete table if there are complete-eligible phrases.
    if ($document->hasPhasesToComplete()) {
      $output['mark_complete'] = array(
        '#markup' => drupal_render(drupal_get_form('lingotek_mark_phases_complete', $node)),
      );
    }

    $output['upgrade_form'] = array(
      '#markup' => drupal_render(drupal_get_form('lingotek_advanced_parsing_upgrade_form')),
    );
  }

  return $output;
}

/*
 * Synchonize translations with what is available on the Lingotek platform (current translations)
 */
function lingotek_update($nid) {
  $node = node_load($nid);
  foreach ($_POST['targets'] as $target) {
    lingotek_download_document($node, check_plain($target));
  }
  drupal_set_message(t('Updated local translations for the selected languages.'));
  drupal_json_output(array('status' => 0, 'updated' => $_POST['targets'], 'nid' => $nid));
}

/**
 * Form constructor for parsing upgrade of a node.
 *
 * @return array
 *   A FAPI form array.
 */
function lingotek_advanced_parsing_upgrade_form($form_state) {
  $form = array();

  if (!variable_get('lingotek_advanced_parsing', FALSE)) {
    $router_item = menu_get_item();
    if (!empty($router_item['page_arguments'][0]->nid)) {
      $node_id = $router_item['page_arguments'][0]->nid;

      $form['node_id'] = array(
        '#type' => 'hidden',
        '#value' => $node_id,
      );

      $form['advanced_parsing_upgrade'] = array(
        '#type' => 'fieldset',
        '#title' => t('Advanced Content Parsing'),
        '#description' => t('Your site is currently set to use legacy ("simple") content parsing. Use the button below to upgrade this node to advanced content parsing.'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );

      $advanced_parsing = lingotek_lingonode($node_id, 'use_advanced_parsing');
      if (empty($advanced_parsing)) {
        $form['advanced_parsing_upgrade']['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Upgrade node'),
        );
      }
      else {
        $form['advanced_parsing_upgrade']['already_upgraded'] = array(
          '#markup' => t('This node has already been upgraded to use advanced parsing.')
        );
      }
    }
    else {
      watchdog('lingotek', 'Unable to locate node ID for advanced parsing upgrade form: @path', array('@path' => $_GET['q']), WATCHDOG_ERROR);
    }
  }

  return $form;
}


/**
 * Submit handler for the lingotek_advanced_parsing_upgrade_form form.
 *
 * @param array $form
 *   A FAPI form array.
 * @param array $form_state
 *   A FAPI form state array.
 */
function lingotek_advanced_parsing_upgrade_form_submit($form, $form_state) {
  $error = FALSE;

  if (!empty($form_state['values']['node_id'])) {
    lingotek_lingonode($form_state['values']['node_id'], 'use_advanced_parsing', 1);
    $target_node = node_load($form_state['values']['node_id']);
    if ($target_node->nid) {
      if (LingotekApi::instance()->updateContentDocument($target_node)) {
        drupal_set_message(t('This node has been upgraded to use advanced content parsing.'));
      }
      else {
        $error = TRUE;
        watchdog('lingotek', 'Error updating node for advanced content parsing. Lingotek updateContentDocument call failed.', NULL, WATCHDOG_ERROR);
      }
    }
    else {
      $error = TRUE;
      watchdog('lingotek', 'Unable to load target node for content parsing upgrade: @node_id',
        array('@node_id' => $form_state['values']['node_id']), WATCHDOG_ERROR);
    }
  }
  else {
    $error = TRUE;
    watchdog('lingotek', 'No target node ID in parsing upgrade form data.', NULL, WATCHDOG_ERROR);
  }

  if ($error) {
    drupal_set_message(t('There was an error upgrading this node. It has <strong>not</strong> been updated to use advanced parsing.'), 'error');
  }
}

/**
 * Form constructor for the lingotek_mark_phases_complete form.
 *
 * @param array $form
 *   A FAPI form array.
 * @param array $form_state
 *   A FAPI form state array.
 * @param object $node
 *   The Drupal node whose complete-eligible phases should be displayed.
 *
 * @return array
 *   A FAPI form data array.
 */
function lingotek_mark_phases_complete($form, $form_state, $node) {
  $form = array();

  $document_id = lingotek_lingonode($node->nid, 'document_id_' . $node->language);
  if (class_exists('LingotekDocument') && class_exists('LingotekPhase') && $document_id) {
    $api = LingotekApi::instance();

    $document = LingotekDocument::load($document_id);
    if ($progress = $document->translationProgress()) {
      $targets = $progress->translationTargets;

      foreach ($targets as $target) {
        $language = lingotek_drupal_language($target->language);
        $current_phase = $document->currentPhase($target->id);

        $phase_complete_percent = $current_phase->percentComplete;
        if (empty($phase_complete_percent)) {
          $phase_complete_percent = 0;
        }        
        
        if ($current_phase && $current_phase->canBeMarkedComplete()) {
          $phase_link = l($current_phase->name, '#', array('attributes' => array(
            'onclick' => 'window.open(\'' . $api->getWorkbenchLink($document_id, $current_phase->id) . '\'); return false;')));

          $row = array(
            lingotek_language_native($language) . ' (' . lingotek_language_name($language) . ')',
            $phase_link,
            $phase_complete_percent . '%',
          );

          $options[$current_phase->id] = $row;
        }
      }

      $form['mark_complete'] = array(
        '#type' => 'fieldset',
        '#title' => t('Mark Workflow Phases complete'),
        '#description' => t('The following Translation Targets have Phases that can be marked as complete.'),
      );

      $form['mark_complete']['phases'] = array(
        '#type' => 'tableselect',
        '#header' => array(
          t('Target Language'),
          t('Phase'),
          t('Phase Progress'),
        ),
        '#options' => $options,
      );

      $form['mark_complete']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Mark Selected Phases as Complete'),
      );
    }
    else {
      watchdog('lingotek', 'Unable to build mark as complete form: could not get progress data from API.',
        NULL, WATCHDOG_ERROR);
    }
  }

  return $form;
}

/**
 * Submit handler for the lingotek_mark_phases_complete form.
 */
function lingotek_mark_phases_complete_submit($form, $form_state) {
  if (!empty($form_state['values']['phases'])) {
    $api = LingotekApi::instance();
    $errors = FALSE;
    foreach ($form_state['values']['phases'] as $phase_id) {
      if ($phase_id) {
        if (!$api->markPhaseComplete($phase_id)) {
          $errors = TRUE;
        }
      }
    }

    if (!$errors) {
      drupal_set_message(t('All selected phases were marked as complete.'));
    }
    else {
      drupal_set_message(t('There were errors marking one or more phases as complete.'), 'error');
    }
  }
  else {
    drupal_set_message(t('No phases were selected.'), 'error');
  }
}