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

Contents of /contributions/modules/technorati/technorati.module

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


Revision 1.11 - (show annotations) (download) (as text)
Sun Jul 27 16:28:34 2008 UTC (15 months, 4 weeks ago) by kbahey
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.10: +95 -40 lines
File MIME type: text/x-php
#276874 by jandd, port to Drupal 6.x
1 <?php
2 // $Id: technorati.module,v 1.6.2.3 2007/05/01 19:55:38 kbahey Exp $
3
4 /*
5 * @file
6 * Technorati module for Drupal 6.x.
7 *
8 * This module enables Technorati (http://technorati.com) tags for Drupal
9 * node. Tags can be stored in a separate database table or can be taken from
10 * the taxonomy tags.
11 *
12 * Copyright (c) 2006 http://2bits.com
13 * Copyright (c) 2008 Jan Dittberner <jan@dittberner.info>
14 */
15
16 define('TECHNORATI_NODE_TYPE', 'technorati_node_type_');
17
18 define('TECHNORATI_MODE_NONE', 0);
19 define('TECHNORATI_MODE_MANUAL', 1);
20 define('TECHNORATI_MODE_TAXONOMY', 2);
21 define('TECHNORATI_MODE_BOTH', 3);
22
23 define('TECHNORATI_DISPLAY_TYPE', 'technorati_display_type');
24
25 define('TECHNORATI_DISPLAY_NONE', 0);
26 define('TECHNORATI_DISPLAY_TEASER', 1);
27 define('TECHNORATI_DISPLAY_FULL', 2);
28 define('TECHNORATI_DISPLAY_BOTH', 3);
29
30 /**
31 * Implementation of hook_help().
32 */
33 function technorati_help($path, $arg) {
34 switch ($path) {
35 case 'admin/help#technorati' :
36 return '<p>'. t('Enables Technorati tags for selected content types, and pings Technorati when new content is created.') .'</p>';
37 }
38 }
39
40 /**
41 * Implementation of hook_menu().
42 */
43 function technorati_menu() {
44 $items = array();
45
46 $items['admin/settings/technorati'] = array(
47 'title' => 'Technorati',
48 'description' => 'technorati settings',
49 'page callback' => 'drupal_get_form',
50 'page arguments' => array('technorati_admin_settings'),
51 'access arguments' => array('administer site configuration'),
52 'type' => MENU_NORMAL_ITEM
53 );
54 return $items;
55 }
56
57 /**
58 * Form definition for technorati module settings.
59 */
60 function technorati_admin_settings() {
61 if (!module_exists('ping')) {
62 drupal_set_message(t('This module requires that the %pingmodule be enabled',
63 array('%pingmodule' => l('ping module', 'admin/modules'))), 'error');
64 return;
65 }
66
67 $display_options = array(
68 TECHNORATI_DISPLAY_NONE => t('None'),
69 TECHNORATI_DISPLAY_TEASER => t('Teaser view'),
70 TECHNORATI_DISPLAY_FULL => t('Full page view'),
71 TECHNORATI_DISPLAY_BOTH => t('Both'),
72 );
73
74 $node_options = array(
75 TECHNORATI_MODE_NONE => t('None'),
76 TECHNORATI_MODE_MANUAL => t('Manual entry'),
77 TECHNORATI_MODE_TAXONOMY => t('Drupal categories'),
78 TECHNORATI_MODE_BOTH => t('Both'),
79 );
80
81 $form[TECHNORATI_DISPLAY_TYPE] = array(
82 '#type' => 'select',
83 '#title' => t('How to display the technorati tags'),
84 '#default_value' => variable_get(TECHNORATI_DISPLAY_TYPE, TECHNORATI_DISPLAY_FULL),
85 '#options' => $display_options,
86 '#description' => t('Select how to display the tags.<ul><li>None: means that the module will not display the tags. The theme can use the $node->technorati object to display the tags anywhere.</li><li>Teaser: means that the tags will only be displayed when the node is in teaser view.</li><li>Full page: means that the tags will only be displayed when the node is in full page view.</li><li>Both: means the tags will be displayed in both teaser and full view.</li></ul>'),
87 );
88
89 $form['types'] = array(
90 '#type' => 'fieldset',
91 '#title' => t('Content types'),
92 '#collapsible' => TRUE,
93 '#description' => t('Select the type of tags to use for each content type.<ul><li>None: means do not do any Technorati tags for this content type.</li><li>Manual entry: means that the tags have to be entered manually for each node.</li><li>Drupal categories: means that the terms the node belong to will be used as Technorati tags.</li><li>Both: means a combination of manual entries and categories.</li></ul>'),
94 );
95
96 foreach (node_get_types() as $node_type => $node_name) {
97 $type = TECHNORATI_NODE_TYPE . $node_type;
98 $form['types'][$type] = array(
99 '#type' => 'select',
100 '#title' => $node_type,
101 '#default_value' => variable_get($type, TECHNORATI_MODE_NONE),
102 '#options' => $node_options,
103 );
104 }
105
106 return system_settings_form($form);
107 }
108
109 /**
110 * Implementation of hook_form_alter().
111 */
112 function technorati_form_alter(&$form, &$form_state, $form_id) {
113 if (preg_match('/^(.*)_node_form$/', $form_id, $matches)) {
114 // Get the node type we are processing
115 $node_type = $matches[1];
116
117 // Check what the technorati mode for that node type
118 $mode = variable_get(TECHNORATI_NODE_TYPE . $node_type, TECHNORATI_MODE_NONE);
119 switch ($mode) {
120 case TECHNORATI_MODE_NONE:
121 case TECHNORATI_MODE_TAXONOMY:
122 // No need to do anything in the node form
123 return;
124
125 case TECHNORATI_MODE_MANUAL:
126 case TECHNORATI_MODE_BOTH:
127 // We need to inject the texfield for technorati tags in the form
128 $tags = '';
129 // Load the node and get the technorati tags, if present
130 if ($nid = $form['nid']['#value']) {
131 $node = node_load($nid);
132 if (is_array($node->technorati_tags)) {
133 $tags = implode(',', $node->technorati_tags);
134 }
135 }
136
137 $form['technorati'] = array(
138 '#type' => 'fieldset',
139 '#title' => t('Technorati'),
140 '#collapsible' => TRUE,
141 '#collapsed' => TRUE,
142 );
143
144 $form['technorati']['technorati_tags'] = array(
145 '#type' => 'textfield',
146 '#title' => t('Technorati tags'),
147 '#default_value' => $tags,
148 '#size' => 80,
149 '#maxlength' => 120,
150 '#description' => t('Enter your Technorati tags, separated by commas.'),
151 );
152 }
153 }
154 }
155
156 /**
157 * Implementation of hook_theme().
158 */
159 function technorati_theme() {
160 return array(
161 'technorati_tags' => array(
162 'arguments' => array('tags' => NULL),
163 ),
164 );
165 }
166
167 /**
168 * Implementation of hook_nodeapi().
169 */
170 function technorati_nodeapi(&$node, $op, $a3, $a4) {
171 $mode = variable_get(TECHNORATI_NODE_TYPE . $node->type, TECHNORATI_MODE_NONE);
172 switch ($mode) {
173 case TECHNORATI_MODE_NONE:
174 case TECHNORATI_MODE_TAXONOMY:
175 // No need to do anything in the node form
176 return;
177 }
178
179 switch ($op) {
180 case 'insert':
181 db_query("INSERT INTO {technorati} (nid, tags) VALUES (%d, '%s')",
182 $node->nid, serialize(explode(',', $node->technorati_tags)));
183 break;
184
185 case 'update':
186 db_query('DELETE FROM {technorati} WHERE nid = %d', $node->nid);
187 db_query("INSERT INTO {technorati} (nid, tags) VALUES (%d, '%s')",
188 $node->nid, serialize(explode(',', $node->technorati_tags)));
189 break;
190
191 case 'delete':
192 db_query('DELETE FROM {technorati} WHERE nid = %d', $node->nid);
193 break;
194
195 case 'load':
196 $result = db_query('SELECT tags FROM {technorati} WHERE nid = %d', $node->nid);
197 $tags = unserialize(db_result($result));
198 if ($tags) {
199 return array('technorati_tags' => $tags);
200 }
201 break;
202
203 case 'view':
204 $technorati = array(
205 '#value' => theme('technorati_tags', _technorati_process_tags($node)),
206 '#weight' => 10,
207 );
208 $mode = variable_get(TECHNORATI_DISPLAY_TYPE, TECHNORATI_DISPLAY_FULL);
209 switch ($mode) {
210 case TECHNORATI_DISPLAY_NONE:
211 // No inline display. Theme will handle it all.
212 break;
213 case TECHNORATI_DISPLAY_TEASER:
214 // Teaser view only
215 if ($a3) {
216 $node->content['technorati'] = $technorati;
217 }
218 break;
219 case TECHNORATI_DISPLAY_FULL:
220 // Full page view only
221 if (!$a3) {
222 $node->content['technorati'] = $technorati;
223 }
224 break;
225 case TECHNORATI_DISPLAY_BOTH:
226 // Teaser and full page view
227 $node->content['technorati'] = $technorati;
228 break;
229 }
230 break;
231 }
232 }
233
234 /**
235 * Theme function for registered theme 'technorati_tags'.
236 */
237 function theme_technorati_tags($tags) {
238 $path = base_path() . drupal_get_path('module', 'technorati') .'/technobubble.gif';
239 $output = '<div class="technorati_tags">';
240 $output .= '<img src="'. $path .'"/>';
241 $output .= '<strong>'. t('Technorati Tags: ') .'</strong>';
242 $output .= implode(' ', $tags);
243 $output .= '</div>';
244 //$output .= '<script type="text/javascript" src="http://technorati.com/embed/CODE.js"></script>';
245 return $output;
246 }
247
248 /**
249 * This function is called by technorati_nodeapi() with $op 'view' and
250 * processes the tags in a node type and technorati mode specific way.
251 */
252 function _technorati_process_tags($node) {
253 $mode = variable_get(TECHNORATI_NODE_TYPE . $node->type, TECHNORATI_MODE_NONE);
254 switch ($mode) {
255 case TECHNORATI_MODE_MANUAL:
256 return _technorati_manual($node);
257
258 case TECHNORATI_MODE_TAXONOMY:
259 return _technorati_taxonomy($node);
260
261 case TECHNORATI_MODE_BOTH:
262 return array_merge(_technorati_taxonomy($node), _technorati_manual($node));
263 }
264 }
265
266 /**
267 * This function handles the manually assigned technorati tags of a node. It
268 * is called by _technorati_process_tags() for the technorati modes 'manual'
269 * and 'both'.
270 */
271 function _technorati_manual($node) {
272 $links = array();
273 if (is_array($node->technorati_tags)) {
274 foreach ($node->technorati_tags as $tag) {
275 $links[] = _technorati_link($tag);
276 }
277 }
278 return $links;
279 }
280
281 /**
282 * This function handles the taxonomy tags of a node and creates technorati
283 * links from them. It is called by _technorati_process_tags() for the
284 * technorati modes 'taxonomy' and 'both'.
285 */
286 function _technorati_taxonomy($node) {
287 $links = array();
288 $terms = taxonomy_node_get_terms($node);
289 foreach ($terms as $term) {
290 $links[] = _technorati_link($term->name);
291 }
292 return $links;
293 }
294
295 /**
296 * Calls the Technorati ping service at http://rpc.technorati.com/rpc/ping and
297 * notifies changes to the given URL.
298 */
299 function technorati_ping($name = '', $url = '') {
300 $result = xmlrpc('http://rpc.technorati.com/rpc/ping', 'weblogUpdates.ping', $name, $url);
301 if ($result) {
302 watchdog("directory ping", 'Successfully notified technorati.com site.', WATCHDOG_NOTICE);
303 }
304 else {
305 watchdog('directory ping', 'Failed to notify technorati.com site.', WATCHDOG_WARNING);
306 }
307 }
308
309 /**
310 * Remove whitespace between tags, and format the n words making up
311 * multi-word tags with (n-1) "+" symbols.
312 */
313 function _technorati_link($tag) {
314 $tag = _technorati_wsstrip(str_replace('+', ' ', check_plain($tag)));
315 $output = '<a href="http://technorati.com/tag/';
316 $output .= str_replace(" ", "+", $tag);
317 $output .= '" rel="tag">';
318 $output .= $tag;
319 $output .= '</a>';
320
321 return $output;
322 }
323
324 /**
325 * Strip whitespace from left and/or right of tags, and reduce multiples
326 * to just one.
327 *
328 * Reference: daggillies's comment on http://www.php.net/trim
329 */
330 function _technorati_wsstrip($str) {
331 $str = ereg_replace(' +', ' ', trim($str));
332 return ereg_replace("[\r\t\n]", '', $str);
333 }

  ViewVC Help
Powered by ViewVC 1.1.2