diff --git a/modules/system/system.admin_image.inc b/modules/system/system.admin_image.inc new file mode 100644 index 0000000..dd3a361 --- /dev/null +++ b/modules/system/system.admin_image.inc @@ -0,0 +1,744 @@ + $preset['name'])); +} + +/** + * Form builder; Edit a preset name and action order. + * + * @ingroup forms + * @see system_image_preset_form_submit() + * @see system_image_preset_name_validate() + */ +function system_image_preset_form($form_state, $preset_name) { + $preset = image_preset_load($preset_name); + $form = array(); + + $form['name'] = array( + '#type' => 'textfield', + '#size' => '64', + '#title' => t('Preset name'), + '#default_value' => $preset['name'], + '#description' => t('The name is used in URLs for generated images. Only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for preset names.'), + '#element_validate' => array('system_image_preset_name_validate'), + ); + + // No need to show a preview if there are no actions. + if (count($preset['actions'])) { + $preview_path = file_create_path('imageactions_sample.png'); + if (!file_exists($preview_path)) { + // Copy of the sample image into the files directory allows us to generate previews without + // needing special case handling. + file_unmanaged_copy('misc/sample.png', 'imageactions_sample.png'); + } + $form['preview'] = array( + '#type' => 'fieldset', + '#title' => t('Preview'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['preview']['image'] = array( + '#markup' => theme('image_preset', $preset['name'], $preview_path), + ); + } + + $form['ipid'] = array( + '#type' => 'value', + '#value' => $preset['ipid'], + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Update preset'), + ); + return $form; +} + +/** + * Submit handler for saving an image preset name. + */ +function system_image_preset_form_submit($form, &$form_state) { + // Update the preset name if it has changed. + if ($preset = image_preset_load(NULL, $form_state['values']['ipid'])) { + if ($preset['name'] != $form_state['values']['name']) { + $preset['name'] = $form_state['values']['name']; + image_preset_save($preset); + $form_state['redirect'] = 'admin/settings/image-presets/' . $preset['name']; + } + } + + menu_rebuild(); + drupal_set_message('Changes to the preset have been saved.'); +} + +/** + * Form builder; Edit a preset's actions. + * + * @ingroup forms + * @see system_image_preset_action_form_submit() + * @see theme_admin_image_preset_actions() + */ +function system_image_preset_actions_form($form_state, $preset_name) { + $preset = image_preset_load($preset_name); + $form = array(); + + $form['ipid'] = array( + '#type' => 'value', + '#value' => $preset['ipid'], + ); + + $form['actions'] = array( + '#tree' => TRUE, + '#type' => 'markup', + '#theme' => 'admin_image_preset_actions', + ); + + foreach ($preset['actions'] as $i => $action) { + $action_form['name'] = array( + '#markup' => $action['name'], + ); + $action_form['action'] = array( + '#type' => 'value', + '#value' => $action['action'], + ); + $action_form['iaid'] = array( + '#type' => 'value', + '#value' => $action['iaid'], + ); + $action_form['ipid'] = array( + '#type' => 'value', + '#value' => $action['ipid'], + ); + + $action_form['summary'] = array( + '#markup' => theme($action['summary'], $action['data']), + ); + $action_form['data'] = array( + '#type' => 'value', + '#value' => $action['data'], + ); + $action_form['weight'] = array( + '#type' => 'weight', + '#default_value' => $action['weight'], + ); + $action_form['configure'] = array( + '#markup' => ($action['data']) ? l(t('Configure'), 'admin/settings/image-presets/'. $preset['name'] .'/actions/'. $action['iaid'] ) : 'n/a', + ); + $action_form['remove'] = array( + '#markup' => l(t('Delete'), 'admin/settings/image-presets/'. $preset['name'] .'/actions/'. $action['iaid'] .'/delete'), + ); + $form['actions'][$i] = $action_form; + } + + $form['new_actions'] = array( + '#tree' => TRUE, + '#type' => 'fieldset', + '#title' => t('Add actions'), + '#collapsible' => FALSE, + '#theme' => 'admin_image_preset_actions_add', + '#preset' => $preset, + ); + + foreach (image_action_definitions() as $action => $definition) { + $form['new_actions'][$action] = array( + '#type' => 'value', + '#value' => $definition, + ); + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Update preset'), + ); + return $form; +} + +/** + * Submit handler for saving an image preset action weights. + */ +function system_image_preset_actions_form_submit($form, &$form_state) { + // Update the individual action weights. + foreach ($form_state['values']['actions'] as $new_action) { + $action = image_action_load($new_action['iaid']); + $action['weight'] = $new_action['weight']; + image_action_save($action); + } + + menu_rebuild(); + drupal_set_message('Changes to the preset have been saved.'); +} + +/** + * Form builder; Form for adding and editing image actions. + * + * @ingroup forms + * @see system_image_preset_add_form_submit() + * @see system_image_preset_name_validate() + */ +function system_image_preset_add_form($form_state) { + $form = array(); + $form['name'] = array( + '#type' => 'textfield', + '#size' => '64', + '#title' => t('Preset name'), + '#default_value' => '', + '#description' => t('The name is used in URLs for generated images. Only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for preset names.'), + '#element_validate' => array('system_image_preset_name_validate'), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Create new preset'), + ); + return $form; +} + +/** + * Submit handler for adding a new preset. + */ +function system_image_preset_add_form_submit($form, &$form_state) { + $preset = array('name' => $form_state['values']['name']); + $preset = image_preset_save($preset); + menu_rebuild(); + drupal_set_message(t('Preset %name was created.', array('%name' => $preset['name']))); + $form_state['redirect'] = 'admin/settings/image-presets/'. $preset['name']; +} + +/** + * Element validate function to ensure unique, URL safe preset names. + */ +function system_image_preset_name_validate($element, $form_state) { + // Check for duplicates. + $presets = image_presets(); + if (isset($presets[$element['#value']]) && (!isset($form_state['values']['ipid']) || $presets[$element['#value']]['ipid'] != $form_state['values']['ipid'])) { + form_set_error($element['#name'], t('The image preset name %name is already in use.', array('%name' => $element['#value']))); + } + + // Check for illegal characters in preset names. + if (preg_match('/[^0-9a-z_\-]/', $element['#value'])) { + form_set_error($element['#name'], t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for preset names.')); + } +} + +/** + * Form builder; Form for deleting an image preset. + * + * @param $preset + * Name of the preset to delete. + * + * @ingroup forms + * @see system_image_preset_delete_form_submit() + */ +function system_image_preset_delete_form($form_state, $preset = '') { + if (empty($preset)) { + drupal_set_message(t('The specified preset was not found.'), 'error'); + drupal_goto('admin/settings/image-presets'); + } + + $preset = image_preset_load($preset); + + $form = array(); + $form['name'] = array( + '#type' => 'value', + '#value' => $preset['name'], + ); + $form['ipid'] = array( + '#type' => 'value', + '#value' => $preset['ipid'], + ); + return confirm_form( + $form, + t('Are you sure you want to delete the preset %preset?', array('%preset' => $preset['name'])), + 'admin/settings/image-presets', + t('All images that have been generated for this preset will be permanently deleted.'), + t('Delete'), t('Cancel') + ); +} + +/** + * Submit handler to delete an image preset. + */ +function system_image_preset_delete_form_submit($form, &$form_state) { + $preset = image_preset_load(NULL, $form_state['values']['ipid']); + image_preset_delete($preset); + menu_rebuild(); + drupal_set_message(t('Preset %name was deleted.', array('%name' => $preset['name']))); + $form_state['redirect'] = 'admin/settings/image-presets'; +} + +/** + * Form builder; Confirm flushing a preset's cached images. + * + * @param $preset + * Name of preset to flush. + * @ingroup forms + * @see system_image_preset_flush_form_submit() + */ +function system_image_preset_flush_form(&$form_state, $preset = '') { + if (empty($preset)) { + drupal_set_message(t('The specified preset was not found.'), 'error'); + $form_state['redirect'] = 'admin/settings/image-presets'; + } + + $preset = image_preset_load($preset); + + $form = array(); + $form['name'] = array( + '#type' => 'value', + '#value' => $preset['name'], + ); + $form['ipid'] = array( + '#type' => 'value', + '#value' => $preset['ipid'], + ); + return confirm_form( + $form, + t('Are you sure you want to flush the %preset preset?', array('%preset' => $preset['name'])), + 'admin/settings/image-presets', + t('This will delete all the generated images for the %preset preset. Regenerating the images may cause a temporary increase in your server\'s load.', array('%preset' => $preset['name'])), + t('Flush'), t('Cancel') + ); +} + +/** + * Submit handler for flushing an image preset's cached files. + */ +function system_image_preset_flush_form_submit($form, &$form_state) { + $preset = image_preset_load(NULL, $form_state['values']['ipid']); + image_preset_flush($preset); + drupal_set_message(t('Preset %name was flushed.', array('%name' => $preset['name']))); + $form_state['redirect'] = 'admin/settings/image-presets'; +} + +/** + * Menu title callback; Title for editing, deleting, and adding image actions. + * + * @param $action + * An image action array. + */ +function system_image_action_title($string, $action) { + if (!is_array($action)) { + $action = image_action_load($action); + } + return t($string, array('!name' => $action['name'])); +} + +/** + * Form builder; Form for adding and editing image actions. + * + * This form is used universally for editing all actions. Each action adds its + * own custom section to the form by calling the form function specified in + * hook_image_actions(). + * + * @ingroup forms + * @see hook_image_actions() + * @see system_image_actions() + * @see system_image_resize_form() + * @see system_image_scale_form() + * @see system_image_rotate_form() + * @see system_image_crop_form() + * @see system_image_desaturate_form() + * @see system_image_action_form_submit() + */ +function system_image_action_form($form_state, $preset_name, $action = '') { + $preset = image_preset_load($preset_name); + if (!is_array($action)) { + $action = image_action_load($action); + } + + $form = array( + '#tree' => TRUE, + '#image_preset' => $preset, + '#image_action' => $action, + ); + + if (drupal_function_exists($action['form'])) { + $form['data'] = call_user_func($action['form'], $action['data']); + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => isset($action['iaid']) ? t('Update action') : t('Add action'), + ); + return $form; +} + +/** + * Submit handler for updating an image action. + */ +function system_image_action_form_submit($form, &$form_state) { + $preset = $form['#image_preset']; + $action = array_merge($form['#image_action'], $form_state['values']); + $action['ipid'] = $preset['ipid']; + if (empty($action['iaid'])) { + $action['weight'] = count($preset['actions']); + } + + image_action_save($action); + menu_rebuild(); + drupal_set_message(t('The action was successfully updated.')); + $form_state['redirect'] = 'admin/settings/image-presets/'. $preset['name'] .'/actions'; +} + +/** + * Form builder; Form for deleting an image action. + * + * @params $preset + * Name of the preset to remove the action from. + * @params $action + * Name of the action to remove. + * @ingroup forms + * @see system_image_action_delete_form_submit() + */ +function system_image_action_delete_form($form_state, $preset, $action) { + $preset = image_preset_load($preset); + $action = image_action_load($action); + + $form = array( + '#image_preset' => $preset, + '#image_action' => $action, + ); + + $question = t('Are you sure you want to delete the @action action from the %preset preset?', array('%preset' => $preset['name'], '@action' => $action['name'])); + $description = t('Deleting this action will regenerate all images for the %preset preset.', array('%preset' => $preset['name'])); + return confirm_form($form, $question, 'admin/settings/images/presets/' . $preset['name'], $description, t('Delete')); +} + +/** + * Submit handler to delete an image action. + */ +function system_image_action_delete_form_submit($form, &$form_state) { + $preset = $form['#image_preset']; + $action = $form['#image_action']; + + image_action_delete($action); + menu_rebuild(); + drupal_set_message(t('The image action %name has been deleted.', array('%name' => $action['name']))); + $form_state['redirect'] = 'admin/settings/image-presets/'. $preset['name'] .'/actions'; +} + +/** + * Form structure for the image resize form. + * + * Note that this is not a complete form, it only contains the portion of the + * form for configuring the resize options. Therefor it does not not need to + * include metadata about the action, nor a submit button. + * + * @param $data + * The current configuration for this resize action. + */ +function system_image_resize_form($data) { + $form['width'] = array( + '#type' => 'textfield', + '#title' => t('Width'), + '#default_value' => isset($data['width']) ? $data['width'] : '', + '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'), + '#required' => TRUE, + ); + $form['height'] = array( + '#type' => 'textfield', + '#title' => t('Height'), + '#default_value' => isset($data['height']) ? $data['height'] : '', + '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'), + '#required' => TRUE, + ); + return $form; +} + +/** + * Form structure for the image scale form. + * + * Note that this is not a complete form, it only contains the portion of the + * form for configuring the scale options. Therefor it does not not need to + * include metadata about the action, nor a submit button. + * + * @param $data + * The current configuration for this scale action. + */ +function system_image_scale_form($data) { + $form = system_image_resize_form($data); + $form['width']['#required'] = FALSE; + $form['height']['#required'] = FALSE; + $form['upscale'] = array( + '#type' => 'checkbox', + '#default_value' => (isset($data['upscale'])) ? $data['upscale'] : 0, + '#title' => t('Allow Upscaling'), + '#description' => t('Let scale make images larger than their original size'), + ); + return $form; +} + +/** + * Form structure for the image crop form. + * + * Note that this is not a complete form, it only contains the portion of the + * form for configuring the crop options. Therefor it does not not need to + * include metadata about the action, nor a submit button. + * + * @param $data + * The current configuration for this crop action. + */ +function system_image_crop_form($data) { + $data += array( + 'width' => '', + 'height' => '', + 'xoffset' => '', + 'yoffset' => '', + ); + + $form['width'] = array( + '#type' => 'textfield', + '#title' => t('Width'), + '#default_value' => $data['width'], + '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'), + '#required' => TRUE, + ); + $form['height'] = array( + '#type' => 'textfield', + '#title' => t('Height'), + '#default_value' => $data['height'], + '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'), + '#required' => TRUE, + ); + $form['xoffset'] = array( + '#type' => 'textfield', + '#title' => t('Horizontal offset'), + '#default_value' => $data['xoffset'], + '#description' => t('Enter a x-axis offset in pixels or use a keyword: left, center, or right.'), + ); + $form['yoffset'] = array( + '#type' => 'textfield', + '#title' => t('Vertical offset'), + '#default_value' => $data['yoffset'], + '#description' => t('Enter a y-axis offset in pixels or use a keyword: top, center, or bottom.'), + ); + return $form; +} + +/** + * Form structure for the image desaturate form. + * + * Note that this is not a complete form, it only contains the portion of the + * form for configuring the desaturate options. Therefor it does not not need to + * include metadata about the action, nor a submit button. + * + * @param $data + * The current configuration for this crop action. + */ +function system_image_desaturate_form($data) { + return array( + '#markup' => '
', + ); +} + +/** + * Form structure for the image rotate form. + * + * Note that this is not a complete form, it only contains the portion of the + * form for configuring the rotate options. Therefor it does not not need to + * include metadata about the action, nor a submit button. + * + * @param $data + * The current configuration for this rotate action. + */ +function system_image_rotate_form($data) { + $form['degrees'] = array( + '#type' => 'textfield', + '#default_value' => (isset($data['degrees'])) ? $data['degrees'] : 0, + '#title' => t('Rotation angle'), + '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'), + '#required' => TRUE, + ); + $form['random'] = array( + '#type' => 'checkbox', + '#default_value' => (isset($data['random'])) ? $data['random'] : 0, + '#title' => t('Randomize'), + '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'), + ); + $form['bgcolor'] = array( + '#type' => 'textfield', + '#default_value' => (isset($data['bgcolor'])) ? $data['bgcolor'] : '#FFFFFF', + '#title' => t('Background color'), + '#description' => t('The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black).'), + '#required' => TRUE, + ); + return $form; +} + +/** + * Display the page containing the list of image presets. + * + * @param $presets + * An array of all the image presets returned by image_get_presets(). + * @see image_get_presets() + * @ingroup themeable + */ +function theme_admin_image_presets($presets) { + $header = array(t('Preset name'), array('data' => t('Operations'), 'colspan' => 3)); + $rows = array(); + foreach ($presets as $preset) { + $row = array(); + $row[] = l($preset['name'], 'admin/settings/image-presets/'. $preset['name']); + $link_attributes = array( + 'attributes' => array( + 'class' => 'image-preset-link', + ), + ); + $row[] = l(t('Edit'), 'admin/settings/image-presets/'. $preset['name'], $link_attributes); + $row[] = l(t('Delete'), 'admin/settings/image-presets/'. $preset['name'] .'/delete', $link_attributes); + $row[] = l(t('Flush'), 'admin/settings/image-presets/'. $preset['name'] .'/flush', $link_attributes); + $rows[] = $row; + } + + if (empty($rows)) { + $rows[] = array(array( + 'colspan' => 4, + 'data' => t('There are currently no presets. Add a new one.', array('!url' => url('admin/settings/images/presets/add'))), + )); + } + + return theme('table', $header, $rows); +} + +/** + * Theme callback for listing the actions within a specific image preset. + * + * @param $form + * An associative array containing the structure of the actions group. + * @ingroup themeable + */ +function theme_admin_image_preset_actions($form) { + $header = array(t('Action'), t('Settings'), t('Weight'), '',''); + $rows = array(); + foreach(element_children($form) as $key) { + if (!is_numeric($key)) { + continue; + } + $row = array(); + $form[$key]['weight']['#attributes']['class'] = 'image-action-order-weight'; + $row[] = drupal_render($form[$key]['name']); + $row[] = drupal_render($form[$key]['summary']); + $row[] = drupal_render($form[$key]['weight']); + $row[] = drupal_render($form[$key]['configure']); + $row[] = drupal_render($form[$key]['remove']); + $rows[] = array( + 'data' => $row, + 'class' => 'draggable', + ); + } + + if (empty($rows)) { + $rows[] = array(array( + 'colspan' => 5, + 'data' => t('There are currently no actions in this preset. Add one by selecting a preset from the New actions fieldset.'), + )); + } + + $output = theme('table', $header, $rows, array('id' => 'image-preset-actions')); + drupal_add_tabledrag('image-preset-actions', 'order', 'sibling', 'image-action-order-weight'); + return $output; +} + +/** + * Theme callback for adding new actions to a preset. + * + * @param $form + * An associative array containing the structure of the new actions fieldset. + * @ingroup themeable + */ +function theme_admin_image_preset_actions_add($form) { + $preset = $form['#preset']; + + $output = '