You are here

globallink_webform.inc in GlobalLink Connect for Drupal 7.7

File

globallink_webform/globallink_webform.inc
View source
<?php

/**
 * Sends webforms for translation.
 *
 * @param array $lids
 *   The array of webform LIDs.
 * @param string $pd4
 *   The project director details.
 * @param string $submission_name
 *   The name of the submission.
 * @param string $due_date
 *   When the translation is due.
 * @param string $project_code
 *   The project's registered code.
 * @param string $source_locale
 *   The locale of the content being translated.
 * @param array $target_locale_arr
 *   Array of desired locales to translate into.
 * @param array $submission_details
 *   Associative array of details about the submission.
 *
 * @return object
 *   GlobalLink object that represents active translation.
 */
function globallink_webform_send_for_translations($lids, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority, $parents) {
  $globallink_arr = array();
  $submitter = $submission_details['submitter'];

  // For each target locale code, we check if the lid is in Sent or Error
  // We group lids per target locale code
  $final_tgt_arr = array();
  foreach ($target_locale_arr as $target_locale) {
    foreach ($parents as $pid => $p) {
      foreach ($p as $lid) {
        $active_row = globallink_webform_get_sent_tpt_rows_by_lid_and_target($pid, $lid, $target_locale);
        if ($active_row != NULL) {
          continue;
        }
        if (isset($final_tgt_arr[$target_locale][$pid])) {
          array_push($final_tgt_arr[$target_locale][$pid], $lid);
        }
        else {
          $final_tgt_arr[$target_locale][$pid] = array();
          array_push($final_tgt_arr[$target_locale][$pid], $lid);
        }
      }
    }
  }
  foreach ($final_tgt_arr as $target_locale_code => $final_parent_arr) {
    $xmls = globallink_webform_get_xml($final_parent_arr);
    foreach ($xmls as $parent => $xml) {
      $tgt_arr = array();
      $tgt_arr[] = $target_locale_code;
      $title = array();
      $name = 'Webform_' . $parent . '.xml';
      foreach ($final_parent_arr[$parent] as $key => $value) {
        $t1 = db_select('locales_source', 'ls')
          ->fields('ls')
          ->condition('lid', $value, '=')
          ->execute()
          ->fetchAll();
        $title[$value] = $t1[0]->source;
      }
      $globallink = new GlobalLink();
      $globallink->type = GLOBALLINK_ENTITY_TYPE_WEBFORM;
      $globallink->metadata = GLOBALLINK_ENTITY_TYPE_WEBFORM;
      $globallink->title = $title;
      $globallink->sourceLocale = $source_locale;
      $globallink->targetLocale = $tgt_arr;
      $globallink->sourceXML = $xml;
      $globallink->sourceFileName = $name;
      $globallink->submissionName = $submission_name;
      $globallink->submissionPriority = $submission_priority;
      $globallink->dueDate = $due_date;
      $globallink->otherObjectId = $parent;
      $globallink->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
      $globallink->lids = $final_parent_arr[$parent];
      $globallink_arr[GLOBALLINK_ENTITY_TYPE_WEBFORM][] = $globallink;
    }
  }
  return $globallink_arr;
}

/**
 * Gets XML data from specific webform.
 *
 * @param string $lid
 *   The webform LID.
 *
 * @return array
 *   Associative array of block XML data.
 */
