5 * Defines LingotekDocument.
9 * A class representing a Lingotek Document
11 class LingotekDocument
{
13 * A Lingotek Document ID.
20 * A reference to the Lingotek API.
24 protected
$api = NULL
;
27 * Static store for Documents already loaded in this request.
29 public static
$documents = array();
35 * A Lingotek Document ID.
37 public
function __construct($document_id) {
38 $this->document_id
= intval($document_id);
42 * Gets the translation targets associated with this document.
45 * An array of Translation Target, as returned by a getDocument
48 public
function translationTargets() {
51 if ($document = LingotekApi
::instance()->getDocument($this->document_id
)) {
52 if (!empty($document->translationTargets
)) {
53 foreach ($document->translationTargets as
$target) {
54 $targets[lingotek_drupal_language($target->language
)] = $target;
63 * Gets the current workflow phase for the document.
65 * @param int $translation_target_id
66 * The ID of the translation target whose current phase should be returned.
69 * A LingotekPhase object if the current phase could be found, or FALSE on failure.
71 public
function currentPhase($translation_target_id) {
74 if ($progress = $this->translationProgress()) {
75 foreach ($progress->translationTargets as
$target) {
76 if ($target->id
== $translation_target_id && !empty($target->phases
)) {
77 $current_phase = FALSE
;
78 foreach ($target->phases as
$phase) {
80 if (!$phase->isMarkedComplete
) {
81 $current_phase = $phase;
86 // Return either the first uncompleted phase, or the last phase if all phases are complete.
87 $current_phase = ($current_phase) ?
$current_phase : end($target->phases
);
88 $phase = LingotekPhase
::loadWithData($current_phase);
98 * Determines whether or not the document has Translation Targets in a complete-eligible phase.
101 * TRUE if complete-eligible phases are present. FALSE otherwise.
103 public
function hasPhasesToComplete() {
106 if (class_exists('LingotekPhase')) {
107 $progress = $this->translationProgress();
108 foreach ($progress->translationTargets as
$target) {
109 $current_phase = $this->currentPhase($target->id
);
110 if ($current_phase->canBeMarkedComplete()) {
121 * Gets the translation progress data for the Document.
124 * The data object returned by a call to getDocumentProgress on success, FALSE on failure.
126 public
function translationProgress() {
127 $progress = &drupal_static(__FUNCTION__ .
'-' .
$this->document_id
);
130 $progress = $this->api
->getDocumentProgress($this->document_id
);
137 * Injects reference to an API object.
139 * @param LingotekApi $api
140 * An instantiated Lingotek API object.
142 public
function setApi(LingotekApi
$api) {
147 * Factory method for getting a loaded LingotekDocument object.
149 * @param int $document_id
150 * A Lingotek Document ID.
152 * @return LingotekDocument
153 * A loaded LingotekDocument object.
155 public static
function load($document_id) {
156 $document_id = intval($document_id);
157 if (empty($documents[$document_id])) {
158 $document = new
LingotekDocument($document_id);
159 $document->setApi(LingotekApi
::instance());
160 $documents[$document_id] = $document;
163 return $documents[$document_id];