<?php
/*
* Copyright Bekanntmacher
*
* @author Simon Wohler <s.wohler@bekanntmacher.ch>
* @license commercial
*/
namespace Bekanntmacher\BoxmakerBundle\Module;
use Bekanntmacher\BoxmakerBundle\Classes\Config;
use Bekanntmacher\BoxmakerBundle\Classes\UserInput;
use Bekanntmacher\BoxmakerBundle\Model\FarbeModel;
use Bekanntmacher\BoxmakerBundle\Model\TypeModel;
use Bekanntmacher\BoxmakerBundle\Model\ConfigModel;
use Bekanntmacher\BoxmakerBundle\Model\WelleModel;
use Bekanntmacher\BoxmakerBundle\Model\TypeOptionModel;
use Bekanntmacher\BoxmakerBundle\Calculation\OptionCalculator;
use Contao\System;
use Haste\Util\Url;
use Mpdf;
use Contao\File;
use Haste\Form\Form;
/**
* Boxmaker Typelist
*/
class TypeDetail extends TypeUtil
{
/**
* @var string Template
*/
protected $strTemplate = 'mod_boxmaker_typedetail';
protected $objType = null;
protected $objConfig = null;
protected $objUserInput = null;
/**
* Display a wildcard in the back end
* @return string
*/
public function generate()
{
// Display a wildcard in the back end
if (TL_MODE === 'BE')
{
$template =new \BackendTemplate('be_wildcard');
$template->wildcard = '### BOXMAKER TYPE DETAIL ###';
$template->title = $this->name;
$template->id = $this->id;
$template->link = $this->name;
$template->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
// Type
$objTypeModel = TypeModel::findPublishedById((int) \Input::get('type'), array('eager' => true));
if ($objTypeModel === null)
{
global $objPage;
$objHandler = new $GLOBALS['TL_PTY']['error_404']();
$objHandler->generate($objPage->id);
}
// Type and type_config
$arrType = $this->compileTypeData($objTypeModel);
$this->objType = new Type($arrType);
$this->objType->addTypeConfig($objTypeModel->getRelated('type_config_set')->row());
$this->Template->type = $this->objType->getAll();
$this->Template->typeImagePaths_json = json_encode($arrType['image']);
// Boxmaker Konfiguration
$this->objConfig = new Config(ConfigModel::findByPk($this->boxmaker_config)->row());
// User Input
$this->objUserInput = new UserInput($this->objConfig, $this->objType->type_standardwelle);
$this->objUserInput->set('farbe_title', FarbeModel::findByPk($this->objUserInput->farbe)->selecttitle);
$this->Template->userInput = $this->objUserInput->getAll();
// Preise berechnen
$this->arrPrices = $this->addTypePrices();
$this->Template->prices_json = json_encode($this->arrPrices['arrPriceTotal']);
// Wellen Table
$this->generateWellenTable();
// Select Druckverfahren
$str_1f_selected = ($this->objUserInput->druckverfahren != null && $this->objUserInput->druckfarben == '_1f') ? $this->objUserInput->druckverfahren : $this->arrPrices['cheapestPrintingMethod']['_1f'];
$arr_1f_select = $this->getPrintingMethodSelect('1f');
$this->Template->printingMethodSelect_1f = (count($arr_1f_select)) ? $arr_1f_select : [];
$this->Template->printingMethodSelect_1f_selected = str_replace('_1f','', $str_1f_selected);
$str_4f_selected = ($this->objUserInput->druckverfahren != null && $this->objUserInput->druckfarben == '_4f') ? $this->objUserInput->druckverfahren : $this->arrPrices['cheapestPrintingMethod']['_4f'];
$arr_4f_select = $this->getPrintingMethodSelect('4f');
$this->Template->printingMethodSelect_4f = (count($arr_4f_select)) ? $arr_4f_select : [];
$this->Template->printingMethodSelect_4f_selected = str_replace('_4f','', $str_4f_selected);
$this->Template->dva = ($this->objUserInput->druckfarben !== null) ? $this->objUserInput->druckfarben : 'blank';
$this->Template->sa = $this->objUserInput->seite;
// CHF, Wellenbild, etc.
$this->addGeneralTemplateData();
$this->addWellenInfo();
// Druckbild Weiss hinterlegt
$objOptionCalc = new OptionCalculator($this->objType,$this->objUserInput);
$objO = new \stdClass();
$objO->surcharge_stk = $this->objConfig->hg_print_surcharge_stk;
$objO->surcharge_m2 = $this->objConfig->hg_print_surcharge_m2;
$objO->surcharge_total = $this->objConfig->hg_print_surcharge_total;
$this->Template->white_background_title = $this->objConfig->wp_title;
$this->Template->white_background_price = $this->getAmount($objOptionCalc->calculateOptionPriceStk($objO), true, true, false);
// Lieferzeit
$this->generateDeliveryTable();
// Muster
$this->Template->boxmaker_price_sample_blank = $this->objConfig->boxmaker_price_sample_blank;
$this->Template->boxmaker_price_sample_printed = $this->objConfig->boxmaker_price_sample_printed;
// Debug Info
$this->Template->showDebugInfo = false;
if (System::getContainer()->get('contao.security.token_checker')->hasBackendUser()) {
$this->Template->showDebugInfo = true;
$this->Template->cheapestProductionMethod = $this->arrPrices['cheapestProductionMethod'];
$this->Template->debug = print_r($this->arrPrices,true);
}
// Weiterleitungsseiten
$objJumpToCart = \PageModel::findByPk($this->boxmaker_jumpToCart);
if($objJumpToCart !== null) {
$this->Template->boxmaker_jumpToCart = $objJumpToCart->getFrontendUrl();
}
$objJumpToStart = \PageModel::findByPk($this->boxmaker_jumpToStart);
if($objJumpToStart !== null) {
$this->Template->boxmaker_jumpToStart = $objJumpToStart->getFrontendUrl();
}
}
// generate Deliverytable
private function generateDeliveryTable()
{
$idWelle = ($this->objUserInput->welle != null) ? $this->objUserInput->welle : $this->objType->type_standardwelle;
$idFarbe = ($this->objUserInput->farbe != null) ? $this->objUserInput->farbe : 1;
$arrTableRaw = $this->arrPrices['arrDeliverySurcharge'][$idWelle][$idFarbe]['blank'];
$arrTable = array();
foreach($arrTableRaw as $methode => $surcharge)
{
$arrTable[$methode] = array
(
'title' => $this->getDeliveryDate($methode),
'price' => $surcharge,
'class' => ($surcharge == 0) ? 'active' : ''
);
}
$arrLabels = array
(
'p' => $this->getDeliveryDate('p'),
'ht' => $this->getDeliveryDate('ht'),
'ap' => $this->getDeliveryDate('ap'),
);
$this->Template->deliveryPrices_json = json_encode($this->arrPrices['arrDeliverySurcharge']);
$this->Template->deliveryDateLabels_json = json_encode($arrLabels);
$this->Template->deliveryDateLabels_json;
$this->Template->deliveryTable = array_reverse($arrTable);
$this->Template->delivery_date = $this->getDeliveryDate($this->arrPrices['cheapestProductionMethod']);
}
// ok generate Wellentable
private function generateWellenTable()
{
$objWellen = WelleModel::findAllOrderedBySorting();
$objFarben = FarbeModel::findAllOrderedBySorting();
$arrMaterial = $this->arrPrices['arrAll']['material'][$this->arrPrices['cheapestProductionMethod']];
$idWelle = ($this->objUserInput->welle != null) ? $this->objUserInput->welle : $this->objType->type_standardwelle;
$arrTable = array();
$arrJson = array();
foreach ($objWellen as $welle)
{
$arrTable[$welle->id]['title'] = $welle->title;
foreach ($objFarben as $f)
{
$arrTable[$welle->id]['fields'][$f->id] = array
(
'title' => $f->title,
'av' => (key_exists('farbe_'.$f->id, (array) $arrMaterial[$welle->id])) ? 1 : 0,
'class' => ($f->id == $this->objUserInput->farbe && $welle->id == $idWelle) ? ' active': ''
);
$arrJson[$welle->id][$f->id] = $f->farbe;
}
}
$this->Template->wellenHead = $objFarben->fetchEach('title');
$this->Template->wellenHead_json = json_encode($objFarben->fetchEach('selecttitle'));
$this->Template->wellenTable = $arrTable;
$this->Template->wellenFarbeAlias_json = json_encode($arrJson);
}
// ok druckverfahren für die select's als array zurückgeben
private function getPrintingMethodSelect($strF='1f')
{
$arrReturn = array();
foreach ($GLOBALS['BOXMAKER']['printingMethods'] as $method)
{
if($this->objUserInput->stk < $this->objType->{$method . '_min'})
{
continue;
}
if($method == 'blank' || strpos($method, $strF) === false || ($method == 'offsetdruck_1f')) {
continue;
}
$arrReturn[] = array (
'value' => str_replace(array('_1f','_4f'), '', $method),
'label' => $GLOBALS['BOXMAKER']['printingMethodsLabels_short'][$method]
);
}
return ($arrReturn);
}
// ok Welleninfo
private function addWellenInfo()
{
// Wellen Image und Title
$arrWellenInfo = array();
$objWelle = WelleModel::findAllOrderedBySorting();
foreach ($objWelle as $welle)
{
$image = \FilesModel::findByUuid($welle->image);
$arrWellenInfo[$welle->id] = array
(
'path' => $image->path,
'title' => $welle->title
);
}
$this->Template->wellenInfo = $arrWellenInfo[$this->objType->type_standardwelle];
$this->Template->wellenInfo_json = json_encode($arrWellenInfo);
}
}