Repository navigation
Expand file tree
/
Copy pathcomments.ext.php
More file actions
354 lines (274 loc) · 9.63 KB
/
Copy pathcomments.ext.php
File metadata and controls
354 lines (274 loc) · 9.63 KB
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
<?php
class EXT_Comments extends SYS_Model_Database
{
//--------------------------------------------------------------------------
public $per_page = 50; // Кол-во комментариев на страницу
public $tree_mode = TRUE; // Включить древовидный режим. (кол-во уровней+1)
public $mailing_author = TRUE; // Оповещать автора коммента об ответе
public $captcha_protection = TRUE; // Защита от спама каптчей
public $code_protection = FALSE; // Защита от спама скрытым кодом
public $js_validation = FALSE; // Проверка формы до отправления
public $allow_guest = FALSE; // Гости могут добавлять комментарий
public $auto_registration = FALSE; // Автоматически регистрировать пользователей оставивших комментарий
public $post_delay = 180; // 3 min
// public $allow_reply = TRUE; // Разрешить отвечать на комментарии (древовидная стр-ра)
public $mailing = array();
//--------------------------------------------------------------------------
public $table = 'comments';
public $type = '';
public $rel_id = 0;
public $page = 1;
public $total_pages = 1;
//--------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
sys::set_config_items($this, 'comments');
}
//--------------------------------------------------------------------------
public function init()
{
$this->fields['comments'] = array(
'id' => array(),
'pid' => array(),
'uid' => array(
'default' => $this->user->id
),
'type' => array(
'default' => $this->type
),
'rel_id' => array(
'default' => $this->rel_id
),
'status' => array(
'label' => 'Статус',
'default' => 1,
'field' => 'select',
'options' => 'status_list',
'user_group' => array(1)
),
'message' => array(
'label' => 'Сообщение',
'field' => 'textarea',
'rules' => $this->user->group_id == 1 ? 'trim' : 'trim|strip_tags|required|min_length[2]',
),
'postdate' => array(
'label' => 'Дата публикации',
'default' => time(),
),
'ip' => array(
'default' => isset($_SERVER['REMOTE_ADDR']) ? ip2long($_SERVER['REMOTE_ADDR']) : 0
),
/*'username' => array(
'label' => 'Имя (для гостя)',
'field' => 'input',
'rules' => 'trim|strip_tags|length[3,25]',
),
'useremail' => array(
'label' => 'E-mail (для гостя)',
'field' => 'input',
'rules' => 'trim|valid_email|max_length[100]',
),*/
'ratio' => NULL
);
}
//--------------------------------------------------------------------------
public function get_data()
{
$result = new stdClass;
$result->parents = array();
$result->childs = array();
$count_parents = $this->get_count(NULL,NULL,$this->tree_mode?'pid=0':'');
if ($count_parents > $this->per_page)
{
$this->total_pages = floor($count_parents / $this->per_page);
$this->db->limit( $this->page*$this->per_page, ($this->page-1)*$this->per_page );
}
// get parents
$this->db->where('pid=0');
$result->parents = $this->get_result();
if ($this->tree_mode && $result->parents)
{
foreach ($result->parents as $row)
{
$min_date = isset($min_date) ? min($min_date, $row->postdate) : $row->postdate;
// $max_date = isset($max_date) ? max($max_date, $row->postdate) : $row->postdate;
}
$this->db->where('pid>0 AND postdate > ' . $min_date)->order_by('postdate');
$childs = $this->get_result();
foreach ($childs as $i=>$row)
{
$result->childs[$row->pid][$row->id] =& $childs[$i];
}
}
return $result;
}
//--------------------------------------------------------------------------
public function check_permissions()
{
if ($this->user->id) return TRUE;
if ($this->allow_guest) return TRUE;
$this->template->error('К сожалению Вы не можете добавить комментарий! :-(');
return FALSE;
}
//--------------------------------------------------------------------------
public function init_form()
{
parent::init_form();
if ( ! $this->user->id)
{
if ($this->captcha_protection_enable())
{
$this->load->extension('captcha')->init();
}
if ($this->code_protection_enable())
{
$this->load->extension('code_protection')->init();
}
}
if ($this->js_validation)
{
$this->load->extension('js_validation')->init_form(&$this->form);
}
if ($this->user->group_id == 1) return;
// post_delay blocking
if ($this->post_delay)
{
$time = time() - $this->post_delay;
if ($this->user->id)
{
$this->db->where('uid=?', $this->user->id);
}
else
{
$this->db->where('ip=?', ip2long($_SERVER['REMOTE_ADDR']));
}
$this->db->select('postdate')->where('postdate>?', $time);
$row = $this->db->get($this->table())->row();
if ($row)
{
$time = h_date::time_elapsed_string($time, $row->postdate);
$this->form->set_error('', "Оставить новый комментарий вы сможете только через {$time}");
}
}
}
//--------------------------------------------------------------------------
public function code_protection_enable()
{
if ($this->user->group_id) return FALSE;
if ( ! $this->code_protection) return FALSE;
return TRUE;
}
//--------------------------------------------------------------------------
public function captcha_protection_enable()
{
if ($this->user->group_id) return FALSE;
if ( ! $this->captcha_protection) return FALSE;
return TRUE;
}
//--------------------------------------------------------------------------
public function prepare_row_result(&$row)
{
$row = parent::prepare_row_result($row);
$row->status_name = $this->status_list($row->status);
$row->message = nl2br($row->message);
if (isset($row->group_id)) $this->user->model->prepare_row_result(&$row, $row->uid);
return $row;
}
//--------------------------------------------------------------------------
public function status_list($val = NULL)
{
static $list = array(
0 => 'Отключен',
1 => 'Включен'
);
if ($val !== NULL) return $list[$val];
return $list;
}
//--------------------------------------------------------------------------
public function get()
{
$this->db->order_by('comments.postdate DESC, comments.id DESC');
if ($this->user->group_id != 1)
{
$this->db->where('comments.status=1');
//$this->db->where('users.status=1');
}
if ($this->type) $this->db->where('comments.type = ?' , $this->type);
if ($this->rel_id) $this->db->where('comments.rel_id = ?', $this->rel_id);
$this->db->select('users.*, comments.*');
$this->db->join('users', 'users.id = comments.uid', 'left');
// $this->db->join('comments', 'comments.rel_id = topics.id');
return parent::get();
}
//--------------------------------------------------------------------------
public function insert()
{
$_POST['type'] = $this->type;
$_POST['rel_id'] = $this->rel_id;
$insert_id = parent::insert();
if ($insert_id)
{
$this->mail_process($insert_id);
}
return $insert_id;
}
//--------------------------------------------------------------------------
public function delete($table=NULL,$where=NULL)
{
$res = $this->db->select('comments.id, uid')->where($where)->get('comments')->result();
$com_id = array();
foreach ($res as $row)
{
$com_id[] = $row->id;
}
return parent::delete($table, 'comments.id IN (' . implode(',', $com_id) . ')');
}
//--------------------------------------------------------------------------
public function get_count($type = NULL, $rel_id = NULL, $where = NULL)
{
static $result = array();
if ($type === NULL) $type = $this->type;
if ($rel_id === NULL) $rel_id = $this->rel_id;
$key = $type . $rel_id . $where;
if (isset($result[$key])) return $result[$key];
if ($where) $this->db->where($where);
$result[$key] = $this->db->select('COUNT(*) AS x')
->where('type = ? AND rel_id = ?', $type, $rel_id)
->get($this->table)->row()->x;
return $result[$key];
}
//--------------------------------------------------------------------------
public function mail_process($comment_id)
{
if (empty($this->mailing[$this->type]) || ! $this->rel_id) return;
$mailing = $this->mailing[$this->type];
$template = $mailing['template'];
$model_name = ! empty($mailing['model']) ? $mailing['model'] : $this->type;
$this->db->where('comments.id=?', $comment_id);
$comment = $this->get_row();
// GET DATA
$model =& $this->load->model($model_name);
$this->db->where($model->table . '.id=?', $this->rel_id);
$data = $model->get_row();
if ( ! $data || empty($data->uid) || $data->uid == sys::$ext->user->id)
{
return;
}
$this->load->library('Mail');
$full_link = $this->mail->base_url . $this->uri->uri_string . '/#comment_' . $comment_id;
// Mail data
$email_data['full_link'] =& $full_link;
$email_data['data'] =& $data;
$email_data['comment'] =& $comment;
$email_data['comment_id'] =& $comment_id;
$email_data['mailing'] =& $mailing;
// SEND
// $this->mail->debug = TRUE;
// $this->mail->debug_email = FALSE;
// echo "<hr>";
$this->mail->send_to_user($data->uid, $template, $email_data);
// die('<hr>mail_process');
}
//--------------------------------------------------------------------------
}