<?php

/**
 * i-doit Report Manager View
 *
 * @package     i-doit
 * @subpackage  Reports
 * @author      Leonard Fischer <lfischer@i-doit.org>
 * @copyright   Copyright 2013 - synetics GmbH
 * @license     http://www.i-doit.com/license
 * @since       1.0
 */
class isys_qrcode_reportview_qr_codes extends isys_report_view
{

    /**
     * @return string
     */
    public static function name()
    {
        return 'LC__REPORT__VIEW__QR_CODES';
    }

    /**
     * @return string
     */
    public static function description()
    {
        return 'LC__REPORT__VIEW__QR_CODES_DESCRIPTION';
    }

    /**
     * @return string
     */
    public function template()
    {
        return __DIR__ . '/view_qr_codes.tpl';
    }

    /**
     * @return string
     */
    public static function viewtype()
    {
        return 'LC__REPORT__VIEW__QR_CODES';
    }

    /**
     * Start-method - Implement the logic for displaying your data here.
     *
     * @author  Leonard Fischer <lfischer@i-doit.org>
     * @return  void
     */
    public function start()
    {
        if (!defined('C__MODULE__SYSTEM') || !defined('C__MODULE__QRCODE')) {
            return;
        }
        global $g_dirs;

        $l_rules = [
            'C__QR_CODE_ERROR_CORRECTION'       => [
                'p_arData'   => [
                    'l' => $this->language->get('LC__REPORT__VIEW__QR_CODES__CORRECTION__LOW'),
                    'm' => $this->language->get('LC__REPORT__VIEW__QR_CODES__CORRECTION__MEDIUM'),
                    'q' => $this->language->get('LC__REPORT__VIEW__QR_CODES__CORRECTION__QUALITY'),
                    'h' => $this->language->get('LC__REPORT__VIEW__QR_CODES__CORRECTION__HIGH')
                ],
                'p_strClass' => 'input-mini'
            ],
            'C__QR_CODE_DEFAULT_TEXT_ALIGNMENT' => [
                'p_arData'        => [
                    'left'    => $this->language->get('LC__QR_CODE_DEFAULT_TEXT_ALIGNMENT_LEFT'),
                    'center'  => $this->language->get('LC__QR_CODE_DEFAULT_TEXT_ALIGNMENT_CENTER'),
                    'right'   => $this->language->get('LC__QR_CODE_DEFAULT_TEXT_ALIGNMENT_RIGHT'),
                    'justify' => $this->language->get('LC__QR_CODE_DEFAULT_TEXT_ALIGNMENT_JUSTIFY')
                ],
                'p_strSelectedID' => 'center',
                'p_strClass'      => 'input-mini'
            ]
        ];

        $l_layouts = file_get_contents(__DIR__ . '/view_qr_code_layouts.html');
        $l_layouts = str_replace([
            '%qrcode_url%',
            '%description%',
            '%logo_url%'
        ], [
            $g_dirs['images'] . 'icons/tree/qr_code.png',
            $this->language->get('LC__REPORT__VIEW__QR_CODES__LAYOUT_DESCRIPTION'),
            $g_dirs['images'] . 'favicon.png'
        ], $l_layouts);

        $l_configuration_url = isys_helper_link::create_url([
            C__GET__MODULE_ID     => C__MODULE__SYSTEM,
            C__GET__MODULE_SUB_ID => C__MODULE__QRCODE,
            C__GET__TREE_NODE     => C__MODULE__QRCODE . '0'
        ]);

        // Assign the ajax URL to the template.
        $this->template
            ->activate_editmode()
            ->smarty_tom_add_rules('tom.content.bottom.content', $l_rules)
            ->assign('configuration_url', $l_configuration_url)
            ->assign('ajax_url', isys_glob_add_to_query('ajax', 1))
            ->assign('layouts', explode('---', $l_layouts));
    }

    /**
     * Method for ajax-requests.
     *
     * @author  Leonard Fischer <lfischer@i-doit.org>
     */
    public function ajax_request()
    {
        global $g_dirs;

        // This will be used for the printable popup.
        if ($_GET['printview'] == '1') {
            $l_layouts = explode('---', file_get_contents(__DIR__ . '/view_qr_code_layouts.html'));

            $l_layout = str_replace([
                '%qrcode_url%',
                '%description%',
                '%logo_url%'
            ], [
                $g_dirs['images'] . 'icons/tree/qr_code.png',
                $this->language->get('LC__REPORT__VIEW__QR_CODES__LAYOUT_DESCRIPTION'),
                $g_dirs['images'] . 'favicon.png'
            ], $l_layouts[$_GET['layout']]);

            $this->template
                ->assign('ajax_url', isys_glob_url_remove('?' . $_SERVER['QUERY_STRING'], 'printview'))
                ->assign('qr_code_size', $_GET['size'])
                ->assign('qr_code_error_correction', $_GET['error_correction'])
                ->assign('columns', $_GET['cols'])
                ->assign('obj_ids', $_GET['objects'])
                ->assign('text_alignment', $_GET['text_alignment'])
                ->assign('layout', $l_layout)
                ->display(__DIR__ . '/view_qr_codes_popup.tpl');
            die();
        }

        $l_return = ['success' => true];

        try {
            if (isset($_POST['obj_ids'])) {
                $l_object_ids = isys_format_json::decode($_POST['obj_ids'], true);
            } else {
                $l_object_ids = [];
            }

            /** @var $l_qrcode_module isys_module_qrcode */
            $l_qrcode_module = isys_factory::get_instance('isys_module_qrcode')
                ->init(isys_module_request::get_instance());

            if (is_array($l_object_ids)) {
                foreach ($l_object_ids as $l_object_id) {
                    $l_qr_code_data = $l_qrcode_module->load_qr_code($l_object_id);

                    if (isys_tenantsettings::get('barcode.type', 'qr') === 'qr') {
                        $l_url = 'src/tools/php/qr/qr_img.php?d=' . $l_qr_code_data['url'];
                    } else {
                        $l_url = 'src/tools/php/barcode.php?height=65&barcode=' . $l_qr_code_data['sysid'];
                    }

                    $l_return['data'][] = [
                        'logo'        => $l_qr_code_data['logo'],
                        'success'     => true,
                        'url'         => isys_application::instance()->www_path . $l_url,
                        'description' => $l_qr_code_data['description']
                    ];
                }
            }
        } catch (Exception $e) {
            $l_return['success'] = false;
            $l_return['message'] = $e->getMessage();
        }

        header('Content-Type: application/json');

        echo isys_format_json::encode($l_return);

        die;
    }
}
