You are here

globallink_custom_entity.inc in GlobalLink Connect for Drupal 7.7

File

globallink_custom_entity/globallink_custom_entity.inc
View source
<?php

function globallink_entity_reference_get_xml($entity, $target_arr, $tnid = NULL, $tvid = NULL, &$name = '', $for_display = FALSE, $source_lang = '', $entity_type) {
  if (is_null($entity) || !$entity) {
    return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
  }
  if ($entity && is_object($entity)) {
    $name = globallink_format_file_name('CustomEntity') . '_' . $entity->type . '_' . $entity->id . $name;
    $xml = globallink_entity_reference_generate_xml_document($entity, $target_arr, $tnid, $for_display, $source_lang, $entity_type);
    return $xml;
  }
  return GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
}
function globallink_entity_reference_generate_xml_document($entity, $target_arr, $tnid = NULL, $for_display = FALSE, $source_lang = '', $entity_type) {
  $entityref = array();
  $is_hook_enabled = variable_get('globallink_implementation_type', 0);
  try {
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->formatOutput = TRUE;
    $root = $dom
      ->createElement('content');
    $eid = $dom
      ->createAttribute('eid');
    if ($tnid != NULL) {
      $eid->value = $tnid;
    }
    else {
      $eid->value = $entity->id;
    }
    $root
      ->appendChild($eid);
    $type = $dom
      ->createAttribute('entityType');
    $type->value = $entity_type;
    $root
      ->appendChild($type);
    $entity_bundle = $dom
      ->createAttribute('entityBundle');
    $entity_bundle->value = $entity->type;
    $root
      ->appendChild($entity_bundle);
    $url = $dom
      ->createAttribute('pageUrl');
    $root
      ->appendChild($url);
    $dom
      ->appendChild($root);
    $field_arr = field_info_instances($entity_type, $entity->type);

    //check if fields are added to field config table.

    //globallink_custom_entity_add_fields_config($field_arr, $entity, $entity_type);
    $keys = array_keys($field_arr);
    foreach ($keys as $field) {
      $field_def = field_read_field($field);
      $items = field_get_items($entity_type, $entity, $field, $source_lang);
      if ($items) {
        $parent_fc = '';
        if ($field_def['type'] == 'field_collection') {
          $parent_fc = $field;
          if (isset($items[0]['revision_id'])) {
            entity_load('field_collection_item', array(
              $items[0]['value'],
            ), array(
              'revision_id' => $items[0]['revision_id'],
            ));
          }
        }
        $entityref[] = globallink_entity_traverse_fields_and_field_collections($entity_type, $entity->type, $parent_fc, $entity->type, $entity->id, $items, $field, $field_def, $dom, $root, $entity, $target_arr, 0);
      }
    }
    if (module_exists('metatag')) {
      if (isset($entity->metatags)) {
        $ignore_metatags = [
          'geo.position',
          'icbm',
          'rating',
          'referrer',
          'revisit-after',
          'content-language',
        ];
        if ($for_display) {
          if (!empty($entity->metatags[$entity->language])) {
            $metatags = $entity->metatags[$entity->language];
            foreach ($metatags as $name => $value) {
              if (isset($value['value']) && !is_array($value['value'])) {
                if (in_array($name, $ignore_metatags)) {
                  continue;
                }
                globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
                  'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
                  'name' => $name,
                  'label' => 'Metatag - ' . ucwords($name),
                ));
              }
            }
          }
        }
        elseif (globallink_is_field_configured_for_translation(GLOBALLINK_ENTITY_TYPE_NODE, $entity->type, 'metatags', $entity->type)) {
          if (!empty($entity->metatags[$entity->language])) {
            $metatags = $entity->metatags[$entity->language];
            foreach ($metatags as $name => $value) {
              if (isset($value['value']) && !is_array($value['value'])) {
                if (in_array($name, $ignore_metatags)) {
                  continue;
                }
                globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
                  'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
                  'name' => $name,
                  'label' => 'Metatag - ' . ucwords($name),
                ));
              }
            }
          }
        }
      }
    }
  } catch (Exception $e) {
    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);
    throw $e;
  }
  $root_element = $dom
    ->getElementsByTagName('content')
    ->item(0);
  if (!$root_element
    ->hasChildNodes()) {
    return FALSE;
  }
  $result = array(
    $dom
      ->saveXML(),
    $entityref,
  );
  return $result;
}
function globallink_eck_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_eck_get_translated_array($target_xml, $target_locale);
    $enid = $translated_arr['eid'];

    // get the entity type here.
    $entity_type = $translated_arr['entity_type'];
    $entity_bundle = $translated_arr['entity_bundle'];
    unset($translated_arr['eid']);
    $entity = entity_load_single($entity_type, $enid);
    if (empty($entity)) {
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      $globallink->sourceDeleted = TRUE;
      return;
    }
    if (module_exists('metatag')) {
      if (isset($translated_arr['metatag'])) {
        $lang = entity_language($entity_type, $entity);
        $target_metatag_arr = $translated_arr['metatag'];
        if (!empty($entity->metatags[$lang])) {
          $metatags_names = array_keys($entity->metatags[$lang]);
          $n_metatag =& $entity->metatags;
          foreach ($metatags_names as $name) {
            if (isset($target_metatag_arr[$name]) && isset($target_metatag_arr[$name][0])) {
              $gl_obj = $target_metatag_arr[$name]['0'];
              if (is_object($gl_obj)) {
                $translated_content = $gl_obj->translatedContent;
              }
              else {
                $translated_content = $gl_obj;
              }
              $n_metatag[$target_locale][$name] = array(
                'value' => $translated_content,
              );
            }
          }
        }
      }
    }
    $fields = globallink_eck_get_fields($entity_bundle, $entity_type);
    foreach ($fields as $field) {
      $field_def = field_read_field($field);
      $t_field_lang = LANGUAGE_NONE;
      if ($field_def['type'] == 'image') {
        $entity->{$field}[$target_locale] = $entity->{$field}[$source_locale];
        $delta = key($entity->{$field}[$target_locale]);
        if (!isset($translated_arr[$field])) {
          continue;
        }
        $key = key($translated_arr[$field]);
        $obj = $translated_arr[$field][$key][$delta];
        if (is_object($obj)) {
          if (isset($obj->title)) {
            $entity->{$field}[$target_locale][$delta]['title'] = $obj->title;
          }
          if (isset($obj->alt)) {
            $entity->{$field}[$target_locale][$delta]['alt'] = $obj->alt;
          }
        }
      }
      else {
        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}[$source_locale];
        }
        $t_field_arr = $translated_arr[$field][$t_field_lang];
        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;
          }
          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
      'status' => variable_get('globallink_publish_node', 0),
      // publish translation
      'translate' => 0,
    );
    $entity->translations->hook[$target_locale] = array(
      'hook' => 'insert',
      'date' => NULL,
    );
    $result = entity_save($entity_type, $entity);
    if ($result != FALSE) {

      /*if (module_exists('field_collection') && isset($translated_arr['field_collection'])) {
          globallink_beans_save_field_collections($entity, $translated_arr, $target_locale);
        }*/
    }
  } catch (SoapFault $se) {
    $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_eck_get_translated_array($xml, $target_lang = LANGUAGE_NONE) {
  if (is_null($xml) || !is_string($xml) || $xml == '') {
    return array();
  }
  $dom = new DomDocument();
  $dom->preserveWhiteSpace = FALSE;
  $dom
    ->loadXML($xml);
  $arr = array();
  $contents = $dom
    ->getElementsByTagName('content');
  $eid = '';
  foreach ($contents as $content) {
    if (!is_null($content->attributes)) {
      foreach ($content->attributes as $attr_name => $attr_node) {
        if ($attr_name == 'eid') {
          $eid = $attr_node->value;
        }
        else {
          if ($attr_name == 'entityType') {
            $entity_type = $attr_node->value;
          }
          else {
            if ($attr_name == 'entityBundle') {
              $entity_bundle = $attr_node->value;
            }
          }
        }
      }
    }
  }
  if ($eid == '') {
    return array();
  }
  $arr['eid'] = $eid;
  $arr['entity_type'] = $entity_type;
  $arr['entity_bundle'] = $entity_bundle;
  $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 '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($arr[$field_image_object->field_name][$target_lang][$field_image_object->delta])) {
          $arr[$field_image_object->field_name][$target_lang][$field_image_object->delta] = new GLFieldImage();
        }
        if (isset($field_image_object->title)) {
          $arr[$field_image_object->field_name][$target_lang][$field_image_object->delta]->title = $field_image_object->title;
        }
        if (isset($field_image_object->alt)) {
          $arr[$field_image_object->field_name][$target_lang][$field_image_object->delta]->alt = $field_image_object->alt;
        }
      }
      else {
        $arr[$field_image_object->field_name][$target_lang][$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_name':
          $field_obj->fieldName = $attr_node->value;
          continue 2;
        case 'delta':
          $field_obj->delta = $attr_node->value;
          continue 2;
        case 'parent_fc':
          $field_obj->parentFCName = $attr_node->value;
          continue 2;
        case 'bundle':
          $field_obj->bundle = $attr_node->value;
          continue 2;
        case 'entity_id':
          $field_obj->entityId = $attr_node->value;
          continue 2;
        case 'entity_type':
          $field_obj->entityType = $attr_node->value;
          continue 2;
      }
    }
    if (is_null($field_obj->delta)) {
      $field_obj->delta = '0';
    }
    if ($field_obj->entityType == 'field_collection_item') {
      $arr['field_collection'][$field_obj->parentFCName][$field_obj->bundle][$field_obj->entityId][$field_obj->fieldName][$target_lang][$field_obj->delta] = $field_obj;
    }
    else {
      $arr[$field_obj->fieldName][$target_lang][$field_obj->delta] = $field_obj;
    }
  }
  $metatags = $dom
    ->getElementsByTagName('metatag');
  foreach ($metatags as $metatag) {
    $metatag_obj = new GLField();
    $metatag_obj->type = 'metatag';
    $metatag_obj->translatedContent = $metatag->nodeValue;
    if (is_null($metatag->attributes)) {
      continue;
    }
    foreach ($metatag->attributes as $attr_name => $attr_node) {
      switch ($attr_name) {
        case 'entity_type':
          $metatag_obj->entityType = $attr_node->value;
          continue 2;
        case 'content_type':
          $field_obj->contentType = $attr_node->value;
          continue 2;
        case 'bundle':
          $field_obj->bundle = $attr_node->value;
          continue 2;
        case 'entity_id':
          $metatag_obj->entityId = $attr_node->value;
          continue 2;
        case 'name':
          $metatag_obj->fieldName = $attr_node->value;
          continue 2;
        case 'label':
          $metatag_obj->fieldLabel = $attr_node->value;
          continue 2;
      }
    }
    if (is_null($metatag_obj->entityId)) {
      $metatag_obj->entityId = '0';
    }
    if (is_null($metatag_obj->bundle)) {
      $metatag_obj->bundle = $metatag_obj->fieldName;
    }
    $arr['metatag'][$metatag_obj->bundle][$metatag_obj->entityId] = $metatag_obj;
  }
  return $arr;
}
function globallink_eck_get_fields($entity_bundle, $entity_type) {
  $fields = array();
  $info = entity_get_info($entity_type);
  foreach ($info['bundles'] as $bundle_name => $bundle_info) {
    if ($bundle_name == $entity_bundle) {
      foreach (field_info_instances($entity_type, $bundle_name) as $field) {
        $fields[] = $field['field_name'];
      }
    }
  }
  return $fields;
}

