You are here

imceimage.module in Imce CCK Image 6

Same filename and directory in other branches
  1. 6.2 imceimage.module

File

imceimage.module
View source
<?php

/**
 * Implementation of hook_menu
 */
function imceimage_menu() {
  $items = array();
  $items['imceimage/verify'] = array(
    'title' => 'imceimage_verify',
    'page callback' => 'imceimage_verify',
    'access callback' => true,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Verify that the file is a valid image
 */
function imceimage_verify() {
  $args = func_get_args();

  //convert the url into filepath
  $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);
}

/**
 * Declare information about a field type hook_field_info()
 *
 * @return
 *   An array keyed by field type name. Each element of the array is an associative
 *   array with these keys and values:
 *   - "label": The human-readable label for the field type.
 */
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,
      ),
    ),
  );
}

/**
 * hook_field_settings()
 **/
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;
  }
}

/**
 * hook_field() implementation
 **/
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'] != '') {

            //check the file for a valid image..
            $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;
  }
}

/**
 * implementation of hook_content_is_empty..
 **/
function imceimage_content_is_empty($item, $field) {
  return $item['imceimage_path'] == '' && $item['imceimage_width'] == '' && $item['imceimage_height'] == '' && $item['imceimage_alt'] == '' && $item['imceimage_title'] == '';
}

/**
* Declare information about a formatter.
*
* @return
*   An array keyed by formatter name. Each element of the array is an associative
*   array with these keys and values:
*   - "label": The human-readable label for the formatter.
*   - "field types": An array of field type names that can be displayed using
*     this formatter.
*/
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;
}

/**
 * Declare information about a widget.
 *
 * @return
 *   An array keyed by widget name. Each element of the array is an associative
 *   array with these keys and values:
 *   - "label": The human-readable label for the widget.
 *   - "field types": An array of field type names that can be edited using
 *     this widget.
 */
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,
      ),
    ),
  );
}

/**
 * Handle the parameters for a widget.
 *
 * @param $op
 *   The operation to be performed. Possible values:
 *   - "form": Display the widget settings form.
 *   - "validate": Check the widget settings form for errors.
 *   - "save": Declare which pieces of information to save back to the database.
 *   - "callbacks": Describe the widget's behaviour regarding hook_widget operations.
 * @param $widget
 *   The widget on which the operation is to be performed.
 * @return
 *   This varies depending on the operation.
 *   - "form": an array of form elements to add to the settings page.
 *   - "validate": no return value. Use form_set_error().
 *   - "save": an array of names of form elements to be saved in the database.
 *   - "callbacks": an array describing the widget's behaviour regarding hook_widget
 *     operations. The array is keyed by hook_widget operations ('form', 'validate'...)
 *     and has the following possible values :
 *       CONTENT_CALLBACK_NONE     : do nothing for this operation
 *       CONTENT_CALLBACK_CUSTOM   : use the behaviour in hook_widget(operation)
 *       CONTENT_CALLBACK_DEFAULT  : use content.module's default bahaviour
 *     Note : currently only the 'default value' operation implements this feature.
 *     All other widget operation implemented by the module _will_ be executed
 *     no matter what.
 */
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,
      );
  }
}

/**
 * hook_widget
 * This does not do anything much and instead just uses the element type
 * imceimage defined in hook_elements - also include the javscript file
 * for integrating with imce module.
 */
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;
}

// hook_views_api
function imceimage_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Theme an image
 */
function theme_imceimage_image($path, $width = '', $height = '', $alt = '', $title = '', $id = '') {

  //if is a blank image - deliver a blank image
  if ($path == '' || empty($path)) {
    $path = base_path() . drupal_get_path('module', 'imceimage') . '/includes/images/1x1.png';
  }

  //make sure that this is a valid image..
  $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;
}

/**
 * displays the image as a link. given the class imceimage-link
 **/
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);
}

/**
 * displays the image as a link. given the class imceimage-link
 **/
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',
    ),
  ));
}

/**
 * displays the image as a url
 */
function theme_imceimage_formatter_url($element) {
  $item = $element['#item'];
  return $item['imceimage_path'];
}

/**
 * displays the image as an img
 */
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 '';
  }
}

/**
 * displays the image as an img with caption
 */
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 '';
  }
}

//begin imagecache support
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 '';
  }

  // $file_path_url = $server . $file_directory . $subdir . $filename;
  // $server can be different when there are multi-sites
  $path_parts = parse_url($item['imceimage_path']);

  // $file_directory can be different for public / private download
  $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);

  //"subdir/subdir2/imageabc.jpg"
  $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! -->';
}

// end Imagecache addition
function theme_imceimage($element) {
  return $element['#children'];
}

/**
 * hook_theme - that works together with hook_formatter and hook_elements
 * to provide the default theme and also a theme to display the
 * image as a link.
 **/
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;
}

/**
 * hook_elements - implementation defines name of the field type and
 * the items that are stored against this field.
 **/
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;
}

/**
 * process handler for hook_elements renders the form element for
 * imceimage.
 */
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'];

  // Workaround to have all the "visible" fields in a fieldset
  // and making possible to save them at the same time
  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;
}

/**
 * helper function to find out file location from an URL.
 */
function _imceimage_image_to_filepath($url) {
  $doc_root = $_SERVER['DOCUMENT_ROOT'];
  $file_path = $doc_root . '/' . $url;
  return $file_path;
}

Functions

Namesort descending Description
imceimage_content_is_empty implementation of hook_content_is_empty..
imceimage_elements hook_elements - implementation defines name of the field type and the items that are stored against this field.
imceimage_field hook_field() implementation
imceimage_field_formatter
imceimage_field_formatter_info Declare information about a formatter.
imceimage_field_info Declare information about a field type hook_field_info()
imceimage_field_settings hook_field_settings()
imceimage_menu Implementation of hook_menu
imceimage_process process handler for hook_elements renders the form element for imceimage.
imceimage_theme hook_theme - that works together with hook_formatter and hook_elements to provide the default theme and also a theme to display the image as a link.
imceimage_verify Verify that the file is a valid image
imceimage_views_api
imceimage_widget hook_widget This does not do anything much and instead just uses the element type imceimage defined in hook_elements - also include the javscript file for integrating with imce module.
imceimage_widget_info Declare information about a widget.
imceimage_widget_settings Handle the parameters for a widget.
theme_imceimage
theme_imceimage_formatter
theme_imceimage_formatter_default displays the image as an img
theme_imceimage_formatter_link displays the image as a link. given the class imceimage-link
theme_imceimage_formatter_thumb displays the image as a link. given the class imceimage-link
theme_imceimage_formatter_url displays the image as a url
theme_imceimage_formatter_with_caption displays the image as an img with caption
theme_imceimage_image Theme an image
_imceimage_get_thumb
_imceimage_image_to_filepath helper function to find out file location from an URL.