You are here

function _video_image_presave in Video 6.2

Same name and namespace in other branches
  1. 6 plugins/video_image/video_image.module \_video_image_presave()
1 call to _video_image_presave()
video_image_nodeapi in plugins/video_image/video_image.module
Implementation of hook_nodeapi()

File

plugins/video_image/video_image.module, line 296
Enable image support for video module.

Code

function _video_image_presave(&$node) {

  //print_r($node);

  //exit;

  // ############# PREPARE #######################
  // ------------- MOVED -------------------------
  if (count($_POST)) {
    $validators = array(
      'file_validate_is_image' => array(),
    );
    $field_name = file_save_upload('image', $validators);
    if (!$field_name && video_image_is_autothumbable($node)) {
      _video_image_thumbnail_debug(t('video_image_nodeapi: prepare: ready to thumbnail video'));
      $field_name = module_invoke('video_' . $node->vtype, 'v_auto_thumbnail', $node);

      //      echo 'ok';
      //      print_r($field_name);
      //      exit;
      if ($field_name === false && count($_POST)) {
        drupal_set_message(t('The thumbnailing process of your video failed for some reason. Video thumbnail will not be available.'), 'error');
      }
    }
  }
  if ($field_name) {

    /*
          if($node->iid){

          // Remove all the existing images.
          $result = db_query("SELECT f.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->iid);
          while ($file = db_fetch_object($result)) {
     file_delete(file_create_path($file->filepath));
     db_query("DELETE FROM {files} WHERE fid = %d", $file->fid);
          }
          //db_query("DELETE FROM {image} WHERE nid = %d", $node->iid);
          }
    *
    */
    $node->tempimage = _video_image_temp_image_store($field_name);
    $node->new_image = TRUE;

    //    print_r($node);
    //    die;
  }
  else {
    if (is_array($_POST['tempimage']) && ($_POST['op'] == 'Preview' || $_POST['op'] == 'Submit')) {

      //print_r($node);

      //exit;
      $node->tempimage = (array) _video_image_temp_image_load(array_values($_POST['tempimage']['fids']));
    }
  }

  // ###########################################################################
  if ($node->regenerate_thumbnail) {
    _video_image_regenerate_thumbnail($node);
  }

  //if (is_array($node->tempimage['fids']) && $node->tempimage['fids']['_original']) {
  if (is_array($node->tempimage['fids']) && $node->tempimage['fids']['thumbnail']) {
    $image = _video_image_temp_image_load(array_values($node->tempimage['fids']));

    //print_r($image);

    //exit;

    //GMM: This DELETE query causes the uploaded video file to disappear

    //     especially when using ffmpeg to convert the video - no need to kill the
    //     tempimage, since its status is 0 in {files} Drupal cron will nix it

    //db_query("DELETE FROM {files} WHERE fid in (%s)",

    //        implode(',', array_values($node->tempimage['fids'])));
    // initialize standard node fields
    //    print_r($image);
    //    exit;
    $image->uid = $node->uid;
    $image->created = time();
    $image->title = t('Video thumbnail for !title', array(
      '!title' => $node->title,
    ));
    $image = node_submit($image);
    $image->uid = $node->uid;
    $image->status = _video_image_publish_thumbnails();
    $image->promote = _video_image_promote_thumbnails();

    // This is a messages hack (we don't want to see what happens under the covers)
    _video_image_pause_messages(true);
    if ($node->iid) {
      $oldimage = node_load($node->iid);
      $oldimage->images = $image->images;

      // delete the old images?

      //GMM: Fix to use the proper variable to trigger current Image module to rebuild derivative images

      //     Tested against Image 6.x-1.0-aplha4
      //  $oldimage->new_image = 1;
      $oldimage->new_file = true;
      node_save($oldimage);
      $node->iid = $oldimage->nid;
      $node->serial_data['iid'] = $node->iid;
    }
    else {
      node_save($image);

      //print_r($image);

      //exit;

      //image_insert($image);
      $node->iid = $image->nid;

      // store the iid into the serial_data
      $node->serial_data['iid'] = $node->iid;

      //print_r($node);

      //die;

      // needed to set the correct status and promote values even if the user does not have enough permissions. Is there a better solution???
      // db_query('UPDATE {node} SET status = %d, promote = %d WHERE nid = %d AND vid = %d', _video_image_publish_thumbnails(), _video_image_promote_thumbnails(), $image->nid, $image->vid);
    }
    _video_image_pause_messages();
  }
  else {
    $node->serial_data['iid'] = $node->iid;
  }
}