functions.inc.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // Label functions
  3. function pa($array) {
  4. echo '<pre>';
  5. print_r($array);
  6. echo '</pre>';
  7. }
  8. function getLabels() {
  9. global $db;
  10. $return = array();
  11. $labels = $db->selectQuery("SELECT * FROM `labels`;");
  12. foreach($labels as $label) {
  13. $return[] = new Label($label->id, $label->name);
  14. }
  15. return $return;
  16. }
  17. function getLabelById($labelId) {
  18. global $db;
  19. $return = array();
  20. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `id` = " . $labelId . ";");
  21. foreach($labels as $label) {
  22. $return[] = new Label($label->id, $label->name);
  23. }
  24. return $return;
  25. }
  26. function getLabelByPath($path) {
  27. global $db;
  28. $return = array();
  29. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `path` = '" . $path . "';");
  30. foreach($labels as $label) {
  31. $return[] = new Label($label->id, $label->name);
  32. }
  33. return $return;
  34. }
  35. function getDocumentsByLabelId($labelId) {
  36. global $db;
  37. $return = array();
  38. $documents = $db->selectQuery("SELECT * FROM `documents` WHERE `label_id` = " . $labelId . ";");
  39. foreach($documents as $document) {
  40. $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
  41. }
  42. return $return;
  43. }
  44. function getDocumentsByPath($path) {
  45. global $db;
  46. $return = array();
  47. $documents = $db->selectQuery("SELECT documents.* FROM `documents` JOIN `labels` ON labels.id = documents.label_id WHERE labels.path = '" . $path . "';");
  48. foreach($documents as $document) {
  49. $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
  50. }
  51. return $return;
  52. }
  53. function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange) {
  54. global $db;
  55. $query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ");";
  56. $db->insertQuery($query);
  57. }
  58. function searchNewFiles($scanDir) {
  59. global $db;
  60. global $CONFIG;
  61. $oldDocuments = getDocumentsByPath($scanDir);
  62. $files = scandir($CONFIG['documentPath'] . $scanDir);
  63. foreach($files as $file) {
  64. $existed = false;
  65. if($file === '.' || $file === '..') {
  66. continue;
  67. }
  68. foreach($oldDocuments as $oldDocument) {
  69. if($oldDocument->getFileName() === $file) { // TODO: Check update-date, maybe removed files
  70. $existed = true;
  71. break;
  72. }
  73. }
  74. if($existed) {
  75. continue;
  76. }
  77. addDocument('file', $file, '/', getLabelByPath($scanDir)[0]->getId(), '', 'NOW()', 'NOW()'); // TODO: get dates by filesystem
  78. }
  79. }
  80. ?>