/[drupal]/contributions/modules/adsense_injector/adsense_injector.module
ViewVC logotype

Diff of /contributions/modules/adsense_injector/adsense_injector.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1 by inactivist, Sun Jan 7 20:22:07 2007 UTC revision 1.2 by hswong3i, Tue Dec 16 08:11:55 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id: adsense_injector.module,v 1.1.2.6 2007/04/06 16:05:55 inactivist Exp $
   /*  $Id: adsense_injector.module 149 2007-01-07 17:54:26Z mcurry $ */  
   
   /**  
    * Inject adsense ads into node content automatically.  
    *  
    * Copyright (c) 2006 Exodus Development, Inc.  All Rights Reserved.  
    * Licensed under the terms of the GNU Public License (GPL) version 2.  Please see LICENSE.txt for  
    * license terms.  Posession and use of this code signifies acceptance of license  
    * terms.  
    *  
    * Visit Exodus Development at  
    * http://exodusdev.com exodusdev@gmail.com  
    *  
    * Project homepage:  
    * http://exodusdev.com/drupal/4.7/modules/adsense_injector.module  
    *  
    * The purpose of this module is to provide a simple method of automatically placing 'inline'  
    * adsense module content into a full-page view of selected node types.  
    * It does this by intercepting node rendering using hook_nodeapi during 'page' rendering,  
    * and injecting selected text into the $node->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 149 2007-01-07 17:54:26Z mcurry $' );  
 define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', FALSE);  
 define('ADSENSE_INJECTOR_BODY_AD_PREFIX_DEFAULT', '<div style="float:left; margin: 0 1em .25em 0;" class="ad-auto-inserted">');  
 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', '</div>');  
 define('ADSENSE_INJECTOR_BODY_AD_BODY_SUFFIX_DEFAULT', '<br class="clear" />');  
 define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75);  
3    
4  /**  /**
5   * Prefix for variable table entries - append node type name, store as boolean   * Inject adsense ads into node content automatically.
6   * value, nonzero means insert ad content   *
7     * Copyright (c) 2006, 2007 Exodus Development, Inc. All Rights Reserved.
8     * Licensed under the terms of the GNU Public License (GPL) version 2. Please see LICENSE.txt for
9     * license terms. Posession and use of this code signifies acceptance of license
10     * terms.
11     *
12     * Visit Exodus Development at
13     * http://exodusdev.com exodusdev@gmail.com
14     *
15     * Project homepage:
16     * http://exodusdev.com/drupal/4.7/modules/adsense_injector.module
17     *
18     * The purpose of this module is to provide a simple method of automatically placing 'inline'
19     * adsense module content into a full-page view of selected node types.
20     * It does this by intercepting node rendering using hook_nodeapi during 'page' rendering,
21     * and injecting selected text into the $node->body field.
22     *
23     * It uses a very simple scheme at present (just string concatenation)
24     * and could be enhanced greatly by using regular expressions, templates,
25     * or other techniques in a later revision.
26   */   */
27    
28  define('ADSENSE_INJECTOR_INSERT_BODY_AD_NODETYPE', 'adsense_injector_nodetype_');  define('ADSENSE_INJECTOR_MODULE_VERSION', '$Id: adsense_injector.module,v 1.1.2.6 2007/04/06 16:05:55 inactivist Exp $');
29    define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', TRUE);
30    
31    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]');
32    define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75);
33    
34    define('ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT', FALSE);
35    define('ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT', '%teaser<div class="adsense-injector-list-ad">[adsense:468x60:1:1]</div>');
36    
37  /**  /**
38   * Implementation of hook_help().   * Prefix for variable table entries - append node type name, store as boolean
39     * value, nonzero means insert ad content.
40   */   */
41  function adsense_injector_help($section) {  define('ADSENSE_INJECTOR_INSERT_AD_NODETYPE', 'adsense_injector_nodetype_');
   switch ($section) {  
     case 'admin/modules#description':  
       return t('Automatically insert adsense ads into node content.  <strong>Requires adsense.module</strong>');  
   }  
 }  
