forked from leonardoxc/leonardoxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CL_comments.php
259 lines (207 loc) · 7.16 KB
/
CL_comments.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
<?
//************************************************************************
// Leonardo XC Server, http://www.leonardoxc.net
//
// Copyright (c) 2004-2010 by Andreadakis Manolis
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// $Id: CL_comments.php,v 1.10 2010/11/25 12:36:16 manolis Exp $
//
//************************************************************************
class flightComments {
var $flightID;
var $commentsNum;
var $comments;
var $threads;
var $valuesArray;
var $gotValues;
function flightComments($flightID="") {
if ($flightID!="") {
$this->flightID=$flightID;
}
//$this->valuesArray=array("flightID","path","name","description");
$this->gotValues=0;
$this->commentsNum=0;
$this->comments=array();
}
function setCommentsStatus($enable) {
global $db,$commentsTable,$flightsTable;
$query="UPDATE $flightsTable SET commentsEnabled=$enable WHERE ID=".$this->flightID;
$res= $db->sql_query($query );
if($res <= 0){
echo "Error in setCommentsStatusfor flight ".$this->flightID." : $query<BR>";
return 0;
}
return 1;
}
function addComment($params,$updateFlightsTable=1) {
global $db,$commentsTable,$flightsTable;
$now=gmdate("Y-m-d H:i:s");
$query="INSERT INTO $commentsTable (flightID,parentID,userID,userServerID,guestName,guestPass,guestEmail,
`text`,languageCode,dateAdded,dateUpdated)
VALUES (".
$this->flightID.",'".prep_for_DB($params['parentID'])."','".
prep_for_DB($params['userID'])."','".
prep_for_DB($params['userServerID'])."','".
prep_for_DB($params['guestName'])."','".
prep_for_DB($params['guestPass'])."','".
prep_for_DB($params['guestEmail'])."','".
prep_for_DB($params['text'])."','".
prep_for_DB($params['languageCode'])."','".
$now."','".
$now."' ) ";
// echo $query;
$res= $db->sql_query($query);
if($res <= 0){
echo "Error putting comment for flight ".$this->flightID." to DB: $query<BR>";
return 0;
}
$newID=$db->sql_nextid();
if ($updateFlightsTable) {
$query="UPDATE $flightsTable SET commentsNum=commentsNum+1 WHERE ID=".$this->flightID;
$res= $db->sql_query($query );
if($res <= 0){
echo "Error updating commentsNum for flight ".$this->flightID." : $query<BR>";
}
}
return $newID;
}
function changeComment($params) {
global $db,$commentsTable;
$now=gmdate("Y-m-d H:i:s");
$query="UPDATE $commentsTable SET `text`='".prep_for_DB($params['text'])."' , dateUpdated ='$now'
WHERE commentID=".$params['commentID']." ";
// echo $query;
$res= $db->sql_query($query);
if($res <= 0){
echo "Error putting comment for flight ".$this->flightID." to DB: $query<BR>";
return 0;
}
return $newID;
}
function deleteComment($commentID,$parentID,$updateFlightsTable=1) {
//if (!$this->gotValues) $this->getFromDB();
global $db,$commentsTable,$flightsTable;
$result=0;
if ($updateFlightsTable) {
$res= $db->sql_query("UPDATE $flightsTable SET commentsNum=commentsNum-1 WHERE ID=".$this->flightID );
if($res <= 0){
echo "Error updating commentsNum for flight".$this->flightID."<BR>";
$result++;
}
}
// echo "###".$this->comments[$commentNum]['ID'];
$res= $db->sql_query("DELETE FROM $commentsTable WHERE commentID=$commentID");
if($res <= 0){
echo "Error deleting comment $commentID for flight".$this->flightID."<BR>";
$result++;
}
// make all childs of this comment to have the parent of the deleted comment
$res= $db->sql_query("UPDATE $commentsTable SET parentID=$parentID WHERE parentID=$commentID");
if($res <= 0){
echo "Error updating orphaned childen of deleted comment<BR>";
$result++;
}
return $result;
}
function deleteAllComments($updateFlightsTable=1) {
global $db,$commentsTable,$flightsTable;
if ($updateFlightsTable) {
$res= $db->sql_query("UPDATE $flightsTable SET commentsNum=0 WHERE ID=".$this->flightID );
if($res <= 0){
echo "Error updating hascomments for flight ".$this->flightID."<BR>";
}
}
$res= $db->sql_query("DELETE FROM $commentsTable WHERE flightID=".$this->flightID );
if($res <= 0){
echo "Error deleting comments for flight".$this->flightID."<BR>";
}
}
function getFirstFromDB() {
global $db,$commentsTable;
$sql="SELECT * FROM $commentsTable WHERE flightID=".$this->flightID ." ORDER BY commentID ASC LIMIT 1";
$res= $db->sql_query($sql);
if($res <= 0){
echo "Error getting comments from DB for flight".$this->flightID." <BR>";
return 0;
}
$row = $db->sql_fetchrow($res);
return $row;
}
function getFromDB() {
global $db,$commentsTable;
$sql="SELECT * FROM $commentsTable WHERE flightID=".$this->flightID ." ORDER BY commentID ASC";
$res= $db->sql_query($sql);
if($res <= 0){
echo "Error getting comments from DB for flight".$this->flightID." <BR>";
return 0;
}
$comments = array();
$commentsParents= array();
$commentsNum=0;
while ($row = $db->sql_fetchrow($res) ) {
// echo "got ".$row['text']."<br>";
$commentsParents[]=array('id'=>$row['commentID'], 'parent_id'=>$row['parentID'] );
$comments[$row['commentID']]=$row;
//print_r($this->comments[$this->commentsNum]);
$this->commentsNum++;
}
$threadedComments = new threadedComments($commentsParents);
$this->threads=$threadedComments->threads;
$this->comments=$comments;
$this->commentsNum=$commentsNum;
$this->gotValues=1;
return 1;
}
function getThreadsOutput() {
$str='';
foreach($this->threads as $thread) {
$commentData=$this->comments[$thread['id']];
$str.="<div class='comments depth".$thread['depth']."'>";
$str.=$commentData['text'];
$str.="</div>";
}
return $str;
}
}
class threadedComments {
public $parents = array();
public $children = array();
public $threads=array();
function __construct($comments) {
foreach ($comments as $comment) {
if (!$comment['parent_id'] ) {
$this->parents[$comment['id']][] = $comment;
} else {
$this->children[$comment['parent_id']][] = $comment;
}
}
$this->makeThreads();
//echo "Init Threaded_comments:<br>";
//print_r($this->parents);
//print_r($this->children);
//echo "Threads are ready : <BR>";
//print_r($this->threads);
}
public function makeThreads() {
$this->threads=array();
foreach ($this->parents as $c) {
$this->process_parent($c);
}
}
private function process_comment($comment, $depth) {
$this->threads[]=array('depth'=>$depth,'id'=>$comment['id']);
}
private function process_parent($comment, $depth = 0) {
foreach ($comment as $c) {
$this->process_comment($c, $depth);
if (isset($this->children[$c['id']])) {
$this->process_parent($this->children[$c['id']], $depth + 1);
}
}
}
}
?>