<?php

require_once ("secure_area.php");

class Config extends Secure_area {

    function __construct() {
        parent::__construct('config');
    }

    function index() {
        $this->load->view("config");
    }

    function save() {
        $filename = getcwd() . "/images/company_logo.php";

        if (file_exists($filename)) {

            if (!empty($_FILES['image_upload_box']['tmp_name'])) {
                file_put_contents($filename, serialize($_FILES));


                if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png" || $_FILES["image_upload_box"]["type"] == "image/png") && ($_FILES["image_upload_box"]["size"] < 4000000)) {
                    // some settings
                    $max_upload_width = 88;
                    $max_upload_height = 100;

                    // if uploaded image was JPG/JPEG
                    if ($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg") {
                        $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
                    }
                    // if uploaded image was GIF
                    if ($_FILES["image_upload_box"]["type"] == "image/gif") {
                        $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
                    }
                    // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
                    // if uploaded image was BMP
                    if ($_FILES["image_upload_box"]["type"] == "image/bmp") {
                        $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
                    }
                    // if uploaded image was PNG
                    if ($_FILES["image_upload_box"]["type"] == "image/x-png" || $_FILES["image_upload_box"]["type"] == "image/png") {
                        $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
                    }



                    $remote_file = getcwd() . "/images/" . $_FILES["image_upload_box"]["name"];
                    imagejpeg($image_source, $remote_file, 100);
                    chmod($remote_file, 0644);



                    // get width and height of original image
                    list($image_width, $image_height) = getimagesize($remote_file);

                    if ($image_width > $max_upload_width || $image_height > $max_upload_height) {
                        $proportions = $image_width / $image_height;

                        if ($image_width > $image_height) {
                            $new_width = $max_upload_width;
                            $new_height = round($max_upload_width / $proportions);
                        } else {
                            $new_height = $max_upload_height;
                            $new_width = round($max_upload_height * $proportions);
                        }


                        $new_image = imagecreatetruecolor($new_width, $new_height);
                        $image_source = imagecreatefromjpeg($remote_file);

                        imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
                        imagejpeg($new_image, $remote_file, 100);

                        imagedestroy($new_image);
                    }

                    imagedestroy($image_source);



                    //file_put_contents($filename, serialize($_FILES));

                    $fhandle = fopen($filename, "w");
                    fwrite($fhandle, $_FILES["image_upload_box"]["name"]);
                    fclose($fhandle);
                }
            }
        }


        $batch_save_data = array(
            'company' => $this->input->post('company'),
            'address' => $this->input->post('address'),
            'phone' => $this->input->post('phone'),
            'vat_reg_no' => $this->input->post('vat_reg_no'),
            'email' => $this->input->post('email'),
            'fax' => $this->input->post('fax'),
            'website' => $this->input->post('website'),
            'default_tax_1_rate' => $this->input->post('default_tax_1_rate'),
            'default_tax_1_name' => $this->input->post('default_tax_1_name'),
            'default_tax_2_rate' => $this->input->post('default_tax_2_rate'),
            'default_tax_2_name' => $this->input->post('default_tax_2_name'),
            'currency_symbol' => $this->input->post('currency_symbol'),
            'currency_side' => $this->input->post('currency_side'), /*             * GARRISON ADDED 4/20/2013* */
            'return_policy' => $this->input->post('return_policy'),
            'language' => $this->input->post('language'),
            'timezone' => $this->input->post('timezone'),
            'print_after_sale' => $this->input->post('print_after_sale'),
            'amount_per_point' => $this->input->post('amount_per_point'),
            'point_per_sell' => $this->input->post('point_per_sell'),
            'minimum_use_points' => $this->input->post('minimum_use_points'),
            'custom1_name' => $this->input->post('custom1_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom2_name' => $this->input->post('custom2_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom3_name' => $this->input->post('custom3_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom4_name' => $this->input->post('custom4_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom5_name' => $this->input->post('custom5_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom6_name' => $this->input->post('custom6_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom7_name' => $this->input->post('custom7_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom8_name' => $this->input->post('custom8_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom9_name' => $this->input->post('custom9_name'), /*             * GARRISON ADDED 4/20/2013* */
            'custom10_name' => $this->input->post('custom10_name')/*                 * GARRISON ADDED 4/20/2013* */
        );

        if ($this->Appconfig->batch_save($batch_save_data)) {
            echo json_encode(array('success' => true, 'message' => $this->lang->line('config_saved_successfully')));
        }
    }

}

?>