/administrator/components/com_kunena/libraries/forum/topic/user/helper.php

https://github.com/draganz/Kunena-2.0 · PHP · 235 lines · 173 code · 30 blank · 32 comment · 26 complexity · d3e529806b78269edd23c56c9874c28a MD5 · raw file

  1. <?php
  2. /**
  3. * Kunena Component
  4. * @package Kunena.Framework
  5. * @subpackage Forum.Topic.User
  6. *
  7. * @copyright (C) 2008 - 2012 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. /**
  13. * Kunena Forum Topic User Helper Class
  14. */
  15. abstract class KunenaForumTopicUserHelper {
  16. // Global for every instance
  17. protected static $_instances = array();
  18. protected static $_topics = array();
  19. /**
  20. * Returns KunenaForumTopicUser object
  21. *
  22. * @access public
  23. * @param identifier The user topic to load - Can be only an integer.
  24. * @return KunenaForumTopicUser The user topic object.
  25. * @since 1.7
  26. */
  27. static public function get($topic = null, $user = null, $reload = false) {
  28. if ($topic instanceof KunenaForumTopic) {
  29. $topic = $topic->id;
  30. }
  31. $topic = intval ( $topic );
  32. $user = KunenaUserHelper::get($user);
  33. if ($topic < 1)
  34. return new KunenaForumTopicUser (null, $user);
  35. if ($reload || empty ( self::$_instances [$user->userid][$topic] )) {
  36. $topics = self::getTopics ( $topic, $user );
  37. self::$_instances [$user->userid][$topic] = self::$_topics [$topic][$user->userid] = array_pop($topics);
  38. }
  39. return self::$_instances [$user->userid][$topic];
  40. }
  41. static public function getTopics($ids = false, $user=null) {
  42. $user = KunenaUserHelper::get($user);
  43. if ($ids === false) {
  44. return isset(self::$_instances[$user->userid]) ? self::$_instances[$user->userid] : array();
  45. } elseif (!is_array ($ids) ) {
  46. $ids = array($ids);
  47. }
  48. // Convert topic objects into ids
  49. foreach ($ids as $i=>$id) {
  50. if ($id instanceof KunenaForumTopic) $ids[$i] = $id->id;
  51. }
  52. $ids = array_unique($ids);
  53. self::loadTopics($ids, $user);
  54. $list = array ();
  55. foreach ( $ids as $id ) {
  56. if (!empty(self::$_instances [$user->userid][$id])) {
  57. $list [$id] = self::$_instances [$user->userid][$id];
  58. }
  59. }
  60. return $list;
  61. }
  62. public static function move($old, $new) {
  63. // Update database
  64. $db = JFactory::getDBO ();
  65. $query ="UPDATE #__kunena_user_topics SET topic_id={$db->quote($new->id)}, category_id={$db->quote($new->category_id)} WHERE topic_id={$db->quote($old->id)}";
  66. $db->setQuery($query);
  67. $db->query ();
  68. if (KunenaError::checkDatabaseError ())
  69. return false;
  70. // Update internal state
  71. if (isset(self::$_topics [$old->id])) {
  72. if ($new->id != $old->id) {
  73. self::$_topics [$new->id] = self::$_topics [$old->id];
  74. unset(self::$_topics [$old->id]);
  75. }
  76. foreach (self::$_topics [$new->id] as &$instance) {
  77. $instance->topic_id = $new->id;
  78. $instance->category_id = $new->category_id;
  79. }
  80. }
  81. return true;
  82. }
  83. public static function merge($old, $new) {
  84. $db = JFactory::getDBO ();
  85. // Move all user topics which do not exist in new topic
  86. $queries[] = "UPDATE #__kunena_user_topics AS ut
  87. INNER JOIN #__kunena_user_topics AS o ON o.user_id = ut.user_id
  88. SET ut.topic_id={$db->quote($new->id)}, ut.category_id={$db->quote($new->category_id)}
  89. WHERE o.topic_id={$db->quote($old->id)} AND ut.topic_id IS NULL";
  90. // Merge user topics information that exists in both topics
  91. $queries[] = "UPDATE #__kunena_user_topics AS ut
  92. INNER JOIN #__kunena_user_topics AS o ON o.user_id = ut.user_id
  93. SET ut.posts = o.posts + ut.posts,
  94. ut.last_post_id = GREATEST( o.last_post_id, ut.last_post_id ),
  95. ut.owner = GREATEST( o.owner, ut.owner ),
  96. ut.favorite = GREATEST( o.favorite, ut.favorite ),
  97. ut.subscribed = GREATEST( o.subscribed, ut.subscribed )
  98. WHERE ut.topic_id = {$db->quote($new->id)}
  99. AND o.topic_id = {$db->quote($old->id)}";
  100. // Delete all user topics from the shadow topic
  101. $queries[] = "DELETE FROM #__kunena_user_topics WHERE topic_id={$db->quote($old->id)}";
  102. foreach ($queries as $query) {
  103. $db->setQuery($query);
  104. $db->query ();
  105. if (KunenaError::checkDatabaseError ())
  106. return false;
  107. }
  108. // Update internal state
  109. self::reloadTopic($old->id);
  110. self::reloadTopic($new->id);
  111. return true;
  112. }
  113. public static function recount($topicids=false, $start=0, $end=0) {
  114. $db = JFactory::getDBO ();
  115. if (is_array($topicids)) {
  116. $where = 'AND m.thread IN ('.implode(',', $topicids).')';
  117. $where2 = 'AND ut.topic_id IN ('.implode(',', $topicids).')';
  118. } elseif ((int)$topicids) {
  119. $where = 'AND m.thread='.(int)$topicids;
  120. $where2 = 'AND ut.topic_id='.(int)$topicids;
  121. } else {
  122. $where = '';
  123. $where2 = '';
  124. }
  125. if ($end) {
  126. $where .= " AND (m.thread BETWEEN {$start} AND {$end})";
  127. $where2 .= " AND (ut.topic_id BETWEEN {$start} AND {$end})";
  128. }
  129. // Create missing user topics and update post count and last post if there are posts by that user
  130. $query ="INSERT INTO #__kunena_user_topics (user_id, topic_id, category_id, posts, last_post_id, owner)
  131. SELECT m.userid AS user_id, m.thread AS topic_id, m.catid AS category_id, SUM(m.hold=0) AS posts, MAX(IF(m.hold=0,m.id,0)) AS last_post_id, MAX(IF(m.parent=0,1,0)) AS owner
  132. FROM #__kunena_messages AS m
  133. WHERE m.userid>0 AND m.moved=0 {$where}
  134. GROUP BY m.userid, m.thread
  135. ON DUPLICATE KEY UPDATE category_id=VALUES(category_id), posts=VALUES(posts), last_post_id=VALUES(last_post_id)";
  136. $db->setQuery($query);
  137. $db->query ();
  138. if (KunenaError::checkDatabaseError ())
  139. return false;
  140. $rows = $db->getAffectedRows ();
  141. // Find user topics where last post doesn't exist and reset values in it
  142. $query ="UPDATE #__kunena_user_topics AS ut
  143. LEFT JOIN #__kunena_messages AS m ON ut.last_post_id=m.id AND m.hold=0
  144. SET posts=0, last_post_id=0
  145. WHERE m.id IS NULL {$where2}";
  146. $db->setQuery($query);
  147. $db->query ();
  148. if (KunenaError::checkDatabaseError ())
  149. return false;
  150. $rows += $db->getAffectedRows ();
  151. // Delete entries that have default values
  152. $query ="DELETE ut FROM #__kunena_user_topics AS ut WHERE ut.posts=0 AND ut.owner=0 AND ut.favorite=0 AND ut.subscribed=0 AND ut.params='' {$where2}";
  153. $db->setQuery($query);
  154. $db->query ();
  155. if (KunenaError::checkDatabaseError ())
  156. return false;
  157. $rows += $db->getAffectedRows ();
  158. return $rows;
  159. }
  160. // Internal functions
  161. static protected function loadTopics($ids, $user) {
  162. foreach ($ids as $i=>$id) {
  163. $id = intval($id);
  164. if (!$id || isset(self::$_instances [$user->userid][$id]))
  165. unset($ids[$i]);
  166. }
  167. if (empty($ids))
  168. return;
  169. $idlist = implode(',', $ids);
  170. $db = JFactory::getDBO ();
  171. $query = "SELECT * FROM #__kunena_user_topics WHERE user_id={$db->quote($user->userid)} AND topic_id IN ({$idlist})";
  172. $db->setQuery ( $query );
  173. $results = (array) $db->loadAssocList ('topic_id');
  174. KunenaError::checkDatabaseError ();
  175. foreach ( $ids as $id ) {
  176. if (isset($results[$id])) {
  177. $instance = new KunenaForumTopicUser ();
  178. $instance->bind ( $results[$id] );
  179. $instance->exists(true);
  180. self::$_instances [$user->userid][$id] = self::$_topics [$id][$user->userid] = $instance;
  181. } else {
  182. self::$_instances [$user->userid][$id] = self::$_topics [$id][$user->userid] = new KunenaForumTopicUser ($id, $user->userid);
  183. }
  184. }
  185. unset ($results);
  186. }
  187. static protected function reloadTopic($id) {
  188. if (empty(self::$_topics [$id])) return;
  189. $idlist = implode(',', array_keys(self::$_topics [$id]));
  190. $db = JFactory::getDBO ();
  191. $query = "SELECT * FROM #__kunena_user_topics WHERE user_id IN ({$idlist}) AND topic_id={$id}";
  192. $db->setQuery ( $query );
  193. $results = (array) $db->loadAssocList ('user_id');
  194. KunenaError::checkDatabaseError ();
  195. foreach ( self::$_topics [$id] as $instance ) {
  196. if (isset($results[$instance->user_id])) {
  197. $instance->bind ( $results[$instance->user_id] );
  198. $instance->exists(true);
  199. } else {
  200. $instance->reset();
  201. }
  202. }
  203. unset ($results);
  204. }
  205. }