body field.
*
* It uses a very simple scheme at present (just string concatenation)
* and could be enhanced greatly by using regular expressions, templates,
* or other techniques in a later revision.
*/
define('ADSENSE_INJECTOR_MODULE_VERSION', '$Id: adsense_injector.module,v 1.1 2007/01/07 20:22:07 inactivist Exp $' );
define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', FALSE);
define('ADSENSE_INJECTOR_BODY_AD_PREFIX_DEFAULT', '
');
define('ADSENSE_INJECTOR_BODY_ADSENSE_FORMAT_DEFAULT', '200x200');
define('ADSENSE_INJECTOR_BODY_ADSENSE_GROUP_DEFAULT', 1);
define('ADSENSE_INJECTOR_BODY_ADSENSE_CHANNEL_DEFAULT', 1);
define('ADSENSE_INJECTOR_BODY_AD_SUFFIX_DEFAULT', '
');
define('ADSENSE_INJECTOR_BODY_AD_BODY_SUFFIX_DEFAULT', '
');
define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75);
/**
* Prefix for variable table entries - append node type name, store as boolean
* value, nonzero means insert ad content
*/
define('ADSENSE_INJECTOR_INSERT_BODY_AD_NODETYPE', 'adsense_injector_nodetype_');
/**
* Implementation of hook_help().
*/
function adsense_injector_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Automatically insert adsense ads into node content. Requires adsense.module');
}
}
/**
* Count words in a string.
* @param $str ref to a string (ref so no copy)
* @param $max max # of words we care about. Return value will never exceed this.
* @return the count of words, where delimiter is one or more spaces
* @todo Efficiency, find better way to do this
*/
function _adsense_injector_count_words(&$str, $max=100) {
return count(explode(' ', $str, $max)); // lifted from node.module node_validate() function.
}
/**
* Get the minimum node body wordcount for insertion.
* May be content-type specific, but at present, it's
* global to all node types.
* @param $nodetype the node type
* @param $defval the default value
* @return The minimum insertion wordcount
*/
function _adsense_injector_minwords_cfg($nodetype, $defval=75) {
return variable_get('adsense_injector_body_minwords', $defval);
}
/**
* Implementation of hook_nodeapi.
* If rendering a full page, and the node type one of the configured types,
* inject configured adsense content using simple string concatenation.
* @todo: Evaluate efficiency of string concat vs. sprintf, other methods.
*/
function adsense_injector_nodeapi(&$node, $op, $teaser, $page) {
// insert an ad into the body.
if ($page) {
if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)
&& variable_get(ADSENSE_INJECTOR_INSERT_BODY_AD_NODETYPE . $node->type, FALSE)
&& _adsense_page_match() ) {
$minwords = _adsense_injector_minwords_cfg($node->type);
$wordcount = _adsense_injector_count_words($node->body);
if ($wordcount >= $minwords) {
$node->body = ""
. variable_get('adsense_injector_body_ad_prefix', ADSENSE_INJECTOR_BODY_AD_PREFIX_DEFAULT)
. adsense_display(
variable_get('adsense_injector_body_adsense_format', ADSENSE_INJECTOR_BODY_ADSENSE_FORMAT_DEFAULT),
variable_get('adsense_injector_body_adsense_group', ADSENSE_INJECTOR_BODY_ADSENSE_GROUP_DEFAULT),
variable_get('adsense_injector_body_adsense_channel', ADSENSE_INJECTOR_BODY_ADSENSE_CHANNEL_DEFAULT))
. variable_get('adsense_injector_body_ad_suffix', ADSENSE_INJECTOR_BODY_AD_SUFFIX_DEFAULT)
. $node->body
. variable_get('adsense_injector_body_ad_body_suffix', ADSENSE_INJECTOR_BODY_AD_BODY_SUFFIX_DEFAULT);
} else {
$node->body = "" . $node->body;
}
}
}
}
/**
* Implementation of hook_settings
*/
function adsense_injector_settings() {
$form['module_banner'] = array('#type' => 'markup',
'#value' => 'Module development sponsored by
Exodus Development');
$form['module_id'] = array('#type' => 'markup', '#value' => ADSENSE_INJECTOR_MODULE_VERSION .'
');
$form['node_ad_body_insertion'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Node body ad insertion'),
'#description' => t('Requires adsense.module'),
);
{
$form['node_ad_body_insertion']['adsense_injector_insert_body_ad'] =
array('#type' => 'checkbox',
'#title'=> t('Insert inline ad in node body on page views'),
'#default_value' => variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT),
'#description' => t('Description'),
'#required'=>FALSE);
$form['node_ad_body_insertion']['adsense_injector_body_minwords'] =
array('#type' => 'textfield',
'#title'=> t('Minimum node body word count'),
'#default_value' => variable_get('adsense_injector_body_minwords', ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT),
'#description' => t('The minimum node body word count threshold - only inject if node body has at least this many words.'));
$form['node_ad_body_insertion']['adsense_injector_body_adsense_format'] =
array('#type' => 'textfield',
'#title'=> t('Ad Format'),
'#default_value' => variable_get('adsense_injector_body_adsense_format', ADSENSE_INJECTOR_BODY_ADSENSE_FORMAT_DEFAULT),
'#description' => t('Adsense module ad format string (example: 200x200) - see adsense.module help for list of supported formats.'));
$form['node_ad_body_insertion']['adsense_injector_body_adsense_group'] =
array('#type' => 'textfield',
'#title'=> t('Ad group'),
'#default_value' => variable_get('adsense_injector_body_adsense_group', ADSENSE_INJECTOR_BODY_ADSENSE_GROUP_DEFAULT),
'#description' => t('Adsense module group'));
$form['node_ad_body_insertion']['adsense_injector_body_adsense_channel'] =
array('#type' => 'textfield',
'#title'=> t('Ad channel'),
'#default_value' => variable_get('adsense_injector_body_adsense_channel', ADSENSE_INJECTOR_BODY_ADSENSE_CHANNEL_DEFAULT),
'#description' => t('Adsense module channel'));
$form['node_ad_body_insertion']['formatting'] =
array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Special Formatting'),
'#description' => t('Formatting options'),
);
$form['node_ad_body_insertion']['formatting']['adsense_injector_body_ad_prefix'] =
array('#type' => 'textarea',
'#title'=> t('Ad Prefix'),
'#rows' => 3,
'#cols' => 40,
'#default_value' => variable_get('adsense_injector_body_ad_prefix', ADSENSE_INJECTOR_BODY_AD_PREFIX_DEFAULT),
'#description' => t('Text to insert before adsense content'),
'#required' => TRUE);
$form['node_ad_body_insertion']['formatting']['adsense_injector_body_ad_suffix'] =
array('#type' => 'textarea',
'#title'=> t('Ad Suffix'),
'#rows' => 3,
'#cols' => 40,
'#default_value' => variable_get('adsense_injector_body_ad_suffix', ADSENSE_INJECTOR_BODY_AD_SUFFIX_DEFAULT),
'#description' => t('Text to insert after adsense content'));
$form['node_ad_body_insertion']['formatting']['adsense_injector_body_ad_body_suffix'] =
array('#type' => 'textarea',
'#title'=> t('Ad Body Suffix'),
'#rows' => 3,
'#cols' => 40,
'#default_value' => variable_get('adsense_injector_body_ad_body_suffix', ADSENSE_INJECTOR_BODY_AD_BODY_SUFFIX_DEFAULT),
'#description' => t('Text to append to node body'));
$form['node_ad_body_insertion']['nodes'] =
array('#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Node Types'),
'#description' => t('Nodes types to display inline ads'),
);
/**
* Enumerate node types, and set up form fields for each
*/
$enabled_count = 0;
foreach (node_get_types() as $type => $name) {
$nodetypes[$type] = $name;
$enabled = variable_get(ADSENSE_INJECTOR_INSERT_BODY_AD_NODETYPE . $type, FALSE);
if ($enabled) $enabled_count++;
$form['node_ad_body_insertion']['nodes'][ADSENSE_INJECTOR_INSERT_BODY_AD_NODETYPE . $type] =
array('#type' => 'checkbox',
'#title'=> $name,
'#default_value' => $enabled,
'#description' => t('Display inline ads on %nodetype nodes', array('%nodetype' => $name)),
'#required'=>FALSE);
}
// do some sanity checking
if ($enabled_count == 0) {
$form['node_ad_body_insertion']['nodes']['#collapsed'] = FALSE;
$form['node_ad_body_insertion']['nodes']['no_nodes_enabled'] = array('#type' => 'markup',
'#value' => 'No node types selected (no ads will be inserted!)
');
}
}
return $form;
}