aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/functions.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/functions.php')
-rw-r--r--inc/functions.php180
1 files changed, 53 insertions, 127 deletions
diff --git a/inc/functions.php b/inc/functions.php
index ef1fc0e2..205f3968 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -1,4 +1,12 @@
1<?php 1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
2 10
3/** 11/**
4 * Permet de générer l'URL de poche pour le bookmarklet 12 * Permet de générer l'URL de poche pour le bookmarklet
@@ -117,6 +125,7 @@ function prepare_url($url)
117 } 125 }
118 } 126 }
119 127
128 $msg->add('e', 'error during url preparation');
120 logm('error during url preparation'); 129 logm('error during url preparation');
121 return FALSE; 130 return FALSE;
122} 131}
@@ -228,20 +237,35 @@ function remove_directory($directory)
228 237
229function display_view($view, $id = 0, $full_head = 'yes') 238function display_view($view, $id = 0, $full_head = 'yes')
230{ 239{
231 global $tpl; 240 global $tpl, $store, $msg;
232 241
233 switch ($view) 242 switch ($view)
234 { 243 {
244 case 'export':
245 $entries = $store->retrieveAll();
246 $tpl->assign('export', myTool::renderJson($entries));
247 $tpl->draw('export');
248 logm('export view');
249 break;
250 case 'config':
251 $tpl->assign('load_all_js', 0);
252 $tpl->draw('head');
253 $tpl->draw('home');
254 $tpl->draw('config');
255 $tpl->draw('js');
256 $tpl->draw('footer');
257 logm('config view');
258 break;
235 case 'view': 259 case 'view':
236 $entry = get_article($id); 260 $entry = $store->retrieveOneById($id);
237 261
238 if ($entry != NULL) { 262 if ($entry != NULL) {
239 $tpl->assign('id', $entry[0]['id']); 263 $tpl->assign('id', $entry['id']);
240 $tpl->assign('url', $entry[0]['url']); 264 $tpl->assign('url', $entry['url']);
241 $tpl->assign('title', $entry[0]['title']); 265 $tpl->assign('title', $entry['title']);
242 $tpl->assign('content', $entry[0]['content']); 266 $tpl->assign('content', $entry['content']);
243 $tpl->assign('is_fav', $entry[0]['is_fav']); 267 $tpl->assign('is_fav', $entry['is_fav']);
244 $tpl->assign('is_read', $entry[0]['is_read']); 268 $tpl->assign('is_read', $entry['is_read']);
245 $tpl->assign('load_all_js', 0); 269 $tpl->assign('load_all_js', 0);
246 $tpl->draw('view'); 270 $tpl->draw('view');
247 } 271 }
@@ -252,7 +276,7 @@ function display_view($view, $id = 0, $full_head = 'yes')
252 logm('view link #' . $id); 276 logm('view link #' . $id);
253 break; 277 break;
254 default: # home view 278 default: # home view
255 $entries = get_entries($view); 279 $entries = $store->getEntriesByView($view);
256 280
257 $tpl->assign('entries', $entries); 281 $tpl->assign('entries', $entries);
258 282
@@ -277,7 +301,7 @@ function display_view($view, $id = 0, $full_head = 'yes')
277 */ 301 */
278function action_to_do($action, $url, $id = 0) 302function action_to_do($action, $url, $id = 0)
279{ 303{
280 global $db; 304 global $store, $msg;
281 305
282 switch ($action) 306 switch ($action)
283 { 307 {
@@ -285,140 +309,42 @@ function action_to_do($action, $url, $id = 0)
285 if ($url == '') 309 if ($url == '')
286 continue; 310 continue;
287 311
288 if($parametres_url = prepare_url($url)) { 312 if (MyTool::isUrl($url)) {
289 $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; 313 if($parametres_url = prepare_url($url)) {
290 $params_action = array($url, $parametres_url['title'], $parametres_url['content']); 314 $store->add($url, $parametres_url['title'], $parametres_url['content']);
315 $last_id = $store->getLastId();
316 if (DOWNLOAD_PICTURES) {
317 $content = filtre_picture($parametres_url['content'], $url, $last_id);
318 }
319 $msg->add('s', 'the link has been added successfully');
320 }
321 }
322 else {
323 $msg->add('e', 'the link has been added successfully');
324 logm($url . ' is not a valid url');
291 } 325 }
292 326
293 logm('add link ' . $url); 327 logm('add link ' . $url);
294 break; 328 break;
295 case 'delete': 329 case 'delete':
296 remove_directory(ABS_PATH . $id); 330 remove_directory(ABS_PATH . $id);
297 $sql_action = "DELETE FROM entries WHERE id=?"; 331 $store->deleteById($id);
298 $params_action = array($id); 332 $msg->add('s', 'the link has been deleted successfully');
299 logm('delete link #' . $id); 333 logm('delete link #' . $id);
300 break; 334 break;
301 case 'toggle_fav' : 335 case 'toggle_fav' :
302 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; 336 $store->favoriteById($id);
303 $params_action = array($id); 337 $msg->add('s', 'the favorite toggle has been done successfully');
304 logm('mark as favorite link #' . $id); 338 logm('mark as favorite link #' . $id);
305 break; 339 break;
306 case 'toggle_archive' : 340 case 'toggle_archive' :
307 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; 341 $store->archiveById($id);
308 $params_action = array($id); 342 $msg->add('s', 'the archive toggle has been done successfully');
309 logm('archive link #' . $id); 343 logm('archive link #' . $id);
310 break; 344 break;
311 default: 345 default:
312 break; 346 break;
313 } 347 }
314
315 try
316 {
317 # action query
318 if (isset($sql_action))
319 {
320 $query = $db->getHandle()->prepare($sql_action);
321 $query->execute($params_action);
322 # if we add a link, we have to download pictures
323 if ($action == 'add') {
324 $last_id = $db->getHandle()->lastInsertId();
325 if (DOWNLOAD_PICTURES) {
326 $content = filtre_picture($parametres_url['content'], $url, $last_id);
327 $sql_update = "UPDATE entries SET content=? WHERE id=?";
328 $params_update = array($content, $last_id);
329 $query_update = $db->getHandle()->prepare($sql_update);
330 $query_update->execute($params_update);
331 }
332 }
333 }
334 }
335 catch (Exception $e)
336 {
337 logm('action query error : '.$e->getMessage());
338 }
339}
340
341/**
342 * Détermine quels liens afficher : home, fav ou archives
343 */
344function get_entries($view)
345{
346 global $db;
347
348 switch ($_SESSION['sort'])
349 {
350 case 'ia':
351 $order = 'ORDER BY id';
352 break;
353 case 'id':
354 $order = 'ORDER BY id DESC';
355 break;
356 case 'ta':
357 $order = 'ORDER BY lower(title)';
358 break;
359 case 'td':
360 $order = 'ORDER BY lower(title) DESC';
361 break;
362 default:
363 $order = 'ORDER BY id';
364 break;
365 }
366
367 switch ($view)
368 {
369 case 'archive':
370 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
371 $params = array(-1);
372 break;
373 case 'fav' :
374 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
375 $params = array(-1);
376 break;
377 default:
378 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
379 $params = array(0);
380 break;
381 }
382
383 # view query
384 try
385 {
386 $query = $db->getHandle()->prepare($sql);
387 $query->execute($params);
388 $entries = $query->fetchAll();
389 }
390 catch (Exception $e)
391 {
392 logm('view query error : '.$e->getMessage());
393 }
394
395 return $entries;
396}
397
398/**
399 * Récupère un article en fonction d'un ID
400 */
401function get_article($id)
402{
403 global $db;
404
405 $entry = NULL;
406 $sql = "SELECT * FROM entries WHERE id=?";
407 $params = array(intval($id));
408
409 # view article query
410 try
411 {
412 $query = $db->getHandle()->prepare($sql);
413 $query->execute($params);
414 $entry = $query->fetchAll();
415 }
416 catch (Exception $e)
417 {
418 logm('get article query error : '.$e->getMessage());
419 }
420
421 return $entry;
422} 348}
423 349
424function logm($message) 350function logm($message)