label.inc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. class Label {
  3. private $id = NULL;
  4. private $name = '';
  5. private $path = '';
  6. public function __construct($id, $name, $path) {
  7. $this->id = $id;
  8. $this->name = $name;
  9. $this->path = $path;
  10. }
  11. public function getID() {
  12. return $this->id;
  13. }
  14. public function getName() {
  15. return $this->name;
  16. }
  17. public function getPath() {
  18. return $this->path;
  19. }
  20. /**
  21. * Get all Labels
  22. *
  23. *
  24. * @return Array(Label) Array with all Labels
  25. *
  26. */
  27. public static function getAllLabels() {
  28. global $db;
  29. $return = array();
  30. $labels = $db->selectQuery("SELECT * FROM `labels`;");
  31. if(!$labels) {
  32. return false;
  33. }
  34. foreach($labels as $label) {
  35. $return[] = new Label($label->id, $label->name, $label->path);
  36. }
  37. return $return;
  38. }
  39. /**
  40. * Get a Label by ID
  41. *
  42. *
  43. * @param int $labelID Label ID
  44. *
  45. * @return Label Selected Label object
  46. *
  47. */
  48. public static function getLabelByID($labelID) {
  49. global $db;
  50. $return = array();
  51. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `id` = " . $labelID . ";");
  52. foreach($labels as $label) {
  53. $return[] = new Label($label->id, $label->name, $label->path);
  54. }
  55. if(sizeof($return) > 1) {
  56. // Error handling!
  57. }
  58. return $return[0];
  59. }
  60. /**
  61. * Get a Label by FS-path
  62. *
  63. *
  64. * @param string $path path, relative to ~/documents-folder
  65. *
  66. * @return Label Selected Label
  67. *
  68. */
  69. public static function getLabelByPath($path) {
  70. global $db;
  71. $return = array();
  72. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `path` = '" . $path . "';");
  73. foreach($labels as $label) {
  74. $return[] = new Label($label->id, $label->name, $label->path);
  75. }
  76. if(sizeof($return) > 1) {
  77. // Error handling!
  78. }
  79. return $return[0];
  80. }
  81. /**
  82. * Get a Label by Name
  83. *
  84. *
  85. * @param string $labelName Name of wanted Label
  86. *
  87. * @return Label Selected Label
  88. *
  89. */
  90. public static function getLabelByName($labelName) {
  91. global $db;
  92. $return = array();
  93. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `name` = '" . $labelName . "';");
  94. foreach($labels as $label) {
  95. $return[] = new Label($label->id, $label->name, $label->path);
  96. }
  97. if(sizeof($return) > 1) {
  98. // Error handling!
  99. }
  100. return $return[0];
  101. }
  102. /**
  103. * Add new Label to DB
  104. *
  105. * @param string $name Name of Label
  106. * @param string $path FS-Path of Label
  107. *
  108. * @return void
  109. *
  110. */
  111. public static function addLabel($name, $path) {
  112. global $db;
  113. global $CONFIG;
  114. $db->insertQuery("INSERT INTO `labels`(`name`, `path`) VALUES ('" . $name . "', '" . $path . "')");
  115. if(!file_exists($CONFIG['documentPath'] . $path) || !is_dir($CONFIG['documentPath'] . $path)) {
  116. mkdir($CONFIG['documentPath'] . $path);
  117. }
  118. }
  119. /**
  120. * Remove a Label by Label-ID
  121. *
  122. * @param int $labelID ID of Label
  123. *
  124. * @return void
  125. *
  126. */
  127. public static function removeLabel($labelID) {
  128. global $db;
  129. $db->removeQuery("DELETE FROM `labels` WHERE `id` = " . $labelID . ";");
  130. }
  131. }
  132. ?>