You are here

function _site_disclaimer_admin_decode_settings in Site Disclaimer 7

Same name and namespace in other branches
  1. 6 site_disclaimer.admin.inc \_site_disclaimer_admin_decode_settings()

Multiple-use function to process settings for:

  • initial settings form build
  • settings form validation
  • settings form rebuild (as part of '#ajax' process)
2 calls to _site_disclaimer_admin_decode_settings()
site_disclaimer_admin_settings in ./site_disclaimer.admin.inc
Menu callback; show settings form.
site_disclaimer_admin_settings_validate in ./site_disclaimer.admin.inc
Validate the site_disclaimer_admin_settings form.

File

./site_disclaimer.admin.inc, line 39
Administration settings for Site Disclaimer module.

Code

function _site_disclaimer_admin_decode_settings(&$form_state, $validate = FALSE) {
  $form_state += array(
    'values' => array(),
  );
  $is_form_submitted = !empty($form_state['values']['op']);
  $orig_mode = isset($form_state['#site_disclaimer_mode']) ? $form_state['#site_disclaimer_mode'] : (isset($form_state['values']['site_disclaimer_node_id']) ? 'node_id' : 'node_title');
  $mode = $orig_mode;
  $form_state['#site_disclaimer_mode_switch'] = FALSE;
  if ($is_form_submitted) {
    if ($form_state['values']['op'] == SITE_DISCLAIMER_NODE_ID) {
      $mode = 'node_id';
      $form_state['#site_disclaimer_mode_switch'] = $mode;
    }
    elseif ($form_state['values']['op'] == SITE_DISCLAIMER_NODE_TITLE) {
      $mode = 'node_title';
      $form_state['#site_disclaimer_mode_switch'] = $mode;
    }
    if (!$validate) {

      // Ignore the switch while validating
      $mode = $orig_mode;
    }
  }
  if (!$is_form_submitted && !$validate) {

    // Start the form in preferred node title mode
    $mode = 'node_title';
  }
  $form_state['#site_disclaimer_mode'] = $mode;
  $title = '';
  $nid = '';
  if ($orig_mode == 'node_title') {
    if (!$is_form_submitted) {
      $title = variable_get('site_disclaimer_node_title', '');
    }
    elseif (!empty($form_state['values']['site_disclaimer_node_title'])) {
      $title = $form_state['values']['site_disclaimer_node_title'];
    }
    if (!empty($title)) {
      $nodes = node_load_multiple(array(), array(
        'title' => $title,
      ));
      if (empty($nodes)) {
        if ($is_form_submitted) {
          form_set_error('site_disclaimer_node_title', t('No post was found with "!title" title.', array(
            '!title' => $title,
          )));
        }
      }
      else {
        $node = array_shift($nodes);
        $nid = $node->nid;
      }
    }
  }
  else {

    // $orig_mode == 'node_id'
    if (!$is_form_submitted) {
      $nid = variable_get('site_disclaimer_node_id', '');
    }
    elseif (!empty($form_state['values']['site_disclaimer_node_id'])) {
      $nid = $form_state['values']['site_disclaimer_node_id'];
    }
    if (!empty($nid)) {
      $node = node_load($nid);
      if (empty($node)) {
        if ($is_form_submitted) {
          form_set_error('site_disclaimer_node_id', t('No post was found with nid !nid.', array(
            '!nid' => $nid,
          )));
        }
      }
      else {
        $title = $node->title;
      }
    }
  }
  $form_state['values']['site_disclaimer_node_title'] = $title;
  $form_state['values']['site_disclaimer_node_id'] = $nid;
  $form_state['#site_disclaimer_nid'] = $nid;
}