Skip to content

Instantly share code, notes, and snippets.

@jpoesen
Created May 29, 2012 08:38
Show Gist options
  • Save jpoesen/2823321 to your computer and use it in GitHub Desktop.
Save jpoesen/2823321 to your computer and use it in GitHub Desktop.
Ads - module
<?php
/**
* @file turner_ads.module
*
* This module provides:
* - a configurable block for 1 ad
* - a block displaying a random ad
* - 2 image presets: ad_rectangle and ad_skyscraper
* - an admin screen to select which content type and field to use
*/
/**
* Implements hook_perm().
*/
function turner_ads_perm() {
return array('administer Turner ads');
}
/**
* Implements hook_menu().
*/
function turner_ads_menu() {
$items = array();
$items['admin/settings/turner/ads'] = array(
'title' => 'Turner Ad System settings',
'description' => 'Settings The Turner Ad System',
'page callback' => 'drupal_get_form',
'page arguments' => array('turner_ads_admin_settings'),
'access arguments' => array('administer Turner ads'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* An administrative form for our module.
*/
function turner_ads_admin_settings() {
$form = array();
// Add the content type radio button list
$form['turner_ads_content_type'] = array(
'#type' => 'radios',
'#title' => t('Ad Content type'),
'#description' => t('Select which content type will serve as advertisement.'),
'#options' => _turner_ads_list_content_types(),
'#default_value' => variable_get('turner_ads_content_type', 0),
'#weight' => '-100',
);
// Add the imagecache preset select list for rectangle ads
$form['turner_ads_preset_rectangle'] = array(
'#type' => 'select',
'#title' => t('Imagecache preset for rectangle ads'),
'#description' => t('Select which imagecache preset to use for rectangle ads.'),
'#options' => _turner_ads_list_imagecache_presets(),
'#default_value' => variable_get('turner_ads_preset_rectangle', 0),
'#weight' => '-50',
);
// exercise: preset select list for skyscraper ads goes here
// end exercise
return system_settings_form($form);
}
/**
* Implements hook_imagecache_default_presets().
*/
function turner_ads_imagecache_default_presets() {
$presets = array();
$presets['turner_ad_rectangle'] = array (
'presetname' => 'Turner Ad - rectangle',
'actions' =>
array (
0 =>
array (
'weight' => '0',
'module' => 'imagecache',
'action' => 'imagecache_scale_and_crop',
'data' =>
array (
'width' => '100',
'height' => '50',
),
),
),
);
$presets['turner_ad_skyscraper'] = array (
'presetname' => 'Turner Ad - skyscraper',
'actions' =>
array (
0 =>
array (
'weight' => '0',
'module' => 'imagecache',
'action' => 'imagecache_scale_and_crop',
'data' =>
array (
'width' => '100',
'height' => '400',
),
),
),
);
return $presets;
}
/**
* Implements hook_block().
*/
function turner_ads_block($op = 'list', $delta = '', $edit = array()) {
switch ($op) {
case 'list':
$blocks['turner_ads_rectangle'] = array(
'info' => t('Turner Ad: rectangle'),
);
$blocks['turner_ads_skyscraper'] = array(
'info' => t('Turner Ad - skyscraper'),
);
return $blocks;
case 'configure':
$form = array();
if ($delta == 'turner_ads_skyscraper') {
$form['turner_ads_skyscraper_node_id'] = array(
'#type' => 'select',
'#title' => t('Skyscraper ad node'),
'#description' => t('The node which contains the image to use for the skyscraper ad'),
'#options' => _turner_ads_skyscraper_node_list(),
'#default_value' => variable_get('turner_ads_skyscraper_node_id', 0),
);
}
return $form;
case 'save':
if ($delta == 'turner_ads_skyscraper') {
variable_set('turner_ads_skyscraper_node_id', $edit['turner_ads_skyscraper_node_id']);
}
return;
case 'view':
switch ($delta) {
case 'turner_ads_rectangle':
$block['subject'] = '<none>';
$block['content'] = turner_ads_block_contents($delta);
break;
// exercise: do the same for skyscraper ads here
// end exercise
}
return $block;
}
}
function turner_ads_block_contents($delta) {
switch ($delta) {
case 'turner_ads_rectangle':
// fetch all appropriate nodes
$term = _turner_ads_get_term('rectangle');
$result = taxonomy_select_nodes(array($term->tid));
$node_ids = array();
while ($record = db_fetch_object($result)) {
array_push($node_ids, $record->nid);
}
// randomize the list of nids
$random_array_key = array_rand($node_ids);
// load the node that contains our ad image
$nid = $node_ids[$random_array_key];
$node = node_load($nid);
// generate the image output
$preset = variable_get('turner_ads_preset_rectangle', '');
$path = $node->field_ad_picture[0]['filepath'];
$link = $node->field_ad_link[0]['value'];
// fetch imagecache preset to use for rectangle ad
if (module_exists('imagecache') && ($preset)) {
$img = theme('imagecache', $preset, $path);
} else {
// use normal theme image
$img = theme('image', $path);
}
$output = l($img, $link, array('html' => TRUE));
return $output;
case 'turner_ads_skyscraper':
// fetch the nid from the node selected in the skyscraper block admin form
$skyscraper_nid = variable_get('turner_ads_skyscraper_node_id', 0);
$node = node_load($skyscraper_nid);
$preset = variable_get('turner_ads_preset_skyscraper', '');
$path = $node->field_ad_picture[0]['filepath'];
$link = $node->field_ad_link[0]['value'];
// fetch imagecache preset to use for skyscraper ad
if (module_exists('imagecache') && ($preset)) {
$img = theme('imagecache', $preset, $path);
} else {
// use normal theme image
$img = theme('image', $path);
}
$output = l($img, $link, array('html' => TRUE));
return $output;
}
}
/**
* Helper function that lists all the currently defined content types.
*
* Returns: array.
*/
function _turner_ads_list_content_types() {
// see the Drupal API docs for more info on node_get_types().
$list = node_get_types();
// we have a list of content type objects but we're really only
// interested in their name. As it happens the machine name of
// the content types is used as the key for the array, so we can
// just use the list of the array's keys as our options list.
return array_keys($list);
}
/**
* Helper function that lists all the currently defined imagecache presets.
*
* Returns: array.
*/
function _turner_ads_list_imagecache_presets() {
$list = array();
if (module_exists('imagecache')) {
$presets = imagecache_presets();
$preset_names = array_keys($presets);
foreach ($preset_names as $preset_name) {
$list[$preset_name] = $preset_name;
}
}
return $list;
}
/**
* Helper function that lists all nodes of the content type chosen
* in the Turner Ads admin settings form.
*
* Returns: array.
*/
function _turner_ads_skyscraper_node_list() {
$list = array();
$term = _turner_ads_get_term('skyscraper');
// fetch all nodes cateforized with the skyscraper term
$result = taxonomy_select_nodes(array($term->tid));
if ($result->num_rows > 0) {
// prepare a nicely formatted list of nodes to return to the listbox field
while ($record = db_fetch_object($result)) {
$list[$record->nid] = $record->title;
}
}
return $list;
}
/**
* Helper function that returns the first term object that matches the name
*
* Returns: array.
*/
function _turner_ads_get_term($name) {
$terms = taxonomy_get_term_by_name($name);
return $terms[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment