You are here

image_hotspots.module in Image Hotspots 7.2

Same filename and directory in other branches
  1. 8 image_hotspots.module

Main function of module.

File

image_hotspots.module
View source
<?php

/**
 * @file
 * Main function of module.
 */
define('IMAGE_HOTSPOTS_STYLE', 'image_hotspot_default');
include_once 'includes/image_hotspots.db.inc';

/**
 * Implements hook_menu().
 */
function image_hotspots_menu() {
  $items['admin/config/media/image-hotspots'] = array(
    'title' => 'Hotspots Style',
    'description' => 'Select CSS settings for displayed Hotspots.',
    'page callback' => 'image_hotspots_style_config',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'image_hotspots_style_config',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'includes/image_hotspots.admin.inc',
    'file path' => drupal_get_path('module', 'image_hotspots'),
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function image_hotspots_theme() {
  return array(
    'image_hotspots_data' => array(
      'template' => 'image-hotspots-data',
      'path' => drupal_get_path('module', 'image_hotspots') . '/templates',
      'variables' => array(),
    ),
    'image_hotspot_element' => array(
      'template' => 'image-hotspots-element',
      'path' => drupal_get_path('module', 'image_hotspots') . '/templates',
      'variables' => array(),
    ),
  );
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function image_hotspots_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  if ($form['#field']['type'] == 'image') {
    $hotspot_fields = variable_get('image_hotspot_fields', array());
    $hotspot_field_settings = array();
    if (isset($hotspot_fields[$form['instance']['bundle']['#value']][$form['instance']['field_name']['#value']]['settings'])) {
      $hotspot_field_settings = $hotspot_fields[$form['instance']['bundle']['#value']][$form['instance']['field_name']['#value']]['settings'];
    }
    $form['instance']['settings']['image_hotspot'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use image hotspots'),
      '#weight' => 12,
      '#default_value' => isset($hotspot_fields[$form['instance']['bundle']['#value']][$form['instance']['field_name']['#value']]) ? TRUE : FALSE,
    );
    $form['instance']['settings']['image_hotspot_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Image hotspot settings'),
      '#states' => array(
        'invisible' => array(
          ':input[name="instance[settings][image_hotspot]"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
      '#weight' => 13,
    );
    $form['instance']['settings']['image_hotspot_settings']['max_resolution'] = array(
      '#type' => 'item',
      '#title' => t('Maximum hotspot resolution'),
      '#field_prefix' => '<div class="container-inline">',
      '#field_suffix' => '</div>',
      '#description' => t('The maximum allowed hotspot size expressed as WIDTHxHEIGHT (e.g. 640x480).'),
    );
    $form['instance']['settings']['image_hotspot_settings']['max_resolution']['image_hotspots_max_width'] = array(
      '#type' => 'textfield',
      '#field_suffix' => ' x ',
      '#size' => 10,
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#default_value' => isset($hotspot_field_settings['max_width']) ? $hotspot_field_settings['max_width'] : '',
    );
    $form['instance']['settings']['image_hotspot_settings']['max_resolution']['image_hotspots_max_height'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#default_value' => isset($hotspot_field_settings['max_height']) ? $hotspot_field_settings['max_height'] : '',
    );
    $form['instance']['settings']['image_hotspot_settings']['min_resolution'] = array(
      '#type' => 'item',
      '#title' => t('Minimum hotspot resolution'),
      '#field_prefix' => '<div class="container-inline">',
      '#field_suffix' => '</div>',
      '#description' => t('The minimum allowed hotpost size expressed as WIDTHxHEIGHT (e.g. 640x480).'),
    );
    $form['instance']['settings']['image_hotspot_settings']['min_resolution']['image_hotspots_min_width'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#default_value' => isset($hotspot_field_settings['min_width']) ? $hotspot_field_settings['min_width'] : '',
      '#field_suffix' => ' x ',
    );
    $form['instance']['settings']['image_hotspot_settings']['min_resolution']['image_hotspots_min_height'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#default_value' => isset($hotspot_field_settings['min_height']) ? $hotspot_field_settings['min_height'] : '',
    );
    $form['instance']['settings']['image_hotspot_settings']['linkable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Hotposts may have link to URL'),
      '#default_value' => isset($hotspot_field_settings['linkable']) ? $hotspot_field_settings['linkable'] : FALSE,
    );
    $form['#submit'][] = 'image_hotspots_form_field_ui_submit';
  }
}

/**
 * Save field name with hotspots.
 */
function image_hotspots_form_field_ui_submit($form, $form_state) {
  $hotspot_fields = variable_get('image_hotspot_fields', array());
  $content_type = $form_state['values']['instance']['bundle'];
  $field_name = $form_state['values']['instance']['field_name'];
  if ($form_state['values']['instance']['settings']['image_hotspot']) {
    $hotspot_settings = $form_state['values']['instance']['settings']['image_hotspot_settings'];
    $hotspot_fields[$content_type][$field_name] = array(
      'field' => $field_name,
      'settings' => array(
        'min_width' => $hotspot_settings['min_resolution']['image_hotspots_min_width'],
        'min_height' => $hotspot_settings['min_resolution']['image_hotspots_min_height'],
        'max_width' => $hotspot_settings['max_resolution']['image_hotspots_max_width'],
        'max_height' => $hotspot_settings['max_resolution']['image_hotspots_max_height'],
        'linkable' => $hotspot_settings['linkable'],
      ),
    );
    variable_set('image_hotspot_fields', $hotspot_fields);
  }
  elseif (isset($hotspot_fields[$content_type][$field_name])) {
    unset($hotspot_fields[$content_type][$field_name]);
    variable_set('image_hotspot_fields', $hotspot_fields);
  }
}

/**
 * Implements hook_image_default_styles().
 */
function image_hotspots_image_default_styles() {
  return array(
    IMAGE_HOTSPOTS_STYLE => array(
      'effects' => array(
        array(
          'name' => 'image_scale',
          'data' => array(
            'width' => 500,
            'height' => '',
            'upscale' => FALSE,
          ),
          'weight' => 0,
        ),
      ),
    ),
  );
}

/**
 * Implements hook_element_info_alter().
 */
function image_hotspots_element_info_alter(&$type) {
  $type['managed_file']['#after_build'][] = 'image_hotspots_process_form_element';
}

/**
 * After build function to process the hotspot widget on forms.
 */
function image_hotspots_process_form_element($element, &$form_state) {
  if (!isset($element['#field_name'])) {
    return $element;
  }
  $hotspot_fields = variable_get('image_hotspot_fields', array());
  if (!isset($hotspot_fields[$element['#bundle']][$element['#field_name']]) || !$element['fid']['#value']) {
    return $element;
  }
  $hotspot_settings = array();
  if (isset($hotspot_fields[$element['#bundle']][$element['#field_name']]['settings'])) {
    $hotspot_settings = $hotspot_fields[$element['#bundle']][$element['#field_name']]['settings'];
  }
  $hotspot_data = '';
  $exist_value = '';
  $id = explode('-', $element['#id']);
  $delta = array_pop($id);
  $flag_value = isset($form_state['values'][$element['#field_name']][$element['#language']][$delta]['image_hotspot_flag']) ? $form_state['values'][$element['#field_name']][$element['#language']][$delta]['image_hotspot_flag'] : 'create';
  if (isset($form_state['values'][$element['#field_name']][$element['#language']][$delta]['image_hotspot'])) {
    $exist_hotspots = $form_state['values'][$element['#field_name']][$element['#language']][$delta]['image_hotspot'];
  }
  else {
    $language = entity_language($element['#entity_type'], $element['#entity']);
    $exist_hotspots = image_hotspots_db_get_coordinates(array(
      $element['fid']['#value'],
    ), $language);
  }
  if (!empty($exist_hotspots)) {
    if (is_array($exist_hotspots)) {
      $exist_value = $exist_hotspots[0]->coordinates;
      $flag_value = 'update';
    }
    else {
      $exist_value = $exist_hotspots;
    }
    $exist_hotspots = drupal_json_decode($exist_value);
    $dimensions = array(
      'width' => $element['width']['#value'],
      'height' => $element['height']['#value'],
    );
    image_style_transform_dimensions(IMAGE_HOTSPOTS_STYLE, $dimensions);
    foreach ($exist_hotspots as $hotspot) {
      $hotspot['x1'] = $hotspot['x1'] ? $dimensions['width'] / (100 / $hotspot['x1']) : 0;
      $hotspot['y1'] = $hotspot['y1'] ? $dimensions['height'] / (100 / $hotspot['y1']) : 0;
      $hotspot['x2'] = $dimensions['width'] / (100 / $hotspot['x2']);
      $hotspot['y2'] = $dimensions['height'] / (100 / $hotspot['y2']);
      $hotspot['linkable'] = isset($hotspot_settings['linkable']) ? $hotspot_settings['linkable'] : FALSE;
      $hotspot_data .= theme('image_hotspots_data', $hotspot);
    }
  }
  $element['image_hotspot_flag'] = array(
    '#type' => 'hidden',
    '#value' => $flag_value,
    '#id' => $element['#id'] . '-image_hotspot_flag',
    '#name' => $element['#name'] . '[image_hotspot_flag]',
    '#weight' => 10,
  );
  $element_data = array(
    'id' => $element['#id'] . '-image_hotspot',
    'name' => $element['#name'] . '[image_hotspot]',
    'value' => $exist_value,
    'image' => image_hotspots_widget($element, $hotspot_settings),
    'hotspot_data' => $hotspot_data,
    'linkable' => $hotspot_settings['linkable'] ? $hotspot_settings['linkable'] : FALSE,
  );
  $image_hotspot = theme('image_hotspot_element', $element_data);
  $element['image_hotspot'] = array(
    '#type' => 'markup',
    '#markup' => $image_hotspot,
    '#weight' => 10,
  );
  $form_state['image_hotspot_fields'][$element['#field_name']] = $element['#field_name'];
  return $element;
}

/**
 * Create a widget to create and manage hotspots.
 *
 * @param array $element
 *   Form element of the image.
 * @param array $hotspot_settings
 *   Array with widget settings.
 */
function image_hotspots_widget(array &$element, array $hotspot_settings) {
  $output = '';
  if (isset($element['#file']->uri)) {
    $image_style = array(
      'style_name' => IMAGE_HOTSPOTS_STYLE,
      'path' => $element['#file']->uri,
      'width' => $element['width']['#value'],
      'height' => $element['height']['#value'],
    );
    $output .= theme_image_style($image_style);
    $attached_js = array(
      array(
        'data' => array(
          'image_hotspot_settings' => $hotspot_settings,
        ),
        'type' => 'setting',
      ),
      drupal_get_path('module', 'image_hotspots') . '/themes/jcrop/jquery.Jcrop.min.js',
      drupal_get_path('module', 'image_hotspots') . '/themes/jquery.json-2.4.min.js',
      drupal_get_path('module', 'image_hotspots') . '/themes/image_hotspots.js',
    );
    $attached_css = array(
      drupal_get_path('module', 'image_hotspots') . '/themes/jcrop/jquery.Jcrop.min.css',
      drupal_get_path('module', 'image_hotspots') . '/themes/image_hotspots.css',
    );
    $js = isset($element['#attached']['js']) ? $element['#attached']['js'] : array();
    $css = isset($element['#attached']['css']) ? $element['#attached']['css'] : array();
    $element['#attached']['js'] = array_merge($js, $attached_js);
    $element['#attached']['css'] = array_merge($css, $attached_css);
  }
  return $output;
}

/**
 * Implements hook_node_submit().
 */
function image_hotspots_node_submit($node, $form, &$form_state) {
  if (isset($form_state['image_hotspot_fields'])) {
    foreach ($form_state['image_hotspot_fields'] as $hotspot_field) {
      $images = current($form_state['values'][$hotspot_field]);
      foreach ($images as $image) {
        if (!$image['fid']) {
          continue;
        }
        $language = entity_language('node', $node);
        if ($image['image_hotspot'] == '') {
          image_hotspots_db_delete($image['fid'], $language);
        }
        elseif ($image['image_hotspot_flag'] == 'create') {
          image_hotspots_db_save($image['fid'], $language, $image['image_hotspot']);
        }
        elseif ($image['image_hotspot_flag'] == 'update') {
          image_hotspots_db_update($image['fid'], $language, $image['image_hotspot']);
        }
      }
    }
  }
}

/**
 * Implements hook_node_view().
 */
function image_hotspots_node_view($node, $view_mode, $langcode) {
  $hotspot_fields = variable_get('image_hotspot_fields', array());
  $images = array();
  if (!empty($hotspot_fields[$node->type])) {
    foreach ($hotspot_fields[$node->type] as $field_settings) {
      $field_name = $field_settings['field'];
      if (empty($node->content[$field_name]) || $node->content[$field_name]['#formatter'] != 'image') {
        continue;
      }
      $image_delta = element_children($node->content[$field_name]);
      foreach ($image_delta as $delta) {
        $fid = $node->content[$field_name][$delta]['#item']['fid'];
        $images[$fid]['fid'] = $fid;
        $node->content[$field_name][$delta]['#item']['attributes']['id'] = 'image-hotspot-' . $fid;
        $node->content[$field_name][$delta]['#item']['attributes']['class'][] = 'image-hotspot';
      }
    }
    if (!empty($images)) {
      $language = entity_language('node', $node);
      $hotspots_data = image_hotspots_db_get_coordinates(array_keys($images), $language);
      $hotspots = array();
      if (!empty($hotspots_data)) {
        $scripts = drupal_add_js();
        $settings = $scripts['settings']['data'];
        foreach ($hotspots_data as $image_hotspots) {
          foreach ($settings as $setting) {
            if (isset($setting['imageHotspots']['fid' . $image_hotspots->fid])) {
              continue 2;
            }
          }
          $hotspots['fid' . $image_hotspots->fid] = drupal_json_decode($image_hotspots->coordinates);
        }
        $hotspot_color = variable_get('image_hotspots_settings');
        if (!empty($hotspots)) {
          drupal_add_js(array(
            'imageHotspots' => $hotspots,
          ), 'setting');
          drupal_add_js(array(
            'imageHotspotsColor' => $hotspot_color,
          ), 'setting');
          drupal_add_js(drupal_get_path('module', 'image_hotspots') . '/themes/tipTip/jquery.tipTip.min.js');
          drupal_add_js(drupal_get_path('module', 'image_hotspots') . '/themes/image_hotspots.js');
          drupal_add_css(drupal_get_path('module', 'image_hotspots') . '/themes/tipTip/tipTip.css');
          drupal_add_css(drupal_get_path('module', 'image_hotspots') . '/themes/image_hotspots.css');
        }
      }
    }
  }
}

/**
 * Implements hook_field_delete_instance().
 */
function image_hotspots_field_delete_instance($instance) {
  $hotspot_fields = variable_get('image_hotspot_fields', array());
  if (isset($hotspot_fields[$instance['bundle']]['field_name'])) {
    unset($hotspot_fields[$instance['bundle']]['field_name']);
    variable_set('image_hotspot_fields');
  }
}

Functions

Constants

Namesort descending Description
IMAGE_HOTSPOTS_STYLE @file Main function of module.