1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-16 21:34:25 +03:00
ifhub.club/classes/modules/topic/mapper/Topic.mapper.class.php
2008-09-21 06:36:57 +00:00

614 lines
15 KiB
PHP

<?
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
class Mapper_Topic extends Mapper {
protected $oUserCurrent=null;
public function SetUserCurrent($oUserCurrent) {
$this->oUserCurrent=$oUserCurrent;
}
public function AddTopic(TopicEntity_Topic $oTopic) {
$sql = "INSERT INTO ".DB_TABLE_TOPIC."
(blog_id,
user_id,
topic_type,
topic_title,
topic_text,
topic_text_short,
topic_text_source,
topic_tags,
topic_date_add,
topic_user_ip,
topic_publish
)
VALUES(?d, ?d, ?, ?, ?, ?, ?, ?, ?, ?, ?d)
";
if ($iId=$this->oDb->query($sql,$oTopic->getBlogId(),$oTopic->getUserId(),$oTopic->getType(),$oTopic->getTitle(),$oTopic->getText(),
$oTopic->getTextShort(),$oTopic->getTextSource(),$oTopic->getTags(),$oTopic->getDateAdd(),$oTopic->getUserIp(),$oTopic->getPublish()))
{
return $iId;
}
return false;
}
public function AddTopicTag(TopicEntity_TopicTag $oTopicTag) {
$sql = "INSERT INTO ".DB_TABLE_TOPIC_TAG."
(topic_id,
user_id,
blog_id,
topic_tag_text
)
VALUES(?d, ?d, ?d, ?)
";
if ($iId=$this->oDb->query($sql,$oTopicTag->getTopicId(),$oTopicTag->getUserId(),$oTopicTag->getBlogId(),$oTopicTag->getText()))
{
return $iId;
}
return false;
}
public function AddTopicVote(TopicEntity_TopicVote $oTopicVote) {
$sql = "INSERT INTO ".DB_TABLE_TOPIC_VOTE."
(topic_id,
user_voter_id,
vote_delta
)
VALUES(?d, ?d, ?f)
";
if ($this->oDb->query($sql,$oTopicVote->getTopicId(),$oTopicVote->getVoterId(),$oTopicVote->getDelta())===0)
{
return true;
}
return false;
}
public function DeleteTopicTagsByTopicId($sTopicId) {
$sql = "DELETE FROM ".DB_TABLE_TOPIC_TAG."
WHERE
topic_id = ?d
";
if ($this->oDb->query($sql,$sTopicId)) {
return true;
}
return false;
}
public function GetTopicById($sId,$oUser,$iPublish) {
$iCurrentUserId=-1;
if (is_object($this->oUserCurrent)) {
$iCurrentUserId=$this->oUserCurrent->getId();
}
$sWhereUser='';
if ($oUser) {
$sWhereUser=' OR t.user_id = '.(int)$oUser->getId();
}
$sql = "SELECT
t.*,
u.user_login as user_login,
b.blog_type as blog_type,
b.blog_url as blog_url,
b.blog_title as blog_title,
IF(tv.topic_id IS NULL,0,1) as user_is_vote,
tv.vote_delta as user_vote_delta
FROM
".DB_TABLE_TOPIC." as t
LEFT JOIN (
SELECT
topic_id,
vote_delta
FROM ".DB_TABLE_TOPIC_VOTE."
WHERE user_voter_id = ?d
) AS tv ON tv.topic_id = t.topic_id,
".DB_TABLE_USER." as u,
".DB_TABLE_BLOG." as b
WHERE
t.topic_id = ?d
AND
(
t.topic_publish = ?d
".$sWhereUser."
)
AND
t.user_id=u.user_id
AND
t.blog_id=b.blog_id
";
if ($aRow=$this->oDb->selectRow($sql,$iCurrentUserId,$sId,$iPublish)) {
return new TopicEntity_Topic($aRow);
}
return null;
}
public function GetTopics($aFilter,&$iCount,$iCurrPage,$iPerPage) {
$iCurrentUserId=-1;
if (is_object($this->oUserCurrent)) {
$iCurrentUserId=$this->oUserCurrent->getId();
}
$sWhere=$this->buildFilter($aFilter);
$sql = "SELECT
t.*,
u.user_login as user_login,
b.blog_title as blog_title,
b.blog_type as blog_type,
b.blog_url as blog_url,
IF(tv.topic_id IS NULL,0,1) as user_is_vote,
tv.vote_delta as user_vote_delta
FROM
".DB_TABLE_TOPIC." as t
LEFT JOIN (
SELECT
topic_id,
vote_delta
FROM ".DB_TABLE_TOPIC_VOTE."
WHERE user_voter_id = ?d
) AS tv ON tv.topic_id = t.topic_id,
".DB_TABLE_USER." as u,
".DB_TABLE_BLOG." as b
WHERE
1=1
".$sWhere."
AND
t.blog_id=b.blog_id
AND
t.user_id=u.user_id
ORDER by t.topic_date_add desc
LIMIT ?d, ?d
;
";
$aTopics=array();
if ($aRows=$this->oDb->selectPage($iCount,$sql,$iCurrentUserId,($iCurrPage-1)*$iPerPage, $iPerPage)) {
foreach ($aRows as $aTopic) {
$aTopics[]=new TopicEntity_Topic($aTopic);
}
}
return $aTopics;
}
public function GetCountTopics($aFilter) {
$sWhere=$this->buildFilter($aFilter);
$sql = "SELECT
count(t.topic_id) as count
FROM
".DB_TABLE_TOPIC." as t,
".DB_TABLE_USER." as u,
".DB_TABLE_BLOG." as b
WHERE
1=1
".$sWhere."
AND
t.blog_id=b.blog_id
AND
t.user_id=u.user_id
;
";
if ($aRow=$this->oDb->selectRow($sql)) {
return $aRow['count'];
}
return false;
}
public function GetTopicsByTag($sTag,&$iCount,$iCurrPage,$iPerPage) {
$iCurrentUserId=-1;
if (is_object($this->oUserCurrent)) {
$iCurrentUserId=$this->oUserCurrent->getId();
}
$sql = "SELECT
t.*,
u.user_login as user_login,
b.blog_title as blog_title,
b.blog_type as blog_type,
b.blog_url as blog_url,
IF(tv.topic_id IS NULL,0,1) as user_is_vote,
tv.vote_delta as user_vote_delta
FROM
".DB_TABLE_TOPIC_TAG." as tt,
".DB_TABLE_TOPIC." as t
LEFT JOIN (
SELECT
topic_id,
vote_delta
FROM ".DB_TABLE_TOPIC_VOTE."
WHERE user_voter_id = ?d
) AS tv ON tv.topic_id = t.topic_id,
".DB_TABLE_USER." as u,
".DB_TABLE_BLOG." as b
WHERE
tt.topic_tag_text = ?
AND
t.topic_id = tt.topic_id
AND
t.topic_publish = 1
AND
b.blog_type in ('personal','open')
AND
t.blog_id=b.blog_id
AND
t.user_id=u.user_id
ORDER by t.topic_date_add desc
LIMIT ?d, ?d
;
";
$aTopics=array();
if ($aRows=$this->oDb->selectPage($iCount,$sql,$iCurrentUserId,$sTag,($iCurrPage-1)*$iPerPage, $iPerPage)) {
foreach ($aRows as $aTopic) {
$aTopics[]=new TopicEntity_Topic($aTopic);
}
}
return $aTopics;
}
public function GetTopicsRatingByDate($sDate,$iLimit) {
$iCurrentUserId=-1;
if (is_object($this->oUserCurrent)) {
$iCurrentUserId=$this->oUserCurrent->getId();
}
$sql = "SELECT
t.*,
u.user_login as user_login,
b.blog_title as blog_title,
b.blog_type as blog_type,
b.blog_url as blog_url,
IF(tv.topic_id IS NULL,0,1) as user_is_vote,
tv.vote_delta as user_vote_delta
FROM
".DB_TABLE_TOPIC." as t
LEFT JOIN (
SELECT
topic_id,
vote_delta
FROM ".DB_TABLE_TOPIC_VOTE."
WHERE user_voter_id = ?d
) AS tv ON tv.topic_id = t.topic_id,
".DB_TABLE_USER." as u,
".DB_TABLE_BLOG." as b
WHERE
t.topic_date_add >= ?
AND
t.topic_publish = 1
AND
b.blog_type in ('personal','open')
AND
t.topic_rating >= 0
AND
t.blog_id=b.blog_id
AND
t.user_id=u.user_id
ORDER by t.topic_rating desc, t.topic_date_add desc
LIMIT 0, ?d ;
";
$aTopics=array();
if ($aRows=$this->oDb->select($sql,$iCurrentUserId,$sDate,$iLimit)) {
foreach ($aRows as $aTopic) {
$aTopics[]=new TopicEntity_Topic($aTopic);
}
}
return $aTopics;
}
public function GetTopicTags($iLimit) {
$sql = "SELECT
tt.topic_tag_text,
count(tt.topic_tag_text) as count
FROM
".DB_TABLE_TOPIC_TAG." as tt,
".DB_TABLE_TOPIC." as t
WHERE
t.topic_id=tt.topic_id
AND
t.topic_publish = 1
GROUP BY
tt.topic_tag_text
ORDER BY
count desc
LIMIT 0, ?d
";
$aReturn=array();
$aReturnSort=array();
if ($aRows=$this->oDb->select($sql,$iLimit)) {
foreach ($aRows as $aRow) {
$aReturn[$aRow['topic_tag_text']]=$aRow;
}
ksort($aReturn);
foreach ($aReturn as $aRow) {
$aReturnSort[]=new TopicEntity_TopicTag($aRow);
}
}
return $aReturnSort;
}
public function GetTopicTagsByUserId($sUserId,$iLimit) {
$sql = "SELECT
topic_tag_text,
count(topic_tag_text) as count
FROM
".DB_TABLE_TOPIC_TAG."
WHERE
user_id = ?
GROUP BY
topic_tag_text
ORDER BY
topic_tag_text ASC
LIMIT 0, ?d
";
$aReturn=array();
if ($aRows=$this->oDb->select($sql,$sUserId,$iLimit)) {
foreach ($aRows as $aRow) {
$aReturn[]=new TopicEntity_TopicTag($aRow);
}
}
return $aReturn;
}
public function GetTopicVote($sTopicId,$sUserId) {
$sql = "SELECT * FROM ".DB_TABLE_TOPIC_VOTE." WHERE topic_id = ?d and user_voter_id = ?d ";
if ($aRow=$this->oDb->selectRow($sql,$sTopicId,$sUserId)) {
return new TopicEntity_TopicVote($aRow);
}
return null;
}
public function increaseTopicCountComment($sTopicId) {
$sql = "UPDATE ".DB_TABLE_TOPIC."
SET
topic_count_comment=topic_count_comment+1
WHERE
topic_id = ?
";
if ($this->oDb->query($sql,$sTopicId)) {
return true;
}
return false;
}
public function UpdateTopic(TopicEntity_Topic $oTopic) {
$sql = "UPDATE ".DB_TABLE_TOPIC."
SET
blog_id= ?d,
topic_title= ?,
topic_text= ?,
topic_text_short= ?,
topic_text_source= ?,
topic_tags= ?,
topic_date_edit = ?,
topic_user_ip= ?,
topic_publish= ? ,
topic_rating= ?f,
topic_count_vote= ?d,
topic_count_read= ?d,
topic_count_comment= ?d
WHERE
topic_id = ?d
";
if ($this->oDb->query($sql,$oTopic->getBlogId(),$oTopic->getTitle(),$oTopic->getText(),$oTopic->getTextShort(),$oTopic->getTextSource(),$oTopic->getTags(),$oTopic->getDateEdit(),$oTopic->getUserIp(),$oTopic->getPublish(),$oTopic->getRating(),$oTopic->getCountVote(),$oTopic->getCountRead(),$oTopic->getCountComment(),$oTopic->getId())) {
return true;
}
return false;
}
protected function buildFilter($aFilter) {
$sWhere='';
if (isset($aFilter['blog_type']) and is_array($aFilter['blog_type'])) {
$sWhere.=" AND b.blog_type in ('".join("','",$aFilter['blog_type'])."') ";
}
if (isset($aFilter['topic_publish'])) {
$sWhere.=" AND t.topic_publish = ".(int)$aFilter['topic_publish'];
}
if (isset($aFilter['user_id'])) {
$sWhere.=" AND t.user_id = ".(int)$aFilter['user_id'];
}
if (isset($aFilter['blog_id'])) {
$sWhere.=" AND t.blog_id = ".(int)$aFilter['blog_id'];
}
if (isset($aFilter['topic_new'])) {
$sWhere.=" AND t.topic_date_add >= '".$aFilter['topic_new']."'";
}
if (isset($aFilter['topic_rating']) and is_array($aFilter['topic_rating'])) {
if ($aFilter['topic_rating']['type']=='top') {
$sWhere.=" AND t.topic_rating >= ".(float)$aFilter['topic_rating']['value'];
} else {
$sWhere.=" AND t.topic_rating < ".(float)$aFilter['topic_rating']['value'];
}
}
return $sWhere;
}
public function AddFavouriteTopic(TopicEntity_FavouriteTopic $oFavouriteTopic) {
$sql = "INSERT INTO ".DB_TABLE_FAVOURITE_TOPIC."
(user_id,
topic_id
)
VALUES(?d, ?d)
";
if ($this->oDb->query($sql,$oFavouriteTopic->getUserId(),$oFavouriteTopic->getTopicId())===0)
{
return true;
}
return false;
}
public function DeleteFavouriteTopic(TopicEntity_FavouriteTopic $oFavouriteTopic) {
$sql = "DELETE FROM ".DB_TABLE_FAVOURITE_TOPIC."
WHERE
user_id = ?d
AND
topic_id = ?d
";
if ($this->oDb->query($sql,$oFavouriteTopic->getUserId(),$oFavouriteTopic->getTopicId()))
{
return true;
}
return false;
}
public function GetFavouriteTopic($sTopicId,$sUserId) {
$sql = "SELECT * FROM ".DB_TABLE_FAVOURITE_TOPIC." WHERE topic_id = ?d and user_id = ?d ";
if ($aRow=$this->oDb->selectRow($sql,$sTopicId,$sUserId)) {
return new TopicEntity_FavouriteTopic($aRow);
}
return null;
}
public function GetTopicsFavouriteByUserId($sUserId,&$iCount,$iCurrPage,$iPerPage) {
$iCurrentUserId=-1;
if (is_object($this->oUserCurrent)) {
$iCurrentUserId=$this->oUserCurrent->getId();
}
$sql = "SELECT
t.*,
u.user_login as user_login,
b.blog_title as blog_title,
b.blog_type as blog_type,
b.blog_url as blog_url,
IF(tv.topic_id IS NULL,0,1) as user_is_vote,
tv.vote_delta as user_vote_delta
FROM
".DB_TABLE_FAVOURITE_TOPIC." as ft,
".DB_TABLE_TOPIC." as t
LEFT JOIN (
SELECT
topic_id,
vote_delta
FROM ".DB_TABLE_TOPIC_VOTE."
WHERE user_voter_id = ?d
) AS tv ON tv.topic_id = t.topic_id,
".DB_TABLE_USER." as u,
".DB_TABLE_BLOG." as b
WHERE
ft.user_id = ?
AND
ft.topic_id=t.topic_id
AND
t.topic_publish = 1
AND
b.blog_type in ('personal','open')
AND
t.blog_id=b.blog_id
AND
t.user_id=u.user_id
ORDER BY t.topic_date_add desc
LIMIT ?d, ?d ;
";
$aTopics=array();
if ($aRows=$this->oDb->selectPage($iCount,$sql,$iCurrentUserId,$sUserId,($iCurrPage-1)*$iPerPage, $iPerPage)) {
foreach ($aRows as $aTopic) {
$aTopics[]=new TopicEntity_Topic($aTopic);
}
}
return $aTopics;
}
public function GetCountTopicsFavouriteByUserId($sUserId) {
$sql = "SELECT
count(t.topic_id) as count
FROM
".DB_TABLE_FAVOURITE_TOPIC." as ft,
".DB_TABLE_TOPIC." as t
WHERE
ft.user_id = ?
AND
ft.topic_id=t.topic_id
AND
t.topic_publish = 1;
";
if ($aRow=$this->oDb->selectRow($sql,$sUserId)) {
return $aRow['count'];
}
return false;
}
public function GetTopicTagsByLike($sTag,$iLimit) {
$sql = "SELECT
topic_tag_text
FROM
".DB_TABLE_TOPIC_TAG."
WHERE
topic_tag_text LIKE ?
GROUP BY
topic_tag_text
LIMIT 0, ?d
";
$aReturn=array();
if ($aRows=$this->oDb->select($sql,$sTag.'%',$iLimit)) {
foreach ($aRows as $aRow) {
$aReturn[]=new TopicEntity_TopicTag($aRow);
}
}
return $aReturn;
}
public function SetDateRead($sTopicId,$sUserId) {
$sDate=date("Y-m-d H:i:s");
$sql = "UPDATE ".DB_TABLE_TOPIC_READ."
SET date_read = ?
WHERE
topic_id = ?
and
user_id = ?
";
return $this->oDb->query($sql,$sDate,$sTopicId,$sUserId);
}
public function AddTopicRead($sTopicId,$sUserId) {
$sDate=date("Y-m-d H:i:s");
$sql = "INSERT INTO ".DB_TABLE_TOPIC_READ."
(topic_id,
user_id,
date_read
)
VALUES(?d, ?d, ?)
";
if ($this->oDb->query($sql,$sTopicId,$sUserId,$sDate)===0)
{
return true;
}
return false;
}
public function GetDateRead($sTopicId,$sUserId) {
$sql = "SELECT
date_read
FROM
".DB_TABLE_TOPIC_READ."
WHERE
topic_id = ?d
AND
user_id = ?d
;
";
if ($aRow=$this->oDb->selectRow($sql,$sTopicId,$sUserId)) {
return $aRow['date_read'];
}
return false;
}
}
?>