You are here

hide_submit_admin.inc in Hide submit button 5

Same filename and directory in other branches
  1. 6 hide_submit_admin.inc
  2. 7 hide_submit_admin.inc

Hide the submit button after clicked to prevent/reduce duplicate postings.

Admin pages and logic are defined in this file.

File

hide_submit_admin.inc
View source
<?php

/**
 * @file
 * Hide the submit button after clicked to prevent/reduce duplicate postings.
 * 
 * Admin pages and logic are defined in this file. 
 */

/**
 * Image deletion confirmation form
 *
 * @param $filename - The file to delete
 * @return drupal form array
 */
function hide_submit_admin_delete_image($filename) {
  $filepath = file_directory_path() . HIDE_SUBMIT_IMG_DIRECTORY . '/' . $filename;
  if (!file_exists($filepath)) {
    drupal_goto('admin/settings/hide-submit');
  }
  $form['hide_submit_image_file_path'] = array(
    '#type' => 'value',
    '#value' => $filepath,
  );
  $form['hide_submit_image_file_name'] = array(
    '#type' => 'value',
    '#value' => $filename,
  );
  $msg = t('Are you sure you want to delete the image file %name', array(
    '%name' => check_plain($filename),
  ));
  return confirm_form($form, $msg, 'admin/settings/hide-submit');
}

/**
 * Image deletion confirmation form submit handler
 *
 * @param $form
 * @param &$form_values
 */
function hide_submit_admin_delete_image_submit($form, $form_values) {
  $filepath = $form_values['hide_submit_image_file_path'];
  $filename = $form_values['hide_submit_image_file_name'];
  $image_dir = file_directory_path() . HIDE_SUBMIT_IMG_DIRECTORY;
  $default_img = drupal_get_path('module', 'hide_submit') . '/images/' . HIDE_SUBMIT_DEFAULT_IMAGE_BASENAME;
  if (file_exists($image_dir) && $filepath && file_check_location($filepath, $image_dir) && file_delete($filepath)) {
    drupal_set_message(t('%name deleted succesfully.', array(
      '%name' => check_plain($filename),
    )));
    $image = variable_get('hide_submit_image', $default_img);

    // After deleting we need to check if the deleted image was selected
    // or was it in the random list..
    if (is_array($image)) {
      $key = array_search($filepath, $image);
      if ($key !== FALSE) {

        // Remove from random list
        unset($image[$key]);
        if (count($image) >= 2) {
          variable_set('hide_submit_image', $image);
          drupal_set_message(t('Image removed from random list'));
        }
        else {
          $image = array_shift($image);
          variable_set('hide_submit_image', $image);
          variable_set('hide_submit_toggle_random', 0);
          variable_set('hide_submit_default_image', md5(basename($image)));
          drupal_set_message(t('Random feature disabled because only one image was selected'));
        }
      }
    }
    elseif ($image == $filepath) {
      variable_set('hide_submit_image', $default_img);
    }
    if (variable_get('hide_submit_default_image', FALSE) == md5($filename)) {
      variable_set('hide_submit_default_image', md5(HIDE_SUBMIT_DEFAULT_IMAGE_BASENAME));
      drupal_set_message(t('Image was selected as default, reverting to module\'s default image'));
    }
  }
  else {
    drupal_set_message(t('Cannot delete %name, something went wrong.', array(
      '%name' => check_plain($filename),
    )));
  }
}

