]> git.sur5r.net Git - contagged/blob - tags.php
weighted tagcloud
[contagged] / tags.php
1 <?
2   require_once('init.php');
3   ldap_login();
4
5   //prepare templates
6   tpl_std();
7   $smarty->assign('tagcloud',tag_cloud());
8   //display templates
9   header('Content-Type: text/html; charset=utf-8');
10   $smarty->display('tags.tpl');
11
12   function tag_cloud(){
13     global $conf;
14     global $LDAP_CON;
15     if(!$conf['extended']) return;
16
17     $result = ldap_queryabooks('(objectClass=contactPerson)','marker');
18
19     $max = 0;
20     $min = 999999999;
21     $tags = array();
22     foreach ($result as $entry){
23       if(!empty($entry['marker']) && count($entry['marker'])){
24         foreach($entry['marker'] as $marker){
25           $marker = strtolower($marker);
26           if (empty($tags[$marker])) { $tags[$marker]=0; }
27           $tags[$marker] += 1;
28           if($tags[$marker] > $max) $max = $tags[$marker];
29           if($tags[$marker] < $min) $min = $tags[$marker];
30         }
31       }
32     }
33     ksort($tags);
34     tag_cloud_weight(&$tags,$min,$max,6);
35
36     $out = '';
37     foreach($tags as $tag => $cnt){
38       $out .= '<a href="index.php?marker='.rawurlencode($tag).'" class="cloud_'.$cnt.'">';
39       $out .= htmlspecialchars($tag).'</a> ';
40     }
41
42     return $out;
43   }
44
45   /**
46    * Calculate weights for a nicer tagcloud distribution
47    */
48   function tag_cloud_weight(&$tags,$min,$max,$levels){
49     // calculate tresholds
50     $tresholds = array();
51     for($i=0; $i<=$levels; $i++){
52         $tresholds[$i] = pow($max - $min + 1, $i/$levels);
53     }
54
55     // assign weights
56     foreach($tags as $tag => $cnt){
57         foreach($tresholds as $tresh => $val){
58             if($cnt <= $val){
59                 $tags[$tag] = $tresh;
60                 break;
61             }
62             $tags[$tag] = $levels;
63         }
64     }
65   }
66
67