You are here

function joomla_content_save in Joomla to Drupal 7.2

1 call to joomla_content_save()
joomla_batch_save in ./joomla.batch.inc

File

./joomla.batch.inc, line 321

Code

function joomla_content_save(&$context) {
  $joomla_update_duplicate = $context['sandbox']['joomla_update_duplicate'];
  $images =& $context['sandbox']['images'];
  $offset =& $context['sandbox']['content_offset'];
  db_set_active('joomla');
  $q = db_select('content', 'cs');
  $q
    ->leftJoin('content_frontpage', 'cf', 'cf.content_id = cs.id');
  $q
    ->fields('cs')
    ->fields('cf', array(
    'content_id',
  ))
    ->range($offset, 10);
  $content = $q
    ->execute()
    ->fetchAll();
  db_set_active();
  foreach ($content as $num => $data_joomla) {
    $context['results']['content_total']++;
    $context['sandbox']['progress']++;
    $content_map = db_query('SELECT n.nid, jcontentid, changed FROM {joomla_content} jc JOIN {node} n ON n.nid = jc.nid WHERE jc.jcontentid = :contentid', array(
      ':contentid' => $data_joomla->id,
    ))
      ->fetch();
    if ($content_map && !$joomla_update_duplicate) {

      // Content item has already been imported and update is off
      continue;
    }

    /**
     * If the content item already exists, but has not been updated
     * since the last import, skip it
     */
    $joomla_changed = strtotime($data_joomla->modified);

    //if changed is negative drupal will throw an error so:
    if ($joomla_changed < 0) {
      $joomla_changed = 0;
    }
    if ($content_map && $joomla_changed == $content_map->changed) {
      continue;
    }
    $node = new stdClass();
    if ($content_map) {
      $node->nid = $content_map->nid;
      $node = node_load($node->nid);
      $node->revision = 1;
      $node->log = 'This node was programmatically updated at ' . format_date(REQUEST_TIME, 'short', NULL, variable_get('joomla_default_language', LANGUAGE_NONE));
    }
    else {
      $node->is_new = TRUE;
      $node->log = 'Initial creation from Joomla module';
    }
    $author_uid = db_query('SELECT uid FROM {joomla_users} WHERE juid = :juid', array(
      ':juid' => $data_joomla->created_by,
    ))
      ->fetch();
    if ($author_uid) {
      $node->uid = intval($author_uid->uid);
    }
    $node->title = $data_joomla->title;
    $node->status = (bool) $data_joomla->state;
    $node->created = strtotime($data_joomla->created);

    //if created is negative drupal will throw an error so:
    if ($node->created < 0) {
      $node->created = 0;
    }
    $node->language = variable_get('joomla_default_language', LANGUAGE_NONE);
    $node->changed = $joomla_changed;

    // Set content type
    if ($data_joomla->sectionid == 0) {
      $joomla_type = variable_get('joomla_default_static_nodetype', JOOMLA_DEFAULT_STATIC_NODETYPE);
    }
    else {
      $joomla_type = variable_get('joomla_default_blog_nodetype', JOOMLA_DEFAULT_BLOG_NODETYPE);
    }
    $node->type = $joomla_type;
    if (module_exists('comment')) {
      $node->comment = variable_get('comment_' . $node->type, COMMENT_NODE_OPEN);
    }
    if (!empty($data_joomla->introtext)) {
      $joomla_body = $data_joomla->introtext . "<!--break-->" . $data_joomla->fulltext;
      $joomla_teaser = $data_joomla->introtext;
    }
    else {
      $joomla_body = $data_joomla->fulltext;
      $joomla_teaser = text_summary($joomla_body);
    }
    $joomla_body = str_replace("{mospagebreak}", "", $joomla_body);

    //images
    if ($data_joomla->images) {
      $joomla_teaser = joomla_replace_mos_image($data_joomla->images, $joomla_teaser);
      $joomla_body = joomla_replace_mos_image($data_joomla->images, $joomla_body);
    }
    $joomla_teaser = joomla_replace_image_link($joomla_teaser);
    $joomla_body = joomla_replace_image_link($joomla_body);
    $lang = field_language('node', $node, NULL, $node->language);
    $node->body[$lang['body']][0]['summary'] = $joomla_teaser;
    $node->body[$lang['body']][0]['value'] = $joomla_body;
    $node->body[$lang['body']][0]['format'] = variable_get('joomla_input_format', JOOMLA_INPUT_FORMAT);
    $tid = db_query('SELECT tid FROM {joomla_categories} WHERE jcategoryid = :jcategoryid AND jsectionid = :jsectionid', array(
      ':jcategoryid' => $data_joomla->catid,
      ':jsectionid' => $data_joomla->sectionid,
    ))
      ->fetchField();
    if ($tid) {
      $vid = db_query('SELECT vid FROM {taxonomy_term_data} WHERE tid = :tid', array(
        ':tid' => $tid,
      ))
        ->fetchField();
      $vocabulary = taxonomy_vocabulary_load($vid);
      $field_name = variable_get('joomla_field_' . $vocabulary->machine_name, FALSE);
      if ($field_name) {
        $node->{$field_name}[$lang[$field_name]][]['tid'] = $tid;
      }
    }

    // Promote to front page?
    if ($data_joomla->content_id) {
      $joomla_promote = 1;
    }
    else {
      $joomla_promote = 0;
    }
    $node->promote = $joomla_promote;
    node_save($node);
    if (!$content_map) {

      // Created new node, update joomla table
      $joomla_content = new stdClass();
      $joomla_content->nid = $node->nid;
      $joomla_content->jcontentid = $data_joomla->id;
      drupal_write_record('joomla_content', $joomla_content);
    }
    if (!$content_map && $node->nid) {
      $context['results']['content_new']++;
    }
    elseif ($content_map && $node->nid) {
      $context['results']['content_updated']++;
    }
    else {
      $context['results']['content_failed']++;
    }

    // Hook to allow other modules to modify the node
    module_invoke_all('joomla', 'node', $node, $data_joomla);
    $context['message'] = t('Now processing %node', array(
      '%node' => $data_joomla->title,
    ));
  }
  $offset += 10;
}