You are here

iss.module in Image Style Selector 7.2

Same filename and directory in other branches
  1. 7 iss.module

File

iss.module
View source
<?php

/**
 * @file
 * Image Style Selector field.
 *
 * The Image Style Selector (ISS) field allows users to select the image style
 * that should be applied on the image. The instance settings of the ISS field
 * allows to specify which image styles are available for the user and to which
 * image field the image style should be applied.
 */
require_once 'iss.admin.inc';

/**
 * Implements hook_help().
 */
function iss_help($path, $arg) {
  switch ($path) {
    case 'admin/help#iss':

      // Construct the path of this module's readme file.
      $path_readme = drupal_get_path('module', 'iss') . '/README.txt';

      // If the readme is available, return the contents.
      if (file_exists($path_readme)) {
        $readme = file_get_contents($path_readme);
        return '<h1>README.txt</h1><pre>' . check_plain($readme) . '</pre>';
      }
  }
}

/**
 * Implements hook_menu().
 */
function iss_menu() {
  $items = array();
  $items['admin/config/media/iss'] = array(
    'title' => 'Image Style Selector',
    'description' => 'Configure Image Style Selector.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'iss_settings_form',
    ),
    'access arguments' => array(
      'administer iss settings',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'iss.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function iss_permission() {
  return array(
    'use iss' => array(
      'title' => t('Select image styles'),
      'description' => t('Select image styles using Image Style Selector.'),
      'restrict access' => FALSE,
    ),
    'administer iss settings' => array(
      'title' => t('Administer Image Style Selector'),
      'description' => t('Access and change the global Manual Crop settings.'),
      'restrict access' => FALSE,
    ),
  );
}

/**
 * Implements
 */
function iss_form_image_style_form_alter(&$form, &$form_state, $form_id) {
  $form['iss_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('ISS enabled'),
    '#description' => t('When this option is chosen, this style effects will be ignored and it will be changed into another styles.'),
    '#default_value' => !empty($form_state['image_style']['iss_enabled']),
    '#element_validate' => array(
      'iss_image_style_iss_enabled_validate',
    ),
  );
}

/**
 * Validation callback.
 */
function iss_image_style_iss_enabled_validate($element, &$form_state) {

  // This allows 'iss_enabled' property to be saved into image_styles table
  // by image_style_form_submit() submit handler.
  $form_state['image_style']['iss_enabled'] = $form_state['values']['iss_enabled'];
}

/**
 * Implements hook_field_widget_form_alter().
 */
function iss_field_widget_form_alter(&$element, &$form_state, $context) {
  $field_type = $context['field']['type'];

  // We are handling only image fields right now.
  if ($field_type != 'image') {
    return;
  }

  // Fields "insert" enabled have choice of image style provided by insert module already.
  $widget_settings = $context['instance']['widget']['settings'];
  if (module_exists('insert') && isset($widget_settings['insert']) && $widget_settings['insert'] == 1) {
    return;
  }

  // Check if this field is iss enabled.
  if (empty($context['instance']['settings']['iss']['iss_enabled'])) {
    return;
  }
  if (!user_access('use iss')) {
    return;
  }
  $iss_config = $context['instance']['settings']['iss']['config'];
  $iss_config_source_styles = $iss_config['source_styles'];
  $iss_config_target_styles = $iss_config['target_styles'];
  $source_styles = array_intersect_key(iss_image_styles('source'), array_flip($iss_config_source_styles));
  $target_styles = array_intersect_key(iss_image_styles('target'), array_flip($iss_config_target_styles));
  foreach (element_children($element) as $key) {

    // If there is no file, don't render element.
    if (empty($element[$key]['#default_value']['fid'])) {
      continue;
    }
    if (count($source_styles) > 1) {
      $element[$key]['iss']['source'] = array(
        '#type' => 'select',
        '#title' => t('Source styles'),
        '#options' => $source_styles,
      );
    }
    $field_parents = $element[$key]['#field_parents'];
    $field_first_parent = array_shift($field_parents);
    if (!empty($field_parents)) {
      $name = $field_first_parent . '[' . implode('][', $field_parents) . '][' . $element[$key]['#field_name'] . '][' . $element[$key]['#language'] . '][' . $key . '][iss][sources]';
    }
    else {
      $name = $element[$key]['#field_name'] . '[' . $element[$key]['#language'] . '][' . $key . '][iss][source]';
    }
    foreach ($source_styles as $source_style => $source_style_label) {
      $element[$key]['iss']['iss_styles'][$source_style] = array(
        '#type' => 'select',
        '#title' => t('Target styles'),
        '#options' => $target_styles,
        '#states' => array(
          'visible' => array(
            "select[name=\"{$name}\"]" => array(
              'value' => $source_style,
            ),
          ),
        ),
      );
      if (!empty($context['items'][$key]['fid'])) {
        $style = db_query('SELECT iss.target_style FROM iss_styles iss WHERE iss.fid = :fid AND iss.source_style = :source', array(
          ':fid' => $context['items'][$key]['fid'],
          ':source' => $source_style,
        ))
          ->fetchField();
        if ($style) {
          $element[$key]['iss']['iss_styles'][$source_style]['#default_value'] = $style;
        }
      }
      foreach ($element[$key]['iss']['iss_styles'][$source_style]['#options'] as $machine_name => &$label) {
        $label = t($label);
      }

      // Change label of the source style in target styles.
      if (isset($element[$key]['iss']['iss_styles'][$source_style]['#options'][$source_style])) {
        $element[$key]['iss']['iss_styles'][$source_style]['#options'][$source_style] .= ' (' . t("default") . ')';
        if (empty($element[$key]['iss']['iss_styles'][$source_style]['#default_value'])) {
          $element[$key]['iss']['iss_styles'][$source_style]['#default_value'] = $source_style;
        }
      }
    }
  }
}

/**
 * Implements hook_field_attach_submit().
 */
function iss_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  $instances = field_info_instances($entity_type, $form['#bundle']);
  foreach ($instances as $instance) {
    $field_name = $instance['field_name'];
    $field = field_info_field($field_name);
    $available_languages = field_available_languages($entity_type, $field);
    $languages = _field_language_suggestion($available_languages, NULL, $field_name);
    foreach ($languages as $langcode) {
      $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
      foreach ($items as $item) {
        if (!empty($item['iss'])) {
          $fid = $item['fid'];
          $iss_data = $item['iss'];
          $file = file_load($fid);
          $file->iss_styles = $iss_data['iss_styles'];
          file_save($file);
        }
      }
    }
  }
}

