Refactor currentPhase code to account for changes in the API (addition of "type"...
[project/lingotek.git] / lib / Drupal / lingotek / LingotekDocument.php
1 <?php
2
3 /**
4 * @file
5 * Defines LingotekDocument.
6 */
7
8 /**
9 * A class representing a Lingotek Document
10 */
11 class LingotekDocument {
12 /**
13 * A Lingotek Document ID.
14 *
15 * @var int
16 */
17 public $document_id;
18
19 /**
20 * A reference to the Lingotek API.
21 *
22 * @var LingotekApi
23 */
24 protected $api = NULL;
25
26 /**
27 * Static store for Documents already loaded in this request.
28 */
29 public static $documents = array();
30
31 /**
32 * Constructor.
33 *
34 * @param $document_id
35 * A Lingotek Document ID.
36 */
37 public function __construct($document_id) {
38 $this->document_id = intval($document_id);
39 }
40
41 /**
42 * Gets the translation targets associated with this document.
43 *
44 * @return array
45 * An array of Translation Target, as returned by a getDocument
46 * Lingotek API call
47 */
48 public function translationTargets() {
49 $targets = array();
50
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;
55 }
56 }
57 }
58
59 return $targets;
60 }
61
62 /**
63 * Gets the current workflow phase for the document.
64 *
65 * @param int $translation_target_id
66 * The ID of the translation target whose current phase should be returned.
67 *
68 * @return mixed
69 * A LingotekPhase object if the current phase could be found, or FALSE on failure.
70 */
71 public function currentPhase($translation_target_id) {
72 $phase = FALSE;
73
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) {
79
80 if (!$phase->isMarkedComplete) {
81 $current_phase = $phase;
82 break;
83 }
84 }
85
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);
89 break;
90 }
91 }
92 }
93
94 return $phase;
95 }
96
97 /**
98 * Determines whether or not the document has Translation Targets in a complete-eligible phase.
99 *
100 * @return bool
101 * TRUE if complete-eligible phases are present. FALSE otherwise.
102 */
103 public function hasPhasesToComplete() {
104 $result = FALSE;
105
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()) {
111 $result = TRUE;
112 break;
113 }
114 }
115 }
116
117 return $result;
118 }
119
120 /**
121 * Gets the translation progress data for the Document.
122 *
123 * @return mixed
124 * The data object returned by a call to getDocumentProgress on success, FALSE on failure.
125 */
126 public function translationProgress() {
127 $progress = &drupal_static(__FUNCTION__ . '-' . $this->document_id);
128
129 if (!$progress) {
130 $progress = $this->api->getDocumentProgress($this->document_id);
131 }
132
133 return $progress;
134 }
135
136 /**
137 * Injects reference to an API object.
138 *
139 * @param LingotekApi $api
140 * An instantiated Lingotek API object.
141 */
142 public function setApi(LingotekApi $api) {
143 $this->api = $api;
144 }
145
146 /**
147 * Factory method for getting a loaded LingotekDocument object.
148 *
149 * @param int $document_id
150 * A Lingotek Document ID.
151 *
152 * @return LingotekDocument
153 * A loaded LingotekDocument object.
154 */
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;
161 }
162
163 return $documents[$document_id];
164 }
165 }