<?php 
/*#################################################################*\
|# Licence Number 0H6R-0808-0UVQ-0111
|# -------------------------------------------------------------   #|
|# Copyright (c)2012 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
require_once 'init.php';

if ($_GET['token'] == 'changemenow') {
    $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']);

    }
}