<?php
class ARTICLES_BOL_ArticleDao extends OW_BaseDao{ const CACHE_TAG_ARTICLE_COUNT = 'articles.article_count'; const CACHE_LIFE_TIME = 86400; //24 hour
/** * Class constructor * */ protected function __construct() { parent::__construct(); } /** * Class instance * * @var ARTICLES_BOL_ArticleDao */ private static $classInstance;
/** * Returns class instance * * @return ARTICLES_BOL_ArticleDao */ public static function getInstance() { if ( self::$classInstance === null ) { self::$classInstance = new self(); }
return self::$classInstance; }
/** * @see OW_BaseDao::getDtoClassName() * */ public function getDtoClassName() { return 'ARTICLES_BOL_Article'; }
/** * @see OW_BaseDao::getTableName() * */ public function getTableName() { return OW_DB_PREFIX . 'articles_article'; }
public function findAdjacentUserArticle( $id, $articleId, $which ) { $part = array();
switch ( $which ) { case 'next': $part['projection'] = 'MIN(`id`)'; $part['inequality'] = '>'; break;
case 'prev': $part['projection'] = 'MAX(`id`)'; $part['inequality'] = '<'; break; }
$query = "SELECT {$part['projection']}FROM {$this->getTableName()}WHERE isDraft = 0 AND authorId = ? AND id {$part['inequality']} ?";
$id = $this->dbo->queryForColumn($query, array($id, $articleId));
return (!empty($id)) ? $this->findById($id) : null; }
public function deleteByAuthorId( $userId ) { $ex = new OW_Example(); $ex->andFieldEqual('authorId', $userId);
$this->deleteByExample($ex); }
public function findUserArticleList( $userId, $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$ex = new OW_Example(); $ex->andFieldEqual('authorId', $userId) ->setOrder('`timestamp` DESC') ->andFieldEqual('isDraft', 0) ->setLimitClause($first, $count);
$cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->findListByExample($ex, $cacheLifeTime, $tags); }
public function findUserDraftList( $userId, $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$ex = new OW_Example(); $ex->andFieldEqual('authorId', $userId) ->andFieldNotEqual('isDraft', 0) ->setOrder('`timestamp` DESC') ->setLimitClause($first, $count);
$cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->findListByExample($ex, $cacheLifeTime, $tags); }
public function countUserArticle( $userId ) { $ex = new OW_Example(); $ex->andFieldEqual('authorId', $userId); $ex->andFieldEqual('isDraft', 0);
$cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->countByExample($ex,$cacheLifeTime, $tags); } public function countCategoryArticle( $catId ) { $ex = new OW_Example(); $ex->andFieldEqual('categoryId', $catId); $ex->andFieldEqual('isDraft', 0);
$cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->countByExample($ex,$cacheLifeTime, $tags); }
public function countUserDraft( $userId ) { $ex = new OW_Example(); $ex->andFieldEqual('authorId', $userId); $ex->andFieldNotEqual('isDraft', 0); $ex->andFieldNotEqual('isDraft', 3);
$cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->countByExample($ex, $cacheLifeTime, $tags); }
public function countArticles($optionalParmams = array()) { $ex = new OW_Example(); $ex->andFieldEqual('isDraft', 0); $ex->andFieldEqual('privacy', 'everybody'); foreach ($optionalParmams as $key => $value){ $ex->andFieldEqual($key, $value); } $cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->countByExample($ex, $cacheLifeTime, $tags); }
public function countUserArticleComment( $userId ) { $query = "SELECT COUNT(*)FROM `{$this->getTableName()}` as `p`INNER JOIN `" . BOL_CommentEntityDao::getInstance()->getTableName() . "` as `ce`ON( `p`.`id` = `ce`.`entityId` and `entityType` = 'article-article' )INNER JOIN `" . BOL_CommentDao::getInstance()->getTableName() . "` as `c`ON( `ce`.`id` = `c`.`commentEntityId` )
WHERE `p`.`authorId` = ? AND `p`.`isDraft` = 0";
return $this->dbo->queryForColumn($query, array($userId)); }
public function countUserArticleNewComment( $userId ) { $query = "SELECT COUNT(*)FROM `{$this->getTableName()}` as `p`INNER JOIN `" . BOL_CommentEntityDao::getInstance()->getTableName() . "` as `ce`ON( `p`.`id` = `ce`.`entityId` and `entityType` = 'article-article' )INNER JOIN `" . BOL_CommentDao::getInstance()->getTableName() . "` as `c`ON( `ce`.`id` = `c`.`commentEntityId` )
WHERE `p`.`authorId` = ? AND `p`.`isDraft` = 0 AND `c`.`createStamp` > ".(time()-86400*7)."";
return $this->dbo->queryForColumn($query, array($userId)); }
public function findUserArticleCommentList( $userId, $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$query = "SELECT `c`.*, `ce`.`entityId`FROM `{$this->getTableName()}` as `p`INNER JOIN `" . BOL_CommentEntityDao::getInstance()->getTableName() . "` as `ce`ON( `p`.`id` = `ce`.`entityId` and `entityType` = 'article-article' )INNER JOIN `" . BOL_CommentDao::getInstance()->getTableName() . "` as `c`ON( `ce`.`id` = `c`.`commentEntityId` )
WHERE `p`.`authorId` = ? AND `p`.`isDraft` = 0ORDER BY `c`.`createStamp` DESCLIMIT ?, ?";
return $this->dbo->queryForList($query, array($userId, $first, $count)); }
public function findUserLastArticle( $userId ) { $ex = new OW_Example(); $ex->andFieldEqual('authorId', $userId)->andFieldEqual('isDraft', 0)->setOrder('timestamp DESC')->setLimitClause(0, 1);
return $this->findObjectByExample($ex); }
public function findUserArchiveData( $id ){ $query = " SELECT YEAR( FROM_UNIXTIME(`timestamp`) ) as `y`, MONTH( FROM_UNIXTIME(`timestamp`) ) as `m` FROM `{$this->getTableName()}` WHERE isDraft = 0 AND `authorId` = ? GROUP BY `y`, `m` ORDER BY `y` DESC, `m` DESC ";
return $this->dbo->queryForList($query, array($id));}
public function findUserArticleListByPeriod( $id, $lb, $ub, $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$ex = new OW_Example(); $ex->andFieldEqual('authorId', $id);
$ex->andFieldBetween('timestamp', $lb, $ub); $ex->andFieldEqual('isDraft', 0); $ex->setOrder('`timestamp` DESC'); $ex->setLimitClause($first, $count);
return $this->findListByExample($ex); }
public function countUserArticleByPeriod( $id, $lb, $ub ) { $ex = new OW_Example(); $ex->andFieldEqual('authorId', $id); $ex->andFieldBetween('timestamp', $lb, $ub); $ex->andFieldEqual('isDraft', 0); $ex->setOrder('`timestamp` DESC');
return $this->countByExample($ex); }
public function findList( $first, $count, $optionalParmams = array(), $order ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$ex = new OW_Example(); $ex->andFieldEqual('isDraft', 0); $ex->andFieldEqual('privacy', 'everybody'); foreach ($optionalParmams as $key => $value){ $ex->andFieldEqual($key, $value); } $ex->setOrder($order)->setLimitClause($first, $count);
$cacheLifeTime = self::CACHE_LIFE_TIME; $tags = array( self::CACHE_TAG_ARTICLE_COUNT );
return $this->findListByExample($ex, $cacheLifeTime, $tags); }
public function findTopRatedList( $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$query = "SELECT p.*, IF(SUM(r.score) IS NOT NULL, SUM(r.score), 0) as `t`FROM `{$this->getTableName()}` as pLEFT JOIN `". OW_DB_PREFIX ."base_rate` as r /*todo: 8aa*/ON( r.`entityType` = 'article-article' AND p.id = r.`entityId` )WHERE p.isDraft = 0GROUP BY p.`id`ORDER BY `t` DESCLIMIT ?, ?";
return $this->dbo->queryForObjectList($query, $this->getDtoClassName(), array($first, $count)); }
public function findListByTag( $tag, $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }
$query = "SELECT p.*FROM `". OW_DB_PREFIX ."base_tag` as tINNER JOIN `". OW_DB_PREFIX ."base_entity_tag` as `et`ON(`t`.`id` = `et`.`tagId` AND `et`.`entityType` = 'article-article')INNER JOIN `{$this->getTableName()}` as pON(`et`.`entityId` = `p`.`id`)WHERE p.isDraft = 0 AND `t`.`label` = '{$tag}'ORDER BY `t`.`id` DESCLIMIT ?, ?";
return $this->dbo->queryForObjectList($query, $this->getDtoClassName(), array($first, $count)); } public function findListByTags( $tags, $first, $count ) { if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; } $query = "SELECT DISTINCT p.*FROM `". OW_DB_PREFIX ."base_tag` as tINNER JOIN `". OW_DB_PREFIX ."base_entity_tag` as `et`ON(`t`.`id` = `et`.`tagId` AND `et`.`entityType` = 'article-article')INNER JOIN `{$this->getTableName()}` as pON(`et`.`entityId` = `p`.`id`)WHERE p.isDraft = 0 AND `t`.`label` in ({$tags})ORDER BY `t`.`id`LIMIT ?, ?"; return $this->dbo->queryForObjectList($query, $this->getDtoClassName(), array($first, $count)); }
public function countByTag( $tag ) { $query = "SELECT count( * )FROM `". OW_DB_PREFIX ."base_tag` as tINNER JOIN `". OW_DB_PREFIX ."base_entity_tag` as `et`ON(`t`.`id` = `et`.`tagId` AND `et`.`entityType` = 'article-article')INNER JOIN `{$this->getTableName()}` as pON(`et`.`entityId` = `p`.`id`)WHERE p.isDraft = 0 AND `t`.`label` = '{$tag}'";
return $this->dbo->queryForColumn($query); }
public function findListByIdList( $list ) { $ex = new OW_Example();
$ex->andFieldInArray('id', $list); $ex->andFieldEqual('privacy', 'everybody');
$ex->setOrder('timestamp DESC');
return $this->findListByExample($ex); }
public function updateArticlesPrivacy( $authorId, $privacy ) { $this->clearCache();
$sql = "UPDATE `" . $this->getTableName() . "` SET `privacy` = :privacy WHERE `authorId` = :authorId";
$this->dbo->query($sql, array('privacy' => $privacy, 'authorId' => $authorId)); }public function getFeaturedArticleList($first, $count) {if ($first < 0) { $first = 0; }
if ($count < 0) { $count = 1; }$query = 'SELECT `a`.* FROM `' . $this->getTableName() . '` AS `a` INNER JOIN `' . ARTICLES_BOL_ArticleFeaturedDao::getInstance()->getTableName() . '` AS `f` ON (`f`.`articleId`=`a`.`id`) ORDER BY `a`.`id` DESC LIMIT ?, ?';return $this->dbo->queryForObjectList($query, $this->getDtoClassName(), array($first, $count));} public function clearCache() { OW::getCacheManager()->clean( array( ARTICLES_BOL_ArticleDao::CACHE_TAG_ARTICLE_COUNT )); }}