<?PHP

class CommentAdmin extends Buyers
{

	public function fetch()
	{
		$comment = new stdClass();
		if($this->request->method('post'))
		{
			$status = "updated";
			$images = array();
			$comment->name	= $this->request->post('name');
			$comment->email	= $this->request->post('email', 'email');
			$comment->date = date('Y-m-d', strtotime($this->request->post('date')));
			$comment->id		= $this->request->post('id', 'integer');
			$comment->approved	= $this->request->post('approved', 'boolean');
			$comment->rating	= $this->request->post('rating');
			$comment->text	= $this->request->post('text');
			$comment->advantages	= $this->request->post('advantages');
			$comment->disadvantages	= $this->request->post('disadvantages');
			$comment->answer	= $this->request->post('answer');

			if(!empty($comment->id))
				$this->comments->update_comment($comment->id, $comment);

			header('Location: '.$this->request->url(array('module'=>'CommentAdmin', 'id'=>$comment->id, 'message_success'=>$status,'return'=>$this->request->get('return'))));
		}
		else
		{
			if($comment->id = $this->request->get('id', 'integer'))
			{
				$comment = $this->comments->get_comment($comment->id);
				$comment->images = json_decode($comment->images) ?? array();
				$comment->product = new stdClass();
				if($comment->product_id > 0)
				{
					$comment->product = $this->products->get_product(intval($comment->product_id));
				}
			}
		}
		$this->design->assign('comment', $comment);
		return $this->design->fetch('comment.tpl');
	}
}
