-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvk_board_comments.module
379 lines (334 loc) · 11.2 KB
/
vk_board_comments.module
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Created by PhpStorm.
* User: Lex
* Date: 04.03.2017
* Time: 20:58
*/
class VKapi {
private $access_token = NULL;
private $protected_key;
private $service_key; // send as access_token
private $client_id;
const API_URL = 'https://api.vk.com/method/';
const API_V = '5.62';
private $last_request = 0;
private $count_last_request = 0;
public function __construct($protected_key = NULL, $service_key = NULL) {
$this->protected_key = variable_get('vk_board_protected_key', '');
$this->service_key = variable_get('vk_board_service_key', '');
$this->client_id = variable_get('vk_board_client_id', '');
$this->getServiceKeyOld();
}
public function getParams() {
return array(
'protected_key' => $this->protected_key,
'service_key' => $this->service_key,
'client_id' => $this->client_id,
'access_token' => $this->access_token,
);
}
private function getServiceKeyOld() {
$url = 'https://oauth.vk.com/access_token';
$params = array(
'client_id' => $this->client_id,
'client_secret' => $this->protected_key,
'grant_type' => 'client_credentials',
'v' => $this::API_V,
);
$url = url($url, array('query'=>$params));
$request = drupal_http_request($url,array());
if($request->code == 200) {
$data = drupal_json_decode($request->data);
$this->access_token = $data['access_token'];
}elseif(isset($request->error)) {
drupal_set_message($request->code . ': ' .$request->error, 'error');
$this->access_token = $this->service_key;
}
}
public function getRequest($method, $params = array(), $is_token = TRUE) {
$params['v'] = $this::API_V;
if($is_token) {
$params['access_token'] = $this->access_token;
}
$url = url($this::API_URL . $method, array('query'=>$params));
$request = drupal_http_request($url,array());
if($request->code == 200) {
return drupal_json_decode($request->data);
}elseif(isset($request->error)) {
drupal_set_message($request->code . ': ' .$request->error, 'error');
return FALSE;
}
}
// likes.getList
public function likesGetList($params = array(), $is_token = TRUE ) {
return $this->getRequest('likes.getList', $params, $is_token);
}
// users.get
public function usersGet($params = array(), $is_token = TRUE ) {
return $this->getRequest('users.get', $params, $is_token);
}
public function usersGetFull($user_ids, $is_token = TRUE ) {
if(is_array($user_ids)) {
$ids = implode(',', $user_ids);
}else {
$ids = $user_ids;
}
$params = array(
'user_ids' => $ids,
'fields' => 'bdate, city, country, photo_200',
);
// $request = $this->getRequest('users.get', $params, $is_token);
return $this->getRequest('users.get', $params, $is_token);
}
// board.getComments
/**
* @param array $params
* - group_id
* - topic_id
* - count
* - extended
* - start_comment_id
* - sort asc | desc
* - offset
* @param bool $is_token
* @return bool|mixed
*/
public function boardGetComments(array $params, $is_token = TRUE) {
return $this->getRequest('board.getComments', $params, $is_token);
}
}
/**
* Implements hook_permission().
*
*/
function vk_board_comments_permission() {
return array(
'access vk board comments page' => array(
'title' => t('Access vk board comments page'),
'description' => t('Allow users to access comments page'),
),
'access config vk board comments' => array(
'title' => t('Access config vk board comments'),
'description' => t('Access config vk board comments'),
),
);
}
/**
* Implements hook_menu().
*/
function vk_board_comments_menu() {
$items['vk-reviews'] = array(
'title' => variable_get('vk_board_page_title', 'Reviews'),
'page callback' => 'vk_board_comments_page',
'access arguments' => array('access vk board comments page'),
'access callback' => TRUE,
'expanded' => TRUE,
);
$items['vk-reviews/more/%'] = array(
'title' => 'Reviews',
'page callback' => 'vk_board_comments_callback',
'delivery callback' => 'ajax_deliver',
'page arguments' => array(2),
'access arguments' => array('access vk board comments page'),
'access callback' => TRUE,
'expanded' => TRUE,
);
//http://drupal7120.cyberlex404.com/vk-reviews#overlay=admin/config/services/rss-publishing
/*
$items['admin/config/services'] = array(
'title' => 'New configuration section',
'position' => 'left',
'weight' => -100,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
*/
$items['admin/config/services/vk-board-comments'] = array(
'title' => 'VK board comments',
'description' => 'VK board comments',
'page callback' => 'drupal_get_form',
'page arguments' => array('vk_board_comments_config_form'),
'access arguments' => array('administer site configuration'),
'file' => 'vk_board_comments.admin.inc',
);
return $items;
}
/**
* Menu callback.
*/
function vk_board_comments_callback($start_cid, $mode = NULL) {
// Если у посетителя отключён javascript, то показываем ему сообщение
if ($mode != 'ajax') {
drupal_set_message('Enable Javascript');
drupal_goto(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '<front>');
}
$commentsData = _vk_board_get_comments_data(0, $start_cid);
$newLastId = $commentsData['last_id'];
//$commands[] = ajax_command_replace('.vk-board-more-load', $test_html);
$commands[] = ajax_command_replace('.vk-board-more-load', $commentsData['render']);
//$commands[] = ajax_command_after('.vk-bc-comment-list div:last-child', '<h2>Some subtitle</h2>');
//$commands[] = ajax_command_html('.reviews-list', _vk_board_comments_render(0, $start_cid));
$commands[] = ajax_command_html('.vk-bc-comment-more', l('more', 'vk-reviews/more/' . $newLastId . '/nojs', array(
'attributes' => array(
'class' => array('use-ajax'),
'title' => 'More comments',
)
)));
// обновляем содержимое блока
//$commands[] = ajax_command_html('#last-comments', _last_comments_block_content($from_cid));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function vk_board_comments_page() {
drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'jquery.form');
$commentsData = _vk_board_get_comments_data();
// dpm($commentsData);
$lastComment = 699;
$link = l('more', 'vk-reviews/more/' . $lastComment . '/nojs', array(
'attributes' => array(
'class' => array('use-ajax'),
'title' => 'More comments',
)
));
$content = '<div class="vk-board-comments-wrapper"><div class="vk-bc-comment-list">' . $commentsData['render'] . '</div><div class="vk-bc-comment-more">' /*. $link */. '</div></div>';
return $content;
}
function _vk_board_get_comments_data($offset = 0, $last_cid = NULL) {
$data = _vk_board_get_comments($offset, $last_cid);
// dpm($data, 'data');
$return = array(
'last_id' => NULL,
'render' => NULL,
'data' => $data,
'comments' => NULL,
);
$comments = array();
$authors = array();
$items = $data['response']['items'];
$profiles = $data['response']['profiles'];
foreach ($profiles as $profile) {
$authors[$profile['id']] = $profile;
}
foreach ($data['response']['groups'] as $group) {
$authors['-' . $group['id']] = $group;
}
foreach ($items as $item) {
$comment = $item;
$comment['author'] = $authors[$item['from_id']];
$comments[$item['id']] = $comment;
}
if(!is_null($last_cid)) {
unset($comments[$last_cid]);
}
$return['comments'] = $comments;
$return['authors'] = $authors;
$render_items = array();
foreach ($comments as $item) {
$render = array(
'#theme' => 'vk_comment',
'#body' => _vk_board_format_text($item['text']),
'#uid' => $item['author']['id'],
'#time' => format_date($item['date'], 'custom', 'H:i d.m.Y'),
'#fullname' => '',
'#cid' => $item['id'],
'#comment_link' => _vk_board_get_comment_link($item),
'#item' => $item,
'#author_link' => _vk_board_get_author_link($item),
);
if (isset($item['author']['first_name']) && isset($item['author']['last_name'])) {
$render['#fullname'] = $item['author']['first_name'] . ' ' . $item['author']['last_name'];
}elseif(isset($item['author']['name'])) {
$render['#fullname'] = $item['author']['name'];
}
if(isset($item['author']['photo_200'])) {
$render['#avasrc'] = $item['author']['photo_200'];
}elseif(isset($item['author']['photo_100'])) {
$render['#avasrc'] = $item['author']['photo_100'];
}
$render_items[$item['id']] = render($render);
}
$variables = array(
'rows' => $render_items,
);
$lastComment = end($comments);
$return['last_id'] = $lastComment['id'];
$output = theme('vk_comment_list', $variables);
$return['render'] = $output . '<div class="vk-board-more-load"></div>';
return $return;
}
function _vk_board_get_comment_link($comment){
$group_id = variable_get('vk_board_group_id', NULL);
$topic_id = variable_get('vk_board_topic_id', NULL);
return 'https://vk.com/topic-' . $group_id . '_' . $topic_id .'?post=' . $comment['id'];
}
function _vk_board_get_author_link($comment){
$link = 'https://vk.com/';
if(isset($comment['author']['type']) && $comment['author']['type'] == 'group') {
if(isset($comment['author']['screen_name'])) {
$link .= $comment['author']['screen_name'];
}
}else{
$link .= 'id' . $comment['author']['id'];
}
return $link;
}
function _vk_board_get_comments($offset = 0, $last_cid = NULL) {
$vkapi = new VKapi();
$count = variable_get('vk_board_comment_count', 100);
$offset = ($offset * $count) + 0;
$params = array(
'group_id' => variable_get('vk_board_group_id', ''),
'topic_id' => variable_get('vk_board_topic_id', ''),
'count' => $count,
'start_comment_id' => $last_cid,
'extended' => 1,
'sort' => 'desc',
'offset' => $offset,
);
return $vkapi->boardGetComments($params, FALSE);
}
function _vk_board_format_text($text) {
$text = check_markup($text, 'full_html');
//dpm($text);
$patterns = array();
$patterns[0] = '/\[id\d+\:bp\-\d+\_\d+\|/';
$patterns[1] = '/\]/';
$replacements = array();
$replacements[0] = '';
$replacements[1] = '';
$text = preg_replace($patterns, $replacements, $text);
return $text;
}
/**
* Implements hook_theme().
*/
function vk_board_comments_theme() {
return array(
'vk_comment' => array( // название хука темизации
'variables' => array( // переменные которые будут доступны в шаблоне
'fullname' => NULL,
'body' => NULL,
'time' => NULL,
'avasrc' => NULL,
'cid' => NULL,
'comment_link' => NULL,
'item' => NULL,
'uid' => NULL,
'author_link' => NULL,
),
'template' => 'vk-comment',
),
'vk_comment_list' => array(
'variables' => array(
'rows' => NULL,
),
'template' => 'vk-comment-list',
),
);
}