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

Contents of /contributions/modules/diggthis/diggthis.module

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


Revision 1.24 - (show annotations) (download) (as text)
Sat Jan 10 20:40:51 2009 UTC (10 months, 2 weeks ago) by yaph
Branch: MAIN
CVS Tags: DRUPAL-6--2-1-RC1, HEAD
Changes since 1.23: +3 -2 lines
File MIME type: text/x-php
Added HTML comments around the JavaScript variables for the digg this button to avoid them from being indexed by Drupal's search system #356731;
1 <?php
2 /**
3 * $Id: diggthis.module,v 1.23 2008/09/10 22:06:08 yaph Exp $
4 *
5 * @file
6 * Author: Ramiro Gómez - http://www.ramiro.org
7 * A Drupal module that adds a Digg this button to your nodes.
8 * The code of this module is partially based on the
9 * service links module by Fredrik Jonsson
10 *
11 * @see apidoc.digg.com
12 *
13 */
14
15 /**
16 * Implementation of hook_menu().
17 */
18 function diggthis_menu() {
19 $items = array();
20 $items['admin/content/diggthis'] = array(
21 'title' => 'Diggthis',
22 'description' => 'Enable the node types and set the properties for the diggthis button.',
23 'page callback' => 'drupal_get_form',
24 'page arguments' => array('diggthis_admin_settings'),
25 'access arguments' => array('administer site configuration'),
26 'type' => MENU_NORMAL_ITEM,
27 'file' => 'diggthis.admin.inc'
28 );
29 $items['diggthis/top-stories'] = array(
30 'title' => 'Digg Top Stories',
31 'page callback' => 'diggthis_top_stories',
32 'access arguments' => array('access digg stories'),
33 'type' => MENU_NORMAL_ITEM,
34 'file' => 'diggthis.pages.inc'
35 );
36 return $items;
37 }
38
39 /**
40 * Implementation of hook_theme
41 */
42 function diggthis_theme() {
43 return array(
44 'diggthis_button' => array(
45 'arguments' => array('js_code' => NULL)
46 ),
47 'diggthis_story' => array(
48 'arguments' => array('story' => NULL)
49 )
50 );
51 }
52
53 /**
54 * Implementation of hook_perm
55 */
56 function diggthis_perm() {
57 return array('access digg stories');
58 }
59
60 /**
61 * Implementation of hook_nodeapi().
62 */
63 function diggthis_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
64 // we're in full node view
65 if ($op == 'view' && !$a3) {
66 if (in_array($node->type, variable_get('diggthis_node_types', array()), TRUE)) {
67 $node->content['diggthis_button'] = array(
68 '#value' => diggthis_button_create($node),
69 '#weight' => variable_get('diggthis_button_weight', 10)
70 );
71 }
72 }
73 }
74
75 /**
76 * Implementaiton of hook_block
77 */
78 function diggthis_block($op = 'list', $delta = 0, $edit = array()) {
79 $blocks = array();
80 switch ( $op ) {
81 case 'list':
82 $blocks[0] = array(
83 'info' => t('Digg This'),
84 'weight' => 0
85 );
86 break;
87
88 case 'view':
89 if ('admin' != arg(0)) {
90 $blocks['content'] = diggthis_button_create();
91 $blocks['subject'] = variable_get('diggthis_block_title_'.$delta, t('Digg this'));
92 break;
93 }
94 }
95 return $blocks;
96 }
97
98 /**
99 * A function that generates the JavaScript
100 * code for embedding hte diggthis button
101 *
102 * @param $node
103 * an optional node object
104 */
105 function diggthis_button_create($node = false) {
106 // we are in node view
107 if ($node) {
108 $path = 'node/'. $node->nid;
109 $title = check_plain($node->title);
110 $teaser = strip_tags($node->teaser);
111 }
112 elseif ('admin' != arg(0)){
113 $path = check_plain($_GET['q']);
114 $title = drupal_get_title();
115 $teaser = '';
116
117 // block display in node view
118 if ('node' == arg(0)
119 && is_numeric(arg(1))
120 && !arg(2)) {
121 $node = node_load(arg(1));
122 $teaser = strip_tags($node->teaser);
123 }
124 }
125
126 // get the absolute URL of the current page
127 $url = url($path, array('absolute' => TRUE));
128
129 // make sure strings are not to long
130
131 $title = truncate_utf8($title, 60, TRUE, FALSE);
132 $teaser = drupal_to_js(truncate_utf8($teaser, 350, TRUE, FALSE));
133
134 // only change the background color if configured
135 $bgcolor = variable_get('diggthis_button_bgcolor', '');
136 if ($bgcolor) {
137 $bg_string = "digg_bgcolor = '$bgcolor';";
138 }
139
140 $skin = variable_get('diggthis_button_skin', 'standard');
141
142 $digg_js =<<<EOF
143 <script type="text/javascript"><!--
144 digg_url = '$url';
145 digg_title = '$title';
146 digg_bodytext = $teaser;
147 digg_skin = '$skin';
148 $bg_string
149 //-->
150 </script>
151 <script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
152 EOF;
153
154 return theme('diggthis_button', $digg_js);
155 }
156
157 /**
158 * Theme function for button display
159 *
160 * @param $js_code
161 * The JavaScript code to display the diggthis button
162 */
163 function theme_diggthis_button($js_code) {
164 return '<div class="diggthis_button">'. $js_code .'</div>';
165 }
166
167 /**
168 * Do the Digg API request and retrieve the XML feed
169 * with the top Digg stories for a given domain.
170 *
171 * @return $xml
172 * The XML string as returned by the Digg Web Service
173 */
174 function diggthis_request($query_string) {
175 $api_url = 'http://services.digg.com/stories';
176 //$request_url = url($api_url, $options);
177 $request_url = $api_url . $query_string;
178 $xml = '';
179
180 $cache = cache_get($request_url, 'cache');
181 if(is_object($cache) && $cache->expire > time()) {
182 $xml = $cache->data;
183 }
184 else {
185 cache_clear_all($request_url, 'cache');
186 $response = drupal_http_request($request_url);
187 if (200 == $response->code) {
188 // cache the XML string since unserializing simplexml objects does not work
189 $xml = $response->data;
190 cache_set($request_url, $xml, 'cache', time() + variable_get('diggthis_cache_lifetime', 43200));
191 }
192 else {
193 watchdog('diggthis', 'Digg top stories could not be retrieved. Server response: "%response"',
194 array('%response' => var_export($response, TRUE)), WATCHDOG_WARNING);
195 return FALSE;
196 }
197 }
198 return $xml;
199 }
200
201 /**
202 * Theme function to markup a single digg story
203 *
204 * @param $data
205 * A PHP SimpleXML object
206 *
207 * @return $html
208 * An HTML string with markup for a Digg story
209 */
210 function theme_diggthis_story($story) {
211 $html = '';
212 $digg_link = l(t('Diggs received: !diggs', array('!diggs' => $story['diggs'])),
213 $story['href'], array('absolute' => TRUE));
214 $story_link = l($story->title, $story['link'], array('absolute' => TRUE));
215
216 $html .= '<div class="digg-story">';
217 $html .= '<h3 class="digg-story-title">' . $story_link . '</h3>';
218 $html .= '<div class="digg-story-description">' . $story->description . '</div>';
219 $html .= '<div class="digg-story-diggs">' . $digg_link . '</div>';
220 $html .= '</div>';
221
222 return $html;
223 }

  ViewVC Help
Powered by ViewVC 1.1.2