/**
 * Implements hook_preprocess_image().
 */
function iss_preprocess_image(&$vars) {
  if (variable_get('iss_apply_method', 'default') == 'preprocess_images' && isset($vars['style_name'])) {

    // TODO: How to handle another stream wrappers?

    //$wrappers = file_get_stream_wrappers();

    //foreach ($wrappers as $wrapper_name => $wrapper_info) {

    //  $wrapper_path = file_stream_wrapper_get_instance_by_scheme($wrapper_name)->getDirectoryPath();

    //}

    // For now we only handling public schema.
    $old_style = $vars['style_name'];
    $schema = 'public';
    $old_style_part = "/styles/{$vars['style_name']}/{$schema}/";
    $url_parsed = drupal_parse_url($vars['path']);
    $path_array = explode($old_style_part, $url_parsed['path']);
    if (isset($path_array[1])) {
      $original_image_uri = "{$schema}://" . $path_array[1];
      $new_style = db_query('SELECT iss.target_style FROM iss_styles iss LEFT JOIN file_managed fm ON fm.fid = iss.fid WHERE fm.uri = :uri AND iss.source_style = :style', array(
        ':style' => $old_style,
        ':uri' => $original_image_uri,
      ))
        ->fetchField();
      if ($new_style) {
        $vars['iss_source_style_name'] = $old_style;
        $vars['style_name'] = $new_style;
        $new_style_path = image_style_url($new_style, $original_image_uri);
        $vars['path'] = $new_style_path;
      }
    }
  }
}

/**
 * Get all styles that are ISS enabled.
 *
 * @param string $filter
 *   Possible values are: "all", "source" and "target"
 *
 * @return array
 */
function iss_image_styles($filter = 'all') {
  $styles = image_styles();
  $styles = array_filter($styles, function ($v) {
    return isset($v['iss_enabled']) && $v['iss_enabled'] == 1;
  });
  array_walk($styles, function (&$v, $k) {
    $v = $v['label'];
  });
  drupal_alter('iss_image_styles', $styles, $filter);
  return $styles;
}

/**
 * Implements hook_file_delete().
 */
function iss_file_delete($file) {
  db_delete('iss_styles')
    ->condition('fid', $file->fid)
    ->execute();
}
function iss_file_insert($file) {
  if (!empty($file->iss_styles)) {
    iss_save_iss_styles($file->fid, $file->iss_styles);
  }
}
function iss_file_update($file) {
  if (!empty($file->iss_styles)) {
    iss_save_iss_styles($file->fid, $file->iss_styles);
  }
}

/**
 * Helper function.
 */
function iss_save_iss_styles($fid, $iss_styles) {
  db_delete('iss_styles')
    ->condition('fid', $fid, '=')
    ->execute();
  foreach ($iss_styles as $source => $target) {
    if ($source != $target) {
      $record = array(
        'fid' => $fid,
        'vid' => 0,
        'source_style' => $source,
        'target_style' => $target,
      );
      drupal_write_record('iss_styles', $record);
    }
  }
}

/**
 * Implements hook_file_load().
 */
function iss_file_load($files) {
  foreach ($files as $fid => &$file) {
    if ($file->type == 'image') {
      $file->iss_styles = array();
      $query = db_query("SELECT iss.source_style, iss.target_style FROM iss_styles iss WHERE iss.fid = :fid", array(
        ':fid' => $fid,
      ));
      while ($result = $query
        ->fetchAssoc()) {
        $file->iss_styles[$result['source_style']] = $result['target_style'];
      }
    }
  }
}

/**
 * Implements hook_preprocess_field().
 */
function iss_preprocess_field(&$vars) {
  if (variable_get('iss_apply_method', 'default') == 'default') {
    $element = $vars['element'];
    $items =& $vars['items'];
    if ($element['#field_type'] == 'image') {
      foreach ($items as $delta => &$item) {
        $file = $item['#item'];
        $image_style =& $item['#image_style'];
        if (!empty($file['iss_styles'])) {
          foreach ($file['iss_styles'] as $source => $target) {
            if ($image_style == $source) {
              $image_style = $target;
            }
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
iss_field_attach_submit Implements hook_field_attach_submit().
iss_field_widget_form_alter Implements hook_field_widget_form_alter().
iss_file_delete Implements hook_file_delete().
iss_file_insert
iss_file_load Implements hook_file_load().
iss_file_update
iss_form_image_style_form_alter Implements
iss_help Implements hook_help().
iss_image_styles Get all styles that are ISS enabled.
iss_image_style_iss_enabled_validate Validation callback.
iss_menu Implements hook_menu().
iss_permission Implements hook_permission().
iss_preprocess_field Implements hook_preprocess_field().
iss_preprocess_image Implements hook_preprocess_image().
iss_save_iss_styles Helper function.