t('Imagecache javascript crop'),
'path' => 'admin/settings/imagecrop',
'callback' => 'drupal_get_form',
'callback arguments' => 'imagecrop_settings',
'access' => $access_settings,
);
$items[] = array(
'path' => 'imagecrop/showcrop',
'callback' => 'imagecrop_showcrop',
'type' => MENU_CALLBACK,
'access' => $access_crop,
);
$items[] = array(
'path' => 'imagecrop/docrop',
'callback' => 'imagecrop_docrop',
'type' => MENU_CALLBACK,
'access' => $access_crop,
);
}
return $items;
}
/**
* Imagecrop settings page
*/
function imagecrop_settings() {
// hook into image module
if (module_exists('image')) {
$options_modules['image'] = t('Hook into image module');
}
// hook into node_images module
if (module_exists('node_images')) {
$result = db_query("SELECT name,value FROM {variable} WHERE name LIKE 'node_images_position_%'");
if (db_num_rows($result) > 0) {
drupal_set_message(t('When you want to enable support for the node_images module, please read the README that comes with the imagecrop module.'));
while ($row = db_fetch_object($result)) {
if (variable_get($row->name, 'hide') != 'hide') {
$explode = explode('_', $row->name);
$options_modules[$row->name] = t('Hook into node_images module for conten type @type', array('@type' => $explode[3]));
}
}
}
}
// hook into imagefield module
if (module_exists('imagefield')) {
$result = db_query("SELECT field_name,label FROM {node_field_instance} WHERE widget_type = 'image'");
while ($row = db_fetch_object($result)) {
$options_fields[$row->field_name] = t('Hook into imagefield %s', array('%s' => $row->label));
}
}
// show checkboxes if options are not empty
if (!empty($options_modules) || !empty($options_fields)) {
if (!empty($options_modules)) {
$form['imagecrop_modules'] = array(
'#type' => 'checkboxes',
'#title' => t('Hook into modules'),
'#default_value' => variable_get('imagecrop_modules', array()),
'#options' => $options_modules,
);
}
if (!empty($options_fields)) {
$form['imagecrop_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Hook into cck fields'),
'#default_value' => variable_get('imagecrop_fields', array()),
'#options' => $options_fields,
);
}
$form['array_filter'] = array('#type' => 'hidden');
}
else {
$form['no_modules_fields'] = array(
'#type' => 'item',
'#value' => t('No modules or fields are found to hook into.'),
);
}
// drupal message if no action is found with javascript_crop
if (imagecrop_action_exists() == FALSE) {
drupal_set_message(t('No preset is found with the javascript_crop action so far. If you want to take advantage of this module, you will need to create at least one preset with that action.'));
}
return system_settings_form($form);
}
/**
* Implementation of hook_cron().
* Delete all references in imagecrop table when
* a) file doesn't exist anymore.
* b) when preset has been deleted.
* c) when javascrip_crop action is removed from a preset.
*/
function imagecrop_cron() {
// get all files which do not exist anymore from the files table
$result = db_query("SELECT ic.fid,ic.presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT fid FROM {files} f WHERE ic.fid = f.fid) AND ic.reference = 'files'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'files');
}
// get all files which do not exist anymore from the node_images table
if (module_exists('node_images')) {
$result = db_query("SELECT ic.fid,presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT id FROM {node_images} ni WHERE ic.fid = ni.id) AND ic.reference = 'node_images'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'node_images');
}
}
/*
* Get all records
* a) from presets which do not exist anymore.
* b) and/or from presets with no imagecrop_javascript action anymore.
*/
// files table
$result = db_query("SELECT ic.fid,ic.presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT presetid FROM {imagecache_action} ia where ic.presetid = ia.presetid AND ia.action = 'imagecrop_javascript') AND ic.reference = 'files'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'files');
}
// node_images table
if (module_exists('node_images')) {
$result = db_query("SELECT ic.fid,ic.presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT presetid FROM {imagecache_action} ia where ic.presetid = ia.presetid AND ia.action = 'imagecrop_javascript') AND ic.reference = 'node_images'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'node_images');
}
}
if (!empty($records)) {
while (list($key, $val) = each($records)) {
db_query("DELETE FROM {imagecrop} WHERE fid=%d AND presetid=%d AND reference = '%s'", $val['fid'], $val['presetid'], $val['reference']);
}
}
}
/**
* Implementation of hook_imagecache_actions().
*/
function imagecrop_imagecache_actions() {
$actions = array(
'imagecrop_javascript' => array(
'name' => 'Javascript crop',
'description' => 'Create a crop with a javascript toolbox.',
'file' => 'imagecrop_actions.inc',
),
);
return $actions;
}
/**
* Helper function to check if a preset exists with the imagecrop_javascript action.
* Needed to determine if we have to display our javascript crop link.
*
* @return true or false
*/
function imagecrop_action_exists() {
$result = db_result(db_query("SELECT actionid FROM {imagecache_action} WHERE action = 'imagecrop_javascript'"));
return $result;
}
/**
* Implementation of hook_form_alter().
* Hook into several existing image modules/fields.
*/
function imagecrop_form_alter($form_id, &$form) {
// do we have presets with javascript_crop ?
$exists = imagecrop_action_exists();
// user access
$access = user_access('crop images with toolbox');
// build array with available modules/fields
$modules = variable_get('imagecrop_modules', array());
$fields = variable_get('imagecrop_fields', array());
$hooks = array_merge($modules, $fields);
// hook into imagefield module
if (module_exists('imagefield') && $exists != FALSE && $access) {
$formfield = imagecrop_find_imagefields($form, $fields);
if ($formfield != FALSE) {
$count = count($formfield);
for ($i=0;$i<$count;$i++) {
$a = 0;
while ($form[$formfield[$i]][$a]['fid']['#value']) {
if (!empty($form[$formfield[$i]][$a]['fid']['#value'])) {
$descr = $form[$formfield[$i]][$a]['description']['#value'] .'
'. imagecrop_linkitem($form[$formfield[$i]][$a]['fid']['#value']);
$form[$formfield[$i]][$a]['description']['#value'] = $descr;
}
$a++;
}
}
}
}
// hook into image module
if (module_exists('image') && $exists != FALSE && $access && in_array('image', $hooks) && isset($form['images']['thumbnail'])) {
// it's anonying we have to make a database call to get the right fid.
$fid = db_result(db_query("SELECT fid FROM {files} WHERE nid=%d AND filename = '_original' AND filepath='%s'", $form['nid']['#value'], $form['images']['_original']['#default_value']));
$form['croptab'] = imagecrop_formitem($fid, -10);
}
// hook into node_images module
if (module_exists('node_images') && $form_id == '_node_images_list' && $exists != FALSE && $access) {
$type = $form['#parameters'][1]->type;
if (variable_get('node_images_position_'. $type, 'hide') != 'hide' && in_array('node_images_position_'. $type, $hooks)) {
$form['imagecrop'] = array('#type' => 'hidden', '#value' => '1');
}
}
}
/**
* Helper function to add form item
*
* @return $form form markup
*/
function imagecrop_formitem($fid, $weight = 0) {
if (!module_exists('thickbox')) {
$form = array(
'#type' => 'item',
'#value' => ''. t('Javascript crop') .'',
'#weight' => $weight,
);
}
else {
$form = array(
'#type' => 'item',
'#value' => ''. t('Javascript crop') .'',
'#weight' => $weight,
);
}
return $form;
}
/**
* Helper function to add click link
*
* @return $form form markup
*/
function imagecrop_linkitem($fid) {
if (!module_exists('thickbox'))
return ''. t('Javascript crop') .'';
else
return ''. t('Javascript crop') .'';
}
/**
* Helper function to search for a cck field image.
* Temporary images not supported at this point.
*
* @param $form complete form object
* @param $fields all available fields from settings
* @return ckk imagefields array or false
*/
function imagecrop_find_imagefields($form, $fields) {
$temp_path = file_directory_temp();
$count = count($fields);
$return = FALSE;
for ($i=0;$i<$count;$i++) {
$temppath = strpos($form[$fields[$i]][0]['filepath']['#value'], $temp_path);
$deleteflag = $form[$fields[$i]][0]['flags']['delete']['#value'];
if (isset($form[$fields[$i]]) && $temppath === FALSE && $deleteflag != 1) {
$return[] = $fields[$i];
}
}
return $return;
}
/**
* Callback with javascript crop.
*
* @param $fid id of file
* @param $presetid id of preset
*/
function imagecrop_docrop($fid, $presetid, $module = '') {
if (module_exists('jquery_interface')) $javascript_library = 'jquery_interface';
if (module_exists('jquery_ui')) $javascript_library = 'jquery_ui';
imagecrop_markup(TRUE, TRUE, $javascript_library);
if (imagecrop_action_exists() == TRUE) {
$presets = return_presets($presetid);
$file = create_image_object($fid, $presetid, $module);
if ($file != FALSE) {
$size_warning = FALSE;
// get size of temporary image
$size = getimagesize($file->dst);
$width = $size[0];
$height = $size[1];
// return warning message if crop toolbox is too big and not resizable.
if (($width < $file->crop_width || $height < $file->crop_height) && $file->resizable == 0) {
$size_warning = FALSE;
}
// add jquery interface or jquery ui
if ($javascript_library == 'jquery_interface') jquery_interface_add();
if ($javascript_library == 'jquery_ui') {
if ($file->resizable)
jquery_ui_add(array('ui.resizable', 'ui.draggable', 'effects.scale'));
else
jquery_ui_add(array('ui.draggable'));
}
// output
if ($size_warning == FALSE) {
$url = file_create_url($file->dst) .'?time='. time();
$output .= '
', '#suffix' => ' | ', '#type' => 'submit', '#value' => t('Create crop')); $form['buttons']['scaling'] = array('#prefix' => '', '#suffix' => ' | ', '#type' => 'select', '#default_value' => $scale, '#options' => imagecrop_scale_options($width, $crop_width)); $form['button']['scaledown'] = array('#prefix' => '', '#suffix' => ' |