function globallink_webform_get_xml($parents) {
  $xmls = array();
  foreach ($parents as $parent => $lids) {
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->formatOutput = TRUE;
    $root = $dom
      ->createElement('content');
    $dom
      ->appendChild($root);

    # code...
    $property_arr = array();
    if (!is_array($lids)) {
      $string_result = db_select('i18n_string', 's')
        ->fields('s')
        ->condition('lid', $lids, '=')
        ->execute();
      foreach ($string_result as $row) {
        $property_arr['property'] = $row->property;
      }
      $id = $dom
        ->createAttribute('pid');
      $id->value = $parent;
      $root
        ->appendChild($id);
      $result = db_select('locales_source', 'ls')
        ->fields('ls')
        ->condition('lid', $lids, '=')
        ->execute();
      foreach ($result as $row) {
        globallink_insert_child_element($dom, $root, GLOBALLINK_ENTITY_TYPE_WEBFORM, $row->source, array(
          'name' => $property_arr['property'],
          'lid' => $lids,
          'location' => $row->location,
          'format' => isset($lids['format']) ? $lids['format'] : '',
        ));
      }
    }
    else {
      foreach ($lids as $lid) {
        $string_result = db_select('i18n_string', 's')
          ->fields('s')
          ->condition('lid', $lid, '=')
          ->execute();
        foreach ($string_result as $row) {
          $property_arr['property'] = $row->property;
        }
        $id = $dom
          ->createAttribute('pid');
        $id->value = $parent;
        $root
          ->appendChild($id);
        $result = db_select('locales_source', 'ls')
          ->fields('ls')
          ->condition('lid', $lid, '=')
          ->execute();
        foreach ($result as $row) {
          globallink_insert_child_element($dom, $root, GLOBALLINK_ENTITY_TYPE_WEBFORM, $row->source, array(
            'name' => $property_arr['property'],
            'lid' => $lid,
            'location' => $row->location,
            'format' => isset($lid['format']) ? $lid['format'] : '',
          ));
        }
      }
    }
    $xml = $dom
      ->saveXML();
    $xmls[$parent] = $xml;
  }
  return $xmls;
}
function globallink_webform_import(&$globallink, $submission_rid, $document_rids = NULL) {
  module_load_include('inc', 'globallink', 'globallink');
  $target_xml = $globallink->targetXML;
  if (!isset($target_xml)) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
    return;
  }
  $globallink->lids = array();
  $language = globallink_get_drupal_locale_code($globallink->targetLocale);
  $translated_arr = globallink_webform_get_translated_items($target_xml);
  $parent = $translated_arr['pid'];
  $parent_arr = globallink_webform_get_doc_rows_by_parent($submission_rid, $parent);
  $doc_arr = array();
  if ($document_rids != NULL) {
    $doc_arr = globallink_webform_get_doc_rows_by_rids($document_rids, $parent);
  }
  foreach ($translated_arr as $w_key => $lid_arr) {
    try {
      if ($w_key == 'pid') {
        continue;
      }
      $lid = $w_key;
      $import = FALSE;
      if ($document_rids != NULL) {
        if (count($doc_arr) > 0) {
          foreach ($doc_arr as $doc) {
            if ($doc->object_id == $lid) {
              $import = TRUE;
              break;
            }
          }
        }
      }

      // Only import the lids selected in this submission
      if ($document_rids != NULL && !$import) {
        continue;
      }
      array_push($globallink->lids, $lid);
      foreach ($lid_arr as $attribute => $translations) {
        if ($attribute == 'lid') {
          continue;
        }
        $webform = '';
        if ($translations == '#title' || $translations == '#description' || strpos($translations, '#options-') !== false) {
          $webform = globallink_load_source_data($lid);
          if ($webform == '') {
            throw new Exception('Source string not found for webform id ' . $lid . ' and field name ' . $attribute);
          }
          $report =& drupal_static(__FUNCTION__, array(
            'additions' => 0,
            'updates' => 0,
            'deletes' => 0,
            'skips' => 0,
          ));
          _locale_import_one_string_db($report, $language, $webform[0]->context, $webform[0]->source, $lid_arr['translation'], GLOBALLINK_ENTITY_TYPE_WEBFORM, $lid_arr['location'], LOCALE_IMPORT_OVERWRITE);
        }
      }
    } catch (Exception $e) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
      watchdog(GLOBALLINK_MODULE, 'Exception - %function - File[%file], Line[%line], Code[%code], Message[%message]', array(
        '%function' => __FUNCTION__,
        '%file' => $e
          ->getFile(),
        '%line' => $e
          ->getLine(),
        '%code' => $e
          ->getCode(),
        '%message' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }
  if ($globallink->status != GLOBALLINK_STATUS_TRANSLATION_ERROR) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
  }
  if ($document_rids != NULL) {
    if ($globallink->status != GLOBALLINK_STATUS_TRANSLATION_ERROR) {
      if (count($parent_arr) == count($globallink->lids)) {
        return TRUE;
      }
    }
    return FALSE;
  }
  elseif ($globallink->status != GLOBALLINK_STATUS_TRANSLATION_ERROR) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Gets webform filters.
 *
 * @return array
 *   Associative array of webform filters.
 */
function globallink_webform_get_translate_filter_query() {
  $filter = array();
  if (isset($_SESSION['globallink_webform_filter'])) {
    foreach ($_SESSION['globallink_webform_filter'] as $key => $value) {
      $filter[$key] = $value;
    }
  }
  return $filter;
}

/**
 * Gets sent webform rows by LID.
 *
 * @param string $lid
 *   The webform LID.
 *
 * @return array
 *   Array of webform rows.
 */
function globallink_webform_get_sent_tpt_rows_by_lid_and_target($pid, $lid, $target) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_WEBFORM, '=')
    ->condition('object_id', $lid, '=')
    ->condition('object_parent_id', $pid, '=')
    ->condition('target_lang_code', $target, '=')
    ->condition('target_status', array(
    GLOBALLINK_STATUS_TRANSLATION_SENT,
    GLOBALLINK_STATUS_TRANSLATION_ERROR,
    GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
  ), 'IN')
    ->execute();
  foreach ($result as $row) {
    return $row;
  }
  return NULL;
}
function globallink_webform_get_doc_rows_by_rids($rids, $parent) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('rid', $rids, 'IN')
    ->condition('object_parent_id', $parent, '=')
    ->execute()
    ->fetchAll();
  return $result;
}
function globallink_webform_get_doc_rows_by_parent($sub_rid, $parent) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_WEBFORM, '=')
    ->condition('submission_rid', $sub_rid, '=')
    ->condition('object_parent_id', $parent, '=')
    ->condition('target_status', array(
    GLOBALLINK_STATUS_TRANSLATION_SENT,
    GLOBALLINK_STATUS_TRANSLATION_ERROR,
    GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
  ), 'IN')
    ->execute()
    ->fetchAll();
  return $result;
}

