You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB

  1. <?php
  2. class CustomCategoryRepository {
  3. function clearCache () {
  4. }
  5. function getCategory ($id, $options=[]) {
  6. global $db;
  7. $stmt = $db->prepare("select content from customCategory where id=:id");
  8. $stmt->bindValue(':id', $id, PDO::PARAM_STR);
  9. if ($stmt->execute()) {
  10. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  11. $result = $row['content'];
  12. $stmt->closeCursor();
  13. return $result;
  14. }
  15. }
  16. function recordAccess ($id) {
  17. global $db;
  18. if (!isset($_SESSION['customCategoryAccess'])) {
  19. $_SESSION['customCategoryAccess'] = [];
  20. }
  21. // update access per session only once a day
  22. if (array_key_exists($id, $_SESSION['customCategoryAccess']) && $_SESSION['customCategoryAccess'][$id] > time() - 86400) {
  23. return;
  24. }
  25. $_SESSION['customCategoryAccess'][$id] = time();
  26. $stmt = $db->prepare("insert into customCategoryAccess (id) values (:id)");
  27. $stmt->bindValue(':id', $id);
  28. $stmt->execute();
  29. }
  30. function saveCategory ($content) {
  31. global $db;
  32. $id = md5($content);
  33. switch ($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
  34. case 'mysql':
  35. $sqlAction = "insert ignore";
  36. break;
  37. case 'sqlite':
  38. default:
  39. $sqlAction = "insert or ignore";
  40. }
  41. $stmt = $db->prepare("{$sqlAction} into customCategory (id, content) values (:id, :content)");
  42. $stmt->bindValue(':id', $id, PDO::PARAM_STR);
  43. $stmt->bindValue(':content', $content, PDO::PARAM_STR);
  44. $result = $stmt->execute();
  45. return $id;
  46. }
  47. function list ($options=[]) {
  48. global $db;
  49. // $sqlCalcAge: the age of the access in days
  50. switch ($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
  51. case 'mysql':
  52. $sqlCalcAge = "datediff(now(), ts)";
  53. break;
  54. case 'sqlite':
  55. $sqlCalcAge = "julianday('now')-julianday(ts)";
  56. }
  57. // the popularity column counts every acess with declining value over time,
  58. // it halves every year.
  59. $stmt = $db->prepare("select customCategory.id, customCategory.created, customCategory.content, t.accessCount, t.popularity, t.lastAccess from customCategory left join (select id, count(id) accessCount, sum(1/(({$sqlCalcAge})/365.25+1)) popularity, max(ts) lastAccess from customCategoryAccess group by id) t on customCategory.id=t.id order by popularity desc, created desc limit 25");
  60. $stmt->execute();
  61. $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  62. $data = array_map(function ($d) {
  63. $d['popularity'] = (float)$d['popularity'];
  64. $d['accessCount'] = (int)$d['accessCount'];
  65. $content = yaml_parse($d['content']);
  66. if ($content && is_array($content) && array_key_exists('name', $content)) {
  67. $d['name'] = lang($content['name']);
  68. }
  69. else {
  70. $d['name'] = 'Custom ' . substr($d['id'], 0, 6);
  71. }
  72. unset($d['content']);
  73. return $d;
  74. }, $data);
  75. $stmt->closeCursor();
  76. return $data;
  77. }
  78. }
  79. $customCategoryRepository = new CustomCategoryRepository();