3
* This file implements the "Tag cloud" widget
5
* @author {@link http://wonderwinds.com/ Ed Bennett}
6
* @author {@link http://fplanque.net/ Francois PLANQUE}
7
* @copyright (c) 2009 by {@link http://quamplures.net/ the Quam Plures project}
8
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3
11
if(!defined('QP_MAIN_INIT')) die('fail');
14
* tag_cloud_Widget class
17
class tag_cloud_Widget extends ComponentWidget
22
function tag_cloud_Widget( $db_row = NULL )
24
// Call parent constructor
25
parent::ComponentWidget( $db_row, 'widget', 'tag_cloud' );
26
$this->widget_name = T_('Tag Cloud');
27
$this->widget_title = T_('Tag cloud');
32
* Get widget's short description
36
return T_('Cloud of all tags; click filters blog on selected tag.');
42
* @todo (3069): rename this to get_title()
44
function get_short_desc()
46
return format_to_output( $this->disp_params['widget_title'] );
53
function load_from_Request()
55
parent::load_from_Request();
57
if( empty( $this->param_array['tag_separator'] ) )
59
// Default name, don't store
60
$this->set( 'tag_separator', ' ' );
66
* Get definitions for editable params
67
* @see Plugin::GetDefaultSettings()
68
* @param local params like 'for_editing' => true
70
function get_param_definitions( $params )
72
$r = array_merge( array(
74
'label' => T_('Max # of tags'),
79
'tag_separator' => array(
80
'label' => T_('Tag separator'),
82
'defaultvalue' => ' ',
85
'tag_min_size' => array(
86
'label' => T_('Min size'),
91
'tag_max_size' => array(
92
'label' => T_('Max size'),
97
'tag_cloud_start' => array(
98
'label' => T_('Cloud start'),
99
'type' => 'html_input',
100
'defaultvalue' => '<p class="tag_cloud">',
103
'tag_cloud_end' => array(
104
'label' => T_('Cloud end'),
105
'type' => 'html_input',
106
'defaultvalue' => '</p>',
108
), parent::get_param_definitions( $params ) );
114
* Display the widget!
115
* @param array must contain at least the basic display params
117
function display( $params )
124
// Nothing to display
128
$this->init_display( $params );
130
// get list of relevant blogs
131
$sql = 'SELECT LOWER(tag_name) AS tag_name, COUNT(DISTINCT itag_itm_ID) AS tag_count
132
FROM T_items__tag INNER JOIN T_items__itemtag ON itag_tag_ID = tag_ID
133
INNER JOIN T_postcats ON itag_itm_ID = postcat_post_ID
134
INNER JOIN T_categories ON postcat_cat_ID = cat_ID
135
INNER JOIN T_items__item ON itag_itm_ID = post_ID
136
WHERE '.$Blog->get_sql_where_aggregate_blog_IDs('cat_blog_ID').'
137
AND post_status = "published" AND post_datestart < NOW()
138
GROUP BY tag_name ORDER BY tag_count DESC LIMIT '.$this->disp_params['max_tags'];
139
$results = $DB->get_results( $sql, OBJECT, 'Get tags' );
140
if( empty( $results ) )
146
$max_count = $results[0]->tag_count;
147
$min_count = $results[count( $results )-1]->tag_count;
148
$count_span = max( 1, $max_count - $min_count );
149
$max_size = $this->disp_params['tag_max_size'];
150
$min_size = $this->disp_params['tag_min_size'];
151
$size_span = $max_size - $min_size;
153
usort( $results, array( $this, 'tag_cloud_cmp' ) );
155
echo $this->disp_params['block_start'];
157
echo $this->disp_params['tag_cloud_start'];
159
foreach( $results as $row )
163
echo $this->disp_params['tag_separator'];
165
// If there's a space in the tag name, quote it
166
$tag_name_disp = strpos( $row->tag_name, ' ' )
167
? '«'.format_to_output( $row->tag_name, 'htmlbody' ).'»'
168
: format_to_output( $row->tag_name, 'htmlbody' );
169
$size = floor( $row->tag_count * $size_span / $count_span + $min_size );
171
echo $Blog->get_tag_link( $row->tag_name, $tag_name_disp, array(
172
'style' => 'font-size: '.$size.'pt;',
173
'title' => sprintf( T_('%d posts'), $row->tag_count )
177
echo $this->disp_params['tag_cloud_end'];
178
echo $this->disp_params['block_end'];
185
* support function for display()
187
function tag_cloud_cmp( $a, $b )
189
return strcasecmp( $a->tag_name, $b->tag_name );