3 * Copyright (C) 2008-2009 Alcatel-Lucent
4 * Copyright 2016, Franck Villaume - TrivialDev
6 * This file is part of FusionForge. FusionForge is free software;
7 * you can redistribute it and/or modify it under the terms of the
8 * GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the Licence, or (at your option)
12 * FusionForge is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Standard Alcatel-Lucent disclaimer for contributing to open source
25 * "The Tag Cloud ("Contribution") has not been tested and/or
26 * validated for release as or in products, combinations with products or
27 * other commercial use. Any use of the Contribution is entirely made at
28 * the user's own responsibility and the user can not rely on any features,
29 * functionalities or performances Alcatel-Lucent has attributed to the
32 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
33 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
35 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
36 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
37 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
38 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
40 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
46 $CLASS_PREFIX = 'tag';
47 $SELECTED_STYLE = 'text-decoration:overline underline';
50 * tag_cloud() - This function displays a tag cloug with the tags defined by projects.
51 * The size of each tag depends of its frequency.
52 * Each tag is a link to display a list of projects where the tag is defined.
53 * Delta between two font sizes is constant :
54 * function is F(count) = size = A * count + B
63 * A = (Smax - Smin) / (Cmax - Cmin)
65 * If Smin = 1 then Smax = number of size = N
67 * A = (N - 1) / (Cmax - Cmin)
69 * F(count) = size = A * count + 1 - A * Cmin
70 * => size = 1 + (count - Cmin) * A
72 * @param array $params selected tag, max tag displayed, number of sizes available,
73 * class prefix for css class, style for selected tag
76 function tag_cloud($params = array()) {
80 global $SELECTED_STYLE;
83 if (!is_array($params)) {
86 if (!isset($params['selected'])) {
87 $params['selected'] = '';
89 if (!isset($params['nb_max'])) {
90 $params['nb_max'] = $NB_MAX;
92 if (!isset($params['nb_size'])) {
93 $params['nb_size'] = $NB_SIZE;
95 if (!isset($params['class_prefix'])) {
96 $params['class_prefix'] = $CLASS_PREFIX;
98 if (!isset($params['selected_style'])) {
99 $params['selected_style'] = $SELECTED_STYLE;
104 // count tag occurrence
105 $res = db_query_params('SELECT project_tags.name,project_tags.group_id
106 FROM project_tags, groups
107 WHERE project_tags.group_id = groups.group_id
108 AND groups.status = $1 AND groups.register_time > 0',
110 $tag_count = array();
111 while ($row = db_fetch_array($res)) {
112 if (forge_check_perm ('project_read', $row['group_id'])) {
113 $tagname = $row['name'];
114 if (!isset ($tag_count[$tagname])) {
115 $tag_count[$tagname] = 0;
117 $tag_count[$tagname]++;
121 // order tags by popularity
122 $count_to_tags = array();
123 foreach ($tag_count as $name => $count) {
124 if (!isset($count_to_tags[$count])) {
125 $count_to_tags[$count] = array();
127 $count_to_tags[$count][] = $name;
130 $available_counts = array_keys($count_to_tags);
131 rsort($available_counts, SORT_NUMERIC);
133 if (!empty($tag_count)) {
137 foreach ($available_counts as $count) {
138 if ($count_min == 0 || $count < $count_min) {
141 if ($count > $count_max) {
144 $nb = $nb + count($count_to_tags[$count]);
145 if ($params['nb_max'] && $nb >= $params['nb_max']) {
146 break; // no limit if nb_max == 0
150 // Compute 'A' parameter of the function
151 if ($count_max != $count_min) {// else we have a division by zero
152 $a = ($params['nb_size'] - 1) / ($count_max - $count_min);
154 // Set value 0 for 'A' parameter just for initialised variable
155 // but it's not realy necessary because if $count_max == $count_min
156 // then $count - $count_min = 0 (see below)
160 ksort($tag_count, SORT_STRING);
161 foreach ($tag_count as $name => $count) {
162 if ($count < $count_min) {
165 $size = intval(1 + ($count - $count_min) * $a);
167 $linkAttr['class'] = $params['class_prefix'] . $size;
168 if ($name == $params['selected']) {
169 $linkAttr['style'] = $params['selected_style'];
171 $return .= util_make_link('/softwaremap/tag_cloud.php?tag='.urlencode($name), htmlspecialchars($name), $linkAttr);
175 if (!strlen($return)) {
176 $return .= $HTML->warning_msg(_('No used tags.'));
183 * list_project_tag() - Returns the list of the tags defined by the project.
184 * Each tag is a link to display a list of projects
185 * where the tag is defined.
187 * @param int Group ID
190 function list_project_tag($group_id) {
191 $req = 'SELECT name FROM project_tags WHERE group_id = $1';
192 $res = db_query_params($req, array($group_id));
193 $nb_tag = db_numrows($res);
197 while ($row = db_fetch_array($res)) {
198 $return .= util_make_link('/softwaremap/tag_cloud.php?tag='.urlencode($row['name']), htmlspecialchars($row['name']))
199 . (($idx < $nb_tag) ? ', ' : '');
209 // c-file-style: "bsd"