Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

wordgraph::get_wordgraph()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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