Example #1
0
 /**
  * @param   string|array $categories
  *
  * @return boolean|integer
  */
 public static function recount($categories = '')
 {
     $db = JFactory::getDBO();
     if (is_array($categories)) {
         $categories = implode(',', $categories);
     }
     $categories = !empty($categories) ? "AND t.category_id IN ({$categories})" : '';
     // Update category post count and last post info on categories which have published topics
     $query = "UPDATE #__kunena_categories AS c\n\t\t\tINNER JOIN (\n\t\t\t\t\tSELECT t.category_id AS id, COUNT( * ) AS numTopics, SUM( t.posts ) AS numPosts, t2.id as last_topic_id\n\t\t\t\t\tFROM #__kunena_topics AS t INNER JOIN (SELECT t.id, t.category_id, t.last_post_time\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM #__kunena_topics AS t,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t(SELECT category_id ,  max(last_post_time) as last_post_time\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM  `#__kunena_topics`\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE hold =0\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND moved_id =0\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tGROUP BY category_id) AS temp\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE temp.last_post_time = t.last_post_time\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{$categories}\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND t.category_id=temp.category_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t) AS t2 ON t2.category_id=t.category_id\n\t\t\t\t\tWHERE t.hold =0\n\t\t\t\t\tAND t.moved_id =0\n\t\t\t\t\t{$categories}\n\t\t\t\t\tGROUP BY t.category_id\n\t\t\t) AS r ON r.id=c.id\n\t\t\tINNER JOIN #__kunena_topics AS tt ON tt.id=r.last_topic_id\n\t\t\tSET c.numTopics = r.numTopics,\n\t\t\t\tc.numPosts = r.numPosts,\n\t\t\t\tc.last_topic_id=r.last_topic_id,\n\t\t\t\tc.last_post_id = tt.last_post_id,\n\t\t\t\tc.last_post_time = tt.last_post_time";
     $db->setQuery($query);
     $db->execute();
     if (KunenaError::checkDatabaseError()) {
         return false;
     }
     $rows = $db->getAffectedRows();
     // Update categories which have no published topics
     $query = "UPDATE #__kunena_categories AS c\n\t\t\tLEFT JOIN #__kunena_topics AS tt ON c.id=tt.category_id AND tt.hold=0\n\t\t\tSET c.numTopics=0,\n\t\t\t\tc.numPosts=0,\n\t\t\t\tc.last_topic_id=0,\n\t\t\t\tc.last_post_id=0,\n\t\t\t\tc.last_post_time=0\n\t\t\tWHERE tt.id IS NULL";
     $db->setQuery($query);
     $db->execute();
     if (KunenaError::checkDatabaseError()) {
         return false;
     }
     $rows += $db->getAffectedRows();
     if ($rows) {
         // If something changed, clean our cache
         KunenaCacheHelper::clearCategories();
     }
     return $rows;
 }