You are here

function textimage_preset_edit in Textimage 7.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. 6.2 textimage_admin.inc \textimage_preset_edit()

Todo.

1 string reference to 'textimage_preset_edit'
textimage_menu in ./textimage.module
Implements hook_menu().

File

./textimage.admin.inc, line 62
Textimage admin page callback

Code

function textimage_preset_edit($form, &$form_state, $op, $pid = NULL) {
  $preset = array();
  $image_presets = array();
  if (isset($form_state['values'])) {
    $preset = $form_state['values'];
  }
  elseif ($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 = textimage_build_preview(array(
      'preset' => $preview,
      'text' => $preview_text,
      'additional_text' => $additional_text,
    ));
  }
  $form['preview'] = array(
    '#type' => 'item',
    '#markup' => '<strong>' . t('Preview') . ":</strong>\n" . '<div id="textimage-preview"><img src="' . file_create_url($preview) . '"></div>',
  );
  $form['settings'] = array(
    '#type' => 'vertical_tabs',
  );

  // 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/config/media/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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_hex_validate',
    ),
    '#required' => TRUE,
    '#attributes' => array(
      'class' => array(
        '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' => '%',
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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' => 'item',
    '#title' => t('Outline'),
  );
  $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',
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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' => array(
        'color',
      ),
    ),
    '#description' => t('Enter the hex color code you wish to use for the stroke outline (i.e. #000000)'),
    '#element_validate' => array(
      '_textimage_hex_validate',
    ),
  );

  // Margin.
  $form['settings']['text']['margin'] = array(
    '#type' => 'item',
    '#title' => t('Margin'),
    '#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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );

  // 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,
    '#element_validate' => array(
      '_textimage_hex_validate',
    ),
    '#attributes' => array(
      'class' => array(
        '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'] : '',
    '#ajax' => array(
      'callback' => 'textimage_js_preview',
    ),
    '#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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );
  $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,
    '#element_validate' => array(
      '_textimage_number_validate',
    ),
  );

  // Preview Textimage.
  $form['settings']['preview'] = array(
    '#type' => 'fieldset',
    '#title' => t('Preview settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['settings']['preview']['text'] = array(
    '#type' => 'item',
    '#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['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save preset'),
  );
  $form['actions']['preview'] = array(
    '#type' => 'button',
    '#value' => t('Preview'),
    '#ajax' => array(
      'callback' => 'textimage_js_preview',
    ),
  );
  return $form;
}