<?php 
include ("../includes/db.php");

$url = 'http://graph.facebook.com/';

$pgid = array (0);
$fanCount = array (0);

$res = mysql_query ("SELECT Id, PageId FROM page");
$nr = mysql_num_rows ($res);
echo $nr . "<br />";
while ($nr > 0) {
	$row = mysql_fetch_array ($res);
	extract ($row);
	
	if ($PageId != NULL) {
		$pageUrl = "" . $url . $PageId . "?fields=id,name,link,picture,fan_count";
		//echo $pageUrl;

		array_push($pgid,$Id);

		// create curl resource
		$ch = curl_init();

		// set url
		curl_setopt($ch, CURLOPT_URL, $pageUrl);
		curl_setopt($ch,CURLOPT_HTTPHEADER,array ("Content-Type: text/xml; charset=utf-8"));
		//return the transfer as a string
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		// $output contains the output string
		$output = curl_exec($ch);
		//$output = utf8_encode($output);
		//echo $output . "<br />";

		$param = json_decode($output, true);
		
		if ($param['fan_count']==NULL) { 
			$pageUrl = "https://api.facebook.com/method/pages.getinfo?fields=fan_count,page_url,name&page_ids=". $PageId;
			curl_setopt($ch, CURLOPT_URL, $pageUrl);
			curl_setopt($ch,CURLOPT_HTTPHEADER,array ("Content-Type: text/xml; charset=utf-8"));
			//return the transfer as a string
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

			// $output contains the output string
			$output = curl_exec($ch);
			$param = json_decode($output, true);
			echo $output . "<br />";

			if ($param['fan_count']==NULL) { 
				$param['fan_count'] = 0;
			}
		} 
		
		array_push($fanCount,$param['fan_count']);
		
		// close curl resource to free up system resources
		curl_close($ch);   
		
		
		//$resUPD = mysql_query ("INSERT INTO report (PageId, Fans) VALUES ($pgid, $fanCount);") or die ("error 2");
	}
	
	$nr--;	
}

for ($i=1; $i<sizeof($pgid); $i++) {
	$dailyFans = 0;
	$weeklyFans = 0;
	$monthlyFans = 0;
	
	$res = mysql_query ("SELECT DailyFans, WeeklyFans, MonthlyFans, TotalFans FROM report WHERE PageId = $pgid[$i] ORDER BY Date DESC LIMIT 0, 32 ");
	$nr2 = mysql_num_rows ($res);
	
	echo "<br/><br/>page: " . $i . " | " . $nr2 . "<br/>";
	
	if ($nr2 > 0) {
		$nr2--;
		//echo " " . $nr2;
		$row = mysql_fetch_array($res);
		extract($row);
		$dailyFans = $fanCount[$i] - $TotalFans;
		echo "daily: ". $dailyFans ."<br />";
	}
	for ($j=0;$j<5;$j++) {
		if ($nr2==0) {
			echo "week break!";
			break; break;	
		}
		$row = mysql_fetch_array($res);
		//extract ($row);
		$nr2--;
		//echo " " . $nr2;
	}
	if ($nr2 > 0) {
		$nr2--;
		//echo " " . $nr2;
		$row = mysql_fetch_array($res);
		extract($row);
		$weeklyFans = $fanCount[$i] - $TotalFans;
		echo "weekly: ". $weeklyFans ."<br />";
	}
	for ($j=0;$j<22;$j++) {
		if ($nr2==0) {
			echo "month break!";
			break; break;	
		}
		$row = mysql_fetch_array($res);
		//extract ($row);
		$nr2--; 
		//echo " " . $nr2;
	}	
	if ($nr2 > 0) {
		$nr2--;
		//echo " " . $nr2;
		$row = mysql_fetch_array($res);
		extract($row);
		$monthlyFans = $fanCount[$i] - $TotalFans;
		echo "monthly: ". $monthlyFans ."<br />";
	}

	//echo $pgid[$i] . " : " . $fanCount[$i] . "<br />";
	$resUPD = mysql_query ("INSERT INTO report (PageId, DailyFans, WeeklyFans, MonthlyFans, TotalFans, Date) VALUES ($pgid[$i], $dailyFans, $weeklyFans, $monthlyFans, $fanCount[$i], '2010-11-08');") or die ("error" . $i);
}
?>