You are here

globallink_fieldable_panels.inc in GlobalLink Connect for Drupal 7.7

File

globallink_fieldable_panels/globallink_fieldable_panels.inc
View source
<?php

/**
 * Sends fieldable panels for translation.
 *
 * @param array $fpids
 *   The array of fieldable panels IDs.
 * @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_fieldable_panels_pane_send_for_translations($fpids, $pd4, $submission_name, $due_date, $project_code, $source_locale, $target_locale_arr, $submission_details, $submission_priority) {
  module_load_include('inc', 'globallink', 'gl_ws/gl_ws_send_translations');
  module_load_include('inc', 'globallink', 'globallink');
  $submitter = $submission_details['submitter'];
  $globallink_arr = array();
  foreach ($fpids as $fpid) {
    $rows = globallink_fieldable_panels_get_sent_rows_by_fpid($fpid);
    $target_arr = $target_locale_arr;
    $fpp_info = globallink_fieldable_panels_pane_get_info($fpid);
    $title = $fpp_info->title;
    foreach ($rows as $row) {
      if (array_search($row->target_lang_code, $target_locale_arr)) {
        unset($target_arr[$row->target_lang_code]);
        watchdog(GLOBALLINK_MODULE, 'Skipping FPP Id - %id for locales %locale', array(
          '%id' => $fpid,
          '%locale' => $row->target_lang_code,
        ), WATCHDOG_DEBUG);
      }
    }
    if (empty($target_arr)) {
      continue;
    }
    $xml = globallink_fieldable_panels_pane_get_xml($fpid);
    $name = 'Fieldable_Panels_Panes_' . $fpid . '.xml';
    $globallink = new GlobalLink();
    $globallink->type = GLOBALLINK_ENTITY_TYPE_FPP;
    $globallink->metadata = GLOBALLINK_ENTITY_TYPE_FPP;
    $globallink->title = $title;
    $globallink->sourceLocale = $source_locale;
    $globallink->targetLocale = $target_arr;
    $globallink->sourceXML = $xml;
    $globallink->sourceFileName = $name;
    $globallink->submissionName = $submission_name;
    $globallink->submissionPriority = $submission_priority;
    $globallink->dueDate = $due_date;
    $globallink->otherObjectId = $fpid;
    $globallink->submissionInstructions = $submission_details['instructions'] . "\nSubmitter: " . $submitter;
    $globallink_arr[GLOBALLINK_ENTITY_TYPE_FPP][] = $globallink;
  }
  return $globallink_arr;
}

/**
 * Gets XML data from specific fieldable panel.
 *
 * @param string $fpid
 *   The fieldable panel ID.
 * @param string $path
 *  The path to the page with the Fieldable Panel Pane on it.
 *
 * @return array
 *   Associative array of fieldable panel XML data.
 */
function globallink_fieldable_panels_pane_get_xml($fpid, $path = NULL) {
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = TRUE;
  $root = $dom
    ->createElement('content');
  $dom
    ->appendChild($root);
  $id = $dom
    ->createAttribute('fpid');
  $id->value = $fpid;
  $root
    ->appendChild($id);
  $fieldable_panels_pane = fieldable_panels_panes_load($fpid);
  $language = $fieldable_panels_pane->language;
  $fields = globallink_fieldable_panels_pane_get_fields($fieldable_panels_pane->bundle);
  $configured_fields = globallink_get_configured_fields_for_fpp($fieldable_panels_pane->bundle);
  foreach ($fields as $field) {
    if (empty($fieldable_panels_pane->{$field})) {
      continue;
    }
    if (array_key_exists($field, $configured_fields) && $configured_fields[$field]->translatable == 1) {
      $field_values = field_get_items('fieldable_panels_pane', $fieldable_panels_pane, $field, $language);
      $field_info = field_info_field($field);
      if (is_array($field_values) && !empty($field_values)) {
        foreach ($field_values as $delta => $values) {
          if ($field_info['type'] == 'link_field') {
            globallink_insert_child_element($dom, $root, 'field', $values['title'], array(
              'field' => $field,
              'delta' => $delta,
              'langcode' => $language,
            ));
          }
          elseif ($field_info['type'] == 'image') {
            if (isset($values['alt'])) {
              globallink_insert_child_element($dom, $root, 'field_image', $values['alt'], array(
                'field_name' => $field,
                'type' => 'alt',
                'delta' => $delta,
                'langcode' => $language,
              ));
            }
            if (isset($values['title'])) {
              globallink_insert_child_element($dom, $root, 'field_image', $values['title'], array(
                'field_name' => $field,
                'type' => 'title',
                'delta' => $delta,
                'langcode' => $language,
              ));
            }
          }
          elseif (!empty($values['value'])) {
            globallink_insert_child_element($dom, $root, 'field', $values['value'], array(
              'field' => $field,
              'delta' => $delta,
              'langcode' => $language,
            ));
          }
        }
      }
      elseif (!empty($field_values) && is_string($field_values)) {

        // Test this without field translation
        globallink_insert_child_element($dom, $root, 'field', $field_values, array(
          'field' => $field,
        ));
      }
    }
  }
  $xml = $dom
    ->saveXML();
  return $xml;
}

