document.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. class document
  3. {
  4. static public function uploadFile(array $_temp){
  5. $tmp = file::record($_temp, DIR_DATAS_DOCS);
  6. if($tmp != FALSE){
  7. return $tmp;
  8. } else {
  9. return FALSE;
  10. }
  11. }
  12. static public function readFile(string $_id){
  13. return file::download($_id, DIR_DATAS_DOCS);
  14. }
  15. static public function deleteFile(string $_id){
  16. return file::delete($_id, DIR_DATAS_DOCS);
  17. }
  18. static public function getTypes(){
  19. db::query("SELECT "
  20. . "* "
  21. . "FROM " . DB_T_TYPE_DOCUMENT);
  22. return db::resultset();
  23. }
  24. public static function delete(int $_id)
  25. {
  26. $tmp = self::get($_id);
  27. db::query("DELETE FROM " . DB_T_DOCUMENTS . " WHERE id = :id");
  28. db::bind(':id', $_id);
  29. db::execute();
  30. return self::deleteFile($tmp["id_file"]);
  31. }
  32. public static function lastAdd()
  33. {
  34. db::query("SELECT MAX(id) AS id FROM " . DB_T_DOCUMENTS);
  35. return db::single()["id"];
  36. }
  37. public static function add()
  38. {
  39. /**
  40. *
  41. * @var string $idFile
  42. */
  43. $file = core::getFiles("document-import");
  44. $idFile = self::uploadFile($file);
  45. $tagsUser = tags::textToId(core::getPost("tagsUser"), 1);
  46. $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2);
  47. if($idFile != NULL){
  48. db::query("INSERT INTO " . DB_T_DOCUMENTS . " (id_type, id_file, titre, date, deadline, description, tagsUser, tagsSupplier) VALUES (:id_type, :id_file, :titre, :date, :deadline, :description, :tagsUser, :tagsSupplier)");
  49. db::bind(':id_type', core::getPost("id_type"));
  50. db::bind(':id_file', $idFile);
  51. db::bind(':titre', core::getPost("titre"));
  52. db::bind(':date', core::getPost("date"));
  53. db::bind(':deadline', core::getPost("deadline"));
  54. db::bind(':description', core::getPost("description"));
  55. db::bind(':tagsUser', $tagsUser);
  56. db::bind(':tagsSupplier', $tagsSupplier);
  57. try {
  58. db::execute();
  59. alert::recSuccess("Document enregistré avec succès");
  60. return TRUE;
  61. } catch (Exception $ex) {
  62. file::delete($idFile, DIR_DATAS_DOCS);
  63. alert::recError("Erreur à l'enregistrement du document : " . core::getPost("titre"));
  64. return FALSE;
  65. }
  66. } else {
  67. file::delete($idFile, DIR_DATAS_DOCS);
  68. alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $file["tmp_name"]);
  69. return FALSE;
  70. }
  71. }
  72. public static function update()
  73. {
  74. $tagsUser = tags::textToId(core::getPost("tagsUser"), 1);
  75. $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2);
  76. if(core::ifPost("done") AND core::getPost("done") == TRUE){
  77. $sql = "id_user_done = :id_user_done, date_done = CURRENT_TIMESTAMP, ";
  78. } else {
  79. $sql = "";
  80. }
  81. db::query("UPDATE " . DB_T_DOCUMENTS . " SET "
  82. . "id_type = :id_type, "
  83. . "titre = :titre, "
  84. . "date = :date, "
  85. . "deadline = :deadline, "
  86. . "description = :description, "
  87. . $sql
  88. . "tagsUser = :tagsUser, "
  89. . "tagsSupplier = :tagsSupplier "
  90. . "WHERE id = :id");
  91. db::bind(':id_type', core::getPost("id_type"));
  92. db::bind(':titre', core::getPost("titre"));
  93. db::bind(':date', core::getPost("date"));
  94. db::bind(':deadline', core::getPost("deadline"));
  95. db::bind(':description', core::getPost("description"));
  96. db::bind(':tagsUser', $tagsUser);
  97. db::bind(':tagsSupplier', $tagsSupplier);
  98. db::bind(':id', core::getPost("id"));
  99. if(core::ifPost("done") AND core::getPost("done") == TRUE){
  100. db::bind(':id_user_done', session::getId());
  101. }
  102. try {
  103. db::execute();
  104. alert::recSuccess("Document mis à jour avec succès");
  105. return TRUE;
  106. } catch (Exception $ex) {
  107. alert::recError("Erreur de mise à jour du document");
  108. return FALSE;
  109. }
  110. }
  111. static public function printFile(string $_id) {
  112. $filePatch = file::download($_id, DIR_DATAS_DOCS);
  113. if (file_exists($filePatch) && is_readable($filePatch)) {
  114. $file_info = new finfo(FILEINFO_MIME_TYPE);
  115. $mime_type = $file_info->file($filePatch);
  116. header('Content-Type: ' . $mime_type);
  117. header('Content-Length: ' . filesize($filePatch));
  118. readfile($filePatch);
  119. } else {
  120. echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
  121. }
  122. }
  123. static public function getList(){
  124. }
  125. static public function get(float $_id){
  126. db::query("SELECT "
  127. . "" . DB_T_DOCUMENTS . ".id, "
  128. . "" . DB_T_DOCUMENTS . ".id_type, "
  129. . "" . DB_T_DOCUMENTS . ".id_file, "
  130. . "" . DB_T_DOCUMENTS . ".titre, "
  131. . "" . DB_T_DOCUMENTS . ".date, "
  132. . "" . DB_T_DOCUMENTS . ".deadline, "
  133. . "" . DB_T_DOCUMENTS . ".description, "
  134. . "" . DB_T_DOCUMENTS . ".tagsUser, "
  135. . "" . DB_T_DOCUMENTS . ".tagsSupplier, "
  136. . "" . DB_T_DOCUMENTS . ".id_user_done, "
  137. . "" . DB_T_DOCUMENTS . ".date_done, "
  138. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS doneUser, "
  139. . "" . DB_T_FILES . ".name, "
  140. . "CONCAT(ROUND((" . DB_T_FILES . ".size / 1024 / 1024), 2), ' Mo') AS size "
  141. . "FROM " . DB_T_DOCUMENTS . " "
  142. . "INNER JOIN " . DB_T_FILES . " ON " . DB_T_FILES . ".id = " . DB_T_DOCUMENTS . ".id_file "
  143. . "LEFT JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_DOCUMENTS . ".id_user_done "
  144. . "WHERE " . DB_T_DOCUMENTS . ".id = :id");
  145. db::bind(':id', $_id);
  146. $row = db::single();
  147. $row["tagsUser"] = tags::idToTtext($row["tagsUser"]);
  148. $row["tagsSupplier"] = tags::idToTtext($row["tagsSupplier"]);
  149. return $row;
  150. }
  151. }