PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ow_plugins/blogs/bol/dao/post_dao.php

https://bitbucket.org/Noelfhim/no_ftp
PHP | 438 lines | 304 code | 79 blank | 55 comment | 16 complexity | ab90293922827fa6cb5d104516bc1be7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This software is intended for use with Oxwall Free Community Software http://www.oxwall.org/ and is
  4. * licensed under The BSD license.
  5. * ---
  6. * Copyright (c) 2011, Oxwall Foundation
  7. * All rights reserved.
  8. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  9. * following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice, this list of conditions and
  12. * the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
  15. * the following disclaimer in the documentation and/or other materials provided with the distribution.
  16. *
  17. * - Neither the name of the Oxwall Foundation nor the names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  24. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /**
  28. * @author Zarif Safiullin <zaph.saph@gmail.com>
  29. * @package ow_plugins.blogs.bol.dao
  30. * @since 1.0
  31. */
  32. class PostDao extends OW_BaseDao
  33. {
  34. const CACHE_TAG_POST_COUNT = 'blogs.post_count';
  35. const CACHE_LIFE_TIME = 86400; //24 hour
  36. /**
  37. * Class constructor
  38. *
  39. */
  40. protected function __construct()
  41. {
  42. parent::__construct();
  43. }
  44. /**
  45. * Class instance
  46. *
  47. * @var PostDao
  48. */
  49. private static $classInstance;
  50. /**
  51. * Returns class instance
  52. *
  53. * @return PostDao
  54. */
  55. public static function getInstance()
  56. {
  57. if ( self::$classInstance === null )
  58. {
  59. self::$classInstance = new self();
  60. }
  61. return self::$classInstance;
  62. }
  63. /**
  64. * @see OW_BaseDao::getDtoClassName()
  65. *
  66. */
  67. public function getDtoClassName()
  68. {
  69. return 'Post';
  70. }
  71. /**
  72. * @see OW_BaseDao::getTableName()
  73. *
  74. */
  75. public function getTableName()
  76. {
  77. return OW_DB_PREFIX . 'blogs_post';
  78. }
  79. public function findAdjacentUserPost( $id, $postId, $which )
  80. {
  81. $part = array();
  82. switch ( $which )
  83. {
  84. case 'next':
  85. $part['projection'] = 'MIN(`id`)';
  86. $part['inequality'] = '>';
  87. break;
  88. case 'prev':
  89. $part['projection'] = 'MAX(`id`)';
  90. $part['inequality'] = '<';
  91. break;
  92. }
  93. $query = "
  94. SELECT {$part['projection']}
  95. FROM {$this->getTableName()}
  96. WHERE isDraft = 0 AND authorId = ? AND id {$part['inequality']} ?
  97. ";
  98. $id = $this->dbo->queryForColumn($query, array($id, $postId));
  99. return (!empty($id)) ? $this->findById($id) : null;
  100. }
  101. public function deleteByAuthorId( $userId )
  102. {
  103. $ex = new OW_Example();
  104. $ex->andFieldEqual('authorId', $userId);
  105. $this->deleteByExample($ex);
  106. }
  107. public function findUserPostList( $userId, $first, $count )
  108. {
  109. if ($first < 0)
  110. {
  111. $first = 0;
  112. }
  113. if ($count < 0)
  114. {
  115. $count = 1;
  116. }
  117. $ex = new OW_Example();
  118. $ex->andFieldEqual('authorId', $userId)
  119. ->setOrder('`timestamp` DESC')
  120. ->andFieldEqual('isDraft', 0)
  121. ->setLimitClause($first, $count);
  122. $cacheLifeTime = self::CACHE_LIFE_TIME;
  123. $tags = array( self::CACHE_TAG_POST_COUNT );
  124. return $this->findListByExample($ex, $cacheLifeTime, $tags);
  125. }
  126. public function findUserDraftList( $userId, $first, $count )
  127. {
  128. if ($first < 0)
  129. {
  130. $first = 0;
  131. }
  132. if ($count < 0)
  133. {
  134. $count = 1;
  135. }
  136. $ex = new OW_Example();
  137. $ex->andFieldEqual('authorId', $userId)
  138. ->andFieldNotEqual('isDraft', 0)
  139. ->setOrder('`timestamp` DESC')
  140. ->setLimitClause($first, $count);
  141. $cacheLifeTime = self::CACHE_LIFE_TIME;
  142. $tags = array( self::CACHE_TAG_POST_COUNT );
  143. return $this->findListByExample($ex, $cacheLifeTime, $tags);
  144. }
  145. public function countUserPost( $userId )
  146. {
  147. $ex = new OW_Example();
  148. $ex->andFieldEqual('authorId', $userId);
  149. $ex->andFieldEqual('isDraft', 0);
  150. $cacheLifeTime = self::CACHE_LIFE_TIME;
  151. $tags = array( self::CACHE_TAG_POST_COUNT );
  152. return $this->countByExample($ex,$cacheLifeTime, $tags);
  153. }
  154. public function countUserDraft( $userId )
  155. {
  156. $ex = new OW_Example();
  157. $ex->andFieldEqual('authorId', $userId);
  158. $ex->andFieldNotEqual('isDraft', 0);
  159. $cacheLifeTime = self::CACHE_LIFE_TIME;
  160. $tags = array( self::CACHE_TAG_POST_COUNT );
  161. return $this->countByExample($ex, $cacheLifeTime, $tags);
  162. }
  163. public function countPosts()
  164. {
  165. $ex = new OW_Example();
  166. $ex->andFieldEqual('isDraft', 0);
  167. $ex->andFieldEqual('privacy', 'everybody');
  168. $cacheLifeTime = self::CACHE_LIFE_TIME;
  169. $tags = array( self::CACHE_TAG_POST_COUNT );
  170. return $this->countByExample($ex, $cacheLifeTime, $tags);
  171. }
  172. public function countUserPostComment( $userId )
  173. {
  174. $query = "
  175. SELECT COUNT(*)
  176. FROM `{$this->getTableName()}` as `p`
  177. INNER JOIN `" . BOL_CommentEntityDao::getInstance()->getTableName() . "` as `ce`
  178. ON( `p`.`id` = `ce`.`entityId` and `entityType` = 'blog-post' )
  179. INNER JOIN `" . BOL_CommentDao::getInstance()->getTableName() . "` as `c`
  180. ON( `ce`.`id` = `c`.`commentEntityId` )
  181. WHERE `p`.`authorId` = ? AND `p`.`isDraft` = 0
  182. ";
  183. return $this->dbo->queryForColumn($query, array($userId));
  184. }
  185. public function countUserPostNewComment( $userId )
  186. {
  187. $query = "
  188. SELECT COUNT(*)
  189. FROM `{$this->getTableName()}` as `p`
  190. INNER JOIN `" . BOL_CommentEntityDao::getInstance()->getTableName() . "` as `ce`
  191. ON( `p`.`id` = `ce`.`entityId` and `entityType` = 'blog-post' )
  192. INNER JOIN `" . BOL_CommentDao::getInstance()->getTableName() . "` as `c`
  193. ON( `ce`.`id` = `c`.`commentEntityId` )
  194. WHERE `p`.`authorId` = ? AND `p`.`isDraft` = 0 AND `c`.`createStamp` > ".(time()-86400*7)."
  195. ";
  196. return $this->dbo->queryForColumn($query, array($userId));
  197. }
  198. public function findUserPostCommentList( $userId, $first, $count )
  199. {
  200. if ($first < 0)
  201. {
  202. $first = 0;
  203. }
  204. if ($count < 0)
  205. {
  206. $count = 1;
  207. }
  208. $query = "
  209. SELECT `c`.*, `ce`.`entityId`
  210. FROM `{$this->getTableName()}` as `p`
  211. INNER JOIN `" . BOL_CommentEntityDao::getInstance()->getTableName() . "` as `ce`
  212. ON( `p`.`id` = `ce`.`entityId` and `entityType` = 'blog-post' )
  213. INNER JOIN `" . BOL_CommentDao::getInstance()->getTableName() . "` as `c`
  214. ON( `ce`.`id` = `c`.`commentEntityId` )
  215. WHERE `p`.`authorId` = ? AND `p`.`isDraft` = 0
  216. ORDER BY `c`.`createStamp` DESC
  217. LIMIT ?, ?
  218. ";
  219. return $this->dbo->queryForList($query, array($userId, $first, $count));
  220. }
  221. public function findUserLastPost( $userId )
  222. {
  223. $ex = new OW_Example();
  224. $ex->andFieldEqual('authorId', $userId)->andFieldEqual('isDraft', 0)->setOrder('timestamp DESC')->setLimitClause(0, 1);
  225. return $this->findObjectByExample($ex);
  226. }
  227. public function findUserArchiveData( $id )
  228. {
  229. $query = "
  230. SELECT YEAR( FROM_UNIXTIME(`timestamp`) ) as `y`, MONTH( FROM_UNIXTIME(`timestamp`) ) as `m`
  231. FROM `{$this->getTableName()}`
  232. WHERE isDraft = 0 AND `authorId` = ?
  233. GROUP BY `y` ASC, `m` DESC
  234. ";
  235. return $this->dbo->queryForList($query, array($id));
  236. }
  237. public function findUserPostListByPeriod( $id, $lb, $ub, $first, $count )
  238. {
  239. if ($first < 0)
  240. {
  241. $first = 0;
  242. }
  243. if ($count < 0)
  244. {
  245. $count = 1;
  246. }
  247. $ex = new OW_Example();
  248. $ex->andFieldEqual('authorId', $id);
  249. $ex->andFieldBetween('timestamp', $lb, $ub);
  250. $ex->andFieldEqual('isDraft', 0);
  251. $ex->setOrder('`timestamp` DESC');
  252. $ex->setLimitClause($first, $count);
  253. return $this->findListByExample($ex);
  254. }
  255. public function countUserPostByPeriod( $id, $lb, $ub )
  256. {
  257. $ex = new OW_Example();
  258. $ex->andFieldEqual('authorId', $id);
  259. $ex->andFieldBetween('timestamp', $lb, $ub);
  260. $ex->andFieldEqual('isDraft', 0);
  261. $ex->setOrder('`timestamp` DESC');
  262. return $this->countByExample($ex);
  263. }
  264. public function findList( $first, $count )
  265. {
  266. if ($first < 0)
  267. {
  268. $first = 0;
  269. }
  270. if ($count < 0)
  271. {
  272. $count = 1;
  273. }
  274. $ex = new OW_Example();
  275. $ex->andFieldEqual('isDraft', 0);
  276. $ex->andFieldEqual('privacy', 'everybody');
  277. $ex->setOrder('timestamp desc')->setLimitClause($first, $count);
  278. $cacheLifeTime = self::CACHE_LIFE_TIME;
  279. $tags = array( self::CACHE_TAG_POST_COUNT );
  280. return $this->findListByExample($ex, $cacheLifeTime, $tags);
  281. }
  282. public function findTopRatedList( $first, $count )
  283. {
  284. if ($first < 0)
  285. {
  286. $first = 0;
  287. }
  288. if ($count < 0)
  289. {
  290. $count = 1;
  291. }
  292. $query = "
  293. SELECT p.*, IF(SUM(r.score) IS NOT NULL, SUM(r.score), 0) as `t`
  294. FROM `{$this->getTableName()}` as p
  295. LEFT JOIN `ow_base_rate` as r /*todo: 8aa*/
  296. ON( r.`entityType` = 'blog-post' AND p.id = r.`entityId` )
  297. WHERE p.isDraft = 0
  298. GROUP BY p.`id`
  299. ORDER BY `t` DESC
  300. LIMIT ?, ?";
  301. return $this->dbo->queryForObjectList($query, $this->getDtoClassName(), array($first, $count));
  302. }
  303. public function findListByTag( $tag, $first, $count )
  304. {
  305. if ($first < 0)
  306. {
  307. $first = 0;
  308. }
  309. if ($count < 0)
  310. {
  311. $count = 1;
  312. }
  313. $query = "
  314. SELECT p.*
  315. FROM `ow_base_tag` as t
  316. INNER JOIN `ow_base_entity_tag` as `et`
  317. ON(`t`.`id` = `et`.`tagId` AND `et`.`entityType` = 'blog-post')
  318. INNER JOIN `{$this->getTableName()}` as p
  319. ON(`et`.`entityId` = `p`.`id`)
  320. WHERE p.isDraft = 0 AND `t`.`label` = '{$tag}'
  321. ORDER BY
  322. LIMIT ?, ?";
  323. return $this->dbo->queryForObjectList($query, $this->getDtoClassName(), array($first, $count));
  324. }
  325. public function countByTag( $tag )
  326. {
  327. $query = "
  328. SELECT count( * )
  329. FROM `ow_base_tag` as t
  330. INNER JOIN `ow_base_entity_tag` as `et`
  331. ON(`t`.`id` = `et`.`tagId` AND `et`.`entityType` = 'blog-post')
  332. INNER JOIN `{$this->getTableName()}` as p
  333. ON(`et`.`entityId` = `p`.`id`)
  334. WHERE p.isDraft = 0 AND `t`.`label` = '{$tag}'";
  335. return $this->dbo->queryForColumn($query);
  336. }
  337. public function findListByIdList( $list )
  338. {
  339. $ex = new OW_Example();
  340. $ex->andFieldInArray('id', $list);
  341. $ex->andFieldEqual('privacy', 'everybody');
  342. $ex->setOrder('timestamp DESC');
  343. return $this->findListByExample($ex);
  344. }
  345. public function updateBlogsPrivacy( $authorId, $privacy )
  346. {
  347. $this->clearCache();
  348. $sql = "UPDATE `" . $this->getTableName() . "` SET `privacy` = :privacy
  349. WHERE `authorId` = :authorId";
  350. $this->dbo->query($sql, array('privacy' => $privacy, 'authorId' => $authorId));
  351. }
  352. public function clearCache()
  353. {
  354. OW::getCacheManager()->clean( array( PostDao::CACHE_TAG_POST_COUNT ));
  355. }
  356. }