You are here

function ad_form in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 ad.module \ad_form()
  2. 5 ad.module \ad_form()
  3. 6 ad.module \ad_form()
  4. 6.2 ad.module \ad_form()
  5. 7 ad.module \ad_form()

Implementation of hook_form().

File

./ad.module, line 496

Code

function ad_form(&$node, &$form_state) {
  $form = array();
  $type = node_get_types('type', $node);

  // When form_state is not empty, we should rather use it's values
  // to not loose data if validation fails.
  if (!empty($form_state['values'])) {
    $node = (object) $form_state['values'];
  }
  $form['aid'] = array(
    '#type' => 'value',
    '#value' => isset($node->nid) ? $node->nid : 0,
  );
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($type->title_label),
      '#required' => TRUE,
      '#default_value' => isset($node->title) ? $node->title : '',
      '#weight' => -5,
    );
  }
  if ($type->has_body) {
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  }
  else {

    // The body array has to exist otherwise the format field gets removed.
    $form['body'] = array(
      '#type' => 'hidden',
      '#value' => '',
    );
    $form['body_field']['format'] = filter_form($node->format);
  }

  // determine the current ad type
  if (!isset($adtype)) {
    $adtypes = ad_get_types();
    if (count($adtypes) == 1) {
      $adtype = key($adtypes);
    }
    else {
      if (!count($adtypes)) {
        drupal_set_message(t('At least one ad type module must be enabled before you can create advertisements.  For example, try <a href="!url">enabling</a> the ad_text or ad_image module.', array(
          '!url' => url('admin/build/modules'),
        )), 'error');
      }
    }
  }

  // display ad type switch
  if ((!isset($node->adtype) || isset($node->adtype_select)) && count($adtypes) > 1) {
    $adtypes = array(
      0 => '---',
    );
    $adtypes += ad_get_types('name');
    $form['select'] = array(
      '#type' => 'fieldset',
      '#title' => t('Select Ad type'),
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
      '#weight' => 3,
    );
    $form['select']['adtype_select_hidden'] = array(
      '#type' => 'hidden',
    );
    $form['select']['adtype_select'] = array(
      '#type' => 'select',
      '#required' => TRUE,
      '#options' => $adtypes,
      '#default_value' => isset($node->adtype_select) ? $node->adtype_select : '',
      '#attributes' => array(
        'onchange' => 'document.getElementById("edit-adtype-select-hidden").value = document.getElementById("edit-adtype-select").value;',
      ),
      '#ahah' => array(
        'path' => 'node/add/ad/ahah',
        'wrapper' => 'adtype-ahah-wrapper',
        'effect' => 'slide',
      ),
    );
  }

  // display type-specific options
  if (isset($node->adtype) && $node->adtype || count($adtypes) == 1) {
    if (isset($node->adtype_select) && $node->adtype_select && $node->adtype_select != $node->adtype) {
      $node->adtype = $node->adtype_select;
    }
    if (count($adtypes) == 1) {
      $node->adtype = $adtype;
    }
    ad_form_add_adtype_elements($form, $node->adtype, $node);

    // add ahah wrapper
    $form['adtype_elements']['#prefix'] = '<div id="adtype-ahah-wrapper">';
    $form['adtype_elements']['#suffix'] = '</div>';
  }
  if (!isset($form['adtype_elements'])) {
    $form['adtype_elements'] = array(
      '#value' => '<div id="adtype-ahah-wrapper"></div>',
      '#weight' => 3.1,
    );
  }

  // fieldset for updating ad status
  $form['adstatus'] = array(
    '#type' => 'fieldset',
    '#title' => t('Status'),
    '#collapsible' => TRUE,
    '#weight' => 4,
  );
  $nid = isset($node->nid) ? $node->nid : 0;
  $adstatus = isset($node->adstatus) ? $node->adstatus : '';

  // display all available status options
  foreach (ad_status_array($nid, $adstatus) as $status => $description) {
    $form['adstatus']["ad{$status}"] = array(
      '#type' => 'radio',
      '#title' => t("{$status}"),
      '#return_value' => $status,
      '#default_value' => isset($node->adstatus) ? $node->adstatus : 'pending',
      '#description' => "{$description}",
      '#parents' => array(
        "adstatus",
      ),
    );
  }

  // display scheduling options
  $form['schedule'] = array(
    '#type' => 'fieldset',
    '#title' => t('Scheduling'),
    '#collapsible' => TRUE,
    // Collapse if there isn't any scheduling data set.
    '#collapsed' => isset($node->autoactivate) || isset($form_state['values']['autoactivate']) || isset($node->autoexpire) || isset($form_state['values']['autoexpire']) || isset($node->maxviews) || isset($form_state['values']['maxviews']) || isset($node->maxclicks) || isset($form_state['values']['maxclicks']) ? FALSE : TRUE,
  );
  $date_api = FALSE;
  if (module_exists('date_popup')) {
    $date_api = TRUE;
  }
  if (isset($node->nid) && ad_permission($node->nid, 'manage status') || user_access('administer advertisements')) {
    if (!$date_api) {
      $form['schedule']['current'] = array(
        '#type' => 'markup',
        '#prefix' => '<div>',
        '#suffix' => '</div>',
        '#value' => t('The current date and time is "%date".', array(
          '%date' => format_date(time(), 'custom', 'F j, Y H:i'),
        )),
      );
    }
    $form['schedule']['autoactivate'] = array(
      '#type' => $date_api ? 'date_popup' : 'textfield',
      '#title' => t('Automatically activate ad'),
      '#required' => FALSE,
      '#description' => t('You can specify a date and time for this advertisement to be automatically activated.  The advertisement needs to be in an <em>approved</em> state before it can be automatically activated.  If you prefer to activate the advertisement immediately, leave this field empty.'),
    );
    if ($date_api) {
      $form['schedule']['autoactivate']['#date_format'] = 'F j, Y H:i';
      $form['schedule']['autoactivate']['#default_value'] = isset($node->autoactivate) && $node->autoactivate > 0 ? format_date((int) $node->autoactivate, 'custom', 'Y-m-j H:i') : '';
    }
    else {
      $form['schedule']['autoactivate']['#default_value'] = isset($node->autoactivate) && $node->autoactivate > 0 ? format_date((int) $node->autoactivate, 'custom', 'F j, Y H:i') : 0;
    }
  }
  if (user_access('administer advertisements')) {

    // admins can expire advertisements
    $form['schedule']['autoexpire'] = array(
      '#type' => $date_api ? 'date_popup' : 'textfield',
      '#title' => t('Automatically expire ad'),
      '#required' => FALSE,
      '#description' => t('You can specify a date and time for this advertisement to be automatically expired.  If you don\'t want the advertisement to expire, leave this field empty.'),
    );
    if ($date_api) {
      $form['schedule']['autoexpire']['#date_format'] = 'F j, Y H:i';
      $form['schedule']['autoexpire']['#default_value'] = isset($node->autoexpire) && $node->autoexpire > 0 ? format_date((int) $node->autoexpire, 'custom', 'Y-m-j H:i') : '';
    }
    else {
      $form['schedule']['autoexpire']['#default_value'] = isset($node->autoexpire) && $node->autoexpire > 0 ? format_date((int) $node->autoexpire, 'custom', 'F j, Y H:i') : 0;
    }
    $form['schedule']['maxviews'] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum impressions'),
      '#required' => FALSE,
      '#size' => 10,
      '#maxlength' => 11,
      '#default_value' => isset($node->maxviews) ? $node->maxviews : 0,
      '#description' => t('You can specify the maximum number of times this advertisement should be displayed, after which it will be automatically expired.  If you don\'t want this advertisement to expire after a certain number of impressions, leave this field set to %zero.', array(
        '%zero' => '0',
      )),
    );
    $form['schedule']['maxclicks'] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum clicks'),
      '#required' => FALSE,
      '#size' => 10,
      '#maxlength' => 11,
      '#default_value' => isset($node->maxclicks) ? $node->maxclicks : 0,
      '#description' => t('You can specify the maximum number of times this advertisement should be clicked, after which it will be automatically expired.  If you don\'t want this advertisement to expire after a certain number of clicks leave this field set to %zero.', array(
        '%zero' => '0',
      )),
    );
  }
  else {

    // display expiration time
    $form['schedule']['autoexpire_display'] = array(
      '#type' => 'markup',
      '#prefix' => '<div>',
      '#suffix' => '</div>',
      '#value' => theme('ad_status_display', $node),
    );
    $form['schedule']['autoexpire'] = array(
      '#type' => 'hidden',
      '#value' => isset($node->autoexpire) ? $node->autoexpire : 0,
    );
  }
  $form['#validate'][] = 'ad_select_adtype';
  return $form;
}