/** * Factory method for getting a loaded LingotekPhase object. * * @param object $api_phase * Phase data as returned by a getPhase Lingotek API call. * * @return LingotekPhase * A loaded LingotekPhase object. */ public static function loadWithData($api_phase) { $api = LingotekApi::instance(); $phase = new LingotekPhase($api_phase); $phase->setApi($api); return $phase; }
/** * Gets the singleton instance of the API class. * * @return LingotekApi * An instantiated LingotekApi object. */ public static function instance() { if (!isset(self::$instance)) { $class_name = __CLASS__; self::$instance = new $class_name(); } return self::$instance; }
/** * Updates the local content of $target_code with data from a Lingotek Document * * @param string $lingotek_locale * The code for the language that needs to be updated. * @return bool * TRUE if the content updates succeeded, FALSE otherwise. */ public function downloadTriggered($lingotek_locale) { $metadata = $this->metadata(); $document_id = $metadata['document_id']; if (empty($document_id)) { LingotekLog::error('Unable to refresh local contents for config chunk @cid. Could not find Lingotek Document ID.', array('@cid' => $this->cid)); return FALSE; } $api = LingotekApi::instance(); $document_xml = $api->downloadDocument($document_id, $lingotek_locale); $target_language = Lingotek::convertLingotek2Drupal($lingotek_locale); /* FAST VERSION (git history for slow version) */ // 1. save the dirty targets associated with given language $dirty_lids = self::getDirtyLidsByChunkIdAndLanguage($this->cid, $target_language); // 2. delete all segment targets associated with given language self::deleteSegmentTranslationsByChunkIdAndLanguage($this->cid, $target_language); // 3. insert all segments for the given language self::saveSegmentTranslations($document_xml, $target_language); // 4. return the dirty targets' statuses self::restoreDirtyLids($dirty_lids); /* END FAST */ // set chunk status to current $this->setStatus(LingotekSync::STATUS_CURRENT); $this->setTargetsStatus(LingotekSync::STATUS_CURRENT, $lingotek_locale); return TRUE; }
/** * Factory method for getting a loaded LingotekNode object. * * @param object $node * A Drupal node. * * @return LingotekNode * A loaded LingotekNode object. */ public static function load($node) { $node = new LingotekNode($node); $node->setApi(LingotekApi::instance()); return $node; }
public static function getDownloadableReport() { $project_id = variable_get('lingotek_project', NULL); $document_ids = LingotekSync::getDocIdsByStatus(LingotekSync::STATUS_PENDING); $report = array('download_targets_workflow_complete' => array(), 'download_targets_workflow_complete_count' => 0, 'download_targets_workflow_incomplete' => array(), 'download_targets_workflow_incomplete_count' => 0); if (empty($document_ids)) { return $report; } // if no documents are PENDING, then no need to make the API call. $api = LingotekApi::instance(); $response = $api->getProgressReport($project_id, $document_ids, TRUE); if (isset($response->byDocumentIdAndTargetLocale)) { $progress_report = $response->byDocumentIdAndTargetLocale; foreach ($progress_report as $doc_id => $target_locales) { foreach ($target_locales as $lingotek_locale => $pct_complete) { $doc_target = array('document_id' => $doc_id, 'locale' => $lingotek_locale); $node_id = self::getNodeIdFromDocId($doc_id); if ($pct_complete == 100) { if (self::getTargetStatus($node_id, $lingotek_locale) == self::STATUS_PENDING) { $report['download_targets_workflow_complete'][] = $doc_target; $report['download_targets_workflow_complete_count']++; } else { // Target already downloaded } } else { $report['download_targets_workflow_incomplete'][] = $doc_target; $report['download_targets_workflow_incomplete_count']++; } } } } return $report; }
/** * Factory method for getting a loaded LingotekEntity object * * @param object $entity * A Drupal entity. * * @return LingotekEntity * A loaded LingotekEntity object. */ public static function load($entity, $entity_type) { $entity = new LingotekEntity($entity, $entity_type); $entity->setApi(LingotekApi::instance()); return $entity; }
public static function updateNotifyUrl() { $security_token = md5(time()); $new_url = lingotek_notify_url_generate($security_token); $api = LingotekApi::instance(); $integration_method_id = variable_get('lingotek_integration_method', ''); if (!strlen($integration_method_id)) { // request integration id when not already set, attempt to detect $params = array('regex' => ".*"); $response = $api->request('searchOutboundIntegrationUrls', $params); if (isset($response->results) && $response->results) { global $base_url; $integration_methods = $response->integrationMethods; foreach ($integration_methods as $integration_method) { if (strpos($integration_method->url, $base_url) !== FALSE) { $integration_method_id = $integration_method->id; // prefer integration with matching base_url } } if (!strlen($integration_method_id)) { reset($integration_methods); // just in case the internal pointer is not pointing to the first element $integration_method = current($integration_methods); // grab the first element in the list $integration_method_id = $integration_method->id; // use the first url found (if no matching url was found previously) } variable_set('lingotek_integration_method', $integration_method_id); } } $parameters = array('id' => $integration_method_id, 'url' => $new_url); $response = $api->request('updateOutboundIntegrationUrl', $parameters); $success = isset($response->results) ? $response->results : FALSE; if ($success) { variable_set('lingotek_notify_url', $new_url); variable_set('lingotek_notify_security_token', $security_token); } return $success; }
/** * Updates the local content with data from a Lingotek Document. * * @return bool * TRUE if the content updates succeeded, FALSE otherwise. */ public function updateLocalContent() { $success = TRUE; $metadata = $this->metadata(); if (!empty($metadata['document_id'])) { $document_id = $metadata['document_id']; $api = LingotekApi::instance(); $document = $api->getDocument($document_id); foreach ($document->translationTargets as $target) { $document_xml = $api->downloadDocument($metadata['document_id'], $target->language); $target_language = Lingotek::convertLingotek2Drupal($target->language); foreach ($document_xml as $drupal_field_name => $content) { // Figure out which subkey of the field data we're targeting. // "value" for standard text fields, or some other key for // compound text fields (text with summary, for example). $target_key = 'value'; $subfield_parts = explode('__', $drupal_field_name); if (count($subfield_parts) == 2) { $drupal_field_name = $subfield_parts[0]; $target_key = $subfield_parts[1]; } $field = field_info_field($drupal_field_name); if (!empty($field['lingotek_translatable'])) { $comment_field =& $this->comment->{$drupal_field_name}; $index = 0; foreach ($content as $text) { $comment_field[$target_language][$index][$target_key] = decode_entities(lingotek_xml_decode($text)); // Copy filter format from source language field. if (!empty($comment_field[$this->comment->language][0]['format'])) { $comment_field[$target_language][$index]['format'] = $comment_field[$this->comment->language][0]['format']; } $index++; } } } $comment_node = LingotekNode::loadById($this->comment->nid); $comment_fields = array_keys(field_info_instances('comment', 'comment_node_' . $comment_node->type)); foreach ($comment_fields as $field) { // Copy any untranslated fields from the default language into this target. if (isset($this->comment->{$field}[$this->comment->language]) && !isset($this->comment->{$field}[$target_language])) { $this->comment->{$field}[$target_language] = $this->comment->{$field}[$this->comment->language]; } // Ensure that all fields get their LANGUAGE_NONE field data populated with the // comment's default language data, to support toggling off of comment translation // at some point in the future. if (!empty($this->comment->{$field}[$this->comment->language])) { $this->comment->{$field}[LANGUAGE_NONE] = $this->comment->{$field}[$this->comment->language]; } } } // This avoids an infitinite loop when hooks resulting from comment_save() are invoked. self::$content_update_in_progress = TRUE; comment_save($this->comment); self::$content_update_in_progress = FALSE; $this->comment = comment_load($this->comment->cid); } else { LingotekLog::error('Unable to refresh local contents for comment @cid. Could not find Lingotek Document ID.', array('@cid' => $this->comment->cid)); $success = FALSE; } return $success; }
/** * 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 = $document_id; if (empty($documents[$document_id])) { $document = new LingotekDocument($document_id); $document->setApi(LingotekApi::instance()); $documents[$document_id] = $document; } return $documents[$document_id]; }