42    
43  /**  /**
44   * Count words in a string.   * Count words in a string.
45   * @param $str ref to a string (ref so no copy)   *
46   * @param $max max # of words we care about.  Return value will never exceed this.   * @param $str
47   * @return the count of words, where delimiter is one or more spaces   *   Target string.
48     * @param $max
49     *   Maximum number of words we care about. Return value will never exceed
50     *   this.
51     * @return
52     *   The count of words, where delimiter is one or more spaces.
53   * @todo Efficiency, find better way to do this   * @todo Efficiency, find better way to do this
54   */   */
55  function _adsense_injector_count_words(&$str, $max=100) {  function _adsense_injector_count_words($str, $max) {
56    return count(explode(' ', $str, $max)); // lifted from node.module node_validate() function.    // lifted from node.module node_validate() function.
57      return count(explode(' ', $str, $max));
58  }  }
59    
60  /**  /**
61   * Get the minimum node body wordcount for insertion.   * Get the minimum node body wordcount for insertion.
62   * May be content-type specific, but at present, it's   *
63   * global to all node types.   * May be content-type specific, but at present, it's global to all node
64   * @param $nodetype the node type   * types.
65   * @param $defval the default value   *
66   * @return The minimum insertion wordcount   * @param $nodetype
67     *   The node type.
68     * @param $defval
69     *   The default value.
70     * @return
71     *   The minimum insertion wordcount.
72   */   */
73  function _adsense_injector_minwords_cfg($nodetype, $defval=75) {  function _adsense_injector_minwords_cfg($nodetype, $defval = 75) {
74    return variable_get('adsense_injector_body_minwords', $defval);    return variable_get('adsense_injector_body_minwords', $defval);
75  }  }
76    
77  /**  /**
78   * Implementation of hook_nodeapi.   * Implementation of hook_nodeapi().
79     *
80   * If rendering a full page, and the node type one of the configured types,   * If rendering a full page, and the node type one of the configured types,
81   * inject configured adsense content using simple string concatenation.   * inject configured adsense content using simple string concatenation.
82     *
83   * @todo: Evaluate efficiency of string concat vs. sprintf, other methods.   * @todo: Evaluate efficiency of string concat vs. sprintf, other methods.
84   */   */
85  function adsense_injector_nodeapi(&$node, $op, $teaser, $page) {  function adsense_injector_nodeapi(&$node, $op, $teaser, $page) {
86    // insert an ad into the body.    // insert an ad into the body.
87    if ($page) {    if ($op == 'alter') {
88      if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)      if (module_exists('adsense') && _adsense_page_match() && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) {
89          && module_exist('adsense')        if ($page) {
90          && variable_get(ADSENSE_INJECTOR_INSERT_BODY_AD_NODETYPE . $node->type, FALSE)          if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) {
91          && _adsense_page_match() ) {            $_body = $node->body;
92        $minwords = _adsense_injector_minwords_cfg($node->type);            $minwords = _adsense_injector_minwords_cfg($node->type);
93        $wordcount = _adsense_injector_count_words($node->body);            $wordcount = _adsense_injector_count_words($_body, $minwords);
94        if ($wordcount >= $minwords) {            if ($wordcount >= $minwords) {
95          $node->body = "<!-- adsense_injector: node body wordcount = $wordcount, required=$minwords - insertion triggered. -->"              $template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT);
96            . variable_get('adsense_injector_body_ad_prefix', ADSENSE_INJECTOR_BODY_AD_PREFIX_DEFAULT)              // Process adsense module tags in the template text, if enabled and possible.
97            . adsense_display(              if (function_exists('_adsense_process_tags')) {
98                              variable_get('adsense_injector_body_adsense_format', ADSENSE_INJECTOR_BODY_ADSENSE_FORMAT_DEFAULT),                $template = _adsense_process_tags($template);
99                              variable_get('adsense_injector_body_adsense_group', ADSENSE_INJECTOR_BODY_ADSENSE_GROUP_DEFAULT),              }
100                              variable_get('adsense_injector_body_adsense_channel', ADSENSE_INJECTOR_BODY_ADSENSE_CHANNEL_DEFAULT))              else {
101            . variable_get('adsense_injector_body_ad_suffix', ADSENSE_INJECTOR_BODY_AD_SUFFIX_DEFAULT)                watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
102            . $node->body              }
103            . variable_get('adsense_injector_body_ad_body_suffix', ADSENSE_INJECTOR_BODY_AD_BODY_SUFFIX_DEFAULT);              $node->body = strtr($template, array('%body' => $_body));
104        } else {            }
105          $node->body = "<!-- adsense_injector: node body word count ($wordcount) is insufficient ($minwords required), so we won't insert an ad. -->" . $node->body;            else {
106                $node->body = "<!-- adsense_injector: node body word count ($wordcount) is insufficient ($minwords required), so we won't insert an ad. -->" . $_body;
107              }
108            }
109          }
110          elseif ($teaser && variable_get('adsense_injector_append_in_listview', ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT)) {
111            $template = variable_get('adsense_injector_listview_insertion_template', ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT);
112            // Process adsense module tags in the template text, if enabled and possible.
113            if (function_exists('_adsense_process_tags')) {
114              $template = _adsense_process_tags($template);
115            }
116            else {
117              watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
118            }
119            $node->body = strtr($template, array('%teaser' => $node->teaser));
120        }        }
121      }      }
122    }    }
123  }  }
124    
   
125  /**  /**
126   * Implementation of hook_settings   * Implementation of hook_menu().
127   */   */
128  function adsense_injector_settings() {  function adsense_injector_menu() {
129    $form['module_banner'] = array('#type' => 'markup',    $items['admin/settings/adsense_injector'] = array(
130                                   '#value' => '<div style="border: solid 1px #eee; margin: .5em; padding: .5em;" <strong>Module development sponsored by <a href="http://exodusdev.com">Exodus Development</a></strong><br/>');      'title' => 'AdSense Injector',
131    $form['module_id'] = array('#type' => 'markup', '#value' =>  ADSENSE_INJECTOR_MODULE_VERSION .'<br/></div>');      'description' => 'Insert Google AdSense ads into full node views automatically.',
132        'page callback' => 'drupal_get_form',
133    $form['node_ad_body_insertion'] = array(      'page arguments' => array('adsense_injector_admin_settings'),
134      '#type' => 'fieldset',      'access arguments' => array('administer site configuration'),
135      '#collapsible' => TRUE,      'file' => 'adsense_injector.admin.inc',
136      '#collapsed' => FALSE,      'type' => MENU_NORMAL_ITEM,
     '#title' => t('Node body ad insertion'),  
     '#description' => t('Requires adsense.module'),  
137    );    );
138      return $items;
   if (module_exist('adsense')) {  
     $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 <strong>Body</strong> 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' => '<div style="padding: 1em; color:yellow; background-color:red;"><strong>No node types selected (no ads will be inserted!) </strong></div>');  
     }  
   }  
   return $form;  
139  }  }
   

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.3