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

Contents of /contributions/modules/related_terms/related_terms.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Aug 6 11:54:39 2008 UTC (15 months, 2 weeks ago) by davyvandenbremt
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Initial import
1 <?php
2
3 /**
4 * Implementation of hook_block().
5 */
6 function related_terms_block($op = 'list', $delta = 0, $edit = array()) {
7
8 $blocks = array(
9 array('info' => t('Related terms'))
10 );
11
12 switch ($op) {
13 case 'list':
14 return $blocks;
15
16 case 'configure':
17
18 $form = array();
19
20 $number_options = drupal_map_assoc(range(0, 50));
21 $number_options[0] = t('Unlimited');
22
23 $sort_options = array(
24 'name' => t('By name'),
25 'freq' => t('By frequency'),
26 'shuffle' => t('Random order')
27 );
28 $vocabularies = taxonomy_get_vocabularies();
29 foreach($vocabularies as $vocabulary) {
30 $vocabularies_options[$vocabulary->vid] = $vocabulary->name;
31 }
32
33 $form['number_of_terms'] = array(
34 '#type' => 'select',
35 '#title' => t('Number of terms'),
36 '#multiple' => FALSE,
37 '#description' => t('Maximum number of related terms that will show.'),
38 '#options' => $number_options,
39 '#default_value' => variable_get('related_terms_number_of_terms', 0),
40 );
41
42 $form['vocabularies'] = array(
43 '#type' => 'select',
44 '#title' => t('Vocabularies'),
45 '#multiple' => TRUE,
46 '#description' => t('Restrict related terms to terms of these vocabularies.'),
47 '#options' => $vocabularies_options,
48 '#default_value' => variable_get('related_terms_vocabularies', array()),
49 );
50
51 $form['sort'] = array(
52 '#type' => 'select',
53 '#title' => t('Sort terms'),
54 '#multiple' => FALSE,
55 '#description' => t('Select what order the related terms need to appear.'),
56 '#options' => $sort_options,
57 '#default_value' => variable_get('related_terms_sort', 'name'),
58 );
59 return $form;
60
61 case 'save':
62
63 variable_set('related_terms_number_of_terms', $edit['number_of_terms']);
64 variable_set('related_terms_vocabularies', $edit['vocabularies']);
65 variable_set('related_terms_sort', $edit['sort']);
66
67 break;
68
69 case 'view':
70
71 $block = array();
72 $block['subject'] = $blocks[$delta]['info'];
73
74 if(arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
75 $related_terms = related_terms_get_related_terms(arg(2), variable_get('related_terms_number_of_terms', 0), variable_get('related_terms_sort', 'name'), variable_get('related_terms_vocabularies', array()));
76 }
77
78 if(count($related_terms) > 0) {
79 $block['content'] = theme('related_terms_block', $related_terms);
80 }
81
82 if (!isset($block['content'])) return;
83
84 return $block;
85
86 default:
87 break;
88 }
89 }
90
91 function theme_related_terms_block($terms) {
92 $output = '';
93 foreach($terms as $term) {
94 $output .= sprintf(
95 '<a style="font-size: %sem;" href="%s">%s</a> ',
96 strval(number_format(0.9 + $term['cnt'] * 0.1, 2, '.', '')),
97 url('taxonomy/term/'.$term['tid']),
98 $term['name']
99 );
100 }
101
102 return $output;
103 }
104
105
106 // $sort can be name, shuffle, frequence
107 function related_terms_get_related_terms($tid, $limit = 0, $sort = 'name', $vids = array()) {
108
109 $restrict_vids = '';
110 if(count($vids) > 0) {
111 $restrict_vids = ' AND td2.vid IN (' . implode(',',$vids) . ') ';
112 }
113
114 $sql = "SELECT tn2.tid, td2.name, COUNT(tn.nid) AS cnt FROM term_node tn INNER JOIN term_node tn2 ON tn2.nid = tn.nid INNER JOIN term_data td ON td.tid = tn.tid INNER JOIN term_data td2 ON td2.tid = tn2.tid WHERE (tn.tid = %d) AND tn2.tid <> %d ".$restrict_vids." GROUP BY tn.tid, tn2.tid ORDER BY cnt";
115
116 if($limit <= 0) {
117 $result = db_query($sql, $tid, $tid);
118 } else {
119 $result = db_query_range($sql, $tid, $tid, 0, $limit);
120 }
121
122 $terms = array();
123
124 $order = 0;
125 $order_cnt = 0;
126
127 $key = 0;
128
129 while($row = db_fetch_object($result)) {
130 if($order == 0) {
131 $order = 1;
132 $order_cnt = $order->cnt;
133 } else if($row->cnt != $order_cnt) {
134 $order++;
135 $order_cnt = $roder->cnt;
136 }
137
138 $order = $row->cnt;
139
140 if($sort == 'name') {
141 $key = $row->name;
142 }
143
144 $terms[$key] = array(
145 'tid' => $row->tid,
146 'name' => $row->name,
147 'cnt' => $order
148 );
149
150 if($sort == 'freq' || $sort == 'shuffle') {
151 $key++;
152 }
153
154 }
155
156 if($sort == 'name') {
157 ksort($terms);
158 }
159
160 if($sort == 'shuffle') {
161 srand((float)microtime() * 1000000);
162 shuffle($terms);
163 }
164
165 return $terms;
166
167 }

  ViewVC Help
Powered by ViewVC 1.1.2