By omlx on
Hi,
I like simple ad module
http://drupal.org/node/149969
but it is for drupal 5, so I started to convert it drupal 6. now it's admin interface works but the ads don't show it the nodes.
I think there are somethings missed. have a look to my try:
<?php
/**
*/
define('MAX_ADS',2);
/**
* Implementation of hook_help().
*/
function simple_ad_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
$output = t('Inject advertising (Adsense, Amazon etc) into pages, blocks, nodes.');
break;
case 'admin/settings/simple_ad':
$output = t('<p>Here you can configure the simple ads.</p>');
break;
}
return $output;
}
/**
* Implementation of hook_perm().
*/
function simple_ad_perm() {
return array ('view simple ads');
}
/**
* Implementation of hook_settings().
*/
function simple_ad_admin_settings() {
$ad_options = array(0 => t('Disabled'), 1 => t('Top of node')
, 2 => t('Bottom of node'), 3 => t('In block for node type')
, 4 => t('In block for all pages'), 5 => t('In node body'),6 => t('In links'), );
$form['simple_ad_on'] = array(
'#type' => 'checkbox',
'#title' => t('Advertising active'),
'#return_value' => 1,
'#default_value' => variable_get('simple_ad_on', 0),
'#description' => t('Turn advertising off or on'),
);
$form['simple_ad_on_users'] = array(
'#type' => 'checkbox',
'#title' => t('Advertising active for logged in users'),
'#return_value' => 1,
'#default_value' => variable_get('simple_ad_on_users', 0),
);
$form['simple_ad_count'] = array(
'#type' => 'select',
'#multiple' => FALSE,
'#title' => t('How many ads'),
'#default_value' => variable_get('simple_ad_count', MAX_ADS),
'#options' => array(1 => 1,2,3,4,5,10 => 10, 15 => 15, 20 =>20, 30 => 30),
);
$form['where_to_show_the_ads'] = array(
'#type' => 'fieldset',
'#title' => t('Where to show the adverts'),
'#description' => t('Set the node types and categories you want to display ads for.<br \>'
.' You MUST select some types and/or categories else no ads will show.<br \>'
. ' Ad will show if type OR category is true'),
);
$form['where_to_show_the_ads']['simple_ad_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Node types'),
'#default_value' => variable_get('simple_ad_node_types', array()),
'#options' => node_get_types('names'),
);
$form['where_to_show_the_ads']['simple_ad_category_types'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Categories'),
'#default_value' => variable_get('simple_ad_category_types', array()),
'#options' => _simple_ad_get_terms(),
);
for ($i = 1; $i <= variable_get('simple_ad_count', MAX_ADS); $i++) {
$form['what_ads_'.$i] = array(
'#type' => 'fieldset',
'#title' => 'Ad '.$i.' ' .variable_get('simple_ad_descr_'.$i, ""),
'#collapsible' => TRUE,
'#collapsed'=> TRUE,
'#description' => t('Set the advertising HTML code.'),
);
$form['what_ads_'.$i]['simple_ad_'.$i] = array(
'#type' => 'textarea',
'#title' => 'Ad '.$i ,
'#default_value' => variable_get('simple_ad_'.$i, ""),
'#description' => t('The advertising HTML code'),
);
$form['what_ads_'.$i]['simple_ad_show_'.$i] = array(
'#type' => 'select',
'#title' => t('Show ad ').$i,
'#default_value' => variable_get('simple_ad_show_'.$i, 0),
'#options' => $ad_options,
'#description' => t('Where to show ad ').$i,
);
$form['what_ads_'.$i]['simple_ad_on_'.$i] = array(
'#type' => 'checkbox',
'#title' => t('Show ad to logged in users'),
'#return_value' => 1,
'#default_value' => variable_get('simple_ad_on_'.$i, 0),
);
$form['what_ads_'.$i]['simple_ad_descr_'.$i] = array(
'#type' => 'textfield',
'#title' => t('description'),
'#default_value' => variable_get('simple_ad_descr_'.$i, ""),
'#size' => 50,
'#maxlength' => 100,
'#description' => t('Describe this ad'),
);
}
return system_settings_form($form);
}
/**
* Implementation of hook_menu().
*/
function simple_ad_menu() {
$items = array();
$items['admin/settings/simplead'] = array(
'title' => 'Simple Ads',
'description' => 'Control which and where simple ads should be active.',
'page callback' => 'drupal_get_form',
'page arguments' => array('simple_ad_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Implementation of hook_nodeapi().
*/
function simple_ad_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'view':
if (_simple_ad_show() && _simple_ad_show($node->type, $node->nid) ) {
for ($i = 1; $i <= variable_get('simple_ad_count', MAX_ADS); $i++) {
if (_simple_ad_show_ad($i)) {
switch (variable_get('simple_ad_show_'.$i, 0)) {
case 1:
if ($page) {
$node->content['simple_ad_'.$i] = array(
'#value' => simple_ad_render($node, $i),
'#weight' => -10,
);
}
break;
case 2:
if ($page) {
$node->content['simple_ad_'.$i] = array(
'#value' => simple_ad_render($node, $i),
'#weight' => 10,
);
$node->content['body']['#value'] .= simple_ad_render($node, $i);
}
break;
case 3: //in block
break;
case 4: //in block
break;
case 5:
if ($page) {
$teaser = node_teaser($node->content['body']['#value']);
/* $body = $node->content['body']['#value'];
$teaser_start = strpos($body,$node->teaser);
$teaser_length = strlen($node->teaser);
$node->content['body']['#value']
= substr($body,0,$teaser_start + $teaser_length + 2)
. simple_ad_render($node, $i)
. substr($body,
$teaser_start + $teaser_length + 2) ;
*/
$node->content['body']['#value']
= $teaser
. simple_ad_render($node, $i)
. substr($node->content['body']['#value']
, strlen($teaser) );
}
break;
case 6: //in links
break;
default:
break;
}
}
}
}
break;
}
}
/**
* Implementation of hook_link().
*/
function simple_ad_link($type, $node = NULL, $teaser = FALSE) {
if (!_simple_ad_show()){
return;
}
$ads = array();
for ($i = 1; $i <= variable_get('simple_ad_count', MAX_ADS); $i++) {
if (_simple_ad_show_ad($i)) {
switch (variable_get('simple_ad_show_'.$i, 0)) {
case 6:
$ads_show = _simple_ad_show($node->type, $node->nid);
if ($type == 'node' && $ads_show && user_access('view simple ads')) {
$ads = simple_ad_render($node, $i);
}
break;
}
}
}
return $ads;
}
/**
* Implementation of hook_block().
*/
function simple_ad_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
for ($i = 1; $i <= variable_get('simple_ad_count', MAX_ADS); $i++) {
switch (variable_get('simple_ad_show_'.$i, 0)) {
case 3:
case 4:
$blocks[$i-1]['info'] = 'Ad '.$i
. ' ' . variable_get('simple_ad_descr_'.$i, "");
break;
}
}
return $blocks;
}
else if ($op == 'view') {
if (!_simple_ad_show()){
return;
}
$i = $delta + 1;
if (_simple_ad_show_ad($i)) {
switch (variable_get('simple_ad_show_'.$i, 0)) {
case 3:
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$ads_show = _simple_ad_show_type($node->type, $node->nid);
if ( $ads_show ) {
$block['subject'] = '';
$block['content'] = simple_ad_render($node, $i);
}
}
break;
case 4:
$block['subject'] = '';
$block['content'] = simple_ad_render($node, $i);
break;
}
return $block;
}
}
}
/**
* Function that renders the simple ads.
* This is the function themers want to call to insert the simple ads.
*/
function simple_ad_render($node, $i, $check_show = FALSE) {
if ($check_show) {
if (_simple_ad_show_ad($i) && _simple_ad_show_type($node->type,$node->nid)) {
} else {
return;
}
}
$ad = theme('simple_ad', variable_get('simple_ad_'.$i, '')
, $i
, variable_get('simple_ad_show_'.$i, 0));
return $ad;
}
function theme_simple_ad($ad, $i, $place) {
switch ( $place ) {
case 5 :
return '<p><span class="advert ad-5">'.$ad.'</span></p>';
break;
case 6 :
return array('simple_ad_'.$i => array(
'title' => $ad,
'attributes' => array('title' => t('advertisment'),'rel' => 'nofollow'),
'html' => TRUE
));
break;
default :
return '<span class="advert">'.$ad.'</span>';
}
return ;
}
/**
* Build a array of all taxonomy terms.
*/
function _simple_ad_get_terms() {
$types = array();
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $term) {
$types[$term->tid] = $term->name;
}
}
return $types;
}
/**
* Check if the simple ads should be displayed .
*/
function _simple_ad_show() {
if (!variable_get('simple_ad_on', 0)){
return FALSE;
}
global $user;
if (($user->uid > 0) && !(variable_get('simple_ad_on_users', 0))) {
return FALSE;
}
if (user_access('view simple ads')) {
return TRUE;
} else {
return FALSE;
}
}/**
* Check if the simple ads should be displayed for the node type/category.
*/
function _simple_ad_show_type($type, $nid) {
$ads_show = FALSE;
$node_type = in_array($type, variable_get('simple_ad_node_types', array()), TRUE);
if (module_exists('taxonomy')) {
$terms = taxonomy_node_get_terms($node);
foreach ($terms as $term) {
$category_type = in_array($term->tid, variable_get('simple_ad_category_types', array()), FALSE);
}
}
if ($node_type || $category_type) {
$ads_show = TRUE;
}
return $ads_show;
}
/**
* Check if the simple ads should be displayed for this ad.
*/
function _simple_ad_show_ad($i) {
global $user;
if ($user->uid > 0 && !variable_get('simple_ad_on_'.$i, 0)) {
return FALSE;
} else {
return TRUE;
}
}
the all module here:
http://itwadi.com/files/simple_ad.tar.gz