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