PageRenderTime 72ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_kunena/libraries/forum/message/helper.php

https://github.com/zarkos/Kunena-2.0
PHP | 340 lines | 277 code | 35 blank | 28 comment | 47 complexity | 59fdc91b528b7a86d946ea6edad24da3 MD5 | raw file
  1. <?php
  2. /**
  3. * Kunena Component
  4. * @package Kunena.Framework
  5. * @subpackage Forum.Message
  6. *
  7. * @copyright (C) 2008 - 2011 Kunena Team. All rights reserved.
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  9. * @link http://www.kunena.org
  10. **/
  11. defined ( '_JEXEC' ) or die ();
  12. kimport ('kunena.error');
  13. kimport ('kunena.databasequery');
  14. kimport ('kunena.forum.message');
  15. kimport ('kunena.forum.topic.helper');
  16. /**
  17. * Kunena Forum Message Helper Class
  18. */
  19. class KunenaForumMessageHelper {
  20. // Global for every instance
  21. protected static $_instances = array();
  22. protected static $_location = false;
  23. private function __construct() {}
  24. /**
  25. * Returns KunenaForumMessage object
  26. *
  27. * @access public
  28. * @param identifier The message to load - Can be only an integer.
  29. * @return KunenaForumMessage The message object.
  30. * @since 1.7
  31. */
  32. static public function get($identifier = null, $reload = false) {
  33. if ($identifier instanceof KunenaForumMessage) {
  34. return $identifier;
  35. }
  36. $id = intval ( $identifier );
  37. if ($id < 1)
  38. return new KunenaForumMessage ();
  39. if ($reload || empty ( self::$_instances [$id] )) {
  40. self::$_instances [$id] = new KunenaForumMessage ( $id );
  41. }
  42. return self::$_instances [$id];
  43. }
  44. static public function getMessages($ids = false, $authorise='read') {
  45. if ($ids === false) {
  46. return self::$_instances;
  47. } elseif (is_array ($ids) ) {
  48. $ids = array_unique($ids);
  49. } else {
  50. $ids = array($ids);
  51. }
  52. self::loadMessages($ids);
  53. $list = array ();
  54. foreach ( $ids as $id ) {
  55. if (!empty(self::$_instances [$id]) && self::$_instances [$id]->authorise($authorise, null, true)) {
  56. $list [$id] = self::$_instances [$id];
  57. }
  58. }
  59. return $list;
  60. }
  61. static public function getMessagesByTopic($topic, $start=0, $limit=0, $ordering='ASC', $hold=0, $orderbyid = false) {
  62. $topic = KunenaForumTopicHelper::get($topic);
  63. if (!$topic->exists())
  64. return array();
  65. $total = $topic->getTotal();
  66. if ($start < 0)
  67. $start = 0;
  68. if ($limit < 1)
  69. $limit = KunenaFactory::getConfig()->messages_per_page;
  70. // If out of range, use last page
  71. if ($total < $start)
  72. $start = intval($total / $limit) * $limit;
  73. $ordering = strtoupper($ordering);
  74. if ($ordering != 'DESC')
  75. $ordering = 'ASC';
  76. return self::loadMessagesByTopic($topic->id, $start, $limit, $ordering, $hold, $orderbyid);
  77. }
  78. static public function getLatestMessages($categories=false, $limitstart=0, $limit=0, $params=array()) {
  79. $reverse = isset($params['reverse']) ? (int) $params['reverse'] : 0;
  80. $orderby = isset($params['orderby']) ? (string) $params['orderby'] : 'm.time DESC';
  81. $starttime = isset($params['starttime']) ? (int) $params['starttime'] : 0;
  82. $mode = isset($params['mode']) ? $params['mode'] : 'recent';
  83. $user = isset($params['user']) ? $params['user'] : false;
  84. $where = isset($params['where']) ? (string) $params['where'] : '';
  85. $childforums = isset($params['childforums']) ? (bool) $params['childforums'] : false;
  86. $db = JFactory::getDBO();
  87. // FIXME: use right config setting
  88. if ($limit < 1) $limit = KunenaFactory::getConfig ()->threads_per_page;
  89. $cquery = new KunenaDatabaseQuery();
  90. $cquery->select('COUNT(*)')
  91. ->from('#__kunena_messages AS m')
  92. ->innerJoin('#__kunena_messages_text AS t ON m.id = t.mesid')
  93. ->where('m.moved=0'); // TODO: remove column
  94. $rquery = new KunenaDatabaseQuery();
  95. $rquery->select('m.*, t.message')
  96. ->from('#__kunena_messages AS m')
  97. ->innerJoin('#__kunena_messages_text AS t ON m.id = t.mesid')
  98. ->where('m.moved=0') // TODO: remove column
  99. ->order($orderby);
  100. $authorise = 'read';
  101. $hold = 'm.hold=0';
  102. $userfield = 'm.userid';
  103. switch ($mode) {
  104. case 'unapproved':
  105. $authorise = 'approve';
  106. $hold = "m.hold=1";
  107. break;
  108. case 'deleted':
  109. $authorise = 'undelete';
  110. $hold = "m.hold>=2";
  111. break;
  112. case 'mythanks':
  113. $userfield = 'th.userid';
  114. $cquery->innerJoin('#__kunena_thankyou AS th ON m.id = th.postid');
  115. $rquery->innerJoin('#__kunena_thankyou AS th ON m.id = th.postid');
  116. break;
  117. case 'thankyou':
  118. $userfield = 'th.targetuserid';
  119. $cquery->innerJoin('#__kunena_thankyou AS th ON m.id = th.postid');
  120. $rquery->innerJoin('#__kunena_thankyou AS th ON m.id = th.postid');
  121. break;
  122. case 'recent':
  123. default:
  124. }
  125. if (is_array($categories) && in_array(0, $categories)) {
  126. $categories = false;
  127. }
  128. $categories = KunenaForumCategoryHelper::getCategories($categories, $reverse, 'topic.'.$authorise);
  129. if ($childforums) {
  130. $categories += KunenaForumCategoryHelper::getChildren($categories, -1, false, array('action'=>'topic.'.$authorise));
  131. }
  132. $catlist = array();
  133. foreach ($categories as $category) {
  134. $catlist += $category->getChannels();
  135. }
  136. if (empty($catlist)) return array(0, array());
  137. $allowed = implode(',', array_keys($catlist));
  138. $cquery->where("m.catid IN ({$allowed})");
  139. $rquery->where("m.catid IN ({$allowed})");
  140. $cquery->where($hold);
  141. $rquery->where($hold);
  142. if ($user) {
  143. $cquery->where("{$userfield}={$db->Quote($user)}");
  144. $rquery->where("{$userfield}={$db->Quote($user)}");
  145. }
  146. // Negative time means no time
  147. if ($starttime == 0) {
  148. $starttime = KunenaFactory::getSession ()->lasttime;
  149. } elseif ($starttime > 0) {
  150. $starttime = JFactory::getDate ()->toUnix () - ($starttime * 3600);
  151. }
  152. if ($starttime > 0) {
  153. $cquery->where("m.time>{$db->Quote($starttime)}");
  154. $rquery->where("m.time>{$db->Quote($starttime)}");
  155. }
  156. if ($where) {
  157. $cquery->where($where);
  158. $rquery->where($where);
  159. }
  160. $db->setQuery ( $cquery );
  161. $total = ( int ) $db->loadResult ();
  162. if (KunenaError::checkDatabaseError() || !$total) return array(0, array());
  163. // If out of range, use last page
  164. if ($total < $limitstart)
  165. $limitstart = intval($total / $limit) * $limit;
  166. $db->setQuery ( $rquery, $limitstart, $limit );
  167. $results = $db->loadObjectList ();
  168. if (KunenaError::checkDatabaseError()) return array(0, array());
  169. $messages = array();
  170. foreach ( $results as $result ) {
  171. $instance = new KunenaForumMessage (false);
  172. $instance->setProperties ( $result );
  173. $instance->exists(true);
  174. self::$_instances [$instance->id] = $instance;
  175. $messages[$instance->id] = $instance;
  176. }
  177. unset ($results);
  178. return array($total, $messages);
  179. }
  180. public function getLocation($mesid, $direction = 'asc', $hold=null) {
  181. if (!$hold) {
  182. $me = KunenaFactory::getUser();
  183. $access = KunenaFactory::getAccessControl();
  184. $hold = $access->getAllowedHold($me->userid, $mesid, false);
  185. }
  186. if (!isset(self::$_location [$mesid])) {
  187. self::loadLocation(array($mesid));
  188. }
  189. $location = self::$_location [$mesid];
  190. $count = 0;
  191. foreach ($location->hold as $meshold=>$values) {
  192. if (isset($hold[$meshold])) {
  193. $count += $values[$direction = 'asc' ? 'before' : 'after'];
  194. if ($direction == 'both') $count += $values['before'];
  195. }
  196. }
  197. return $count;
  198. }
  199. static function loadLocation($mesids) {
  200. // NOTE: if you already know the location using this code just takes resources
  201. if (!is_array($mesids)) $mesids = explode ( ',', $mesids );
  202. $list = array();
  203. $ids = array();
  204. foreach ($mesids as $id) {
  205. if ($id instanceof KunenaForumMessage) {
  206. $id = $id->id;
  207. } else {
  208. $id = (int) $id;
  209. }
  210. if (!isset(self::$_location [$id])) {
  211. $ids[$id] = $id;
  212. self::$_location [$id] = new stdClass();
  213. self::$_location [$id]->hold = array();
  214. }
  215. }
  216. if (empty($ids))
  217. return;
  218. $idlist = implode ( ',', $ids );
  219. $db = JFactory::getDBO ();
  220. $db->setQuery ( "SELECT m.id, mm.hold, m.catid AS category_id, m.thread AS topic_id,
  221. SUM(mm.id<m.id) AS before_count,
  222. SUM(mm.id>m.id) AS after_count
  223. FROM #__kunena_messages AS m
  224. INNER JOIN #__kunena_messages AS mm ON m.thread=mm.thread
  225. WHERE m.id IN ({$idlist})
  226. GROUP BY m.id, mm.hold" );
  227. $results = (array) $db->loadObjectList ();
  228. KunenaError::checkDatabaseError();
  229. foreach ($results as $result) {
  230. $instance = self::$_location [$result->id];
  231. if (!isset($instance->id)) {
  232. $instance->id = $result->id;
  233. $instance->category_id = $result->category_id;
  234. $instance->topic_id = $result->topic_id;
  235. self::$_location [$instance->id] = $instance;
  236. }
  237. $instance->hold[$result->hold] = array('before'=>$result->before_count, 'after'=>$result->after_count);
  238. }
  239. }
  240. static function recount($topicids=false) {
  241. $db = JFactory::getDBO ();
  242. if (is_array($topicids)) {
  243. $where = 'WHERE m.thread IN ('.implode(',', $topicids).')';
  244. } elseif ((int)$topicids) {
  245. $where = 'WHERE m.thread='.(int)$topicids;
  246. } else {
  247. $where = '';
  248. }
  249. // Update catid in all messages
  250. $query ="UPDATE #__kunena_messages AS m
  251. INNER JOIN #__kunena_topics AS tt ON tt.id=m.thread
  252. SET m.catid=tt.category_id {$where}";
  253. $db->setQuery($query);
  254. $db->query ();
  255. if (KunenaError::checkDatabaseError ())
  256. return false;
  257. return $db->getAffectedRows ();
  258. }
  259. // Internal functions
  260. static protected function loadMessages($ids) {
  261. foreach ($ids as $i=>$id) {
  262. if (isset(self::$_instances [$id]))
  263. unset($ids[$i]);
  264. }
  265. if (empty($ids))
  266. return;
  267. $idlist = implode(',', $ids);
  268. $db = JFactory::getDBO ();
  269. $query = "SELECT m.*, t.message FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON m.id=t.mesid WHERE m.id IN ({$idlist})";
  270. $db->setQuery ( $query );
  271. $results = (array) $db->loadAssocList ('id');
  272. KunenaError::checkDatabaseError ();
  273. foreach ( $ids as $id ) {
  274. if (isset($results[$id])) {
  275. $instance = new KunenaForumMessage (false);
  276. $instance->setProperties ( $results[$id] );
  277. $instance->exists(true);
  278. self::$_instances [$id] = $instance;
  279. } else {
  280. self::$_instances [$id] = null;
  281. }
  282. }
  283. unset ($results);
  284. }
  285. static protected function loadMessagesByTopic($topic_id, $start=0, $limit=0, $ordering='ASC', $hold=0, $orderbyid = false) {
  286. $db = JFactory::getDBO ();
  287. $query = "SELECT m.*, t.message
  288. FROM #__kunena_messages AS m
  289. INNER JOIN #__kunena_messages_text AS t ON m.id=t.mesid
  290. WHERE m.thread={$db->quote($topic_id)} AND m.hold IN ({$hold}) ORDER BY m.time {$ordering}";
  291. $db->setQuery ( $query, $start, $limit );
  292. $results = (array) $db->loadAssocList ('id');
  293. KunenaError::checkDatabaseError ();
  294. $list = array();
  295. foreach ( $results as $id=>$result ) {
  296. $instance = new KunenaForumMessage (false);
  297. $instance->setProperties ( $result );
  298. $instance->exists(true);
  299. self::$_instances [$id] = $instance;
  300. $list[$orderbyid ? $id : $start++] = $instance;
  301. }
  302. unset ($results);
  303. return $list;
  304. }
  305. }