View source
<?php
function vsf_manualcrop_init() {
if (current_path() == 'admin/visual_select_file') {
$path = drupal_get_path('module', 'vsf_manualcrop');
drupal_add_js($path . '/vsf_manualcrop.js');
}
elseif (preg_match('#^file/\\d+/vsf_manualcrop/\\w+#', current_path())) {
_visual_select_file_suppress_xbars('vsf_manualcrop');
}
}
function vsf_manualcrop_menu() {
$items['file/%file/vsf_manualcrop/%image_style'] = array(
'title' => 'Manual crop',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vsf_manualcrop_image_form',
1,
3,
),
'access callback' => 'vsf_manualcrop_image_access',
'access arguments' => array(
1,
3,
),
);
return $items;
}
function vsf_manualcrop_image_access($file, $style) {
return user_access('administer nodes') || user_access('visually select files');
}
function vsf_manualcrop_image_form($form, &$form_state, $file, $style) {
$form_state['file'] = $file;
$form_state['style'] = $style;
$style_name = $style['name'];
$form_state['vsf_from'] = @json_decode(@$_GET['vsf_from'], TRUE) ?: array();
$form['preview'] = array(
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#path' => $file->uri,
);
$settings = array(
'manualcrop_enable' => 1,
'manualcrop_thumblist' => 0,
'manualcrop_inline_crop' => 0,
'manualcrop_crop_info' => 1,
'manualcrop_instant_preview' => 0,
'manualcrop_instant_crop' => 0,
'manualcrop_default_crop_area' => 1,
'manualcrop_styles_mode' => 'exclude',
'manualcrop_styles_list' => array(
rand(),
),
'manualcrop_require_cropping' => array(
$style_name => $style_name,
),
);
$form_state['manualcrop_data'] = array();
if (function_exists('manualcrop_croptool_prepare_form')) {
$js_identifier = 'vsf_image';
manualcrop_croptool_prepare_form($form, $form_state, $js_identifier, $settings);
manualcrop_croptool_process($form, $form_state, $js_identifier, $file, NULL, $settings, 'preview');
}
else {
manualcrop_croptool_process($form, $form_state, $form, $file, $settings);
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#href' => 'node',
'#title' => t('Go back'),
'#attributes' => array(
'onclick' => 'history.go(-1); return false',
),
);
$form['#submit'][] = 'vsf_manualcrop_image_form_submit';
return $form;
}
function vsf_manualcrop_image_form_submit($form, &$form_state) {
$style = $form_state['style'];
$style_name = $style['name'];
manualcrop_croptool_submit($form, $form_state);
drupal_get_messages('status', TRUE);
$form_state['redirect'] = array(
'admin/visual_select_file',
array(
'query' => $form_state['vsf_from'],
'fragment' => 'modal',
),
);
}
function vsf_manualcrop_admin_paths() {
return array(
'file/*/vsf_manualcrop/*' => TRUE,
);
}
function vsf_manualcrop_visual_select_file_results_alter(&$results, $context) {
if (!isset($_GET['ckeditor'])) {
return;
}
$styles = array_keys(manualcrop_styles_with_crop());
$results['crop_styles'] = $styles;
$fids = array_keys($results['results']);
if (!$fids) {
return;
}
$query = db_query('SELECT fid, style_name FROM {manualcrop} WHERE fid IN (:fids)', array(
':fids' => $fids,
));
$crops = array();
foreach ($query as $crop) {
$crops[$crop->fid][] = $crop->style_name;
}
foreach ($results['results'] as $fid => &$file) {
$file[4] = @$crops[$fid] ?: array();
unset($file);
}
}
function vsf_manualcrop_visual_select_file_formatter_options_alter(&$options, &$context) {
if (!empty($_GET['vsf_manualcrop'])) {
$context['default_formatter'] = (string) $_GET['vsf_manualcrop'];
}
}
function vsf_manualcrop_views_pre_render($view) {
$query = drupal_get_query_parameters($_REQUEST);
$data['vsf_manualcrop']['query'] = $query;
drupal_add_js($data, array(
'type' => 'setting',
));
}