View source
<?php
function image_fupload_imagefield_help($path, $arg) {
}
function image_fupload_imagefield_menu() {
$items['fupload/js/imagefield'] = array(
'title' => 'Image FUpload',
'page callback' => 'image_fupload_imagefield_node_create',
'access arguments' => array(
'mass upload images',
),
'type' => MENU_CALLBACK,
'file' => 'field_file.inc',
'file path' => drupal_get_path('module', 'filefield'),
);
return $items;
}
function image_fupload_imagefield_theme() {
return array(
'image_fupload_imagefield_widget' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'image_fupload_imagefield_widget.inc',
),
'image_fupload_imagefield_item' => array(
'arguments' => array(
'item' => NULL,
),
),
'image_fupload_imagefield_widget_preview' => array(
'arguments' => array(
'item' => NULL,
),
),
'image_fupload_imagefield_widget_item' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
function image_fupload_imagefield_form_alter(&$form, $form_state, $form_id) {
global $user;
$image_node_types = variable_get('image_node_types', array());
$node_type = drupal_substr($form_id, 0, drupal_strlen($form_id) - 10);
if (image_node_type_load($node_type, TRUE)) {
$field_name = $image_node_types[$node_type]['fieldname'];
$field = content_fields($field_name, $node_type);
if ($field['widget']['type'] == "image_fupload_imagefield_widget" && arg(3) != "noflash" && arg(3) != "list_imagefields" && user_access('mass upload images')) {
$module_path = drupal_get_path('module', 'image_fupload');
drupal_add_js($module_path . '/swfupload/swfupload.js', 'module');
drupal_add_js($module_path . '/swfupload/swfupload.queue.js', 'module');
drupal_add_js($module_path . '/swfupload/fileprogress.js', 'module');
drupal_add_js($module_path . '/swfupload/handlers.js', 'module');
$redirect_url = '';
if (user_access('edit captions')) {
$redirect_url = url('node/add/' . str_replace("_", "-", $node_type) . '/list_imagefields');
}
switch ($field['multiple']) {
case "1":
$upload_limit_count = 100;
break;
case "0":
$upload_limit_count = 1;
break;
default:
$upload_limit_count = $field['multiple'];
}
if ($field['widget']['fupload_mode'] == "single") {
$upload_limit_count = 100;
}
$fupload_widget_weight = $field['widget']['weight'];
$file_max_size = parse_size($field['widget']['max_filesize_per_file']);
$file_extensions = !empty($field['widget']['file_extensions']) ? "*." . str_replace(" ", "; *.", $field['widget']['file_extensions']) : '*.jpg; *.jpeg; *.png; *.gif';
drupal_add_js(theme('swfupload_settings', base_path() . $module_path, url('fupload/flash'), round($file_max_size / 1024), "*." . str_replace(" ", "; *.", $field['widget']['file_extensions'] . ' ' . strtoupper($field['widget']['file_extensions'])), !empty($user->sid) ? $user->sid : session_id(), $upload_limit_count, $node_type, $field_name, !empty($field['required']) ? "true" : "false", $field['widget']['fupload_mode'], $redirect_url), 'inline');
drupal_add_css($module_path . '/image_fupload-style.css', 'module', 'all', FALSE);
$form['node_type'] = array(
'#type' => 'value',
'#value' => $node_type,
);
$form['field_name'] = array(
'#type' => 'value',
'#value' => $field_name,
);
if (!isset($form['#node']->nid)) {
unset($form[$field_name], $form['#field_info'][$field_name], $form['#validate'][array_search('filefield_node_form_validate', $form['#validate'])]);
$form['buttons']['submit']['#attributes'] = array(
'style' => 'display:none;',
);
if ($field['widget']['fupload_mode'] == "single") {
if (user_access('edit captions')) {
form_fields_destroy($form, $field['widget']['fupload_previewlist_field_settings']);
form_set_cache($form['form_build_id'], $form, $form_state);
unset($form['buttons']['preview']);
}
else {
unset($form['title']);
form_set_cache($form['form_build_id'], $form, $form_state);
unset($form['buttons']['preview']);
}
}
else {
if (user_access('edit captions')) {
form_fields_destroy($form, $field['widget']['fupload_previewlist_field_settings']);
form_set_cache($form['form_build_id'], $form, $form_state);
unset($form['buttons']['preview']);
}
else {
form_set_cache($form['form_build_id'], $form, $form_state);
unset($form['buttons']['preview']);
}
}
}
else {
}
if (isset($form['#node']->nid) && $field['widget']['fupload_mode'] == "single") {
}
else {
$form['fupload'] = array(
'#weight' => $fupload_widget_weight - 0.1,
);
$form['fupload']['message'] = array(
'#value' => '<div id="jsstatus"></div>',
'#prefix' => '<div id="uploadform" style="visibility:hidden;"></div>',
'#weight' => -7,
);
$form['fupload']['image'] = array(
'#type' => 'fieldset',
'#title' => t('Images'),
'#value' => '<span style="font-style:italic;">' . t('Click the "Select Images" icon on the left below to begin.') . '</span>',
'#weight' => -6,
'#attributes' => array(
'class' => 'flash',
'id' => 'fsUploadProgress',
),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['fupload']['upload_info'] = array(
'#value' => t('0 files uploaded.') . ' ',
'#prefix' => '<div id="divStatus">',
'#suffix' => '</div>',
'#weight' => -5,
);
$form['fupload']['upload_buttons'] = array(
'#prefix' => '<div>',
'#value' => '<span id="spanUploadButton"></span> <input id="btnCancel" type="button" value="' . t('Cancel All Uploads') . '" onclick="swfu.cancelQueue();" disabled="disabled" /> ',
'#suffix' => '</div>',
'#weight' => -4,
);
$form['fupload']['upload_buttons']['node_create'] = array(
'#type' => 'submit',
'#value' => t('Process queued images'),
'#weight' => -3,
'#ahah' => array(
'path' => 'fupload/js/imagefield',
'event' => 'click',
'method' => 'replace',
'wrapper' => 'jsstatus',
'progress' => array(
'type' => 'bar',
'message' => t('Images in queue are processed...'),
),
),
);
$form['fupload']['upload_buttons']['delete_queue'] = array(
'#type' => 'submit',
'#value' => t('Delete queued images'),
'#weight' => -2,
'#ahah' => array(
'path' => 'fupload/js/deletequeue/' . $field_name,
'event' => 'click',
'method' => 'append',
'wrapper' => 'jsstatus',
),
);
if (user_access('edit captions') && !isset($form['#node']->nid)) {
$form['fupload']['upload_buttons']['next_step'] = array(
'#value' => '<input type="button" value="' . t('Next step') . '" id="imagepreviewlistbutton" style="visibility:hidden" />',
'#weight' => -1,
);
}
if (isset($form['#node']->nid)) {
$form['fupload']['upload_info']['#value'] .= t('Queued files will be uploaded by clicking the "Upload images" button.');
$form['fupload']['upload_buttons']['submit'] = array(
'#value' => '<input type="button" value="' . t('Upload images') . '" id="startuploadbutton" onclick="startUploadProcess();window.location.href=\'#uploadform\'" />',
'#weight' => -2,
);
}
else {
$form['fupload']['upload_info']['#value'] .= t('Queued files will be uploaded by clicking the "Save" button at the bottom of this page.');
$form['buttons']['fupload_submit'] = array(
'#value' => '<input type="button" value="' . t('Save') . '" id="startuploadbutton" onclick="startUploadProcess();window.location.href=\'#uploadform\'" />',
'#weight' => 19,
'#submit' => array(
'node_form_submit',
),
);
}
}
}
}
}
function image_fupload_imagefield_node_create() {
global $user;
$form_build_id = $_POST['form_build_id'];
$form_id = $_POST['form_id'];
if (isset($form_build_id) && isset($form_id)) {
$form_error = 0;
$message = '';
$redirect_url = '';
$form_state = array(
'rebuild' => TRUE,
'values' => $_POST,
);
if (!($form = form_get_cache($form_build_id, $form_state))) {
form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
$output = theme('status_messages');
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
exit;
}
$field_name = $form['field_name']['#value'];
$node_type = $form['node_type']['#value'];
$field = content_fields($field_name, $node_type);
module_load_include('inc', 'node', 'node.pages');
db_query("UPDATE {files} SET filename = '%s' WHERE uid = %d AND status = %d AND filename = '%s'", image_fupload_image_status($field_name, IMAGE_PROCESSED), $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status($field_name, IMAGE_HALFPROCESSED));
$form['#post'] = $_POST;
$form['#post']['form_id'] = $form['form_id']['#value'];
if (!isset($form['title'])) {
$form['#post']['title'] = 'Image';
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'title2change',
'#default_value' => '',
);
}
if (isset($form['nid']['#value'])) {
$form['gallery_nid']['#value'] = $form['nid']['#value'];
unset($form['nid']['#value'], $form_state['values']['nid']);
}
drupal_process_form($form_id, $form, $form_state);
if (!form_get_errors()) {
$imagefield_files = array();
$result = db_query_range("SELECT * FROM {files} WHERE uid = %d AND status = %d AND filename = '%s'", $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status($field_name, IMAGE_UNMACHINED), 0, 3);
while ($image = db_fetch_object($result)) {
$img_cache[] = $image;
db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d", image_fupload_image_status($field_name, IMAGE_HALFPROCESSED), $image->fid);
if ($form['title']['#title'] == "title2change") {
$form['title']['#value'] = theme('fupload_create_filename', $image, $field['widget']['fupload_title_replacements']);
$form_state['values']['title'] = $form['title']['#value'];
}
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$files_path = _fupload_import_widget_files_directory($field);
if ($field['widget']['fupload_mode'] == "single") {
if ($file = field_file_save_file($image->filepath, $validators, $files_path)) {
$node = node_form_submit_build_node($form, $form_state);
$insert = empty($node->nid);
$node->{$field_name} = array(
0 => $file,
);
node_save($node);
$node_link = l(t('view'), 'node/' . $node->nid);
$watchdog_args = array(
'@type' => $node->type,
'%title' => $node->title,
);
$t_args = array(
'@type' => node_get_types('name', $node),
'%title' => $node->title,
);
if ($insert) {
watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been created.', $t_args));
}
else {
watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been updated.', $t_args));
}
if ($node->nid) {
$form_state['nid'] = $node->nid;
db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d", image_fupload_image_status($field_name, IMAGE_PROCESSED), $image->fid);
if (user_access('edit captions')) {
db_query("INSERT INTO {fupload_previewlist} (fieldname, uid, nid, fid, created) VALUES ('%s', %d, %d, %d, %d)", $field_name, $user->uid, $node->nid, $file['fid'], time());
}
}
else {
drupal_set_message(t('The post could not be saved.'), 'error');
}
}
}
else {
if (!isset($form['gallery_nid']['#value'])) {
$node = _fupload_node_save($form, $form_state, $form_build_id);
}
else {
$node->nid = $form['gallery_nid']['#value'];
$form['nid']['#value'] = $node->nid;
$form_state['values']['nid'] = $node->nid;
}
$validators['image_fupload_validate_total_filesize'] = array(
$node->nid,
$field_name,
parse_size($field['widget']['max_filesize_per_node']),
);
if ($file = field_file_save_file($image->filepath, $validators, $files_path)) {
$imagefield_files[] = $file;
drupal_set_message(t('Image %title added.', array(
'%title' => $file['filename'],
)));
db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d", image_fupload_image_status($field_name, IMAGE_PROCESSED), $image->fid);
if (user_access('edit captions')) {
db_query("INSERT INTO {fupload_previewlist} (fieldname, uid, nid, fid, created) VALUES ('%s', %d, %d, %d, %d)", $field_name, $user->uid, $node->nid, $file['fid'], time());
}
}
else {
$form_error = 2;
}
}
}
if (count($imagefield_files)) {
$node = node_load($node->nid);
$node->{$field_name} = array_merge($node->{$field_name}, $imagefield_files);
node_save($node);
}
if (isset($form['gallery_nid']['#value']) && !user_access('edit captions')) {
$redirect_url = 'node/' . $form['gallery_nid']['#value'];
}
if (!empty($field['widget']['fupload_previewlist_redirecturl']) && !user_access('edit captions')) {
$redirect_url = $field['widget']['fupload_previewlist_redirecturl'];
}
if (!empty($redirect_url) && $form_error == 0) {
$message .= '<input type="hidden" name="redirect_url" id="redirect_url" value="' . url($redirect_url) . '" />';
}
$result = db_fetch_object(db_query("SELECT COUNT(*) AS img_count FROM {files} WHERE uid = %d AND status = %d AND filename = '%s'", $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status($field_name, IMAGE_UNMACHINED)));
$message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="' . $result->img_count . '" />';
}
else {
if ($form_error != 2) {
$form_error = 1;
}
$message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="0" />';
}
$message .= '<input type="hidden" name="form_errors" id="form_errors" value="' . $form_error . '" />';
$message .= theme('status_messages');
drupal_json(array(
'status' => TRUE,
'data' => $message,
));
}
else {
drupal_json(array(
'status' => FALSE,
'data' => t('Error: No or wrong POST Data'),
));
}
}
function image_fupload_imagefield_elements() {
$elements = array();
$elements_imagefield = module_invoke('imagefield', 'elements');
$elements['image_fupload_imagefield_widget'] = $elements_imagefield['imagefield_widget'];
return $elements;
}
function image_fupload_imagefield_widget_info() {
return array(
'image_fupload_imagefield_widget' => array(
'label' => t('Image FUpload'),
'field types' => array(
'image',
'filefield',
),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_CUSTOM,
),
'description' => t('An edit widget for image files being uploaded via Image FUpload, including a preview of the image and editing options.'),
),
);
}
function image_fupload_imagefield_widget_settings($op, $widget) {
module_load_include('inc', 'image_fupload_imagefield', 'image_fupload_imagefield_widget');
switch ($op) {
case 'form':
return image_fupload_imagefield_widget_settings_form($widget);
case 'validate':
return image_fupload_imagefield_widget_settings_validate($widget);
case 'save':
return image_fupload_imagefield_widget_settings_save($widget);
}
}
function image_fupload_imagefield_widget($form, $form_state, $field, $items, $delta = 0) {
$element = call_user_func('imagefield_widget', &$form, &$form_state, $field, $items, $delta);
return $element;
}
function image_fupload_imagefield_content_fieldapi($op, $field, $new_instance = NULL) {
$image_node_types = variable_get('image_node_types', array());
switch ($op) {
case "delete instance":
if ($image_node_types[$field['type_name']]['fieldname'] == $field['field_name']) {
_fupload_imagepreview_settings('delete', $field['type_name']);
}
break;
}
}
function form_fields_destroy(&$form, $fields_to_remove = array()) {
$fields = array();
if (count($fields_to_remove)) {
foreach ($fields_to_remove as $key) {
$elements = explode("_", $key, 2);
if (!isset($fields[$elements[0]]) && $fields_to_remove[$key] != FALSE) {
$fields[$elements[0]] = array();
}
if ($fields_to_remove[$key] != FALSE) {
array_push($fields[$elements[0]], $elements[1]);
}
}
}
if (isset($fields['node'])) {
if (in_array("title", $fields['node'])) {
unset($form['title']);
}
if (in_array("description", $fields['node'])) {
unset($form['body_field']);
}
}
if (isset($fields['taxonomy'])) {
for ($i = 0; $i < count($fields['taxonomy']); $i++) {
unset($form['taxonomy'][$fields['taxonomy'][$i]]);
unset($form['taxonomy']['tags'][$fields['taxonomy'][$i]]);
if (!count($form['taxonomy']['tags'])) {
unset($form['taxonomy']['tags']);
}
}
if (count(taxonomy_get_vocabularies($form['#node']->type)) == count($fields['taxonomy'])) {
unset($form['taxonomy']);
}
}
if (isset($fields['cck'])) {
for ($i = 0; $i < count($fields['cck']); $i++) {
unset($form[$fields['cck'][$i]]);
unset($form['#field_info'][$fields['cck'][$i]]);
}
if (!count($form['#field_info'])) {
unset($form['#field_info']);
}
}
}
function theme_image_fupload_imagefield_item($item) {
return theme('imagefield_item', $item);
}
function theme_image_fupload_imagefield_widget_preview($item = NULL) {
return theme('imagefield_widget_preview', $item);
}
function theme_image_fupload_imagefield_widget_item($element) {
return theme('imagefield_widget_item', $element);
}
function _fupload_import_widget_files_directory($field) {
$widget_file_path = $field['widget']['file_path'];
if (module_exists('token')) {
global $user;
$widget_file_path = token_replace($widget_file_path, 'user', $user);
}
$dir = file_directory_path() . '/' . $widget_file_path;
if (file_check_directory($dir, TRUE)) {
return $dir;
}
return file_directory_path();
}
function _fupload_node_save(&$form, $form_state, $form_build_id) {
$node = node_form_submit_build_node($form, $form_state);
$insert = empty($node->nid);
node_save($node);
$node_link = l(t('view'), 'node/' . $node->nid);
$watchdog_args = array(
'@type' => $node->type,
'%title' => $node->title,
);
$t_args = array(
'@type' => node_get_types('name', $node),
'%title' => $node->title,
);
if ($insert) {
watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been created.', $t_args));
}
if ($node->nid) {
$form['gallery_nid']['#value'] = $node->nid;
$form_state['values']['gallery_nid'] = $node->nid;
form_set_cache($form_build_id, $form, $form_state);
return $node;
}
else {
drupal_set_message(t('The post could not be saved.'), 'error');
return FALSE;
}
}
function image_fupload_validate_total_filesize($file, $nid, $field_name, $node_size = 0) {
global $user;
$errors = array();
$total_size = 0;
if ($node_size && $user->uid != 1) {
$result = db_query("SELECT f.filesize FROM {content_%s} AS t JOIN {files} AS f ON t.%s_fid = f.fid WHERE t.nid = %d", $field_name, $field_name, $nid);
while ($image = db_fetch_object($result)) {
$total_size += $image->filesize;
}
if ($total_size + $file->filesize > $node_size) {
$errors[] = t('The new total filesize of this node, %tsize, would exceed field settings of %msize.', array(
'%tsize' => format_size($total_size + $file->filesize),
'%msize' => format_size($node_size),
));
}
}
return $errors;
}