示例#1
0
function prepare_report()
{
    $cntryarr = load_countries_from_csv("./country.csv");
    $category = load_catnames_from_db();
    global $curr;
    $fname = tempnam("/tmp", "krep");
    //$report_fname=$client_dir."/".$fname.".csv";
    $fw = fopen($fname, "w");
    fputs($fw, "id,launch_time,till_days,pname, parent_category, goal, pledged, state, state_days, creator_name, bakerscount, category_name,  categoryposition, location_name,  location_state, location_country,days_till_deadline,pledged_pct,year,month,goal_dim, till_days_dim, pct_dim, state_days_dim, pledged_rng, goal_rng\n");
    $res = pg_query("select p.id as id,deadline, currency, pname, goal, pledged, \"state\", to_timestamp(launchedat) as launch_time, ((deadline-launchedat)/86400) as till_days,  ((statechangedat-launchedat)/86400) as state_days, cr.cname as creator_name, bakerscount, cat.cname as category_name , cat.parentid as parent_category, categoryposition, l.lname as loc_name, l.cstate as loc_state, l.ccountry as loc_country from project as p, creator as cr, category as cat, location as l where l.id=p.l_id and cr.id=p.creator_id and cat.id=p.cat_id;");
    while ($row2 = @pg_fetch_assoc($res)) {
        if (0 == $row2['launch_time']) {
            continue;
        }
        // skip fake projects
        if (100 < $row2['till_days']) {
            continue;
        }
        // export row
        $line = array();
        $line[] = $row2['id'];
        $line[] = convert_to_cps_date($row2['launch_time']);
        $line[] = $row2['till_days'];
        $line[] = preg_replace('/[^A-Za-z0-9\\ \\-]/', '', $row2['pname']);
        $pcat = (int) $row2['parent_category'];
        if (0 == $pcat) {
            $line[] = $row2['category_name'];
        } else {
            $line[] = $category[$pcat];
        }
        $currency = $row2['currency'];
        //process currency
        $goal = $line[] = convert_to_usd($curr, $currency, $row2['goal']);
        $pledged = $line[] = convert_to_usd($curr, $currency, $row2['pledged']);
        // rest of
        $line[] = $row2['state'];
        $line[] = $row2['state_days'];
        $line[] = preg_replace('/[^A-Za-z0-9\\ \\-]/', '', $row2['creator_name']);
        $line[] = $row2['bakerscount'];
        $line[] = $row2['category_name'];
        $line[] = $row2['categoryposition'];
        $line[] = $row2['loc_name'];
        $line[] = $row2['loc_state'];
        if (!isset($cntryarr[$row2['loc_country']])) {
            $line[] = "unknown country";
        } else {
            $line[] = $cntryarr[$row2['loc_country']];
        }
        $days_left = floor(($row2['deadline'] - time()) / 86400);
        if (0 > $days_left) {
            $days_left = 0;
        }
        $line[] = $days_left;
        if (0 == $goal) {
            $goal = 1.0E-5;
        }
        $line[] = floor($pledged / $goal * 100);
        //$line[]=$row2['loc_country'];
        $launch_date = strtotime($row2['launch_time']);
        $line[] = date("Y", $launch_date);
        $line[] = date("M", $launch_date);
        $line[] = $goal;
        $line[] = $row2['till_days'];
        $line[] = floor($pledged / $goal * 100);
        $line[] = $row2['state_days'];
        $line[] = get_range($pledged);
        $line[] = get_range($goal);
        fputcsv($fw, $line);
    }
    fclose($fw);
    $zip = new ZipArchive();
    $filename = $fname . ".zip";
    if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
        die("cant open <{$filename}>\n");
    }
    $zip->addFile($fname, "data.csv");
    $zip->close();
    @unlink($fname);
    return $filename;
}
示例#2
0
//$fname="$wwwrealpath/ppfake.csv";  // fake file
//$server_url=$base_url."?s=ImportService&delimeter=,&nullstr=null&reload=1&withheader=1&name=dspaypal&reloaddim=1";
//$host_reply=askhost($server_url.$sk.$schemajson, array('file_contents'=>'@'.$fname),"","","",120000,array("appid: $capsidea_appid","sig: $secret"),true);// defined in askhost.php
//$jsonres=json_decode($host_reply["data"], true);
//$ikey=(int)$jsonres["Key"];
//if (0==$ikey) $ikey=$cdata["k"]; // $ikey = something useful
//$sk="&schemakey=".$ikey;
// get real data - $ftp_response
if (0 != $parent_key) {
    //  uploading parent content
    file_put_contents($my_data_dir . "/" . $hash . ".prc", "50");
    $source_id = $parent_key;
    $cases = load_cases_from_db($source_id);
    $ware = array();
    $merchant = load_merchant_data_from_db($source_id, $ware);
    $cntryarr = load_countries_from_csv("./country.csv");
    $report_fname = generate_application_report_to_cvs($tmp_dir, $dbconn, $source_id, $cases, $merchant, $ware, $cntryarr);
    unset($cases);
    unset($merchant);
    unset($ware);
}
// else { // bad luck, jfdi - load data, process, link, and generate report
// 	$file_num=0;
// 	foreach ($ret as $fname) {
// 		$merchant_cube_id=$ikey;
// 		file_put_contents($my_data_dir."/".$hash.".prc", 11+$file_num); // report progress
// 		$client_dir=$tmp_dir.$merchant_cube_id."/";
// 		if ((FALSE===strpos($fname, "TRR-"))&&(FALSE===strpos($fname, "DDR-"))) continue; // unknown file, skip it
// 		if (FALSE!==strpos($fname, ".gz")) {$dbg=$dbg."archive $fname\n";continue;} // file is archive (empty? broken?)
// 		$realfname=$fname;
// 		$fname=str_replace("/ppreports/outgoing/", "", $realfname);