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

include_once 'auth.php';

session_start();

ob_start();

//include 'func.php';
require_once 'htmlhead.php';

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


$db = mysql_connect("smashhit.db.8821700.hostedresource.com", "smashhit", "Pass2012!") or die(mysql_error());
mysql_select_db("smashhit",$db) or die(mysql_error());



$lau = isset($_GET['lau']) ? (int)$_GET['lau'] : null;
if ($lau && is_numeric($lau)) {
	$qry = 'select * from bc_user where user_ = "' . mysql_escape_string($lau) .  '"';
    $_res = @mysql_query($qry);
    $udata = @mysql_fetch_assoc($_res);
    if (!empty($udata)) {
    	$token = md5(time() . $udata['bc_user'] . time() . $udata['username'] . time() . $udata['password']);
        $_SESSION[$token] = $udata;
        echo '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"

			id="BussinessCard" width="905" height="800"

			codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> 
			<param name="movie" value="BussinessCard.swf?userlogin=yes&token=' . $token . '" /> 
			<param name="quality" value="high" /> 
			<param name="bgcolor" value="#869ca7" /> 
			<param name="allowScriptAccess" value="sameDomain" /> 
			<embed src="BussinessCard.swf?userlogin=yes&token=' . $token . '" quality="high" bgcolor="#869ca7"
				width="905" height="800" name="BussinessCard" align="middle"
				play="true"
				loop="false"
				quality="high"
				allowScriptAccess="sameDomain"
				type="application/x-shockwave-flash"
				pluginspage="http://www.adobe.com/go/getflashplayer"> 
			</embed> 
	</object> ';
	} else {
    	die("Error: Can't find user by id. Please try again.");
	}
	
	require_once 'htmlfoot.php';
	
	$data = ob_get_contents();
	ob_clean();
	
	echo $data;
	die;
}

$statuses = array(
    1    =>    'pending',
    2    =>    'inprogress',
    3    =>    'completed'
);
#dump($_POST);die;
$act = isset($_POST['act']) ? $_POST['act'] : null;
if ($act == 'manage') {

    $action = isset($_POST['action']) ? $_POST['action'] : null;
    if ($action == 'Delete') {
        $cid = isset($_POST['cid']) ? $_POST['cid'] : array();
        if ($cid) {
            $del = array();
            foreach ($cid as $id) {
                if (is_numeric($id)) {
                    $del[] = $id;
                }
            }
            $qry = 'delete from job where job_ in (' . implode(',', $del) . ')';
            mysql_query($qry) or die(mysql_error());
        }
    } elseif ($action == 'Update') {
        $cid = isset($_POST['cid']) ? $_POST['cid'] : array();
        $status = isset($_POST['status']) ? $_POST['status'] : '';
        if ($status && $cid && isset($statuses[$status])) {
            $upd = array();
            foreach ($cid as $id) {
                if (is_numeric($id)) {
                    $upd[] = $id;
                }
            }
            $qry = 'update job set status = "' . mysql_escape_string($statuses[$status]) . '" where job_ in (' . implode(', ', $upd) . ')';
            mysql_query($qry) or die(mysql_error());
        }
    }
    header('Location: ' . $self_url . 'managejobs.php');
    ob_clean();
    die;
}

?>

<h3>Manage Jobs</h3>
<a href="managejobs.php?sort=name">Sort By Name</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="managejobs.php?sort=date">Sort By Date</a>
<?php
 if(isset ($_GET['sort']) && $_GET['sort']=="name" ){
     $qry = 'select j.*, u.username, u.user_, uc.cardid from job as j inner join usercard as uc on (uc.usercard_ = j.usercard_) inner join bc_user as u on (u.user_ = uc.user_) order by username';
    $jobs = mysql_query($qry) or die(mysql_error());
}
    
else{
$qry = 'select j.*, u.username, u.user_, uc.cardid from job as j inner join usercard as uc on (uc.usercard_ = j.usercard_) inner join bc_user as u on (u.user_ = uc.user_) order by postdate desc';
$jobs = mysql_query($qry) or die(mysql_error());
}
?>

<table class="data">
	<tr>
		<th>
			Username
		</th>
		<th>
			Thumbnail
		</th>
        <th>
            Download
        </th>
        <th>
			Date
		</th>
		<th>
			Status
		</th>
		<th>

		</th>
	</tr>
<?php

$res = mysql_query($qry) or die(mysql_error());

while (($row=mysql_fetch_assoc($res))!==false) {
    
    ?>
	<tr>
		<td>
			<?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8' ); ?>
		</td>
		<td>
			<a href="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/back_preview.png'; ?>" target="_blank">
				<img src="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/front_preview.png'; ?>" />
			</a>
		</td>
        <td style="font-family: Arial,Verdana; font-size: 10px; text-align: left;">
            <a href="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/rendered_front.png'; ?>" target="_blank">Front PNG</a><br />
            <a href="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/rendered_back.png'; ?>" target="_blank">Back PNG</a><br /><br />
            <a href="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/rendered_pdf_front.pdf'; ?>" target="_blank">Front PDF</a><br />
            <a href="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/rendered_pdf_back.pdf'; ?>" target="_blank">Back PDF</a><br />
            <a href="<?php echo $self_url . $user_data_dir . $row['user_'] . '/' . $row['cardid'] . '/proof.pdf'; ?>" target="_blank">Proof PDF</a><br /><br />
            <a href="managejobs.php?lau=<?php echo $row['user_']; ?>" target="_blank">Login as user</a>
        </td>
        <td>
			<?php echo $row['postdate']; ?>
		</td>
		<td>
			<form action="managejobs.php" method="post">
			<input type="hidden" name="cid[]" value="<?php echo $row['job_']; ?>" />
			<input type="hidden" name="act" value="manage" />
			<select name="status">
				<option value="1" <?php if($row['status'] == 'pending') echo 'selected'; ?>>pending</option>
				<option value="2" <?php if($row['status'] == 'inprogress') echo 'selected'; ?>>in progress</option>
				<option value="3" <?php if($row['status'] == 'completed') echo 'selected'; ?>>completed</option>
			</select>
			<input type="submit" name="action" value="Update" />
			</form>
		</td>
		<td>
			<form action="managejobs.php" method="post">
			<input type="hidden" name="cid[]" value="<?php echo $row['job_']; ?>" />
			<input type="hidden" name="act" value="manage" />
			<input type="submit" name="action" value="Delete" />
			</form>
		</td>
	</tr>
<?php
}
?>
</table>


<?php

require_once 'htmlfoot.php';

$data = ob_get_contents();
ob_clean();

echo $data;
?>