View source
<?php
function imceimage_menu() {
$items = array();
$items['imceimage/verify'] = array(
'title' => 'imceimage_verify',
'page callback' => 'imceimage_verify',
'access callback' => true,
'type' => MENU_CALLBACK,
);
return $items;
}
function imceimage_verify() {
$args = func_get_args();
$url = join('/', $args);
$file_path = _imceimage_image_to_filepath($url);
if (($info = image_get_info($file_path)) == FALSE) {
$info = array();
$info['validimage'] = FALSE;
}
else {
$info['validimage'] = TRUE;
}
drupal_json($info);
}
function imceimage_field_info() {
return array(
'imceimage' => array(
'label' => 'IMCE Image',
'description' => t('Use IMCE browser to attach images to content'),
'callbacks' => array(
'tables' => CONTENT_CALLBACK_DEFAULT,
'arguments' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
function imceimage_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
return $form;
case 'save':
return 'imceimage_file_types';
case 'database columns':
$columns = array(
'imceimage_path' => array(
'type' => 'char',
'length' => 255,
'not null' => FALSE,
'default' => "",
),
'imceimage_width' => array(
'type' => 'int',
'not null' => TRUE,
'default' => "0",
),
'imceimage_height' => array(
'type' => 'int',
'not null' => TRUE,
'default' => "0",
),
'imceimage_alt' => array(
'type' => 'text',
),
'imceimage_title' => array(
'type' => 'text',
),
);
return $columns;
}
}
function imceimage_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
if (is_array($items)) {
foreach ($items as $delta => $item) {
$error_field = $field['field_name'] . '][' . $delta . '][value';
if ($item['imceimage_path'] != '') {
$file_path = _imceimage_image_to_filepath($item['imceimage_path']);
if (image_get_info($file_path) == FALSE) {
form_set_error($error_field, t('%name is not a valid image or has been deleted (url: %url)', array(
'%name' => t($field['widget']['label']),
'%file' => $file_path,
'%url' => $item['imceimage_path'],
)));
}
}
}
}
break;
}
}
function imceimage_content_is_empty($item, $field) {
return $item['imceimage_path'] == '' && $item['imceimage_width'] == '' && $item['imceimage_height'] == '' && $item['imceimage_alt'] == '' && $item['imceimage_title'] == '';
}
function imceimage_field_formatter_info() {
$formatters = array(
'default' => array(
'label' => 'Default',
'field types' => array(
'imceimage',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'with_caption' => array(
'label' => 'With caption',
'field types' => array(
'imceimage',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'link' => array(
'label' => 'Link',
'field types' => array(
'imceimage',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'url' => array(
'label' => 'URL',
'field types' => array(
'imceimage',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'thumb' => array(
'label' => 'Thumbnail',
'field types' => array(
'imceimage',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
if (module_exists('imagecache')) {
foreach (imagecache_presets() as $preset) {
$formatters[$preset['presetname'] . '_default'] = array(
'label' => t('@preset image', array(
'@preset' => $preset['presetname'],
)),
'field types' => array(
'imceimage',
),
);
$formatters[$preset['presetname'] . '_linked'] = array(
'label' => t('@preset image linked to node', array(
'@preset' => $preset['presetname'],
)),
'field types' => array(
'imceimage',
),
);
$formatters[$preset['presetname'] . '_imagelink'] = array(
'label' => t('@preset image linked to image', array(
'@preset' => $preset['presetname'],
)),
'field types' => array(
'imceimage',
),
);
$formatters[$preset['presetname'] . '_path'] = array(
'label' => t('@preset file path', array(
'@preset' => $preset['presetname'],
)),
'field types' => array(
'imceimage',
),
);
$formatters[$preset['presetname'] . '_url'] = array(
'label' => t('@preset URL', array(
'@preset' => $preset['presetname'],
)),
'field types' => array(
'imceimage',
),
);
}
}
return $formatters;
}
function imceimage_widget_info() {
return array(
'imceimage' => array(
'label' => 'IMCE Image',
'field types' => array(
'imceimage',
),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
function imceimage_widget_settings($op, $widget) {
switch ($op) {
case 'form':
$form = array();
$form['imceimage_file_types'] = array(
'#type' => 'textfield',
'#title' => t('Valid File Types'),
'#default_value' => $widget['imceimage_file_types'] ? $widget['imceimage_file_types'] : 'png,gif,jpg,jpeg',
'#description' => t('This is not implemented currently'),
'#required' => TRUE,
);
return $form;
case 'validate':
break;
case 'save':
return array(
'imceimage_file_types',
);
case 'callbacks':
return array(
'default value' => CONTENT_CALLBACK_NONE,
);
}
}
function imceimage_widget(&$form, &$form_state, $field, $items, $delta = 0) {
static $inc_js = FALSE;
if (!$inc_js) {
drupal_add_js(drupal_get_path('module', 'imceimage') . '/includes/js/imceimage.js');
drupal_add_js(array(
'imceimage' => array(
'url' => url('imceimage/verify'),
),
), 'setting');
$inc_js = TRUE;
}
$element = array(
'#type' => 'imceimage',
'#default_value' => isset($items[$delta]) ? $items[$delta] : array(),
);
return $element;
}
function imceimage_views_api() {
return array(
'api' => 2.0,
);
}
function theme_imceimage_image($path, $width = '', $height = '', $alt = '', $title = '', $id = '') {
if ($path == '' || empty($path)) {
$path = base_path() . drupal_get_path('module', 'imceimage') . '/includes/images/1x1.png';
}
$file_path = _imceimage_image_to_filepath($path);
if (($info = image_get_info($file_path)) == false) {
$path = base_path() . drupal_get_path('module', 'imceimage') . '/includes/images/warning.png';
$alt = 'Image does not exist anymore or an invalid image';
$width = 32;
$height = 32;
}
$path = 'src="' . $path . '" ';
$alt = 'alt="' . $alt . '" ';
$title = 'title="' . $title . '"';
$id = !empty($id) ? 'id="' . $id . '" ' : '';
$width = !empty($width) ? 'width="' . $width . '" ' : '';
$height = !empty($height) ? 'height="' . $height . '" ' : '';
return '<img ' . $path . $alt . $title . $width . $height . $id . '/>';
}
function _imceimage_get_thumb($image) {
$thumb_prefix = 'thumb_';
$path = $image['imceimage_path'];
$thumb_name = $thumb_prefix . basename($path);
$thumb_path = dirname($path) . "/" . $thumb_name;
list($width, $height, $type, $image_attributes) = @getimagesize($thumb_path);
$thumb = array();
$thumb['imceimage_path'] = $thumb_path;
$thumb['imceimage_width'] = $width;
$thumb['imceimage_height'] = $height;
$thumb['imceimage_alt'] = $image['imceimage_alt'];
$thumb['imceimage_title'] = $image['imceimage_title'];
return $thumb;
}
function theme_imceimage_formatter_thumb($element) {
$item = $element['#item'];
$field = $element['#field_name'];
$delta = $element['#delta'];
$id = "imceimage-" . $field . "-thumb";
$thumb = _imceimage_get_thumb($item);
return theme_imceimage_image($thumb['imceimage_path'], $thumb['imceimage_width'], $thumb['imceimage_height'], $thumb['imceimage_alt'], $thumb['imceimage_title'], $id);
}
function theme_imceimage_formatter_link($element) {
$item = $element['#item'];
$title = '';
if (isset($item['imceimage_alt'])) {
$title = $item['imceimage_alt'];
}
else {
if (isset($item['imceimage_title'])) {
$title = $item['imceimage_title'];
}
else {
$title = $element['#node']->title;
}
}
return l($title, $item['imceimage_path'], array(
'attributes' => array(
'class' => 'imceimage-link',
),
));
}
function theme_imceimage_formatter_url($element) {
$item = $element['#item'];
return $item['imceimage_path'];
}
function theme_imceimage_formatter_default($element) {
$item = $element['#item'];
$field = $element['#field_name'];
$delta = $element['#delta'];
$id = "imceimage-" . $field;
if ($item['imceimage_path']) {
return theme_imceimage_image($item['imceimage_path'], $item['imceimage_width'], $item['imceimage_height'], $item['imceimage_alt'], $item['imceimage_title'], $id);
}
else {
return '';
}
}
function theme_imceimage_formatter_with_caption($element) {
$item = $element['#item'];
$field = $element['#field_name'];
$delta = $element['#delta'];
$id = "imceimage-" . $field;
if ($item['imceimage_path']) {
$image = theme_imceimage_image($item['imceimage_path'], $item['imceimage_width'], $item['imceimage_height'], $item['imceimage_alt'], $item['imceimage_title'], $id);
$caption = $item['imceimage_alt'];
return "<div class='image-with-caption'><div class='image'>" . $image . "</div><div class='caption'>" . $caption . "</div></div>";
}
else {
return '';
}
}
function theme_imceimage_formatter($element) {
if (!isset($element['#item']['nid'])) {
$element['#item']['nid'] = $element['#node']->nid;
}
if (isset($element['#item']['nid']) && ($node = node_load($element['#item']['nid']))) {
$result = imceimage_field_formatter($element['#field_name'], $element['#item'], $element['#formatter'], $node);
return $result;
}
}
function imceimage_field_formatter($field, $item, $formatter, $node) {
$parts = explode('_', $formatter);
$style = array_pop($parts);
$presetname = implode('_', $parts);
$class = "imagecache imagecache-{$presetname} imagecache-{$style} imagecache-{$formatter}";
$caption = $item['imceimage_alt'];
$title = $item['imceimage_title'];
$image_url = $item['imceimage_path'];
if (empty($image_url)) {
return '';
}
$path_parts = parse_url($item['imceimage_path']);
$file_directory = variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE ? 'system/files' : file_directory_path();
$filename = basename($item['imceimage_path']);
$subdir = str_replace("/" . $file_directory . "/", '', str_replace('/' . $filename, '', $path_parts['path']));
$orig_filepath = $subdir . '/' . $filename;
$imagecache_url = imagecache_create_url($presetname, $orig_filepath);
$full_img_url = file_create_url($orig_filepath);
if ($preset = imagecache_preset_by_name($presetname)) {
$dst = imagecache_create_path($presetname, $orig_filepath);
if (!file_exists($dst)) {
imagecache_build_derivative($preset['actions'], $filepath, $dst);
}
switch ($style) {
case 'linked':
$imagetag = theme('imagecache', $presetname, $orig_filepath, $caption, $title);
$img = l($imagetag, 'node/' . $node->nid, array(
'attributes' => array(
'class' => $class,
),
'html' => true,
));
return $img;
case 'imagelink':
$imagetag = theme('imagecache', $presetname, $orig_filepath, $caption, $title);
$img = l($imagetag, $full_img_url, array(
'attributes' => array(
'class' => $class,
'target' => '_blank',
),
'html' => true,
));
return $img;
case 'url':
return $imagecache_url;
case 'path':
return imagecache_create_path($presetname, $orig_filepath);
default:
return theme('imagecache', $presetname, $orig_filepath, $caption, $title);
}
}
return '<!-- imagecache formatter preset(' . $presetname . ') not found! -->';
}
function theme_imceimage($element) {
return $element['#children'];
}
function imceimage_theme() {
$theme = array(
'imceimage' => array(
'arguments' => array(
'element' => NULL,
),
),
'imceimage_formatter_default' => array(
'arguments' => array(
'element' => NULL,
),
),
'imceimage_formatter_with_caption' => array(
'arguments' => array(
'element' => NULL,
),
),
'imceimage_formatter_link' => array(
'arguments' => array(
'element' => NULL,
),
),
'imceimage_formatter_url' => array(
'arguments' => array(
'element' => NULL,
),
),
'imceimage_formatter_thumb' => array(
'arguments' => array(
'element' => NULL,
),
),
);
if (module_exists('imagecache')) {
foreach (imagecache_presets() as $preset) {
$theme['imceimage_formatter_' . $preset['presetname'] . '_default'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_imceimage_formatter',
);
$theme['imceimage_formatter_' . $preset['presetname'] . '_linked'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_imceimage_formatter',
);
$theme['imceimage_formatter_' . $preset['presetname'] . '_imagelink'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_imceimage_formatter',
);
$theme['imceimage_formatter_' . $preset['presetname'] . '_path'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_imceimage_formatter',
);
$theme['imceimage_formatter_' . $preset['presetname'] . '_url'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_imceimage_formatter',
);
}
}
return $theme;
}
function imceimage_elements() {
$el = array();
$el['imceimage'] = array(
'#input' => TRUE,
'#columns' => array(
'imceimage_path',
'imceimage_width',
'imceimage_height',
'imceimage_alt',
'imceimage_title',
),
'#delta' => 0,
'#process' => array(
'imceimage_process',
),
'#theme' => 'imceimage_override',
);
return $el;
}
function imceimage_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
$delta = $element['#delta'];
$link = url('imce/browse');
$field_id = $field['field_name'];
if (isset($element['#value']['imce_widget_container'])) {
$element['#value'] = $element['#value']['imce_widget_container'];
}
$links = t('Select Image') . '</a>]' . '[<a class="imceimage-remove" href="javascript:' . "imceImage.remove('{$delta}-{$field_id}')" . '">' . t('Remove') . '</a>]';
$imceparams['url'] = "imceimagepath-{$delta}-{$field_id}";
$imceparams['width'] = "imceimagewidth-{$delta}-{$field_id}";
$imceparams['height'] = "imceimageheight-{$delta}-{$field_id}";
$link = url('imce');
$element['imce_widget_container'] = array(
'#type' => 'fieldset',
'#attributes' => array(
'style' => 'display:none',
'id' => "imceimagefieldset-{$delta}-{$field_id}",
),
);
if (!stripos(theme_imceimage_image($element['#value']['imceimage_path'], $element['#value']['imceimage_width'], $element['#value']['imceimage_height'], $element['#value']['imceimage_alt'], $element['#value']['imceimage_title'], "imceimageimg-{$delta}-{$field_id}"), '1x1')) {
unset($element['imce_widget_container']['#attributes']['style']);
}
$element['imce_add'] = array(
'#type' => 'item',
'#value' => $field['widget']['label'] . ' [<a href="' . "javascript:imceImage.open('{$link}', '{$delta}-{$field_id}')" . '" class="imceimage-add">' . $links,
'#weight' => -1,
);
$element['imce_widget_container']['imceimage_path'] = array(
'#type' => 'hidden',
'#id' => $imceparams['url'],
'#default_value' => $element['#value']['imceimage_path'],
);
$element['imce_widget_container']['imceimage_width'] = array(
'#type' => 'hidden',
'#id' => $imceparams['width'],
'#default_value' => $element['#value']['imceimage_width'],
);
$element['imce_widget_container']['imceimage_height'] = array(
'#type' => 'hidden',
'#id' => $imceparams['height'],
'#default_value' => $element['#value']['imceimage_height'],
);
$element['imce_widget_container']['imce_view'] = array(
'#type' => 'item',
'#id' => "imceimageimg-{$delta}-{$field_id}",
'#value' => theme_imceimage_image($element['#value']['imceimage_path'], 100, 100, $element['#value']['imceimage_alt'], $element['#value']['imceimage_title'], "imceimageimg-{$delta}-{$field_id}"),
);
$element['imce_widget_container']['imceimage_alt'] = array(
'#type' => 'textfield',
'#id' => "imceimagealt-{$delta}-{$field_id}",
'#default_value' => isset($element['#value']['imceimage_alt']) ? check_plain($element['#value']['imceimage_alt']) : NULL,
'#autocomplete_path' => $element['#autocomplete_path'],
'#rows' => 1,
'#attributes' => array(
'class' => 'text',
),
'#title' => t('Alternate Text'),
'#description' => t('The alternate text (or ALT, or caption) for this image.'),
);
$element['imce_widget_container']['imceimage_title'] = array(
'#type' => 'textfield',
'#id' => "imceimagetitle-{$delta}-{$field_id}",
'#default_value' => isset($element['#value']['imceimage_title']) ? check_plain($element['#value']['imceimage_title']) : NULL,
'#autocomplete_path' => $element['#autocomplete_path'],
'#rows' => 1,
'#attributes' => array(
'class' => 'text',
),
'#title' => t('Title'),
'#description' => t('The title text for this image.'),
);
return $element;
}
function _imceimage_image_to_filepath($url) {
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$file_path = $doc_root . '/' . $url;
return $file_path;
}