You are here

function joomla_comment_save in Joomla to Drupal 7.2

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

File

./joomla.batch.inc, line 456

Code

function joomla_comment_save(&$context) {
  $joomla_update_duplicate = $context['sandbox']['joomla_update_duplicate'];
  $offset =& $context['sandbox']['comments_offset'];
  db_set_active('joomla');
  $jcomments = db_select('jcomments', 'jc')
    ->fields('jc')
    ->orderBy('jc.id', 'ASC')
    ->range($offset, 10)
    ->execute()
    ->fetchAll();
  db_set_active();
  foreach ($jcomments as $num => $jcomment) {
    $context['sandbox']['progress']++;
    $context['results']['comments_total']++;
    db_set_active();
    $cid = db_query("SELECT cid FROM {joomla_comments} WHERE jcommentid = :jcid", array(
      ':jcid' => $jcomment->id,
    ))
      ->fetchField();

    // Check if the comment has selected to update previously imported comments
    if ($cid && !$joomla_update_duplicate) {
      continue;
    }
    $comment = new stdClass();

    // Set cid if we are updating an existing record
    if ($cid) {
      $comment->cid = $cid;
    }
    $nid = db_query('SELECT nid FROM {joomla_content} WHERE jcontentid = :jcontentid', array(
      ':jcontentid' => $jcomment->object_id,
    ))
      ->fetchField();
    $uid = db_query('SELECT uid FROM {joomla_users} WHERE juid = :juid', array(
      ':juid' => $jcomment->userid,
    ))
      ->fetchField();
    if (!$nid) {
      $context['results']['comments_failed']++;
      continue;
    }
    $pid = 0;
    if ($jcomment->parent) {
      $pid = db_query('SELECT cid FROM {joomla_comments} WHERE jcommentid = :jparent', array(
        ':jparent' => $jcomment->parent,
      ))
        ->fetchField();
    }
    $comment->pid = $pid;
    $comment->nid = $nid;
    $comment->uid = $uid ? $uid : 0;
    $comment->subject = isset($jcomment->title) && $jcomment->title ? $jcomment->title : truncate_utf8(trim(decode_entities(strip_tags($jcomment->comment))), 29, TRUE);
    $comment->hostname = $jcomment->ip;
    $comment->created = strtotime($jcomment->date);
    $comment->changed = $comment->created;
    $comment->status = $jcomment->published;
    $comment->name = $jcomment->name;
    $comment->mail = valid_email_address($jcomment->email) ? $jcomment->email : NULL;
    $comment->homepage = $jcomment->homepage;
    $comment->language = LANGUAGE_NONE;

    // no negative values
    $comment->created = $comment->created < 0 ? NULL : $comment->created;
    $comment->comment_body[LANGUAGE_NONE][0]['value'] = $jcomment->comment;
    $comment->comment_body[LANGUAGE_NONE][0]['format'] = variable_get('joomla_input_format', JOOMLA_INPUT_FORMAT);
    comment_save($comment);
    if ($comment->cid) {

      // Write into the joomla -> drupal comment mapping table
      $joomla_comment = new stdClass();
      $joomla_comment->cid = $comment->cid;
      $joomla_comment->jcommentid = $jcomment->id;
      if ($cid) {
        drupal_write_record('joomla_comments', $joomla_comment, 'cid');
      }
      else {
        drupal_write_record('joomla_comments', $joomla_comment);
      }
    }
    if ($cid && $comment->cid) {
      $context['results']['comments_updated']++;
    }
    elseif (!$cid && $comment->cid) {
      $context['results']['comments_new']++;
    }
    else {
      $context['results']['comments_failed']++;
    }

    // Hook to allow other modules to modify the term
    module_invoke_all('joomla', 'jcomment', $comment, $jcomment);
    $context['message'] = t('Now processing JComment: %comment', array(
      '%comment' => $comment->subject,
    ));
  }
  $offset += 10;
}