Completed
Push — develop ( b18c7a...273208 )
by Daniel
11:54 queued 09:05
created

wordgraph::show_word()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
14
class wordgraph extends block
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\content_visibility */
20
	protected $content_visibility;
21
22
	/** @var \phpbb\db\driver\driver_interface */
23
	protected $db;
24
25
	/** @var string */
26
	protected $phpbb_root_path;
27
28
	/** @var string */
29
	protected $php_ext;
30
31
	/** @var int */
32
	protected $cache_time;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\auth\auth					$auth					Auth object
38
	 * @param \phpbb\content_visibility			$content_visibility		Content visibility
39
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
40
	 * @param string							$phpbb_root_path		phpBB root path
41
	 * @param string							$php_ext				phpEx
42
	 */
43 2
	public function __construct(\phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $cache_time = 10800)
44
	{
45 2
		$this->auth = $auth;
46 2
		$this->content_visibility = $content_visibility;
47 2
		$this->db = $db;
48 2
		$this->phpbb_root_path = $phpbb_root_path;
49 2
		$this->php_ext = $php_ext;
50 2
		$this->cache_time = $cache_time;
51 2
	}
52
53
	/**
54
	 * {@inheritdoc}
55
	 */
56 1
	public function get_config(array $settings)
57
	{
58
		return array(
59 1
			'legend1'			=> 'SETTINGS',
60 1
			'show_word_count'	=> array('lang' => 'SHOW_WORD_COUNT', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => 0),
61 1
			'max_num_words'		=> array('lang' => 'MAX_WORDS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 2, 'explain' => false, 'default' => 15),
62 1
			'max_word_size'		=> array('lang' => 'WORD_MAX_SIZE', 'validate' => 'int:0:55', 'type' => 'number:0:55', 'maxlength' => 2, 'explain' => false, 'default' => 25, 'append' => 'PIXEL'),
63 1
			'min_word_size'		=> array('lang' => 'WORD_MIN_SIZE', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 9, 'append' => 'PIXEL'),
64 1
			'exclude_words'		=> array('lang' => 'EXCLUDE_WORDS', 'validate' => 'string', 'type' => 'textarea:5:50', 'maxlength' => 255, 'explain' => true, 'default' => ''),
65 1
		);
66
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71 1
	public function display(array $bdata, $edit_mode = false)
72
	{
73 1
		$settings = $bdata['settings'];
74
75 1
		$block_title = 'WORDGRAPH';
76 1
		$words_array = $this->get_words($settings);
77
78 1
		if (!sizeof($words_array))
79 1
		{
80
			return array(
81
				'title'		=> $block_title,
82
				'content'	=> '',
83
			);
84
		}
85
86 1
		$this->show_graph($words_array, $settings);
87
88
		return array(
89 1
			'title'		=> $block_title,
90 1
			'content'	=> $this->ptemplate->render_view('blitze/sitemaker', 'blocks/wordgraph.html', 'wordgraph_block')
91 1
		);
92
	}
93
94
	/**
95
	 * @param array $words_array
96
	 * @param array $settings
97
	 */
98 1
	private function show_graph(array $words_array, array $settings)
99
	{
100 1
		$params = $this->get_graph_params($words_array, $settings);
101
102
		// Sort words in result
103 1
		$words = array_keys($words_array);
104 1
		sort($words);
105
106 1
		foreach ($words as $word)
107
		{
108 1
			$color = $params['min_sat'] + (($words_array[$word] - $params['min_count']) * $params['color_step']);
109 1
			$r = dechex($color);
110 1
			$b = dechex($params['max_sat'] - $color);
111 1
			$g = 'c';
112
113 1
			$this->ptemplate->assign_block_vars('wordgraph', array(
114 1
				'WORD'			=> $this->show_word($word, $words_array[$word], $settings['show_word_count']),
115 1
				'WORD_SIZE'		=> $settings['min_word_size'] + (($words_array[$word] - $params['min_count']) * $params['size_step']),
116 1
				'WORD_COLOR'	=> $r . $g . $b,
117 1
				'WORD_URL'		=> append_sid("{$this->phpbb_root_path}search.$this->php_ext", 'keywords=' . urlencode($word)),
118 1
			));
119 1
		}
120 1
	}
121
122
	/**
123
	 * @param array $words_array
124
	 * @param array $settings
125
	 * @return array
126
	 */
127 1
	private function get_graph_params(array $words_array, array $settings)
128
	{
129 1
		$max_sat = hexdec('f');
130 1
		$min_sat = hexdec(0);
131
132 1
		$max_count = max(array_values($words_array));
133 1
		$min_count = min(array_values($words_array));
134
135 1
		$spread = $max_count - $min_count;
136 1
		$spread = ($spread) ? $spread : 1;
137
138
		return array(
139 1
			'min_sat'	=> $min_sat,
140 1
			'max_sat'	=> $max_sat,
141
142 1
			'min_count'	=> $min_count,
143 1
			'max_count'	=> $max_count,
144
145 1
			'spread'	=> $spread,
146
147
			// determine the font-size increment
148 1
			'size_step'		=> ($settings['max_word_size'] - $settings['min_word_size']) / $spread,
149 1
			'color_step'	=> ($max_sat - $min_sat) / $spread,
150 1
		);
151
	}
152
153
	/**
154
	 * @param array $settings
155
	 * @return array
156
	 */
157 1
	private function get_words(array $settings)
158
	{
159 1
		$sql_array = $this->get_words_sql($settings['exclude_words']);
160 1
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
161 1
		$result = $this->db->sql_query_limit($sql, $settings['max_num_words'], 0, $this->cache_time);
162
163 1
		$words_array = array();
164 1
		while ($row = $this->db->sql_fetchrow($result))
165
		{
166 1
			$word = ucwords(strtolower($row['word_text']));
167 1
			$words_array[$word] = $row['word_count'];
168 1
		}
169 1
		$this->db->sql_freeresult($result);
170
171 1
		return $words_array;
172
	}
173
174
	/**
175
	 * @param string $exclude_words
176
	 * @return array
177
	 */
178 1
	private function get_words_sql($exclude_words)
179
	{
180 1
		$sql_where = $this->exclude_words_sql($exclude_words);
181
		return array(
182 1
			'SELECT'	=> 'l.word_text, l.word_count',
183
			'FROM'		=> array(
184 1
				SEARCH_WORDLIST_TABLE	=> 'l',
185 1
				SEARCH_WORDMATCH_TABLE	=> 'm',
186 1
				TOPICS_TABLE			=> 't',
187 1
				POSTS_TABLE				=> 'p',
188 1
			),
189
			'WHERE'		=> 'l.word_common <> 1
190
				AND l.word_count > 0
191
				AND m.word_id = l.word_id
192
				AND m.post_id = p.post_id
193
				AND t.topic_id = p.topic_id
194 1
				AND t.topic_time <= ' . time() . '
195 1
				AND ' . $this->content_visibility->get_global_visibility_sql('topic', array_keys($this->auth->acl_getf('!f_read', true)), 't.') .
196 1
				$sql_where,
197 1
			'GROUP_BY'	=> 'l.word_text, l.word_count',
198
			'ORDER_BY'	=> 'l.word_count DESC'
199 1
		);
200
	}
201
202
	/**
203
	 * @param string $exclude_words Comma separated string of words
204
	 * @return string
205
	 */
206 1
	private function exclude_words_sql($exclude_words)
207
	{
208 1
		$sql_where = '';
209
		if ($exclude_words)
210 1
		{
211
			$exclude_words = array_filter(explode(',', str_replace(' ', '', $exclude_words)));
212
			$sql_where = (sizeof($exclude_words)) ? ' AND ' . $this->db->sql_in_set('l.word_text', $exclude_words, true) : '';
213
		}
214
215 1
		return $sql_where;
216
	}
217
218
	/**
219
	 * @param string $word
220
	 * @param int $count
221
	 * @param bool $show_count
222
	 * @return string
223
	 */
224 1
	private function show_word($word, $count, $show_count)
225
	{
226 1
		return censor_text(($show_count) ? $word . '(' . $count . ')' : $word);
227
	}
228
}
229