/**
  * Updates the forum favorite database. It calls count_forum_favorites to get the group by count, 
  * and then stores the result in another table for easy access later.
  * Run after updating a users favorites
  * Takes one argument, the categories to refresh, if nothing is passed, refreshes everything.
  */
 function refresh_forum_favorites($catid)
 {
     global $db;
     $favfunct = new favorite_functions();
     if (!$catid) {
         $categories = $favfunct->get_favorites_categories(1);
         foreach ($categories as $cat) {
             $favfunct->refresh_forum_favorites($cat['category_id']);
         }
         return;
     }
     $catid = intval($catid);
     $data = $favfunct->count_forum_favorites(array('category_id' => $catid));
     $i = 1;
     foreach ($data as $item) {
         $where_fields = array('type' => 0, 'category_id' => $catid, 'listitem_id' => $i);
         $sql_ary = array('listitem_text' => $item['listitem_text'], 'listitem_url' => $item['listitem_url'] ? $item['listitem_url'] : '', 'listitem_count' => $item['listitem_count']);
         $i++;
         $favfunct->update_insert(FAVORITES_SPECIAL_TABLE, $sql_ary, $where_fields);
     }
     //Delete any leftover old values from table
     $sql = 'DELETE FROM ' . FAVORITES_SPECIAL_TABLE . " WHERE\n\t\ttype = 0 AND category_id = {$catid} AND listitem_id >= {$i}";
     //catid intval'd earlier so safe here
     $db->sql_query($sql);
 }