factorisation code

This commit is contained in:
nicosomb 2013-04-15 09:58:34 +02:00
parent 8046748b42
commit 263d6c6756
4 changed files with 111 additions and 77 deletions

View file

@ -22,5 +22,4 @@ raintpl::$cache_dir = './cache/';
raintpl::$base_url = get_poche_url(); raintpl::$base_url = get_poche_url();
raintpl::configure('path_replace', false); raintpl::configure('path_replace', false);
raintpl::configure('debug', false); raintpl::configure('debug', false);
$tpl = new raintpl(); $tpl = new raintpl();
?>

View file

@ -120,4 +120,108 @@ function prepare_url($url)
$parametres['content'] = $r->articleContent->innerHTML; $parametres['content'] = $r->articleContent->innerHTML;
return $parametres; return $parametres;
}
/**
* Appel d'une action (mark as fav, archive, delete)
*/
function action_to_do($action, $id)
{
global $db;
switch ($action)
{
case 'add':
if ($url == '')
continue;
$parametres_url = prepare_url($url);
$sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
$params_action = array($url, $parametres_url['title'], $parametres_url['content']);
break;
case 'delete':
$sql_action = "DELETE FROM entries WHERE id=?";
$params_action = array($id);
break;
default:
break;
}
try
{
# action query
if (isset($sql_action))
{
$query = $db->getHandle()->prepare($sql_action);
$query->execute($params_action);
}
}
catch (Exception $e)
{
die('action query error : '.$e->getMessage());
}
}
/**
* Détermine quels liens afficher : home, fav ou archives
*/
function display_view($view)
{
global $db;
switch ($view)
{
case 'archive':
$sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc";
$params = array(-1);
break;
case 'fav' :
$sql = "SELECT * FROM entries WHERE is_fav=? ORDER BY id desc";
$params = array(-1);
break;
default:
$sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc";
$params = array(0);
break;
}
# view query
try
{
$query = $db->getHandle()->prepare($sql);
$query->execute($params);
$entries = $query->fetchAll();
}
catch (Exception $e)
{
die('view query error : '.$e->getMessage());
}
return $entries;
}
/**
* Récupère un article en fonction d'un ID
*/
function get_article($id)
{
global $db;
$entry = NULL;
$sql = "SELECT * FROM entries WHERE id=?";
$params = array(intval($id));
# view article query
try
{
$query = $db->getHandle()->prepare($sql);
$query->execute($params);
$entry = $query->fetchAll();
}
catch (Exception $e)
{
die('query error : '.$e->getMessage());
}
return $entry;
} }

View file

@ -11,70 +11,12 @@
include dirname(__FILE__).'/inc/config.php'; include dirname(__FILE__).'/inc/config.php';
$action = (isset ($_GET['action'])) ? htmlspecialchars($_GET['action']) : ''; $action = (isset ($_GET['action'])) ? htmlspecialchars($_GET['action']) : '';
$view = (isset ($_GET['view'])) ? htmlspecialchars($_GET['view']) : ''; $view = (isset ($_GET['view'])) ? htmlspecialchars($_GET['view']) : 'index';
$id = (isset ($_GET['id'])) ? htmlspecialchars($_GET['id']) : ''; $id = (isset ($_GET['id'])) ? htmlspecialchars($_GET['id']) : '';
$url = (isset ($_GET['url'])) ? $_GET['url'] : ''; $url = (isset ($_GET['url'])) ? $_GET['url'] : '';
switch ($action) action_to_do($action, $id);
{ $entries = display_view($view);
case 'add':
if ($url == '')
continue;
$parametres_url = prepare_url($url);
$sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
$params_action = array($url, $parametres_url['title'], $parametres_url['content']);
break;
case 'delete':
$sql_action = "DELETE FROM entries WHERE id=?";
$params_action = array($id);
break;
default:
break;
}
try
{
# action query
if (isset($sql_action))
{
$query = $db->getHandle()->prepare($sql_action);
$query->execute($params_action);
}
}
catch (Exception $e)
{
die('action query error : '.$e->getMessage());
}
switch ($view)
{
case 'archive':
$sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc";
$params = array(-1);
break;
case 'fav' :
$sql = "SELECT * FROM entries WHERE is_fav=? ORDER BY id desc";
$params = array(-1);
break;
default:
$sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc";
$params = array(0);
$view = 'index';
break;
}
# view query
try
{
$query = $db->getHandle()->prepare($sql);
$query->execute($params);
$entries = $query->fetchAll();
}
catch (Exception $e)
{
die('view query error : '.$e->getMessage());
}
$tpl->assign('title', 'poche, a read it later open source system'); $tpl->assign('title', 'poche, a read it later open source system');
$tpl->assign('view', $view); $tpl->assign('view', $view);

View file

@ -10,22 +10,11 @@
include dirname(__FILE__).'/inc/config.php'; include dirname(__FILE__).'/inc/config.php';
if(isset($_GET['id']) && $_GET['id'] != '') { $id = (isset ($_GET['id'])) ? htmlspecialchars($_GET['id']) : '';
$sql = "SELECT * FROM entries WHERE id=?"; if(!empty($id)) {
$params = array(intval($_GET['id']));
# view article query $entry = get_article($id);
try
{
$query = $db->getHandle()->prepare($sql);
$query->execute($params);
$entry = $query->fetchAll();
}
catch (Exception $e)
{
die('query error : '.$e->getMessage());
}
if ($entry != NULL) { if ($entry != NULL) {
$tpl->assign('id', $entry[0]['id']); $tpl->assign('id', $entry[0]['id']);