diff options
author | nicosomb <nicolas@loeuillet.org> | 2013-04-19 15:46:04 +0200 |
---|---|---|
committer | nicosomb <nicolas@loeuillet.org> | 2013-04-19 15:46:04 +0200 |
commit | 14890de35a19b44df6537e601240fe38ff6a9ed9 (patch) | |
tree | 7802fdcce30bab91c7f97f73fd0456cc0c5d8bd3 /inc/functions.php | |
parent | ff4d8c8c1efca0759330906419cb5f36de86d156 (diff) | |
download | wallabag-14890de35a19b44df6537e601240fe38ff6a9ed9.tar.gz wallabag-14890de35a19b44df6537e601240fe38ff6a9ed9.tar.zst wallabag-14890de35a19b44df6537e601240fe38ff6a9ed9.zip |
création d'une classe store() pour gérer tout type de stockage : sqlite, file, mysql, etc.
Diffstat (limited to 'inc/functions.php')
-rw-r--r-- | inc/functions.php | 144 |
1 files changed, 18 insertions, 126 deletions
diff --git a/inc/functions.php b/inc/functions.php index ef1fc0e2..df7e9b17 100644 --- a/inc/functions.php +++ b/inc/functions.php | |||
@@ -228,20 +228,20 @@ function remove_directory($directory) | |||
228 | 228 | ||
229 | function display_view($view, $id = 0, $full_head = 'yes') | 229 | function display_view($view, $id = 0, $full_head = 'yes') |
230 | { | 230 | { |
231 | global $tpl; | 231 | global $tpl, $store; |
232 | 232 | ||
233 | switch ($view) | 233 | switch ($view) |
234 | { | 234 | { |
235 | case 'view': | 235 | case 'view': |
236 | $entry = get_article($id); | 236 | $entry = $store->retrieveOneById($id); |
237 | 237 | ||
238 | if ($entry != NULL) { | 238 | if ($entry != NULL) { |
239 | $tpl->assign('id', $entry[0]['id']); | 239 | $tpl->assign('id', $entry['id']); |
240 | $tpl->assign('url', $entry[0]['url']); | 240 | $tpl->assign('url', $entry['url']); |
241 | $tpl->assign('title', $entry[0]['title']); | 241 | $tpl->assign('title', $entry['title']); |
242 | $tpl->assign('content', $entry[0]['content']); | 242 | $tpl->assign('content', $entry['content']); |
243 | $tpl->assign('is_fav', $entry[0]['is_fav']); | 243 | $tpl->assign('is_fav', $entry['is_fav']); |
244 | $tpl->assign('is_read', $entry[0]['is_read']); | 244 | $tpl->assign('is_read', $entry['is_read']); |
245 | $tpl->assign('load_all_js', 0); | 245 | $tpl->assign('load_all_js', 0); |
246 | $tpl->draw('view'); | 246 | $tpl->draw('view'); |
247 | } | 247 | } |
@@ -252,7 +252,7 @@ function display_view($view, $id = 0, $full_head = 'yes') | |||
252 | logm('view link #' . $id); | 252 | logm('view link #' . $id); |
253 | break; | 253 | break; |
254 | default: # home view | 254 | default: # home view |
255 | $entries = get_entries($view); | 255 | $entries = $store->getEntriesByView($view); |
256 | 256 | ||
257 | $tpl->assign('entries', $entries); | 257 | $tpl->assign('entries', $entries); |
258 | 258 | ||
@@ -277,7 +277,7 @@ function display_view($view, $id = 0, $full_head = 'yes') | |||
277 | */ | 277 | */ |
278 | function action_to_do($action, $url, $id = 0) | 278 | function action_to_do($action, $url, $id = 0) |
279 | { | 279 | { |
280 | global $db; | 280 | global $store; |
281 | 281 | ||
282 | switch ($action) | 282 | switch ($action) |
283 | { | 283 | { |
@@ -286,139 +286,31 @@ function action_to_do($action, $url, $id = 0) | |||
286 | continue; | 286 | continue; |
287 | 287 | ||
288 | if($parametres_url = prepare_url($url)) { | 288 | if($parametres_url = prepare_url($url)) { |
289 | $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; | 289 | $store->add($url, $parametres_url['title'], $parametres_url['content']); |
290 | $params_action = array($url, $parametres_url['title'], $parametres_url['content']); | 290 | $last_id = $store->getLastId(); |
291 | if (DOWNLOAD_PICTURES) { | ||
292 | $content = filtre_picture($parametres_url['content'], $url, $last_id); | ||
293 | } | ||
291 | } | 294 | } |
292 | 295 | ||
293 | logm('add link ' . $url); | 296 | logm('add link ' . $url); |
294 | break; | 297 | break; |
295 | case 'delete': | 298 | case 'delete': |
296 | remove_directory(ABS_PATH . $id); | 299 | remove_directory(ABS_PATH . $id); |
297 | $sql_action = "DELETE FROM entries WHERE id=?"; | 300 | $store->deleteById($id); |
298 | $params_action = array($id); | ||
299 | logm('delete link #' . $id); | 301 | logm('delete link #' . $id); |
300 | break; | 302 | break; |
301 | case 'toggle_fav' : | 303 | case 'toggle_fav' : |
302 | $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; | 304 | $store->favoriteById($id); |
303 | $params_action = array($id); | ||
304 | logm('mark as favorite link #' . $id); | 305 | logm('mark as favorite link #' . $id); |
305 | break; | 306 | break; |
306 | case 'toggle_archive' : | 307 | case 'toggle_archive' : |
307 | $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; | 308 | $store->archiveById($id); |
308 | $params_action = array($id); | ||
309 | logm('archive link #' . $id); | 309 | logm('archive link #' . $id); |
310 | break; | 310 | break; |
311 | default: | 311 | default: |
312 | break; | 312 | break; |
313 | } | 313 | } |
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 | */ | ||
344 | function 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 | */ | ||
401 | function 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 | } | 314 | } |
423 | 315 | ||
424 | function logm($message) | 316 | function logm($message) |