/**
 * Gets fieldable panels fields based on bundle.
 *
 * @param string $bundle
 *   The bundle.
 *
 * @return array
 *   Array of fields.
 */
function globallink_fieldable_panels_pane_get_fields($bundle) {
  $fields = array();
  $info = entity_get_info('fieldable_panels_pane');
  foreach ($info['bundles'] as $bundle_name => $bundle_info) {
    if ($bundle_name == $bundle) {
      foreach (field_info_instances('fieldable_panels_pane', $bundle_name) as $field) {
        $fields[] = $field['field_name'];
      }
    }
  }
  return $fields;
}

/**
 * Gets fieldable panel translation status.
 *
 * @param string $fpid
 *   The fieldable panel ID.
 * @param string $tgt_locale
 *   The target locale.
 * @param string $title
 *   The translation title.
 *
 * @return string
 *   Status message.
 */
function globallink_fieldable_panels_check_delete($fpid) {
  $entity = fieldable_panels_panes_load($fpid);
  if (empty($entity)) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Gets sent fieldable panel rows by node ID.
 *
 * @param string $fpid
 *   The fieldable panel ID.
 *
 * @return array
 *   Array of fieldable panels rows.
 */
function globallink_fieldable_panels_get_sent_rows_by_fpid($fpid) {
  $result = db_select('globallink_document', 'tc')
    ->fields('tc')
    ->condition('object_id', $fpid, '=')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_FPP, '=')
    ->condition('target_status', array(
    GLOBALLINK_STATUS_TRANSLATION_SENT,
    GLOBALLINK_STATUS_TRANSLATION_ERROR,
    GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
  ), 'IN')
    ->execute();
  $rows = array();
  foreach ($result as $row) {
    $rows[] = $row;
  }
  return $rows;
}

/**
 * Gets array of translated fieldable panels.
 *
 * @param object $xml
 *   XML representation of fieldable panels.
 *
 * @return array
 *   Associative array of translated fieldable panels.
 */
