Completed
Push — develop ( 273208...5b590b )
by Daniel
09:31
created

wordgraph   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 16
c 8
b 1
f 1
lcom 1
cbo 2
dl 0
loc 211
ccs 93
cts 93
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A show_graph() 0 23 2
B get_graph_params() 0 25 2
A get_words() 0 16 2
A get_words_sql() 0 23 1
A exclude_words_sql() 0 11 3
A show_word() 0 4 2
A __construct() 0 9 1
A get_config() 0 11 1
A display() 0 18 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 3
	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 3
		$this->auth = $auth;
46 3
		$this->content_visibility = $content_visibility;
47 3
		$this->db = $db;
48 3
		$this->phpbb_root_path = $phpbb_root_path;
49 3
		$this->php_ext = $php_ext;
50 3
		$this->cache_time = $cache_time;
51 3
	}
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 2
	public function display(array $bdata, $edit_mode = false)
72
	{
73 2
		$settings = $bdata['settings'];
74
		$block = array(
75 2
			'title'		=> 'WORDGRAPH',
76 2
			'content'	=> '',
77 2
		);
78
79 2
		$words_array = $this->get_words($settings);
80 2
		if (sizeof($words_array))
81 2
		{
82 2
			$this->show_graph($words_array, $settings);
83
	
84 2
			$block['content'] = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/wordgraph.html', 'wordgraph_block');
85 2
		}
86
87 2
		return $block;
88
	}
89
90
	/**
91
	 * @param array $words_array
92
	 * @param array $settings
93
	 */
94 2
	private function show_graph(array $words_array, array $settings)
95
	{
96 2
		$params = $this->get_graph_params($words_array, $settings);
97
98
		// Sort words in result
99 2
		$words = array_keys($words_array);
100 2
		sort($words);
101
102 2
		foreach ($words as $word)
103
		{
104 2
			$color = $params['min_sat'] + (($words_array[$word] - $params['min_count']) * $params['color_step']);
105 2
			$r = dechex($color);
106 2
			$b = dechex($params['max_sat'] - $color);
107 2
			$g = 'c';
108
109 2
			$this->ptemplate->assign_block_vars('wordgraph', array(
110 2
				'WORD'			=> $this->show_word($word, $words_array[$word], $settings['show_word_count']),
111 2
				'WORD_SIZE'		=> $settings['min_word_size'] + (($words_array[$word] - $params['min_count']) * $params['size_step']),
112 2
				'WORD_COLOR'	=> $r . $g . $b,
113 2
				'WORD_URL'		=> append_sid("{$this->phpbb_root_path}search.$this->php_ext", 'keywords=' . urlencode($word)),
114 2
			));
115 2
		}
116 2
	}
117
118
	/**
119
	 * @param array $words_array
120
	 * @param array $settings
121
	 * @return array
122
	 */
123 2
	private function get_graph_params(array $words_array, array $settings)
124
	{
125 2
		$max_sat = hexdec('f');
126 2
		$min_sat = hexdec(0);
127
128 2
		$max_count = max(array_values($words_array));
129 2
		$min_count = min(array_values($words_array));
130
131 2
		$spread = $max_count - $min_count;
132 2
		$spread = ($spread) ? $spread : 1;
133
134
		return array(
135 2
			'min_sat'	=> $min_sat,
136 2
			'max_sat'	=> $max_sat,
137
138 2
			'min_count'	=> $min_count,
139 2
			'max_count'	=> $max_count,
140
141 2
			'spread'	=> $spread,
142
143
			// determine the font-size increment
144 2
			'size_step'		=> ($settings['max_word_size'] - $settings['min_word_size']) / $spread,
145 2
			'color_step'	=> ($max_sat - $min_sat) / $spread,
146 2
		);
147
	}
148
149
	/**
150
	 * @param array $settings
151
	 * @return array
152
	 */
153 2
	private function get_words(array $settings)
154
	{
155 2
		$sql_array = $this->get_words_sql($settings['exclude_words']);
156 2
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
157 2
		$result = $this->db->sql_query_limit($sql, $settings['max_num_words'], 0, $this->cache_time);
158
159 2
		$words_array = array();
160 2
		while ($row = $this->db->sql_fetchrow($result))
161
		{
162 2
			$word = ucwords(strtolower($row['word_text']));
163 2
			$words_array[$word] = $row['word_count'];
164 2
		}
165 2
		$this->db->sql_freeresult($result);
166
167 2
		return $words_array;
168
	}
169
170
	/**
171
	 * @param string $exclude_words
172
	 * @return array
173
	 */
174 2
	private function get_words_sql($exclude_words)
175
	{
176 2
		$sql_where = $this->exclude_words_sql($exclude_words);
177
		return array(
178 2
			'SELECT'	=> 'l.word_text, l.word_count',
179
			'FROM'		=> array(
180 2
				SEARCH_WORDLIST_TABLE	=> 'l',
181 2
				SEARCH_WORDMATCH_TABLE	=> 'm',
182 2
				TOPICS_TABLE			=> 't',
183 2
				POSTS_TABLE				=> 'p',
184 2
			),
185
			'WHERE'		=> 'l.word_common <> 1
186
				AND l.word_count > 0
187
				AND m.word_id = l.word_id
188
				AND m.post_id = p.post_id
189
				AND t.topic_id = p.topic_id
190 2
				AND t.topic_time <= ' . time() . '
191 2
				AND ' . $this->content_visibility->get_global_visibility_sql('topic', array_keys($this->auth->acl_getf('!f_read', true)), 't.') .
192 2
				$sql_where,
193 2
			'GROUP_BY'	=> 'l.word_text, l.word_count',
194
			'ORDER_BY'	=> 'l.word_count DESC'
195 2
		);
196
	}
197
198
	/**
199
	 * @param string $exclude_words Comma separated string of words
200
	 * @return string
201
	 */
202 2
	private function exclude_words_sql($exclude_words)
203
	{
204 2
		$sql_where = '';
205
		if ($exclude_words)
206 2
		{
207 1
			$exclude_words = array_filter(explode(',', str_replace(' ', '', strtolower($exclude_words))));
208 1
			$sql_where = (sizeof($exclude_words)) ? ' AND ' . $this->db->sql_in_set('l.word_text', $exclude_words, true) : '';
209 1
		}
210
211 2
		return $sql_where;
212
	}
213
214
	/**
215
	 * @param string $word
216
	 * @param int $count
217
	 * @param bool $show_count
218
	 * @return string
219
	 */
220 2
	private function show_word($word, $count, $show_count)
221
	{
222 2
		return censor_text(($show_count) ? $word . '(' . $count . ')' : $word);
223
	}
224
}
225