You are here

function image_attach_nodeapi in Image 5.2

Same name and namespace in other branches
  1. 5 contrib/image_attach/image_attach.module \image_attach_nodeapi()
  2. 6 contrib/image_attach/image_attach.module \image_attach_nodeapi()

Implementation of hook_nodeapi().

File

contrib/image_attach/image_attach.module, line 234
image_attach.module

Code

function image_attach_nodeapi(&$node, $op, $teaser, $page) {

  // Make sure that if an image is deleted it is detached from any nodes.
  if ($node->type == 'image') {
    switch ($op) {
      case 'delete':
        db_query("DELETE FROM {image_attach} WHERE iid=%d", $node->nid);
    }
    return;
  }
  else {
    if (variable_get('image_attach_' . $node->type, 0) == 0) {
      return;
    }
  }
  switch ($op) {
    case 'prepare':
      if (file_check_upload('image')) {
        $image->title = $_POST['image_title'];
        $image->uid = $node->uid;
        $image->name = $node->name;
        $image->created = $node->created;
        $image->type = 'image';

        // Set the node's defaults... (copied this from node and comment.module)
        $node_options = variable_get('node_options_image', array(
          'status',
          'promote',
        ));
        $image->status = in_array('status', $node_options);
        $image->promote = in_array('promote', $node_options);
        if (module_exists('comment')) {
          $image->comment = variable_get('comment_image', COMMENT_NODE_READ_WRITE);
        }
        image_prepare($image, 'image');
        if ($image->images) {
          if (empty($image->title)) {
            $image->title = basename($image->images[IMAGE_ORIGINAL]);
          }
          node_validate($image);
          if (!form_get_errors()) {
            $image = node_submit($image);
            node_save($image);
            $node->iid = $image->nid;
            $node->new_image = TRUE;
          }
        }
      }
      elseif (!empty($_POST['iid'])) {
        $node->iid = (int) $_POST['iid'];
      }
      break;
    case 'insert':
    case 'update':
      if (isset($node->iid)) {
        db_query("DELETE FROM {image_attach} WHERE nid=%d", $node->nid);
        if ($node->iid > 0) {
          db_query("INSERT INTO {image_attach} (nid, iid) VALUES (%d, %d)", $node->nid, $node->iid);
        }
      }
      break;
    case 'delete':
      db_query("DELETE FROM {image_attach} WHERE nid=%d", $node->nid);
      break;
    case 'load':
      $iid = db_result(db_query("SELECT iid FROM {image_attach} WHERE nid=%d", $node->nid));
      return array(
        'iid' => $iid,
      );

    // Pass the body and teaser objects to the theme again to add the images
    case 'view':
      if ($node->iid) {
        $teaser_or_body = $teaser ? 'teaser' : 'body';
        $node->content['image_attach'] = array(
          '#value' => theme("image_attach_{$teaser_or_body}", $node),
          '#weight' => variable_get("image_attach_weight_{$teaser_or_body}_{$node->type}", 0),
        );
      }
      break;
    case 'rss item':
      $ret = array();
      if ($node->iid && ($image = node_load($node->iid))) {
        $info = image_get_info(file_create_path($image->images[IMAGE_PREVIEW]));
        $ret[] = array(
          'key' => 'enclosure',
          'attributes' => array(
            'url' => url("image/view/{$node->iid}/" . IMAGE_PREVIEW, NULL, NULL, TRUE),
            'length' => $info['file_size'],
            'type' => $info['mime_type'],
          ),
        );
      }
      return $ret;
  }
}