qa-tag-cloud.php 3.9 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-plugin/tag-cloud-widget/qa-tag-cloud.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Widget module class for tag cloud plugin


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

	class qa_tag_cloud {
28

29
		public function option_default($option)
Gideon Greenspan committed
30 31 32
		{
			if ($option=='tag_cloud_count_tags')
				return 100;
33
			if ($option=='tag_cloud_font_size')
Gideon Greenspan committed
34
				return 24;
35
			if ($option=='tag_cloud_size_popular')
Gideon Greenspan committed
36 37 38
				return true;
		}

39

40
		public function admin_form()
Gideon Greenspan committed
41 42
		{
			$saved=false;
43

Gideon Greenspan committed
44 45 46 47 48 49
			if (qa_clicked('tag_cloud_save_button')) {
				qa_opt('tag_cloud_count_tags', (int)qa_post_text('tag_cloud_count_tags_field'));
				qa_opt('tag_cloud_font_size', (int)qa_post_text('tag_cloud_font_size_field'));
				qa_opt('tag_cloud_size_popular', (int)qa_post_text('tag_cloud_size_popular_field'));
				$saved=true;
			}
50

Gideon Greenspan committed
51 52
			return array(
				'ok' => $saved ? 'Tag cloud settings saved' : null,
53

Gideon Greenspan committed
54 55 56 57 58 59
				'fields' => array(
					array(
						'label' => 'Maximum tags to show:',
						'type' => 'number',
						'value' => (int)qa_opt('tag_cloud_count_tags'),
						'suffix' => 'tags',
Gideon Greenspan committed
60
						'tags' => 'name="tag_cloud_count_tags_field"',
Gideon Greenspan committed
61 62 63 64 65 66 67
					),

					array(
						'label' => 'Starting font size:',
						'suffix' => 'pixels',
						'type' => 'number',
						'value' => (int)qa_opt('tag_cloud_font_size'),
Gideon Greenspan committed
68
						'tags' => 'name="tag_cloud_font_size_field"',
Gideon Greenspan committed
69
					),
70

Gideon Greenspan committed
71 72 73 74
					array(
						'label' => 'Font size represents tag popularity',
						'type' => 'checkbox',
						'value' => qa_opt('tag_cloud_size_popular'),
Gideon Greenspan committed
75
						'tags' => 'name="tag_cloud_size_popular_field"',
Gideon Greenspan committed
76 77
					),
				),
78

Gideon Greenspan committed
79 80 81
				'buttons' => array(
					array(
						'label' => 'Save Changes',
Gideon Greenspan committed
82
						'tags' => 'name="tag_cloud_save_button"',
Gideon Greenspan committed
83 84 85 86 87
					),
				),
			);
		}

88

89
		public function allow_template($template)
Gideon Greenspan committed
90
		{
91 92 93 94 95
			$allowed = array(
				'activity', 'qa', 'questions', 'hot', 'ask', 'categories', 'question',
				'tag', 'tags', 'unanswered', 'user', 'users', 'search', 'admin', 'custom',
			);
			return in_array($template, $allowed);
Gideon Greenspan committed
96 97
		}

98

99
		public function allow_region($region)
Gideon Greenspan committed
100 101 102
		{
			return ($region=='side');
		}
103

Gideon Greenspan committed
104

105
		public function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
Gideon Greenspan committed
106 107
		{
			require_once QA_INCLUDE_DIR.'qa-db-selects.php';
108

Gideon Greenspan committed
109
			$populartags=qa_db_single_select(qa_db_popular_tags_selectspec(0, (int)qa_opt('tag_cloud_count_tags')));
110

Gideon Greenspan committed
111 112
			reset($populartags);
			$maxcount=current($populartags);
113

Gideon Greenspan committed
114
			$themeobject->output(
Gideon Greenspan committed
115
				'<h2 style="margin-top:0; padding-top:0;">',
Gideon Greenspan committed
116
				qa_lang_html('main/popular_tags'),
Gideon Greenspan committed
117
				'</h2>'
Gideon Greenspan committed
118
			);
119

Gideon Greenspan committed
120
			$themeobject->output('<div style="font-size:10px;">');
121

Gideon Greenspan committed
122 123
			$maxsize=qa_opt('tag_cloud_font_size');
			$scale=qa_opt('tag_cloud_size_popular');
Gideon Greenspan committed
124
			$blockwordspreg=qa_get_block_words_preg();
125

Gideon Greenspan committed
126
			foreach ($populartags as $tag => $count) {
Gideon Greenspan committed
127 128
				if (count(qa_block_words_match_all($tag, $blockwordspreg)))
					continue; // skip censored tags
129

Gideon Greenspan committed
130
				$size=number_format(($scale ? ($maxsize*$count/$maxcount) : $maxsize), 1);
131

Gideon Greenspan committed
132
				if (($size>=5) || !$scale)
Gideon Greenspan committed
133
					$themeobject->output('<a href="'.qa_path_html('tag/'.$tag).'" style="font-size:'.$size.'px; vertical-align:baseline;">'.qa_html($tag).'</a>');
Gideon Greenspan committed
134
			}
135

Gideon Greenspan committed
136
			$themeobject->output('</div>');
Gideon Greenspan committed
137
		}
138

Gideon Greenspan committed
139
	}
140

Gideon Greenspan committed
141 142 143 144

/*
	Omit PHP closing tag to help avoid accidental output
*/