<?php
// $Id: adsense_injector.module,v 1.1.2.6 2007/04/06 16:05:55 inactivist Exp $

/**
 * @file
 * Inject adsense ads into node content automatically.
 */

define('ADSENSE_INJECTOR_MODULE_VERSION', '$Id: adsense_injector.module,v 1.1.2.6 2007/04/06 16:05:55 inactivist Exp $');
define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', TRUE);

define('ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT', '<div class="ad-auto-inserted" style="float:left; margin: 0 1em .25em 0;">[adsense:120x240:1:1]</div>%body<br class="clear"/>[adsense:468x60:1:1]');
define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75);

define('ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT', FALSE);
define('ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT', '%teaser<div class="adsense-injector-list-ad">[adsense:468x60:1:1]</div>');

/**
 * Prefix for variable table entries - append node type name, store as boolean
 * value, nonzero means insert ad content.
 */
define('ADSENSE_INJECTOR_INSERT_AD_NODETYPE', 'adsense_injector_nodetype_');

/**
 * Count words in a string.
 *
 * @param $str
 *   Target string.
 * @param $max
 *   Maximum number 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) {
  // lifted from node.module node_validate() function.
  return count(explode(' ', $str, $max));
}

/**
 * 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 ($op == 'alter') {
    if (module_exists('adsense') && _adsense_page_match() && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) {
      if ($page) {
        if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) {
          $_body = $node->body;
          $minwords = _adsense_injector_minwords_cfg($node->type);
          $wordcount = _adsense_injector_count_words($_body, $minwords);
          if ($wordcount >= $minwords) {
            $template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT);
            // Process adsense module tags in the template text, if enabled and possible.
            if (function_exists('_adsense_process_tags')) {
              $template = _adsense_process_tags($template);
            }
            else {
              watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
            }
            $node->body = strtr($template, array('%body' => $_body));
          }
          else {
            $node->body = "<!-- adsense_injector: node body word count ($wordcount) is insufficient ($minwords required), so we won't insert an ad. -->" . $_body;
          }
        }
      }
      elseif ($teaser && variable_get('adsense_injector_append_in_listview', ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT)) {
        $template = variable_get('adsense_injector_listview_insertion_template', ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT);
        // Process adsense module tags in the template text, if enabled and possible.
        if (function_exists('_adsense_process_tags')) {
          $template = _adsense_process_tags($template);
        }
        else {
          watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
        }
        $node->body = strtr($template, array('%teaser' => $node->teaser));
      }
    }
  }
}

/**
 * Implementation of hook_menu().
 */
function adsense_injector_menu() {
  $items['admin/settings/adsense_injector'] = array(
    'title' => 'AdSense Injector',
    'description' => 'Insert Google AdSense ads into full node views automatically.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('adsense_injector_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'adsense_injector.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
