<?php
/*
 * Created on 22.03.2010 at 12:01:16
 *
 * Encoding: UTF-8
 *
 * Author: Alexey "greek" Samoylov
 * A.Samoylov@program-ace.com.ua
 *
 * Copyright by Program Ace <http://program-ace.com>
 * All right reserved
 */

header('Content-type: application/json');
error_reporting(E_ALL);
session_start();

/*
include 'func.php';
*/

$db = mysql_connect("localhost", "additions", "appleton") or die(mysql_error());
mysql_select_db("web2print",$db) or die(mysql_error());

//$db = mysql_connect("localhost","root","root") or die(mysql_error());
//mysql_select_db("ndc360_designs") or die(mysql_error());

$userData = isset($_SESSION['userdata']) ? $_SESSION['userdata'] : null;

$result = new stdClass();
$result->result = 'success';
$result->list = array();

if (empty($userData)) {
    $result->result = 'error';
    $result->message = 'User not logged';
} else {

    $jobs = isset($_POST['jobs']) ? $_POST['jobs'] : '';

    $jobs = explode(',', $jobs);
    if (!empty($jobs)) {
        $result->message = 'Nothing to do';
        foreach ($jobs as $job) {
        	if (is_numeric($job)) {
                $qry = 'select count(*) as cnt from usercard where usercard_ = ' . mysql_escape_string($job);
                $res = mysql_query($qry) or die(mysql_error());
                if (($row=mysql_fetch_assoc($res))!==false && isset($row['cnt'])) {
                    $qry = 'insert into job (usercard_, postdate, status) values (' . mysql_escape_string($job) . ', \'' . mysql_escape_string(date('Y-m-d H:i:s')) . '\', \'pending\')';
                    $res = @mysql_query($qry);
                }
                $result->message = 'Your Card Was Submitted Successfully';
        	}
        };

        $qry = 'select uc.*, j.status from usercard as uc left join job as j on (j.usercard_ = uc.usercard_) where user_ = ' . $userData['user_'];
        $res = mysql_query($qry) or die(mysql_error());

        $rows = array();

        while (($row=mysql_fetch_assoc($res))!==false) {
            $result->list[] = $row;
        }
    } else {
    	$result->result = 'error';
        $result->message = 'Nothing to do';
    }

}

echo json_encode($result);
?>
