<?php 
/*#################################################################*\
|# Licence Number 0H6R-0808-0UVQ-0111
|# -------------------------------------------------------------   #|
|# Copyright (c)2012 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
 # ################################################################################
 # Project:   PHP Link Directory
 #
 # **********************************************************************
 # Copyright (C) 2004-2012 NetCreated, Inc. (http://www.netcreated.com/)
 #
 # This software is for use only to those who have purchased a license.
 # A license must be purchased for EACH installation of the software.
 #
 # By using the software you agree to the terms:
 #
 #    - You may not redistribute, sell or otherwise share this software
 #      in whole or in part without the consent of the the ownership
 #      of PHP Link Directory. Please contact david@david-duval.com
 #      if you need more information.
 #
 #    - You agree to retain a link back to http://www.phplinkdirectory.com/
 #      on all pages of your directory if you purchased any of our "link back" 
 #      versions of the software.
 #
 #
 # In some cases, license holders may be required to agree to changes
 # in the software license before receiving updates to the software.
 # **********************************************************************
 #
 # For questions, help, comments, discussion, etc., please join the
 # PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
 #
 # @link           http://www.phplinkdirectory.com/
 # @copyright      2004-2012 NetCreated, Inc. (http://www.netcreated.com/)
 # @projectManager David DuVal <david@david-duval.com>
 # @package        PHPLinkDirectory
 # @version        4.2.1
 # ################################################################################
 */


require_once 'init.php';
    $topCats = $db->getAll('SELECT * FROM PLD_CATEGORY WHERE PARENT_ID = 0');
    foreach ($topCats as $topCat) {
        $count = $db->getOne('SELECT COUNT(*) AS cnt FROM PLD_CATEGORY WHERE PARENT_ID = '.$topCat['ID']);
        $db->execute('UPDATE PLD_CATEGORY SET `COUNT` = '.$count.' WHERE ID = '.$topCat['ID']);
		 $count2 = $db->getOne('SELECT COUNT(*) AS cnt FROM PLD_LINK WHERE STATUS="2" AND CATEGORY_ID  = '.$topCat['ID']);
        $db->execute('UPDATE PLD_CATEGORY SET `COUNT` = `COUNT` + '.$count2.' WHERE ID = '.$topCat['ID']);
		$count3 = $db->getOne('SELECT COUNT(*) AS cnt FROM PLD_LINK WHERE STATUS="2" AND CATEGORY_ID = '.$topCat['ID']);
        $db->execute('UPDATE PLD_CATEGORY SET `LINK_COUNT` = '.$count3.' WHERE ID = '.$topCat['ID']);
    }
    $categs = $db->getAll('SELECT * FROM PLD_CATEGORY WHERE PARENT_ID > 0');
    foreach ($categs as $categ) {
        $count = $db->getOne('SELECT COUNT(*) AS cnt FROM PLD_LINK WHERE STATUS="2" AND CATEGORY_ID = '.$categ['ID']);
        $db->execute('UPDATE PLD_CATEGORY SET `COUNT` = '.$count.' WHERE ID = '.$categ['ID']);
		 $count2 = $db->getOne('SELECT COUNT(*) AS cnt FROM PLD_CATEGORY WHERE PARENT_ID = '.$categ['ID']);
        $db->execute('UPDATE PLD_CATEGORY SET `COUNT` = `COUNT` + '.$count2.' WHERE ID = '.$categ['ID']);
$count3 = $db->getOne('SELECT COUNT(*) AS cnt FROM PLD_LINK WHERE STATUS="2" AND CATEGORY_ID = '.$categ['ID']);
        $db->execute('UPDATE PLD_CATEGORY SET `LINK_COUNT` = '.$count3.' WHERE ID = '.$categ['ID']);
    }
$url = DOC_ROOT."/index.php";
      $title_msg = _L('The counts are being rebuilt. You will be redirected back to the home page now.');
      $redirect  = javascript_redirect($url, 6, $title_msg, $cust_msg, 1);
      $tpl->assign('redirect', $redirect);
 ?>