qa-tag-cloud.php 4.21 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-plugin/tag-cloud-widget/qa-tag-cloud.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.

	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
*/

Scott committed
23 24 25 26
class qa_tag_cloud
{
	public function option_default($option)
	{
Scott committed
27 28 29 30 31 32 33 34 35 36
		switch ($option) {
			case 'tag_cloud_count_tags':
				return 100;
			case 'tag_cloud_font_size':
				return 24;
			case 'tag_cloud_minimal_font_size':
				return 10;
			case 'tag_cloud_size_popular':
				return true;
		}
Scott committed
37 38 39 40 41 42 43 44 45 46 47 48
	}


	public function admin_form()
	{
		$saved = qa_clicked('tag_cloud_save_button');

		if ($saved) {
			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_minimal_font_size', (int) qa_post_text('tag_cloud_minimal_font_size_field'));
			qa_opt('tag_cloud_size_popular', (int) qa_post_text('tag_cloud_size_popular_field'));
Scott committed
49 50
		}

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

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

Scott committed
63 64 65 66 67 68 69
				array(
					'label' => 'Biggest font size:',
					'suffix' => 'pixels',
					'type' => 'number',
					'value' => (int) qa_opt('tag_cloud_font_size'),
					'tags' => 'name="tag_cloud_font_size_field"',
				),
Scott committed
70

Scott committed
71 72 73 74 75 76
				array(
					'label' => 'Smallest allowed font size:',
					'suffix' => 'pixels',
					'type' => 'number',
					'value' => (int) qa_opt('tag_cloud_minimal_font_size'),
					'tags' => 'name="tag_cloud_minimal_font_size_field"',
Scott committed
77 78
				),

Scott committed
79 80 81 82 83
				array(
					'label' => 'Font size represents tag popularity',
					'type' => 'checkbox',
					'value' => qa_opt('tag_cloud_size_popular'),
					'tags' => 'name="tag_cloud_size_popular_field"',
Scott committed
84
				),
Scott committed
85
			),
Scott committed
86

Scott committed
87 88 89 90 91 92 93 94
			'buttons' => array(
				array(
					'label' => 'Save Changes',
					'tags' => 'name="tag_cloud_save_button"',
				),
			),
		);
	}
Scott committed
95 96


Scott committed
97 98 99 100 101 102 103 104
	public function allow_template($template)
	{
		$allowed = array(
			'activity', 'qa', 'questions', 'hot', 'ask', 'categories', 'question',
			'tag', 'tags', 'unanswered', 'user', 'users', 'search', 'admin', 'custom',
		);
		return in_array($template, $allowed);
	}
Scott committed
105 106


Scott committed
107 108 109 110
	public function allow_region($region)
	{
		return ($region === 'side');
	}
Scott committed
111 112


Scott committed
113 114
	public function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
	{
Scott committed
115
		require_once QA_INCLUDE_DIR.'db/selects.php';
Scott committed
116

Scott committed
117
		$populartags = qa_db_single_select(qa_db_popular_tags_selectspec(0, (int) qa_opt('tag_cloud_count_tags')));
Scott committed
118

119
		$populartagslog = array_map(array($this, 'log_callback'), $populartags);
Scott committed
120 121

		$maxcount = reset($populartagslog);
Scott committed
122

Scott committed
123
		$themeobject->output(sprintf('<h2 style="margin-top: 0; padding-top: 0;">%s</h2>', qa_lang_html('main/popular_tags')));
Scott committed
124

Scott committed
125
		$themeobject->output('<div style="font-size: 10px;">');
Scott committed
126

Scott committed
127 128 129 130
		$maxsize = qa_opt('tag_cloud_font_size');
		$minsize = qa_opt('tag_cloud_minimal_font_size');
		$scale = qa_opt('tag_cloud_size_popular');
		$blockwordspreg = qa_get_block_words_preg();
Scott committed
131

Scott committed
132
		foreach ($populartagslog as $tag => $count) {
Scott committed
133
			$matches = qa_block_words_match_all($tag, $blockwordspreg);
Scott committed
134 135 136 137 138 139 140 141 142 143 144
			if (!empty($matches)) {
				continue;
			}

			if ($scale) {
				$size = number_format($maxsize * $count / $maxcount, 1);
				if ($size < $minsize) {
					$size = $minsize;
				}
			} else {
				$size = $maxsize;
Scott committed
145
			}
Scott committed
146 147

			$themeobject->output(sprintf('<a href="%s" style="font-size: %dpx; vertical-align: baseline;">%s</a>', qa_path_html('tag/' . $tag), $size, qa_html($tag)));
Scott committed
148 149
		}

Scott committed
150
		$themeobject->output('</div>');
Scott committed
151
	}
152 153 154 155 156

	private function log_callback($e)
	{
		return log($e) + 1;
	}
Scott committed
157
}