Files
wyczarujprezent.pl/modules/configurator/controllers/admin/AdminConfiguratorController.php
2024-10-28 22:14:22 +01:00

208 lines
10 KiB
PHP

<?php
require(dirname(__FILE__).'/../../classes/My_PDF.php');
class AdminConfiguratorController extends ModuleAdminController {
public function __construct()
{
parent::__construct();
$this->controller_displayName = $this->l('Settings');
$this->toolbar_title = $this->l('Settings');
$this->bootstrap = true;
}
public function postProcess()
{
if (Tools::isSubmit('submitConfiguratorSettings'))
{
Configuration::updateValue('configurator_from_date', Tools::getValue("configurator_from_date"));
Configuration::updateValue('configurator_to_date', Tools::getValue("configurator_to_date"));
$configurator_group = Tools::getValue("configurator_group");
$q = "SELECT `name` FROM `" . _DB_PREFIX_ . "configurator_group` WHERE `id_configurator_group` = ".(int)$configurator_group;
$configurator_group_name = Db::getInstance()->getValue($q);
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config_product` where `date` >= '".Tools::getValue("configurator_from_date")."' AND `date` <= '".Tools::getValue("configurator_to_date")."' AND `id_configurator_group` = ".(int)$configurator_group;
$configsproducts = Db::getInstance()->executeS($q);
array_map('unlink', array_filter((array) glob(dirname(__FILE__)."/../../tmp/*/*")));
array_map('unlink', array_filter((array) glob(dirname(__FILE__)."/../../tmp/*")));
$zipUrl = dirname(__FILE__)."/../../tmp/".$configurator_group_name."_".date("Y-m-d").".zip";
if(file_exists($zipUrl)) {
unlink($zipUrl);
}
$zip = new ZipArchive();
if ($zip->open($zipUrl, ZipArchive::CREATE) === TRUE) {
foreach ($configsproducts as $configproduct) {
try {
$cart = new Cart($configproduct['id_cart']);
$customer = new Customer($cart->id_customer);
$this->saveConfigFile($configproduct, $customer->email);
$zip->addFile(dirname(__FILE__).'/../../tmp/'.$customer->email.'/config_product_'.$configproduct['id_config_product'].'.pdf', $customer->email."/".$configproduct['id_config_product'].'.pdf');
} catch (Exception $e) {
}
}
$zip->close();
}
// Header content type
header("Content-type: application/zip");
header("Content-Length: " . filesize($zipUrl));
header('Content-disposition: attachment; filename="'.$configurator_group_name."_".date("Y-m-d").'.zip"');
ob_clean();
flush();
die(readfile($zipUrl));
}
}
public function saveConfigFile($configproduct, $email)
{
if(!file_exists(dirname(__FILE__).'/../../tmp/'.$email)) {
mkdir(dirname(__FILE__).'/../../tmp/'.$email, 0755);
}
$configurator_fields = array();
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config` where `id_product` = ".(int)$configproduct['id_cart_product'].' AND `id_page` = '.(int)$configproduct['page'];
$configsIds = Db::getInstance()->executeS($q);
$fonts = json_decode(file_get_contents(dirname(__FILE__)."/../../fonts.json"), true);
if($configsIds) {
foreach ($configsIds as $configId) {
$print_width = $configId['print_width'];
$print_height = $configId['print_height'];
$orientation = PDF_PAGE_ORIENTATION;
if($configId['print_width'] < $configId['print_height']) {
$print_width = $configId['print_height'];
$print_height = $configId['print_width'];
$orientation = 'H';
}
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config_field_value` where `id_config` = ".$configId['id_config']." AND `id_config_product` = ".$configproduct['id_config_product'];
$fields = Db::getInstance()->executeS($q);
$html_first = '<img id="configurator_main_image" src="'.'https://wyczarujprezent.pl'.$configId['image'].'"/>';
$pdf = new My_PDF($orientation, PDF_UNIT, array($this->getUnitsCount($print_width), $this->getUnitsCount($print_height)), true, 'UTF-8', true);
$pdf->changeTheDefault(false);
$pdf->AddPage();
$pdf->SetMargins(0, 0, 0,true);
$pdf->writeHTMLCell($this->pixelsToUnits($configId['width']), $this->pixelsToUnits($configId['height']), 0, 0, $html_first);
foreach ($fields as $field) {
$width = (float)(($configId['width']*$field['width'])/100);
$height = (float)(($configId['height']*$field['height'])/100);
$x = (float)(($configId['width']*$field['x'])/100);
$y = (float)(($configId['height']*$field['y'])/100);
foreach($fonts as $font) {
if($font['font-family'] === $field['font']) {
$fontname = TCPDF_FONTS::addTTFfont($font['src'], 'TrueTypeUnicode', '', 96);
$pdf->SetFont($fontname, '', $this->pixelsToUnits($field['fontsize']), '', false);
break;
}
}
// die($this->percentToUnits($configId['width'], $width).", ".$this->percentToUnits($configId['height'], $height).", ".$this->percentToUnits($configId['width'], $x).", ".$this->percentToUnits($configId['height'], $y));
if($field['type'] == 1) {
$html_first = '<div class="configurator_field" style="line-height: normal;transform: rotate('.$field['rotation'].'deg);'.(isset($field['textalign']) && $field['textalign'] ? ' text-align: '.$field['textalign'].';' : '').' color: '.$field['color'].'; overflow: hidden; font-size: '.$this->pixelsToUnits($field['fontsize']).'mm;">'.$field['text'].'</div>';
$pdf->writeHTMLCell($this->pixelsToUnits($width), $this->pixelsToUnits($height), $this->pixelsToUnits($x), $this->pixelsToUnits($y), $html_first);
}
else {
$html_first = '<img id="configurator_main_image" src="'.'https://wyczarujprezent.pl'.$field['text'].'" width="'.$this->pixelsToUnits($width).'mm" height="'.$this->pixelsToUnits($height).'mm"/>';
$pdf->writeHTMLCell($this->pixelsToUnits($width), $this->pixelsToUnits($height), $this->pixelsToUnits($x), $this->pixelsToUnits($y), $html_first);
}
}
$pdf->endPage();
$pdf->Output(dirname(__FILE__).'/../../tmp/'.$email.'/config_product_'.$configproduct['id_config_product'].'.pdf', 'F');
}
}
}
public function percentToUnits($pixels, $percentage) {
return (float)(($pixels*($percentage/100)) / 12);
}
public function pixelsToUnits($pixels) {
return (float)($pixels / 12);
}
public function unitsToPixels($units) {
return (float)($units * 12);
}
public function getUnitsCount($units) {
return (float)$this->pixelsToUnits($this->unitsToPixels($units));
}
public function renderList()
{
return $this->renderForm();
}
public function renderForm()
{
$q = "SELECT * FROM `" . _DB_PREFIX_ . "configurator_group`";
$configurator_groups = Db::getInstance()->executeS($q);
$this->action_displayName = $this->l('Settings');
$this->fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings')
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Nazwa grupy'),
'name' => 'configurator_group',
'options' => array(
'query' => $configurator_groups,
'id' => 'id_configurator_group',
'name' => 'name'
)
),
array
(
'type' => 'datetime',
'label' => $this->l('Pobierz od daty'),
'name' => 'configurator_from_date',
'lang' => false,
'autoload_rte' => true
),
array
(
'type' => 'datetime',
'label' => $this->l('Pobierz do daty'),
'name' => 'configurator_to_date',
'lang' => false,
'autoload_rte' => true
)
),
'submit' => array(
'title' => $this->l('Pobierz'),
'name' => 'submitConfiguratorSettings',
'class' => 'btn btn-default'
)
);
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper = new HelperForm();
$helper->default_form_language = $lang->id;
$helper->show_toolbar = true;
$helper->table = $this->table;
$helper->token = Tools::getAdminTokenLite('AdminConfiguratorController');
$helper->module = $this->module;
$helper->fields_value = $this->getConfiguration();
unset($this->toolbar_btn['back'], $this->toolbar_btn['new']);
return $helper->generateForm(array($this->fields_form[0]));
}
public function getConfiguration()
{
$fields = array(
'configurator_from_date' => Tools::getValue('configurator_from_date', Configuration::get('configurator_from_date')),
'configurator_to_date' => Tools::getValue('configurator_to_date', Configuration::get('configurator_to_date')),
);
return $fields;
}
// Temporary bypass functions
public function listHelperAction() {}
}