<?php

class collection{
	
	function __construct(){
		if($_SERVER['REQUEST_METHOD']=='GET'){
			$this->view();
		}else{
			$this->add();
		}
	}

	private function add(){
		if(strlen($_POST['url'])<10){
			$this->warning($GLOBALS['lang']['war5']); return false;
		}
		global $connect;
		$url=preg_replace('/[^a-zA-Z0-9\-._~:\/?#\[\]@!$&\'()*+,;=%]/','',$_POST['url']);
		$url=base64_encode($url);

		$MySQL=mysqli_query($connect,'INSERT INTO urls(link) VALUES("'.$url.'")');

		$text=$GLOBALS['lang']['tri3'];
		$location='collection';
		include('./inc/success.php');
	}

	private function warning($text){
	   $location='collection';
	   include('./inc/warning.php');
    }

	private function view(){
		/*global $connect;
		$MySQL=mysqli_query($connect,'SELECT login FROM config');
		$row=mysqli_fetch_assoc($MySQL);
		include('./inc/forms/setting.php');*/

		global $connect;
		$MySQL=mysqli_query($connect,'SELECT * FROM urls ORDER BY id DESC');

		$th='';

		while($row=mysqli_fetch_assoc($MySQL)){
			$th.='
								<tr id="id'.$row['id'].'">
									<td>'.$row['id'].'</td>
									<td>'.$this->lessText(base64_decode($row['link'])).'</td>
									<td id="actions'.$row['id'].'">
										<a onclick="check('.$row['id'].')" class="btn btn-primary btn-xs">
											<span class="glyphicon glyphicon-edit"></span> '.$GLOBALS['lang']['tri13'].'
										</a> 
										<a onclick="deltr('.$row['id'].');" class="btn btn-danger btn-xs">
											<span class="glyphicon glyphicon-remove"></span> '.$GLOBALS['lang']['tri6'].'
										</a>
									</td>
								</tr>';
		}

		if($th==''){
			$th='<tr><td colspan="18"><div class="bs-component"><div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button>'.$GLOBALS['lang']['mes5'].'</div></td></tr>';
		}

		include('./inc/forms/collection.php');
	}

	private function lessText($url){
		if(strlen($url)<120){
			return $url;
		}else{
			return substr($url,0,117).'...';
		}
	}
}

?>