Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.716 diff -u -r1.716 system.module --- modules/system/system.module 28 Jun 2009 12:52:57 -0000 1.716 +++ modules/system/system.module 1 Jul 2009 17:55:19 -0000 @@ -170,6 +170,38 @@ 'arguments' => array('content' => NULL), 'file' => 'system.admin.inc', ), + 'admin_image_presets' => array( + 'arguments' => array('presets' => NULL), + 'file' => 'system.admin.inc', + ), + 'admin_image_preset_actions' => array( + 'arguments' => array('form' => NULL), + 'file' => 'system.admin.inc', + ), + 'admin_image_preset_actions_add' => array( + 'arguments' => array('form' => NULL), + 'file' => 'system.admin.inc', + ), + 'system_image_resize_summary' => array( + 'arguments' => array('data' => NULL), + 'file' => 'image.actions.inc', + ), + 'system_image_scale_summary' => array( + 'arguments' => array('data' => NULL), + 'file' => 'image.actions.inc', + ), + 'system_image_crop_summary' => array( + 'arguments' => array('data' => NULL), + 'file' => 'image.actions.inc', + ), + 'system_image_desaturate_summary' => array( + 'arguments' => array('data' => NULL), + 'file' => 'image.actions.inc', + ), + 'system_image_rotate_summary' => array( + 'arguments' => array('data' => NULL), + 'file' => 'image.actions.inc', + ), 'system_admin_by_module' => array( 'arguments' => array('menu_items' => NULL), 'file' => 'system.admin.inc', @@ -204,6 +236,10 @@ 'title' => t('Administer files'), 'description' => t('Manage user-uploaded files.'), ), + 'administer image presets' => array( + 'title' => t('Administer image presets'), + 'description' => t('Create and modify presets for generating image modifications such as thumbnails.'), + ), 'access administration pages' => array( 'title' => t('Access administration pages'), 'description' => t('View the administration panel and browse the help system.'), @@ -701,6 +737,108 @@ 'page arguments' => array('system_file_system_settings'), 'access arguments' => array('administer site configuration'), ); + $items['admin/settings/image-presets'] = array( + 'title' => 'Image presets', + 'description' => 'Configure presets for generating image thumbnails and select an image toolkit.', + 'page callback' => 'system_image_presets', + 'access arguments' => array('administer image presets'), + ); + $items['admin/settings/image-presets/list'] = array( + 'title' => 'List', + 'description' => 'List the current image presets on the site.', + 'page callback' => 'system_image_presets', + 'access arguments' => array('administer image presets'), + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => 1, + ); + $items['admin/settings/image-presets/add'] = array( + 'title' => 'Add preset', + 'description' => 'Add a new image preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_preset_add_form'), + 'access arguments' => array('administer image presets'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 2, + ); + foreach (image_presets() as $preset) { + $items['admin/settings/image-presets/'. $preset['name']] = array( + 'title' => 'Edit preset', + 'title callback' => 'system_image_preset_title', + 'title arguments' => array('Edit preset !name', 3), + 'description' => 'Configure an image preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_preset_form', 3), + 'access arguments' => array('administer image presets'), + 'type' => MENU_CALLBACK, + ); + $items['admin/settings/image-presets/'. $preset['name'] .'/edit'] = array( + 'title' => 'Edit', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => 1, + ); + $items['admin/settings/image-presets/'. $preset['name'] .'/actions'] = array( + 'title' => 'Actions', + 'description' => 'Configure an image preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_preset_actions_form', 3), + 'access arguments' => array('administer image presets'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 2, + ); + $items['admin/settings/image-presets/'. $preset['name'] .'/delete'] = array( + 'title' => 'Delete preset', + 'title callback' => 'system_image_preset_title', + 'title arguments' => array('Delete preset !name', 3), + 'description' => 'Configure presets for generating image thumbnails and other manipulations.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_preset_delete_form', 3, TRUE), + 'access arguments' => array('administer image presets'), + 'type' => MENU_CALLBACK, + ); + $items['admin/settings/image-presets/'. $preset['name'] .'/flush'] = array( + 'title' => 'Flush preset', + 'title callback' => 'system_image_preset_title', + 'title arguments' => array('Flush preset !name', 3), + 'description' => 'Flush all the created images for a preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_preset_flush_form', 3, TRUE), + 'access arguments' => array('administer image presets'), + 'type' => MENU_CALLBACK, + ); + // Cast as an array in case this is called before any actions have + // been added, such as when a new preset is created. + foreach ((array) $preset['actions'] as $action) { + $items['admin/settings/image-presets/'. $preset['name'] .'/actions/'. $action['iaid']] = array( + 'title' => $action['name'], + 'description' => 'Edit an exiting action within a preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_action_form', 3, 5), + 'access arguments' => array('administer image presets'), + 'type' => MENU_LOCAL_TASK, + 'weight' => $action['weight'], + ); + $items['admin/settings/image-presets/'. $preset['name'] .'/actions/'. $action['iaid'] .'/delete'] = array( + 'title' => 'Delete image action', + 'title callback' => 'system_image_action_title', + 'title arguments' => array('Delete !name action', 5), + 'description' => 'Delete an exiting action from a preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_action_delete_form', 3, 5), + 'access arguments' => array('administer image presets'), + 'type' => MENU_CALLBACK, + ); + } + $items['admin/settings/image-presets/'. $preset['name'] .'/add/%image_action_definition'] = array( + 'title' => 'Add image action', + 'title callback' => 'system_image_action_title', + 'title arguments' => array('Add !name action', 5), + 'description' => 'Add a new action to a preset.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('system_image_action_form', 3, 5), + 'access arguments' => array('administer image presets'), + 'type' => MENU_CALLBACK, + ); + } $items['admin/settings/image-toolkit'] = array( 'title' => 'Image toolkit', 'description' => 'Choose which image toolkit to use if you have installed optional toolkits.', Index: modules/system/system.test =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.test,v retrieving revision 1.53 diff -u -r1.53 system.test --- modules/system/system.test 28 Jun 2009 03:56:43 -0000 1.53 +++ modules/system/system.test 1 Jul 2009 17:55:19 -0000 @@ -481,6 +481,217 @@ } /** + * Tests creation, deletion, and editing of image presets and actions. + */ +class AdminImagePresets extends DrupalWebTestCase { + + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Image presets and actions configuration'), + 'description' => t('Tests creation, deletion, and editing of image presets and actions.'), + 'group' => t('System') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + parent::setUp(); + + // Create an administrative user. + $this->admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer image presets')); + $this->drupalLogin($this->admin_user); + } + + /** + * Given an image preset, generate an image. + */ + function createSampleImage($preset) { + static $file_path; + + // First, we need to make sure we have an image in our testing + // file directory. Copy over an image on the first run. + if (!isset($file_path)) { + $file = reset($this->drupalGetTestFiles('image')); + $file_path = file_unmanaged_copy($file->filename); + } + + return image_preset_generate($preset['name'], $file_path) ? $file_path : FALSE; + } + + /** + * Count the number of images currently create for a preset. + */ + function getImageCount($preset) { + $directory = file_directory_path() . '/presets/' . $preset['name']; + return count(file_scan_directory($directory, '/.*/')); + } + + function testPreset() { + // Setup a preset to be created and actions to add to it. + $preset_name = strtolower($this->randomName(10)); + $preset_path = 'admin/settings/image-presets/' . $preset_name; + $action_edits = array( + 'system_image_resize' => array( + 'data[width]' => 100, + 'data[height]' => 101, + ), + 'system_image_scale' => array( + 'data[width]' => 110, + 'data[height]' => 111, + 'data[upscale]' => 1, + ), + 'system_image_scale_and_crop' => array( + 'data[width]' => 120, + 'data[height]' => 121, + ), + 'system_image_crop' => array( + 'data[width]' => 130, + 'data[height]' => 131, + 'data[xoffset]' => 1, + 'data[yoffset]' => 2, + ), + 'system_image_desaturate' => array( + // No options for desaturate. + ), + 'system_image_rotate' => array( + 'data[degrees]' => 5, + 'data[random]' => 1, + 'data[bgcolor]' => '#FFFF00', + ), + ); + + /* Add preset form. */ + + $edit = array( + 'name' => $preset_name, + ); + $this->drupalPost('admin/settings/image-presets/add', $edit, t('Create new preset')); + $this->assertRaw(t('Preset %name was created.', array('%name' => $preset_name)), t('Image preset successfully created.')); + + /* Add action form. */ + + // Add each sample action to the preset. + foreach ($action_edits as $action => $edit) { + // Add the action. + $this->drupalPost($preset_path . '/add/' . $action, $edit, t('Add action')); + } + + /* Edit action form. */ + + // Revisit each form to make sure the action was saved. + $preset = image_preset_load($preset_name); + + foreach ($preset['actions'] as $iaid => $action) { + $this->drupalGet($preset_path . '/' . $iaid); + foreach ($action_edits[$action['action']] as $field => $value) { + $this->assertFieldByName($field, $value, t('The %field field in the %action action has the correct value of %value.', array('%field' => $field, '%action' => $action['action'], '%value' => $value))); + } + } + + /* Image preset overview form (ordering and renaming). */ + + // Confirm the order of actions is maintained according to the order we + // added the fields. + $action_edits_order = array_keys($action_edits); + $actions_order = array_values($preset['actions']); + $order_correct = TRUE; + foreach ($actions_order as $index => $action) { + if ($action_edits_order[$index] != $action['action']) { + $order_correct = FALSE; + } + } + $this->assertTrue($order_correct, t('The order of the actions is correctly set by default.')); + + // Test the preset overview form. + // Change the name of the preset and adjust the weights of actions. + $preset_name = strtolower($this->randomName(10)); + $weight = count($action_edits); + $edit = array( + 'name' => $preset_name, + ); + foreach ($preset['actions'] as $iaid => $action) { + $edit['actions[' . $iaid . '][weight]'] = $weight; + $weight--; + } + + // Create an image to make sure it gets flushed after saving. + $image_path = $this->createSampleImage($preset); + $this->assertEqual($this->getImageCount($preset), 1, t('Image preset %preset image %file successfully generated.', array('%preset' => $preset['name'], '%file' => $image_path))); + + $this->drupalPost($preset_path, $edit, t('Update preset')); + + // Note that after changing the preset name, the preset path is changed. + $preset_path = 'admin/settings/image-presets/' . $preset_name; + + // Check that the URL was updated. + $this->drupalGet($preset_path); + $this->assertResponse(200, t('Image preset %original renamed to %new', array('%original' => $preset['name'], '%new' => $preset_name))); + + // Check that the image was flushed after updating the preset. + // This is especially important when renaming the preset. Make sure that + // the old image directory has been deleted. + $this->assertEqual($this->getImageCount($preset), 0, t('Image preset %preset was flushed after renaming the preset and updating the order of actions.', array('%preset' => $preset['name']))); + + // Load the preset by the new name with the new weights. + $preset = image_preset_load($preset_name, NULL, TRUE); + + // Confirm the new preset order was saved. + $action_edits_order = array_reverse($action_edits_order); + $actions_order = array_values($preset['actions']); + $order_correct = TRUE; + foreach ($actions_order as $index => $action) { + if ($action_edits_order[$index] != $action['action']) { + $order_correct = FALSE; + } + } + $this->assertTrue($order_correct, t('The order of the actions is correctly set by default.')); + + /* Image action deletion form. */ + + // Create an image to make sure it gets flushed after deleting an action. + $image_path = $this->createSampleImage($preset); + $this->assertEqual($this->getImageCount($preset), 1, t('Image preset %preset image %file successfully generated.', array('%preset' => $preset['name'], '%file' => $image_path))); + + // Test action deletion form. + $action = array_pop($preset['actions']); + $this->drupalPost($preset_path . '/' . $action['iaid'] . '/delete', array(), t('Delete')); + $this->assertRaw(t('The image action %name has been deleted.', array('%name' => $action['name'])), t('Image action deleted.')); + + // Check that the preset was flushed after deleting an action. + $this->assertEqual($this->getImageCount($preset), 0, t('Image preset %preset was flushed after deleting an action.', array('%preset' => $preset['name']))); + + /* Preset flush form. */ + + // Create an image to make sure it gets flushed after deleting an action. + $image_path = $this->createSampleImage($preset); + $this->assertEqual($this->getImageCount($preset), 1, t('Image preset %preset image %file successfully generated.', array('%preset' => $preset['name'], '%file' => $image_path))); + + $this->drupalPost($preset_path . '/flush', array(), t('Flush')); + $this->assertRaw(t('Preset %name was flushed.', array('%name' => $preset['name'])), t('Image preset %preset flush form submitted.')); + + $this->assertEqual($this->getImageCount($preset), 0, t('Image preset %preset was flushed after submitting the flush form.', array('%preset' => $preset['name']))); + + /* Preset deletion form. */ + + // Delete the preset. + $this->drupalPost($preset_path . '/delete', array(), t('Delete')); + + // Confirm the preset directory has been removed. + $directory = file_directory_path() . '/presets/' . $preset_name; + $this->assertFalse(is_dir($directory), t('Image preset %preset directory removed on preset deletion.', array('%preset' => $preset['name']))); + + // Confirm the preset is no longer available. + $this->assertFalse(image_preset_load($preset_name), t('Image preset %preset successfully deleted.', array('%preset' => $preset['name']))); + + } +} + +/** * Tests custom access denied functionality. */ class AccessDeniedTestCase extends DrupalWebTestCase { Index: modules/user/user.test =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.test,v retrieving revision 1.43 diff -u -r1.43 user.test --- modules/user/user.test 13 Jun 2009 20:40:09 -0000 1.43 +++ modules/user/user.test 1 Jul 2009 17:55:21 -0000 @@ -562,8 +562,14 @@ // user's profile page. $text = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $test_dim)); $this->assertRaw($text, t('Image was resized.')); + if ($preset = variable_get('user_picture_preset', '')) { + $url_path = image_preset_path($preset, $pic_path, FALSE); + } + else { + $url_path = $pic_path; + } $alt = t("@user's picture", array('@user' => $this->user->name)); - $this->assertRaw(theme('image', $pic_path, $alt, $alt, '', FALSE), t("Image is displayed in user's edit page")); + $this->assertRaw(theme('image', $url_path, $alt, $alt, NULL, FALSE), t("Image is displayed in user's edit page")); // Check if file is located in proper directory. $this->assertTrue(is_file($pic_path), t("File is located in proper directory")); @@ -696,8 +702,14 @@ $pic_path = $this->saveUserPicture($image); // Check if image is displayed in user's profile page. + if ($preset = variable_get('user_picture_preset', '')) { + $url_path = image_preset_path($preset, $pic_path, FALSE); + } + else { + $url_path = $pic_path; + } $this->drupalGet('user'); - $this->assertRaw($pic_path, t("Image is displayed in user's profile page")); + $this->assertRaw($url_path, t("Image is displayed in user's profile page")); // Check if file is located in proper directory. $this->assertTrue(is_file($pic_path), t('File is located in proper directory')); Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.1003 diff -u -r1.1003 user.module --- modules/user/user.module 28 Jun 2009 12:52:57 -0000 1.1003 +++ modules/user/user.module 1 Jul 2009 17:55:21 -0000 @@ -627,8 +627,8 @@ // If required, validate the uploaded picture. $validators = array( 'file_validate_is_image' => array(), - 'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')), - 'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024), + 'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '1024x1024')), + 'file_validate_size' => array(variable_get('user_picture_file_size', '800') * 1024), ); // Save the file as a temporary file. @@ -1201,7 +1201,12 @@ } if (isset($filepath)) { $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous')))); - $variables['picture'] = theme('image', $filepath, $alt, $alt, '', FALSE); + if ($preset = variable_get('user_picture_preset', '')) { + $variables['picture'] = theme('image_preset', $preset, $filepath, $alt, $alt, NULL, FALSE); + } + else { + $variables['picture'] = theme('image', $filepath, $alt, $alt, NULL, FALSE); + } if (!empty($account->uid) && user_access('access user profiles')) { $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); $variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes); @@ -1911,7 +1916,7 @@ '#type' => 'file', '#title' => t('Upload picture'), '#size' => 48, - '#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions pixels and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) . ' ' . variable_get('user_picture_guidelines', ''), + '#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions pixels and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '1024x1024'), '%size' => variable_get('user_picture_file_size', '800'))) . ' ' . variable_get('user_picture_guidelines', ''), ); $form['#validate'][] = 'user_validate_picture'; } Index: modules/user/user.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.admin.inc,v retrieving revision 1.59 diff -u -r1.59 user.admin.inc --- modules/user/user.admin.inc 28 Jun 2009 20:51:09 -0000 1.59 +++ modules/user/user.admin.inc 1 Jul 2009 17:55:20 -0000 @@ -360,10 +360,21 @@ '#maxlength' => 255, '#description' => t('URL of picture to display for users with no custom picture selected. Leave blank for none.'), ); + $preset_options = array('' => t('Original image')); + foreach (image_presets() as $preset) { + $preset_options[$preset['name']] = $preset['name']; + } + $form['personalization']['pictures']['user_picture_preset'] = array( + '#type' => 'select', + '#title' => t('Picture image preset'), + '#default_value' => variable_get('user_picture_preset', ''), + '#options' => $preset_options, + '#description' => t('A preset may be used to give user pictures consistent display.'), + ); $form['personalization']['pictures']['user_picture_dimensions'] = array( '#type' => 'textfield', '#title' => t('Picture maximum dimensions'), - '#default_value' => variable_get('user_picture_dimensions', '85x85'), + '#default_value' => variable_get('user_picture_dimensions', '1024x1024'), '#size' => 15, '#maxlength' => 10, '#description' => t('Maximum dimensions for pictures, in pixels.'), @@ -371,7 +382,7 @@ $form['personalization']['pictures']['user_picture_file_size'] = array( '#type' => 'textfield', '#title' => t('Picture maximum file size'), - '#default_value' => variable_get('user_picture_file_size', '30'), + '#default_value' => variable_get('user_picture_file_size', '800'), '#size' => 15, '#maxlength' => 10, '#description' => t('Maximum file size for pictures, in kB.'), Index: profiles/default/default.profile =================================================================== RCS file: /cvs/drupal/drupal/profiles/default/default.profile,v retrieving revision 1.51 diff -u -r1.51 default.profile --- profiles/default/default.profile 27 Jun 2009 17:32:00 -0000 1.51 +++ profiles/default/default.profile 1 Jul 2009 17:55:21 -0000 @@ -196,6 +196,25 @@ // Don't display date and author information for page nodes by default. variable_set('node_submitted_page', FALSE); + // Create an image preset. + $preset = array('name' => 'thumbnail_square'); + $preset = image_preset_save($preset); + $action = array( + 'ipid' => $preset['ipid'], + 'action' => 'system_image_scale_and_crop', + 'data' => array('width' => '85', 'height' => '85'), + ); + image_action_save($action); + + // Enable user picture support and set the default to a square thumbnail option. + variable_set('user_pictures', '1'); + variable_set('user_picture_preset', 'thumbnail_square'); + + $theme_settings = theme_get_settings(); + $theme_settings['toggle_node_user_picture'] = '1'; + $theme_settings['toggle_comment_user_picture'] = '1'; + variable_set('theme_settings', $theme_settings); + // Create a default vocabulary named "Tags", enabled for the 'article' content type. $description = st('Use tags to group articles on similar topics into categories.'); $help = st('Enter a comma-separated list of words.'); Index: modules/system/system.admin_image.inc =================================================================== RCS file: modules/system/system.admin_image.inc diff -N modules/system/system.admin_image.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/system/system.admin_image.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,747 @@ + $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); + menu_rebuild(); + cache_clear_all(); + } + } + + drupal_set_message('Changes to the preset have been saved.'); + $form_state['redirect'] = 'admin/settings/image-presets'; +} + +/** + * 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'] ) : '', + ); + $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); + } + + 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); + 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(); + cache_clear_all(); + 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(); + cache_clear_all(); + 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/image-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(); + cache_clear_all(); + 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' => '
' . t('The desaturate action needs no configuration. Confirm the addition of this action.') . '
', + ); +} + +/** + * 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' => 4)); + $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('Actions'), 'admin/settings/image-presets/' . $preset['name'] . '/actions', $link_attributes); + $row[] = l(t('Rename'), 'admin/settings/image-presets/' . $preset['name'], $link_attributes); + $row[] = l(t('Flush'), 'admin/settings/image-presets/' . $preset['name'] . '/flush', $link_attributes); + $row[] = l(t('Delete'), 'admin/settings/image-presets/' . $preset['name'] . '/delete', $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/image-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 = '
'; + foreach (element_children($form) as $action) { + $definition = $form[$action]['#value']; + $output .= '
' . l(t('Add !action', array('!action' => $definition['name'])), 'admin/settings/image-presets/' . $preset['name'] . '/add/' . $action) . '
'; + $output .= '
' . $definition['description'] .'
'; + } + $output .= '
'; + return $output; +} + +/** + * Theme callback for image resize action summary output. + * + * @param $data + * The current configuration for this resize action. + * @ingroup themeable + */ +function theme_system_image_resize_summary($data) { + return t('Width') . ': '. $data['width'] . ', ' . t('Height') . ': ' . $data['height']; +} + +/** + * Theme callback for image scale action summary output. + * + * @param $data + * The current configuration for this scale action. + * @ingroup themeable + */ +function theme_system_image_scale_summary($data) { + $output = theme('system_image_resize_summary', $data); + $output .= ', ' . t('Upscale') . ': '; + $output .= $data['upscale'] ? t('Yes') : t('No'); + return $output; +} + +/** + * Theme callback for image crop action summary output. + * + * @param $data + * The current configuration for this crop action. + * @ingroup themeable + */ +function theme_system_image_crop_summary($data) { + $output = ''; + $output .= t('Width') . ': ' . ($data['width'] ? $data['width'] : t('auto')); + $output .= ', ' . t('Height') . ': ' . ($data['height'] ? $data['height'] : t('auto')); + $output .= ', ' . t('X offset') . ': ' . (empty($data['xoffset']) ? 0 : $data['xoffset']); + $output .= ', ' . t('Y offset') . ': ' . (empty($data['yoffset']) ? 0 : $data['yoffset']); + return $output; +} + +/** + * Theme callback for image desaturate action summary output. + * + * @param $data + * The current configuration for this desaturate action. + * @ingroup themeable + */ +function theme_system_image_desaturate_summary($data) { + return ''; +} + +/** + * Theme callback for image rotate action summary output. + * + * @param $data + * The current configuration for this rotate action. + * @ingroup themeable + */ +function theme_system_image_rotate_summary($data) { + $output = t('Degrees: ') . $data['degrees'] .', '; + $output .= t('Randomize: ') . (($data['random']) ? t('Yes') : t('No')) .', '; + $output .= t('Background: ') . $data['bgcolor']; + return $output; +}