-
Notifications
You must be signed in to change notification settings - Fork 0
/
countsimilar.php
241 lines (187 loc) · 6.13 KB
/
countsimilar.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
<?php
/**
* @package Kunena.Plugin
* @subpackage countsimilar
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @author André Luiz Pereira <[<[email protected]>]>
*/
defined('_JEXEC') or die ();
class plgKunenaCountsimilar extends JPlugin
{
public function __construct(&$subject, $config)
{
// Do not load if Kunena version is not supported or Kunena is offline
if (!(class_exists('KunenaForum') && KunenaForum::isCompatible('4.0') && KunenaForum::installed()))
{
return;
}
parent::__construct($subject, $config);
$this->loadLanguage('plg_kunena_countsimilar.sys', JPATH_ADMINISTRATOR) || $this->loadLanguage('plg_kunena_countsimilar.sys', KPATH_ADMIN);
}
/**
* detect and dispach event button
* @param string $scenario scenario type
* @param object $bts object buttons
* @param object $obj get objects defaults the class
* @return vold create new object buttom
*/
public function onKunenaGetButtons($scenario, $bts, $obj) //'topic.action', $this->topicButtons, $this
{
//$bts->def('testebt', $obj->getButton('#', 'indented', 'layout', 'user'));
if($scenario == 'topic.action'){
$countsames = $this->countSames($obj->topic->id);
$count = 0;
$list = array();
if($countsames){
$count = $countsames['count'];
$list = $countsames['list'];
}
//detect user
if($obj->me->userid){
$userinlist = $this->checkUser($obj->me->userid, $list);
if($userinlist){
$bts->def('mesmoproblema', '<a class="kicon-button kbuttonuser btn-left" href="#"><span class="layout-mesmoproblema">'.JText::_('PLG_KUNENA_COUNTSIMILAR_ERRORUM').'</span></a>');
}else{
$bts->def('mesmoproblema', '<a data-kunemacheck="'.$obj->topic->id.'" class="kicon-button kbuttonuser btn-left" href="#"><span class="layout-mesmoproblema">'.JText::_('PLG_KUNENA_COUNTSIMILAR_MESMOPROBLEMA').'</span></a>');
}
}
$bts->def('countmesmoproblema', '<small class="similar-smallinfo"><span class="countnum">'.$count.'</span>'.JText::_('PLG_KUNENA_COUNTSIMILAR_COUNTMESMOPROBLEMA').'</small>');
}
}
/**
* Check user and count
* @param int $id topic id
* @return mixer return list and count or int 0 if not result
*/
private function countSames($id){
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('userid'))
->from($db->quoteName('#__kunena_sameproblem'))
->where('topic_id = '.(int)$id);
$db->setQuery($query);
$db->execute();
$num_rows = $db->getNumRows();
$list = $db->loadObjectList();
return ($num_rows)?array('count'=>$num_rows, 'list'=>$list):0;
}
/**
* @param int $user_id get user
* @param array $list get list error
* @return bollean return if user exist in list
*/
private function checkUser($user_id, $list){
$return = false;
foreach ($list as $r => $user) {
if($user->userid == $user_id){
$return = true;
}
}
return $return;
}
/**
* simulate ajax click
* @return string return ajax notification
*/
public function onKunenaBeforeRender()
{
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
if($app->isAdmin()) {
return;
}
$idPost = $app->input->post->get('inserir',0,'INT');
$option = $app->input->get('option',0,'STRING');
$view = $app->input->get('view',0,'STRING');
if($option == 'com_kunena' && $view == 'topic'){
$user = JFactory::getUser();
if (!$user->guest) {
$script = <<<HTML
jQuery(function($){
$('[data-kunemacheck]').one('click',function(e){
e.preventDefault();
var obj = $(this);
var kcheck = obj.data('kunemacheck');
var kurl =$(location).attr('href');
obj.find('span.layout-mesmoproblema').text('...');
$.post( kurl, {inserir:kcheck},function(resultados, status){
if(resultados.length < 150){
obj.find('span.layout-mesmoproblema').text(resultados);
}else{
obj.text("error");
}
});
});
});
HTML;
$doc->addScriptDeclaration($script);
} //check se está logado
}
if($idPost){
$mensagens = 'fazio';
$listsames = $this->countSames($idPost);
$user = JFactory::getUser();
if (!$user->guest) {
$userid = $user->id;
$mensagens = $this->aplicarBD($userid, $listsames['list'], $idPost);
}else{
$mensagens = JText::_('PLG_KUNENA_COUNTSIMILAR_ERRORTRES');
}
//$doc->setMimeEncoding('application/json');
// JResponse::setHeader('Content-Disposition','attachment;filename="progress-report-results.json"');
// echo json_encode($mensagens);
echo $mensagens;
$app->close();
}
}
/**
* set database clicks
* @param int $userid id do usuário
* @param mixer $list lista de usuários que marcou, ou false
* @param int $idPost id do topco
* @return string Retorna o texto da ação
*/
private function aplicarBD($userid , $list, $idPost){
$mensagem = '';
$executeDB = 0;
//existe uma lista neste post
if($list){
$userinlist = $this->checkUser($userid, $list);
if($userinlist){
$mensagem = JText::_('PLG_KUNENA_COUNTSIMILAR_ERRORUM');
}else{
$executeDB = 1;
}
}else{
$executeDB = 1;
}
// o usuário não está na lista adiciona o usuário
if($executeDB){
//insere na tabela __kunena_sameproblem
$mensagem = $this->insertTable($idPost, $userid);
}
return $mensagem;
}
/**
* create new click
* @param int $idPost Id do topico
* @param int $userid Id do usuário
* @return [string] Retorna que foi salvo com sucesso
*/
private function insertTable($idPost, $userid){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$columns = array('topic_id', 'userid');
$values = array((int)$idPost, (int)$userid);
$query
->insert($db->quoteName('#__kunena_sameproblem'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->execute();
//retorna se a execução foi um sucesso
return JText::_('PLG_KUNENA_COUNTSIMILAR_SUCCESS');
}
}