<?php
/**
  @licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.0.9
  @date  16/06/10, 10:45
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
*/
$DB=Database::getInstance();
?>
<!-- downloads //-->
<?php
  if (isset($_GET['action']) && $_GET['action'] !='history_info') {
// Get last order id for checkout_success
    $orders_query = $DB->query("select orders_id from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by orders_id desc limit 1");
    $orders = $orders_query->fetchAssoc();
    $last_order = $orders['orders_id'];
  } elseif (isset($_GET['order_id']) && is_int($_GET['order_id']) ) {
    $last_order = $_GET['order_id'];
  } else $exe=false;

if($exe) {
// Now get all downloadable products in that order
  $downloads_query = $DB->query("select date_format(o.date_purchased, '%Y-%m-%d') as date_purchased_day, opd.download_maxdays, op.products_name, opd.orders_products_download_id, opd.orders_products_filename, opd.download_count, opd.download_maxdays from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . " opd where o.customers_id = '" . (int)$customer_id . "' and o.orders_id = '" . (int)$last_order . "' and o.orders_id = op.orders_id and op.orders_products_id = opd.orders_products_id and opd.orders_products_filename != ''");
  if ($downloads_query->__get('NumRows') > 0) {
?>

      <h4><?php echo HEADING_DOWNLOAD; ?></h4>

    <table class="download" cellspacing="0" summary="<?php echo HEADING_DOWNLOAD; ?>">
    <thead>
    <tr>
      <th>&nbsp;</th>
      <th>&nbsp;</th>
      <th>&nbsp;</th>
     </tr>
    </thead>
    <tbody>

<!-- list of products -->
<?php
    while ($downloads = $downloads_query->fetchAssoc()) {
      list($dt_year, $dt_month, $dt_day) = explode('-', $downloads['date_purchased_day']);
      $download_timestamp = mktime(23, 59, 59, $dt_month, $dt_day + $downloads['download_maxdays'], $dt_year);
      $download_expiry = date('Y-m-d H:i:s', $download_timestamp);
?>

    <tr>
<!-- left box -->
<?php
// The link will appear only if:
// - Download remaining count is > 0, AND
// - The file is present in the DOWNLOAD directory, AND EITHER
// - No expiry date is enforced (maxdays == 0), OR
// - The expiry date is not reached
      if ( ($downloads['download_count'] > 0) && (file_exists(DIR_FS_DOWNLOAD . $downloads['orders_products_filename'])) && ( ($downloads['download_maxdays'] == 0) || ($download_timestamp > time())) ) {
        echo '<td><a href="' . tep_href_link(FILENAME_DOWNLOAD, 'order=' . $last_order . '&id=' . $downloads['orders_products_download_id']) . '">' . $downloads['products_name'] . '</a></td>' . "\n";
      } else {
        echo '<td>' . $downloads['products_name'] . '</td>' . "\n";
      }
?>
<!-- right box -->
<?php
      echo '<td>' . TABLE_HEADING_DOWNLOAD_DATE . tep_date_long($download_expiry) . '</td>' . "\n" .
           '<td>' . $downloads['download_count'] . TABLE_HEADING_DOWNLOAD_COUNT . '</td>' . "\n" .
           '</tr>' . "\n";
    }
?>

      </tbody>
      </table>

<?php
    if ($_GET['action'] !='history_info') {
?>
     <p><?php printf(FOOTER_DOWNLOAD, '<a href="' . tep_href_link(FILENAME_ACCOUNT, '', 'SSL') . '">' . HEADER_TITLE_MY_ACCOUNT . '</a>'); ?></p>
<?php
    }
  }
}
?>
<!-- downloads_eof //-->