adsense_cse.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/adsense/cse/adsense_cse.module

Displays Google AdSense ads on Drupal pages

This is the core module of the AdSense package, with the Drupal hooks and other administrative functions.

Constants

NameDescription
ADSENSE_CSE_AD_BLOCK_DEFAULT
ADSENSE_CSE_AD_LOCATION_DEFAULT
ADSENSE_CSE_COLOR_BOX_BACKGROUND_DEFAULT
ADSENSE_CSE_COUNTRY_DEFAULT
ADSENSE_CSE_ENCODING_DEFAULT
ADSENSE_CSE_FRAME_WIDTH_DEFAULT
ADSENSE_CSE_LANGUAGE_DEFAULT
ADSENSE_CSE_LOGO_DEFAULT
ADSENSE_CSE_NUMBER_BLOCKS_DEFAULT
ADSENSE_CSE_RESULTS_PATH
ADSENSE_CSE_TEXTBOX_LENGTH_DEFAULT

Functions & methods

NameDescription
adsense_cse_blockImplementation of hook_block().
adsense_cse_menuImplementation of hook_menu().
_adsense_cse_get_block_configConfiguration of the provided block
_adsense_cse_get_searchboxGenerates the CSE search box

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Displays Google AdSense ads on Drupal pages
  5. *
  6. * This is the core module of the AdSense package, with the Drupal hooks
  7. * and other administrative functions.
  8. */
  9. define('ADSENSE_CSE_RESULTS_PATH', 'adsense/cse');
  10. define('ADSENSE_CSE_AD_BLOCK_DEFAULT', '');
  11. define('ADSENSE_CSE_AD_LOCATION_DEFAULT', 'adsense_cse_loc_top_right');
  12. define('ADSENSE_CSE_COLOR_BOX_BACKGROUND_DEFAULT', 'FFFFFF');
  13. define('ADSENSE_CSE_COUNTRY_DEFAULT', 'www.google.com');
  14. define('ADSENSE_CSE_ENCODING_DEFAULT', 'UTF-8');
  15. define('ADSENSE_CSE_FRAME_WIDTH_DEFAULT', 800);
  16. define('ADSENSE_CSE_LOGO_DEFAULT', 'adsense_cse_branding_right');
  17. define('ADSENSE_CSE_NUMBER_BLOCKS_DEFAULT', 2);
  18. define('ADSENSE_CSE_TEXTBOX_LENGTH_DEFAULT', 31);
  19. define('ADSENSE_CSE_LANGUAGE_DEFAULT', 'en');
  20. /**
  21. * Implementation of hook_menu().
  22. */
  23. function adsense_cse_menu() {
  24. $items = array();
  25. $results_path = variable_get('clean_url', 0) ? ADSENSE_CSE_RESULTS_PATH : '.';
  26. $items['admin/settings/adsense/cse'] = array(
  27. 'title' => 'Custom Search',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('adsense_cse_settings'),
  30. 'access arguments' => array('administer adsense'),
  31. 'weight' => 2,
  32. 'type' => MENU_LOCAL_TASK,
  33. 'file' => 'adsense_cse.admin.inc',
  34. );
  35. $items[$results_path] = array(
  36. 'title' => 'Search Results',
  37. 'page callback' => '_adsense_cse_results',
  38. 'access callback' => TRUE,
  39. 'type' => MENU_CALLBACK,
  40. 'file' => 'adsense_cse.results.inc',
  41. );
  42. return $items;
  43. }
  44. /**
  45. * Implementation of hook_block().
  46. */
  47. function adsense_cse_block($op = 'list', $delta = 0, $edit = array()) {
  48. $block = NULL;
  49. switch ($op) {
  50. case 'list':
  51. $max = variable_get('adsense_cse_number_blocks', ADSENSE_CSE_NUMBER_BLOCKS_DEFAULT);
  52. for ($count=0; $count < $max ; $count++) {
  53. if ($ad = _adsense_cse_get_block_config($count)) {
  54. $title = $ad[0];
  55. }
  56. else {
  57. $title = t('AdSense CSE: unconfigured ') . $count;
  58. }
  59. $block[$count]['info'] = $title;
  60. $block[$count]['cache'] = BLOCK_NO_CACHE;
  61. }
  62. break;
  63. case 'configure':
  64. $ad = _adsense_cse_get_block_config($delta);
  65. $form['info'] = array(
  66. '#type' => 'textfield',
  67. '#title' => t('Block description'),
  68. '#default_value' => ($ad) ? $ad[0] : '',
  69. '#maxlength' => 64,
  70. '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))),
  71. '#required' => TRUE,
  72. '#weight' => -19,
  73. );
  74. $form['ad_slot'] = array(
  75. '#type' => 'textfield',
  76. '#title' => t('Ad Slot ID'),
  77. '#default_value' => ($ad) ? $ad[1] : '',
  78. '#description' => t('This is the provided by the AdSense site in the Search Box Code "cx" field. This is usually provided in the form partner-<em>Publisher ID</em>:<em>Slot Id</em>. If the code provided is, for example, partner-pub-0123456789:<strong>abcdef-ghij</strong>, then insert only <strong>abcdef-ghij</strong> here.'),
  79. '#required' => TRUE,
  80. );
  81. return $form;
  82. case 'save':
  83. $data = implode(':', array(urlencode($edit['info']), $edit['ad_slot']));
  84. variable_set('adsense_cse_ad_block_'. $delta, $data);
  85. return;
  86. case 'view':
  87. if (_adsense_page_match()) {
  88. $ad = _adsense_cse_get_block_config($delta);
  89. $block['content'] = ($ad) ? adsense_display(array('format' => 'Search Box', 'slot' => $ad[1])) : t('AdSense unconfigured block. <a href=!url>Click to configure.</a>', array('!url' => url('admin/build/block/configure/adsense_cse/'. $delta)));
  90. }
  91. break;
  92. }
  93. return $block;
  94. }
  95. /**
  96. * Configuration of the provided block
  97. *
  98. * @param $delta
  99. * block number
  100. * @return
  101. * array with the block configuration or FALSE if no such block was found
  102. */
  103. function _adsense_cse_get_block_config($delta = 0) {
  104. if ($data = variable_get('adsense_cse_ad_block_'. $delta, ADSENSE_CSE_AD_BLOCK_DEFAULT)) {
  105. $ad = explode(':', $data);
  106. $ad[0] = urldecode($ad[0]);
  107. return $ad;
  108. }
  109. return FALSE;
  110. }
  111. /**
  112. * Generates the CSE search box
  113. *
  114. * @param $slot
  115. * Slot Id for the AdSense for Search
  116. * @return
  117. * HTML with the search input form
  118. */
  119. function _adsense_cse_get_searchbox($client, $slot = NULL) {
  120. global $base_url;
  121. $branding = variable_get('adsense_cse_logo', ADSENSE_CSE_LOGO_DEFAULT);
  122. $box_background_color = variable_get('adsense_cse_color_box_background', ADSENSE_CSE_COLOR_BOX_BACKGROUND_DEFAULT);
  123. $ad_location = variable_get('adsense_cse_ad_location', ADSENSE_CSE_AD_LOCATION_DEFAULT);
  124. $encoding = variable_get('adsense_cse_encoding', ADSENSE_CSE_ENCODING_DEFAULT);
  125. $textbox_length = variable_get('adsense_cse_textbox_length', ADSENSE_CSE_TEXTBOX_LENGTH_DEFAULT);
  126. $language = variable_get('adsense_cse_language', ADSENSE_CSE_LANGUAGE_DEFAULT);
  127. $search = t('Search');
  128. $custom_search = t('Custom Search');
  129. if (variable_get('clean_url', 0)) {
  130. $results_path = url(ADSENSE_CSE_RESULTS_PATH, array('absolute' => TRUE));
  131. $hidden_q_field = '';
  132. }
  133. else {
  134. $results_path = $base_url;
  135. $hidden_q_field = '<input type="hidden" name="q" value="." />';
  136. }
  137. if ($box_background_color == '000000') {
  138. $box_text_color = 'FFFFFF';
  139. }
  140. else {
  141. $box_text_color = '000000';
  142. }
  143. switch ($ad_location) {
  144. case 'adsense_cse_loc_top_right':
  145. $forid = 10;
  146. break;
  147. case 'adsense_cse_loc_top_bottom':
  148. $forid = 11;
  149. break;
  150. case 'adsense_cse_loc_right':
  151. $forid = 9;
  152. break;
  153. }
  154. if ($branding == 'adsense_cse_branding_watermark') {
  155. // Since we use as_q, Google's Javascript can't be used, so we use a modified local copy
  156. $script = $base_url .'/'. drupal_get_path('module', 'adsense_cse') .'/adsense_cse.js';
  157. // If there is a watermark, the indentation of the code makes it impossible to re-use the same text
  158. $output = <<<CSE_TXT1
  159. <script type="text/javascript"><!--
  160. drupal_adsense_cse_lang = '$language';
  161. //-->
  162. </script>
  163. <form action="$results_path" id="cse-search-box">
  164. <div>$hidden_q_field
  165. <input type="hidden" name="cx" value="partner-$client:$slot" />
  166. <input type="hidden" name="cof" value="FORID:$forid" />
  167. <input type="hidden" name="ie" value="$encoding" />
  168. <input type="text" name="as_q" size="$textbox_length" />
  169. <input type="submit" name="sa" value="$search" />
  170. </div>
  171. </form>
  172. <script type="text/javascript" src="$script"></script>
  173. CSE_TXT1;
  174. //<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>
  175. }
  176. else {
  177. if ($branding == 'adsense_cse_branding_right') {
  178. $class = 'cse-branding-right';
  179. }
  180. else {
  181. $class = 'cse-branding-bottom';
  182. }
  183. $output = <<<CSE_TXT2
  184. <style type="text/css">
  185. @import url(http://www.google.com/cse/api/branding.css);
  186. </style>
  187. <div class="$class" style="background-color:#$box_background_color;color:#$box_text_color">
  188. <div class="cse-branding-form">
  189. <form action="$results_path" id="cse-search-box">
  190. <div>$hidden_q_field
  191. <input type="hidden" name="cx" value="partner-$client:$slot" />
  192. <input type="hidden" name="cof" value="FORID:$forid" />
  193. <input type="hidden" name="ie" value="$encoding" />
  194. <input type="text" name="as_q" size="$textbox_length" />
  195. <input type="submit" name="sa" value="$search" />
  196. </div>
  197. </form>
  198. </div>
  199. <div class="cse-branding-logo">
  200. <img src="http://www.google.com/images/poweredby_transparent/poweredby_$box_background_color.gif" alt="Google" />
  201. </div>
  202. <div class="cse-branding-text">
  203. $custom_search
  204. </div>
  205. </div>
  206. CSE_TXT2;
  207. }
  208. return $output;
  209. }