You are here

function shoutbox_add_form in Shoutbox 7

Same name and namespace in other branches
  1. 5 shoutbox.module \shoutbox_add_form()
  2. 6.2 shoutbox.module \shoutbox_add_form()
  3. 6 shoutbox.module \shoutbox_add_form()
  4. 7.2 shoutbox.module \shoutbox_add_form()

Generates form for adding shouts.

1 string reference to 'shoutbox_add_form'
shoutbox_view in ./shoutbox.module
View the shoutbox.

File

./shoutbox.module, line 451
Shoutbox module displays a block for users to create short messages for the whole site. Uses AHAH to update the database and display content.

Code

function shoutbox_add_form($form) {
  global $user;

  // Check permissions before showing input form.
  if (!(_shoutbox_user_access('post shouts') || _shoutbox_user_access('post shouts without approval'))) {
    $form[] = array(
      '#type' => 'item',
      '#markup' => theme('shoutbox_post_forbidden'),
    );
    return $form;
  }

  // If we're viewing a shoutbox page that is being paged, don't
  // show the form, because an ajax update won't make sense. Instead,
  // give a link back to the unpaged page.
  if ($_GET['q'] == 'shoutbox' && isset($_GET['page'])) {
    return array(
      'shoutbox_return_link' => array(
        '#type' => 'item',
        // Use $_GET['q'] because the page might not be just 'shoutbox'.
        '#markup' => '« ' . l(t('Return to the shoutbox'), $_GET['q']),
      ),
    );
  }
  $max = variable_get('shoutbox_max_length', 255);
  $form['wrapper'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
  );

  // Placeholder for inline error messages.
  $form['wrapper']['error_message'] = array(
    '#type' => 'item',
    '#markup' => '<div id="shoutbox-error" class="messages error" title="Click to close"></div>',
  );

  // The shout field.
  $form['wrapper']['message'] = array(
    '#type' => variable_get('shoutbox_widget_type', 'textfield'),
    '#attributes' => array(
      'placeholder' => variable_get('shoutbox_default_message'),
    ),
    '#size' => "",
    '#maxlength' => $max ? $max : NULL,
  );
  $form['wrapper']['nick_name_area'] = array(
    '#type' => 'item',
  );
  $form['wrapper']['nick_submit_area'] = array(
    '#type' => 'item',
  );

  // The nickname field.
  $default_nick = t(DEFAULTNICK);
  if (variable_get('shoutbox_defaultname', 1) && $user->uid) {
    $default_nick = $user->name;
    if ($user->uid > 0) {

      // See if we can use a custom profile field for the name.
      if ($field = variable_get('shoutbox_profile_name', '')) {
        $aElements = explode("<split>", $field);
        if (count($aElements) == 2) {
          if ($aElements[0] == "profile") {
            $name = db_query("SELECT v.value FROM {profile_value} v INNER JOIN {profile_field} f ON v.fid = f.fid\n                        WHERE f.name = :name AND v.uid = :uid", array(
              ':name' => $aElements[1],
              ':uid' => $user->uid,
            ))
              ->fetchField();
            $default_nick = $name ? $name : $user->name;
          }
          elseif ($aElements[0] == "custom") {
            $name = db_query("SELECT f.field_" . $aElements[1] . "_value FROM field_data_field_" . $aElements[1] . " as f\n                            WHERE f.entity_type='user' AND f.bundle='user'\n                            AND f.entity_id=:id", array(
              ':id' => $user->uid,
            ))
              ->fetchField();
            $default_nick = $name ? $name : $user->name;
          }
        }
      }
    }
    $form['wrapper']['nick_name_area']['nick'] = array(
      '#type' => 'textfield',
      '#default_value' => $default_nick,
      '#maxlength' => $max ? $max : NULL,
      '#size' => "",
      '#attributes' => array(
        'disabled' => 'disabled',
      ),
    );
  }
  else {
    if ($default_nick == t(DEFAULTNICK)) {
      $form['wrapper']['nick_name_area']['nick'] = array(
        '#type' => 'textfield',
        '#attributes' => array(
          'placeholder' => $default_nick,
        ),
        '#maxlength' => $max ? $max : NULL,
        '#size' => "",
      );
    }
    else {
      $form['wrapper']['nick_name_area']['nick'] = array(
        '#type' => 'textfield',
        '#default_value' => $default_nick,
        '#maxlength' => $max ? $max : NULL,
        '#size' => "",
        '#required' => TRUE,
      );
    }
  }

  //  // Placeholder for ajax throbber image.
  //  $form['wrapper']['nick_submit_area']['throbber'] = array(
  //    '#type' => 'item',
  //    '#markup' => '<div id="shoutbox-throbber" class="throbber"></div>'
  //  );
  $bAjaxSubmitEnable = variable_get('shoutbox_submit_type', 'post') == 'ajax' ? 1 : 0;

  // The submit button.
  if ($bAjaxSubmitEnable) {
    $form['wrapper']['nick_submit_area']['submit'] = array(
      '#type' => 'submit',
      '#value' => t(DEFAULTSHOUTSINGULAR_CC),
      '#ajax' => array(
        'callback' => '_handle_form_submit',
        'effect' => 'fade',
        'progress' => array(
          'message' => NULL,
          'type' => 'throbber',
        ),
        'event' => 'click',
      ),
    );
  }
  else {
    $form['wrapper']['nick_submit_area']['submit'] = array(
      '#type' => 'submit',
      '#value' => t(DEFAULTSHOUTSINGULAR_CC),
    );
  }
  $interval = shoutbox_get_refresh_rate();
  if ($interval) {
    $form['wrapper']['interval_message'] = array(
      '#type' => 'item',
      '#interval' => $interval,
      '#theme' => 'shoutbox_interval_message',
    );
  }
  $form['shoutbox_ajax'] = array(
    '#type' => 'hidden',
    '#value' => $bAjaxSubmitEnable,
    '#attributes' => array(
      'id' => 'shoutbox_ajax',
    ),
  );
  $shout = new stdClass();

  // Allow modules to alter the form.
  shoutbox_invoke('form', $shout, $form);
  $form['#prefix'] = '<div class="shoutbox-add-form">';
  $form['#suffix'] = '</div>';
  return $form;
}