Created
May 29, 2012 16:33
-
-
Save jpoesen/2829419 to your computer and use it in GitHub Desktop.
Turner ads UPDATED VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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', | |
); | |
if (module_exists('imagecache')){ | |
// 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 | |
// Add the imagecache preset select list for rectangle ads | |
$form['turner_ads_preset_skyscraper'] = array( | |
'#type' => 'select', | |
'#title' => t('Imagecache preset for skyscraper ads'), | |
'#description' => t('Select which imagecache preset to use for skyscraper ads.'), | |
'#options' => _turner_ads_list_imagecache_presets(), | |
'#default_value' => variable_get('turner_ads_preset_skyscraper', 0), | |
'#weight' => '-50', | |
); | |
} else { | |
$form['turner_ads_notice'] = array( | |
'#type' => 'markup', | |
'#value' => t('<p>You should enable imagecache for more options</p>'), | |
'#weight' => '-50', | |
); | |
} | |
// end exercise | |
return system_settings_form($form); | |
} | |
/** | |
* 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; | |
} | |
/** | |
* Implements hook_perm(). | |
*/ | |
function turner_ads_perm() { | |
return array('administer Turner ads'); | |
} | |
/** | |
* 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); | |
} | |
/** | |
* 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; | |
} | |
/** | |
* 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; | |
} | |
/** | |
* 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; | |
case 'turner_ads_skyscraper': | |
$block['subject'] = '<none>'; | |
$block['content'] = turner_ads_block_contents($delta); | |
break; | |
} | |
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 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