<?php
include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';

class box_new_products extends ModeleBoxes
{
	public $boxcode = "newproductsreview";
	public $boximg = "product";
	public $boxlabel = "BoxNewProductsToReview";
	public $depends = array("product", "importzugferd");

	public function __construct($db, $param = '')
	{
		$this->db = $db;
	}

	public function loadBox($max = 5)
	{
		global $langs;

		$langs->load('importzugferd@importzugferd');

		// Auf Produktseite alle Einträge zeigen
		if (strpos($_SERVER['PHP_SELF'], '/product/index.php') !== false) {
			$max = 0; // 0 = kein Limit
		}

		include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
		$productstatic = new Product($this->db);

		// Anzahl zählen
		$sql = "SELECT COUNT(*) as total FROM ".MAIN_DB_PREFIX."product WHERE ref LIKE 'New%'";
		$resql = $this->db->query($sql);
		$total = 0;
		if ($resql) {
			$obj = $this->db->fetch_object($resql);
			$total = $obj->total;
		}

		$this->info_box_head = array(
			'text' => $langs->trans("BoxNewProductsToReview").' <span class="badge">'.$total.'</span>',
			'sublink' => dol_buildpath('/importzugferd/new_products.php', 1),
			'subtext' => $langs->trans("ShowAll"),
			'subpicto' => 'object_product',
		);

		// Produkte laden
		$sql = "SELECT rowid, ref, label, datec FROM ".MAIN_DB_PREFIX."product";
		$sql .= " WHERE ref LIKE 'New%'";
		$sql .= " ORDER BY datec DESC";
		if ($max > 0) {
			$sql .= " LIMIT ".((int) $max);
		}

		$result = $this->db->query($sql);
		if ($result) {
			$num = $this->db->num_rows($result);
			$line = 0;

			while ($line < $num) {
				$objp = $this->db->fetch_object($result);

				$productstatic->id = $objp->rowid;
				$productstatic->ref = $objp->ref;
				$productstatic->label = $objp->label;

				$this->info_box_contents[$line][] = array(
					'td' => 'class="tdoverflowmax150"',
					'text' => $productstatic->getNomUrl(1),
					'asis' => 1,
				);

				$this->info_box_contents[$line][] = array(
					'td' => 'class="tdoverflowmax150"',
					'text' => $objp->label,
				);

				$this->info_box_contents[$line][] = array(
					'td' => 'class="right"',
					'text' => dol_print_date($this->db->jdate($objp->datec), 'day'),
				);

				$line++;
			}

			if ($num == 0) {
				$this->info_box_contents[0][0] = array(
					'td' => 'class="center"',
					'text' => $langs->trans("NoNewProductsToReview"),
				);
			}
		}
	}

	public function showBox($head = null, $contents = null, $nooutput = 0)
	{
		return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
	}
}
