<?php
    if(empty($_GET['domname']) || empty($_GET['date_from']) || empty($_GET['date_to']))
	{
?>
    <html>
    <body>
        <form action="" method="get" />
    	    <table>
    	    <tr>
    	    <td>Domena</td><td><input type="text" name="domname" /></td>
    	    </tr>
    	    <tr>
    	    <td>Od</td><td><input type="text" name="date_from" value=<?php echo '"'.date("Y-m-01").'"'; ?> /></td>
    	    </tr>
    	    <tr>
    	    <td>Do</td><td><input type="text" name="date_to"   value=<?php echo '"'.date("Y-m-t").'"'; ?> /></td>
    	    </tr>
    	    </table>
    	    <input type="submit" />
	</form>
    </body>
    </html>
<?php
	}
	else
	{
	    $domainName=$_GET['domname'];
	    $dateFrom=$_GET['date_from'];
	    $dateTo=$_GET['date_to'];
	    $db_name="networds_networds";
	    $db_user="networds_networds";
	    $db_pass=")DsCq=p>Yf7?";
	    $db_host="localhost";
	
	    $link = mysql_connect($db_host, $db_user, $db_pass);

	    if(!$link)
	    {
    		die("Connection error");
	    }

	    if(!mysql_select_db($db_name))
	    {
	        die("Database not exists");
	    }

	    $result=mysql_query("SET NAMES 'utf8'");
	    
	    // --- słowa kluczowe dla wskazanej domeny
	    $keywordID=mysql_query("SELECT dk.id FROM user_domain_keyword AS dk JOIN user_domain AS ud ON dk.domain_id=ud.id WHERE ud.domain='".$domainName."'");
	    $keywordIDArray=array();
	    $kwCount=0;
	    while( $row=mysql_fetch_assoc($keywordID) )
	    {
		$keywordIDArray[]=$row['id'];
		$kwCount++;
	    }
	    if($kwCount == 0)
	    {
		echo "Brak wyników - <a href=\"index.php\">spróbuj ponownie</a>";
		exit;
	    }
	    //---------------------------------------------------------------------------------
	    
	    $lastSlash=strrpos($domainName, '/');
	    $lastSlash=$lastSlash==0 ? 0 : $lastSlash+1;
	    $nameLen=strlen($domainName); 
	    $fileName=substr($domainName, $lastSlash, $nameLen-$lastSlash).'.csv';
	    header('Content-Type: text/csv');
	    header('Content-disposition: attachment; filename='.$fileName);
	    
	    // --- przygotowanie zapytania
	    $kwList="";
	    foreach($keywordIDArray as $ka)
	    {
		$kwList.=$ka.", ";
	    }
	    $kwList=substr($kwList, 0, strlen($kwList)-2);
	    $query="SELECT stat.date, kw.keyword, stat.position FROM user_domain_keywords_stats_pl AS stat JOIN user_domain_keyword AS kw ON stat.keyword_id=kw.id WHERE stat.date >= '".$dateFrom."' AND stat.date <= '".$dateTo."' AND stat.keyword_id IN (".$kwList.") AND kw.active=1 ORDER BY kw.keyword, stat.date";
	    //---------------------------------------------------------------------------------
	    
	    //listowanie
	    $statsArray=array();
	    $stats=mysql_query($query);
	    $csv="";
	    while ( $row=mysql_fetch_assoc($stats) )
	    {
		//$statsArray[]=array("date" => $row['date'], "keyword" => $row['keyword'], "position" => $row['position']);
		$csv.=$row['keyword'].";".$row['date'].";". (empty($row['position']) ? '-' : $row['position']) .";\n";
		//echo "<br>";
	    }
	    //---------------------------------------------------------------------------------
	    $csv=iconv("utf-8", "windows-1250", $csv);
	    echo $csv;
	}
?>