<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of userinfo_admin
 *
 * @author Igor Ratsun
 */
class userinfo_admin extends MX_Controller {

    public function __construct() {
        $this->load->language('userinfo/base');
    }

    public function init() {

        $this->load->model('userinfo/userinfo_model');
        $user_name = $this->userinfo_model->get_user_name();
        
        $content = array(
            'user_name' => !empty($user_name) ? $user_name : $this->rt_user->get_user_data('login'),
            'link' => '/' . $this->config->item('admin_page') . '/mconfig/index/userinfo'
        );

        return $this->parser->parse('userinfo/init', array_merge($content, $this->lang->language), TRUE);
    }

    public function change_pass() {
        // Ajax
        if (!empty($_POST)) {
            $errors = array();

            // Check new pass
            $newpass = $this->input->post('newpass');
            if (empty($newpass)) {
                $errors[] = 'newpass';
            }

            // Check new newpassconf
            if ($this->input->post('newpass') !== $this->input->post('newpassconf')) {
                $errors[] = 'newpassconf';
            }

            // check old pass
            $id = $this->rt_user->get_user_id($this->rt_user->get_user_data(), $this->input->post('oldpass'));
            if (empty($id)) {
                $errors[] = 'oldpass';
            }

            if (empty($errors)) {
                $this->rt_user->change_pass($id, $newpass);
                $RS = array('success');
            } else {
                $RS = array(
                    'errors' => $errors
                );
            }

            die(json_encode($RS));
        }

        $this->load->language('userinfo/pass');
        $this->ciext->load_bootstrap();
        $this->js_css->parse_js('userinfo/js/main.js');
        $this->load->helper('form');
        $content = array(
            'form_open' => form_open(base_url() . $this->config->item('admin_page') . '/mconfig/index/userinfo/change_pass', 'id="change_form" class="form-horizontal"'),
            'form_close' => form_close()
        );

        return array(
            'content' => $this->parser->parse('userinfo/change_pass', array_merge($content, $this->lang->language), TRUE),
            'title' => $this->lang->line('title')
        );
    }

    public function change_info() {
        // Access check
        $user_id = $this->uri->get_next_segment('user');
        if (!empty($user_id) && !$this->rt_user->isAllowed('acl', 'change_user_pass')) {
            $this->admin_lib->show_denied();
        }
        
        // Unset left menu
        $this->admin_lib->content['user_left_menu'] = '';

        $this->load->model('userinfo/userinfo_model');

        // Ajax
        if (!empty($_POST)) {

            $errors = array();

            if (empty($errors)) {
                $this->userinfo_model->save($user_id);
                $RS = array('success');
            } else {
                $RS = array(
                    'errors' => $errors
                );
            }

            die(json_encode($RS));
        }

        $this->load->language('userinfo/info');
        $this->load->config('userinfo/config');
        $this->ciext->load_bootstrap();
        //$this->js_css->add_css(base_url() . $this->config->item('assets_folder') . '/' . $this->config->item('components_folder') . '/ciext/css/main.css');
        $this->js_css->parse_css('userinfo/css/account.css');
        //$this->js_css->remove_css(base_url() . $this->config->item('assets_folder') . '/' . $this->config->item('components_folder') . '/ciext/css/reset.css');
        $this->ciext->load_jquery_ui();

        $this->js_css->add_js('/' . $this->config->item('assets_folder') . '/plugins/uploadify3.2/jquery.uploadify.min.js');
        $this->js_css->add_css('/' . $this->config->item('assets_folder') . '/plugins/uploadify3.2/uploadify.css');

        $this->js_css->parse_js('userinfo/js/main.js');
        
        // Set datapicker language
        $this->js_css->add_js('/' . $this->config->item('assets_folder') . '/grocery_crud/js/jquery_plugins/ui/i18n/datepicker/jquery.ui.datepicker-' . str_replace('ua', 'uk', $this->uri->current_language) . '.js');
        
        
        
        $this->load->helper('form');
        
        $this->lang->language['ln_photo'] = $this->parser->parse_string($this->lang->language['ln_photo'], array(
            'width' => $this->config->item('photo_width'),
            'height' => $this->config->item('photo_height')
        ), TRUE);
        
        $content = array_merge(array(
            'form_open' => form_open(base_url() . $this->config->item('admin_page') . '/mconfig/index/userinfo/change_info' . (!empty($user_id) ? '/user/' . $user_id : ''), 'id="change_form" class="form-horizontal"'),
            'photo_path' => $this->config->item('photo_path'),
            'photohash' => substr(md5((!empty($user_id) ? $user_id : $this->rt_user->id_user)) . $this->config->item('encryption_key'), 0, 8),
            'form_close' => form_close()
                ), $this->lang->language);

        return array(
            'content' => $this->parser->parse('userinfo/change_info', array_merge($content, $this->userinfo_model->get_data($user_id)), TRUE),
            'title' => $this->lang->line('title')
        );
    }

    public function uploadify() {
        $this->load->config('userinfo/config');
        $targetFolder = $this->config->item('photo_path'); // Relative to the root

        $result = array(
            'success' => 'false',
            'msg' => ''
        );

        if (!empty($_FILES)) {
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;


            // Validate the file type
            $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp'); // File extensions
            $fileParts = pathinfo($_FILES['Filedata']['name']);

            $targetFile = rtrim($targetPath, '/') . '/' . $_POST['photohash'] . '.' . $fileParts['extension'];

            if (in_array($fileParts['extension'], $fileTypes)) {
                move_uploaded_file($tempFile, $targetFile);

                $config['image_library'] = 'gd2';
                $config['source_image'] = $targetFile;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = $this->config->item('photo_width');
                $config['height'] = $this->config->item('photo_height');
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();

                $result['msg'] = 1;
                $result['file_name'] = basename($targetFile);
            } else {
                $result['msg'] = 'Invalid file type.';
            }
        }

        die(json_encode($result));
    }

}