You are here

function _google_admanager_block in DFP Small Business (Google Ad Manager) 6.2

Same name and namespace in other branches
  1. 6.3 google_admanager.admin.inc \_google_admanager_block()

Callback for hook_block()

1 call to _google_admanager_block()
google_admanager_block in ./google_admanager.module
Implementation of hook_block().

File

./google_admanager.admin.inc, line 227
Admin functionality

Code

function _google_admanager_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks = array();
    if (!variable_get('google_admanager_noblock', FALSE)) {
      $ad_slots = _google_admanager_get_ad_slots();
      foreach ($ad_slots as $delta => $name) {
        $blocks[$delta] = array(
          'info' => 'GAM Ad slot: ' . $name,
          'cache' => BLOCK_NO_CACHE,
        );
      }
    }
    $superslots = variable_get('google_admanager_superslots', array());
    foreach ($superslots as $name => $slots) {
      $blocks['superslot:' . $name] = array(
        'info' => 'GAM Superslot: ' . $name,
        'cache' => BLOCK_NO_CACHE,
      );
    }
    return $blocks;
  }
  elseif ($op == 'configure') {

    // Reuse the 'use PHP for block visibility' from block.module
    if (!user_access('use PHP for block visibility') || substr($delta, 0, 10) !== 'superslot:') {
      return;
    }
    $superslots = variable_get('google_admanager_superslots', array());
    $name = substr($delta, 10);
    if (!isset($superslots[$name])) {
      return;
    }
    $form = array();
    $form['visibility'] = array(
      '#type' => 'fieldset',
      '#title' => t('Ad slots visibility'),
      '#description' => t('Use PHP code to define ad slot visibility. For example, to display an ad slot only to anonymous user, use <em>return empty($GLOBALS[\'user\']->uid);</em>. Or, to simple enable an ad slot, use <em>return TRUE;</em>'),
    );
    $ad_slots = array_values(_google_admanager_get_ad_slots());
    $ad_slots = array_combine($ad_slots, $ad_slots);
    $superslot = $superslots[$name];

    // Create 5 empty slots configuration
    // @TODO: It could be better to implement AHAH form
    $superslot += array(
      'fake slot1' => '',
      'fake slot2' => '',
      'fake slot3' => '',
    );
    $i = 1;
    foreach ($superslot as $ad_slot => $php) {
      $form['visibility']['superslot_' . $i . '_adslot'] = array(
        '#type' => 'select',
        '#title' => t('Ad slot'),
        '#default_value' => $ad_slot,
        '#options' => $ad_slots,
      );
      $form['visibility']['superslot_' . $i++ . '_php'] = array(
        '#type' => 'textfield',
        '#title' => t('PHP code for visibility condition'),
        '#default_value' => $php,
      );
    }
    return $form;
  }
  elseif ($op == 'save') {
    if (!user_access('use PHP for block visibility') || substr($delta, 0, 10) !== 'superslot:') {
      return;
    }

    // When save account settings, delete blocks which belongs to the ad slots that have been removed
    $superslot = array();
    foreach ($edit as $key => $value) {
      if (preg_match('/superslot_(\\d+)_adslot/', $key)) {
        $php = $edit[str_replace('adslot', 'php', $key)];
        if (!empty($php)) {
          $superslot[$value] = $php;
        }
      }
    }
    $superslots = variable_get('google_admanager_superslots', array());
    $superslots[substr($delta, 10)] = $superslot;
    variable_set('google_admanager_superslots', $superslots);
  }
}