Cheetah
ChWsbCmtsQuery.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbDb');
9 
13 class ChWsbCmtsQuery extends ChWsbDb
14 {
15  var $_aSystem; // current voting system
16  var $_sTable;
18 
19  function __construct(&$aSystem)
20  {
21  $this->_aSystem = &$aSystem;
22  $this->_sTable = $this->_aSystem['table_cmts'];
23  $this->_sTableTrack = $this->_aSystem['table_track'];
24  parent::__construct();
25  }
26 
27  function getTableName ()
28  {
29  return $this->_sTable;
30  }
31 
32  function getComments ($iId, $iCmtParentId = 0, $iAuthorId = 0, $sCmtOrder = 'ASC', $iStart = 0, $iCount = -1)
33  {
35  $iTimestamp = time();
36  $sFields = "'' AS `cmt_rated`,";
37  $sJoin = '';
38  if ($iAuthorId) {
39  $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
40  $sJoin = "LEFT JOIN {$this->_sTableTrack} AS `r` ON (`r`.`cmt_system_id` = ".$this->_aSystem['system_id']." AND `r`.`cmt_id` = `c`.`cmt_id` AND `r`.`cmt_rate_author_id` = $iAuthorId)";
41  }
42 
43  $aCmts = $this->getAll("SELECT
44  $sFields
45  `c`.`cmt_id`,
46  `c`.`cmt_parent_id`,
47  `c`.`cmt_object_id`,
48  `c`.`cmt_author_id`,
49  `c`.`cmt_text`,
50  `c`.`cmt_mood`,
51  `c`.`cmt_rate`,
52  `c`.`cmt_rate_count`,
53  `c`.`cmt_replies`,
54  UNIX_TIMESTAMP(`c`.`cmt_time`) AS `cmt_time_ts`,
55  ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`,
56  `p`.`NickName` AS `cmt_author_name`
57  FROM {$this->_sTable} AS `c`
58  LEFT JOIN `Profiles` AS `p` ON (`p`.`ID` = `c`.`cmt_author_id`)
59  $sJoin
60  WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_parent_id` = ?
61  ORDER BY `c`.`cmt_time` " . (strtoupper($sCmtOrder) == 'ASC' ? 'ASC' : 'DESC') . ($iCount != -1 ? ' LIMIT ' . $iStart . ', ' . $iCount : ''),
62  [
63  $iId,
64  $iCmtParentId
65  ]
66  );
67 
68  //LEFT JOIN `media` AS `m` ON (`m`.`med_id` = `p`.`Avatar` AND `m`.`med_status` = 'active')
69 
70  foreach($aCmts as $k => $aCmt) {
71  $aCmts[$k]['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $aCmt['cmt_text']);
72  $aCmts[$k]['cmt_ago'] = defineTimeInterval($aCmt['cmt_time_ts']);
73  }
74 
75  return $aCmts;
76  }
77 
78  function getComment ($iId, $iCmtId, $iAuthorId = 0)
79  {
81 
82  $iTimestamp = time();
83  $sFields = "'' AS `cmt_rated`,";
84  $sJoin = '';
85  if ($iAuthorId) {
86  $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
87  $sJoin = "LEFT JOIN {$this->_sTableTrack} AS `r` ON (`r`.`cmt_system_id` = ".$this->_aSystem['system_id']." AND `r`.`cmt_id` = `c`.`cmt_id` AND `r`.`cmt_rate_author_id` = $iAuthorId)";
88  }
89  $aComment = $this->getRow("SELECT
90  $sFields
91  `c`.`cmt_id`,
92  `c`.`cmt_parent_id`,
93  `c`.`cmt_object_id`,
94  `c`.`cmt_author_id`,
95  `c`.`cmt_text`,
96  `c`.`cmt_mood`,
97  `c`.`cmt_rate`,
98  `c`.`cmt_rate_count`,
99  `c`.`cmt_replies`,
100  UNIX_TIMESTAMP(`c`.`cmt_time`) AS `cmt_time_ts`,
101  ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`,
102  `p`.`NickName` AS `cmt_author_name`
103  FROM {$this->_sTable} AS `c`
104  LEFT JOIN `Profiles` AS `p` ON (`p`.`ID` = `c`.`cmt_author_id`)
105  $sJoin
106  WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_id` = ?
107  LIMIT 1", [$iId, $iCmtId]);
108 
109  if(!empty($aComment) && is_array($aComment)) {
110  $aComment['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $aComment['cmt_text']);
111  $aComment['cmt_ago'] = defineTimeInterval($aComment['cmt_time_ts']);
112  }
113 
114  return $aComment;
115  }
116 
117  function getCommentSimple ($iId, $iCmtId)
118  {
119  $iTimestamp = time();
120  return $this->getRow("
121  SELECT
122  *, ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`
123  FROM {$this->_sTable} AS `c`
124  WHERE `cmt_object_id` = ? AND `cmt_id` = ?
125  LIMIT 1", [$iId, $iCmtId]);
126  }
127 
128  function addComment ($iId, $iCmtParentId, $iAuthorId, $sText, $iMood)
129  {
130  if (!$this->query("INSERT INTO {$this->_sTable} SET
131  `cmt_parent_id` = '$iCmtParentId',
132  `cmt_object_id` = '$iId',
133  `cmt_author_id` = '$iAuthorId',
134  `cmt_text` = '$sText',
135  `cmt_mood` = '$iMood',
136  `cmt_time` = NOW()"))
137  {
138  return false;
139  }
140 
141  $iRet = $this->lastId();
142 
143  if ($iCmtParentId)
144  $this->query ("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` + 1 WHERE `cmt_id` = '$iCmtParentId' LIMIT 1");
145 
146  return $iRet;
147  }
148 
149  function removeComment ($iId, $iCmtId, $iCmtParentId)
150  {
151  if (!$this->query("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = '$iId' AND `cmt_id` = '$iCmtId' LIMIT 1"))
152  return false;
153 
154  $this->query ("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = '$iCmtParentId' LIMIT 1");
155 
156  return true;
157  }
158 
159  function updateComment ($iId, $iCmtId, $sText, $iMood)
160  {
161  return $this->query("UPDATE {$this->_sTable} SET `cmt_text` = '$sText', `cmt_mood` = '$iMood' WHERE `cmt_object_id` = '$iId' AND `cmt_id` = '$iCmtId' LIMIT 1");
162  }
163 
164  function rateComment ($iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp)
165  {
166  $iTimestamp = time();
167  if ($this->query("INSERT IGNORE INTO {$this->_sTableTrack} SET
168  `cmt_system_id` = '$iSystemId',
169  `cmt_id` = '$iCmtId',
170  `cmt_rate` = '$iRate',
171  `cmt_rate_author_id` = '$iAuthorId',
172  `cmt_rate_author_nip` = INET_ATON('$sAuthorIp'),
173  `cmt_rate_ts` = $iTimestamp"))
174  {
175  $this->query("UPDATE {$this->_sTable} SET `cmt_rate` = `cmt_rate` + $iRate, `cmt_rate_count` = `cmt_rate_count` + 1 WHERE `cmt_id` = '$iCmtId' LIMIT 1");
176  return true;
177  }
178 
179  return false;
180  }
181 
182  function deleteAuthorComments ($iAuthorId)
183  {
184  $aObjectsIds = array();
185  $isDelOccured = 0;
186  $a = $this->getAll ("SELECT `cmt_id`, `cmt_parent_id`, `cmt_object_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", [$iAuthorId]);
187  foreach ($a as $r) {
188  $this->query ("DELETE FROM {$this->_sTable} WHERE `cmt_id` = '{$r['cmt_id']}'");
189  $this->query ("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = '{$r['cmt_parent_id']}'");
190  $aObjectsIds[$r['cmt_object_id']] = $r['cmt_object_id'];
191  $isDelOccured = 1;
192  }
193 
194  $this->query ("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = '$iAuthorId' AND `cmt_replies` != 0");
195 
196  if ($isDelOccured) {
197  foreach ($aObjectsIds as $iObjectId) {
198  $iCount = $this->getObjectCommentsCount ($iObjectId);
199  $this->updateTriggerTable($iObjectId, $iCount);
200  }
201  $this->query ("OPTIMIZE TABLE {$this->_sTable}");
202  }
203  }
204 
205  function deleteObjectComments ($iObjectId)
206  {
207  $this->query ("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = '$iObjectId'");
208  $this->query ("OPTIMIZE TABLE {$this->_sTable}");
209  }
210 
211  function getObjectCommentsCount ($iObjectId, $iParentId = -1)
212  {
213  return $this->getOne ("SELECT COUNT(*) FROM `" . $this->_sTable ."` WHERE `cmt_object_id`='" . $iObjectId . "'" . ($iParentId != -1 ? " AND `cmt_parent_id`='" . $iParentId . "'" : ""));
214  }
215 
216  function updateTriggerTable($iId, $iCount)
217  {
218  if (empty($this->_aSystem['trigger_table']))
219  return true;
220  return $this->query("UPDATE `{$this->_aSystem['trigger_table']}` SET `{$this->_aSystem['trigger_field_comments']}` = '$iCount' WHERE `{$this->_aSystem['trigger_field_id']}` = '$iId' LIMIT 1");
221  }
222 
223  function maintenance()
224  {
225  $iTimestamp = time();
226  $iDeletedRecords = $this->query("DELETE FROM {$this->_sTableTrack} WHERE `cmt_rate_ts` < ($iTimestamp - " . (int)CH_OLD_CMT_VOTES . ")");
227  if ($iDeletedRecords)
228  $this->query("OPTIMIZE TABLE {$this->_sTableTrack}");
229  return $iDeletedRecords;
230  }
231 }
ChWsbCmtsQuery\getComments
getComments($iId, $iCmtParentId=0, $iAuthorId=0, $sCmtOrder='ASC', $iStart=0, $iCount=-1)
Definition: ChWsbCmtsQuery.php:32
ChWsbCmtsQuery\getCommentSimple
getCommentSimple($iId, $iCmtId)
Definition: ChWsbCmtsQuery.php:117
defineTimeInterval
defineTimeInterval($iTime, $bAutoDateConvert=true, $bShort=false)
Definition: utils.inc.php:831
CH_OLD_CMT_VOTES
const CH_OLD_CMT_VOTES
Definition: ChWsbCmts.php:10
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
$sHomeUrl
$sHomeUrl
Definition: header.inc.php:41
php
ChWsbCmtsQuery\$_sTableTrack
$_sTableTrack
Definition: ChWsbCmtsQuery.php:17
$iId
$iId
Definition: license.php:15
ChWsbDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:206
ChWsbCmtsQuery\__construct
__construct(&$aSystem)
Definition: ChWsbCmtsQuery.php:19
ChWsbCmtsQuery
Definition: ChWsbCmtsQuery.php:14
ChWsbCmtsQuery\removeComment
removeComment($iId, $iCmtId, $iCmtParentId)
Definition: ChWsbCmtsQuery.php:149
ChWsbDb\getRow
getRow($sQuery, $aBindings=[], $iFetchStyle=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:225
ChWsbCmtsQuery\deleteAuthorComments
deleteAuthorComments($iAuthorId)
Definition: ChWsbCmtsQuery.php:182
ChWsbDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbDb.php:386
ChWsbCmtsQuery\updateComment
updateComment($iId, $iCmtId, $sText, $iMood)
Definition: ChWsbCmtsQuery.php:159
ChWsbCmtsQuery\getTableName
getTableName()
Definition: ChWsbCmtsQuery.php:27
ChWsbCmtsQuery\deleteObjectComments
deleteObjectComments($iObjectId)
Definition: ChWsbCmtsQuery.php:205
ChWsbCmtsQuery\rateComment
rateComment($iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp)
Definition: ChWsbCmtsQuery.php:164
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
ChWsbDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbDb.php:263
ChWsbCmtsQuery\getObjectCommentsCount
getObjectCommentsCount($iObjectId, $iParentId=-1)
Definition: ChWsbCmtsQuery.php:211
ChWsbCmtsQuery\getComment
getComment($iId, $iCmtId, $iAuthorId=0)
Definition: ChWsbCmtsQuery.php:78
ChWsbCmtsQuery\maintenance
maintenance()
Definition: ChWsbCmtsQuery.php:223
ChWsbCmtsQuery\$_sTable
$_sTable
Definition: ChWsbCmtsQuery.php:16
ChWsbCmtsQuery\updateTriggerTable
updateTriggerTable($iId, $iCount)
Definition: ChWsbCmtsQuery.php:216
ChWsbDb\lastId
lastId()
Definition: ChWsbDb.php:449
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
ChWsbDb
Definition: ChWsbDb.php:13
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbCmtsQuery\addComment
addComment($iId, $iCmtParentId, $iAuthorId, $sText, $iMood)
Definition: ChWsbCmtsQuery.php:128
ChWsbCmtsQuery\$_aSystem
$_aSystem
Definition: ChWsbCmtsQuery.php:15