You are here

function ed_classified_form in Classified Ads 5.2

Same name and namespace in other branches
  1. 5 ed_classified.module \ed_classified_form()
  2. 6.2 ed_classified.module \ed_classified_form()
  3. 7.2 ed_classified.module \ed_classified_form()

Implementation of hook_form(). This is how we piggyback off of the node type by tweaking the form with our elements. Drupal 5.x version is called $form_values rather than $form_state (both are an array).

File

./ed_classified.module, line 756
Simple text-based classified ads module. Michael Curry, Exodus Development, Inc. exodusdev@gmail.com for more information, please visit http://exodusdev.com/drupal/modules/classified.module Copyright (c) 2006, 2007 Exodus Development, Inc. All Rights…

Code

function ed_classified_form(&$node, $form_state) {
  module_load_include('inc', 'ed_classified', 'ed_classified_utils');
  $type = node_get_types('type', $node);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
  );
  $max_body_length = _ed_classified_variable_get('ad_standard_body_length', EDI_CLASSIFIED_VAR_DEF_BODYLEN_LIMIT);
  $current_body_length = strlen(trim($node->body));
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($type->body_label),
    '#default_value' => $node->body,
    '#required' => TRUE,
    '#description' => t('The main body text of your ad.  Please note that ads are limited to !limit characters or less.', array(
      '!limit' => $max_body_length,
    )),
    '#rows' => 10,
    // Add a length counter so people know they have gone over.
    // http://lists.evolt.org/archive/Week-of-Mon-20040315/156773.html
    // TODO: This should be a part of the jstools module...
    // TODO: need a function and handle onblur, focus, etc. so clipboard paste works.
    // Use behaviors js rather than this clunky code?
    // onkeypress doesn't detect all keys in IE.  Use onkeydown/onkeyup
    // TODO: use "className" for IE6
    // http://www.webmasterworld.com/forum91/5280.htm
    '#attributes' => array(
      'onkeyup' => "document . getElementById('_edi_classified_body_length_remaining') . innerHTML=this.value.length;document.getElementById('_edi_classified_body_length_remaining') . setAttribute('class', this.value.length <= {$max_body_length} ? 'classified-bodylength-ok' : 'classified-bodylength-exceeded');",
    ),
  );
  $form['body_filter']['body_length_remaining'] = array(
    '#type' => 'markup',
    '#value' => t('Body characters used: <span id="_edi_classified_body_length_remaining">%initial_value</span> of %max_value', array(
      '%initial_value' => $current_body_length,
      '%max_value' => $max_body_length,
    )),
    '#prefix' => '<span id="classified-bodylength-msg">',
    '#suffix' => '</span>',
  );

  // END counter code
  $form['body_filter']['filter'] = filter_form($node->format);

  /* Set up expiration info fieldset */
  if ($node->expires_on > 0) {

    // if not creating ad for the first time
    $form['expiration_fieldset'] = array(
      '#type' => 'fieldset',
      '#title' => t('Ad Expiration'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['expiration_fieldset']['expires_on_text'] = array(
      '#value' => theme('ed_classified_ending_date', $node->expires_on),
    );

    // TODO: fix this - using hidden field to preserve value
    $form['expiration_fieldset']['expires_on'] = array(
      '#type' => 'hidden',
      '#value' => $node->expires_on,
    );

    // if has proper perms, allow editing of expiration date as appropriate
    if (user_access('reset classified ad expiration')) {
      $form['expiration_fieldset']['reset_expiration'] = array(
        '#type' => 'checkbox',
        '#default_value' => '0',
        '#title' => t('Reset ad expiration (extend expiration date)'),
        '#description' => t('If this is checked, the ad\'s expiration date will be reset upon saving changes.  Duration may depend on assigned categories.'),
      );
    }
  }

  // TODO: Enter additional form elements
  // TODO: Expiration, contact info, category (from the classified taxonomy dedicated to classified)
  // if user has appropriate access, display and allow change of state, expiration date.
  if (user_access('administer classified ads')) {

    // show state here.
  }

  // append our form submit handler
  $form['#submit'][] = '_ed_classified_form_submit';
  return $form;
}