You are here

function joomla_import_categories in Joomla to Drupal 7

Same name and namespace in other branches
  1. 6 joomla.module \joomla_import_categories()
2 calls to joomla_import_categories()
joomla_cron in ./joomla.module
joomla_import_form_submit in ./joomla.module
3 string references to 'joomla_import_categories'
joomla_cron in ./joomla.module
joomla_import_form_checkboxes in ./joomla.module
These checkboxes are used on both the admin and import forms
joomla_uninstall in ./joomla.install
Implementation of hook_uninstall().

File

./joomla.module, line 503
The joomla module used for migrate Joomla to Drupal.

Code

function joomla_import_categories($joomla_update_duplicate = NULL) {
  joomla_database_init();
  if ($joomla_update_duplicate === NULL) {
    $joomla_update_duplicate = variable_get('joomla_update_duplicate', JOOMLA_UPDATE_DUPLICATE);
  }
  $joomla_prefix = variable_get('joomla_prefix', JOOMLA_PREFIX);
  db_set_active('joomla');
  $results_joomla_sec = db_query("SELECT * FROM {$joomla_prefix}sections");
  $sections_total = 0;
  $sections_updated = 0;
  $sections_new = 0;
  $sections_failed = 0;
  $section_vocabulary_map = array();

  //Joomla Sections to Drupal
  foreach ($results_joomla_sec as $section) {
    $sections_total++;
    db_set_active();
    $vid = db_query("SELECT vid FROM {joomla_sections} WHERE jsectionid = :jsectionid", array(
      ':jsectionid' => $section->id,
    ))
      ->fetchField();
    if ($vid && !$joomla_update_duplicate) {
      continue;
    }
    $vocabulary = NULL;
    if ($vid) {
      $vocabulary = taxonomy_vocabulary_load($vid);
      if (!$vocabulary) {
        drupal_set_message(t('Unable to load vocabulary id @id', array(
          '@id' => $vid,
        )), 'error');
        $sections_failed++;
        continue;
      }
    }
    else {
      $vocabulary = new stdClass();
    }
    $vocabulary->name = $section->title;
    if (function_exists('transliteration_get')) {
      $section->title = transliteration_get($section->title, '?', language_default('language'));
    }
    $vocabulary->machine_name = strtolower(str_replace(' ', '_', $section->title));
    $vocabulary->description = $section->name;
    $vocabulary->weight = $section->ordering;

    //check vocabulary name to be unique
    $duplicate = db_query("SELECT name FROM {taxonomy_vocabulary} WHERE name = :name LIMIT 1", array(
      ':name' => $vocabulary->name,
    ))
      ->fetchField();
    if (!empty($duplicate) && !$vid) {

      //the vocabulary name already exists
      continue;
    }
    $res = FALSE;
    if ($vid) {
      $res = drupal_write_record('taxonomy_vocabulary', $vocabulary, 'vid');
    }
    else {
      $res = drupal_write_record('taxonomy_vocabulary', $vocabulary);

      // Create an entry in the section <-> vocabulary map table
      $joomla_section = new Stdclass();
      $joomla_section->vid = $vocabulary->vid;
      $joomla_section->jsectionid = $section->id;
      drupal_write_record('joomla_sections', $joomla_section);
    }

    // Also, store this in a map for use when importing categories
    $section_vocabulary_map[$section->id] = $vocabulary->vid;
    switch ($res) {
      case SAVED_NEW:
        $sections_new++;
        break;
      case SAVED_UPDATED:
        $sections_updated++;
        break;
      default:
        $sections_failed++;
        break;
    }

    // Hook to allow other modules to modify the vocabulary
    module_invoke_all('joomla', 'taxonomy_vocabulary', $vocabulary, $section->id);
    db_set_active('joomla');
    joomla_sleep($sections_total);
  }

  //Process Joomla categories -> Drupal terms
  $categories_total = 0;
  $categories_updated = 0;
  $categories_new = 0;
  $categories_failed = 0;
  db_set_active('joomla');
  $categories = db_query("SELECT id,title,name,section,ordering FROM {$joomla_prefix}categories");
  foreach ($categories as $category) {

    // We have some sections that are non-integers. Ignore those.
    if ($category->section <= 0) {
      continue;
    }
    $categories_total++;
    db_set_active();
    $term_map = db_query("SELECT tid,jsectionid FROM {joomla_categories} WHERE jcategoryid = :jcategoryid", array(
      ':jcategoryid' => $category->id,
    ))
      ->fetch();
    if (isset($term_map->tid) && !$joomla_update_duplicate) {
      continue;
    }
    $term = NULL;
    if ($term_map) {
      $term = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(
        ':tid' => $term_map->tid,
      ))
        ->fetch();
      if (!$term) {
        drupal_set_message(t('Unable to load term id @id', array(
          '@id' => $term_map->tid,
        )), 'error');
        $categories_failed++;
        continue;
      }
    }
    else {
      $term = new stdClass();
    }
    $term->name = $category->title;
    $term->description = $category->name;
    $term->weight = $category->ordering;
    $term->vid = $section_vocabulary_map[$category->section];
    $res = FALSE;
    if ($term_map) {
      $res = drupal_write_record('taxonomy_term_data', $term, 'tid');

      // Check if the Joomla category's section has changed
      if ($term_map->jsectionid != $category->section) {
        $term_map->jsectionid = $category->section;
        drupal_write_record('joomla_categories', $term_map, 'jsectionid');
      }
    }
    else {
      $res = drupal_write_record('taxonomy_term_data', $term);

      // Create an entry in the section <-> vocabulary map table
      $joomla_category = new Stdclass();
      $joomla_category->tid = $term->tid;
      $joomla_category->jsectionid = $category->section;
      $joomla_category->jcategoryid = $category->id;
      drupal_write_record('joomla_categories', $joomla_category);
      $term_hierarchy = new stdClass();
      $term_hierarchy->tid = $term->tid;
      $term_hierarchy->parent = 0;
      drupal_write_record('taxonomy_term_hierarchy', $term_hierarchy);
    }
    switch ($res) {
      case SAVED_NEW:
        $categories_new++;
        break;
      case SAVED_UPDATED:
        $categories_updated++;
        break;
      default:
        $categories_failed++;
        break;
    }
  }

  // Hook to allow other modules to modify the term
  module_invoke_all('joomla', 'term', $term, $category->id);
  joomla_sleep($categories_total);
  drupal_set_message(t('Processed @total sections (@new new, @updated updated, @failed errors)', array(
    '@total' => $sections_total,
    '@new' => $sections_new,
    '@updated' => $sections_updated,
    '@failed' => $sections_failed,
  )));
  drupal_set_message(t('Processed @total categories (@new new, @updated updated, @failed errors)', array(
    '@total' => $categories_total,
    '@new' => $categories_new,
    '@updated' => $categories_updated,
    '@failed' => $categories_failed,
  )));
}