Hello !!

I have a little question about serving ads with the openx module.

The way I would like to go is the following :

I have a drupal website with some roles. One role is "brand", and it is used for users who are "brands" of specific goods. We have 1000+ users with role "brand".
Each brand can give us some ads. We add it into OpenX server. For each brand, we can have some ads with different dimensions.

Now, on the drupal website, each brand has a homepage, and we would like to display advertising of the current brand on it's homepage. Example :

Cartier give us two ads : 300x200 and 300x600. We add it to openX to have two zone ID : 25 and 26.
Now, I would like to display theses ads by calling dynamically a block and give it the correct zone ID, ie :

$block = module_invoke( 'openads', 'block', 'view', 'dynamic-ads-25' );
print $block['content'];

My problem is actually, we need to go to the openX administration page and fill a row for each zone. If each brand give us two ads, we can have 2000+ rows to fill and be in a real hell to display correct block on correct page.

The goal is to have a custom profile field to link zones to users, and add in my "homepage" template file a code like above, by retrieving the correct zone of the user and put it as argument of module_invoke method.

With this kind of construction, we can display correct zone for correct user without creating all zones in openX administration page of the module and configure blocks.

Here a code to implement it in a sub-module (called dynamic_openads) with only hook_block defined, but I think it can be easily added in the original module, if you think it's interesting :

/**
 * Implementation of hook_block
 * 
 * Add a new block that themer can display using 
 *    $delta = 'dynamic-ads-2';
 *    $block = module_invoke( 'dynamic_openads', 'block', 'view', $delta );
 *    print $block['content'];
 *
 * @param $op
 * @param $delta : use 'dynamic-ads-{id}' or 'dynamic-ads-{id}-{code}'
 * @param $edit
 * @return array
 */
function dynamic_openads_block($op = 'list', $delta = 0, $edit = array() ) {
  
  $tmp = $delta;
  // test if the delta is "dynamic-ads"
  if( substr( $tmp, 0, 11 ) == "dynamic-ads" ) {
    
    // if it's case, rewrite delta as 'dynamic-ads' 
    $delta = substr( $tmp, 0, 11 );
    
    // and extract id and eventually code from delta value
    $extra = substr( $tmp, 12 );
    $t = explode( "-", $extra );
    $args = array();
    $args['id'] = $t[0];
    $args['code'] = isset( $t[1] ) ? $t[1] : "";
  }

  
  // op = list, set minimal configuration for the dynamic block
  // user shouldn't use the block configuration admin page to display
  // it in a specific region. This block must be called dynamically
  // with module_invoke method
  if( $op == 'list' ) {
  
    $blocks['dynamic-ads'] = array(
      'info' => t('Dynamic openads block'),
      'cache' => BLOCK_NO_CACHE,
      'enabled' => 0,
    );
    return $blocks;
  }
  
  else if( $op == 'view' ) {
    
    switch( $delta ) {
      case 'dynamic-ads' :
        // create the block by calling theme function from openads module and 
        // give id and code to retrieve correct ads
        $block['subject'] = strtoupper( t( "Dynamic openads block" ) );
        $block['content'] = theme('openads_invoke', $args["id"], $args["code"] );
        break;
    }
    
    return $block;
  }
}

Now I can use

$block = module_invoke( 'dynamic_openads', 'block', 'view', 'dynamic-ads-25' );
print $block['content'];

To get the ads with zone-id = 25. Just use 'dynamic-ads-{id}' or 'dynamic-ads-{id}-{code}' to retrieve the ads you want from the openX server.

The only thing you can't is filter display by role, because the block isn't listed in the openx administration page of the module.

What do you think about ?

Comments

titouille’s picture

Project: OpenX » Openads