When I update rotor item nodes using views bulk operations the rotor image gets lost.

In my particular case I was using the 'Modify node taxonomy terms' bulk operation.

I'm not sure if this is related to #931836: Images disapear but it could possibly be.

Comments

agileware’s picture

Title: Updating a rotor item node with views bulk operations causes the rotor image to be lost » Saving a rotor item node programmatically causes the rotor image to be lost
Priority: Normal » Major

The problem would appear to be occurring whenever a rotor item node is saved programmatically (ie. not via the node edit form).
- Changing status too as it now affects many more use cases than just views bulk operations.

It is because of this:

<?php
function _rotor_save(&$node) {
  if (!$node->nid) return;
  $fid = 0;
  if (is_object($node->rotor_image)) {
    $fid = upload_element_save($node->rotor_image, 'rotor', FILE_EXISTS_RENAME);
  }
  $exists = db_result(db_query("SELECT 1 FROM {rotor_item} WHERE nid = %d", $node->nid));
  if($exists)
    db_query("UPDATE {rotor_item} SET fid=%d, alt_text='%s', image_title='%s', url='%s', link_target='%s' WHERE nid = %d",
     $fid, $node->alt_text, $node->image_title, $node->url, $node->link_target, $node->nid);
  else
    db_query(
      "INSERT INTO {rotor_item} (nid, fid, alt_text, image_title, url, link_target)
      VALUES (%d, %d, '%s', '%s', '%s', '%s')"
      ,$node->nid, $fid, $node->alt_text, $node->image_title, $node->url, $node->link_target);
}
?>

Because it only checks is_object($node->rotor_image) and $node->rotor_image is not going to exist unless you are using the node form.
Then $exists will be TRUE because there is a row for that node, so it will save the fid as 0.

It should just user $node->fid if it exists and $node->rotor_image is not there.

agileware’s picture

Combined with #932878: Node revisions don't work this poses a fairly significant problem as the revisions aren't there to save you so you have to go to backups.

mrfelton’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

agileware’s picture

Status: Closed (fixed) » Active

Took me a while to get back to sorry.

I just had a quick look at the commit and there seems to be an issue with the new code.
I can't do a patch right now but I can do one later.

<?php
function _rotor_save(&$node) {
  if (!$node->nid) {
    return;
  }
  if (is_object($node->rotor_image)) {
    $fid = upload_element_save($node->rotor_image, 'rotor', FILE_EXISTS_RENAME);
  }
  else {
    $fid = db_result(db_query("SELECT fid FROM {rotor_item} WHERE nid = %d", $node->nid));
  }
  if (isset($fid)) {
    db_query("UPDATE {rotor_item} SET fid=%d, alt_text='%s', image_title='%s', url='%s', link_target='%s' WHERE nid = %d",
     $fid, $node->alt_text, $node->image_title, $node->url, $node->link_target, $node->nid);
  }
  else {
    db_query(
      "INSERT INTO {rotor_item} (nid, fid, alt_text, image_title, url, link_target)
      VALUES (%d, %d, '%s', '%s', '%s', '%s')"
      ,$node->nid, $fid, $node->alt_text, $node->image_title, $node->url, $node->link_target);
  }
}
?>

The problem is in that function if $fid is set it does an update, else an insert but fid will always be set, so when creating new rotor items it tries to do an update on a row that doesn't exist.

This was caused by

$exists = db_result(db_query("SELECT 1 FROM {rotor_item} WHERE nid = %d", $node->nid));
if($exists)

being replaced with

if (isset($fid)) {
agileware’s picture

Version: 6.x-2.7 » 6.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new1.37 KB

Here is a fix.

mrfelton’s picture

Status: Needs review » Fixed

Fixed in git. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.