~quam-plures-core/quam-plures/qp5_colls-blogs_chaps-cats

« back to all changes in this revision

Viewing changes to qp_inc/widgets/_tag_cloud.widget.php

http://forums.quamplures.net/viewtopic.php?p=9237#p9237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * This file implements the "Tag cloud" widget
 
4
 *
 
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
 
9
 * @package widgets
 
10
 */
 
11
if(!defined('QP_MAIN_INIT')) die('fail');
 
12
 
 
13
/**
 
14
 * tag_cloud_Widget class
 
15
 * @package widgets
 
16
 */
 
17
class tag_cloud_Widget extends ComponentWidget
 
18
{
 
19
        /**
 
20
         * Constructor
 
21
         */
 
22
        function tag_cloud_Widget( $db_row = NULL )
 
23
        {
 
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');
 
28
        }
 
29
 
 
30
 
 
31
        /**
 
32
         * Get widget's short description
 
33
         */
 
34
        function get_desc()
 
35
        {
 
36
                return T_('Cloud of all tags; click filters blog on selected tag.');
 
37
        }
 
38
 
 
39
 
 
40
        /**
 
41
         * Get widget's title
 
42
         * @todo (3069): rename this to get_title()
 
43
         */
 
44
        function get_short_desc()
 
45
        {
 
46
                return format_to_output( $this->disp_params['widget_title'] );
 
47
        }
 
48
 
 
49
 
 
50
        /**
 
51
         * Load params
 
52
         */
 
53
        function load_from_Request()
 
54
        {
 
55
                parent::load_from_Request();
 
56
                // SPECIAL treatments
 
57
                if( empty( $this->param_array['tag_separator'] ) )
 
58
                {
 
59
                        // Default name, don't store
 
60
                        $this->set( 'tag_separator', ' ' );
 
61
                }
 
62
        }
 
63
 
 
64
 
 
65
        /**
 
66
         * Get definitions for editable params
 
67
         * @see Plugin::GetDefaultSettings()
 
68
         * @param local params like 'for_editing' => true
 
69
         */
 
70
        function get_param_definitions( $params )
 
71
        {
 
72
                $r = array_merge( array(
 
73
                        'max_tags' => array(
 
74
                                'label' => T_('Max # of tags'),
 
75
                                'type' => 'integer',
 
76
                                'defaultvalue' => 50,
 
77
                                'size' => 4,
 
78
                        ),
 
79
                        'tag_separator' => array(
 
80
                                'label' => T_('Tag separator'),
 
81
                                'type' => 'text',
 
82
                                'defaultvalue' => ' ',
 
83
                                'maxlength' => 100,
 
84
                        ),
 
85
                        'tag_min_size' => array(
 
86
                                'label' => T_('Min size'),
 
87
                                'type' => 'integer',
 
88
                                'defaultvalue' => 8,
 
89
                                'size' => 3,
 
90
                        ),
 
91
                        'tag_max_size' => array(
 
92
                                'label' => T_('Max size'),
 
93
                                'type' => 'integer',
 
94
                                'defaultvalue' => 22,
 
95
                                'size' => 3,
 
96
                        ),
 
97
                        'tag_cloud_start' => array(
 
98
                                'label' => T_('Cloud start'),
 
99
                                'type' => 'html_input',
 
100
                                'defaultvalue' => '<p class="tag_cloud">',
 
101
                                'size' => 28,
 
102
                        ),
 
103
                        'tag_cloud_end' => array(
 
104
                                'label' => T_('Cloud end'),
 
105
                                'type' => 'html_input',
 
106
                                'defaultvalue' => '</p>',
 
107
                        ),
 
108
                ), parent::get_param_definitions( $params ) );
 
109
                return $r;
 
110
        }
 
111
 
 
112
 
 
113
        /**
 
114
         * Display the widget!
 
115
         * @param array must contain at least the basic display params
 
116
         */
 
117
        function display( $params )
 
118
        {
 
119
                global $Blog;
 
120
                global $DB;
 
121
 
 
122
                if( empty( $Blog ) )
 
123
                {
 
124
                        // Nothing to display
 
125
                        return;
 
126
                }
 
127
 
 
128
                $this->init_display( $params );
 
129
 
 
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 ) )
 
141
                {
 
142
                        // No tags!
 
143
                        return;
 
144
                }
 
145
 
 
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;
 
152
 
 
153
                usort( $results, array( $this, 'tag_cloud_cmp' ) );
 
154
 
 
155
                echo $this->disp_params['block_start'];
 
156
                $this->disp_title();
 
157
                echo $this->disp_params['tag_cloud_start'];
 
158
                $count = 0;
 
159
                foreach( $results as $row )
 
160
                {
 
161
                        if( $count > 0 )
 
162
                        {
 
163
                                echo $this->disp_params['tag_separator'];
 
164
                        }
 
165
                        // If there's a space in the tag name, quote it
 
166
                        $tag_name_disp = strpos( $row->tag_name, ' ' )
 
167
                                ? '&laquo;'.format_to_output( $row->tag_name, 'htmlbody' ).'&raquo;'
 
168
                                : format_to_output( $row->tag_name, 'htmlbody' );
 
169
                        $size = floor( $row->tag_count * $size_span / $count_span + $min_size );
 
170
 
 
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 )
 
174
                        ) );
 
175
                        $count++;
 
176
                }
 
177
                echo $this->disp_params['tag_cloud_end'];
 
178
                echo $this->disp_params['block_end'];
 
179
 
 
180
                return true;
 
181
        }
 
182
 
 
183
 
 
184
        /**
 
185
         * support function for display()
 
186
         */
 
187
        function tag_cloud_cmp( $a, $b )
 
188
        {
 
189
                return strcasecmp( $a->tag_name, $b->tag_name );
 
190
        }
 
191
 
 
192
}
 
193
 
 
194
?>