/**
 * Implementation of admin_settings callback
*/
function hide_submit_admin_settings() {
  drupal_add_js(drupal_get_path('module', 'hide_submit') . '/hide_submit_admin_settings.js');
  $default_img = drupal_get_path('module', 'hide_submit') . '/images/' . HIDE_SUBMIT_DEFAULT_IMAGE_BASENAME;
  $image_dir = file_directory_path() . HIDE_SUBMIT_IMG_DIRECTORY;

  // Check for a new uploaded image, and use that instead.
  if ($file = file_save_upload('hide_submit_image_upload')) {
    if (file_check_directory($image_dir, FILE_CREATE_DIRECTORY, 'hide_submit_image_upload')) {
      if (file_copy($file, $image_dir, FILE_EXISTS_RENAME)) {
        $_POST['hide_submit_custom_image_link'] = $file->filepath;
        $_POST['hide_submit_toggle_custom_image'] = 0;
      }
    }
  }

  // Create the form
  $form = array();

  // --- PREVIEW ---
  $form['fieldset_preview'] = array(
    '#type' => 'fieldset',
    '#title' => t('Preview of current animation and message settings'),
    '#collapsible' => FALSE,
  );
  $form['fieldset_preview']['preview'] = array(
    '#type' => 'markup',
    '#id' => 'hide-submit-preview-user',
    '#value' => filter_xss_admin(_hide_submit_clean_for_javascript(_hide_submit_get_message())),
  );

  // --- LOAD OPTIONS FIELDSET ---
  $form['fieldset_js_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Script settings'),
    '#collapsible' => TRUE,
  );
  $form['fieldset_js_settings']['hide_submit_script_mode'] = array(
    '#type' => 'radios',
    '#title' => t('Script mode'),
    '#default_value' => variable_get('hide_submit_script_mode', HIDE_SUBMIT_MODE_HIDE),
    '#options' => array(
      HIDE_SUBMIT_MODE_HIDE => t('Hide buttons and display message'),
      HIDE_SUBMIT_MODE_DISABLE => t('Disable buttons'),
    ),
    '#description' => t("What the script shall do? hide the buttons or disable them"),
  );
  $form['fieldset_js_settings']['hide_submit_js_load_option'] = array(
    '#type' => 'radios',
    '#title' => t('Javascript load options'),
    '#default_value' => variable_get('hide_submit_js_load_option', HIDE_SUBMIT_DEFAULT_JS_LOAD),
    '#options' => array(
      HIDE_SUBMIT_IN_CONTENT_ADD_EDIT => t('Add/Edit node pages only'),
      HIDE_SUBMIT_IN_ALL_PAGES => t('Every page'),
      HIDE_SUBMIT_IN_LISTED_PAGES => t('Only on the listed pages.'),
      HIDE_SUBMIT_EXCLUDE_LISTED_PAGES => t('Every page except the listed pages.'),
    ),
    '#description' => t("Add/Edit node view will load for pages with url '/node/add' and '/node/*/edit'.  Every page, is well, EVERY PAGE.  For the last two options use the page list box to set a list of include/exclude urls."),
  );
  $form['fieldset_js_settings']['hide_submit_js_load_pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Page list'),
    '#default_value' => variable_get('hide_submit_js_load_pages', ''),
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );

  // --- MESSAGE  ---
  $form['fieldset_message'] = array(
    '#type' => 'fieldset',
    '#title' => t('Message'),
    '#collapsible' => TRUE,
    '#description' => t("The message to print after submit is clicked and the button become invisible. i.e. Please wait.."),
  );
  $form['fieldset_message']['hide_submit_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Default Message'),
    '#rows' => 2,
    '#default_value' => variable_get('hide_submit_message', t('Please wait...')),
  );

  // --- IMAGE SELECTION FIELDSET ---
  $form['fieldset_images'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image or animation'),
    '#collapsible' => FALSE,
    '#theme' => 'hide_submit_images_fieldset',
  );

  // jpg, jpeg, gif, png
  $mask = "^.+\\.(([jJ][pP][gG)|([jJ][pP][eE][gG])|([gG][iI][fF])|([pP][nN][gG]))\$";
  $default_images = file_scan_directory(drupal_get_path('module', 'hide_submit') . '/images', $mask);
  $user_images = file_scan_directory($image_dir, $mask);
  $options = array();
  foreach (array_merge($default_images, $user_images) as $image_file) {

    //$count = 1;
    $key = md5($image_file->basename);
    $options[$key] = '';
    $list_of_images[$key] = $image_file->filename;
    $form['fieldset_images']['hide_submit_images'][$key]['image'] = array(
      '#type' => 'markup',
      '#title' => $image_file->basename,
      '#value' => theme('image', $image_file->filename),
    );
  }
  $form['list_of_images'] = array(
    '#type' => 'value',
    '#value' => $list_of_images,
  );
  foreach ($user_images as $image_file) {
    $key = md5($image_file->basename);
    $form['fieldset_images']['hide_submit_images'][$key]['operations'] = array(
      '#value' => l(t('delete'), 'admin/settings/hide-submit/delete/' . $image_file->basename),
    );
  }
  $form['fieldset_images']['hide_submit_random'] = array(
    '#type' => 'checkboxes',
    '#default_value' => variable_get('hide_submit_random', array()),
    '#options' => $options,
  );
  $form['fieldset_images']['hide_submit_default_image'] = array(
    '#type' => 'radios',
    '#default_value' => variable_get('hide_submit_default_image', md5(HIDE_SUBMIT_DEFAULT_IMAGE_BASENAME)),
    '#options' => $options,
  );
  $form['fieldset_images']["hide_submit_toggle_custom_image"] = array(
    '#type' => 'checkbox',
    '#title' => t('Use custom link'),
    '#default_value' => variable_get('hide_submit_toggle_custom_image', 0),
    '#tree' => FALSE,
    '#description' => t('Check here if you want use a custom image link.'),
  );
  $form['fieldset_images']['hide_submit_custom_image_link'] = array(
    '#prefix' => '<div id="edit-hide-submit-custom-image-link-wrapper">',
    '#suffix' => '</div>',
    '#type' => 'textfield',
    '#title' => t('Custom image or animation'),
    '#default_value' => variable_get('hide_submit_custom_image_link', $default_img),
    '#description' => t("A link to an image, this image will be added to message.."),
  );

  // Upload picture
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['fieldset_images']['hide_submit_image_upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload picture'),
    '#size' => 48,
    '#description' => t('Upload image or gif animation to display with the message.'),
  );
  $form['fieldset_images']["hide_submit_toggle_random"] = array(
    '#type' => 'checkbox',
    '#title' => t('Randomize images'),
    '#default_value' => variable_get('hide_submit_toggle_random', 0),
    '#tree' => FALSE,
    '#description' => t('Check here if you want use random image each time the script loads. You need to check the <em>random</em> box for at least two images'),
  );

  // --- Advanced ---
  $form['fieldset_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['fieldset_advanced']['hide_submit_form_exclusion_mode'] = array(
    '#type' => 'radios',
    '#title' => t('Exclusion mode'),
    '#default_value' => variable_get('hide_submit_form_exclusion_mode', HIDE_SUBMIT_EXCLUDE_LISTED_FORMS),
    '#options' => array(
      HIDE_SUBMIT_EXCLUDE_LISTED_FORMS => t('Exclude listed forms'),
      HIDE_SUBMIT_EXCLUDE_UNLISTED_FORMS => t('Exclude unlisted forms'),
    ),
    '#description' => t("Choose exclusion mode for listed forms. <br /> NOTE: Forms that are not part of drupal Form-API will not be affected by this configuration"),
  );
  $form['fieldset_advanced']['hide_submit_form_exclusion'] = array(
    '#type' => 'textarea',
    '#title' => t('Form-id exclusion list'),
    '#default_value' => variable_get('hide_submit_form_exclusion', 'shoutbox_add_form'),
    '#description' => t("Here you can make a list of forms (form-id) you wish not to hide submit buttons . <br />For example you can exclude the login block submit button from hiding by typing <em>user_login_block</em>"),
  );
  $form['fieldset_advanced']['hide_submit_attribute_filter'] = array(
    '#type' => 'textarea',
    '#title' => t('Attribute Filter (Experimental)'),
    '#default_value' => variable_get('hide_submit_attribute_filter', ''),
    '#description' => t("Here you can make a list of attribute filters see %selectors for formatting details<br />For example to filter the 'Save as draft' button type [value!=Save As Draft]", array(
      '%selectors' => l(t('Attribute Filters:'), 'http://docs.jquery.com/Selectors'),
    )),
  );
  $form['fieldset_advanced']["hide_submit_debug"] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable debugging'),
    '#default_value' => variable_get('hide_submit_debug', FALSE),
    '#tree' => FALSE,
    '#description' => t('Debugging turned on will paint included and excluded submit buttons'),
  );
  $form['#validate'] = array(
    'hide_submit_admin_settings_validate' => array(),
  );

  // add extra submit handler (also run the default handler)
  $form['#submit'] = array(
    'system_settings_form_submit' => array(),
    '_hide_submit_admin_settings_submit' => array(),
  );
  return system_settings_form($form);
}

