<?php
// $Id: image_attach.module,v 1.2.2.3 2006/10/25 14:38:06 walkah Exp $

/**
 * @file image_attach.module
 */

/**
 * Implementation of hook_help().
 */
function image_attach_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      // This description is shown in the listing at admin/modules.
      return t('Allows easy attaching of image nodes to other content types.');
  }
}

/**
 * Implementation of hook_settings()
 */
function image_attach_settings() {
  $form = array();
  
  $form['image_attach_existing'] = array(
    '#type' => 'radios',
    '#title' => t('Attach existing images.'),
    '#default_value' => variable_get('image_attach_existing', 1),
    '#options' => array(0 => 'Disabled', 1 => 'Enabled'),
    '#description' => t('When enabled, will allow existing image nodes to be attached instead of uploading new images.')
  );
  
  return $form;
}

/**
 * implementation of hook_form_alter()
 */
function image_attach_form_alter($form_id, &$form) {
  if (!isset($form['type']) || $form['type'] == 'image') {
    return;
  }
  // Make a copy of the type to shorten up the code
  $type =  $form['type']['#value'];
  $enabled = variable_get('image_attach_'. $type, 0);
  switch ($form_id) {
     
    // checkbox in the node's content type configuration page.
    case $type .'_node_settings':
      if(function_exists('_image_check_settings')){
          _image_check_settings();
        $form['workflow']['image_attach_'. $type] = array(
          '#type' => 'radios',
          '#title' => t('Attach Images'),
          '#default_value' => $enabled,
          '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
          '#description' => t('Should this node allows users to upload an image?'),
        );
      } else {
        drupal_set_message(t('The image module is not installed. The image_attach module will not function without it.'), 'error');
      }
      break;
   
    // if enabled adjust the form
    case $type .'_node_form':
      if ($enabled == 1 &&  function_exists('_image_check_settings')) {
        _image_check_settings();
        $node = $form['#node'];
        $existing = variable_get('image_attach_existing', 1);
        $value = ($node->new_image) ? '#value' : '#default_value';
        $form['#attributes'] = array("enctype" => "multipart/form-data");

        $form['image_attach'] = array(
          '#type' => 'fieldset', 
          '#title' => t('Attached Images'), 
          '#collapsible' => TRUE, 
          '#collapsed' => !$node->iid
        );
        if ($node->iid) {
          $image = node_load($node->iid);
          $form['image_attach']['image_thumbnail'] = array(
            '#type' => 'item', 
            '#title' => t('Thumbnail'), 
            '#value' => image_display($image, 'thumbnail')
          );
        }
        if ($existing && user_access('access content')) {         
          $form['image_attach']['iid'] = array(
            '#type' => 'select', 
            '#title' => t('Existing Image'), 
            '#options' => _image_attach_get_image_nodes(),
            $value => $node->iid,
            '#description' => t('Choose an image already existing on the server if you do not upload a new one.')
            );
          $form['image_attach'][] = array(
            '#type' => 'item',
            '#value' => t('-or-'),
            '#attributes' => array('class' => 'either-choice')
	        );
        }
        else {
          $form['image_attach']['iid'] = array(
            '#type' => 'hidden',
            '#value' => $node->iid
            );
        }
        $form['image_attach']['image'] = array(
          '#type' => 'file',
          '#title' => t('Upload Image')
          );
        $form['image_attach']['image_title'] = array(
          '#type' => 'textfield', 
          '#title' => t('Image title'),
          $value => '',
          '#description' => t('The title the image will be shown with.')
        );        
      }
      break;
  }
}

/**
 * Implementation of hook_menu()
 */
function image_attach_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'image_attach',
      'title' => t('Image Attachment View'),
      'callback' => 'image_attach_view_image',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
* Implementation of hook_nodeapi().
*/
function image_attach_nodeapi(&$node, $op, $teaser, $page) {
  if(variable_get('image_attach_'. $node->type, 0) == 0){
      return;
  }
  switch ($op) {
    case 'prepare':
      $image->title = $_POST['edit']['image_title'];
      $image->uid = $node->uid;
      $image->name = $node->name;
      $image->created = $node->created;
      $image->type = 'image';
      image_prepare($image, 'image');
      if ($image->images) {
        node_validate($image);
        if (!form_get_errors()) {
          $image = node_submit($image);
          node_save($image);
          $node->iid = $image->nid;
          $node->new_image = TRUE;
        }
      }
      elseif (variable_get('image_attach_existing', 1) && $_POST['edit']['iid']) {
        $node->iid = $_POST['edit']['iid'];
      }
      break;
    case 'insert':
    case 'update':
      if ($node->iid) {
        db_query("DELETE FROM {image_attach} WHERE nid=%d", $node->nid);
        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 && function_exists('image_display')){
        $node->body = theme('image_attach_body', $node);
        if ($teaser){
          $node->teaser = theme('image_attach_teaser', $node);
        }
      }
  }
}

/**
 * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
 */
function _image_attach_get_image_nodes() {
  $rows = array(0 => '');
  $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.status=1 AND type='image' ORDER BY n.sticky DESC, n.title ASC"));
  if (db_num_rows($result) == 0) {
      return array();
  }
  
  while ($node = db_fetch_object($result)) {
      $rows[$node->nid] = $node->title;
  }  
  return $rows;
}

/**
 * Theme the teaser.
 *
 * Override this in template.php to include a case statement if you want different node types to appear differently.
 * If you have additional image sizes you defined in image.module, you can use them by theming this function as well.
 */
function theme_image_attach_teaser($node){
  theme_add_style(drupal_get_path('module', 'image_attach') .'/image_attach.css');

  $image = node_load($node->iid);
  
  $info = image_get_info(file_create_path($image->images['thumbnail']));
  $output = '';
  $output .= '<div style="width: '. $info['width'] .'px" class="image-attach-teaser">';
  $output .= l(image_display($image, 'thumbnail'), "node/$node->nid", array(), NULL, NULL, FALSE, TRUE);
  $output .= '</div>'."\n";
  $output .= $node->teaser;
  return $output;
}

/**
 * Theme the body
 */
function theme_image_attach_body($node){
  theme_add_style(drupal_get_path('module', 'image_attach') .'/image_attach.css');

  $image = node_load($node->iid);
  
  $info = image_get_info(file_create_path($image->images['thumbnail']));
  $output = '';
  $output .= '<div style="width: '. $info['width'] .'px" class="image-attach-body">';
  $output .= l(image_display($image, 'thumbnail'), "node/$node->nid", array(), NULL, NULL, FALSE, TRUE);
  $output .= '</div>'."\n";
  $output .= $node->body;
  return $output;
}

