function joomla_category_save in Joomla to Drupal 7.2
1 call to joomla_category_save()
File
- ./
joomla.batch.inc, line 173
Code
function joomla_category_save(&$context) {
$joomla_update_duplicate = $context['sandbox']['joomla_update_duplicate'];
$offset =& $context['sandbox']['sections_offset'];
// Sections must finish before we can start importing terms
$sections_finished =& $context['sandbox']['sections_finished'];
db_set_active('joomla');
$sections = db_select('sections', 's')
->fields('s')
->range($offset, 10)
->execute()
->fetchAll();
$sections_finished = (bool) (!$categories);
db_set_active();
//Joomla Sections to Drupal
foreach ($sections as $num => $section) {
$context['sandbox']['progress']++;
$context['results']['sections_total']++;
$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');
$context['results']['sections_failed']++;
continue;
}
}
else {
// the vocabulary is new, we must create a new content type with term reference field
$vocabulary = new stdClass();
}
$vocabulary->name = $section->title;
if (function_exists('transliteration_get')) {
$section->title = transliteration_get($section->title, '?', language_default('language'));
}
$vocabulary->machine_name = drupal_strtolower(str_replace(' ', '_', $section->title));
$vocabulary->description = $section->description;
$vocabulary->weight = $section->ordering;
//check vocabulary name to be unique
$duplicate = db_query_range("SELECT name FROM {taxonomy_vocabulary} WHERE machine_name = :machine_name", 0, 1, array(
':machine_name' => $vocabulary->machine_name,
))
->fetchField();
if (!empty($duplicate) && !$vid) {
//the vocabulary machine_name already exists
continue;
}
$res = FALSE;
$res = taxonomy_vocabulary_save($vocabulary);
if (!$vid) {
// 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);
}
switch ($res) {
case SAVED_NEW:
$context['results']['sections_new']++;
break;
case SAVED_UPDATED:
$context['results']['sections_updated']++;
break;
default:
$context['results']['sections_failed']++;
break;
}
// Hook to allow other modules to modify the vocabulary
module_invoke_all('joomla', 'taxonomy_vocabulary', $vocabulary, $section);
$context['message'] = t('Now processing %section', array(
'%section' => $vocabulary->name,
));
}
if ($sections_finished) {
$cat_offset =& $context['sandbox']['categories_offset'];
db_set_active('joomla');
$categories = db_select('categories', 'c')
->fields('c', array(
'id',
'title',
'description',
'section',
'ordering',
))
->range($cat_offset, 10)
->execute()
->fetchAll();
db_set_active();
//Process Joomla categories -> Drupal terms
foreach ($categories as $num => $category) {
$context['results']['categories_total']++;
$context['sandbox']['progress']++;
// We have some sections that are non-integers. Ignore those.
if ($category->section <= 0) {
continue;
}
$term_map = db_query("SELECT tid,jsectionid FROM {joomla_categories} WHERE jcategoryid = :jcategoryid", array(
':jcategoryid' => $category->id,
))
->fetchField();
if ($term_map && !$joomla_update_duplicate) {
continue;
}
$term = NULL;
if ($term_map) {
$term = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(
':tid' => $term_map,
))
->fetch();
if (!$term) {
drupal_set_message(t('Unable to load term id @id', array(
'@id' => $term_map->tid,
)), 'error');
$context['results']['categories_failed']++;
continue;
}
}
else {
$term = new stdClass();
}
$term->name = $category->title;
$term->description = $category->description;
$term->weight = $category->ordering;
$term->vid = db_query('SELECT vid FROM {joomla_sections} WHERE jsectionid = :section', array(
':section' => $category->section,
))
->fetchField();
$res = FALSE;
$res = taxonomy_term_save($term);
if ($term_map) {
// 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 {
// 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);
}
switch ($res) {
case SAVED_NEW:
$context['results']['categories_new']++;
break;
case SAVED_UPDATED:
$context['results']['categories_updated']++;
break;
default:
$context['results']['categories_failed']++;
break;
}
// Hook to allow other modules to modify the term
module_invoke_all('joomla', 'term', $term, $category);
$context['message'] = t('Now processing %term', array(
'%term' => $term->name,
));
}
$cat_offset += 10;
}
$offset += 10;
}