forked from berkes/tagadelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagadelic.module
535 lines (483 loc) · 16.5 KB
/
tagadelic.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/**
* Implements hook_help().
*/
function tagadelic_help($path, $arg) {
switch ($path) {
case 'admin/help#tagadelic':
return t('Tagadelic offers dynamic urls. <br/>Visit example.com/tagadelic/list/2,1,5 to get the vocabularies 2,1 and 5 listed as tag groups. <br/>Visit example.com/tagadelic/chunk/2,1,5 to get a tag cloud of the terms in the vocabularies 2,1 and 5.<br/> Note that we limit to five vocabularies.');
}
}
/**
* Implements hook_init().
*/
function tagadelic_init() {
drupal_add_css(drupal_get_path('module', 'tagadelic') . '/tagadelic.css');
}
/**
* Implements hook_menu().
*/
function tagadelic_menu() {
$items = array();
$items['admin/config/content/tagadelic'] = array(
'title' => 'Tagadelic configuration',
'description' => 'Configure the tag clouds. Set the order, the number of tags, and the depth of the clouds.',
'page callback' => 'drupal_get_form',
'page arguments' => array('tagadelic_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
);
$items['tagadelic'] = array(
'title' => 'Tags',
'page callback' => 'tagadelic_page_chunk',
'page arguments' => array(NULL),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['tagadelic/list/%tagadelic_vocs'] = array(
'title callback' => 'tagadelic_page_title_callback',
'title arguments' => array(2),
'page callback' => 'tagadelic_page_list',
'page arguments' => array(2),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['tagadelic/chunk/%tagadelic_vocs'] = array(
'title callback' => 'tagadelic_page_title_callback',
'title arguments' => array(2),
'page callback' => 'tagadelic_page_chunk',
'page arguments' => array(2),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
/**
* Menu callback. Admin setting page for tagadelic.
*/
function tagadelic_settings() {
$options = array('weight,asc' => t('by weight, ascending'), 'weight,desc' => t('by weight, descending'), 'title,asc' => t('by title, ascending'), 'title,desc' => t('by title, descending'), 'random,none' => t('random'));
$form['tagadelic_sort_order'] = array(
'#type' => 'radios',
'#title' => t('Tagadelic sort order'),
'#options' => $options,
'#default_value' => variable_get('tagadelic_sort_order', 'title,asc'),
'#description' => t('Determines the sort order of the tags on the freetagging page.'),
);
$form['tagadelic_page_amount'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t('Amount of tags on the pages'),
'#default_value' => variable_get('tagadelic_page_amount', '60'),
'#description' => t('The amount of tags that will show up in a cloud on the pages. Amount of tags in blocks must be configured in the block settings of the various cloud blocks.'),
);
$form['tagadelic_levels'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t('Number of levels'),
'#default_value' => variable_get('tagadelic_levels', 6),
'#description' => t('The number of levels between the least popular tags and the most popular ones. Different levels will be assigned a different class to be themed in tagadelic.css'),
);
return system_settings_form($form);
}
/**
* Menu wildcard callback.
*/
function tagadelic_vocs_load($vocs) {
if (is_numeric($vocs)) {
$vocs = array($vocs);
}
elseif (preg_match('/^([0-9]+,){1,5}[0-9]+$/', $vocs)) {
$vocs = explode(',', $vocs);
}
return $vocs;
}
/**
* Menu callback to render the tagadelic title.
*
* @param $vocs
* An array of taxonomy vocabulary ids.
*/
function tagadelic_page_title_callback($vocs) {
$title = '';
if (empty($vocs)) {
$title = t('Tags');
}
else {
$title = t('Tags in @vocabularies', array('@vocabularies' => theme('tagadelic_list_vocs', $vocs)));
}
return $title;
}
/**
* Theme function that creates a comma separated list of vocabulary terms.
*
* @params $vocs
* An array of taxonomy vocabulary ids.
*
* @ingroup themable
*/
function theme_tagadelic_list_vocs($vocs) {
foreach ($vocs as $vid) {
$vocabulary = taxonomy_vocabulary_load($vid);
$voc_names[] = $vocabulary->name;
}
return join(', ', $voc_names);
}
/**
* Menu callback renders a tagadelic page.
>>>>>>> 3316b95... Issue #1210654 by pfrenssen - Cleanup Documentation
*/
function tagadelic_page_chunk($vocs) {
if ($vocs == NULL) {
foreach (taxonomy_get_vocabularies(NULL) as $vocabulary) {
$vocs[] = $vocabulary->vid;
}
}
$tags = tagadelic_get_weighted_tags($vocs, variable_get('tagadelic_levels', 6), variable_get('tagadelic_page_amount', '60'));
$tags = tagadelic_sort_tags($tags);
$output = theme('tagadelic_weighted', array('terms' => $tags));
if (!$output) {
return drupal_not_found();
}
$output = "<div class=\"wrapper tagadelic\">$output</div>";
return $output;
}
/**
* Menu callback renders a tagadelic page with listed items: each vocabulary.
*/
function tagadelic_page_list($vocs) {
if ($vocs == NULL) {
return drupal_not_found();
}
$output = '';
foreach ($vocs as $vid) {
$vocabulary = taxonomy_vocabulary_load($vid);
// Clean out vocabulary, so that we don't have to leave security to our
// theme layer.
$vocabulary->description = filter_xss_admin($vocabulary->description);
$vocabulary->name = filter_xss_admin($vocabulary->name);
$tags = tagadelic_get_weighted_tags(array($vocabulary->vid), variable_get('tagadelic_levels', 6), variable_get('tagadelic_page_amount', '60'));
$tags = tagadelic_sort_tags($tags);
$output .= theme('tagadelic_list_box', array('vocabulary' => $vocabulary, 'tags' => $tags));
}
if (!$output) {
return drupal_not_found();
}
$output = "<div class=\"wrapper tagadelic\">$output</div>";
return $output;
}
/**
* API that returns a multidimensional array with tags given a node.
*
* @param $node
* A node object.
*/
function tagadelic_node_get_terms($node) {
static $vocs;
if ($terms = taxonomy_node_get_terms($node, 'tid')) {
if (!isset($vocs[$node->type])) {
$vocs[$node->type] = taxonomy_get_vocabularies($node->type);
}
$tags = array();
foreach ($terms as $tid => $term) {
if ($vocs[$node->type][$term->vid]->tags) {
$tags[$term->vid][$tid] = $term;
}
}
return $tags;
}
}
/**
* API function that returns the tags of a node in fancy titled lists.
*
* @param $node
* A node object.
*/
function tagadelic_tags_lists($node) {
if (is_array($node->tags)) {
$output = '';
foreach ($node->tags as $vid => $terms) {
$vocabulary = taxonomy_vocabulary_load($vid);
$title = l($vocabulary->name, "tagadelic/chunk/$vid");
$items = array();
foreach ($terms as $term) {
$items[] = l($term->name, taxonomy_term_path($term), array('attributes' => array('title' => t('view all posts tagged with "@tag"', array('@tag' => $term->name)))));
}
$output .= theme('item_list', $items, $title);
}
return $output;
}
}
/**
* Function that gets the information from the database, passes it along to the
* weight builder and returns these weighted tags. Note that the tags are
* unordered at this stage, hence they need ordering either by calling our api
* or by your own ordering data.
*
* @param $vids
* Vocabulary ids representing the vocabularies where you want the tags from.
* @param $steps
* The amount of tag-sizes you will be using. If you give "12" you sill get
* six different "weights". Defaults to 6 and is optional.
* @return
* An <em>unordered</em> array with tags-objects, containing the attribute
<<<<<<< HEAD
* $tag->weight;
=======
* $tag->weight.
>>>>>>> 3316b95... Issue #1210654 by pfrenssen - Cleanup Documentation
*/
function tagadelic_get_weighted_tags($vids, $steps = 6, $size = 60) {
// Build the options so we can cache multiple versions.
global $language;
<<<<<<< HEAD
$options = implode('_', $vids) . '_' . $language->language . '_' . $steps . '_' . $size;
// Check if the cache exists
$cache_name = 'tagadelic_cache_' . $options;
$cache = cache_get($cache_name, 'cache_page');
$tags = array();
// make sure cache has data
=======
$options = implode('_',$vids) .'_'. $language->language .'_'. $steps .'_'. $size;
// Check if the cache exists.
$cache_name = 'tagadelic_cache_'. $options;
$cache = cache_get($cache_name, 'cache_page');
// Make sure cache has data.
>>>>>>> 3316b95... Issue #1210654 by pfrenssen - Cleanup Documentation
if (isset($cache->data)) {
$tags = $cache->data;
}
else {
<<<<<<< HEAD
if (!is_array($vids) || count($vids) == 0) {
return array();
}
$result = db_query_range('SELECT COUNT(*) AS count, td.tid, td.vid, td.name, td.description FROM {taxonomy_term_data} td INNER JOIN {taxonomy_index} tn ON td.tid = tn.tid INNER JOIN {node} n ON tn.nid = n.nid WHERE td.vid IN (' . implode(',', array_fill(0, count($vids), '?')) . ') GROUP BY td.tid, td.vid, td.name, td.description HAVING COUNT(*) > 0 ORDER BY count DESC', 0, $size, $vids);
foreach ($result as $tag) {
$tags[$tag->tid] = $tag;
}
$tags = tagadelic_build_weighted_tags($tags, $steps);
=======
if (!is_array($vids) || count($vids) == 0) {
return array();
}
$result = db_query_range(db_rewrite_sql('SELECT COUNT(*) AS count, td.tid, td.vid, td.name, td.description FROM {term_data} td INNER JOIN {term_node} tn ON td.tid = tn.tid INNER JOIN {node} n ON tn.vid = n.vid WHERE td.vid IN ('. db_placeholders($vids) .') AND n.status = 1 GROUP BY td.tid, td.vid, td.name, td.description HAVING COUNT(*) > 0 ORDER BY count DESC'), $vids, 0, $size);
$tags = tagadelic_build_weighted_tags($result, $steps);
>>>>>>> 3316b95... Issue #1210654 by pfrenssen - Cleanup Documentation
cache_set($cache_name, $tags, 'cache_page', CACHE_TEMPORARY);
}
return $tags;
}
/**
* API that returns an array with weighted tags.
*
* This is the hard part. People with better ideas are very very welcome to send
* these to [email protected]. Distribution is one thing that needs attention.
*
* @param $result
* A query result, any query result that contains an <em>object</em> with the
* following attributes: $tag->count, $tag->tid, $tag->name and $tag->vid.
* Refer to tagadelic_get_weighted_tags() for an example.
* @param $steps
* The amount of tag-sizes you will be using. If you give "12" you sill get
* six different "weights". Defaults to 6 and is optional.
*
* @return
* An <em>unordered</em> array with tags-objects, containing the attribute
* $tag->weight.
*/
function tagadelic_build_weighted_tags($result, $steps = 6) {
// Find minimum and maximum log-count. By our MatheMagician Steven Wittens aka
// UnConeD.
$tags = array();
$min = 1e9;
$max = -1e9;
foreach ($tags as $id => $tag) {
$tag->number_of_posts = $tag->count;
$tag->count = log($tag->count);
$min = min($min, $tag->count);
$max = max($max, $tag->count);
$tags_tmp[$id] = $tag;
}
// Note: we need to ensure the range is slightly too large to make sure even
// the largest element is rounded down.
$range = max(.01, $max - $min) * 1.0001;
foreach ($tags_tmp as $key => $value) {
$tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);
}
return $tags;
}
/**
* API function to order a set of tags.
*
* @todo
* If you feel like making this more modular, please send me patches.
**/
function tagadelic_sort_tags($tags) {
list($sort, $order) = explode(',', variable_get('tagadelic_sort_order', 'title,asc'));
switch ($sort) {
case 'title':
usort($tags, "_tagadelic_sort_by_title");
break;
case 'weight':
usort($tags, "_tagadelic_sort_by_weight");
break;
case 'random':
shuffle($tags);
break;
}
if ($order == 'desc') {
$tags = array_reverse($tags);
}
return $tags;
}
/**
* Callback for usort, sort by count.
*/
function _tagadelic_sort_by_title($a, $b) {
return strnatcasecmp($a->name, $b->name);
}
/**
* Callback for usort, sort by weight.
*/
function _tagadelic_sort_by_weight($a, $b) {
if ($a->weight == $b->weight) {
// Ensure correct order when same weight
return $a->count > $b->count;
}
return $a->weight > $b->weight;
}
/**
* Theme function that renders the HTML for the tags.
*
* @ingroup themable
*/
function theme_tagadelic_weighted(array $vars) {
$terms = $vars['terms'];
$output = '';
foreach ($terms as $term) {
$weight = $term->weight;
$output .= l($term->name, drupal_get_path_alias('taxonomy/term/' . $term->tid), array(
'attributes' => array(
'class' => array("tagadelic", "level$weight"),
'rel' => 'tag',
'title' => $term->description,
)
)
) . " \n";
}
return $output;
}
/**
* Theme function that renders an entry in tagadelic/list/ views.
*
* @param $vocabulary
* A full vocabulary object.
* @param $tags
* An array with weigthed tag objects.
*
* @ingroup themable
*/
function theme_tagadelic_list_box(array $vars) {
$vocabulary = $vars['vocabulary'];
$tags = $vars['tags'];
$content = theme('tagadelic_weighted', array('terms' => $tags));
$output = '';
if ($vocabulary->description) {
$content = '<h2></h2><div>' . $vocabulary->description . '</div>' . $content;
}
$output .= '<h2>' . $vocabulary->name . '</h2><div>' . $content . '</div>';
return $output;
}
/**
* Theme function to provide a more link.
*
* @param $vid
* Vocabulary id for which more link is wanted. Optional, if left empty, no
* vocabulary is expected, and the more-link will point to the main tagadelic
* page at /tagadelic instead.
*
* @ingroup themable
*/
function tagadelic_block_info() {
$blocks = array();
foreach (taxonomy_get_vocabularies() as $voc) {
$blocks[$voc->vid] = array();
$blocks[$voc->vid]['info'] = variable_get('tagadelic_block_title_' . $voc->vid, t('Tags in @voc', array('@voc' => $voc->name)));
$blocks[$voc->vid]['cache'] = DRUPAL_CACHE_GLOBAL;
}
return $blocks;
}
/**
* Implements hook_block().
*/
function tagadelic_block_view($delta = '') {
$block = array();
if ($voc = taxonomy_vocabulary_load($delta)) {
$blocks['subject'] = variable_get('tagadelic_block_title_' . $delta, t('Tags in @voc', array('@voc' => $voc->name)));
$tags = tagadelic_get_weighted_tags(array($delta), variable_get('tagadelic_levels', 6), variable_get('tagadelic_block_tags_' . $delta, 12));
$tags = tagadelic_sort_tags($tags);
$blocks['content'] = theme('tagadelic_weighted', array('terms' => $tags)); //return a chunk of 12 tags
if (count($tags) >= variable_get('tagadelic_block_tags_' . $delta, 12)) {
$blocks['content'] .= theme('more_link', array('title' => t('more tags'), 'url' => "tagadelic/chunk/{$voc->vid}"));
}
<<<<<<< HEAD
=======
$blocks[0]['info'] = t('Tags for the current post');
$blocks[0]['cache'] = BLOCK_CACHE_PER_PAGE;
$blocks['all']['info'] = t('Tags for all vocabularies');
$blocks['all']['cache'] = BLOCK_CACHE_GLOBAL;
return $blocks;
}
elseif ($op == 'configure') {
$voc = taxonomy_vocabulary_load($delta);
$form = array();
$form['tags'] = array(
'#type' => 'textfield',
'#title' => t('Tags to show'),
'#default_value' => variable_get('tagadelic_block_tags_'. $delta, 12),
'#maxlength' => 3,
'#description' => t('The number of tags to show in this block.'),
);
return $form;
}
elseif ($op == 'save') {
variable_set('tagadelic_block_tags_'. $delta, $edit['tags']);
return;
>>>>>>> 3316b95... Issue #1210654 by pfrenssen - Cleanup Documentation
}
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function tagadelic_block_configure($delta = '') {
$voc = taxonomy_vocabulary_load($delta);
$form = array();
$form['tags'] = array(
'#type' => 'textfield',
'#title' => t('Tags to show'),
'#default_value' => variable_get('tagadelic_block_tags_' . $delta, 12),
'#maxlength' => 3,
'#description' => t('The number of tags to show in this block.'),
);
return $form;
}
/**
* Implements hook_block_save().
*/
function tagadelic_block_save($delta = '', $edit = array()) {
variable_set('tagadelic_block_tags_' . $delta, $edit['tags']);
}
/**
* Implements hook_theme().
*/
function tagadelic_theme() {
return array(
'tagadelic_list_box' => array('arguments' => array('vocabulary' => NULL, 'tags' => NULL)),
'tagadelic_weighted' => array('arguments' => array('terms' => array()))
);
}