Passed
Push — renovate/configure ( c923d4...2c70da )
by
unknown
20:13
created

wordgraph::exclude_words_sql()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 10
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
	 */
41
	public function __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\services\forum\data $forum, $phpbb_root_path, $php_ext, $cache_time)
42
	{
43
		$this->db = $db;
44
		$this->forum = $forum;
45
		$this->phpbb_root_path = $phpbb_root_path;
46
		$this->php_ext = $php_ext;
47
		$this->cache_time = $cache_time;
48
	}
49
50
	/**
51
	 * {@inheritdoc}
52
	 */
53
	public function get_config(array $settings)
54
	{
55
		return array(
56
			'legend1'			=> 'SETTINGS',
57
			'show_word_count'	=> array('lang' => 'SHOW_WORD_COUNT', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => 0),
58
			'max_num_words'		=> array('lang' => 'MAX_WORDS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 2, 'explain' => false, 'default' => 15),
59
			'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
			'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
			'exclude_words'		=> array('lang' => 'EXCLUDE_WORDS', 'validate' => 'string', 'type' => 'textarea:5:50', 'maxlength' => 255, 'explain' => true, 'default' => ''),
62
		);
63
	}
64
65
	/**
66
	 * {@inheritdoc}
67
	 */
68
	public function display(array $bdata, $edit_mode = false)
69
	{
70
		$settings = $bdata['settings'];
71
		$words_array = $this->get_words($settings);
72
73
		return array(
74
			'title'	=> 'WORDGRAPH',
75
			'data'	=> array(
76
				'words'	=> $this->get_wordgraph($words_array, $settings),
77
			),
78
		);
79
	}
80
81
	/**
82
	 * @param array $words_array
83
	 * @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
		// Sort words in result
91
		$words = array_filter(array_keys($words_array));
92
		sort($words);
93
94
		$wordgraph = [];
95
		foreach ($words as $word)
96
		{
97
			$color = (int) ($params['min_sat'] + (($words_array[$word] - $params['min_count']) * $params['color_step']));
98
			$r = dechex($color);
99
			$b = dechex($params['max_sat'] - $color);
100
			$g = 'c';
101
102
			$wordgraph[] = array(
103
				'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
				'WORD_COLOR'	=> $r . $g . $b,
106
				'WORD_URL'		=> $this->get_url($word),
107
			);
108
		}
109
110
		return $wordgraph;
111
	}
112
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
	{
120
		$max_sat = (int) hexdec('f');
121
		$min_sat = (int) hexdec(0);
122
123
		$max_count = (int) max(array_values($words_array));
124
		$min_count = (int) min(array_values($words_array));
125
126
		$spread = $max_count - $min_count;
127
		$spread = ($spread) ? $spread : 1;
128
129
		return array(
130
			'min_sat'	=> $min_sat,
131
			'max_sat'	=> $max_sat,
132
133
			'min_count'	=> $min_count,
134
			'max_count'	=> $max_count,
135
136
			'spread'	=> $spread,
137
138
			// determine the font-size increment
139
			'size_step'		=> ($settings['max_word_size'] - $settings['min_word_size']) / $spread,
140
			'color_step'	=> ($max_sat - $min_sat) / $spread,
141
		);
142
	}
143
144
	/**
145
	 * @param array $settings
146
	 * @return array
147
	 */
148
	protected function get_words(array $settings)
149
	{
150
		$sql_array = $this->forum->query(false, false)
151
			->fetch_custom($this->get_custom_sql_array($settings), array('SELECT'))
152
			->build(true, true, false)
153
			->get_sql_array();
154
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
155
		$result = $this->db->sql_query_limit($sql, $settings['max_num_words'], 0, $this->cache_time);
156
157
		$words_array = array();
158
		while ($row = $this->db->sql_fetchrow($result))
159
		{
160
			$word = ucwords(strtolower($row['word_text']));
161
			$words_array[$word] = $row['word_count'];
162
		}
163
		$this->db->sql_freeresult($result);
164
165
		return $words_array;
166
	}
167
168
	/**
169
	 * @param array $settings
170
	 * @return array
171
	 */
172
	protected function get_custom_sql_array(array $settings)
173
	{
174
		return array(
175
			'SELECT'	=> array('l.word_text', 'l.word_count'),
176
			'FROM'		=> array(
177
				SEARCH_WORDLIST_TABLE	=> 'l',
178
				SEARCH_WORDMATCH_TABLE	=> 'm',
179
				TOPICS_TABLE			=> 't',
180
				POSTS_TABLE				=> 'p',
181
			),
182
			'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
			'GROUP_BY'	=> 'l.word_text, l.word_count',
188
			'ORDER_BY'	=> 'l.word_count DESC'
189
		);
190
	}
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
	{
198
		$sql_where = '';
199
		if ($exclude_words)
200
		{
201
			$exclude_words = array_filter(explode(',', str_replace(' ', '', strtolower($exclude_words))));
202
			$sql_where = (sizeof($exclude_words)) ? ' AND ' . $this->db->sql_in_set('l.word_text', $exclude_words, true) : '';
203
		}
204
205
		return $sql_where;
206
	}
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
	{
216
		return censor_text(($show_count) ? $word . '(' . $count . ')' : $word);
217
	}
218
219
	/**
220
	 * @param string $word
221
	 * @return string
222
	 */
223
	protected function get_url($word)
224
	{
225
		return append_sid("{$this->phpbb_root_path}search.$this->php_ext", 'keywords=' . urlencode($word));
226
	}
227
228
	/**
229
	 * {@inheritdoc}
230
	 */
231
	public function get_template()
232
	{
233
		return '@blitze_sitemaker/blocks/wordgraph.html';
234
	}
235
}
236