utility-form.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @file Utility form, conversion and rendering functions for image processes.
  4. */
  5. /**
  6. * Prepares a sub form for displaying positioning fields.
  7. *
  8. * @param array $data
  9. * Effect data of the effect where this sub form will be integrated.
  10. *
  11. * @return array
  12. * The form definition for this sub form.
  13. */
  14. function imagecache_actions_pos_form(array $data) {
  15. $defaults = array(
  16. 'xpos' => 'center',
  17. 'ypos' => 'center',
  18. );
  19. $data = array_merge($defaults, (array) $data);
  20. $description1 = t('Enter an offset in pixels (e.g. 10, 10px), a percentage (e.g. 25%), or one of the keywords: <em>left</em>, <em>center</em>, or <em>right</em> wih an optional offset (e.g. center, right - 10%).');
  21. $description2 = t('Enter an offset in pixels (e.g. 10, 10px), a percentage (e.g. 25%), or one of the keywords: <em>top</em>, <em>center</em>, or <em>bottom</em> wih an optional offset (e.g. center, bottom - 10%).');
  22. $form = array(
  23. 'xpos' => array(
  24. '#type' => 'textfield',
  25. '#title' => t('X offset'),
  26. '#default_value' => $data['xpos'],
  27. '#size' => 6,
  28. '#description' => $description1,
  29. '#element_validate' => array('imagecache_actions_validate_number'),
  30. ),
  31. 'ypos' => array(
  32. '#type' => 'textfield',
  33. '#title' => t('Y offset'),
  34. '#default_value' => $data['ypos'],
  35. '#size' => 6,
  36. '#description' => $description2,
  37. '#element_validate' => array('imagecache_actions_validate_number'),
  38. ),
  39. );
  40. return $form;
  41. }
  42. /**
  43. * Form element validator that checks for a valid number.
  44. *
  45. * @param array $element
  46. * @param $form_state
  47. */
  48. function imagecache_actions_validate_number(&$element, &$form_state) {
  49. if (empty($element['#value'])) {
  50. form_set_value($element, 0, $form_state);
  51. }
  52. }
  53. /**
  54. * Form element validator that checks a transparency percentage value.
  55. *
  56. * @param array $element
  57. */
  58. function imagecache_actions_validate_alpha(&$element/*, &$form_status*/) {
  59. if (!is_numeric($element['#value']) || $element['#value'] < 1 || $element['#value'] > 100) {
  60. form_set_error(join('][', $element['#parents']), t('Opacity must be a number between 1 and 100.'));
  61. }
  62. }