Index: image_attach.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_attach/image_attach.module,v
retrieving revision 1.44
diff -u -p -r1.44 image_attach.module
--- image_attach.module 27 Dec 2008 06:58:41 -0000 1.44
+++ image_attach.module 6 Jan 2009 22:25:08 -0000
@@ -1,5 +1,5 @@
iid) && $node->iid) {
- $image = node_load($node->iid);
- if (node_access('view', $image)) {
- $img = image_display($image, variable_get('image_attach_block_0_size', IMAGE_THUMBNAIL));
- return array(
- 'subject' => t('Attached images'),
- 'content' => l($img, "node/$node->iid", array('html' => TRUE)),
- );
+ $output['subject'] = t('Attached images');
+ foreach ($node->iid as $this_image) {
+ $image = node_load($this_image);
+ if (node_access('view', $image)) {
+ $img = image_display($image, variable_get('image_attach_block_0_size', IMAGE_THUMBNAIL));
+ $content .= '
'.l($img, "node/$this_image", array('html' => TRUE)).'
';
+ }
}
+ $output['content'] = ''.$content.'
';
+ return $output;
}
}
}
@@ -191,12 +193,14 @@ function image_attach_form_alter(&$form,
$form['#validate'][] = 'image_attach_validate';
if (!empty($node->iid)) {
- $image = node_load($node->iid);
- $form['image_attach']['image_thumbnail'] = array(
- '#type' => 'item',
- '#title' => t('Thumbnail'),
- '#value' => image_display($image, 'thumbnail')
- );
+ foreach ($node->iid as $iid) {
+ $image = node_load($iid);
+ $form['image_attach']['image_thumbnail'][] = array(
+ '#type' => 'item',
+ '#title' => t('Thumbnail'),
+ '#value' => image_display($image, 'thumbnail')
+ );
+ }
}
if (variable_get('image_attach_existing', 1) && user_access('access content')) {
$form['image_attach']['iid'] = array(
@@ -204,7 +208,9 @@ function image_attach_form_alter(&$form,
'#title' => t('Existing image'),
'#options' => _image_attach_get_image_nodes(),
$value => empty($node->iid) ? NULL : $node->iid,
- '#description' => t('Choose an image already existing on the server if you do not upload a new one.')
+ '#description' => t('Choose an image already existing on the server if you do not upload a new one.'),
+ '#multiple' => TRUE,
+ '#size' => 6
);
$form['image_attach'][] = array(
'#type' => 'item',
@@ -213,10 +219,12 @@ function image_attach_form_alter(&$form,
);
}
else {
- $form['image_attach']['iid'] = array(
- '#type' => 'hidden',
- $value => empty($node->iid) ? NULL : $node->iid,
- );
+ foreach ($node->iid as $iid) {
+ $form['image_attach']['iid'][] = array(
+ '#type' => 'hidden',
+ $value => empty($iid) ? NULL : $iid,
+ );
+ }
}
$form['image_attach']['image'] = array(
'#type' => 'file',
@@ -228,17 +236,53 @@ function image_attach_form_alter(&$form,
$value => '',
'#description' => t('The title the image will be shown with.')
);
+ //additional submit button which adds image and brings you back to node edit
+ $form['image_attach']['image_attach_multiple'] = array(
+ '#type' => 'submit',
+ '#value' => t('Attach'),
+ '#submit' => array('image_attach_image_add_submit'),
+ '#validate' => array('image_attach_validate'),
+ );
}
}
}
/**
+ * This submit function adds a new image and returns you to the
+ * node edit form directly afterwards, without creating the new node yet
+ */
+function image_attach_image_add_submit(&$form, &$form_state) {
+ // Rebuild the attached image data
+ if (isset($form_state['values']['iid'])) {
+ db_query("DELETE FROM {image_attach} WHERE nid=%d", $form['nid']['#value']);
+ if (count($form_state['values']['iid'])) {
+ $form_state['values']['iid'] = is_array($form_state['values']['iid']) ? $form_state['values']['iid'] : array($form_state['values']['iid']);
+ foreach ($form_state['values']['iid'] as $iid) {
+ db_query("INSERT INTO {image_attach} (nid, iid) VALUES (%d, %d)", $form['nid']['#value'], $iid);
+ }
+ }
+ }
+
+ // Convert taxonomy format from Preview to Object.
+ if (module_exists('taxonomy') && !empty($form_state['values']['taxonomy'])) {
+ $tnode = new stdClass();
+ $tnode->taxonomy = $form_state['values']['taxonomy'];
+ $form_state['values']['taxonomy'] = taxonomy_preview_terms($tnode);
+ unset($tnode);
+ }
+
+ // Rebuild the node edit form
+ node_form_submit_build_node($form, $form_state);
+
+}
+
+/**
* Capture node form submission and immediately create an image if one has been
* uploaded.
* Note that the new image nodes are created even on preview. Taking several
* attempts may create trash.
*/
-function image_attach_validate($form, &$form_state) {
+function image_attach_validate(&$form, &$form_state) {
$validators = array(
'file_validate_is_image' => array(),
);
@@ -248,7 +292,8 @@ function image_attach_validate($form, &$
$image = image_create_node_from($file->filepath, $image_title, '');
if ($image && !form_get_errors()) {
drupal_set_message(t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/'. $image->nid) )));
- form_set_value($form['image_attach']['iid'], $image->nid, $form_state);
+ // This line changed to append to array of images instead of reset the single value
+ $form_state['values']['iid'][$image->nid] = $image->nid;
}
}
}
@@ -273,8 +318,11 @@ function image_attach_nodeapi(&$node, $o
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);
+ if (count($node->iid)) {
+ $node->iid = is_array($node->iid) ? $node->iid : array($node->iid);
+ foreach ($node->iid as $iid) {
+ db_query("INSERT INTO {image_attach} (nid, iid) VALUES (%d, %d)", $node->nid, $iid);
+ }
}
}
break;
@@ -284,28 +332,39 @@ function image_attach_nodeapi(&$node, $o
break;
case 'load':
- $iid = db_result(db_query("SELECT iid FROM {image_attach} WHERE nid=%d", $node->nid));
- return array('iid' => $iid);
+ $res = (db_query("SELECT iid FROM {image_attach} WHERE nid=%d", $node->nid));
+ $iids = array();
+ while($iid = db_fetch_array($res) ){
+ $iids[] = $iid['iid'];
+ }
+ return array('iid' => $iids);
// Pass the body and teaser objects to the theme again to add the images
case 'view':
- if ($node->iid) {
+ if (count($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),
- );
+ $node->content['image_attach'] = array();
+ $node->content['image_attach']['#weight'] = variable_get("image_attach_weight_{$teaser_or_body}_{$node->type}", 0);
+ if ($teaser_or_body == 'teaser') {
+ $node->content['image_attach'][$node->iid[0]]['#value'] = theme("image_attach_{$teaser_or_body}", $node, $node->iid[0]);
+ $node->content['image_attach'][$node->iid[0]]['#weight'] = variable_get("image_attach_weight_{$teaser_or_body}_{$node->type}", 0);
+ } else {
+ foreach ($node->iid as $iid) {
+ $content .= theme("image_attach_{$teaser_or_body}", $node, $iid);
+ }
+ $node->content['image_attach']['#value'] = ''.$content.'
';
+ }
}
break;
case 'rss item':
$ret = array();
- if ($node->iid && $image = node_load($node->iid)) {
+ if (count($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, array('absolute' => TRUE)),
+ 'url' => url("image/view/{$node->iid[0]}/". IMAGE_PREVIEW, array('absolute' => TRUE)),
'length' => $info['file_size'],
'type' => $info['mime_type'],
)
@@ -372,55 +431,55 @@ function image_attach_theme() {
}
/**
- * Theme the teaser.
+ * Theme an image shown in 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) {
+function theme_image_attach_teaser($node, $iid) {
+ $output = '';
$img_size = variable_get('image_attach_size_teaser_'. $node->type, 'thumbnail');
if ($img_size != IMAGE_ATTACH_HIDDEN) {
drupal_add_css(drupal_get_path('module', 'image_attach') .'/image_attach.css');
-
- $image = node_load($node->iid);
+
+ $image = node_load($iid);
if (!node_access('view', $image)) {
// If the image is restricted, don't show it as an attachment.
- return NULL;
+ return $output;
}
$info = image_get_info(file_create_path($image->images[$img_size]));
- $output = '';
- $output .= l(image_display($image, $img_size), "node/$node->nid", array('html' => TRUE));
+ $output .= '
';
+ $output .= l(image_display($image, $img_size), "node/$iid", array('html' => TRUE));
$output .= '
'."\n";
-
- return $output;
}
+ return $output;
}
/**
- * Theme the body
+ * Theme an image shown in body
*/
-function theme_image_attach_body($node) {
+function theme_image_attach_body($node, $iid) {
+ $output = '';
$img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_THUMBNAIL);
if ($img_size != IMAGE_ATTACH_HIDDEN) {
drupal_add_css(drupal_get_path('module', 'image_attach') .'/image_attach.css');
- $image = node_load($node->iid);
- if (!node_access('view', $image)) {
- // If the image is restricted, don't show it as an attachment.
- return NULL;
- }
- $published = $image->status ? '' : ' image-unpublished';
- $info = image_get_info(file_create_path($image->images[$img_size]));
-
- $output = '
';
- $output .= l(image_display($image, $img_size), "node/$node->iid", array('html' => TRUE));
- $output .= '
'."\n";
+ $image = node_load($iid);
+ if (!node_access('view', $image)) {
+ // If the image is restricted, don't show it as an attachment.
+ return NULL;
+ }
+ $published = $image->status ? '' : ' image-unpublished';
+ $info = image_get_info(file_create_path($image->images[$img_size]));
- return $output;
+ $output = '
';
+ $output .= l(image_display($image, $img_size), "node/$iid", array('html' => TRUE));
+ $output .= '
'."\n";
}
+ return $output;
}