You are here

function ad_form in Advertisement 5.2

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

Drupal _form hook.

File

./ad.module, line 614
An advertising system for Drupal powered websites.

Code

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

  // determine the current ad type
  if (!isset($adtype)) {
    $adtypes = module_invoke_all('adapi', 'type', array());
    switch (sizeof($adtypes)) {
      case 0:
        drupal_set_message(t('At least one ad type module must be enabled before you can create advertisements.  For example, try %enabling the ad_text or ad_image module.', array(
          '%enabling' => l('enabling', 'admin/modules'),
        )), 'error');
        break;
      case 1:
        $adtype = $adtypes[0];
        break;
      default:
        if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'ad') {
          $adtype = arg(3) ? arg(3) : $edit['adtype'];
          $form['adtype'] = array(
            '#type' => 'radios',
            '#title' => t('Style of ad'),
            '#options' => drupal_map_assoc($adtypes),
            '#default_value' => $adtype ? $adtype : $adtypes[0],
            '#required' => TRUE,
            '#description' => t('Select the type of ad that you wish to create from the above options.'),
          );
        }
        break;
    }
  }

  // display type-specific options
  if (isset($adtype)) {
    $elements = module_invoke('ad_' . $adtype, 'adapi', 'form', $node);
    foreach ($elements as $element => $values) {
      $form["{$element}"] = $values;
    }
    $form['adtype'] = array(
      '#type' => 'hidden',
      '#value' => $adtype,
    );
  }

  // fieldset for updating ad status
  $form['adstatus'] = array(
    '#type' => 'fieldset',
    '#title' => t('Status'),
    '#collapsible' => TRUE,
  );

  // display all available status options
  foreach (ad_status_array($node->nid, $node->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",
      ),
    );
  }
  if (ad_permission($node->nid, 'access statistics')) {

    // display statistics
    $form['statistics'] = array(
      '#type' => 'fieldset',
      '#title' => t('Statistics'),
      '#collapsible' => TRUE,
    );
    $form['statistics']['data'] = array(
      '#type' => 'markup',
      '#prefix' => '<div>',
      '#suffix' => '</div>',
      '#value' => theme('ad_statistics_display', ad_statistics($node->nid)),
    );
  }

  // display scheduling options
  $form['schedule'] = array(
    '#type' => 'fieldset',
    '#title' => t('Scheduling'),
    '#collapsible' => TRUE,
    // Collapse if there isn't any scheduling data set.
    '#collapsed' => $node->autoactivate || $edit['autoactivate'] || $node->autoexpire || $edit['autoexpire'] || $node->maxviews || $edit['maxviews'] || $node->maxclicks || $edit['maxclicks'] ? FALSE : TRUE,
  );
  if (ad_permission($node->nid, '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' => $node->autoactivate ? format_date((int) $node->autoactivate, 'custom', 'F j, Y H:i') : '',
      '#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' => $node->autoexpire ? format_date((int) $node->autoexpire, 'custom', 'F j, Y H:i') : '',
      '#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 views'),
      '#required' => FALSE,
      '#size' => 10,
      '#maxlength' => 11,
      '#default_value' => $node->maxviews,
      '#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 views, 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' => $node->maxclicks,
      '#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' => $node->autoexpire,
    );
  }
  return $form;
}