From 263d6c6756a2549100ae186042f157ff64944bfa Mon Sep 17 00:00:00 2001 From: nicosomb Date: Mon, 15 Apr 2013 09:58:34 +0200 Subject: [PATCH] factorisation code --- inc/config.php | 3 +- inc/functions.php | 104 ++++++++++++++++++++++++++++++++++++++++++++++ index.php | 64 ++-------------------------- view.php | 17 ++------ 4 files changed, 111 insertions(+), 77 deletions(-) diff --git a/inc/config.php b/inc/config.php index 6d8be9ee9..29a22507c 100644 --- a/inc/config.php +++ b/inc/config.php @@ -22,5 +22,4 @@ raintpl::$cache_dir = './cache/'; raintpl::$base_url = get_poche_url(); raintpl::configure('path_replace', false); raintpl::configure('debug', false); -$tpl = new raintpl(); -?> \ No newline at end of file +$tpl = new raintpl(); \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php index ffd400ef3..45828bf2b 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -120,4 +120,108 @@ function prepare_url($url) $parametres['content'] = $r->articleContent->innerHTML; 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; } \ No newline at end of file diff --git a/index.php b/index.php index ef18254a4..1cb32f58c 100755 --- a/index.php +++ b/index.php @@ -11,70 +11,12 @@ include dirname(__FILE__).'/inc/config.php'; $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']) : ''; $url = (isset ($_GET['url'])) ? $_GET['url'] : ''; -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()); -} - -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()); -} +action_to_do($action, $id); +$entries = display_view($view); $tpl->assign('title', 'poche, a read it later open source system'); $tpl->assign('view', $view); diff --git a/view.php b/view.php index 0ee8957b9..dfc26b9d5 100755 --- a/view.php +++ b/view.php @@ -10,22 +10,11 @@ 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=?"; - $params = array(intval($_GET['id'])); +if(!empty($id)) { - # view article query - try - { - $query = $db->getHandle()->prepare($sql); - $query->execute($params); - $entry = $query->fetchAll(); - } - catch (Exception $e) - { - die('query error : '.$e->getMessage()); - } + $entry = get_article($id); if ($entry != NULL) { $tpl->assign('id', $entry[0]['id']);