You are here

function ad_form in Advertisement 6

Same name and namespace in other branches
  1. 5.2 ad.module \ad_form()
  2. 5 ad.module \ad_form()
  3. 6.3 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 500
An advertising system for Drupal powered websites.

Code

function ad_form($node, &$form_state) {
  $form = array();
  $type = arg(3);
  if (function_exists('ad_' . $type . '_display_ad')) {
    $adtype = $type;
  }
  else {
    $adtype = isset($node->adtype) ? $node->adtype : '';
  }
  $form['aid'] = array(
    '#type' => 'value',
    '#value' => isset($node->nid) ? $node->nid : 0,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => isset($node->title) ? $node->title : '',
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($node->body) ? $node->body : '',
    '#rows' => 3,
  );
  $form['body_filter']['format'] = filter_form($node->format);

  // determine the current ad type
  if (!isset($adtype)) {
    $adtypes = ad_get_types();
    if (sizeof($adtypes) == 1) {
      $adtype = key($adtypes);
    }
    else {
      if (!sizeof($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 type-specific options
  if (isset($adtype)) {
    $elements = module_invoke('ad_' . $adtype, 'adapi', 'form', $node);
    if (is_array($elements)) {
      foreach ($elements as $element => $values) {
        $form[$element] = $values;
      }
    }
    $form['adtype'] = array(
      '#type' => 'hidden',
      '#value' => $adtype,
    );
  }
  if (user_access('administer advertisements')) {

    // admins can set any status on advertisements
    $form['adstatus'] = array(
      '#type' => 'fieldset',
      '#title' => t('Status'),
      '#collapsible' => TRUE,
    );
    foreach (ad_status_array() 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',
        ),
      );
    }
  }
  else {
    if (ad_adaccess($node, 'manage status')) {
      if (!$node->adstatus || $node->adstatus == 'pending') {
        $adstatus = ad_status_array();
        $node->adstatus = 'pending';
        $form['adstatus'] = array(
          '#type' => 'fieldset',
          '#title' => t('Status'),
          '#collapsible' => TRUE,
        );
        $form['adstatus']['display'] = array(
          '#type' => 'markup',
          '#value' => '<p><strong>' . t('Status') . ':</strong> ' . t($node->adstatus) . '<br />' . t($adstatus[$node->adstatus]),
        );
        $form['adstatus']['adpending'] = array(
          '#type' => 'value',
          '#value' => $node->adstatus,
        );
      }
      else {
        $adstatus = ad_status_array(FALSE);

        // display status options
        $form['adstatus'] = array(
          '#type' => 'fieldset',
          '#title' => t('Status'),
          '#collapsible' => TRUE,
        );
        foreach ($adstatus as $status => $description) {
          $form['adstatus']["ad{$status}"] = array(
            '#type' => 'radio',
            '#title' => t($status),
            '#return_value' => $status,
            '#default_value' => $node->adstatus ? $node->adstatus : 'pending',
            '#description' => $description,
            '#parents' => array(
              "adstatus",
            ),
          );
        }
      }
    }
    else {
      $adstatus = ad_status_array();
      if (!isset($node->adstatus)) {
        $node->adstatus = 'pending';
      }
      $form['ad_adstatus'] = array(
        '#type' => 'fieldset',
        '#title' => t('Status'),
        '#collapsible' => TRUE,
      );
      $form['ad_adstatus']['adstatus_display'] = array(
        '#type' => 'markup',
        '#value' => '<p><strong>' . t('Status') . ':</strong> ' . t($node->adstatus) . '<br />' . t($adstatus[$node->adstatus]),
      );
      $form['adstatus'] = array(
        '#type' => 'value',
        '#value' => $node->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,
  );
  if (ad_adaccess($node, 'manage status')) {
    $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' => 'textfield',
      '#title' => t('Automatically activate ad'),
      '#required' => FALSE,
      '#default_value' => isset($node->autoactivate) && $node->autoactivate > 0 ? format_date((int) $node->autoactivate, 'custom', 'F j, Y H:i') : 0,
      '#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 (user_access('administer advertisements')) {

    // admins can expire advertisements
    $form['schedule']['autoexpire'] = array(
      '#type' => 'textfield',
      '#title' => t('Automatically expire ad'),
      '#required' => FALSE,
      '#default_value' => isset($node->autoexpire) && $node->autoexpire > 0 ? format_date((int) $node->autoexpire, 'custom', 'F j, Y H:i') : 0,
      '#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.'),
    );
    $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,
    );
  }
  return $form;
}