| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- /**
- * @see BxDolCmts
- */
- class BxDolCmtsQuery extends BxDolDb
- {
- protected $_oMain;
- protected $_sTable;
- protected $_sTriggerTable;
- protected $_sTriggerFieldId;
- protected $_sTriggerFieldAuthor;
- protected $_sTriggerFieldTitle;
- protected $_sTriggerFieldComments;
- protected $_sTableFiles;
- protected $_sTableFiles2Entries;
- protected $_sTableIds;
- public function __construct(&$oMain)
- {
- $this->_oMain = $oMain;
- $aSystem = $this->_oMain->getSystemInfo();
- $this->_sTable = $aSystem['table'];
- $this->_sTriggerTable = $aSystem['trigger_table'];
- $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
- $this->_sTriggerFieldAuthor = $aSystem['trigger_field_author'];
- $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
- $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
- $this->_sTableFiles = $aSystem['table_images'];
- $this->_sTableFiles2Entries = $aSystem['table_images2entries'];
- $this->_sTableIds = $aSystem['table_ids'];
- parent::__construct();
- }
- function getTableName ()
- {
- return $this->_sTable;
- }
- function setTableNameFiles($sTable)
- {
- $this->_sTableFiles = $sTable;
- }
- function setTableNameFiles2Entries($sTable)
- {
- $this->_sTableFiles2Entries = $sTable;
- }
- function getCommentsCountAll ($iId)
- {
- $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldComments}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ?", $iId);
- return (int)$this->getOne($sQuery);
- }
- function getCommentsCount ($iId, $iCmtVParentId = -1, $iAuthorId = 0, $sFilter = '')
- {
- $aBindings = array(
- 'cmt_object_id' => $iId
- );
- $sWhereClause = '';
- if((int)$iCmtVParentId >= 0) {
- $aBindings['cmt_vparent_id'] = $iCmtVParentId;
- $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
- }
- $sJoinClause = '';
- switch($sFilter) {
- case BX_CMT_FILTER_FRIENDS:
- case BX_CMT_FILTER_SUBSCRIPTIONS:
- $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
-
- $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
- $sJoinClause .= ' ' . $aQueryParts['join'];
- break;
- case BX_CMT_FILTER_OTHERS:
- $aBindings['cmt_author_id'] = $iAuthorId;
- $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
- break;
- }
- $sQuery = "SELECT COUNT(*) FROM `{$this->_sTable}` $sJoinClause WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
- return (int)$this->getOne($sQuery, $aBindings);
- }
- function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '', $aOrder = array(), $iStart = 0, $iCount = -1)
- {
- $aBindings = array(
- 'cmt_object_id' => $iId
- );
- $sFields = $sJoin = "";
- $oVote = $this->_oMain->getVoteObject(0);
- if($oVote !== false) {
- $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
- $sFields .= $aSql['fields'];
- $sJoin .= $aSql['join'];
- }
- $sWhereParent = '';
- if((int)$iCmtVParentId >= 0) {
- $aBindings['cmt_vparent_id'] = $iCmtVParentId;
- $sWhereParent = " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
- }
- if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
- $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
- $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
- $sJoin .= ' ' . $aQueryParts['join'];
- }
- $sOder = " ORDER BY `{$this->_sTable}`.`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 `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
- break;
- case BX_CMT_ORDER_BY_POPULAR:
- $sOder = " ORDER BY `{$this->_sTable}`.`cmt_rate` " . $aOrder['way'];
- break;
- }
- }
- $sLimit = $iCount != -1 ? $this->prepareAsString(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
- $sQuery = "SELECT
- `{$this->_sTable}`.`cmt_id`,
- `{$this->_sTable}`.`cmt_parent_id`,
- `{$this->_sTable}`.`cmt_vparent_id`,
- `{$this->_sTable}`.`cmt_object_id`,
- `{$this->_sTable}`.`cmt_author_id`,
- `{$this->_sTable}`.`cmt_level`,
- `{$this->_sTable}`.`cmt_text`,
- `{$this->_sTable}`.`cmt_replies`,
- `{$this->_sTable}`.`cmt_time`
- $sFields
- FROM `{$this->_sTable}`
- $sJoin
- WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereParent . $sOder . $sLimit;
- return $this->getAll($sQuery, $aBindings);
- }
- function getCommentsBy($aParams = array())
- {
- $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
- $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
- $sSelectClause = "
- `{$this->_sTable}`.`cmt_id`,
- `{$this->_sTable}`.`cmt_parent_id`,
- `{$this->_sTable}`.`cmt_vparent_id`,
- `{$this->_sTable}`.`cmt_object_id`,
- `{$this->_sTable}`.`cmt_author_id`,
- `{$this->_sTable}`.`cmt_level`,
- `{$this->_sTable}`.`cmt_text`,
- `{$this->_sTable}`.`cmt_replies`,
- `{$this->_sTable}`.`cmt_time`";
- if(isset($aParams['object_id'])) {
- $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
- $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
- }
- switch($aParams['type']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1]['id'] = (int)$aParams['id'];
- $sWhereClause .= " AND `{$this->_sTable}`.`cmt_id` = :id";
- break;
- case 'latest':
- if(!empty($aParams['author'])) {
- $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author'];
- $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` " . (isset($aParams['others']) && (int)$aParams['others'] == 1 ? "<>" : "=") . " :cmt_author_id";
- }
- $sOrderClause = "`{$this->_sTable}`.`cmt_time` DESC";
- $sLimitClause = "";
- if(isset($aParams['per_page']))
- $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
- break;
- case 'object_id':
- $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
- $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
- $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
- $sLimitClause = "";
- if(isset($aParams['per_page']))
- $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
- break;
- case 'search_ids':
- $aMethod['name'] = 'getColumn';
- $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
- if (!empty($aParams['start']) && !empty($aParams['per_page']))
- $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
- elseif (!empty($aParams['per_page']))
- $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
- $sWhereConditions = "1";
- foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
- $sSearchValue = "";
- switch ($aSearchParam['operator']) {
- case 'like':
- $sSearchValue = " LIKE " . $this->escape("%" . $aSearchParam['value'] . "%");
- break;
- case 'in':
- $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
- break;
- case 'and':
- $iResult = 0;
- if (is_array($aSearchParam['value']))
- foreach ($aSearchParam['value'] as $iValue)
- $iResult |= pow (2, $iValue - 1);
- else
- $iResult = (int)$aSearchParam['value'];
- $sSearchValue = " & " . $iResult . "";
- break;
- default:
- $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
- $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
- }
- $sWhereConditions .= " AND `{$this->_sTable}`.`" . $sSearchParam . "`" . $sSearchValue;
- }
- $sWhereClause .= " AND (" . $sWhereConditions . ")";
- $sOrderClause .= "`{$this->_sTable}`.`cmt_time` ASC";
- break;
- case 'all_ids':
- $aMethod['name'] = 'getColumn';
- $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
- $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
- break;
- }
- if(!empty($sOrderClause))
- $sOrderClause = 'ORDER BY ' . $sOrderClause;
- if(!empty($sLimitClause))
- $sLimitClause = 'LIMIT ' . $sLimitClause;
- $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->_sTable}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- function getComment ($iId, $iCmtId)
- {
- $sFields = $sJoin = "";
- $oVote = $this->_oMain->getVoteObject($iCmtId);
- if($oVote !== false) {
- $aSql = $oVote->getSqlParts($this->_sTableIds, 'id');
- $sFields .= $aSql['fields'];
- $sJoin .= $aSql['join'];
- }
- $sQuery = $this->prepare("SELECT
- `{$this->_sTable}`.`cmt_id`,
- `{$this->_sTableIds}`.`id` AS `cmt_unique_id`,
- `{$this->_sTable}`.`cmt_parent_id`,
- `{$this->_sTable}`.`cmt_vparent_id`,
- `{$this->_sTable}`.`cmt_object_id`,
- `{$this->_sTable}`.`cmt_author_id`,
- `{$this->_sTable}`.`cmt_level`,
- `{$this->_sTable}`.`cmt_text`,
- `{$this->_sTable}`.`cmt_replies`,
- `{$this->_sTable}`.`cmt_time`
- $sFields
- FROM `{$this->_sTable}`
- LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = ?)
- $sJoin
- WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
- LIMIT 1", $this->_oMain->getSystemId(), $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->_sTableFiles2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
- return (int)$this->query($sQuery) > 0;
- }
- function getFiles($iSystemId, $iCmtId, $iId = false)
- {
- $aBindings = array(
- 'system_id' => $iSystemId
- );
- $sJoin = "";
- $sWhere = " AND `tf2e`.`system_id` = :system_id ";
- if($iCmtId !== false) {
- $aBindings['cmt_id'] = $iCmtId;
- $sWhere .= " AND `tf2e`.`cmt_id` = :cmt_id ";
- }
- if($iId !== false) {
- $aBindings['cmt_object_id'] = $iId;
- $sWhere .= " AND `te`.`cmt_object_id` = :cmt_object_id";
- $sJoin .= " INNER JOIN `{$this->_sTable}` AS `te` ON (`tf2e`.`cmt_id` = `te`.`cmt_id`) ";
- }
- $sQuery = "SELECT
- `tf2e`.*,
- `tf`.`file_name` AS `file_name`,
- `tf`.`mime_type` AS `mime_type`,
- `tf`.`size` AS `size`
- FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
- LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`) " . $sJoin . "
- WHERE 1 " . $sWhere;
- return $this->getAll($sQuery, $aBindings);
- }
- function deleteImages($iSystemId, $iCmtId)
- {
- $aBindings = array(
- 'system_id' => $iSystemId
- );
- $sWhereAddon = "";
- if ($iCmtId !== false) {
- $aBindings['cmt_id'] = $iCmtId;
- $sWhereAddon = " AND `cmt_id` = :cmt_id ";
- }
- return $this->query("DELETE FROM `{$this->_sTableFiles2Entries}` WHERE `system_id` = :system_id" . $sWhereAddon, $aBindings);
- }
- 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 deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
- {
- $aSystem = $this->_oMain->getSystemInfo();
- $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);
- $aFilesMore = $this->convertImagesArray($this->getFiles($aSystem['system_id'], $r['cmt_id']));
- $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
- if ($aFilesMore && null !== $aFiles)
- $aFiles = array_merge($aFiles, $aFilesMore);
- if (null !== $aCmtIds)
- $aCmtIds[] = $r['cmt_id'];
- $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, &$aFilesReturn = null, &$aCmtIds = null)
- {
- $aSystem = $this->_oMain->getSystemInfo();
- $aFiles = $this->convertImagesArray($this->getFiles($aSystem['system_id'], false, $iObjectId));
- if ($aFiles) {
- $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles2Entries} WHERE `system_id` = ? AND `image_id` IN(" . $this->implode_escape($aFiles) . ")", $aSystem['system_id']);
- $this->query($sQuery);
- }
- if (null !== $aCmtIds) {
- $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
- $aCmtIds = $this->getColumn ($sQuery);
- }
- $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
- $this->query ($sQuery);
- $this->query ("OPTIMIZE TABLE {$this->_sTable}");
- if (null !== $aFilesReturn)
- $aFilesReturn = $aFiles;
- }
- function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
- {
- // get files
- if (null !== $aFiles)
- $aFiles = $this->convertImagesArray($this->getFiles($iSystemId, false));
- // delete files
- $this->deleteImages($iSystemId, false);
- if (null !== $aCmtIds)
- $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
- // delete comments
- $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
- $this->query ($sQuery);
- }
- function deleteCmtIds ($iSystemId, $iCmtId)
- {
- $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
- return $this->query ($sQuery);
- }
- function getObjectAuthorId($iId)
- {
- $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
- 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 getUniqId($iSystemId, $iCmtId)
- {
- $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
- if ($iUniqId = $this->getOne($sQuery))
- return $iUniqId;
-
- $sQuery = $this->prepare("INSERT INTO `{$this->_sTableIds}` SET `system_id` = ?, `cmt_id` = ?", $iSystemId, $iCmtId);
- if (!$this->query($sQuery))
- return false;
- return $this->lastId();
- }
- protected function convertImagesArray($a)
- {
- if (!$a || !is_array($a))
- return array();
- $aFiles = array ();
- foreach ($a as $aFile)
- $aFiles[] = $aFile['image_id'];
- return $aFiles;
- }
- }
- /** @} */
|