| 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' => $this->get_wordgraph($words_array, $settings), |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $words_array |
||
| 81 | * @param array $settings |
||
| 82 | * @return [] |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 83 | */ |
||
| 84 | protected function get_wordgraph(array $words_array, array $settings) |
||
| 85 | { |
||
| 86 | $params = $this->get_graph_params($words_array, $settings); |
||
| 87 | |||
| 88 | // Sort words in result |
||
| 89 | $words = array_filter(array_keys($words_array)); |
||
| 90 | sort($words); |
||
| 91 | |||
| 92 | $wordgraph = []; |
||
| 93 | foreach ($words as $word) |
||
| 94 | { |
||
| 95 | $color = (int) ($params['min_sat'] + (($words_array[$word] - $params['min_count']) * $params['color_step'])); |
||
| 96 | $r = dechex($color); |
||
| 97 | $b = dechex($params['max_sat'] - $color); |
||
| 98 | $g = 'c'; |
||
| 99 | |||
| 100 | $wordgraph[] = array( |
||
| 101 | 'WORD' => $this->show_word($word, $words_array[$word], $settings['show_word_count']), |
||
| 102 | 'WORD_SIZE' => $settings['min_word_size'] + (($words_array[$word] - $params['min_count']) * $params['size_step']), |
||
| 103 | 'WORD_COLOR' => $r . $g . $b, |
||
| 104 | 'WORD_URL' => $this->get_url($word), |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $wordgraph; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array $words_array |
||
| 113 | * @param array $settings |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | protected function get_graph_params(array $words_array, array $settings) |
||
| 117 | { |
||
| 118 | $max_sat = hexdec('f'); |
||
| 119 | $min_sat = hexdec(0); |
||
| 120 | |||
| 121 | $max_count = max(array_values($words_array)); |
||
| 122 | $min_count = min(array_values($words_array)); |
||
| 123 | |||
| 124 | $spread = $max_count - $min_count; |
||
| 125 | $spread = ($spread) ? $spread : 1; |
||
| 126 | |||
| 127 | return array( |
||
| 128 | 'min_sat' => $min_sat, |
||
| 129 | 'max_sat' => $max_sat, |
||
| 130 | |||
| 131 | 'min_count' => $min_count, |
||
| 132 | 'max_count' => $max_count, |
||
| 133 | |||
| 134 | 'spread' => $spread, |
||
| 135 | |||
| 136 | // determine the font-size increment |
||
| 137 | 'size_step' => ($settings['max_word_size'] - $settings['min_word_size']) / $spread, |
||
| 138 | 'color_step' => ($max_sat - $min_sat) / $spread, |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param array $settings |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | protected function get_words(array $settings) |
||
| 147 | { |
||
| 148 | $sql_array = $this->forum->query(false, false) |
||
| 149 | ->fetch_custom($this->get_custom_sql_array($settings), array('SELECT')) |
||
| 150 | ->build(true, true, false) |
||
| 151 | ->get_sql_array(); |
||
| 152 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
| 153 | $result = $this->db->sql_query_limit($sql, $settings['max_num_words'], 0, $this->cache_time); |
||
| 154 | |||
| 155 | $words_array = array(); |
||
| 156 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 157 | { |
||
| 158 | $word = ucwords(strtolower($row['word_text'])); |
||
| 159 | $words_array[$word] = $row['word_count']; |
||
| 160 | } |
||
| 161 | $this->db->sql_freeresult($result); |
||
| 162 | |||
| 163 | return $words_array; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param array $settings |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function get_custom_sql_array(array $settings) |
||
| 171 | { |
||
| 172 | return array( |
||
| 173 | 'SELECT' => array('l.word_text', 'l.word_count'), |
||
| 174 | 'FROM' => array( |
||
| 175 | SEARCH_WORDLIST_TABLE => 'l', |
||
| 176 | SEARCH_WORDMATCH_TABLE => 'm', |
||
| 177 | TOPICS_TABLE => 't', |
||
| 178 | POSTS_TABLE => 'p', |
||
| 179 | ), |
||
| 180 | 'WHERE' => 'l.word_common <> 1 |
||
| 181 | AND l.word_count > 0 |
||
| 182 | AND m.word_id = l.word_id |
||
| 183 | AND m.post_id = p.post_id |
||
| 184 | AND t.topic_id = p.topic_id' . $this->exclude_words_sql($settings['exclude_words']), |
||
| 185 | 'GROUP_BY' => 'l.word_text, l.word_count', |
||
| 186 | 'ORDER_BY' => 'l.word_count DESC' |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $exclude_words Comma separated string of words |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | protected function exclude_words_sql($exclude_words) |
||
| 195 | { |
||
| 196 | $sql_where = ''; |
||
| 197 | if ($exclude_words) |
||
| 198 | { |
||
| 199 | $exclude_words = array_filter(explode(',', str_replace(' ', '', strtolower($exclude_words)))); |
||
| 200 | $sql_where = (sizeof($exclude_words)) ? ' AND ' . $this->db->sql_in_set('l.word_text', $exclude_words, true) : ''; |
||
| 201 | } |
||
| 202 | |||
| 203 | return $sql_where; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $word |
||
| 208 | * @param int $count |
||
| 209 | * @param bool $show_count |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | protected function show_word($word, $count, $show_count) |
||
| 213 | { |
||
| 214 | return censor_text(($show_count) ? $word . '(' . $count . ')' : $word); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $word |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | protected function get_url($word) |
||
| 222 | { |
||
| 223 | return append_sid("{$this->phpbb_root_path}search.$this->php_ext", 'keywords=' . urlencode($word)); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function get_template() |
||
| 230 | { |
||
| 231 | return '@blitze_sitemaker/blocks/wordgraph.html'; |
||
| 232 | } |
||
| 233 | } |
||
| 234 |