<?php

class ControllerCommonUser extends Controller
{

    public function index()
    {

        if($this->user->getEmail() == Type_DefaultValues::Tp_UserFuturaApp){
            return false;
        }

        $this->load->helper('ftr_geral');

        $language = $this->language->load('common/user');

        $this->data = array_merge($this->data, $language);

        $this->data['email'] = $this->user->getEmail();
        $this->data['checkinfo'] = $this->user->getCheckInfo();
        $this->data['usercheck'] = $this->load->typeForTwig('Type_UserCheck');


        $this->response->setOutput($this->render('common/user'));
    }


    public function save()
    {

        $this->language->load('common/user');

        $json = array();

        if(isset($this->request->post['confirmed'])){
            $confirmed = $this->request->post['confirmed'];
        }else{
            $json['warning'] = $this->language->get('error_confirmed');
        }

        $this->load->model('user/user');
        if($confirmed == Type_UserCheck::Tp_Confirmed){

            if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email']) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL) || !preg_match('/^[a-zA-Z0-9\._-]+@/', $this->request->post['email'])) {
                $json['email'] = $this->language->get('error_email');
            }

            if(!$json){
                $this->model_user_user->editCheckInfo($this->user->getId(), Type_UserCheck::Tp_Confirmed, $this->request->post['email']);
                $json['success'] = true ;
            }

        }else{
            $this->model_user_user->editCheckInfo($this->user->getId(), Type_UserCheck::Tp_NotDisplay, $this->request->post['email']);
            $json['reload'] = true ;
        }

        $this->response->setOutputJson($json);
    }

}
