| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- /**
- * @package Dolphin Core
- * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
- * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
- */
- defined('BX_DOL') or die('hack attempt');
- bx_import('BxDolDb');
- /**
- * @see BxDolCmts
- */
- class BxDolCmtsQuery extends BxDolDb
- {
- protected $_oMain;
- protected $_sTable;
- protected $_sTableTrack;
- protected $_sTriggerTable;
- protected $_sTriggerFieldId;
- protected $_sTriggerFieldTitle;
- protected $_sTriggerFieldComments;
- protected $_sTableImages;
- protected $_sTableImages2Entries;
- function BxDolCmtsQuery(&$oMain)
- {
- $this->_oMain = $oMain;
- $aSystem = $this->_oMain->getSystemInfo();
- $this->_sTable = $aSystem['table_cmts'];
- $this->_sTableTrack = $aSystem['table_track'];
- $this->_sTriggerTable = $aSystem['trigger_table'];
- $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
- $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
- $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
- $this->_sTableImages = $aSystem['table_images'];
- $this->_sTableImages2Entries = $aSystem['table_images2entries'];
- parent::BxDolDb();
- }
- function getTableName ()
- {
- return $this->_sTable;
- }
- function getCommentsCount ($iId, $iCmtVParentId = 0) {
- $sWhereParent = '';
- if((int)$iCmtVParentId >= 0)
- $sWhereParent = $this->prepare(" AND `cmt_vparent_id` = ?", $iCmtVParentId);
- $sQuery = $this->prepare("SELECT COUNT(*) FROM `{$this->_sTable}` WHERE `cmt_object_id` = ?" . $sWhereParent, $iId);
- return (int)$this->getOne($sQuery);
- }
- function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $aOrder = array(), $iStart = 0, $iCount = -1)
- {
- $sFields = "'' AS `cmt_rated`,";
- $sJoin = "";
- if ($iAuthorId) {
- $sFields = "`r`.`cmt_rate` AS `cmt_rated`,";
- $sJoin = $this->prepare(" LEFT JOIN `{$this->_sTableTrack}` AS `r` ON (`r`.`cmt_system_id` = ? AND `r`.`cmt_id` = `c`.`cmt_id` AND `r`.`cmt_rate_author_id` = ?)", $this->_oMain->getSystemId(), $iAuthorId);
- }
- $sWhereParent = '';
- if((int)$iCmtVParentId >= 0)
- $sWhereParent = $this->prepare(" AND `c`.`cmt_vparent_id` = ?", $iCmtVParentId);
- $sOder = " ORDER BY `c`.`cmt_time` ASC";
- if(isset($aOrder['by']) && isset($aOrder['way'])) {
- $aOrder['way'] = strtoupper(in_array($aOrder['way'], array(BX_CMT_ORDER_WAY_ASC, BX_CMT_ORDER_WAY_DESC)) ? $aOrder['way'] : BX_CMT_ORDER_WAY_ASC);
- switch($aOrder['by']) {
- case BX_CMT_ORDER_BY_DATE:
- $sOder = " ORDER BY `c`.`cmt_time` " . $aOrder['way'];
- break;
- case BX_CMT_ORDER_BY_POPULAR:
- $sOder = " ORDER BY `c`.`cmt_rate` " . $aOrder['way'];
- break;
- case BX_CMT_ORDER_BY_CONNECTION:
- $sFields .= " IF(NOT ISNULL(`tcc`.`id`), 1, 0) AS `cmt_author_contact`,";
- $sFields .= " IF(NOT ISNULL(`tcf`.`id`), 1, 0) AS `cmt_author_friend`,";
- $sJoin .= $this->prepare(" LEFT JOIN `sys_profiles_conn_contacts` AS `tcc` ON ((`c`.`cmt_author_id`=`tcc`.`initiator` AND `tcc`.`content`=?) OR (`c`.`cmt_author_id`=`tcc`.`content` AND `tcc`.`initiator`=?))", $iAuthorId, $iAuthorId);
- $sJoin .= $this->prepare(" LEFT JOIN `sys_profiles_conn_friends` AS `tcf` ON ((`c`.`cmt_author_id`=`tcf`.`initiator` AND `tcf`.`content`=?) OR (`c`.`cmt_author_id`=`tcf`.`content` AND `tcf`.`initiator`=?))", $iAuthorId, $iAuthorId);
- $sOder = " ORDER BY `cmt_author_friend` " . $aOrder['way'] . ",`cmt_author_contact` " . $aOrder['way'] . ",`c`.`cmt_time` ASC";
- break;
- }
- }
- $sLimit = $iCount != -1 ? $this->prepare(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
- $sQuery = $this->prepare("SELECT
- $sFields
- `c`.`cmt_id`,
- `c`.`cmt_parent_id`,
- `c`.`cmt_vparent_id`,
- `c`.`cmt_object_id`,
- `c`.`cmt_author_id`,
- `c`.`cmt_level`,
- `c`.`cmt_text`,
- `c`.`cmt_rate`,
- `c`.`cmt_rate_count`,
- `c`.`cmt_replies`,
- `c`.`cmt_time`
- FROM `{$this->_sTable}` AS `c`
- $sJoin
- WHERE `c`.`cmt_object_id` = ?" . $sWhereParent . $sOder . $sLimit, $iId);
- return $this->getAll($sQuery);
- }
- function getComment ($iId, $iCmtId, $iAuthorId = 0)
- {
- $sFields = "'' AS `cmt_rated`,";
- $sJoin = '';
- if ($iAuthorId) {
- $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
- $sJoin = $this->prepare("LEFT JOIN {$this->_sTableTrack} AS `r` ON (`r`.`cmt_system_id` = ? AND `r`.`cmt_id` = `c`.`cmt_id` AND `r`.`cmt_rate_author_id` = ?)", $this->_oMain->getSystemId(), $iAuthorId);
- }
- $sQuery = $this->prepare("SELECT
- $sFields
- `c`.`cmt_id`,
- `c`.`cmt_parent_id`,
- `c`.`cmt_vparent_id`,
- `c`.`cmt_object_id`,
- `c`.`cmt_author_id`,
- `c`.`cmt_level`,
- `c`.`cmt_text`,
- `c`.`cmt_rate`,
- `c`.`cmt_rate_count`,
- `c`.`cmt_replies`,
- `c`.`cmt_time`
- FROM {$this->_sTable} AS `c`
- $sJoin
- WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_id` = ?
- LIMIT 1", $iId, $iCmtId);
- return $this->getRow($sQuery);
- }
- function getCommentSimple ($iId, $iCmtId)
- {
- $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
- return $this->getRow($sQuery);
- }
- function removeComment ($iId, $iCmtId, $iCmtParentId)
- {
- $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
- if (!$this->query($sQuery))
- return false;
- if($iCmtParentId)
- $this->updateRepliesCount($iCmtParentId, -1);
- return true;
- }
- function saveImages($iSystemId, $iCmtId, $iImageId)
- {
- $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableImages2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
- return (int)$this->query($sQuery) > 0;
- }
- function getImages($iSystemId, $iCmtId)
- {
- $sQuery = $this->prepare("SELECT * FROM `{$this->_sTableImages2Entries}` WHERE `system_id`=? AND `cmt_id`=?", $iSystemId, $iCmtId);
- return $this->getAll($sQuery);
- }
- function deleteImages($iSystemId, $iCmtId)
- {
- $sQuery = $this->prepare("DELETE FROM `{$this->_sTableImages2Entries}` WHERE `system_id`=? AND `cmt_id`=?", $iSystemId, $iCmtId);
- return $this->query($sQuery);
- }
- function updateRepliesCount($iCmtId, $iCount)
- {
- $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
- return $this->query($sQuery);
- }
- function rateComment ($iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp)
- {
- $iCount = 0;
- $iTimestamp = time();
- if($iRate == BX_CMT_RATE_VALUE_PLUS) {
- $iCount = 1;
- $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET
- `cmt_system_id` = ?,
- `cmt_id` = ?,
- `cmt_rate` = ?,
- `cmt_rate_author_id` = ?,
- `cmt_rate_author_nip` = INET_ATON(?),
- `cmt_rate_ts` = ?", $iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp, $iTimestamp);
- }
- else if($iRate == BX_CMT_RATE_VALUE_MINUS) {
- $iCount = -1;
- $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE
- `cmt_system_id` = ? AND
- `cmt_id` = ? AND
- `cmt_rate_author_id` = ?", $iSystemId, $iCmtId, $iAuthorId);
- }
- if ($this->query($sQuery)) {
- $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET
- `cmt_rate` = `cmt_rate` + ?,
- `cmt_rate_count` = `cmt_rate_count` + ?
- WHERE `cmt_id` = ?
- LIMIT 1", $iRate, $iCount, $iCmtId);
- $this->query($sQuery);
- return true;
- }
- return false;
- }
- function getRatedBy ($iSystemId, $iCmtId)
- {
- $sQuery = $this->prepare("SELECT `cmt_rate_author_id` FROM `{$this->_sTableTrack}` WHERE `cmt_system_id`=? AND `cmt_id`=?", $iSystemId, $iCmtId);
- return $this->getColumn($sQuery);
- }
- function deleteAuthorComments ($iAuthorId)
- {
- $isDelOccured = 0;
- $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
- $a = $this->getAll ($sQuery);
- for ( reset($a) ; list (, $r) = each ($a) ; )
- {
- $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
- $this->query ($sQuery);
- $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
- $this->query ($sQuery);
- $isDelOccured = 1;
- }
- $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
- $this->query ($sQuery);
- if ($isDelOccured)
- $this->query ("OPTIMIZE TABLE {$this->_sTable}");
- }
- function deleteObjectComments ($iObjectId)
- {
- $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
- $this->query ($sQuery);
- $this->query ("OPTIMIZE TABLE {$this->_sTable}");
- }
- function getObjectCommentsCount ($iObjectId, $iParentId = -1)
- {
- $sWhere = '';
- if ($iParentId != -1)
- $sWhere = $this->prepare(" AND `cmt_parent_id` = ?", $iParentId);
- $sQuery = $this->prepare("SELECT COUNT(*) FROM `" . $this->_sTable ."` WHERE `cmt_object_id` = ? " . $sWhere, $iObjectId);
- return $this->getOne ($sQuery);
- }
- function getObjectTitle($iId)
- {
- $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
- return $this->getOne($sQuery);
- }
- function updateTriggerTable($iId, $iCount)
- {
- $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
- return $this->query($sQuery);
- }
- function maintenance()
- {
- $iTimestamp = time();
- $iDeletedRecords = $this->query("DELETE FROM {$this->_sTableTrack} WHERE `cmt_rate_ts` < ($iTimestamp - " . (int)BX_OLD_CMT_VOTES . ")");
- if ($iDeletedRecords)
- $this->query("OPTIMIZE TABLE {$this->_sTableTrack}");
- return $iDeletedRecords;
- }
- }
|