summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Durbin2012-12-03 19:40:11 (GMT)
committer Ben Durbin2012-12-03 19:40:11 (GMT)
commite577c710084c69c1bcac85ae855bb1bffeed4e2e (patch)
treef11279b5350a71cca3e7a20be8f10435a8db4e22
parent8cca0d7370688173715bff0fb39091f947e08578 (diff)
Refactor currentPhase code to account for changes in the API (addition of "type" parameter on Phase data when retrieved via getDocumentProgress).
-rw-r--r--lib/Drupal/lingotek/LingotekDocument.php56
-rw-r--r--lib/Drupal/lingotek/LingotekPhase.php24
-rw-r--r--lingotek.page.inc94
3 files changed, 126 insertions, 48 deletions
diff --git a/lib/Drupal/lingotek/LingotekDocument.php b/lib/Drupal/lingotek/LingotekDocument.php
index a97ac63..98804cf 100644
--- a/lib/Drupal/lingotek/LingotekDocument.php
+++ b/lib/Drupal/lingotek/LingotekDocument.php
@@ -60,6 +60,41 @@ class LingotekDocument {
}
/**
+ * 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.
*
* @return bool
@@ -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;
@@ -82,6 +118,22 @@ class LingotekDocument {
}
/**
+ * 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.
*
* @param LingotekApi $api
diff --git a/lib/Drupal/lingotek/LingotekPhase.php b/lib/Drupal/lingotek/LingotekPhase.php
index b181984..73278e0 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 d594f35..4d64e5b 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(
'<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_percent_complete . '%',
+ $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));
@@ -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;