You are here

simple_adsense.admin.inc in Simple AdSense 7

Configuration form.

File

simple_adsense.admin.inc
View source
<?php

/**
 * @file
 * Configuration form.
 */

/**
 * Form_simple_adsense.
 */
function simple_adsense_settings($form, &$form_state) {
  $form['#tree'] = TRUE;
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('Settings for simple adsense'),
  );
  $form['client'] = array(
    '#type' => 'textfield',
    '#title' => t('Client'),
    '#required' => TRUE,
    '#description' => 'Google AdSense Publisher Id. eg: pub-9513614146655499',
    '#default_value' => variable_get("simple_adsense_client", 'pub-9513614146655499'),
  );
  $slots = variable_get("simple_adsense_slots", NULL);
  if (empty($form_state['num_slots'])) {
    if (isset($slots)) {
      $form_state['num_slots'] = count($slots);
    }
    else {
      $form_state['num_slots'] = 1;
    }
  }
  for ($i = 1; $i <= $form_state['num_slots']; $i++) {
    $form['slot'][$i] = array(
      '#type' => 'textfield',
      '#title' => t("AD Unit @num's ID", array(
        '@num' => $i,
      )),
      '#required' => TRUE,
      '#description' => "AD Unit ID, eg: 5725906500",
      '#default_value' => isset($slots[$i]) ? $slots[$i] : NULL,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  $form['add_slot'] = array(
    '#type' => 'submit',
    '#value' => t('Add another AD Unit ID'),
    '#submit' => array(
      'simple_adsense_settings_add_slot',
    ),
  );
  if ($form_state['num_slots'] > 1) {
    $form['remove_slot'] = array(
      '#type' => 'submit',
      '#value' => t('Remove Last AD Unit ID'),
      '#submit' => array(
        'simple_adsense_settings_remove_slot',
      ),
      '#limit_validation_errors' => array(),
    );
  }
  return $form;
}

/**
 * Add form_simple_adsense_add_slot.
 */
function simple_adsense_settings_add_slot($form, &$form_state) {
  $form_state['num_slots']++;
  $form_state['rebuild'] = TRUE;
}

/**
 * Remove slot.
 */
function simple_adsense_settings_remove_slot($form, &$form_state) {
  if ($form_state['num_slots'] > 1) {
    $form_state['num_slots']--;
  }
  $form_state['rebuild'] = TRUE;
}

/**
 * Form validate.
 */
function simple_adsense_settings_validate($form, &$form_state) {
  for ($i = 1; $i <= $form_state['num_slots']; $i++) {
    $slot = $form_state['values']['slot'][$i];
    if (!preg_match('/^\\d+$/', $slot)) {
      form_set_error("slot][{$i}", t("AD Unit ID @i must be numbers", array(
        '@i' => $i,
      )));
    }
  }
  $client = $form_state['values']['client'];
  if (!preg_match('/^pub-\\d+$/', $client)) {
    form_set_error("client", t('Publisher ID must be like this: pub-9513614146655499'));
  }
}

/**
 * Form submit.
 */
function simple_adsense_settings_submit($form, &$form_state) {
  $slots = array();
  for ($i = 1; $i <= $form_state['num_slots']; $i++) {
    $slots[$i] = $form_state['values']['slot'][$i];
  }
  $client = $form_state['values']['client'];
  variable_set('simple_adsense_slots', $slots);
  variable_set('simple_adsense_client', $client);
  $output = t("Settings have been saved successfully, go to !blocks next.", array(
    "!blocks" => l(t("Blocks administration Page"), "admin/structure/block"),
  ));
  drupal_set_message($output);
}

Functions

Namesort descending Description
simple_adsense_settings Form_simple_adsense.
simple_adsense_settings_add_slot Add form_simple_adsense_add_slot.
simple_adsense_settings_remove_slot Remove slot.
simple_adsense_settings_submit Form submit.
simple_adsense_settings_validate Form validate.