View source
<?php
function fupload_list_images_imagefield(&$form_state, $node_type) {
global $user;
$count = 0;
$node_type = str_replace("-", "_", $node_type);
$image_node_types = variable_get('image_node_types', array());
$field_name = $image_node_types[$node_type]['fieldname'];
$fields = content_types($node_type);
$field_image = $fields['fields'][$field_name];
$fields_preview_list = array(
'node' => array(),
'imagefield' => array(),
'taxonomy' => array(),
'cck' => array(),
);
foreach ($field_image['widget']['fupload_previewlist_field_settings'] as $key) {
$elements = explode("_", $key, 2);
if ($field_image['widget']['fupload_previewlist_field_settings'][$key] != FALSE) {
array_push($fields_preview_list[$elements[0]], $elements[1]);
}
}
$token_installed = module_exists("token");
$form_taxonomy = array();
if (module_exists("taxonomy") && count($fields_preview_list['taxonomy'])) {
$form_taxonomy = array(
'type' => array(
'#value' => $node_type,
),
'#node' => (object) array(
'type' => $node_type,
),
);
taxonomy_form_alter($form_taxonomy, array(), $node_type . '_node_form');
foreach ($form_taxonomy['taxonomy'] as $key => $value) {
if (!in_array($key, $fields_preview_list['taxonomy']) && is_numeric($key)) {
unset($form_taxonomy['taxonomy'][$key]);
}
}
if (is_array($form_taxonomy['taxonomy']['tags'])) {
foreach ($form_taxonomy['taxonomy']['tags'] as $key => $value) {
if (!in_array($key, $fields_preview_list['taxonomy'])) {
unset($form_taxonomy['taxonomy']['tags'][$key]);
}
}
}
}
$result = db_query("SELECT p.nid, p.fid, f.filename, f.filepath FROM {fupload_previewlist} AS p JOIN {files} AS f ON p.fid = f.fid WHERE p.fieldname = '%s' AND p.uid = %d ORDER BY p.fid ASC", $field_name, $user->uid);
while ($image = db_fetch_object($result)) {
$count += 1;
$image_items[$image->fid] = $image->nid;
if ($field_image['widget']['fupload_mode'] == "single") {
$node = db_query("SELECT title, body FROM {node_revisions} WHERE nid = %d", $image->nid);
$node = db_fetch_object($node);
}
$form[$image->fid] = array(
'#type' => 'fieldset',
'#title' => t('Image @count', array(
'@count' => $count,
)),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form[$image->fid]['preview_image'] = array(
'#prefix' => '<div class="image_fupload_preview">',
'#value' => theme('imagecache', $image_node_types[$node_type]['imagecache_preset'], $image->filepath, check_plain($image->filename), check_plain($image->filename), $field_image['widget']['fupload_previewlist_img_attributes']),
'#suffix' => '</div>',
'#weight' => -16,
);
if (in_array("title", $fields_preview_list['node'])) {
$form[$image->fid]['node_title_' . $image->fid] = array(
'#type' => 'textfield',
'#title' => check_plain($fields['title_label']),
'#default_value' => isset($form_state['values']['node_title_' . $image->fid]) ? $form_state['values']['node_title_' . $image->fid] : $node->title,
'#size' => 60,
'#required' => TRUE,
'#disabled' => FALSE,
'#weight' => $fields['extra']['title']['weight'],
);
}
if (in_array("title", $fields_preview_list['imagefield']) && $field_image['widget']['custom_title']) {
if ($token_installed && !empty($field_image['widget']['title'])) {
$default_value = token_replace($field_image['widget']['title']);
}
else {
if (!empty($field_image['widget']['title'])) {
$default_value = $field_image['widget']['title'];
}
else {
$default_value = $image->filename;
}
}
$form[$image->fid]['imagefield_title_' . $image->fid] = array(
'#type' => 'textfield',
'#title' => $field_image['widget']['label'] . ' ' . t('title'),
'#default_value' => isset($form_state['values']['imagefield_title_' . $image->fid]) ? $form_state['values']['imagefield_title_' . $image->fid] : $default_value,
'#size' => 60,
'#required' => FALSE,
'#disabled' => FALSE,
'#weight' => $field_image['widget']['weight'],
);
}
if (in_array("alt", $fields_preview_list['imagefield']) && $field_image['widget']['custom_alt']) {
if ($token_installed && !empty($field_image['widget']['alt'])) {
$default_value = token_replace($field_image['widget']['alt']);
}
else {
if (!empty($field_image['widget']['alt'])) {
$default_value = $field_image['widget']['alt'];
}
else {
$default_value = $image->filename;
}
}
$form[$image->fid]['imagefield_alt_' . $image->fid] = array(
'#type' => 'textfield',
'#title' => $field_image['widget']['label'] . ' ' . t('alt'),
'#default_value' => isset($form_state['values']['imagefield_alt_' . $image->fid]) ? $form_state['values']['imagefield_alt_' . $image->fid] : $default_value,
'#size' => 60,
'#required' => FALSE,
'#disabled' => FALSE,
'#weight' => $field_image['widget']['weight'] + 0.3,
);
}
if (in_array("description", $fields_preview_list['imagefield'])) {
$form[$image->fid]['imagefield_description_' . $image->fid] = array(
'#type' => 'textfield',
'#title' => $field_image['widget']['label'] . ' ' . t('description'),
'#default_value' => $form_state['values']['imagefield_description_' . $image->fid],
'#size' => 60,
'#required' => FALSE,
'#disabled' => FALSE,
'#weight' => $field_image['widget']['weight'] + 0.5,
);
}
if (in_array("description", $fields_preview_list['node'])) {
$form[$image->fid]['node_body_' . $image->fid] = array(
'#type' => 'textarea',
'#title' => check_plain($fields['extra']['body_field']['label']),
'#default_value' => isset($form_state['values']['node_body_' . $image->fid]) ? $form_state['values']['node_body_' . $image->fid] : '',
'#rows' => 5,
'#disabled' => FALSE,
'#weight' => $fields['extra']['body_field']['weight'],
);
$form[$image->fid]['format_' . $image->fid] = filter_form(FILTER_FORMAT_DEFAULT, $fields['extra']['body_field']['weight'], array(
'format_' . $image->fid,
));
$form[$image->fid]['format_' . $image->fid]['#weight'] = $fields['extra']['body_field']['weight'] + 0.1;
}
if (count($form_taxonomy)) {
$form_taxonomy['taxonomy']['#weight'] = $fields['extra']['taxonomy']['weight'];
$form[$image->fid]['taxonomy_' . $image->fid] = $form_taxonomy['taxonomy'];
}
}
if ($count) {
$form['text'] = array(
'#value' => t('In this step, you can edit the captions of all uploaded images. To complete this task, click the button "Done Editing" at the bottom of this page.'),
'#weight' => -19,
);
$form['node_type'] = array(
'#type' => 'value',
'#value' => $node_type,
);
$form['field_name'] = array(
'#type' => 'value',
'#value' => $field_name,
);
$form['storage_mode'] = array(
'#type' => 'value',
'#value' => $field_image['widget']['fupload_mode'],
);
$form['image_items'] = array(
'#type' => 'value',
'#value' => $image_items,
);
$form['#validate'][] = 'fupload_list_images_imagefield_validate';
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Done Editing'),
);
if ($field_image['widget']['fupload_previewlist_redirecturl']) {
$form['#redirect'] = $field_image['widget']['fupload_previewlist_redirecturl'];
}
else {
if ($field_image['widget']['fupload_mode'] == "multiple") {
$form['#redirect'] = 'node/' . array_pop($image_items);
}
else {
$form['#redirect'] = 'node/add/' . str_replace("_", "-", $node_type);
}
}
}
else {
$form['text'] = array(
'#value' => t('No images yet in queue.'),
'#weight' => -19,
);
drupal_set_message(t('No images have been found in queue, probably no images have been uploaded yet. Please return to !upload_page if you want to upload some images.', array(
'!upload_page' => l(t('image upload page'), 'node/add/' . str_replace("_", "-", $node_type)),
)), 'warning');
}
return $form;
}
function fupload_list_images_imagefield_validate($form, &$form_state) {
$i = 0;
$storage_mode = $form_state['values']['storage_mode'];
$type = node_get_types('type', $form_state['values']['node_type']);
foreach ($form_state['values']['image_items'] as $fid => $nid) {
$i++;
if ($storage_mode == "multiple" && empty($node) || $storage_mode == "single") {
$node = node_load($nid);
}
$node->taxonomy = $form_state['values']['taxonomy_' . $fid];
if (!empty($type->min_word_count) && isset($form_state['values']['node_body_' . $fid]) && count(explode(' ', $form_state['values']['node_body_' . $fid])) < $type->min_word_count) {
form_set_error('node_body_' . $fid, t('The field %body_label of the @img_number. image is too short. You need at least %words words.', array(
'%body_label' => $type->body_label,
'@img_number' => $i,
'%words' => $type->min_word_count,
)));
}
unset($node->body);
node_validate($node);
}
}
function fupload_list_images_imagefield_submit($form, &$form_state) {
global $user;
$imagefield_fields = array(
"title",
"description",
"alt",
);
$storage_mode = $form_state['values']['storage_mode'];
foreach ($form_state['values']['image_items'] as $fid => $nid) {
if ($storage_mode == "multiple") {
if (!isset($node)) {
$node = node_load($nid);
}
$image_field = $node->{$form_state}['values']['field_name'];
for ($x = 0; $x < count($image_field); $x++) {
if ($image_field[$x]['fid'] == $fid) {
foreach ($imagefield_fields as $key => $value) {
if (isset($form_state['values']['imagefield_' . $value . '_' . $fid])) {
$image_field[$x]['data'][$value] = $form_state['values']['imagefield_' . $value . '_' . $fid];
}
}
break;
}
}
$node->{$form_state}['values']['field_name'] = $image_field;
}
if ($storage_mode == "single") {
$node = node_load($nid);
if (isset($form_state['values']['node_title_' . $fid])) {
$node->title = !empty($form_state['values']['node_title_' . $fid]) ? $form_state['values']['node_title_' . $fid] : $node->title;
}
if (isset($form_state['values']['node_body_' . $fid])) {
$node->body = !empty($form_state['values']['node_body_' . $fid]) ? $form_state['values']['node_body_' . $fid] : $node->body;
}
$node->teaser = node_teaser($node->body, $form_state['values']['format_' . $fid]);
if (isset($form_state['values']['format_' . $fid])) {
$node->format = isset($form_state['values']['format_' . $fid]) ? $form_state['values']['format_' . $fid] : $node->format;
}
if (isset($form_state['values']['taxonomy_' . $fid])) {
$node->taxonomy = !empty($node->taxonomy) ? array_merge($form_state['values']['taxonomy_' . $fid], $node->taxonomy) : $form_state['values']['taxonomy_' . $fid];
}
$image_field = $node->{$form_state}['values']['field_name'];
foreach ($imagefield_fields as $key => $value) {
if (isset($form_state['values']['imagefield_' . $value . '_' . $fid])) {
$image_field[0]['data'][$value] = $form_state['values']['imagefield_' . $value . '_' . $fid];
}
}
$node->{$form_state}['values']['field_name'] = $image_field;
node_save($node);
}
}
if ($storage_mode == "multiple") {
node_save($node);
}
if (!form_get_errors()) {
db_query("DELETE FROM {fupload_previewlist} WHERE fieldname = '%s' AND uid = %d", $form_state['values']['field_name'], $user->uid);
}
drupal_set_message(t('All images have been saved and published.'));
drupal_redirect_form($form);
}