diff --git a/lib/Drupal/lingotek/LingotekDocument.php b/lib/Drupal/lingotek/LingotekDocument.php index a97ac63f0a83e6f430ae408bef1f46afa4515372..98804cf868ec23578b6e3df74352a7f318a200c2 100644 --- a/lib/Drupal/lingotek/LingotekDocument.php +++ b/lib/Drupal/lingotek/LingotekDocument.php @@ -59,6 +59,41 @@ class LingotekDocument { return $targets; } + /** + * Gets the current workflow phase for the document. + * + * @param int $translation_target_id + * The ID of the translation target whose current phase should be returned. + * + * @return mixed + * A LingotekPhase object if the current phase could be found, or FALSE on failure. + */ + public function currentPhase($translation_target_id) { + $phase = FALSE; + + if ($progress = $this->translationProgress()) { + foreach ($progress->translationTargets as $target) { + if ($target->id == $translation_target_id && !empty($target->phases)) { + $current_phase = FALSE; + foreach ($target->phases as $phase) { + + if (!$phase->isMarkedComplete) { + $current_phase = $phase; + break; + } + } + + // Return either the first uncompleted phase, or the last phase if all phases are complete. + $current_phase = ($current_phase) ? $current_phase : end($target->phases); + $phase = LingotekPhase::loadWithData($current_phase); + break; + } + } + } + + return $phase; + } + /** * Determines whether or not the document has Translation Targets in a complete-eligible phase. * @@ -69,8 +104,9 @@ class LingotekDocument { $result = FALSE; if (class_exists('LingotekPhase')) { - foreach ($this->translationTargets() as $target) { - $current_phase = LingotekPhase::loadWithData($this->api->currentPhase($target->id)); + $progress = $this->translationProgress(); + foreach ($progress->translationTargets as $target) { + $current_phase = $this->currentPhase($target->id); if ($current_phase->canBeMarkedComplete()) { $result = TRUE; break; @@ -81,6 +117,22 @@ class LingotekDocument { return $result; } + /** + * Gets the translation progress data for the Document. + * + * @return mixed + * The data object returned by a call to getDocumentProgress on success, FALSE on failure. + */ + public function translationProgress() { + $progress = &drupal_static(__FUNCTION__ . '-' . $this->document_id); + + if (!$progress) { + $progress = $this->api->getDocumentProgress($this->document_id); + } + + return $progress; + } + /** * Injects reference to an API object. * diff --git a/lib/Drupal/lingotek/LingotekPhase.php b/lib/Drupal/lingotek/LingotekPhase.php index b1819845cf9c98348f8272b548aff2cc3b24997f..73278e08355add9613ee0084a64dcf5792486779 100644 --- a/lib/Drupal/lingotek/LingotekPhase.php +++ b/lib/Drupal/lingotek/LingotekPhase.php @@ -84,16 +84,11 @@ class LingotekPhase { // These phase types need to be at 100% complete in order to // be eligible for mark as complete. $needs_100_complete_phase_types = array( - 'Translate', - 'Review', + 'TRANSLATION', + 'REVIEW', ); - // TODO: The getPhase Lingotek API call doesn't currently return the machine-readable - // type for a given phase, just the (user-editable) label. Currently, - // the logic below will break if the user has customized the label - // from the defaults of "Translate" or "Review". - // Follow up and fix this once the API has been enhanced. - if (in_array($this->phase->name, $needs_100_complete_phase_types)) { + if (in_array($this->phase->type, $needs_100_complete_phase_types)) { if ($this->phase->percentComplete == 100 && !$this->phase->isMarkedComplete) { $result = TRUE; } @@ -106,4 +101,17 @@ class LingotekPhase { return $result; } + + /** + * Magic get for phase property access. + */ + public function __get($property) { + $value = NULL; + + if (!empty($this->phase->$property)) { + $value = $this->phase->$property; + } + + return $value; + } } diff --git a/lingotek.page.inc b/lingotek.page.inc index d594f35778245a2369a89a9a5d5f3b37349132fd..4d64e5b74e72bef6148ea8c3c9a7fc63202330be 100644 --- a/lingotek.page.inc +++ b/lingotek.page.inc @@ -35,15 +35,18 @@ function lingotek_pm($node) { $rows = array(); foreach ($progress as $language => $target) { - $current_phase = LingotekApi::instance()->currentPhase($target->id); - $phase_complete = (!empty($current_phase->isMarkedComplete)) ? TRUE : FALSE; - $phase_percent_complete = (!empty($current_phase->percentComplete)) ? $current_phase->percentComplete : 0; + $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( '', lingotek_language_native($language) . ' (' . lingotek_language_name($language) . ')', $target->percentComplete . '%', lingotek_get_workbench_url($node, $language, TRUE), - $phase_percent_complete . '%', + $phase_complete_percent . '%', ($phase_complete) ? '' : '', ); lingotek_trace("lingotek_pm table row", array('language' => $language)); @@ -226,45 +229,60 @@ function lingotek_mark_phases_complete($form, $form_state, $node) { $document_id = lingotek_lingonode($node->nid, 'document_id_' . $node->language); if (class_exists('LingotekDocument') && class_exists('LingotekPhase') && $document_id) { + $api = LingotekApi::instance(); - $targets = LingotekDocument::load($document_id)->translationTargets(); - - foreach ($targets as $language => $target) { - $current_phase = LingotekApi::instance()->currentPhase($target->id); - $phase = LingotekPhase::loadWithData($current_phase); - if ($phase->canBeMarkedComplete()) { - $phase_complete = (!empty($current_phase->isMarkedComplete)) ? TRUE : FALSE; - $phase_percent_complete = (!empty($current_phase->percentComplete)) ? $current_phase->percentComplete : 0; - $row = array( - lingotek_language_native($language) . ' (' . lingotek_language_name($language) . ')', - lingotek_get_workbench_url($node, $language, TRUE), - $phase_percent_complete . '%', - ); - - $options[$current_phase->id] = $row; + $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'] = 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']['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'), - ); + $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;