Index: image_attach.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_attach/image_attach.module,v retrieving revision 1.4 diff -u -p -r1.4 image_attach.module --- image_attach.module 23 Aug 2006 22:21:11 -0000 1.4 +++ image_attach.module 20 Oct 2006 17:04:04 -0000 @@ -17,6 +17,23 @@ function image_attach_help($section) { } /** + * Implementation of hook_settings() + */ +function image_attach_settings() { + $form = array(); + + $form['existingImageAttachDisabled'] = array( + '#type' => 'radios', + '#title' => t('Existing Image Attachments'), + '#default_value' => variable_get('existingImageAttachDisabled', 0), + '#options' => array(0 => 'Enabled', 1 => 'Disabled'), + '#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) { @@ -47,25 +64,102 @@ function image_attach_form_alter($form_i // if enabled adjust the form case $type .'_node_form': if ($enabled == 1 && function_exists('_image_check_settings')) { + $existing = !variable_get('existingImageAttachDisabled', 0); _image_check_settings(); $node = $form['#node']; $form['#attributes'] = array("enctype" => "multipart/form-data"); - $form['image_attach'] = array('#type' => 'fieldset', '#title' => t('Attached Images'), '#collapsible' => TRUE); + $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')); + $form['image_attach']['image_thumbnail'] = array( + '#type' => 'item', + '#title' => t('Thumbnail'), + '#value' => image_display($image, 'thumbnail') + ); } - $value = ($node->new_image) ? '#value' : '#default_value'; - $form['image_attach']['iid'] = array('#type' => 'hidden' , $value => $node->iid); - $form['image_attach']['image'] = array('#type' => 'file', '#title' => t('Image')); - $form['image_attach']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => $image->title); + $form['image_attach']['image'] = array( + '#type' => 'file', + '#title' => t('Upload Image') + ); + if (module_exist('image_gallery')) { + $form['image_attach']['image_gallery'] = taxonomy_form(_image_gallery_get_vid()); + $form['image_attach']['image_gallery']['#weight'] = 0; // Override weight to keep it here + $form['image_attach']['image_gallery']['#description'] = t('Choose an image gallery to place the new image in.'); + } + $form['image_attach']['image_title'] = array( + '#type' => 'textfield', + '#title' => t('Image title'), + '#default_value' => '', + '#description' => t('The title the image will be shown with.') + ); + if ($existing && user_access('access content')) { + $form['image_attach'][] = array( + '#type' => 'item', + '#value' => t('-or-'), + '#attributes' => array('class' => 'either-choice') + ); + $form['image_attach']['iid'] = array( + '#type' => 'select', + '#title' => t('Existing Image'), + '#options' => _image_attach_get_image_nodes(), + '#default_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( + '#value' => << + +EOD + ); + } } 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) { @@ -79,17 +173,23 @@ function image_attach_nodeapi(&$node, $o $image->name = $node->name; $image->created = $node->created; $image->type = 'image'; + $gallery = taxonomy_get_term($_POST['edit']['image_gallery']); + if ($gallery) + $image->taxonomy = array($gallery->tid => $gallery); + else + $image->taxonomy = array(); image_prepare($image, 'image'); if ($image->images) { node_validate($image); if (!form_get_errors()) { $image = node_submit($image); node_save($image); + taxonomy_node_save($image->nid, $image->taxonomy); $node->iid = $image->nid; $node->new_image = TRUE; } } - elseif ($_POST['edit']['iid']) { + elseif (!variable_get('existingImageAttachDisabled', 0) && $_POST['edit']['iid']) { $node->iid = $_POST['edit']['iid']; } break; @@ -119,6 +219,55 @@ function image_attach_nodeapi(&$node, $o } /** + * 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 => ''); + if (module_exist('image_gallery')) { + $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n LEFT JOIN {term_node} t ON t.nid=n.nid WHERE n.status=1 AND n.type='image' AND t.tid IS NULL ORDER BY n.sticky DESC, n.title ASC")); + while ($node = db_fetch_object($result)) { + $rows[$node->nid] = $node->title; + } + + $tree = taxonomy_get_tree(_image_gallery_get_vid()); + foreach ($tree as $gallery) { + $prefix = str_repeat('--', $gallery->depth+1).' '; + $rows[$prefix.$gallery->name] = array(); + + $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {term_node} t INNER JOIN {node} n ON t.nid=n.nid WHERE n.status=1 AND n.type='image' AND t.tid=%d ORDER BY n.sticky DESC, n.title ASC"), $gallery->tid); + while ($image = db_fetch_object($result)) { + $rows[$prefix.$gallery->name][$image->nid] = $image->title; + } + } + } else { + $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.status=1 AND type='image' ORDER BY n.stickty 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; +} + +function image_attach_view_image($nid=null) { + if (empty($nid)) { + header('HTTP/1.1 404 Image Not Found'); + exit; + } + $node = node_load($nid); + if (!$node) { + header('HTTP/1.1 404 Image Not Found'); + exit; + } + + print image_display($node, 'thumbnail'); + exit; +} + +/** * Theme the teaser. * * Override this in template.php to include a case statement if you want different node types to appear differently.