You are here

function google_admanager_block in DFP Small Business (Google Ad Manager) 6

Same name and namespace in other branches
  1. 5 google_admanager.module \google_admanager_block()
  2. 6.3 google_admanager.module \google_admanager_block()
  3. 6.2 google_admanager.module \google_admanager_block()

Implementing hook_block

File

./google_admanager.module, line 10

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;
  }
  else {
    if ($op == 'view') {
      $ad_slots = _google_admanager_get_ad_slots();
      $block = array(
        'subject' => '',
        'content' => '',
      );
      if ($id = variable_get('google_admanager_account', '')) {
        if (isset($ad_slots[$delta])) {
          $block['content'] = theme('google_admanager_block', $id, $ad_slots[$delta]);
        }
        elseif (substr($delta, 0, 10) == 'superslot:') {
          $superslots = variable_get('google_admanager_superslots', array());
          if ($superslot = $superslots[substr($delta, 10)]) {
            foreach ($superslot as $ad_slot => $php) {
              if (eval($php)) {
                $block['content'] .= theme('google_admanager_block', $id, $ad_slot);
              }
            }
          }
        }
      }
      return $block;
    }
    else {
      if ($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;
      }
      else {
        if ($op = 'save') {
          if (!user_access('use PHP for block visibility') && substr($delta, 0, 10) !== 'superslot:') {
            return;
          }
          $superslot = array();
          foreach ($edit as $key => $value) {
            if (preg_match('/superslot_(\\d+)_adslot/', $key)) {
              $php = $edit[str_replace('adslot', 'php', $key)];
              drupal_set_message($value . ': ' . $php);
              if (!empty($php)) {
                $superslot[$value] = $php;
              }
            }
          }
          $superslots = variable_get('google_admanager_superslots', array());
          $superslots[substr($delta, 10)] = $superslot;
          variable_set('google_admanager_superslots', $superslots);
        }
      }
    }
  }
}