forum.class.php
15.3 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
class Forum{
var $db = null;
var $tpl = null;
var $error = null;
var $tree = null;
function Forum(&$db,&$tpl,&$error){
$this->db = &$db;
$this->tpl = &$tpl;
$this->error = &$error;
$this->tree = &new CDBTree($this->db,"tree","id");
}
function trim(&$data){
foreach($data as $key=>$value){
$data[$key] = strip_tags(trim($value));
}
}
function valid($data,$upload = null,$user_id = false){
if(isset($data['register_login'])){
if( !preg_match("/^.{1,30}$/i",$data['register_login']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ëîãèí, îò 1 äî 30 ñèìâîëîâ.";
elseif($this->isLogin($data['register_login'],$user_id))$this->error[] = "Îøèáêà ââîäà, òàêîé Ëîãèí óæå åñòü!";
}
if(isset($data['register_psw'])){
if( !preg_match("/^.{1,30}$/i",$data['register_psw']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ïàðîëü, îò 1 äî 30 ñèìâîëîâ.";
}
if(isset($data['register_email'])){
if( !preg_match("/^([^@]+)+@([^@]+)\.([^@]+)$/i",$data['register_email']) || strlen($data['register_email'])>50 ) $this->error[] = "Îøèáêà ââîäà ïîëÿ E-mail, äî 50 ñèìâîëîâ.";
}
if(isset($data['register_name'])){
if( !preg_match("/^.{1,30}$/i",$data['register_name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èìÿ, îò 1 äî 30 ñèìâîëîâ.";
}
if(isset($data['title'])){
if( !preg_match("/^.{1,250}$/i",$data['title']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Íàçâàíèå òåìû, îò 1 äî 250 ñèìâîëîâ.";
}
if(isset($data['about'])){
if( !preg_match("/.+/is",$data['about']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Îïèñàíèå.";
}
if(isset($upload['pic']['name']) && $upload['pic']['name']!=null){
$type = substr(strrchr($upload['pic']['name'],"."),1);
if( !preg_match("/^(jpeg|jpg|gif|png)$/i",$type) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èçîáðàæåíèÿ, òîëüêî jpg,png,gif.";
}
if(isset($data['login'])){
if( !preg_match("/.{1,50}/i",$data['login']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èìÿ.";
elseif($this->isLogin($data['login'],$user_id))$this->error[] = "Îøèáêà ââîäà, òàêîé Ëîãèí óæå åñòü!";
}
if(isset($data['about_comment'])){
if( !preg_match("/.+/is",$data['about_comment']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Êîììåíòàðèè.";
}
if(isset($data['treeID'])){
if( !preg_match("/^([0-9]+)$/",$data['treeID']) && $data['treeID']>0 ) $this->error[] = "Îøèáêà íîìåðà âåòêè!!!.";
}
if(isset($data['topicID'])){
if( !preg_match("/^([0-9]+)$/",$data['topicID']) && $data['topicID']>0 ) $this->error[] = "Îøèáêà íîìåðà òåìû ôîðóìà!!!.";
}
return ( count($this->error) ) ? true : false;
}
function isLogin($login,$user_id){
$sql = "select count(*) from forum_user where login=?";
if($user_id>0)$sql .= "and id<>'$user_id'";
return $this->db->getOne($sql,array($login));
}
function isUser($login,$psw){
$sql = "select id from forum_user where login=? and psw=?";
$user_id = $this->db->getOne($sql,array($login,$psw));
return $user_id;
}
function viewUser($id){
$sql = "select * from forum_user where id=?";
$row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
$this->tpl->assign("user",$row);
}
function saveUser($data,$upload,$user_id = false){
$table_name = "forum_user";
$DB_AUTOQUERY = ($user_id>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
$where = ($user_id>0) ? "id='$user_id'" : '';
$fields_values = array('login' => $data['register_login'],'psw' => $data['register_psw'],'email' => $data['register_email'],'name' => $data['register_name'],'about' => $data['register_about']);
if($upload['pic']['tmp_name']){
if($user_id>0){$this->deleteUserPic($user_id);}
$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"90",'height'=>"90",'upload_path'=>"./uploaded/pic/forum/user/"));
}
$this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,$where);
}
function deleteUserPic($id){
$sql = "select pic from forum_user where id=?";
$pic = $this->db->getOne($sql,array($id));
@unlink("./uploaded/pic/forum/user/$pic");
}
function delete($id){
$sql = "select tree_id,pic from topic where id=?";
$topic_row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
if($topic_row['pic']){@unlink("./pic/forum/{$topic_row['pic']}");}
$this->tree->deleteAll($topic_row['tree_id']);
$sql = "delete from topic where id=?";
$this->db->query($sql,array($id));
}
function deleteParentElemant($parent_table,$parent_id){
$sql = "select id,tree_id,pic from topic where parent_table=? and parent_table_id=?";
$topic_row = $this->db->getRow($sql,array($parent_table,$parent_id),DB_FETCHMODE_ASSOC);
if($topic_row['pic']){@unlink("./pic/forum/{$topic_row['pic']}");}
$this->tree->deleteAll($topic_row['tree_id']);
$sql = "delete from topic where id=?";
$this->db->query($sql,array($topic_row['id']));
}
function deleteComment($id){
$sql1 = "select topic_id,pic from tree where id=? limit 1";
$row = $this->db->getRow($sql1,array($id),DB_FETCHMODE_ASSOC);
$topic_id = $row['topic_id'];
if($row['pic']){@unlink("./uploaded/pic/forum/{$row['pic']}");}
$this->tree->delete($id);
$sql = "select id,mktime from tree where topic_id=? And _level>1 order by mktime desc limit 1";
$last = $this->db->getRow($sql,array($topic_id),DB_FETCHMODE_ASSOC);
$sql2 = "update topic set comments=comments-1,comment_last_mktime=?,comment_last=? where id=?";
$this->db->query($sql2,array($last['mktime'],$last['id'],$topic_id));
}
function updateTopic($data,$upload = null){
$table_name = "topic";
$fields_values = array("title"=>$data['data']['title'],"about"=>$data['data']['about'],"meta_title"=>$data['data']['meta_title'],"meta_description"=>$data['data']['meta_description'],"meta_keywords"=>$data['data']['meta_keywords'],"meta_about"=>$data['data']['meta_about']);
$this->db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_UPDATE,"id='".$data['update_id']."'");
}
function SaveTopic($data,$upload = null,$user_id = 0){
$table_name = "topic";
$topic_next_id = $this->db->nextId('mySequenceTopic');
//Ñîçäàåì íà÷àëüíóþ âåòêó...
$tree_parent_id = 1;
$tree_next_id = $this->db->nextId('mySequenceTree');
$arrayData = array("id"=>$tree_next_id,"parent_id"=>$tree_parent_id,"topic_id"=>$topic_next_id,"user_id"=>$user_id,"mktime"=>mktime());
//print_r($arrayData);
$this->tree->insert($tree_parent_id,$arrayData);
//Êîíåö...
//Ñîçäàåì òåìó ôîðóìà...
switch($data['parent_table']){
case "rooms" :
$fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
"parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
"title"=>$data['data']['company'],"about"=>$data['data']['about'],
"mktime"=>mktime());
if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"120",'height'=>"120",'upload_path'=>"./uploaded/pic/forum/topic/"));}
break;
case "product" :
$fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
"parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
"title"=>$data['data']['name'],"about"=>$data['data']['about'],
"mktime"=>mktime());
if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"120",'height'=>"120",'upload_path'=>"./uploaded/pic/forum/topic/"));}
break;
case "articles" :
$fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
"parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
"title"=>$data['data']['title'],"about"=>$data['data']['text'],
"mktime"=>mktime());
if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"200",'height'=>"200",'upload_path'=>"./uploaded/pic/forum/topic/"));}
break;
case "news" :
$fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
"parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
"title"=>$data['data']['title'],"about"=>$data['data']['text'],
"mktime"=>mktime());
if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"200",'height'=>"200",'upload_path'=>"./uploaded/pic/forum/topic/"));}
break;
case "questioning" :
$fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
"parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
"title"=>$data['data']['name'],
"mktime"=>mktime());
break;
default : //TOPIC
$fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
"parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
"title"=>$data['data']['title'],"about"=>$data['data']['about'],
"mktime"=>mktime(),"comment_last_mktime"=>mktime());
// if($upload = null && $upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"120",'height'=>"120",'upload_path'=>"./pic/forum/"));}
break;
}
//print_r($fields_values);exit;
$this->db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT);
}
function reload(){
print'
<script language="JavaScript">
<!--
function closeIt() {
window.opener.document.location.reload();
window.close();
}
closeIt();
// -->
</script>
';
}
function displaySaveComment(){
$this->tpl->display("save_comment.tpl");
}
function displayUpdateComment(){
$this->tpl->display("update_comment.tpl");
}
function beginOneTreeID($id){
$sql = "select a.id from tree a, tree b where b.id=? and (a._left<b._left and a._right>b._right) and a._level=1 limit 1";
return $this->db->getOne($sql,$id);
}
function saveComment($data,$user_id = 0,$upload){
$tree_next_id = $this->db->nextId('mySequenceTree');
$arrayData = array("id"=>$tree_next_id,"user_id"=>$user_id,"parent_id"=>$data['treeID'],"topic_id"=>$data['topicID'],"about"=>$data['about_comment'],"login"=>$data['login'],"mktime"=>mktime());
if(isset($upload['pic']['tmp_name']) && $upload['pic']['tmp_name']){
$arrayData['pic'] = upload_ImageResize($upload['pic'],array('width'=>"300",'height'=>"300",'upload_path'=>"./uploaded/pic/forum/"));
}
$this->tree->insert($data['treeID'],$arrayData);
$beginID = $this->beginOneTreeID($tree_next_id);
$sql = "update topic set comments=comments+1,comment_last=?,comment_last_mktime=? where tree_id=?";
$this->db->query($sql, array($tree_next_id,mktime(),$beginID));
}
function updateComment($data,$upload){
$table_name = "tree";
$fields_values = array("about"=>$data['about_comment']);
if(isset($upload['pic']['tmp_name']) && $upload['pic']['tmp_name']){
$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"300",'height'=>"300",'upload_path'=>"./uploaded/pic/forum/"));
}
$this->db->autoExecute($table_name,$fields_values,DB_AUTOQUERY_UPDATE,"id={$data['update_id']}");
}
function viewComments($id){
$sql = "select a.*,forum_user.pic as pic1 from tree b, tree a LEFT JOIN forum_user ON forum_user.id=a.user_id where b.id=? and (a._left>b._left and a._right<b._right) and a._level=2 order by a.mktime desc";
$pagerOptions = Array(
'mode' => 'Sliding',
'delta' => 6,
'perPage' => 30,
);
$res = Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array($id));
foreach($res['data'] as $key=>$row){
$sql = "select * from tree where parent_id=? order by mktime desc";
$res['data'][$key]['child'] = $this->db->getAll($sql,array($row['id']),DB_FETCHMODE_ASSOC);
}
//print_r($res);
$this->tpl->assign('commentsData', $res);
}
function viewCommentOne($id){
$sql = "select * from tree where id=?";
$row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
$this->tpl->assign('comment',$row);
}
function viewTopicsCommentsLast(){
$sql = "select topic.*,tree.about as comment_str from topic left join tree on tree.id=topic.comment_last Where 1=1 order by topic.comment_last_mktime desc, topic.mktime desc limit 7";
$row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
$this->tpl->assign('topics',$row);
}
function last_comment(){
$sql = "select tree.*,forum_user.login from tree LEFT JOIN forum_user ON forum_user.id=tree.user_id where tree._level>1 order by tree.mktime desc limit 3";
$row = $this -> db -> getAll($sql,array(),DB_FETCHMODE_ASSOC);
$this -> tpl -> assign("last_comments",$row);
}
function viewTopics($param){
$search = array();
if(isset($param['firmID']) && $param['firmID']>0)$search[] = "topic.firm_id={$param['firmID']}";
$sql = "select topic.* from topic where 1=1 ";
if(count($search))$sql .= " and " . implode(" and ",$search) . " ";
$sql .= "order by topic.comment_last_mktime desc";
$pagerOptions = Array(
'mode' => 'Sliding',
'delta' => 6,
'perPage' => 20,
'linkClass'=>"link_class",
'curPageLinkClassName'=>"link_class_active",
'spacesBeforeSeparator' => 1,
'spacesAfterSeparator' => 1
);
$this->tpl->assign('topicData', Pager_Wrapper_DB($this->db, $sql, $pagerOptions));
}
function viewFirmTopics($firm_id){
$sql = "select * from topic where firm_id=? order by mktime desc";
$pagerOptions = Array(
'mode' => 'Sliding',
'delta' => 6,
'perPage' => 10,
'spacesBeforeSeparator' => 1,
'spacesAfterSeparator' => 1
);
$this->tpl->assign('topicFirmData', Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array($firm_id)));
}
function viewTopic($id,&$row){
$sql = "select * from topic where id=? limit 1";
$row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
$this->tpl->assign('topic', $row);
return $row['tree_id'];
}
function viewParentTopic($parent_table,$parent_id){
$sql = "select * from topic where parent_table=? and parent_table_id=? limit 1";
$row = $this->db->getRow($sql,array($parent_table,$parent_id),DB_FETCHMODE_ASSOC);
$this->tpl->assign('topic_parent', $row);
}
function Click($id){
$sql = "update topic set view=view+1 where id=? limit 1";
$this->db->query($sql,$id);
}
function yourComments($firm_id,$parent_table = 'gallery'){
$mktime = mktime(0,0,0,date("m"),date("d"),date("Y"));
$end_mktime = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
$sql = "select count(a.id) from tree a,tree b,topic where topic.parent_table=? AND topic.firm_id=? AND b.id=topic.tree_id AND (a._left>b._left and a._right<b._right) and (a.mktime>=? and a.mktime<=?)";
$count = $this->db->getOne($sql,array($parent_table,$firm_id,$mktime,$end_mktime));
$this->tpl->assign('countCommensDays_'.$parent_table,$count);
}
function displayForum(){
$this->tpl->assign("tpl","forum.tpl");
}
function displayTopic(){
$this->tpl->assign("tpl","topic.tpl");
}
function displayRegister(){
$this->tpl->assign("tpl","forum_register.tpl");
}
}
?>