You are here

function image_attach_nodeapi in Image 6

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

Implementation of hook_nodeapi().

File

contrib/image_attach/image_attach.module, line 465
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 'insert':
    case 'update':
      db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
      if (!empty($node->iids)) {
        if (!empty($node->image_thumbnail)) {

          // Form submitted, order iids according to drag-to-reorder table.
          $node->iids = _image_attach_set_image_weight($node->image_thumbnail, $node->iids);
        }
        $weight = 0;
        foreach ($node->iids as $iid) {
          db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight++);
        }
      }
      break;
    case 'delete':
      db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
      break;
    case 'load':
      $res = db_query("SELECT iid FROM {image_attach} WHERE nid = %d ORDER BY weight", $node->nid);
      $iids = array();
      while ($iid = db_fetch_array($res)) {
        $iids[] = $iid['iid'];
      }
      return array(
        'iids' => $iids,
      );

    // Pass the body and teaser objects to the theme again to add the images.
    case 'view':
      if (!empty($node->iids)) {
        $node->image_attach = _image_attach_node_load_attached($node->iids, $teaser);
        $teaser_or_body = $teaser ? 'teaser' : 'body';
        $img_size = variable_get('image_attach_size_' . $teaser_or_body . '_' . $node->type, IMAGE_THUMBNAIL);

        // Don't show anything if the attached images are set to hidden.
        if ($img_size == IMAGE_ATTACH_HIDDEN) {
          return;
        }

        // Set weight, either from CCK or our own settings. Cribbed from signup!
        if (module_exists('content')) {

          // Due to a bug in CCK (http://drupal.org/node/363456), we need
          // to call this function twice to ensure we get the real value.
          // The bug is present when you first enable the setting to
          // display in the node instead of a separate tab, or when you
          // first upgrade to the version that contains this code.
          content_extra_field_weight($node->type, 'image_attach');
          $weight = content_extra_field_weight($node->type, 'image_attach');
        }
        else {
          $weight = variable_get("image_attach_weight_{$teaser_or_body}_{$node->type}", 0);
        }
        $node->content['image_attach'] = array(
          '#weight' => $weight,
          '#value' => theme('image_attach_attached_images_node', $node->nid, $node->image_attach, $img_size, $teaser),
        );
      }
      break;
    case 'rss item':
      $ret = array();
      if (!empty($node->iids) && ($image = node_load($node->iids[0]))) {
        $info = image_get_info(file_create_path($image->images[IMAGE_PREVIEW]));
        $ret[] = array(
          'key' => 'enclosure',
          'attributes' => array(
            'url' => url("image/view/{$node->iids[0]}/" . IMAGE_PREVIEW, array(
              'absolute' => TRUE,
            )),
            'length' => $info['file_size'],
            'type' => $info['mime_type'],
          ),
        );
      }
      return $ret;
  }
}