function globallink_fieldable_panels_get_translated_array($xml) {
  if (is_null($xml) || !is_string($xml) || $xml == '') {
    return array();
  }
  $dom = new DomDocument();
  $dom->preserveWhiteSpace = FALSE;
  $dom
    ->loadXML($xml);
  $arr = array();
  $titles = $dom
    ->getElementsByTagName('title');
  foreach ($titles as $title) {
    $arr['title'] = $title->nodeValue;
  }
  $contents = $dom
    ->getElementsByTagName('content');
  $fpid = '';
  foreach ($contents as $content) {
    if (!is_null($content->attributes)) {
      foreach ($content->attributes as $attr_name => $attr_node) {
        if ($attr_name == 'fpid') {
          $fpid = $attr_node->value;
        }
      }
    }
  }
  if ($fpid == '') {
    return array();
  }
  $arr['fpid'] = $fpid;
  $field_image = $dom
    ->getElementsByTagName('field_image');
  foreach ($field_image as $attr) {
    $field_image_object = new GLFieldImage();
    if (!is_null($attr->attributes)) {
      foreach ($attr->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'type':
            if ($attr_node->value == 'title') {
              $field_image_object->title = $attr->nodeValue;
            }
            elseif ($attr_node->value == 'alt') {
              $field_image_object->alt = $attr->nodeValue;
            }
            continue 2;
          case 'delta':
            $field_image_object->delta = $attr_node->value;
            continue 2;
          case 'field_name':
            $field_image_object->field_name = $attr_node->value;
            continue 2;
          case 'subfield':
            $field_image_object->subfield = $attr_node->value;
            $field_image_object->{$field_image_object->subfield} = $attr->nodeValue;
            $field_image_object->type = 'file_entity';
            continue 2;
          case 'langcode':
            $field_image_object->langcode = $attr_node->value;
            continue 2;
          case 'label':
          case 'subfield_label':
            $field_image_object->{$attr_name} = $attr_node->value;
            continue 2;
        }
      }
      if (is_null($field_image_object->delta)) {
        $field_image_object->delta = '0';
      }
      if (isset($field_image_object->field_name)) {
        if (isset($field_image_object->title)) {
          $arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta]->title = $field_image_object->title;
        }
        if (isset($field_image_object->alt)) {
          $arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta]->alt = $field_image_object->alt;
        }
      }
      else {
        $arr[$field_image_object->field_name][$field_image_object->langcode][$field_image_object->delta] = $field_image_object;
      }
    }
  }
  $fields = $dom
    ->getElementsByTagName('field');
  foreach ($fields as $field) {
    $field_obj = new GLField();
    $field_obj->type = 'field';
    $field_obj->translatedContent = $field->nodeValue;
    if (is_null($field->attributes)) {
      continue;
    }
    foreach ($field->attributes as $attr_name => $attr_node) {
      switch ($attr_name) {
        case 'field':
          $field_obj->fieldName = $attr_node->value;
          continue 2;
        case 'delta':
          $field_obj->delta = $attr_node->value;
          continue 2;
        case 'langcode':
          $field_obj->langcode = $attr_node->value;
          continue 2;
      }
    }
    if (is_null($field_obj->langcode)) {
      $field_obj->langcode = LANGUAGE_NONE;
    }
    if (is_null($field_obj->delta)) {
      $field_obj->delta = '0';
    }
    $arr[$field_obj->fieldName][$field_obj->langcode][$field_obj->delta] = $field_obj;
  }
  return $arr;
}
function globallink_fieldable_panels_import(&$globallink) {
  module_load_include('inc', 'globallink', 'globallink');
  $target_xml = $globallink->targetXML;
  try {
    if (empty($target_xml)) {
      throw new Exception("Target XML is missing.");
    }
    $target_locale = globallink_get_drupal_locale_code($globallink->targetLocale);
    $source_locale = globallink_get_drupal_locale_code($globallink->sourceLocale);
    $translated_arr = globallink_fieldable_panels_get_translated_array($target_xml);
    $fpid = $translated_arr['fpid'];
    unset($translated_arr['fpid']);
    $entity = fieldable_panels_panes_load($fpid);
    if (empty($entity)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      $globallink->sourceDeleted = TRUE;
      return;
    }
    $fields = globallink_fieldable_panels_pane_get_fields($entity->bundle);
    foreach ($fields as $field) {
      $field_def = field_read_field($field);
      $t_field_lang = LANGUAGE_NONE;
      if (empty($translated_arr[$field])) {
        if (isset($entity->{$field}[$source_locale])) {
          $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
        }
        continue;
      }
      if (key($translated_arr[$field]) !== LANGUAGE_NONE) {
        $t_field_lang = key($translated_arr[$field]);
      }
      if (empty($entity->{$field}[$target_locale]) && $target_locale != LANGUAGE_NONE) {
        $entity->{$field}[$target_locale] = $entity->{$field}[$t_field_lang];
      }
      $t_field_arr = $translated_arr[$field][$source_locale];
      foreach ($entity->{$field}[$target_locale] as $delta => $fp_field) {
        if (empty($t_field_arr[$delta])) {
          continue;
        }
        $translation = $t_field_arr[$delta]->translatedContent;
        if ($field_def['type'] == 'link_field') {
          $entity->{$field}[$target_locale][$delta]['title'] = $translation;
        }
        elseif ($field_def['type'] == 'image') {
          if (isset($t_field_arr[$delta]->title)) {
            $entity->{$field}[$target_locale][$delta]['title'] = $t_field_arr[$delta]->title;
          }
          if (isset($t_field_arr[$delta]->alt)) {
            $entity->{$field}[$target_locale][$delta]['alt'] = $t_field_arr[$delta]->alt;
          }
        }
        else {
          $entity->{$field}[$target_locale][$delta]['value'] = $translation;
        }
      }
    }
    $entity->revision = 1;
    $entity->translations->data[$target_locale] = array(
      'language' => $target_locale,
      // Target language
      'source' => $source_locale,
      // Source language
      'uid' => $entity->uid,
      'status' => variable_get('globallink_publish_node', 0),
      // publish translation
      'translate' => 0,
      // Translation out of date
      'created' => $entity->created,
    );
    $entity->translations->hook[$target_locale] = array(
      'hook' => 'insert',
      'date' => NULL,
    );
    fieldable_panels_panes_save($entity);
  } 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;
  }
  return;
}
function globallink_get_all_translatable_fpp_types_and_names() {
  $entity_info = entity_get_info('fieldable_panels_pane');
  $arr = array();
  if (isset($entity_info) && isset($entity_info['bundles'])) {
    $bundles = $entity_info['bundles'];
    foreach ($bundles as $key => $bundle) {
      $arr['fpp:' . $key] = $bundle['label'];
    }
    asort($arr);
  }
  return $arr;
}
function globallink_get_configured_fields_for_fpp($fpp_type) {
  $result = db_select('globallink_field_config', 'tfg')
    ->fields('tfg')
    ->condition('content_type', 'fpp:' . $fpp_type, '=')
    ->condition('entity_type', 'fieldable_panels_pane', '=')
    ->execute();
  $arr = array();
  foreach ($result as $row) {
    $arr[$row->field_name] = $row;
  }
  return $arr;
}
function globallink_fieldable_panels_pane_get_info($fpid) {
  $results = db_select(GLOBALLINK_ENTITY_TYPE_FPP, 'fpp')
    ->fields('fpp')
    ->condition('fpid', $fpid, '=')
    ->execute();
  $arr = new stdClass();
  foreach ($results as $result) {
    $arr = $result;
  }
  return $arr;
}
function globallink_fpp_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_FPP, '=');
  $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;
}