View source
<?php
define('ITU_CAROUSEL_VISIBLE_ITEMS', 5);
define('ITU_USE_INSERT_MODULE', 1);
function itweak_upload_init() {
drupal_add_css(drupal_get_path('module', 'itweak_upload') . '/itweak_upload.css');
$carousel_visible = ITU_CAROUSEL_VISIBLE_ITEMS;
if (module_exists('jcarousellite')) {
$selector = '.itu-attachment-thumbs-jcarousellite';
$jcarousellite_options = <<<END
// btnGo: ['.externalControl .1', '.externalControl .2', '.externalControl .3'],
auto: null, // auto: 800, speed: 1000,
speed: 400,
vertical: false,
circular: false,
visible: {<span class="php-variable">$carousel_visible</span>},
start: 0,
scroll: 1,
mouseWheel: true,
END;
$js = <<<END
\$(function() {
\$("{<span class="php-variable">$selector</span>}").each(function(index) {
l=\$(this);
p=l.parents('.itu-attachment-images');
if (!p[0].id) {p[0].id = 'itu-attachment-images-' + index;}
c=l.parents('.item-list')
c.jCarouselLite({
btnPrev: "#" + p[0].id + " .itu-attachment-jcarousellite-prev",
btnNext: "#" + p[0].id + " .itu-attachment-jcarousellite-next",
{<span class="php-variable">$jcarousellite_options</span>}
});
// Fix jCarousel Lite width bug:
ul=\$("ul",c);
h=\$.css(ul[0],'width');
h+=8;
ul.css('width',h+"px")
});
});
END;
jcarousellite_add($js);
}
if (module_exists('jcarousel')) {
$selector = '.itu-attachment-thumbs-jcarousel';
$options = array(
'scroll' => 1,
);
$skin = 0 ? 'ie7' : 'tango';
$skin_path = '';
jcarousel_add($selector, $options, $skin, $skin_path);
}
}
function UNUSED_itweak_upload_perm() {
$perms[] = 'administer itweak_upload';
return $perms;
}
function itweak_upload_menu() {
$items = array();
$items['ajax/itu/progress'] = array(
'page callback' => 'itweak_upload_progress',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function _itweak_upload_isimage($file) {
if (strpos($file->filemime, 'image/') !== 0) {
return FALSE;
}
$image = image_get_info(file_create_path($file->filepath));
return is_array($image) && $image['extension'] != '';
}
function itweak_upload_imagecache_default_presets() {
$presets = array();
$presets['AttachmentThumbnail'] = array(
'presetname' => 'AttachmentThumbnail',
'actions' => array(
0 => array(
'weight' => '0',
'module' => 'imagecache',
'action' => 'imagecache_scale_and_crop',
'data' => array(
'width' => 60,
'height' => 60,
'upscale' => 1,
),
),
),
);
return $presets;
}
function _itweak_upload_file_create_url($file) {
if (function_exists('_private_upload_create_url')) {
$href = _private_upload_create_url($file);
}
else {
$alias = module_exists('filefield_paths') ? db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", 'filefield_paths/alias/' . $file->fid)) : false;
$href = $alias ? $alias : file_create_url($file->filepath);
}
return $href;
}
function itweak_upload_itweak_upload_preview($file, $derivative, $show_title = FALSE, $show_caption = FALSE, $options = NULL) {
if (_itweak_upload_isimage($file)) {
$text = empty($file->description) ? $file->filename : $file->description;
$href = _itweak_upload_file_create_url($file);
if ($derivative == '_none') {
return;
}
elseif ($derivative == '_original') {
$thumbnail = theme('image', file_create_path($file->filepath), $text, $text);
}
else {
$thumbnail = theme('imagecache', $derivative, file_create_path($file->filepath), $text, $text);
}
$text = check_plain($text);
$title_text = $show_title ? $text : NULL;
$caption_text = $show_caption ? $text : NULL;
return array(
'#type' => 'item',
'#value' => theme('itweak_upload_thumbnail', $thumbnail, $href, $title_text, $caption_text, $options),
);
}
}
function _itweak_upload_files_thumbnails(&$files, $vid, $cid, $node_type, $group) {
$derivative = _itweak_upload_get_derivative('upload', $node_type);
$show_title = _itweak_upload_get_setting('thumbnail_title', '', $node_type, 1);
foreach (element_children($files) as $fid) {
$file = new stdClass();
$file->fid = $files[$fid]['fid']['#value'];
$file->filename = $files[$fid]['filename']['#value'];
$file->filepath = $files[$fid]['filepath']['#value'];
$file->filemime = $files[$fid]['filemime']['#value'];
$file->filesize = $files[$fid]['filesize']['#value'];
$file->vid = $vid;
$file->cid = $cid;
$file->description = $files[$fid]['description']['#default_value'];
$file->list = $files[$fid]['list']['#default_value'];
$options = _itweak_upload_get_link_options($file, 'upload', $node_type, $group);
$preview = module_invoke_all('itweak_upload_preview', $file, $derivative, $show_title, FALSE, $options);
if ($preview) {
$files[$fid]['preview'] = $preview;
}
}
}
function _itweak_upload_upload_form_prerender($form) {
$node_type = $form['#node']->type;
if (isset($form['#node']->itu_comment)) {
$node_type_name = t('comment');
$cid = $form['cid']['#value'] ? $form['cid']['#value'] : 'new';
$group = 'c' . $cid;
}
else {
$node_type_name = strtolower(node_get_types('name', $node_type));
$cid = NULL;
$group = !empty($form['#node']->nid) ? $form['#node']->nid : 'new';
}
drupal_add_js(drupal_get_path('module', 'itweak_upload') . '/itweak_upload.js');
$collapse = variable_get('itweak_upload_collapse_' . $node_type, 0);
$form['attachments']['#collapsible'] = $collapse != 0;
$form['attachments']['#collapsed'] = $collapse > 1;
$form['attachments']['#title'] = t('Attach files to this @type', array(
'@type' => $node_type_name,
));
global $user;
$limits = _upload_file_limits($user);
$add_descr = ($limits['resolution'] ? t('Images are larger than %resolution will be resized. ', array(
'%resolution' => $limits['resolution'],
)) : '') . t('Files must be smaller than %filesize and have one of the following extensions: %extensions.', array(
'%filesize' => format_size($limits['file_size']),
'%extensions' => $limits['extensions'],
));
if (!isset($form['attachments']['#description'])) {
$form['attachments']['#description'] = '';
}
if (FALSE === strpos($form['attachments']['#description'], $add_descr) && _itweak_upload_get_setting('show_attachments_description', '', $node_type, 1)) {
$form['attachments']['#description'] .= ' ' . $add_descr;
}
$form['buttons']['#weight'] = 100;
$form['attachments']['wrapper']['#pre_render'][] = '_itweak_upload_upload_form_prerender_themes';
$form['attachments']['wrapper']['files']['#node_type'] = $node_type;
if (_itweak_upload_get_setting('', 'upload_preview', $node_type, 1) && is_array($form['attachments']['wrapper']['files'])) {
$vid = !empty($form['#node']->vid) ? $form['#node']->vid : null;
_itweak_upload_files_thumbnails($form['attachments']['wrapper']['files'], $vid, $cid, $node_type, $group);
}
return $form;
}
function _itweak_upload_upload_form_prerender_themes($form) {
$form['#theme'] = 'itweak_upload_upload_form_new';
if (isset($form['files'])) {
$form['files']['#theme'] = 'itweak_upload_upload_form_current';
}
return $form;
}
function _itweak_upload_get_setting($setting_base, $setting_name, $node_type, $default = NULL) {
$ret = $default;
if ($setting_base) {
$ret1 = variable_get('itweak_upload_' . $setting_base . '_default', $ret);
if ($ret1 !== '_default') {
$ret = $ret1;
}
if ($setting_name != '') {
$ret1 = variable_get('itweak_upload_' . $setting_base . '_default_' . $node_type, $ret);
if ($ret1 !== '_default') {
$ret = $ret1;
}
}
$setting_name = $setting_base . ($setting_name ? '_' . $setting_name : '');
}
$ret1 = variable_get('itweak_upload_' . $setting_name . '_' . $node_type, $ret);
if ($ret1 !== '_default') {
$ret = $ret1;
}
return $ret;
}
function _itweak_upload_setting_link_default() {
if (module_exists('lightbox2')) {
return 'lightbox2grouped';
}
if (module_exists('colorbox')) {
return 'colorbox';
}
if (module_exists('thickbox')) {
return 'thickbox';
}
if (module_exists('fancybox')) {
return 'fancybox';
}
if (module_exists('shadowbox')) {
return 'shadowboxgrouped';
}
if (module_exists('highslide')) {
return 'highslidegrouped';
}
return 'none';
}
function _itweak_upload_gallery_type_default() {
if (module_exists('jcarousel')) {
return 'jcarousel';
}
if (module_exists('jcarousellite')) {
return 'jcarousellite';
}
return 'none';
}
function _itweak_upload_get_link_options($file, $setting_name, $node_type, $group) {
$href = _itweak_upload_file_create_url($file);
$text = $file->description ? $file->description : $file->filename;
$options = array();
$link_option = _itweak_upload_get_setting('thumbnail_link', $setting_name, $node_type, _itweak_upload_setting_link_default());
$handler = '';
if (module_exists('lightbox2')) {
switch ($link_option) {
case 'lightbox2':
$handler = 'lightbox';
$handler .= '[<a href="' . $href . '">' . $text . '</a>]';
$options['attributes'] = array(
'rel' => $handler,
);
break;
case 'lightbox2grouped':
$handler = 'lightbox' . ($group ? '[attachment-thumb-' . $group . ']' : '[attachment-thumb]');
$handler .= '[<a href="' . $href . '">' . $text . '</a>]';
$options['attributes'] = array(
'rel' => $handler,
);
break;
case 'lightbox2slideshow':
$handler = 'lightshow' . ($group ? '[attachment-thumb-' . $group . ']' : '[attachment-thumb]');
$options['attributes'] = array(
'rel' => $handler,
);
break;
}
}
if (module_exists('colorbox')) {
switch ($link_option) {
case 'colorbox':
$handler = $group ? 'node_' . $group : 'node';
$options['attributes'] = array(
'class' => 'colorbox',
'rel' => $handler,
);
break;
}
}
if (module_exists('thickbox')) {
switch ($link_option) {
case 'thickbox':
$handler = $group ? 'node_' . $group : 'node';
$options['attributes'] = array(
'class' => 'thickbox',
'rel' => $handler,
);
break;
}
}
if (module_exists('fancybox')) {
switch ($link_option) {
case 'fancybox':
$handler = $group ? 'node_' . $group : 'node';
$options['attributes'] = array(
'class' => 'fancybox',
'rel' => $handler,
);
break;
}
}
if (module_exists('shadowbox')) {
switch ($link_option) {
case 'shadowbox':
$handler = 'shadowbox';
$options['attributes'] = array(
'class' => 'shadowbox',
'rel' => $handler,
);
break;
case 'shadowboxgrouped':
$handler = 'shadowbox' . ($group ? '[' . $group . ']' : '');
$options['attributes'] = array(
'class' => 'shadowbox',
'rel' => $handler,
);
break;
}
}
if (module_exists('highslide')) {
switch ($link_option) {
case 'highslide':
$options['attributes'] = array(
'class' => 'highslide',
);
break;
case 'highslidegrouped':
$handler = $group ? 'node_' . $group : 'node';
$options['attributes'] = array(
'class' => 'Ahighslide',
'onclick' => 'return hs.expand(this, { slideshowGroup: \'' . $handler . '\' });',
);
break;
}
}
return $options;
}
function _itweak_upload_encode_derivative($name = 'AttachmentThumbnail') {
$presets = module_exists('imagecache') ? imagecache_preset_by_name($name) : array();
return count($presets) ? $presets['presetid'] : '_none';
}
function _itweak_upload_get_derivative($setting_name, $node_type) {
$derivative = _itweak_upload_get_setting('thumbnail_preset', $setting_name, $node_type, _itweak_upload_encode_derivative());
if ($derivative != '_original' && $derivative != '_none' && is_numeric($derivative)) {
if (module_exists('imagecache')) {
$imagecache_preset = imagecache_preset($derivative);
$derivative = $imagecache_preset['presetname'];
}
else {
$derivative = '_none';
}
}
return $derivative;
}
function _UNUSED_itweak_upload_set_insert_widget_settings($widget, $node_type) {
if (!$widget) {
$widget = array(
'insert' => 0,
'insert_styles' => array(
'auto' => 'auto',
),
'insert_default' => '',
'insert_class' => '',
'insert_width' => '',
);
}
variable_set('itweak_upload_insert_enable_' . $node_type, $widget['insert']);
variable_set('itweak_upload_insert_styles_' . $node_type, $widget['insert_styles']);
variable_set('itweak_upload_insert_default_' . $node_type, $widget['insert_default']);
variable_set('itweak_upload_insert_class_' . $node_type, $widget['insert_class']);
variable_set('itweak_upload_insert_width_' . $node_type, $widget['insert_width']);
}
function _itweak_upload_get_insert_widget_settings($node_type) {
$load_vars = TRUE;
if ($node_type != 'default') {
$load_vars = variable_get('itweak_upload_insert_override_default_' . $node_type, 0);
$widget = _itweak_upload_get_insert_widget_settings('default');
}
else {
$widget = array(
'type' => 'itweak_upload_widget',
'insert' => 1,
'insert_styles' => array(
'auto' => 'auto',
),
'insert_default' => 'auto',
'insert_class' => 'itu-insert-file',
'insert_width' => '',
);
}
if ($load_vars) {
$widget = array(
'type' => 'itweak_upload_widget',
'insert' => variable_get('itweak_upload_insert_enable_' . $node_type, $widget['insert']),
'insert_styles' => array_filter(variable_get('itweak_upload_insert_styles_' . $node_type, $widget['insert_styles'])),
'insert_default' => variable_get('itweak_upload_insert_default_' . $node_type, $widget['insert_default']),
'insert_class' => variable_get('itweak_upload_insert_class_' . $node_type, $widget['insert_class']),
'insert_width' => variable_get('itweak_upload_insert_width_' . $node_type, $widget['insert_width']),
);
}
return $widget;
}
function itweak_upload_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
$form['#pre_render'][] = '_itweak_upload_upload_form_prerender';
$form['#submit'][] = '_itweak_upload_files_imagecache_flush';
$form += array(
'#input' => TRUE,
'#itu' => 1,
);
$form['#process'][] = '_itweak_upload_process';
}
switch ($form_id) {
case 'upload_js':
$cached_form_state = array();
if (empty($_POST) || !($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#node']) || !isset($cached_form['attachments'])) {
return;
}
$node_type = $cached_form['#node']->type;
if (_itweak_upload_get_setting('', 'upload_preview', $node_type, 1)) {
$group = $cached_form['#node']->nid ? $cached_form['#node']->nid : 'new';
$vid = $cached_form['#node']->vid;
_itweak_upload_files_thumbnails($form['files'], $vid, NULL, $node_type, $group);
}
$form['#pre_render'][] = '_itweak_upload_upload_form_prerender_themes';
$form['files']['#node_type'] = $node_type;
$form += array(
'#input' => TRUE,
);
$form['#process'][] = '_itweak_upload_process';
break;
case 'comment_form':
if (module_exists('comment_upload')) {
$nid = $form['nid']['#value'];
$node = node_load($nid);
$form['#node']->itu_comment = TRUE;
if (!isset($form['#node']->type)) {
$form['#node']->type = $node->type;
}
$form['#node']->vid = $node->vid;
$form['#pre_render'][] = '_itweak_upload_upload_form_prerender';
$form['#submit'][] = '_itweak_upload_files_imagecache_flush';
$form += array(
'#input' => TRUE,
'#itu' => 1,
);
$form['#process'][] = '_itweak_upload_process';
if (isset($form['preview'])) {
$form['preview']['#type'] = 'submit';
}
}
break;
case 'comment_upload_js':
if (module_exists('comment_upload')) {
$cached_form_state = array();
if (empty($_POST) || !($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#comment_upload_storage']) || !isset($cached_form['attachments'])) {
return;
}
$nid = $cached_form['nid']['#value'];
$node = node_load($nid);
$node_type = $node->type;
if (_itweak_upload_get_setting('', 'upload_preview', $node_type, 1)) {
$vid = $node->vid;
$cid = $cached_form['cid']['#value'] ? $cached_form['cid']['#value'] : 'new';
$group = 'c' . $cid;
_itweak_upload_files_thumbnails($form['files'], $vid, $cid, $node_type, $group);
}
$form['#pre_render'][] = '_itweak_upload_upload_form_prerender_themes';
$form['files']['#node_type'] = $node_type;
$form += array(
'#input' => TRUE,
);
$form['#process'][] = '_itweak_upload_process';
}
break;
case 'node_type_form':
module_load_include('admin.inc', 'itweak_upload');
_itweak_upload_node_type_form($form);
break;
case 'upload_admin_settings':
module_load_include('admin.inc', 'itweak_upload');
_itweak_upload_admin_settings($form);
break;
}
}
function itweak_upload_theme() {
return array(
'itweak_upload_upload_form_new' => array(
'arguments' => array(
'form' => NULL,
),
'function' => 'itweak_upload_upload_form_new',
),
'itweak_upload_upload_form_current' => array(
'arguments' => array(
'form' => NULL,
),
'function' => 'itweak_upload_upload_form_current',
),
'itweak_upload_upload_attachments' => array(
'arguments' => array(
'files' => NULL,
),
'function' => 'itweak_upload_upload_attachments',
),
'itweak_upload_comment_upload_attachments' => array(
'arguments' => array(
'files' => NULL,
'display_images' => FALSE,
'preset' => NULL,
'itu' => NULL,
),
'function' => 'itweak_upload_comment_upload_attachments',
),
'itweak_upload_thumbnail' => array(
'arguments' => array(
'thumbnail' => NULL,
'href' => NULL,
'title_text' => NULL,
'caption_text' => NULL,
'options' => NULL,
),
),
'itweak_upload_images' => array(
'arguments' => array(
'list' => NULL,
'options' => NULL,
),
),
'itweak_upload_images_body' => array(
'arguments' => array(
'thumbnails' => NULL,
'limit' => NULL,
'options' => NULL,
),
),
'itweak_upload_images_teaser' => array(
'arguments' => array(
'thumbnails' => NULL,
'limit' => NULL,
'options' => NULL,
),
),
'itweak_upload_images_comment' => array(
'arguments' => array(
'thumbnails' => NULL,
'limit' => NULL,
'options' => NULL,
),
),
'itweak_upload_field_widget_settings_styles' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'itweak_upload.admin.inc',
),
'view_uploaded_files_forbidden' => array(
'arguments' => array(
'node' => NULL,
),
),
);
}
function itweak_upload_upload_form_new($form) {
unset($form['new']['upload']['#title']);
unset($form['new']['upload']['#description']);
drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight');
return drupal_render($form);
}
function itweak_upload_upload_form_current($form) {
drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight');
$node_type = isset($form['#node_type']) ? $form['#node_type'] : 'default';
if (ITU_USE_INSERT_MODULE && module_exists('insert')) {
$widget = _itweak_upload_get_insert_widget_settings($node_type);
$widget['class'] = isset($widget['insert_class']) ? $widget['insert_class'] . ' ' : '';
}
$rows = array();
foreach (element_children($form) as $key) {
$extension = strtolower(substr(strrchr($form[$key]['filename']['#value'], '.'), 1));
if ($widget) {
static $js_added;
if (!isset($js_added)) {
$js_added = array();
drupal_add_js(drupal_get_path('module', 'insert') . '/insert.js');
drupal_add_js(drupal_get_path('module', 'itweak_upload') . '/itweak_upload.insert.js');
$insert_settings = array(
'maxWidth' => $widget['insert_width'],
'wrapper' => 'tr',
'fields' => array(
'alt' => 'input[name$="[alt]"], textarea[name$="[alt]"]',
'title' => 'input[name$="[title]"], textarea[name$="[title]"]',
'description' => 'input[name$="[description]"], textarea[name$="[description]"]',
),
);
}
if (!isset($js_added[$widget['type']])) {
$js_added[$widget['type']] = TRUE;
drupal_add_js(array(
'itweak_upload' => array(
'widgets' => array(
$widget['type'] => $insert_settings,
),
),
), 'setting');
}
$insert_styles = array_filter((array) $widget['insert_styles']);
$default = !empty($widget['insert_default']) ? $widget['insert_default'] : 'auto';
if (!isset($insert_styles[$default])) {
$insert_styles[$default] = $default;
}
$element =& $form[$key];
$item = array(
'filepath' => $form[$key]['filepath']['#value'],
'filename' => $form[$key]['filename']['#value'],
);
$style_options = array();
foreach ($insert_styles as $style_name => $enabled) {
if (is_integer($style_name)) {
$style_name = $enabled;
$enabled = TRUE;
}
if ($enabled && ($style = insert_style_load($style_name))) {
$widget['insert_class'] = $widget['class'] . 'mime-' . $extension;
$element['insert_templates'][$style_name] = array(
'#type' => 'hidden',
'#value' => insert_content($item, $style, $widget),
'#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $style_name),
'#name' => $element['#name'] . '[insert_template][' . $style_name . ']',
'#attributes' => array(
'class' => 'insert-template',
),
);
$style_options[$style_name] = $style['label'];
}
}
$element['insert'] = array(
'#theme' => 'insert_widget',
'#type' => 'markup',
'#options' => $style_options,
'#widget' => $widget,
'#weight' => -1,
'#default_value' => $default,
);
}
$form[$key]['weight']['#attributes']['class'] = 'upload-weight';
$row = array(
'',
);
if (isset($form[$key]['preview']) && is_array($form[$key]['preview'])) {
$row[] = array(
'data' => drupal_render($form[$key]['preview']),
'class' => 'mime',
);
}
else {
$row[] = array(
'data' => '',
'class' => 'mime',
);
}
$html = '';
$url = $form[$key]['description']['#description'];
unset($form[$key]['description']['#description']);
$form[$key]['description']['#size'] = 40;
$form[$key]['description']['#attributes'] = array(
'class' => 'rename',
);
$html .= drupal_render($form[$key]['description']);
$html .= '<span class="details">' . drupal_render($form[$key]['size']) . ' - ' . $url . '</span>';
$row[] = array(
'data' => $html,
'class' => 'file container-inline',
);
if ($widget) {
$html = drupal_render($form[$key]['insert_templates']);
$html .= drupal_render($form[$key]['insert']);
$html .= '<span class="details">' . ' ' . '</span>';
$row[] = array(
'data' => $html,
'class' => 'itu-insert container-inline',
);
}
$form[$key]['remove']['#attributes'] = array(
'class' => 'itu-remove',
);
$form[$key]['remove']['#suffix'] = ' ' . t('Remove');
$row[] = array(
'data' => drupal_render($form[$key]['remove']),
'class' => 'itu-remove container-inline',
);
$form[$key]['list']['#suffix'] = ' ' . t('List');
$html = drupal_render($form[$key]['list']);
if (isset($form[$key]['private'])) {
$form[$key]['private']['#suffix'] = ' ' . t('Private');
$html .= ' ' . drupal_render($form[$key]['private']);
}
$html .= '<span class="details">' . ' ' . '</span>';
$row[] = array(
'data' => $html,
'class' => 'list container-inline',
);
$row[] = drupal_render($form[$key]['weight']);
$rows[] = array(
'data' => $row,
'class' => 'draggable mime-' . $extension,
);
}
$output = '';
if (count($rows)) {
$output .= theme('table', array(), $rows, array(
'id' => 'upload-attachments',
));
}
$output .= drupal_render($form);
return $output;
}
function itweak_upload_upload_attachments($files) {
$stats = function_exists('_download_count_stats');
$header = $stats ? array(
array(
'data' => t('Preview'),
'class' => 'preview',
),
array(
'data' => t('Attachment'),
'class' => 'file',
),
array(
'data' => t('Count / Last Download'),
'class' => 'download_stats',
'colspan' => 2,
),
array(
'data' => t('Size'),
'class' => 'size',
),
) : array(
array(
'data' => t('Preview'),
'class' => 'preview',
),
array(
'data' => t('Attachment'),
'class' => 'file',
),
array(
'data' => t('Size'),
'class' => 'size',
),
);
$rows = array();
foreach ($files as $file) {
$file = (object) $file;
if ($file->list && empty($file->remove) && empty($file->hidden)) {
$extension = strtolower(substr(strrchr($file->filename, '.'), 1));
$href = _itweak_upload_file_create_url($file);
$text = $file->description ? $file->description : $file->filename;
$row = array();
$options = isset($file->preview_options) ? $file->preview_options : array();
if (!$file->access || !(isset($file->preview) || _itweak_upload_isimage($file) || isset($options['custom']))) {
$options = array();
}
$data = compact('file', 'text', 'href', 'options');
drupal_alter('itweak_upload_prerender', $data);
extract($data);
if (isset($file->preview)) {
$row[] = array(
'data' => drupal_render($file->preview),
'class' => 'mime mime-' . $extension,
);
}
else {
$row[] = array(
'data' => '',
'class' => 'mime mime-' . $extension,
);
}
$row[] = array(
'data' => l($text, $href, $options),
'class' => 'file',
);
if ($stats) {
_download_count_stats($file);
$row[] = array(
'data' => $file->download_count,
'class' => 'download_count',
);
$row[] = array(
'data' => $file->download_last,
'class' => 'download_last',
);
}
$row[] = array(
'data' => format_size($file->filesize),
'class' => 'size',
);
$rows[] = $row;
}
}
if (count($rows)) {
return '<div class="itu-attachments">' . theme('table', $header, $rows, array(
'class' => 'itu-attachment-list' . ($stats ? ' withstats' : ' withoutstats'),
'id' => 'attachments',
)) . '</div>';
}
}
function itweak_upload_comment_upload_attachments($files, $display_images = FALSE, $preset = NULL, $itu = NULL) {
if ($itu) {
return itweak_upload_upload_attachments($files);
}
}
function _itweak_upload_preprocess_files(&$files, &$thumbnails, $files_display, $setting_name, $node_type, $group) {
$derivative = _itweak_upload_get_derivative($setting_name, $node_type);
$show_title = _itweak_upload_get_setting('thumbnail_title', '', $node_type, 1);
$show_caption = FALSE;
$thumbnails = array();
$cnt_other = 0;
foreach ($files as $fid => $file) {
$file = (object) $file;
if ($file->list && empty($file->remove) && file_exists(file_create_path($file->filepath))) {
$access = isset($file->access) ? $file->access : TRUE;
$filepath = $file->filepath;
$basepath = file_directory_path();
if (strpos($filepath, $basepath) === 0) {
$filepath = substr($filepath, strlen($basepath) + 1);
}
$headers = module_invoke_all('file_download', $filepath, TRUE);
$access = !in_array(-1, $headers);
if (is_object($files[$fid])) {
$files[$fid]->access = $access;
}
else {
$files[$fid]['access'] = $access;
}
$preview = FALSE;
if ($access && $files_display > 1) {
$options = _itweak_upload_get_link_options($file, $setting_name, $node_type, $group);
$preview = module_invoke_all('itweak_upload_preview', $file, $derivative, $show_title, $show_caption, $options);
}
if ($files_display == 1 || !$preview || $preview['#type'] != 'item') {
$cnt_other += 1;
}
else {
if (is_object($files[$fid])) {
$files[$fid]->preview = $preview;
$files[$fid]->preview_options = $options;
}
else {
$files[$fid]['preview'] = $preview;
$files[$fid]['preview_options'] = $options;
}
if ($files_display == 2) {
$cnt_other += 1;
}
else {
if ($files_display > 2) {
$thumbnails[] = (object) $files[$fid];
if (is_object($files[$fid])) {
$files[$fid]->hidden = true;
}
else {
$files[$fid]['hidden'] = true;
}
}
}
}
}
}
return $cnt_other;
}
function itweak_upload_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'alter':
if (isset($node->itu_files)) {
$node->files = $node->itu_files;
unset($node->itu_files);
}
break;
case 'view':
$files_display = $teaser ? variable_get('itweak_upload_teaser_display_' . $node->type, 0) : variable_get('itweak_upload_node_display_' . $node->type, 1);
if ($files_display && !empty($node->files) && user_access('view uploaded files')) {
$group = $node->nid;
$setting_name = $teaser ? 'teaser' : 'node';
$teaser_images_max = variable_get('itweak_upload_teaser_images_' . $node->type, 0);
if ($teaser_images_max === '') {
$teaser_images_max = -1;
}
$cnt_other = _itweak_upload_preprocess_files($node->files, $thumbnails, $files_display, $setting_name, $node->type, $group);
if ($cnt_other) {
if ($files_display != 4) {
$node->content['files'] = array(
'#value' => theme('itweak_upload_upload_attachments', $node->files),
'#weight' => 50,
);
}
}
else {
unset($node->content['files']);
}
$node->itu_files = $node->files;
$node->files = array();
if (count($thumbnails)) {
$options = array(
'gallery_type' => _itweak_upload_get_setting('gallery_type', $setting_name, $node->type, _itweak_upload_gallery_type_default()),
);
if ($teaser) {
$node->content['itweak_upload'] = array(
'#value' => theme('itweak_upload_images_teaser', $thumbnails, $teaser_images_max, $options),
'#weight' => 49,
);
}
else {
$node->content['itweak_upload'] = array(
'#value' => theme('itweak_upload_images_body', $thumbnails, -1, $options),
'#weight' => 49,
);
}
}
}
if ($files_display && !empty($node->files) && !user_access('view uploaded files')) {
$node->content['itweak_upload'] = array(
'#value' => theme('view_uploaded_files_forbidden', $node),
'#weight' => 49,
);
}
break;
case 'delete':
_itweak_upload_delete($node);
break;
}
}
function theme_itweak_upload_images($list, $options = NULL) {
$gallery_type = $options && isset($options['gallery_type']) ? $options['gallery_type'] : 'none';
$carousel_visible = ITU_CAROUSEL_VISIBLE_ITEMS;
$div_class = 'itu-attachment-images';
$list_class = 'itu-attachment-thumbs';
$jcarousellite = FALSE;
if ($gallery_type == 'jcarousel' && !module_exists('jcarousel')) {
$gallery_type = 'none';
}
if ($gallery_type == 'jcarousellite') {
if (!module_exists('jcarousellite') || count($list) <= $carousel_visible) {
$gallery_type = 'none';
}
else {
$jcarousellite = TRUE;
}
}
if ($gallery_type != '' && $gallery_type != 'none') {
$div_class .= ' ' . $gallery_type;
$list_class .= '-' . $gallery_type;
}
$output = '<div class="' . $div_class . '">';
if ($jcarousellite) {
$output .= '<a href="#" class="itu-attachment-jcarousellite-prev"> </a>';
}
$output .= theme('item_list', $list, NULL, 'ul', array(
'class' => $list_class,
));
if ($jcarousellite) {
$output .= '<a href="#" class="itu-attachment-jcarousellite-next"> </a>';
}
$output .= '</div>';
return $output;
}
function theme_itweak_upload_thumbnail($thumbnail, $href, $title_text = NULL, $caption_text = NULL, $options = NULL) {
if (!$options) {
$options = array();
}
$options += array(
'html' => TRUE,
);
if ($title_text) {
$options['attributes']['title'] = $title_text;
}
$html = l($thumbnail, $href, $options);
$html = '<div class="itu-attachment-thumb">' . $html . '</div>';
return $html;
}
function theme_itweak_upload_images_body($files, $limit = -1, $options = NULL) {
$items = array();
foreach ($files as $file) {
if ($limit != -1 && count($items) >= $limit) {
break;
}
$file = (object) $file;
if ($file->list && empty($file->remove)) {
if (isset($file->preview)) {
$items[] = array(
'data' => $file->preview['#value'],
'class' => '',
);
}
}
}
if (count($items)) {
return theme('itweak_upload_images', $items, $options);
}
}
function theme_itweak_upload_images_teaser($files, $limit, $options = NULL) {
return theme_itweak_upload_images_body($files, $limit, $options);
}
function theme_itweak_upload_images_comment($files, $limit, $options = NULL) {
return theme_itweak_upload_images_body($files, $limit, $options);
}
function itweak_upload_comment(&$comment, $op) {
if ($op != 'delete' && !($op == 'view' && user_access('view files uploaded to comments')) || !module_exists('comment_upload')) {
return;
}
if (!isset($comment->files)) {
$comment->files = comment_upload_load_files($comment->cid);
}
if (!isset($comment->files) || !count($comment->files)) {
return;
}
if ($op == 'delete') {
if (function_exists('imagecache_file_delete')) {
foreach ($comment->files as $fid => $file) {
imagecache_file_delete((object) $file);
}
}
return;
}
$node = node_load($comment->nid);
$node_type = $node->type;
$files_display = variable_get('itweak_upload_comment_display_' . $node_type, 2);
if ($files_display) {
$group = 'c' . $comment->cid;
$setting_name = 'comment';
$cnt_other = _itweak_upload_preprocess_files($comment->files, $thumbnails, $files_display, $setting_name, $node_type, $group);
if ($cnt_other) {
if ($files_display != 4) {
$comment->comment .= theme('itweak_upload_comment_upload_attachments', $comment->files, FALSE, NULL, TRUE);
}
}
$comment->files = array();
if (count($thumbnails)) {
$options = array(
'gallery_type' => _itweak_upload_get_setting('gallery_type', $setting_name, $node_type, _itweak_upload_gallery_type_default()),
);
$comment->comment .= theme('itweak_upload_images_comment', $thumbnails, -1, $options);
}
}
}
function _itweak_upload_files_imagecache_flush(&$form, &$form_state) {
if (function_exists('imagecache_file_delete') && isset($form_state['values']['files'])) {
foreach ($form_state['values']['files'] as $fid => $file) {
imagecache_file_delete((object) $file);
}
}
}
function _itweak_upload_delete(&$node) {
if (function_exists('imagecache_file_delete')) {
if (isset($node->files)) {
foreach ($node->files as $fid => $file) {
imagecache_file_delete((object) $file);
}
}
if (module_exists('comment_upload')) {
$result = db_query("SELECT cu.fid, cu.nid, f.filepath FROM {comment_upload} cu INNER JOIN {files} f ON cu.fid = f.fid WHERE cu.nid = %d", $node->nid);
while ($file = db_fetch_array($result)) {
imagecache_file_delete((object) $file);
}
}
}
}
function itweak_upload_progress_implementation() {
static $implementation;
if (!isset($implementation)) {
$implementation = FALSE;
if (extension_loaded('uploadprogress')) {
$implementation = 'uploadprogress';
}
elseif (extension_loaded('apc') && ini_get('apc.rfc1867')) {
$implementation = 'apc';
}
}
return $implementation;
}
function itweak_upload_progress($key = NULL) {
$progress = array(
'message' => t('Starting upload...'),
'percentage' => -1,
);
$implementation = itweak_upload_progress_implementation();
if ($implementation == 'uploadprogress') {
$status = uploadprogress_get_info($key);
if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
$progress['message'] = t('Uploading... (@current of @total)', array(
'@current' => format_size($status['bytes_uploaded']),
'@total' => format_size($status['bytes_total']),
));
$progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
}
}
elseif ($implementation == 'apc') {
$status = apc_fetch('upload_' . $key);
if (isset($status['current']) && !empty($status['total'])) {
$progress['message'] = t('Uploading... (@current of @total)', array(
'@current' => format_size($status['current']),
'@total' => format_size($status['total']),
));
$progress['percentage'] = round(100 * $status['current'] / $status['total']);
}
}
drupal_json($progress);
}
function _itweak_upload_process($element, $value, $form_state, $complete_form) {
if (isset($element['#itu']) && isset($element['attachments'])) {
$element['attachments']['wrapper'] = _itweak_upload_add_progressbar($element['attachments']['wrapper'], $form_state);
}
else {
$element = _itweak_upload_add_progressbar($element, $form_state);
}
return $element;
}
function _itweak_upload_add_progressbar($form, $form_state) {
$progress_indicator = variable_get('itweak_upload_progress_indicator', 'bar');
if ($progress_indicator != 'throbber' && ($implementation = itweak_upload_progress_implementation())) {
switch ($implementation) {
case 'uploadprogress':
$name = 'UPLOAD_IDENTIFIER';
break;
case 'apc':
$name = 'APC_UPLOAD_PROGRESS';
break;
default:
$name = '';
break;
}
if ($name) {
$upload_progress_key = !empty($form['#post']) ? $form['#post'][$name] : (isset($form[$name]) ? $form[$name]['#value'] : md5(mt_rand()));
$insert = array(
'#name' => $name,
'#type' => 'hidden',
'#value' => $upload_progress_key,
'#attributes' => array(
'class' => 'itweak_upload-progress',
),
);
$form_id = !empty($form['#post']) && !empty($form['#post']['form_id']) ? $form['#post']['form_id'] : $form['form_id'];
$insert = form_builder($form_id, $insert, $form_state);
$insert = drupal_render($insert);
$form['new']['upload']['#prefix'] = empty($form['new']['upload']['#prefix']) ? $insert : $form['new']['upload']['#prefix'] . $insert;
$form['new']['attach']['#ahah']['progress']['path'] = 'ajax/itu/progress/' . $upload_progress_key;
}
$form['new']['attach']['#ahah']['method'] = 'replace';
$form['new']['attach']['#ahah']['effect'] = 'fade';
$form['new']['attach']['#ahah']['progress']['type'] = $progress_indicator;
}
return $form;
}
function theme_view_uploaded_files_forbidden($node) {
global $user;
static $authenticated_view_uploaded_files;
if (!$user->uid) {
if (!isset($authenticated_view_uploaded_files)) {
$authenticated_view_uploaded_files = array_key_exists(DRUPAL_AUTHENTICATED_RID, user_roles(TRUE, 'view uploaded files'));
}
if ($authenticated_view_uploaded_files) {
$destination = drupal_get_destination();
if (variable_get('user_register', 1)) {
$html = t('<a href="@login">Login</a> or <a href="@register">register</a> to view attached files', array(
'@login' => url('user/login', array(
'query' => $destination,
)),
'@register' => url('user/register', array(
'query' => $destination,
)),
));
}
else {
$html = t('<a href="@login">Login</a> to view attached files', array(
'@login' => url('user/login', array(
'query' => $destination,
)),
));
}
$div_class = 'itu-attachment-forbidden';
$html = '<div class="' . $div_class . '">' . $html . '</div>';
return $html;
}
}
}