PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/Comments.php

https://github.com/jiiarra/site
PHP | 371 lines | 187 code | 46 blank | 138 comment | 27 complexity | c31c7ac4698066b9a624c719f80b4929 MD5 | raw file
  1. <?php
  2. /**
  3. * Comments -> Comments database model for comments table.
  4. *
  5. * Copyright (c) <2009>, Markus Riihelä
  6. * Copyright (c) <2009>, Mikko Sallinen
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * License text found in /license/
  19. */
  20. /**
  21. * Comments - class
  22. *
  23. * @package models
  24. * @author Markus Riihelä & Mikko Sallinen
  25. * @copyright 2009 Markus Riihelä & Mikko Sallinen
  26. * @license GPL v2
  27. * @version 1.0
  28. */
  29. class Default_Model_Comments extends Zend_Db_Table_Abstract
  30. {
  31. // Table name
  32. protected $_name = 'comments_cmt';
  33. // Table primary key
  34. protected $_primary = 'id_cmt';
  35. // Table reference map
  36. protected $_referenceMap = array(
  37. 'CommentUser' => array(
  38. 'columns' => array('id_usr_cmt'),
  39. 'refTableClass' => 'Default_Model_User',
  40. 'refColumns' => array('id_usr')
  41. ),
  42. // TEST START
  43. 'CommentComment' => array(
  44. 'columns' => array('id_parent_cmt'),
  45. 'refTableClass' => 'Default_Model_Comments',
  46. 'refColumns' => array('id_cmt')
  47. ),
  48. 'CommentCType' => array(
  49. 'columns' => array('type_cmt'),
  50. 'refTableClass' => 'Default_Model_PageTypes',
  51. 'refColumns' => array('type_ptp')
  52. )
  53. // TEST END
  54. );
  55. /**
  56. * getById
  57. *
  58. * Gets comment by given id value.
  59. *
  60. * @param integer $id Id value of comment.
  61. * @return array
  62. */
  63. public function getById($id = 0)
  64. {
  65. // Array for comment data
  66. $data = array();
  67. // Find comment
  68. $rowset = $this->find((int)$id)->current();
  69. // If comment found
  70. if(count($rowset) == 1) {
  71. // Comment data to array
  72. $comment_data = $rowset->toArray();
  73. // Find dependet users
  74. $usr = $rowset->findDependentRowset('Default_Model_User', 'CommentUser')->toArray();
  75. // If user found
  76. if(!empty($usr)) {
  77. $comment_owner = $usr[0];
  78. } // end if
  79. // Comment data
  80. $data['Data'] = $comment_data;
  81. $data['Poster'] = $comment_owner;
  82. } // end if
  83. return $data;
  84. } // end of getById()
  85. /**
  86. * gets all comments to a certain content by it's ID
  87. *
  88. * @param $id int id of content which comments are wanted
  89. * @author Joel Peltonen
  90. * @return $data array array of data
  91. */
  92. public function getAllByContentId($id = 0, $page = 0, $count = 0)
  93. {
  94. //$data = array();
  95. if ($id != -1) {
  96. $select = $this->_db->select()
  97. ->from(array('comments_cmt' => 'comments_cmt'),
  98. array('*'))
  99. ->join(array('usr' => 'users_usr'),
  100. 'comments_cmt.id_usr_cmt = usr.id_usr',
  101. array('id_usr', 'login_name_usr'))
  102. ->where('id_target_cmt = ?', (int)$id)
  103. ->where('type_cmt = 1')
  104. ->order('created_cmt DESC')
  105. ->limitPage((int)$page, (int)$count)
  106. ;
  107. $data = $this->_db->fetchAll($select);
  108. }
  109. return $data;
  110. }
  111. /**
  112. * getCommentCountByContentId
  113. *
  114. * Gets comment count by content id.
  115. *
  116. * @param $id int Id of content
  117. * @return int
  118. */
  119. public function getCommentCountByContentId($id = 0)
  120. {
  121. if ($id != 0) {
  122. $select = $this->_db->select()
  123. ->from(array('comments_cmt'),
  124. array('commentCount' => 'COUNT(id_cmt)'))
  125. ->where('id_target_cmt = ?', (int)$id)
  126. ->where('type_cmt = 1');
  127. $data = $this->_db->fetchAll($select);
  128. }
  129. return $data[0]['commentCount'];
  130. }
  131. /**
  132. * getByContent
  133. *
  134. * Get comments by content.
  135. *
  136. * @param $id Content id value.
  137. * @return array
  138. */
  139. public function getCommentsByContent($id_cnt = 0)
  140. {
  141. $select = $this->_db->select()
  142. ->from('comments_cmt', array('*'))
  143. ->where('id_target_cmt = ?', (int)$id_cnt)
  144. ->where('type_cmt = ?', $this->getCommentType("content"));
  145. $result = $this->_db->fetchAll($select);
  146. return $result;
  147. }
  148. public function getComments($type, $id, $id_usr, $time) {
  149. $select = $this->select()
  150. ->setIntegrityCheck(false)
  151. ->from($this, array('*'))
  152. ->joinInner( 'users_usr', 'id_usr_cmt = id_usr',
  153. array('id_usr', 'login_name_usr'))
  154. ->where('id_target_cmt = ?', $id)
  155. ->where('type_cmt = ?' , $type)
  156. ->order('created_cmt DESC')
  157. ;
  158. //Zend_Debug::dump($select->__toString());
  159. if ($time != 0) {
  160. $select->where('created_cmt >= from_unixtime('.$time.') and id_usr != '.$id_usr);
  161. $select->orWhere('created_cmt > from_unixtime('.$time.') and id_usr = '.$id_usr);
  162. }
  163. $result = $this->fetchAll($select);
  164. //Zend_Debug::dump($result->toArray()); die;
  165. return $result->toArray();
  166. }
  167. /**
  168. * addComment
  169. *
  170. * Add a new comment to content
  171. *
  172. * @param integer $content_id Content id value.
  173. * @param integer $user_id User id value.
  174. * @param array $data Comment form data.
  175. */
  176. public function addComment($type = 0, $content_id = 0, $user_id = 0, $parent = 0, $data = null)
  177. {
  178. echo $type.$content_id.$user_id.$parent.$data;
  179. // Check if content id, user id and form data exists
  180. if ($type != 0 && $content_id != 0 && $user_id != 0 && $data != null) {
  181. // Create a new row
  182. $comment = $this->createRow();
  183. // Remove line breaks from the beginning of the message
  184. //while (substr($data['comment_message'], 0, strlen(PHP_EOL)) == PHP_EOL) {
  185. // $data['comment_message'] = substr($data['comment_message'], 2);
  186. //}
  187. // Set columns values
  188. $comment->id_target_cmt = $content_id;
  189. $comment->type_cmt = $type;
  190. $comment->id_usr_cmt = $user_id;
  191. $comment->id_parent_cmt = $parent;
  192. $comment->title_cmt = '';//strip_tags($data['comment_subject']);
  193. $comment->body_cmt = strip_tags($data);
  194. // Check if there's still characters left in the message
  195. if (strlen($comment->body_cmt) > 0) {
  196. $comment->created_cmt = new Zend_Db_Expr('NOW()');
  197. $comment->modified_cmt = new Zend_Db_Expr('NOW()');
  198. // Save row
  199. $comment->save();
  200. }
  201. } // end if
  202. } // end of addComment
  203. /**
  204. * getCommentIdsByContentId
  205. * Fetchs all comment ids related to content
  206. *
  207. * @param int id_cnt The id of the content
  208. * @return array $result Query results
  209. * @author Pekka Piispanen
  210. */
  211. public function getCommentIdsByContentId($id_cnt = 0)
  212. {
  213. $select = $this->_db->select()
  214. ->from('comments_cmt', array('id_cmt'))
  215. ->where('id_target_cmt = ?', (int)$id_cnt)
  216. ->where('type_cmt = ?', $this->getCommentType("content"));
  217. $result = $this->_db->fetchAll($select);
  218. return $result;
  219. }
  220. public function getContentIdsByCommentId($id_cmt = 0)
  221. {
  222. $type = $this->getCommentType("content");
  223. $select = $this->_db->select('id_target_cmt')
  224. ->from('comments_cmt', array('id_target_cmt'))
  225. ->where('id_cmt = ?', (int)$id_cmt)
  226. ->where('type_cmt = ?', $type);
  227. $result = $this->_db->fetchAll($select);
  228. return $result;
  229. }
  230. /**
  231. * removeComment
  232. * Removes specified comment
  233. *
  234. * @param int id_cmt Id of the comment
  235. * @author Pekka Piispanen
  236. */
  237. public function removeComment($id_cmt)
  238. {
  239. $where = $this->getAdapter()->quoteInto('id_cmt = ?', (int)$id_cmt);
  240. if ($this->delete($where)) {
  241. return true;
  242. } else {
  243. return false;
  244. }
  245. }
  246. /**
  247. * removeCommentText
  248. * Writes Comment removed into specified comment
  249. *
  250. * @param int id_cmt Id of the comment
  251. * @author Mikko Korpinen
  252. */
  253. public function removeCommentText($id_cmt)
  254. {
  255. $data = array(
  256. "body_cmt" => "Comment removed",
  257. "modified_cmt" => new Zend_Db_Expr('NOW()')
  258. );
  259. $where = $this->getAdapter()->quoteInto('id_cmt = ?', (int)$id_cmt);
  260. $this->update($data, $where);
  261. }
  262. /**
  263. * removeAllContentComments
  264. * Removes all content comments
  265. *
  266. * @param int id_cnt Id of the content
  267. * @author Mikko Korpinen
  268. */
  269. public function removeAllContentComments($id_cnt)
  270. {
  271. $ptModel = new Default_Model_PageTypes();
  272. $where[] = $this->getAdapter()->quoteInto('id_target_cmt = ?', (int)$id_cnt);
  273. $where[] = $this->getAdapter()->quoteInto('type_cmt = ?', $ptModel->getId('content'));
  274. $this->delete($where);
  275. // return false if content still has contents
  276. return $this->fetchAll($where)->count() ? false : true;
  277. }
  278. /**
  279. * commentExists
  280. *
  281. * Check if a comment exists
  282. *
  283. * @param integer $comment_id Comment id value.
  284. * @return boolean True if exists, false if not.
  285. */
  286. public function commentExists($comment_id = 0)
  287. {
  288. // Check if $comment_id exists
  289. if ($comment_id != 0) {
  290. $select = $this->select()
  291. ->where('id_cmt = ?', (int)$comment_id)
  292. ;
  293. $row = $this->fetchRow($select);
  294. if($row == NULL)
  295. return false;
  296. elseif($row != NULL)
  297. return true;
  298. } // end if
  299. } // end of flagComment
  300. /**
  301. * userIsOwner - Return true if user is comment owner
  302. *
  303. * @author Mikko Korpinen
  304. * @param int $id_usr_cmt
  305. * @param int $id_cmt
  306. * @return boolean
  307. */
  308. public function userIsOwner($id_usr_cmt, $id_cmt)
  309. {
  310. $select = $this->select()
  311. ->from($this, array('id_cmt'))
  312. ->where('id_usr_cmt = ?', $id_usr_cmt)
  313. ->where('id_cmt = ?', $id_cmt)
  314. ->limit(1);
  315. $result = $this->_db->fetchAll($select);
  316. if(isset($result[0]) && !empty($result[0])) {
  317. return true;
  318. }
  319. return false;
  320. }
  321. private function getCommentType($type) {
  322. $ptpModel = new Default_Model_PageTypes();
  323. return $ptpModel->getId($type);
  324. }
  325. } // end of class