You are here

function textimage_preset_edit in Textimage 6.2

Same name and namespace in other branches
  1. 5.2 textimage_admin.inc \textimage_preset_edit()
  2. 5 textimage.module \textimage_preset_edit()
  3. 7.2 textimage.admin.inc \textimage_preset_edit()
2 string references to 'textimage_preset_edit'
textimage_menu in ./textimage.module
Implementation of hook_menu().
_textimage_vertical_tabs in ./textimage_admin.inc

File

./textimage_admin.inc, line 59

Code

function textimage_preset_edit(&$form_state, $op, $pid = NULL) {
  $preset = array();
  $image_presets = array();
  if ($op == 'edit' && is_numeric($pid)) {
    $preset = _textimage_preset_load($pid);
  }
  $fonts_path = variable_get('textimage_fonts_path', drupal_get_path('module', 'textimage') . '/fonts');
  $font_options = _textimage_font_list($fonts_path);
  $images_path = variable_get('textimage_images_path', drupal_get_path('module', 'textimage') . '/backgrounds');
  $image_files = drupal_map_assoc(_textimage_image_list($images_path));
  $presets = textimage_get_presets();
  $presets_list = array();
  foreach ($presets as $p) {
    $presets_list[$p->pid] = $p;
  }
  foreach ($presets as $p) {
    if ($p->pid != $pid) {
      $preset_test = $p;
      $preset_err = FALSE;
      while (is_numeric($preset_test->settings['background']['image'])) {
        if ($preset_test->settings['background']['image'] == $pid) {
          $preset_err = TRUE;
          break;
        }
        $preset_test = $presets_list[$preset_test->settings['background']['image']];
      }
      if (!$preset_err) {
        $image_presets[$p->pid] = $p->name;
      }
    }
  }
  $image_options = array(
    t('Default:') => array(
      '' => t('Default (use background color)'),
    ),
    t('Use the result of a Textimage Preset:') => $image_presets,
    t('Use a Background Image:') => $image_files,
  );
  $form = array();
  $form['#tree'] = TRUE;
  if ($op == 'edit') {
    $form['pid'] = array(
      '#type' => 'hidden',
      '#value' => $pid,
    );
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Preset Name'),
    '#description' => t('Preset names should be short and only use alphanumeric characters.'),
    '#default_value' => isset($preset['name']) ? $preset['name'] : '',
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('A short description displayed in the list of presets.'),
    '#default_value' => isset($preset['description']) ? $preset['description'] : '',
    '#rows' => 1,
  );

  // Preview textimage.
  $preview = '';
  if (count($preset) > 0) {
    $preview = $preset;
    $preview['pid'] = 0;
    $preview_text = isset($preview['settings']['preview']['text']['default']) ? $preview['settings']['preview']['text']['default'] : t('Preview');
    $additional_text = array();
    if (isset($preset['settings']['preview']['text']['additional'])) {
      $additional_text = $preset['settings']['preview']['text']['additional'];
      rsort($additional_text);
    }
    $preview = theme_textimage_image($preview, $preview_text, $additional_text, 'png', $preview_text, $preview_text);
  }
  $form['preview'] = array(
    '#value' => '<strong>' . t('Preview') . ":</strong>\n" . '<div id="textimage-preview">' . $preview . '</div>',
  );

  // Text settings
  $form['settings']['font'] = array(
    '#type' => 'fieldset',
    '#title' => t('Font settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['settings']['font']['file'] = array(
    '#type' => 'select',
    '#title' => t('Font'),
    '#options' => $font_options,
    '#default_value' => isset($preset['settings']['font']['file']) ? $preset['settings']['font']['file'] : '',
    '#description' => t('Select the font to be used in this image. If no fonts are listed, check the <a href="!path">settings for Fonts Path</a>.', array(
      '!path' => url('admin/build/textimage/settings'),
    )),
    '#required' => TRUE,
  );
  $form['settings']['font']['size'] = array(
    '#type' => 'textfield',
    '#title' => t('Size'),
    '#field_suffix' => t('px'),
    '#description' => t('Enter the size in pixels of the text to be generated.'),
    '#default_value' => isset($preset['settings']['font']['size']) ? $preset['settings']['font']['size'] : 16,
    '#maxlength' => 5,
    '#size' => 3,
    '#required' => TRUE,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'size',
      ),
    ),
  );
  $form['settings']['font']['color']['hex'] = array(
    '#type' => 'textfield',
    '#title' => t('Color'),
    '#description' => t('Enter the hex color code you wish to use for the generated text (i.e. #000000).'),
    '#default_value' => isset($preset['settings']['font']['color']['hex']) ? $preset['settings']['font']['color']['hex'] : '#000000',
    '#maxlength' => 7,
    '#size' => 8,
    '#validate' => array(
      '_textimage_hex_validate' => array(
        'color',
      ),
    ),
    '#required' => TRUE,
    '#attributes' => array(
      'class' => 'color',
    ),
  );
  $form['settings']['font']['color']['opacity'] = array(
    '#type' => 'textfield',
    '#title' => t('Opacity'),
    '#default_value' => isset($preset['settings']['font']['color']['opacity']) ? $preset['settings']['font']['color']['opacity'] : '100',
    '#maxlength' => 3,
    '#size' => 2,
    '#field_suffix' => '%',
  );
  $form['settings']['text'] = array(
    '#type' => 'fieldset',
    '#title' => t('Text settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['settings']['text']['maximum_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum width'),
    '#field_suffix' => t('px'),
    '#description' => t('Text lines wider than this will be wrapped. Leave blank to disable wrapping.'),
    '#default_value' => isset($preset['settings']['text']['maximum_width']) ? $preset['settings']['text']['maximum_width'] : '',
    '#maxlength' => 4,
    '#size' => 4,
  );
  $form['settings']['text']['fixed_width'] = array(
    '#type' => 'checkbox',
    '#title' => t('Fixed width?'),
    '#description' => t('If checked the size of generated image will always be equal to the max width') . '.',
    '#default_value' => isset($preset['settings']['text']['fixed_width']) ? $preset['settings']['text']['fixed_width'] : '',
  );
  $form['settings']['text']['align'] = array(
    '#type' => 'select',
    '#title' => t('Text Align'),
    '#description' => t('Only works when fixed width is enabled') . '.',
    '#options' => array(
      ALIGN_LEFT => t('Left'),
      ALIGN_CENTER => t('Center'),
      ALIGN_RIGHT => t('Right'),
    ),
    '#default_value' => isset($preset['settings']['text']['align']) ? $preset['settings']['text']['align'] : '',
  );
  $form['settings']['text']['angle'] = array(
    '#type' => 'textfield',
    '#title' => t('Text Rotation'),
    '#field_suffix' => t('&deg;'),
    '#description' => t('Enter the angle in degrees at which the text will be displayed. Positive numbers rotate the text clockwise, negative numbers counter-clockwise.'),
    '#default_value' => isset($preset['settings']['text']['angle']) ? $preset['settings']['text']['angle'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'angle',
      ),
    ),
  );
  $form['settings']['text']['case'] = array(
    '#type' => 'select',
    '#title' => t('Convert to case'),
    '#options' => array(
      '' => t('Default'),
      'upper' => t('UPPERCASE'),
      'lower' => t('lowercase'),
      'ucwords' => t('Uppercase Words'),
      'ucfirst' => t('Uppercase first'),
    ),
    '#description' => t('Covert the input text to a consistent format. The default makes no changes to input text.'),
    '#default_value' => isset($preset['settings']['text']['case']) ? $preset['settings']['text']['case'] : '',
  );

  // Outline
  $form['settings']['text']['stroke'] = array(
    '#type' => 'fieldset',
    '#title' => t('Outline'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['settings']['text']['stroke']['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#description' => t('Optionally add a stroke outline around the text. Enter the stroke width in pixels.'),
    '#default_value' => isset($preset['settings']['text']['stroke']['width']) ? $preset['settings']['text']['stroke']['width'] : 0,
    '#maxlength' => 2,
    '#size' => 3,
    '#field_suffix' => 'px',
    '#validate' => array(
      '_textimage_number_validate' => array(
        'stroke_width',
      ),
    ),
  );
  $form['settings']['text']['stroke']['color'] = array(
    '#type' => 'textfield',
    '#title' => t('Color'),
    '#default_value' => isset($preset['settings']['text']['stroke']['color']) ? $preset['settings']['text']['stroke']['color'] : '#000000',
    '#maxlength' => 7,
    '#size' => 8,
    '#attributes' => array(
      'class' => 'color',
    ),
    '#description' => t('Enter the hex color code you wish to use for the stroke outline (i.e. #000000)'),
    '#validate' => array(
      '_textimage_hex_validate' => array(
        'color',
      ),
    ),
  );

  // Margin
  $form['settings']['text']['margin'] = array(
    '#type' => 'fieldset',
    '#title' => t('Margin'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Specify the margin in pixels to be added around the generated text.'),
  );
  $form['settings']['text']['margin']['top'] = array(
    '#type' => 'textfield',
    '#title' => t('Top'),
    '#field_suffix' => t('px'),
    '#default_value' => isset($preset['settings']['text']['margin']['top']) ? $preset['settings']['text']['margin']['top'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'margin_top',
      ),
    ),
  );
  $form['settings']['text']['margin']['right'] = array(
    '#type' => 'textfield',
    '#title' => t('Right'),
    '#field_suffix' => t('px'),
    '#default_value' => isset($preset['settings']['text']['margin']['right']) ? $preset['settings']['text']['margin']['right'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'margin_right',
      ),
    ),
  );
  $form['settings']['text']['margin']['bottom'] = array(
    '#type' => 'textfield',
    '#title' => t('Bottom'),
    '#field_suffix' => t('px'),
    '#default_value' => isset($preset['settings']['text']['margin']['bottom']) ? $preset['settings']['text']['margin']['bottom'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'margin_bottom',
      ),
    ),
  );
  $form['settings']['text']['margin']['left'] = array(
    '#type' => 'textfield',
    '#title' => t('Left'),
    '#field_suffix' => t('px'),
    '#default_value' => isset($preset['settings']['text']['margin']['left']) ? $preset['settings']['text']['margin']['left'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'margin_left',
      ),
    ),
  );

  // Background Settings
  $form['settings']['background'] = array(
    '#type' => 'fieldset',
    '#title' => t('Background settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['settings']['background']['color'] = array(
    '#type' => 'textfield',
    '#title' => t('Color'),
    '#description' => t('Enter the hex color code you wish to use for the background of the generated image (i.e. #FFFFFF). Leave blank for transparent.'),
    '#default_value' => isset($preset['settings']['background']['color']) ? $preset['settings']['background']['color'] : '#FFFFFF',
    '#maxlength' => 7,
    '#size' => 8,
    '#validate' => array(
      '_textimage_hex_validate' => array(
        'color',
      ),
    ),
    '#attributes' => array(
      'class' => 'color',
    ),
  );
  $form['settings']['background']['image'] = array(
    '#type' => 'select',
    '#title' => t('Background Type'),
    '#options' => $image_options,
    '#default_value' => isset($preset['settings']['background']['image']) ? $preset['settings']['background']['image'] : '',
    '#ahah' => array(
      'path' => 'js/textimage/background',
      'wrapper' => 'textimage-preview-text',
    ),
    '#description' => t('Select the background type to be used in this image.'),
  );
  $form['settings']['background']['xoffset'] = array(
    '#type' => 'textfield',
    '#title' => t('Text X-Offset'),
    '#field_suffix' => 'px',
    '#default_value' => isset($preset['settings']['background']['xoffset']) ? $preset['settings']['background']['xoffset'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'xoffset',
      ),
    ),
  );
  $form['settings']['background']['yoffset'] = array(
    '#type' => 'textfield',
    '#title' => t('Text Y-Offset'),
    '#field_suffix' => 'px',
    '#description' => t('Specify the x and y coordinates on the image you where the top-left corner of the dynamic text should be positioned.'),
    '#default_value' => isset($preset['settings']['background']['yoffset']) ? $preset['settings']['background']['yoffset'] : 0,
    '#maxlength' => 4,
    '#size' => 4,
    '#validate' => array(
      '_textimage_number_validate' => array(
        'yoffset',
      ),
    ),
  );

  // Preview Textimage
  $form['settings']['preview'] = array(
    '#type' => 'fieldset',
    '#title' => t('Preview settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['settings']['preview']['text'] = array(
    '#prefix' => '<div id="textimage-preview-text">',
    '#suffix' => '</div>',
  );
  $form['settings']['preview']['text']['default'] = array(
    '#type' => 'textarea',
    '#title' => t('Preview text'),
    '#default_value' => isset($preset['settings']['preview']['text']['default']) ? $preset['settings']['preview']['text']['default'] : t('Preview'),
    '#rows' => 2,
  );

  // Add additional text fields.
  if (isset($preset['settings']['preview']['text']['additional'])) {
    foreach ($preset['settings']['preview']['text']['additional'] as $id => $value) {
      $form['settings']['preview']['text']['additional'][$id] = array(
        '#type' => 'textarea',
        '#title' => t('Additional text'),
        '#default_value' => isset($preset['settings']['preview']['text']['additional'][$id]) ? $preset['settings']['preview']['text']['additional'][$id] : t('Preview'),
        '#rows' => 2,
      );
    }
  }
  $form['settings']['preview']['button'] = array(
    '#type' => 'button',
    '#value' => t('Preview'),
    '#ahah' => array(
      'path' => 'js/textimage/preview',
      'wrapper' => 'textimage-preview',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  if (module_exists('vertical_tabs')) {
    _textimage_vertical_tabs($form);
  }
  return $form;
}