2010-12-23 12:26:25 +01:00
|
|
|
<?
|
|
|
|
include 'XMLSQL.php';
|
|
|
|
class ShortURL extends XMLSQL{
|
|
|
|
|
|
|
|
const DATABASE = "db/database.xml";
|
|
|
|
const STATE_ALREADY_EXIST = "Ce nom de raccourci existe déjà";
|
|
|
|
const STATE_FIELD_MISSING = "Merci de remplir les 2 champs. ";
|
|
|
|
const STATE_ERROR = "Erreur. ";
|
|
|
|
public $_debug = false;
|
|
|
|
|
|
|
|
public function __construct($path = ''){
|
|
|
|
parent::__construct($path.self::DATABASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shortThisUrl($longUrl, $shortName){
|
|
|
|
if($this->pkAlreadyExists($shortName, 'url')){
|
|
|
|
return self::STATE_ALREADY_EXIST;
|
|
|
|
}else{
|
2011-01-04 11:57:00 +01:00
|
|
|
return $this->insert(array('url'=>$longUrl,'hit'=>'0'), rawurlencode($shortName))->into('url')->query();
|
2010-12-23 12:26:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findThisUrl($shortName){
|
|
|
|
if($this->pkAlreadyExists(rawurlencode($shortName), 'url')){
|
2011-01-04 11:57:00 +01:00
|
|
|
$this->_incrementStatFor($shortName);
|
2010-12-23 12:26:25 +01:00
|
|
|
return $this->select(array('url'))->from('url')->where(rawurlencode($shortName))->query();
|
|
|
|
}else{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function extractEverything(){
|
|
|
|
return $this->select()->from('url')->query();
|
|
|
|
}
|
|
|
|
|
2011-01-04 11:57:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Considering the table with $shortname already exist
|
|
|
|
*/
|
|
|
|
private function _incrementStatFor($shortName){
|
|
|
|
$currentHit = $this->select(array('hit'))->from('url')->where(rawurlencode($shortName))->query();
|
|
|
|
$currentHit = $currentHit[0];
|
|
|
|
return $this->update('url')->set(array('hit'=>$currentHit+1))->where(rawurlencode($shortName))->query();
|
|
|
|
}
|
|
|
|
|
2010-12-23 12:26:25 +01:00
|
|
|
}
|