4 * Lingotek Tab for Nodes
8 * Page callback for the Lingotek local task on node detail pages.
10 * Construct the table summarizing information a Product Manager would want
11 * to know about the progress of the translations.
14 * A Drupal render array of the page contents.
16 function lingotek_pm($node) {
19 drupal_add_js(drupal_get_path('module', 'lingotek') .
'/js/lingotek.pm.js');
21 if (class_exists('LingotekDocument')) {
22 $checkbox = '<input type="checkbox" onchange="lingotek.pm.toggle_checkboxes(this);" />';
26 t('Document Progress'),
32 $document = LingotekDocument
::load(lingotek_lingonode($node->nid
, 'document_id_' .
$node->language
));
34 $progress = lingotek_get_document_targets($document->document_id
, TRUE
);
37 foreach ($progress as
$language => $target) {
38 $current_phase = $document->currentPhase($target->id
);
39 $phase_complete = ($current_phase->isMarkedComplete
) ? TRUE
: FALSE
;
40 $phase_complete_percent = $current_phase->percentComplete
;
41 if (empty($phase_complete_percent)) {
42 $phase_complete_percent = 0;
45 '<input type="checkbox" tag="lingotek_pm_row" language="' .
$language .
'" />',
46 lingotek_language_native($language) .
' (' .
lingotek_language_name($language) .
')',
47 $target->percentComplete .
'%',
48 lingotek_get_workbench_url($node, $language, TRUE
),
49 $phase_complete_percent .
'%',
50 ($phase_complete) ?
'<img src="' .
url(drupal_get_path('module', 'lingotek') .
'/images/ico_tick_17x.png') .
'" />' : '',
52 lingotek_trace("lingotek_pm table row", array('language' => $language));
55 $table = theme('table', array('header' => $headers, 'rows' => $rows));
57 $actions = '<input type="button" id="lingotek-update-button" value="' .
t('Update') .
'"/>';
59 $vars = '<input type="hidden" id="lingotek_nid" value="' .
$node->nid .
'" />';
61 $token_element = array(
63 '#attributes' => array('id' => 'submit-token'),
64 '#value' => drupal_get_token(),
66 $vars .
= drupal_render($token_element);
68 $pull_translations = array();
70 $pull_translations['fieldset'] = array(
71 '#type' => 'fieldset',
72 '#title' => t('Pull translations from Lingotek'),
73 '#description' => t('Pull the latest translations from Lingotek in the selected languages.'),
74 '#collapsible' => TRUE
,
75 '#collapsed' => FALSE
,
78 $pull_translations['fieldset']['contents'] = array(
79 '#markup' => $table .
$actions .
$vars,
82 if (lingotek_supported_type($node->type
) && lingotek_supported_language($node->language
)) {
83 $output['content_push'] = array(
84 '#markup' => (lingotek_lingonode($node->nid
, 'create_lingotek_document')) ?
'' : drupal_render(drupal_get_form('lingotek_content_push_form', $node)),
88 $output['content_pull'] = array(
89 '#markup' => drupal_render($pull_translations),
94 $output['content_pull'] = array(
95 '#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>'
99 // Add the mark as complete table if there are complete-eligible phrases.
100 if ($document->hasPhasesToComplete()) {
101 $output['mark_complete'] = array(
102 '#markup' => drupal_render(drupal_get_form('lingotek_mark_phases_complete', $node)),
106 $output['upgrade_form'] = array(
107 '#markup' => drupal_render(drupal_get_form('lingotek_advanced_parsing_upgrade_form')),
115 * Synchonize translations with what is available on the Lingotek platform (current translations)
117 function lingotek_update($nid) {
118 $node = node_load($nid);
119 foreach ($_POST['targets'] as
$target) {
120 lingotek_download_document($node, check_plain($target));
122 drupal_set_message(t('Updated local translations for the selected languages.'));
123 drupal_json_output(array('status' => 0, 'updated' => $_POST['targets'], 'nid' => $nid));
127 * Form constructor for parsing upgrade of a node.
132 function lingotek_advanced_parsing_upgrade_form($form_state) {
135 if (!variable_get('lingotek_advanced_parsing', FALSE
)) {
136 $router_item = menu_get_item();
137 if (!empty($router_item['page_arguments'][0]->nid
)) {
138 $node_id = $router_item['page_arguments'][0]->nid
;
140 $form['node_id'] = array(
142 '#value' => $node_id,
145 $form['advanced_parsing_upgrade'] = array(
146 '#type' => 'fieldset',
147 '#title' => t('Advanced Content Parsing'),
148 '#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.'),
149 '#collapsible' => TRUE
,
150 '#collapsed' => TRUE
,
153 $advanced_parsing = lingotek_lingonode($node_id, 'use_advanced_parsing');
154 if (empty($advanced_parsing)) {
155 $form['advanced_parsing_upgrade']['submit'] = array(
157 '#value' => t('Upgrade node'),
161 $form['advanced_parsing_upgrade']['already_upgraded'] = array(
162 '#markup' => t('This node has already been upgraded to use advanced parsing.')
167 watchdog('lingotek', 'Unable to locate node ID for advanced parsing upgrade form: @path', array('@path' => $_GET['q']), WATCHDOG_ERROR
);
176 * Submit handler for the lingotek_advanced_parsing_upgrade_form form.
180 * @param array $form_state
181 * A FAPI form state array.
183 function lingotek_advanced_parsing_upgrade_form_submit($form, $form_state) {
186 if (!empty($form_state['values']['node_id'])) {
187 lingotek_lingonode($form_state['values']['node_id'], 'use_advanced_parsing', 1);
188 $target_node = node_load($form_state['values']['node_id']);
189 if ($target_node->nid
) {
190 if (LingotekApi
::instance()->updateContentDocument($target_node)) {
191 drupal_set_message(t('This node has been upgraded to use advanced content parsing.'));
195 watchdog('lingotek', 'Error updating node for advanced content parsing. Lingotek updateContentDocument call failed.', NULL
, WATCHDOG_ERROR
);
200 watchdog('lingotek', 'Unable to load target node for content parsing upgrade: @node_id',
201 array('@node_id' => $form_state['values']['node_id']), WATCHDOG_ERROR
);
206 watchdog('lingotek', 'No target node ID in parsing upgrade form data.', NULL
, WATCHDOG_ERROR
);
210 drupal_set_message(t('There was an error upgrading this node. It has <strong>not</strong> been updated to use advanced parsing.'), 'error');
215 * Form constructor for the lingotek_mark_phases_complete form.
219 * @param array $form_state
220 * A FAPI form state array.
221 * @param object $node
222 * The Drupal node whose complete-eligible phases should be displayed.
225 * A FAPI form data array.
227 function lingotek_mark_phases_complete($form, $form_state, $node) {
230 $document_id = lingotek_lingonode($node->nid
, 'document_id_' .
$node->language
);
231 if (class_exists('LingotekDocument') && class_exists('LingotekPhase') && $document_id) {
232 $api = LingotekApi
::instance();
234 $document = LingotekDocument
::load($document_id);
235 if ($progress = $document->translationProgress()) {
236 $targets = $progress->translationTargets
;
238 foreach ($targets as
$target) {
239 $language = lingotek_drupal_language($target->language
);
240 $current_phase = $document->currentPhase($target->id
);
242 $phase_complete_percent = $current_phase->percentComplete
;
243 if (empty($phase_complete_percent)) {
244 $phase_complete_percent = 0;
247 if ($current_phase && $current_phase->canBeMarkedComplete()) {
248 $phase_link = l($current_phase->name
, '#', array('attributes' => array(
249 'onclick' => 'window.open(\'' .
$api->getWorkbenchLink($document_id, $current_phase->id
) .
'\'); return false;')));
252 lingotek_language_native($language) .
' (' .
lingotek_language_name($language) .
')',
254 $phase_complete_percent .
'%',
257 $options[$current_phase->id
] = $row;
261 $form['mark_complete'] = array(
262 '#type' => 'fieldset',
263 '#title' => t('Mark Workflow Phases complete'),
264 '#description' => t('The following Translation Targets have Phases that can be marked as complete.'),
267 $form['mark_complete']['phases'] = array(
268 '#type' => 'tableselect',
270 t('Target Language'),
274 '#options' => $options,
277 $form['mark_complete']['submit'] = array(
279 '#value' => t('Mark Selected Phases as Complete'),
283 watchdog('lingotek', 'Unable to build mark as complete form: could not get progress data from API.',
284 NULL
, WATCHDOG_ERROR
);
292 * Submit handler for the lingotek_mark_phases_complete form.
294 function lingotek_mark_phases_complete_submit($form, $form_state) {
295 if (!empty($form_state['values']['phases'])) {
296 $api = LingotekApi
::instance();
298 foreach ($form_state['values']['phases'] as
$phase_id) {
300 if (!$api->markPhaseComplete($phase_id)) {
307 drupal_set_message(t('All selected phases were marked as complete.'));
310 drupal_set_message(t('There were errors marking one or more phases as complete.'), 'error');
314 drupal_set_message(t('No phases were selected.'), 'error');