You are here

function ocupload_form_template in One Click Upload 7

Same name and namespace in other branches
  1. 7.2 ocupload.inc \ocupload_form_template()

Form add/edit template

1 string reference to 'ocupload_form_template'
ocupload_menu in ./ocupload.module
Implements hook_menu().

File

./ocupload.inc, line 50
Service functions

Code

function ocupload_form_template($form, &$form_state, $template = NULL) {
  if (!$template) {
    $template = (object) array(
      'tid' => 0,
      'mask' => '',
      'path' => '',
      'max_filesize' => 0,
      'template' => '<a href="!filepath">!filename</a>',
      'template_select' => '<a href="!filepath">!text</a>',
      'image_style' => '',
      'image_style_original' => '',
      'link_to_original' => 0,
      'link_template' => '<a href="!filepath" target="_blank">!image</a>',
      'link_only_big' => 0,
      'max_dimensions' => '',
      'rename_file' => 0,
    );
    drupal_set_title(t('Add template'));
  }
  $form['tid'] = array(
    '#type' => 'value',
    '#value' => $template->tid,
  );
  $form['mask'] = array(
    '#type' => 'textfield',
    '#title' => t('File extensions'),
    '#description' => t('Comma separated list of file extensions which should be handled within this template. Example: <code>jpg,gif,png</code>'),
    '#default_value' => $template->mask,
    '#size' => 40,
    '#required' => TRUE,
  );
  $public_path = variable_get('file_public_path', conf_path() . '/files');
  $form['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Upload path'),
    '#description' => t('Dir name, relative <code>!path</code>, with will upload files. Without the initial and trailing slashes.', array(
      '!path' => $public_path,
    )),
    '#default_value' => $template->path,
    '#field_prefix' => $public_path . '/',
    '#size' => 22,
    '#required' => TRUE,
  );
  if (module_exists('token')) {
    $form['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['token_help']['help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'user',
      ),
    );
  }
  $form['max_filesize'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum upload size'),
    '#default_value' => $template->max_filesize ? format_size($template->max_filesize, 'en') : 0,
    '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', array(
      '%limit' => format_size(file_upload_max_size()),
    )) . ' ' . t('Limit except for the user #1') . '.',
    '#size' => 10,
    '#element_validate' => array(
      '_ocupload_check_max_filesize',
    ),
  );
  $form['template'] = array(
    '#type' => 'textfield',
    '#title' => t('Template to insert into editor'),
    '#default_value' => $template->template,
    '#maxlength' => NULL,
    '#size' => 100,
    '#required' => TRUE,
  );
  $form['template_select'] = array(
    '#type' => 'textfield',
    '#title' => t('Template to replace selected text'),
    '#default_value' => $template->template_select,
    '#maxlength' => NULL,
    '#size' => 100,
    '#required' => TRUE,
  );
  $form['help'] = array(
    '#type' => 'fieldset',
    '#title' => t('Replacement patterns'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['help']['text'] = array(
    '#markup' => '
      !filepath     — ' . t('full path to file') . '<br />
      !filename     — ' . t('file name after upload') . '<br />
      !originalname — ' . t('original file name') . '<br />
      !fileext      — ' . t('file extension') . '<br />
      !filesize     — ' . t('file size') . '<br />
      !text         — ' . t('selected text') . '<br />
    ',
  );
  if (module_exists('image')) {
    $form['image_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Options for images'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['image_settings']['image_style'] = array(
      '#type' => 'select',
      '#title' => t('Use image style'),
      '#options' => image_style_options(),
      '#default_value' => $template->image_style,
    );
    $form['image_settings']['link_to_original'] = array(
      '#type' => 'checkbox',
      '#title' => t('Add link to original image'),
      '#default_value' => $template->link_to_original,
      '#states' => array(
        'invisible' => array(
          'select[name="image_style"]' => array(
            'value' => '',
          ),
        ),
      ),
    );
    $form['image_settings']['link_template'] = array(
      '#type' => 'textfield',
      '#title' => t('Link template'),
      '#default_value' => $template->link_template,
      '#states' => array(
        'visible' => array(
          'input[name="link_to_original"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['image_settings']['link_only_big'] = array(
      '#type' => 'checkbox',
      '#title' => t('Add link only for those images which size exceeds the size, specified in the style'),
      '#default_value' => $template->link_only_big,
      '#states' => array(
        'visible' => array(
          'input[name="link_to_original"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['image_settings']['use_image_style_original'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use image style for original image'),
      '#default_value' => (bool) $template->image_style_original,
      '#states' => array(
        'visible' => array(
          'input[name="link_to_original"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['image_settings']['image_style_original'] = array(
      '#type' => 'select',
      '#title' => t('Image style for original image'),
      '#options' => image_style_options(FALSE),
      '#default_value' => $template->image_style_original,
      '#states' => array(
        'visible' => array(
          'input[name="use_image_style_original"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['image_settings']['max_dimensions'] = array(
      '#type' => 'textfield',
      '#title' => 'Maximum image resolution',
      '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image.'),
      '#default_value' => $template->max_dimensions,
      '#size' => 10,
    );
  }
  $form['rename_file'] = array(
    '#type' => 'checkbox',
    '#title' => t('Rename uploaded file'),
    '#description' => t('Checked, if you want to rename uploded files according to current date/time.'),
    '#default_value' => $template->rename_file,
  );
  $form['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#description' => t('Checked roles which can upload files of this type.'),
    '#options' => array_map('check_plain', user_roles()),
    '#default_value' => array_keys(user_roles(FALSE, 'upload files use template ' . $template->tid)),
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $template->tid ? t('Save template') : t('Add template'),
  );
  return $form;
}