<?php // BEGIN PHP

includeContainer("_library_page_lib.php", 1);

$current_lang = $_SESSION['lang']['code'];
$current_lang_short_code = $_SESSION['lang']['shortcode'];

$mcid = getDolGlobalInt("MARKETPLACE_ROOT_CATEGORY_ID");
if (empty($mcid)) {
  print $weblangs->trans("variableNotDefined", 'MARKETPLACE_ROOT_CATEGORY_ID');
  exit;
}
$root_cat_object = new Categorie($db);
$result = $root_cat_object->fetch($mcid);
$root_category_id = $root_cat_object->id;
$root_category_type = $root_cat_object->type;

$current_category_id = GETPOST('cat', 'alpha');
// END PHP 
?>
<!-- BEGIN LEFT SIDEBAR -->
<div id="left_column" class="column col-xs-12 col-sm-3">
  <section id="left_column_1">
    <div id="categories_block_left" class="block">
      <h2 class="title_block"> <?php print $weblangs->trans("sidebarCategories"); ?> </h2>
      <div class="block_content">
        <?php
        $organized_tree = getOrganizedTree($mcid);
        if (count($organized_tree) < 1) {
            print '<tr class="oddeven nobottom">';
            print '<td colspan="3"><span class="opacitymedium">' . $weblangs->trans("NoSubCat") . '</span></td>';
            print '</tr>';
        }
        ?>
        <ul class="tree dhtml">
          <?php foreach ($organized_tree as $key => $value) : ?>
            <?php if ($value['label'] != "Versions" && $value['label'] != "Specials") : ?>
              <li>
                <a href="index.php?cat=<?php print $value['rowid']; ?>&title=<?php print dolSlugify($value['label']); ?>&l=<?php print $current_lang_short_code; ?>" <?php ($current_category_id == $value['rowid']) ? print 'class="selected"' : ''; ?> title=""> <?php print $value['label']; ?> </a>
                <?php if (isset($value['children'])) : ?>
                  <ul>
                    <?php usort($value['children'], build_sorter('position')); ?>
                    <?php foreach ($value['children'] as $key_children => $value_children) : ?>
                      <li>
                        <a href="index.php?cat=<?php print $value_children['rowid']; ?>&title=<?php print dolSlugify($value_children['label']); ?>&l=<?php print $current_lang_short_code; ?>" <?php ($current_category_id == $value_children['rowid']) ? print 'class="selected"' : ''; ?> title="<?php print dol_escape_htmltag(strip_tags($value_children['description'])); ?>"> <?php print $value_children['label']; ?> </a>
                      </li>
                    <?php endforeach ?>
                  </ul>
                <?php endif ?>
              </li>
            <?php endif ?>
          <?php endforeach ?>
        </ul>
      </div>
    </div>
  </section>
          
  <div id="tags_block_left" class="block tags_block">
    <p class="title_block"> <?php print $weblangs->trans("versions"); ?> </p>
    <div class="block_content">
      <?php
        $mvcid = getDolGlobalInt("MARKETPLACE_VERSIONS_CATEGORY_ID");
        if (empty($mvcid)) {
            print $weblangs->trans("variableNotDefined", 'MARKETPLACE_VERSIONS_CATEGORY_ID');
        } else {
            $full_version_tree = getOrganizedTree($mvcid, 'label');
            if (count($full_version_tree) < 1) {
                print '<tr class="oddeven nobottom">';
                print '<td colspan="3"><span class="opacitymedium">' . $langs->trans("NoSubCat") . '</span></td>';
                print '</tr>';
            }
        }
      ?>

      <?php foreach (array_reverse($full_version_tree) as $key => $value) : ?>
        <?php if ($value['rowid'] != $mvcid) : ?>
          <a class="tag_version" href="index.php?tag=<?php print $value['label']; ?>" title="<?php print $value['description']; ?>"> <?php print $value['label']; ?> </a>
        <?php endif ?>
      <?php endforeach ?>
    </div>
  </div>


  <div id="special_block_right" class="block hideonsmartphone">
    <p class="title_block">
      <a href="#" title="Specials"> <?php print $weblangs->trans("specialPromo"); ?> </a>
    </p>
    <div class="block_content products-block">
      <ul>
        <?php
        $mcsid = getDolGlobalInt("MARKETPLACE_SPECIAL_CATEGORY_ID");
        if (empty($mcsid)) {
          print $weblangs->trans("variableNotDefined", 'MARKETPLACE_SPECIAL_CATEGORY_ID');
          exit;
        }
        $root_cat_object = new Categorie($db);
        $result = $root_cat_object->fetch($mcsid);
        $limit = 2;
        
        $sql = "SELECT c.fk_product as id, o.ref, o.ref_ext, ol.label, ol.description, o.datec, o.price_ttc ";
        $sql .= "FROM llx_categorie_product as c, ";
        $sql .= "llx_product as o, ";
        $sql .= "llx_product_lang as ol ";
        $sql .= "WHERE o.entity IN (1) AND c.fk_categorie = ".$root_cat_object->id." AND c.fk_product = o.rowid AND ol.fk_product = o.rowid ";
        $sql .= "AND ol.lang = '".$db->escape($current_lang)."' AND o.tosell = 1 ";
        $sql .= " ORDER BY RAND() ";
        //$sql .= $db->order('datec', 'DESC');
        $sql .= $db->plimit($limit);

        if ($resql = $db->query($sql)) {
		    $key = 0;
			while ($product = $resql->fetch_object()) {
			    $product->element = "product";
			    
			    $discount = marketplace_check_discount($product->id);
		        $discounted_price = marketplace_calculate_discounted_price($product->price_ttc, $discount);

                $thumb = getImagePublicURLOfObject($product, 1, '_small');
                ?>
                <li class="clearfix">
                    <a class="products-block-image" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>">
                      <img class="replace-2x img-responsive width98" src="<?php print $thumb; ?>" alt="" title="<?php print $product->label; ?>" />
                    </a>
                    <div class="product-content">
                      <h5>
                        <a class="product-name truncate2" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>" title="<?php print $product->label; ?>"> <?php print $product->label; ?> </a>
                      </h5>
                      <p class="product-description truncate2">
                        <?php
                        if (isset($product->description)) {
                          print dolPrintHTML(dol_string_nohtmltag($product->description));
                        }
                        ?>
                      </p>
                      <div class="price-box">
                            <?php if ($discounted_price != $product->price_ttc): ?>
                            	<span class="price special-price"> <?php print price($discounted_price); ?> € </span>
                                <span class="old-price"> <?php print price($product->price_ttc); ?> € </span>
                            <?php else: ?>
                                <span class="price"> <?php print price($product->price_ttc); ?> € </span>
                            <?php endif ?>
                        <!--<span class="price-percent-reduction">-50%</span>
        				<span class="old-price"> 300,00 € </span>-->
                      </div>
                    </div>
                </li>
        <?php 
			}
		}
        ?>
      </ul>
      <div>
        <a class="btn btn-default button button-small" href="index.php?tag=Specials" title="<?php print $weblangs->trans("allSpecials"); ?>">
          <span><?php print $weblangs->trans("allSpecials"); ?> <i class="icon-chevron-right right"></i>
          </span>
        </a>
      </div>
    </div>
  </div>

  <div id="new-products_block_right" class="block products_block hideonsmartphone" contenteditable="true">
    <h4 class="title_block">
      <a href="#" title="New products"><?php print $weblangs->trans("newProducts"); ?></a>
    </h4>
    <div class="block_content products-block">
      <ul class="products">
        <?php
        $result = $root_cat_object->fetch($root_category_id, "");
        if ($result <= 0) {
          dol_print_error($db, $root_cat_object->error);
          exit;
        }
        $limit = 2;
        
        $sql = "SELECT c.fk_product as id, o.ref, o.ref_ext, ol.label, ol.description, o.datec, o.price_ttc ";
        $sql .= "FROM llx_categorie_product as c, ";
        $sql .= "llx_product as o, ";
        $sql .= "llx_product_lang as ol ";
        $sql .= "WHERE o.entity IN (1) AND c.fk_categorie = ".$root_cat_object->id." AND c.fk_product = o.rowid AND ol.fk_product = o.rowid ";
        $sql .= "AND ol.lang = '".$db->escape($current_lang)."' AND o.tosell = 1";

        $delay_for_new = (int) getDolGlobalInt('MARKETPLACE_DELAY_FOR_NEW', 30);
        $dateTime = new \DateTime();
        $interval = new \DateInterval('P'.$delay_for_new.'D');
        $dateTime->sub($interval);
        $sql .= " AND o.datec > '" . $dateTime->format('Y-m-d H:i:s') . "'";

        //$sql .= $db->order('datec', 'DESC');
        $sql .= " ORDER BY RAND() ";
        $sql .= $db->plimit($limit);

        if ($resql = $db->query($sql)) {
		    $key = 0;
			while ($product = $resql->fetch_object()) {
			    $product->element = "product";
			    
			    $discount = marketplace_check_discount($product->id);
		        $discounted_price = marketplace_calculate_discounted_price($product->price_ttc, $discount);

                $thumb = getImagePublicURLOfObject($product, 1, '_small');
                ?>
                <li class="clearfix">

                    <a class="products-block-image" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>" title="<?php print $product->label; ?>">
                      <img class="replace-2x img-responsive width98" src="<?php print $thumb; ?>" alt="" title="<?php print $product->label; ?>" />
                    </a>
                    <div class="product-content">
                      <h5>
                        <a class="product-name truncate2" href="product.php?id=<?php print $product->id; ?>&title=<?php print dolSlugify($product->label); ?>&l=<?php print $current_lang_short_code; ?>" title="<?php print $product->label; ?>"> <?php print $product->label; ?> </a>
                      </h5>
                      <p class="product-description truncate2">
                        <?php
                        if (isset($product->description)) {
                          print dolPrintHTML(dol_string_nohtmltag($product->description));
                        }
                        ?>
                      </p>
                      <div class="price-box">
        				<?php if ($discounted_price != $product->price_ttc): ?>
                        	<span class="price special-price"> <?php print price($discounted_price); ?> € </span>
                            <span class="old-price"> <?php print price($product->price_ttc); ?> € </span>
                        <?php else: ?>
                            <span class="price"> <?php print price($product->price_ttc); ?> € </span>
                        <?php endif ?>
                      </div>
                    </div>
                </li>
        <?php 
			} 
        }
        ?>
      </ul>
      <div>
        <a href="index.php?cat=<?php print $root_category_id; ?>&list=new" title="<?php print $weblangs->trans("allNewProducts"); ?>" class="btn btn-default button button-small">
          <span><?php print $weblangs->trans("allNewProducts"); ?> <i class="icon-chevron-right right"></i>
          </span>
        </a>
      </div>
    </div>
  </div>
</div>
<!-- END LEFT SIDEBAR -->