/**
 * Admin settings validation handler
 *
 * @param $form
 * @param &$form_values
 */
function hide_submit_admin_settings_validate($form, $form_values) {
  if ($form_values['hide_submit_toggle_random']) {
    $selected_count = 0;
    foreach ($form_values['hide_submit_random'] as $key => $v) {
      if ($v) {
        $selected_count++;
      }
    }
    if ($selected_count < 2) {
      form_set_error('hide_submit_toggle_random', t('Random feature requires at least two selected images'));
    }
  }
}

/**
 * Admin settings submit handler
 *
 * @param $form
 * @param &$form_values
 */
function _hide_submit_admin_settings_submit($form, $form_values) {
  $op = isset($form_values['op']) ? $form_values['op'] : '';
  if ($op == t('Reset to defaults')) {
    variable_del('hide_submit_image');
  }

  // Set custom link
  if ($form_values['hide_submit_toggle_custom_image']) {
    variable_set('hide_submit_image', $form_values['hide_submit_custom_image_link']);
    drupal_set_message(t("Custom image is set..."));
  }
  elseif ($form_values['hide_submit_toggle_random']) {
    drupal_set_message(t("Random image feature is set..."));
    $images_to_random = array();
    foreach ($form_values['hide_submit_random'] as $key => $v) {
      if ($v) {
        $images_to_random[] = $form_values['list_of_images'][$key];
      }
    }
    variable_set('hide_submit_image', $images_to_random);
  }
  else {
    variable_set('hide_submit_image', $form_values['list_of_images'][$form_values['hide_submit_default_image']]);
    drupal_set_message(t("Default image is set..."));
  }
}

