View source
<?php
require_once 'iss.admin.inc';
function iss_help($path, $arg) {
switch ($path) {
case 'admin/help#iss':
$path_readme = drupal_get_path('module', 'iss') . '/README.txt';
if (file_exists($path_readme)) {
$readme = file_get_contents($path_readme);
return '<h1>README.txt</h1><pre>' . check_plain($readme) . '</pre>';
}
}
}
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;
}
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,
),
);
}
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',
),
);
}
function iss_image_style_iss_enabled_validate($element, &$form_state) {
$form_state['image_style']['iss_enabled'] = $form_state['values']['iss_enabled'];
}
function iss_field_widget_form_alter(&$element, &$form_state, $context) {
$field_type = $context['field']['type'];
if ($field_type != 'image') {
return;
}
$widget_settings = $context['instance']['widget']['settings'];
if (module_exists('insert') && isset($widget_settings['insert']) && $widget_settings['insert'] == 1) {
return;
}
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 (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);
}
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;
}
}
}
}
}
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);
}
}
}
}
}
function iss_preprocess_image(&$vars) {
if (variable_get('iss_apply_method', 'default') == 'preprocess_images' && isset($vars['style_name'])) {
$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;
}
}
}
}
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;
}
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);
}
}
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);
}
}
}
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'];
}
}
}
}
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;
}
}
}
}
}
}
}