-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
274 lines (263 loc) · 9.29 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* @version 1.0
* @package mod_qitagcloud
* @author www.huangqi.space
* @copyright Copyright(C)2015 Qi Projects. All rights reserved.
* @license GNU/GPL, see LICENSE.php
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
class modTagcloudhelper
{
public function getTags($params, $moduleid)
{
global $mainframe;
$db = JFactory :: getDBO();
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$document = JFactory::getDocument();
$max_tags = $params->get('max_tags');
$selected_tags = $params->get('selected_tags');
$tags = new stdClass();
$count = array();
$tags->size = array();
$tags->color = array();
$tags->id = array();
$tags->alias = array();
$tags->moduleid = $moduleid;
$query = $db->getQuery(true)
->select(
array(
$db->quoteName('m.tag_id') . ' AS tag_id',
$db->quoteName('t.access') . ' AS access',
$db->quoteName('t.alias') . ' AS alias',
$db->quoteName('t.path') . ' AS path',
$db->quoteName('t.title') . ' AS title',
$db->quoteName('t.parent_id') . ' AS parent_id',
$db->quoteName('m.type_alias') . ' AS type_alias',
$db->quoteName('c.core_catid') . ' AS catid'
)
)
->from($db->quoteName('#__contentitem_tag_map') . ' AS m')
->where($db->quoteName('t.access') . ' IN (' . $groups . ')');
// only return accessible content
$query->where($db->quoteName('c.core_access') . ' IN (' . $groups . ')');
// only return published tags
$query->where($db->quoteName('t.published') . ' = 1 ');
// only return published content
$query->where($db->quoteName('c.core_state') . ' = 1 ');
// Optionally filter on language
$language = JComponentHelper::getParams('com_tags')->get('tag_list_language_filter', 'all');
if ($language != 'all')
{
if ($language == 'current_language')
{
$language = JHelperContent::getCurrentLanguage();
}
$query->where($db->quoteName('t.language') . ' IN (' . $db->quote($language) . ', ' . $db->quote('*') . ')');
}
$query->join('INNER', $db->quoteName('#__tags', 't') . ' ON m.tag_id = t.id')
->join('INNER', $db->quoteName('#__ucm_content', 'c') . ' ON m.type_alias = c.core_type_alias AND m.core_content_id = c.core_content_id');
$db->setQuery($query);
//echo $query->__toString();
//exit;
try
{
$results = $db->loadObjectList();
}
catch (RuntimeException $e)
{
$results = array();
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
$filtered_array = Array();
$tmp_array = Array();
$categories_filter = $params->get('categories_filter');
// filtered by selected tags and categories
foreach ($results as $result)
{
if (empty($selected_tags) || in_array($result->tag_id, $selected_tags))
{
$type_alias = explode("." , $result->type_alias);
$cat_alias = $type_alias[0].'.'.$result->catid;
if (empty($categories_filter) || in_array($cat_alias, $categories_filter))
{
if (array_key_exists($result->title, $tmp_array))
{
$tmp_array[$result->title]->count ++;
}
else
{
$tmp_array[$result->title] = new stdClass();
$tmp_array[$result->title]->count = 1;
$tmp_array[$result->title]->id = $result->tag_id;
$tmp_array[$result->title]->alias = $result->alias;
}
}
}
}
$i = 0;
$params->get('random_tags') ? self::shuffle_assoc($tmp_array) : arsort($tmp_array);
foreach ($tmp_array as $k => $v)
{
$filtered_array[$i] = new stdClass();
$filtered_array[$i]->tag = $k;
$filtered_array[$i]->count = $v->count;
$filtered_array[$i]->id = $v->id;
$filtered_array[$i]->alias = $v->alias;
$i++;
if ($i == $max_tags)
{
break;
}
}
unset($tmp_array);
$sorted_results = self::tag_sort($filtered_array, $params->get('order'));
foreach ($sorted_results as $a)
{
$tags->count[$a->tag] = $a->count;
$tags->id[$a->tag] = $a->id;
$tags->alias[$a->tag] = $a->alias;
}
$tags->no_tag_found = empty($tags->count) ? true : false;
$min_count = $tags->no_tag_found ? 0 : min($tags->count);
$max_count = $tags->no_tag_found ? 0 : max($tags->count);
$spread = $max_count - $min_count;
if ($spread <= 0)
$spread = 1;
$font_spread = $params->get('max_font_size') - $params->get('min_font_size');
if ($font_spread < 0)
$font_spread = 1;
$font_step = $font_spread / $spread;
foreach($sorted_results as $result)
{
$tags->size[$result->tag] = (int)($params->get('min_font_size') + (($result->count - $min_count) * $font_step));
$tags->color[$result->tag] = $params->get('colorful') ? "color:#".dechex(rand(0,16777215)) : "";
}
$tags->align = $params->get('align').';';
$tags->target = $params->get('target') ? '_blank' : '_self';
$tags->colorful = $params->get('colorful');
$tags->display_count = $params->get('display_count');
// enable google fonts
if ($params->get('google_font') != '')
{
$family = JString::str_ireplace(' ', '+', JString::trim($params->get('google_font')));
$subset = $params->get('script_subset') == '' ? '' : '&subset='.$params->get('script_subset');
$effect = $params->get('google_font_effect') == '' ? '' : '&effect='.JString::substr($params->get('google_font_effect'), 12);
$document->addStyleSheet('http://fonts.googleapis.com/css?family='.$family.$subset.$effect);
$google_font = $params->get('google_font');
$tags->google_font_effect = $params->get('google_font_effect') == '' ? '' : 'class="'.$params->get('google_font_effect').'"';
}
else
{
$google_font = $tags->google_font_effect = "";
}
// build css codes in head
$css['']['width'] = $params->get('width');
$css['']['height'] = $params->get('height');
$css['']['text-align'] = $params->get('align');
$css['']['line-height'] = $params->get('line_height');
$css['a']['margin-left'] = $params->get('horizontal_space');
$css['a']['margin-right'] = $params->get('horizontal_space');
$css['a']['padding'] = $params->get('padding');
$css['a']['-webkit-border-radius'] = $params->get('border_radius');
$css['a']['-moz-border-radius'] = $params->get('border_radius');
$css['a']['border-radius'] = $params->get('border_radius');
$css['a']['font-family'] = $google_font;
$css['a:link']['text-decoration'] = $params->get('underline') ? 'underline' : 'none';
$css['a:link']['color'] = $params->get('color');
$css['a:link']['background-color'] = $params->get('bg_color');
$css['a:link']['font-weight'] = $params->get('bold') ? 'bold' : 'normal';
$css['a:visited']['text-decoration'] = $params->get('underline');
$css['a:visited']['color'] = $params->get('color');
$css['a:visited']['background-color'] = $params->get('bg_color');
$css['a:visited']['font-weight'] = $params->get('bold');
$css['a:hover']['text-decoration'] = $params->get('hover_underline') ? 'underline' : 'none';
$css['a:hover']['color'] = $params->get('hover_color');
$css['a:hover']['background-color'] = $params->get('hover_bg_color');
self::css_builder($css, $moduleid);
return $tags;
}
public function tag_sort($result, $type)
{
$sort_array = Array();
$sort_array2 = Array();
$sort_array3 = Array();
$merge_array = Array();
if($type == 1)
{
return $result; //no need to sort
}
else
{
foreach($result as $r)
{
$sort_array[] = $r->tag;
$sort_array2[] = $r->tag;
$sort_array3[] = $r->count;
}
}
if($type == 0)
{
//sort by tag name
natcasesort($sort_array);
foreach($sort_array as $i => $a)
{
$merge_array[] = $result[$i];
}
}
elseif($type == 2)
{
//sort by most tagged first
natcasesort($sort_array3);
foreach($sort_array3 as $i => $a)
{
$merge_array[] = $result[$i];
}
}
elseif($type == 3)
{
//sort randomly
shuffle($sort_array2);
foreach($sort_array2 as $a)
{
$key = array_keys($sort_array, $a);
$merge_array[] = $result[$key[0]];
}
}
return $merge_array;
}
public function css_builder($blocks, $moduleid)
{
$document = JFactory::getDocument();
$css = '';
foreach ($blocks as $block_k => $block_v)
{
$css .= '#qitagcloud_'.$moduleid.' '.$block_k.'{';
foreach ($block_v as $attr_k => $attr_v)
{
if ($attr_v == '') continue;
$css .= $attr_k.':'.$attr_v.';';
}
$css .= '}';
}
$document->addStyleDeclaration($css);
}
public function shuffle_assoc(&$array)
{
$keys = array_keys($array);
shuffle($keys);
$randomized_array = Array();
foreach ($keys as $key)
{
$randomized_array[$key] = $array[$key];
}
$array = Array();
foreach ($randomized_array as $k => $v)
{
$array[$k] = $v;
}
}
}
?>