//------------------------------------------------------------------------

//                          THEME FUNCTIONS

//------------------------------------------------------------------------

/**
 * Theme function for the admin settings image fieldset
 *
 * @param $form
 */
function theme_hide_submit_images_fieldset($form) {
  $images = $form['hide_submit_images'];
  foreach (element_children($images) as $key) {
    $filename = $images[$key]['image']['#title'];
    unset($form[$key]['image']['#title']);
    $row = array();
    $row[] = array(
      'data' => drupal_render($form['hide_submit_images'][$key]['image']) . '<br />' . $filename,
      'class' => 'hide-submit-image',
    );
    $row[] = array(
      'data' => drupal_render($form['hide_submit_random'][$key]),
      'class' => 'hide-submit-random',
    );
    $row[] = array(
      'data' => drupal_render($form['hide_submit_default_image'][$key]),
      'class' => 'hide-submit-radio',
    );
    $row[] = array(
      'data' => drupal_render($form['hide_submit_images'][$key]['operations']),
      'class' => 'hide-submit-operations',
    );
    $rows[] = $row;
  }
  $header = array(
    t('Image'),
    t('Random'),
    t('Default'),
    t('Operations'),
  );
  $output = drupal_render($form) . theme('table', $header, $rows);
  return $output;
}

Functions

Namesort descending Description
hide_submit_admin_delete_image Image deletion confirmation form
hide_submit_admin_delete_image_submit Image deletion confirmation form submit handler
hide_submit_admin_settings Implementation of admin_settings callback
hide_submit_admin_settings_validate Admin settings validation handler
theme_hide_submit_images_fieldset Theme function for the admin settings image fieldset
_hide_submit_admin_settings_submit Admin settings submit handler