<?PHP

/**
 * Simpla CMS
 *
 * @copyright 	2009 Denis Pikusov
 * @link 		http://simp.la
 * @author 		Denis Pikusov
 *
 * Отображение статей на сайте
 * Этот класс использует шаблоны articles.tpl и article.tpl
 *
 */

require_once('View.php');

class UserView extends View
{
	function fetch()
	{
		if(empty($this->user) && !$this->request->post('mailing_email'))
		{
			header('Location: '.$this->config->root_url.'/user/login');
			exit();
		}

		if(empty($this->user) && !$this->request->post('mailing_email'))
        {
            header('Location: '.$this->config->root_url.'/user/login');
            exit();
        }
        if($this->request->method('post') && $this->request->post('mailing_email'))
        {
            if(filter_var(trim($this->request->post('mailing_email')), FILTER_VALIDATE_EMAIL))
            {
                $query = $this->db->placehold('select id from __mailing where email=?', $this->request->post('mailing_email'));
                $this->db->query($query);
                if($this->db->num_rows() < 1)
                {
                    $query = $this->db->placehold('insert into __mailing(`email`)VALUES(?)', $this->request->post('mailing_email'));
                    $this->db->query($query);
                }
            }
            else
            {
                $this->design->assign('error', 'email_syntax');
            }
        }
        elseif($this->request->method('get') && $this->request->get('unsubscribe') && $this->request->get('mail'))
        {
            if(filter_var($this->request->get('mail'), FILTER_VALIDATE_EMAIL)){
                $query = $this->db->placehold('select id from __mailing where email=?', $this->request->get('mail'));
                $this->db->query($query);
                if($this->db->num_rows() > 0){
                $hash = md5($this->settings->license.$this->request->get('mail'));
                    if($hash == $this->request->get('unsubscribe')){
                        $query = $this->db->placehold('delete from __mailing where email=?', $this->request->get('mail'));
                        $this->db->query($query);
                    }else{
                        $this->design->assign('error', '00001');
                    }
                }else{
                    $this->design->assign('error', '00001');
                }
            }else{
                $this->design->assign('error', 'email_syntax');
            }
        }
        elseif($this->request->method('post') && $this->request->post('name'))
		{
			$name 			= $this->request->post('name');
			$name2 			= $this->request->post('name2');
			$phone 			= $this->request->post('phone');
			$country 		= $this->request->post('country');
			$region 		= $this->request->post('region'); 	
			$city 			= $this->request->post('city');
			$indx 			= $this->request->post('indx');
			$adress 		= $this->request->post('adress');
			$email			= $this->request->post('email');
			$password		= $this->request->post('password');

			$this->design->assign('name', $name);
			$this->design->assign('name2', $name2);
			$this->design->assign('email', $email);
			$this->design->assign('phone', $phone);
			$this->design->assign('country', $country);
			$this->design->assign('region', $region);
			$this->design->assign('city', $city);
			$this->design->assign('adress', $adress);
			$this->design->assign('indx', $indx);

			$this->db->query('SELECT count(*) as count FROM __users WHERE email=? AND id!=?', $email, $this->user->id);
			$user_exists = $this->db->result('count');

			if($user_exists)
				$this->design->assign('error', 'user_exists');
			elseif(empty($name))
				$this->design->assign('error', 'empty_name');
			elseif(empty($email))
				$this->design->assign('error', 'empty_email');
			elseif($user_id = $this->users->update_user($this->user->id,array(
				'name'=>$name, 
				'name2'=>$name2, 
				'phone'=>$phone, 
				'country'=>$country, 
				'region'=>$region, 
				'city'=>$city, 
				'adress'=>$adress, 
				'indx'=>$indx, 
				'email'=>$email
				)))
			{
				$this->user = $this->users->get_user(intval($user_id));
				$this->design->assign('name', $this->user->name);
				$this->design->assign('name2', $this->user->name2);
				$this->design->assign('phone', $this->user->phone);
				$this->design->assign('country', $this->user->country);
				$this->design->assign('region', $this->user->region); 	
				$this->design->assign('city', $this->user->city);
				$this->design->assign('index', $this->user->index);
				$this->design->assign('adress', $this->user->adress);

				$this->design->assign('user', $this->user);
				$this->design->assign('email', $this->user->email);
			}
			else
				$this->design->assign('error', 'unknown error');

			if(!empty($password))
			{
				$this->users->update_user($this->user->id, array('password'=>$password));
			}

		}
		else
		{
			// Передаем в шаблон
			$this->design->assign('name', $this->user->name);
			$this->design->assign('name2', $this->user->name2);
			$this->design->assign('phone', $this->user->phone);
			$this->design->assign('country', $this->user->country);
			$this->design->assign('region', $this->user->region); 	
			$this->design->assign('city', $this->user->city);
			$this->design->assign('indx', $this->user->indx);
			$this->design->assign('adress', $this->user->adress);
			
			$this->design->assign('email', $this->user->email);
		}

		$orders = $this->orders->get_orders(array('user_id'=>$this->user->id));
		$this->design->assign('orders', $orders);

		if(!$this->request->post('mailing_email')){$this->design->assign('meta_title', $this->user->name);}else{$this->design->assign('meta_title', 'Подписка на рассылку');}
		$body = $this->design->fetch('user.tpl');

		return $body;
	}
}
