diff --git a/admin/configuration.php b/admin/configuration.php index 2258dab99..4258c3437 100644 --- a/admin/configuration.php +++ b/admin/configuration.php @@ -175,32 +175,7 @@ if (isset($_POST['submit'])) } case 'sizes' : { - $fields = array( - 'original_resize', - 'original_resize_maxwidth', - 'original_resize_maxheight', - 'original_resize_quality', - ); - - $updates = array(); - - foreach ($fields as $field) - { - $value = !empty($_POST[$field]) ? $_POST[$field] : null; - $form_values[$field] = $value; - $updates[$field] = $value; - } - - save_upload_form_config($updates, $page['errors']); - - if (count($page['errors']) == 0) - { - array_push( - $page['infos'], - l10n('Your configuration settings are saved') - ); - } - + include(PHPWG_ROOT_PATH.'admin/include/configuration_sizes_process.inc.php'); break; } case 'comments' : @@ -428,69 +403,82 @@ switch ($page['section']) } case 'sizes' : { - $template->assign( - 'sizes', - array( - 'original_resize_maxwidth' => $conf['original_resize_maxwidth'], - 'original_resize_maxheight' => $conf['original_resize_maxheight'], - 'original_resize_quality' => $conf['original_resize_quality'], - ) - ); - - foreach ($sizes_checkboxes as $checkbox) + // we only load the derivatives if it was not already loaded: it occurs + // when submitting the form and an error remains + if (!isset($page['sizes_loaded_in_tpl'])) { - $template->append( + $template->assign( 'sizes', array( - $checkbox => $conf[$checkbox] - ), - true + 'original_resize_maxwidth' => $conf['original_resize_maxwidth'], + 'original_resize_maxheight' => $conf['original_resize_maxheight'], + 'original_resize_quality' => $conf['original_resize_quality'], + ) ); - } - - // derivaties = multiple size - $enabled = ImageStdParams::get_defined_type_map(); - $disabled = @unserialize(@$conf['disabled_derivatives']); - if ($disabled === false) - { - $disabled = array(); - } - - $tpl_vars = array(); - foreach(ImageStdParams::get_all_types() as $type) - { - $tpl_var = array(); - $tpl_var['must_square'] = ($type==IMG_SQUARE ? true : false); - $tpl_var['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; - - if ($params=@$enabled[$type]) + foreach ($sizes_checkboxes as $checkbox) { - $tpl_var['enabled']=true; - } - else - { - $tpl_var['enabled']=false; - $params=@$disabled[$type]; + $template->append( + 'sizes', + array( + $checkbox => $conf[$checkbox] + ), + true + ); } - if ($params) + // derivatives = multiple size + $enabled = ImageStdParams::get_defined_type_map(); + $disabled = @unserialize(@$conf['disabled_derivatives']); + if ($disabled === false) { - list($tpl_var['w'],$tpl_var['h']) = $params->sizing->ideal_size; - if ( ($tpl_var['crop'] = round(100*$params->sizing->max_crop)) > 0) + $disabled = array(); + } + + $common_quality = 50; + + $tpl_vars = array(); + foreach(ImageStdParams::get_all_types() as $type) + { + $tpl_var = array(); + + $tpl_var['must_square'] = ($type==IMG_SQUARE ? true : false); + $tpl_var['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; + + if ($params = @$enabled[$type]) { - list($tpl_var['minw'],$tpl_var['minh']) = $params->sizing->min_size; + $tpl_var['enabled'] = true; } else { - $tpl_var['minw'] = $tpl_var['minh'] = ""; + $tpl_var['enabled']=false; + $params=@$disabled[$type]; } - $tpl_var['sharpen'] = $params->sharpen; - $tpl_var['quality'] = $params->quality; + + if ($params) + { + list($tpl_var['w'],$tpl_var['h']) = $params->sizing->ideal_size; + if ( ($tpl_var['crop'] = round(100*$params->sizing->max_crop)) > 0) + { + list($tpl_var['minw'],$tpl_var['minh']) = $params->sizing->min_size; + } + else + { + $tpl_var['minw'] = $tpl_var['minh'] = ""; + } + $tpl_var['sharpen'] = $params->sharpen; + $tpl_var['quality'] = $params->quality; + + if ($params->quality > $common_quality and $tpl_var['enabled']) + { + $common_quality = $params->quality; + } + } + $tpl_vars[$type]=$tpl_var; } - $tpl_vars[$type]=$tpl_var; + $template->assign('derivatives', $tpl_vars); + $template->assign('resize_quality', $common_quality); } - $template->assign('derivatives', $tpl_vars); break; } diff --git a/admin/include/configuration_sizes_process.inc.php b/admin/include/configuration_sizes_process.inc.php new file mode 100644 index 000000000..603103fe1 --- /dev/null +++ b/admin/include/configuration_sizes_process.inc.php @@ -0,0 +1,265 @@ + 98) +{ + $errors['resize_quality'] = '[50..98]'; +} + +$pderivatives = $_POST['d']; + +// step 1 - sanitize HTML input +foreach ($pderivatives as $type => &$pderivative) +{ + if ($pderivative['must_square'] = ($type==IMG_SQUARE ? true : false)) + { + $pderivative['h'] = $pderivative['w']; + $pderivative['minh'] = $pderivative['minw'] = $pderivative['w']; + $pderivative['crop'] = 100; + } + $pderivative['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; + $pderivative['enabled'] = isset($pderivative['enabled']) || $pderivative['must_enable'] ? true : false; + + if (isset($pderivative['crop'])) + { + $pderivative['crop'] = 100; + $pderivative['minw'] = $pderivative['w']; + $pderivative['minh'] = $pderivative['h']; + } + else + { + $pderivative['crop'] = 0; + $pderivative['minw'] = null; + $pderivative['minh'] = null; + } +} +unset($pderivative); + +// step 2 - check validity +$prev_w = $prev_h = 0; +foreach(ImageStdParams::get_all_types() as $type) +{ + $pderivative = $pderivatives[$type]; + if (!$pderivative['enabled']) + { + continue; + } + + if ($type == IMG_THUMB) + { + $w = intval($pderivative['w']); + if ($w <= 0) + { + $errors[$type]['w'] = '>0'; + } + + $h = intval($pderivative['h']); + if ($h <= 0) + { + $errors[$type]['h'] = '>0'; + } + + if (max($w,$h) <= $prev_w) + { + $errors[$type]['w'] = $errors[$type]['h'] = '>'.$prev_w; + } + } + else + { + $v = intval($pderivative['w']); + if ($v <= 0 or $v <= $prev_w) + { + $errors[$type]['w'] = '>'.$prev_w; + } + + $v = intval($pderivative['h']); + if ($v <= 0 or $v <= $prev_h) + { + $errors[$type]['h'] = '>'.$prev_h; + } + } + + if (count($errors) == 0) + { + $prev_w = intval($pderivative['w']); + $prev_h = intval($pderivative['h']); + } +} + +// step 3 - save data +if (count($errors) == 0) +{ + $enabled = ImageStdParams::get_defined_type_map(); + $disabled = @unserialize( @$conf['disabled_derivatives'] ); + if ($disabled === false) + { + $disabled = array(); + } + $changed_types = array(); + + foreach (ImageStdParams::get_all_types() as $type) + { + $pderivative = $pderivatives[$type]; + + if ($pderivative['enabled']) + { + $new_params = new DerivativeParams( + new SizingParams( + array(intval($pderivative['w']), intval($pderivative['h'])), + round($pderivative['crop'] / 100, 2), + array(intval($pderivative['minw']), intval($pderivative['minh'])) + ) + ); + + $new_params->quality = intval($_POST['resize_quality']); + + ImageStdParams::apply_global($new_params); + + if (isset($enabled[$type])) + { + $old_params = $enabled[$type]; + $same = true; + if (!size_equals($old_params->sizing->ideal_size, $new_params->sizing->ideal_size) + or $old_params->sizing->max_crop != $new_params->sizing->max_crop) + { + $same = false; + } + + if ($same + and $new_params->sizing->max_crop != 0 + and !size_equals($old_params->sizing->min_size, $new_params->sizing->min_size)) + { + $same = false; + } + + if ($new_params->quality != $old_params->quality) + { + $same = false; + } + + if (!$same) + { + $new_params->last_mod_time = time(); + $changed_types[] = $type; + } + else + { + $new_params->last_mod_time = $old_params->last_mod_time; + } + $enabled[$type] = $new_params; + } + else + {// now enabled, before was disabled + $enabled[$type] = $new_params; + unset($disabled[$type]); + } + } + else + {// disabled + if (isset($enabled[$type])) + {// now disabled, before was enabled + $changed_types[] = $type; + $disabled[$type] = $enabled[$type]; + unset($enabled[$type]); + } + } + } + + $enabled_by = array(); // keys ordered by all types + foreach(ImageStdParams::get_all_types() as $type) + { + if (isset($enabled[$type])) + { + $enabled_by[$type] = $enabled[$type]; + } + } + + ImageStdParams::set_and_save($enabled_by); + if (count($disabled) == 0) + { + $query='DELETE FROM '.CONFIG_TABLE.' WHERE param = \'disabled_derivatives\''; + pwg_query($query); + } + else + { + conf_update_param('disabled_derivatives', addslashes(serialize($disabled)) ); + } + $conf['disabled_derivatives'] = serialize($disabled); + + if (count($changed_types)) + { + clear_derivative_cache($changed_types); + } + + array_push( + $page['infos'], + l10n('Your configuration settings are saved') + ); +} +else +{ + foreach ($original_fields as $field) + { + if (isset($_POST[$field])) + { + $template->append( + 'sizes', + array( + $field => $_POST[$field] + ), + true + ); + } + } + + $template->assign('derivatives', $pderivatives); + $template->assign('ferrors', $errors); + $template->assign('resize_quality', $_POST['resize_quality']); + $page['sizes_loaded_in_tpl'] = true; +} +?> \ No newline at end of file diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php index 19482ee02..3df881a2b 100644 --- a/admin/include/functions_upload.inc.php +++ b/admin/include/functions_upload.inc.php @@ -68,7 +68,7 @@ function get_upload_form_config() return $upload_form_config; } -function save_upload_form_config($data, &$errors=array()) +function save_upload_form_config($data, &$errors=array(), &$form_errors=array()) { if (!is_array($data) or empty($data)) { @@ -130,6 +130,8 @@ function save_upload_form_config($data, &$errors=array()) $max ) ); + + $form_errors[$field] = '['.$min.' .. '.$max.']'; } } } diff --git a/admin/themes/default/template/configuration.tpl b/admin/themes/default/template/configuration.tpl index ca54e90d1..d6f0b0064 100644 --- a/admin/themes/default/template/configuration.tpl +++ b/admin/themes/default/template/configuration.tpl @@ -319,8 +319,10 @@ jQuery(document).ready(function(){ }); {/literal}{/footer_script} -{literal} - -{/literal} +{/literal}{/html_head}
-