summaryrefslogtreecommitdiffstats
path: root/lib/Drupal/lingotek/LingotekDocument.php
blob: 98804cf868ec23578b6e3df74352a7f318a200c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

/**
 * @file
 * Defines LingotekDocument.
 */
 
/**
 * A class representing a Lingotek Document
 */
class LingotekDocument {
  /**
   * A Lingotek Document ID.
   *
   * @var int
   */
  public $document_id;
  
  /**
   * A reference to the Lingotek API.
   *
   * @var LingotekApi
   */
  protected $api = NULL;
  
  /**
   * Static store for Documents already loaded in this request.
   */
  public static $documents = array();
  
  /**
   * Constructor.
   *
   * @param $document_id
   *   A Lingotek Document ID.
   */
  public function __construct($document_id) {
    $this->document_id = intval($document_id);
  }
  
  /**
   * Gets the translation targets associated with this document.
   *
   * @return array
   *   An array of Translation Target, as returned by a getDocument
   *   Lingotek API call
   */
  public function translationTargets() {
    $targets = array();
    
    if ($document = LingotekApi::instance()->getDocument($this->document_id)) {
      if (!empty($document->translationTargets)) {
        foreach ($document->translationTargets as $target) {
          $targets[lingotek_drupal_language($target->language)] = $target;
        }        
      }
    }
    
    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.
   *
   * @return bool
   *   TRUE if complete-eligible phases are present. FALSE otherwise.
   */
  public function hasPhasesToComplete() {
    $result = FALSE;
    
    if (class_exists('LingotekPhase')) {
      $progress = $this->translationProgress();
      foreach ($progress->translationTargets as $target) {
        $current_phase = $this->currentPhase($target->id);
        if ($current_phase->canBeMarkedComplete()) {
          $result = TRUE;
          break;
        }
      }
    }
    
    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.
   *
   * @param LingotekApi $api
   *   An instantiated Lingotek API object.
   */
  public function setApi(LingotekApi $api) {
    $this->api = $api;
  }
  
  /**
   * Factory method for getting a loaded LingotekDocument object.
   *
   * @param int $document_id
   *   A Lingotek Document ID.
   *
   * @return LingotekDocument
   *   A loaded LingotekDocument object.
   */
  public static function load($document_id) {
    $document_id = intval($document_id);
    if (empty($documents[$document_id])) {
      $document = new LingotekDocument($document_id);
      $document->setApi(LingotekApi::instance());
      $documents[$document_id] = $document;
    }
    
    return $documents[$document_id];
  }
}