Completed
Push — master ( db7709...24ebcc )
by Daniel
10:24
created

wordgraph::_get_words()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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