3 * Copyright (C) 2008-2009 Alcatel-Lucent
5 * This file is part of FusionForge. FusionForge is free software;
6 * you can redistribute it and/or modify it under the terms of the
7 * GNU General Public License as published by the Free Software
8 * Foundation; either version 2 of the Licence, or (at your option)
11 * FusionForge is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Standard Alcatel-Lucent disclaimer for contributing to open source
24 * "The Tag Cloud ("Contribution") has not been tested and/or
25 * validated for release as or in products, combinations with products or
26 * other commercial use. Any use of the Contribution is entirely made at
27 * the user's own responsibility and the user can not rely on any features,
28 * functionalities or performances Alcatel-Lucent has attributed to the
31 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
32 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
33 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
34 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
35 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
36 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
37 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
38 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
39 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
45 $CLASS_PREFIX = 'tag';
46 $SELECTED_STYLE = 'style="text-decoration:overline underline;"';
49 * tag_cloud() - This function displays a tag cloug with the tags defined by projects.
50 * The size of each tag depends of its frequency.
51 * Each tag is a link to display a list of projects where the tag is defined.
52 * Delta between two font sizes is constant :
53 * function is F(count) = size = A * count + B
62 * A = (Smax - Smin) / (Cmax - Cmin)
64 * If Smin = 1 then Smax = number of size = N
66 * A = (N - 1) / (Cmax - Cmin)
68 * F(count) = size = A * count + 1 - A * Cmin
69 * => size = 1 + (count - Cmin) * A
71 * @param array $params selected tag, max tag displayed, number of sizes available,
72 * class prefix for css class, style for selected tag
75 function tag_cloud($params = array()) {
79 global $SELECTED_STYLE;
81 if (! is_array($params)) $params = array();
82 if (! isset($params['selected'])) {
83 $params['selected'] = '';
85 if (! isset($params['nb_max'])) {
86 $params['nb_max'] = $NB_MAX;
88 if (! isset($params['nb_size'])) {
89 $params['nb_size'] = $NB_SIZE;
91 if (! isset($params['class_prefix'])) {
92 $params['class_prefix'] = $CLASS_PREFIX;
94 if (! isset($params['selected_style'])) {
95 $params['selected_style'] = $SELECTED_STYLE;
100 // count tag occurrence
101 $res = db_query_params ('SELECT project_tags.name,project_tags.group_id
102 FROM project_tags, groups
103 WHERE project_tags.group_id = groups.group_id
104 AND groups.status = $1 AND groups.type_id=1 AND groups.register_time > 0',
106 $tag_count = array();
107 while ($row = db_fetch_array($res)) {
108 if (forge_check_perm ('project_read', $row['group_id'])) {
109 $tagname = $row['name'];
110 if (!isset ($tag_count[$tagname])) {
111 $tag_count[$tagname] = 0;
113 $tag_count[$tagname]++;
117 // order tags by popularity
118 $count_to_tags = array();
119 foreach ($tag_count as $name => $count) {
120 if (!isset($count_to_tags[$count])) {
121 $count_to_tags[$count] = array();
123 $count_to_tags[$count][] = $name;
126 $available_counts = array_keys($count_to_tags);
127 rsort ($available_counts, SORT_NUMERIC);
129 if (count($tag_count) > 0) {
133 foreach ($available_counts as $count) {
134 if ($count_min == 0 || $count < $count_min) $count_min = $count;
135 if ($count > $count_max) $count_max = $count;
136 $nb = $nb + count($count_to_tags[$count]);
137 if ($params['nb_max'] && $nb >= $params['nb_max']) break; // no limit if nb_max == 0
140 // Compute 'A' parameter of the function
141 if ($count_max != $count_min) // else we have a division by zero
143 $a = ($params['nb_size'] - 1) / ($count_max - $count_min);
146 // Set value 0 for 'A' parameter just for initialised variable
147 // but it's not realy necessary because if $count_max == $count_min
148 // then $count - $count_min = 0 (see below)
152 ksort($tag_count, SORT_STRING);
153 foreach ($tag_count as $name => $count) {
154 if ($count < $count_min) continue;
155 $size = intval(1 + ($count - $count_min) * $a);
156 $return .= '<a href="/softwaremap/tag_cloud.php?tag='
158 . '" class="' . $params['class_prefix'] . $size . '" '
159 . (($name == $params['selected']) ? $params['selected_style'] : '' )
160 . '>' . htmlspecialchars($name) . '</a> ';
168 * list_project_tag() - Returns the list of the tags defined by the project.
169 * Each tag is a link to display a list of projects
170 * where the tag is defined.
172 * @param int Group ID
175 function list_project_tag($group_id) {
176 $req = 'SELECT name FROM project_tags WHERE group_id = $1';
177 $res = db_query_params($req, array($group_id));
178 $nb_tag = db_numrows($res);
182 while ($row = db_fetch_array($res)) {
183 $return .= util_make_link('/softwaremap/tag_cloud.php?tag='.urlencode($row['name']), htmlspecialchars($row['name']))
184 . (($idx < $nb_tag) ? ', ' : '');
194 // c-file-style: "bsd"