/**
 * Gets translated webform items from XML data.
 *
 * @param object $xml
 *   XML representation of webform items.
 *
 * @return array
 *   Array of webform items.
 */
function globallink_webform_get_translated_items($xml) {
  if (is_null($xml) || !is_string($xml) || $xml == '') {
    return array();
  }
  $dom = new DomDocument();
  $dom->preserveWhiteSpace = FALSE;
  $dom
    ->loadXML($xml);
  $contents = $dom
    ->getElementsByTagName('content');
  $parent = '';
  foreach ($contents as $content) {
    if (!is_null($content->attributes)) {
      foreach ($content->attributes as $attr_name => $attr_node) {
        if ($attr_name == 'pid') {
          $parent = $attr_node->value;
        }
      }
    }
  }
  if ($parent == '') {
    return array();
  }
  $webform_arr = array();
  $webform_arr['pid'] = $parent;
  $webforms = $dom
    ->getElementsByTagName(GLOBALLINK_ENTITY_TYPE_WEBFORM);
  foreach ($webforms as $webform) {
    if (!is_null($webform->attributes)) {
      $lid = '';
      foreach ($webform->attributes as $attr_name => $attr_node) {
        if ($attr_name == 'lid') {
          $lid = $attr_node->value;
          break;
        }
      }
      $w_arr = array();
      foreach ($webform->attributes as $attr_name => $attr_node) {
        $w_arr[$attr_name] = $attr_node->value;
      }
      $w_arr['translation'] = $webform->nodeValue;
      $webform_arr[$lid] = $w_arr;
    }
  }
  return $webform_arr;
}

/**
 * Gets webform translation status.
 *
 * @param string $lid
 *   The webform LID.
 * @param string $tgt_locale
 *   The target locale.
 * @param string $title
 *   The translation title.
 *
 * @return string
 *   Status message.
 */
function globallink_check_webform_delete($lid) {
  $webform = globallink_load_source_data($lid);
  if (!$webform || is_null($webform)) {
    return TRUE;
  }
  return FALSE;
}
function globallink_webform_get_active_submission_by_id($id) {
  $query = db_select('globallink_document', 'gd');
  $query
    ->join('globallink_submission', 'gs', 'gd.submission_rid = gs.rid');
  $query
    ->condition('gd.object_id', $id, '=');
  $query
    ->condition('gd.entity_type', GLOBALLINK_ENTITY_TYPE_WEBFORM, '=');
  $or = db_or();
  $or
    ->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_SENT, '=');
  $or
    ->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_COMPLETED, '=');
  $or
    ->condition('gd.target_status', GLOBALLINK_STATUS_TRANSLATION_ERROR, '=');
  $query
    ->condition($or);
  $query
    ->fields('gd');
  $query
    ->fields('gs', array(
    'submission',
    'source_lang_code',
    'source_lang_name',
    'sub_target_lang_code',
    'sub_target_lang_name',
  ));
  $results = $query
    ->execute()
    ->fetchAll();
  return $results;
}