//Return active documents.
function globallink_eck_get_sent_rows_by_id($id, $object_type) {
  $result = db_select('globallink_document', 'gd')
    ->fields('gd')
    ->condition('entity_type', GLOBALLINK_ENTITY_TYPE_ECK_ENTITY, '=')
    ->condition('object_id', $id, '=')
    ->condition('object_type', $object_type, '=')
    ->condition('target_status', array(
    GLOBALLINK_STATUS_TRANSLATION_SENT,
    GLOBALLINK_STATUS_TRANSLATION_COMPLETED,
    GLOBALLINK_STATUS_TRANSLATION_ERROR,
  ), 'IN')
    ->execute();
  $rows = array();
  foreach ($result as $row) {
    $rows[] = $row;
  }
  return $rows;
}
function globallink_custom_entity_add_fields_config($field_arr, $entity, $entity_type) {

  //load entity here.

  //get all fields associated, check in field config and if not found add them.
  $keys = array_keys($field_arr);
  foreach ($keys as $field) {
    $field_def = field_read_field($field);
    if (!globallink_is_field_configured_for_translation($entity_type, $entity->type, $field, $entity->type) && $field_def['translatable'] == 1) {
      $allowed = array(
        'text',
        'entityreference',
        ' text_with_summary',
        'text_long',
        'image',
      );
      if (!in_array($field_def['type'], $allowed)) {
        continue;
      }
      db_insert('globallink_field_config')
        ->fields(array(
        'content_type' => $entity->type,
        'entity_type' => $entity_type,
        'bundle' => $entity->type,
        'field_name' => $field,
        'field_type' => $field_def['type'],
        'field_label' => $field,
        'translatable' => 1,
      ))
        ->execute();
    }
  }
}
function globallink_get_all_translatable_customentity_types_and_names() {
  $rows = db_select('eck_bundle', 'eb')
    ->fields('eb')
    ->execute();
  $arr = array();
  foreach ($rows as $row) {
    $arr['ce:' . $row->machine_name] = $row->name;
  }
  asort($arr);
  return $arr;
}
function globallink_get_entity_type_for_bundle($bundle) {
  $rows = db_select('eck_bundle', 'eb')
    ->fields('eb', array(
    'entity_type',
  ))
    ->condition('machine_name', $bundle, '=')
    ->execute();
  foreach ($rows as $row) {
    $entity_type = $row->entity_type;
  }
  return $entity_type;
}
function globallink_get_bundle_for_field($bundle) {
  $rows = db_select('eck_bundle', 'eb')
    ->fields('eb', array(
    'name',
  ))
    ->condition('machine_name', $bundle, '=')
    ->execute();
  foreach ($rows as $row) {
    $bundle = $row->name;
  }
  return $bundle;
}