You are here

class LingotekDocument in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 7.7 lib/Drupal/lingotek/LingotekDocument.php \LingotekDocument
  2. 7.3 lib/Drupal/lingotek/LingotekDocument.php \LingotekDocument
  3. 7.4 lib/Drupal/lingotek/LingotekDocument.php \LingotekDocument
  4. 7.5 lib/Drupal/lingotek/LingotekDocument.php \LingotekDocument
  5. 7.6 lib/Drupal/lingotek/LingotekDocument.php \LingotekDocument

A class representing a Lingotek Document

Hierarchy

Expanded class hierarchy of LingotekDocument

2 string references to 'LingotekDocument'
lingotek_mark_phases_complete in ./lingotek.page.inc
Form constructor for the lingotek_mark_phases_complete form.
lingotek_pm in ./lingotek.page.inc
Page callback for the Lingotek local task on node detail pages.

File

lib/Drupal/lingotek/LingotekDocument.php, line 11
Defines LingotekDocument.

View source
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::convertLingotek2Drupal($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();
      if (is_object($progress)) {
        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];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekDocument::$api protected property A reference to the Lingotek API.
LingotekDocument::$documents public static property Static store for Documents already loaded in this request.
LingotekDocument::$document_id public property A Lingotek Document ID.
LingotekDocument::currentPhase public function Gets the current workflow phase for the document.
LingotekDocument::hasPhasesToComplete public function Determines whether or not the document has Translation Targets in a complete-eligible phase.
LingotekDocument::load public static function Factory method for getting a loaded LingotekDocument object.
LingotekDocument::setApi public function Injects reference to an API object.
LingotekDocument::translationProgress public function Gets the translation progress data for the Document.
LingotekDocument::translationTargets public function Gets the translation targets associated with this document.
LingotekDocument::__construct public function Constructor.