You are here

function globallink_menu_import in GlobalLink Connect for Drupal 7.7

1 call to globallink_menu_import()
globallink_background_import in ./globallink_background_jobs.inc
Imports the documents

File

globallink_menu/globallink_menu.inc, line 227

Code

function globallink_menu_import(&$globallink) {
  module_load_include('inc', 'i18n_menu', 'i18n_menu.admin');
  module_load_include('inc', 'globallink', 'globallink');
  $target_xml = $globallink->targetXML;
  if (empty($target_xml)) {
    $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
    return;
  }
  $language = globallink_get_drupal_locale_code($globallink->targetLocale);
  $arr = globallink_menu_get_translated_items($target_xml);
  try {
    $mlid = $arr['mlid'];
    $t_link_title = $arr['link_title'];
    $source_link = menu_link_load($mlid);
    $parent_link = $source_link['plid'];
    if ($parent_link != 0) {
      $p_link = menu_link_load($parent_link);
      $parent_tr = i18n_menu_link_load($p_link['link_path'], $language);
      if (empty($parent_tr)) {
        $globallink->status = GLOBALLINK_STATUS_TRANSLATION_ERROR;
        watchdog(GLOBALLINK_MODULE, 'Cannot update as the parent %parent is not translated.', array(
          '%parent' => $p_link['link_title'],
        ), WATCHDOG_ERROR);
        return;
      }
    }
    if ($source_link) {
      $t_title = FALSE;
      if (isset($source_link['options'])) {
        if (isset($source_link['options']['attributes'])) {
          if (isset($source_link['options']['attributes']['title'])) {
            if ($source_link['options']['attributes']['title'] != '') {
              $t_title = isset($arr['title']) ? $arr['title'] : FALSE;
            }
          }
        }
      }
      $t_plid = 0;
      if (isset($source_link['plid']) && $source_link['plid'] != 0) {
        $plid = $source_link['plid'];
        $p_source_link = menu_link_load($plid);
        $p_path_arr = translation_path_get_translations($p_source_link['link_path']);
        if (isset($p_path_arr[$language])) {
          $p_target_link = i18n_menu_link_load($p_path_arr[$language], $language);
          $t_plid = $p_target_link['mlid'];
        }
        elseif (isset($p_source_link['i18n_tsid']) && $p_source_link['i18n_tsid'] != 0) {
          $i18n_arr = globallink_menu_get_i18n_menu_set($p_source_link['i18n_tsid']);
          if (isset($i18n_arr[$language])) {
            $menu_link_row = $i18n_arr[$language];
            $t_plid = $menu_link_row->mlid;
          }
        }
      }
      $path_arr = translation_path_get_translations($source_link['link_path']);
      $link_path = '';
      if (!isset($path_arr[$language])) {
        $link_path = $source_link['link_path'];
      }
      else {
        $link_path = $path_arr[$language];
      }
      $t_link = NULL;
      if (isset($source_link['i18n_tsid']) && $source_link['i18n_tsid'] != 0) {
        $i18n_arr = globallink_menu_get_i18n_menu_set($source_link['i18n_tsid']);
        if (isset($i18n_arr[$language])) {
          $menu_link_row = $i18n_arr[$language];
          $t_link = menu_link_load($menu_link_row->mlid);
        }
      }
      if (is_array($t_link)) {
        $t_link['link_title'] = $t_link_title;
        $t_link['menu_name'] = $source_link['menu_name'];
        $t_link['customized'] = $source_link['customized'];
        $t_link['has_children'] = $source_link['has_children'];
        $t_link['expanded'] = $source_link['expanded'];
        $t_link['weight'] = $source_link['weight'];
        $t_link['hidden'] = $source_link['hidden'];
        if ($t_title && $t_link != '') {
          if (isset($t_link['options'])) {
            if (isset($t_link['options']['attributes'])) {
              $t_link['options']['attributes']['title'] = $t_title;
            }
            else {
              $t_link['options']['attributes'] = array(
                'title' => $t_title,
              );
            }
          }
        }
      }
      else {
        $t_link = array();
        $t_link['link_title'] = $t_link_title;
        $t_link['language'] = $language;
        $t_link['link_path'] = $link_path;
        $t_link['menu_name'] = $source_link['menu_name'];
        $t_link['customized'] = $source_link['customized'];
        $t_link['has_children'] = $source_link['has_children'];
        $t_link['expanded'] = $source_link['expanded'];
        $t_link['weight'] = $source_link['weight'];
        $t_link['hidden'] = $source_link['hidden'];
        if ($t_plid != 0) {
          $t_link['plid'] = $t_plid;
        }
        if ($t_title) {
          $t_link['options'] = array(
            'attributes' => array(
              'title' => $t_title,
            ),
            'langcode' => $language,
          );
        }
      }
      menu_link_save($t_link);
      $translation_set = $source_link['i18n_tsid'] ? i18n_translation_set_load($source_link['i18n_tsid']) : i18n_translation_set_create('menu_link');
      $translation_set
        ->add_item($source_link, $source_link['language']);
      $translation_set
        ->add_item($t_link, $language);
      $translation_set
        ->save(TRUE);
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_IMPORTED;
    }
    else {

      // Source Link is deleted
      $globallink->status = GLOBALLINK_STATUS_TRANSLATION_SOURCE_DELETED;
      return;
    }
  } 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;
}