document.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. db::query("DELETE FROM ". DB_T_DOCUMENT_FILES ." WHERE id_files = :id_files");
  17. db::bind(':id_files', $_id);
  18. db::execute();
  19. file::delete($_id, DIR_DATAS_DOCS);
  20. }
  21. static public function deleteFiles(float $_id){
  22. foreach (self::getFiles($_id) as $file) {
  23. db::query("DELETE FROM ". DB_T_DOCUMENT_FILES ." WHERE id_files = :id_files");
  24. db::bind(':id_files', $file["id"]);
  25. db::execute();
  26. file::delete($file["id"], DIR_DATAS_DOCS);
  27. }
  28. }
  29. static public function getTypes(){
  30. db::query("SELECT "
  31. . "* "
  32. . "FROM " . DB_T_TYPE_DOCUMENT . " "
  33. . "ORDER BY " . DB_T_TYPE_DOCUMENT . ".id ASC");
  34. return db::resultset();
  35. }
  36. public static function delete(float $_id)
  37. {
  38. try {
  39. db::query("DELETE FROM ". DB_T_DOCUMENT_TAGS ." WHERE id_documents = :id_documents");
  40. db::bind(':id_documents', $_id);
  41. db::execute();
  42. self::deleteFiles($_id);
  43. db::query("DELETE FROM " . DB_T_DOCUMENTS . " WHERE id = :id");
  44. db::bind(':id', $_id);
  45. db::execute();
  46. alert::recSuccess("Le document vient d'être supprimé");
  47. return TRUE;
  48. } catch (Exception $ex) {
  49. alert::recError("Erreur à la suppression du document");
  50. return FALSE;
  51. }
  52. }
  53. public static function lastAdd()
  54. {
  55. db::query("SELECT MAX(id) AS id FROM " . DB_T_DOCUMENTS);
  56. return db::single()["id"];
  57. }
  58. private static function addFile(float $_idDocument, string $_idFile)
  59. {
  60. db::query("INSERT INTO " . DB_T_DOCUMENT_FILES . " (id_documents, id_files) VALUES (:id_documents, :id_files)");
  61. db::bind(':id_documents', $_idDocument);
  62. db::bind(':id_files', $_idFile);
  63. try {
  64. db::execute();
  65. return TRUE;
  66. } catch (Exception $ex) {
  67. return FALSE;
  68. }
  69. }
  70. private static function addTags(float $_idDocument, string $_tags = NULL, float $_type)
  71. {
  72. db::query("DELETE FROM " . DB_T_DOCUMENT_TAGS . " WHERE id_documents = :id_documents AND id_type_tags = :id_type_tags");
  73. db::bind(':id_documents', $_idDocument);
  74. db::bind(':id_type_tags', $_type);
  75. db::execute();
  76. if($_tags != NULL){
  77. $tags = explode(",", $_tags);
  78. $sqlMaj = "";
  79. foreach ($tags as $tag) {
  80. $sqlMaj .= " (:id_documents, ".$tag.", :id_type_tags),";
  81. }
  82. $sqlMaj = substr($sqlMaj, 0, -1);
  83. db::query("INSERT INTO " . DB_T_DOCUMENT_TAGS . " (id_documents, id_tags, id_type_tags) VALUES" . $sqlMaj);
  84. db::bind(':id_documents', $_idDocument);
  85. db::bind(':id_type_tags', $_type);
  86. try {
  87. db::execute();
  88. return TRUE;
  89. } catch (Exception $ex) {
  90. return FALSE;
  91. }
  92. }
  93. }
  94. public static function getOrphanTags(){
  95. db::query("SELECT
  96. " . DB_T_TAGS . ".id
  97. FROM " . DB_T_TAGS . "
  98. LEFT JOIN " . DB_T_DOCUMENT_TAGS . " ON tags.id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  99. WHERE " . DB_T_DOCUMENT_TAGS . ".id_tags IS NULL AND " . DB_T_TAGS . ".id_type = 2");
  100. return db::resultset();
  101. }
  102. public static function cleanOrphanTags(){
  103. foreach (self::getOrphanTags() as $value) {
  104. db::query("DELETE FROM ". DB_T_TAGS ." WHERE id = :id");
  105. db::bind(':id', $value["id"]);
  106. db::execute();
  107. }
  108. }
  109. public static function add()
  110. {
  111. session::setTemp(core::getPost(), "document");
  112. $file = core::getFiles("document-import");
  113. $md5 = md5_file($file["tmp_name"]);
  114. if(file::findM5($md5) == TRUE){
  115. alert::recError("Ce fichier a déjà été utilisé : " . $file["name"]);
  116. session::setTemp(core::getPost(), "document");
  117. } else {
  118. db::query("INSERT INTO " . DB_T_DOCUMENTS . " (id_type, titre, date, deadline, description, montant, id_user) VALUES (:id_type, :titre, :date, :deadline, :description, :montant, :id_user)");
  119. db::bind(':id_type', core::getPost("id_type"));
  120. db::bind(':titre', core::getPost("titre"));
  121. db::bind(':date', core::getPost("date"));
  122. db::bind(':deadline', core::getPost("deadline"));
  123. db::bind(':description', core::getPost("description"));
  124. db::bind(':montant', core::getPost("montant"));
  125. db::bind(':id_user', session::getId());
  126. try {
  127. db::execute();
  128. $lastId = db::lastInsertId();
  129. } catch (Exception $ex) {
  130. alert::recError("Erreur à l'enregistrement de la fiche : " . core::getPost("titre"));
  131. if(core::isDebug()) { alert::recError("Stack : " . $ex); }
  132. return FALSE;
  133. }
  134. try {
  135. $idFile = self::uploadFile($file);
  136. } catch (Exception $ex) {
  137. alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $idFile);
  138. if(core::isDebug()) { alert::recError("Stack : " . $ex); }
  139. return FALSE;
  140. }
  141. try {
  142. self::addFile($lastId, $idFile);
  143. } catch (Exception $ex) {
  144. alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile);
  145. if(core::isDebug()) { alert::recError("Stack : " . $ex); }
  146. return FALSE;
  147. }
  148. try {
  149. $tagsUser = tags::textToId(core::getPost("tagsUser"), 1);
  150. self::addTags($lastId, $tagsUser, 1);
  151. $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2);
  152. self::addTags($lastId, $tagsSupplier, 2);
  153. } catch (Exception $ex) {
  154. alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile);
  155. if(core::isDebug()) { alert::recError("Stack : " . $ex); }
  156. return FALSE;
  157. }
  158. return $lastId;
  159. }
  160. }
  161. public static function update()
  162. {
  163. if(core::ifFiles("attachement-document") == TRUE){
  164. $file = core::getFiles("attachement-document");
  165. $md5 = md5_file($file["tmp_name"]);
  166. }
  167. if(isset($md5) AND file::findM5($md5) == TRUE){
  168. alert::recError("Le fichier \"" . $file["name"] . "\" a déjà été utilisé");
  169. session::setTemp(core::getPost(), "document");
  170. } else {
  171. if(isset($md5)){
  172. try {
  173. $idFile = self::uploadFile($file);
  174. } catch (Exception $ex) {
  175. alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $idFile);
  176. return FALSE;
  177. }
  178. try {
  179. self::addFile(core::getPost("id"), $idFile);
  180. } catch (Exception $ex) {
  181. alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile);
  182. return FALSE;
  183. }
  184. }
  185. if(core::getPost("delete-attachement")){
  186. foreach (core::getPost("delete-attachement") as $deleteAttach) {
  187. self::deleteFile($deleteAttach);
  188. }
  189. }
  190. try {
  191. $tagsUser = tags::textToId(core::getPost("tagsUser"), 1);
  192. self::addTags(core::getPost("id"), $tagsUser, 1);
  193. $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2);
  194. self::addTags(core::getPost("id"), $tagsSupplier, 2);
  195. } catch (Exception $ex) {
  196. alert::recError("Erreur à l'enregistrement de la liaison : " . core::getPost("id"));
  197. return FALSE;
  198. }
  199. if(core::ifPost("date_done") AND core::getPost("date_done") != ""){
  200. $sql = ", id_user_done = :id_user_done, date_done = :date_done ";
  201. } else {
  202. $sql = "";
  203. }
  204. db::query("UPDATE " . DB_T_DOCUMENTS . " SET "
  205. . "id_type = :id_type, "
  206. . "titre = :titre, "
  207. . "date = :date, "
  208. . "deadline = :deadline, "
  209. . "description = :description, "
  210. . "montant = :montant "
  211. . $sql
  212. . "WHERE id = :id");
  213. db::bind(':id_type', core::getPost("id_type"));
  214. db::bind(':titre', core::getPost("titre"));
  215. db::bind(':date', core::getPost("date"));
  216. db::bind(':deadline', core::getPost("deadline"));
  217. db::bind(':description', core::getPost("description"));
  218. db::bind(':montant', core::getPost("montant"));
  219. db::bind(':id', core::getPost("id"));
  220. if(core::ifPost("date_done") AND core::getPost("date_done") == TRUE){
  221. db::bind(':id_user_done', session::getId());
  222. db::bind(':date_done', core::getPost("date_done"));
  223. }
  224. try {
  225. db::execute();
  226. alert::recSuccess("Document mis à jour avec succès");
  227. return TRUE;
  228. } catch (Exception $ex) {
  229. alert::recError("Erreur de mise à jour du document : " . $ex);
  230. return FALSE;
  231. }
  232. }
  233. }
  234. static public function printFile(string $_id) {
  235. $filePatch = file::download($_id, DIR_DATAS_DOCS);
  236. if (file_exists($filePatch) && is_readable($filePatch)) {
  237. $file_info = new finfo(FILEINFO_MIME_TYPE);
  238. $mime_type = $file_info->file($filePatch);
  239. header('Content-Type: ' . $mime_type);
  240. header('Content-Length: ' . filesize($filePatch));
  241. readfile($filePatch);
  242. } else {
  243. echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
  244. }
  245. }
  246. static public function getList(){
  247. }
  248. static public function get(float $_id){
  249. db::query("SELECT "
  250. . "" . DB_T_DOCUMENTS . ".id, "
  251. . "" . DB_T_DOCUMENTS . ".id_type, "
  252. . "" . DB_T_DOCUMENTS . ".titre, "
  253. . "" . DB_T_DOCUMENTS . ".date, "
  254. . "" . DB_T_DOCUMENTS . ".deadline, "
  255. . "" . DB_T_DOCUMENTS . ".description, "
  256. . "" . DB_T_DOCUMENTS . ".montant, "
  257. . "" . DB_T_DOCUMENTS . ".id_user_done, "
  258. . "" . DB_T_DOCUMENTS . ".date_done, "
  259. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS doneUser "
  260. . "FROM " . DB_T_DOCUMENTS . " "
  261. . "LEFT JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_DOCUMENTS . ".id_user_done "
  262. . "WHERE " . DB_T_DOCUMENTS . ".id = :id");
  263. db::bind(':id', $_id);
  264. $document = db::single();
  265. $document["tagsSupplier"] = self::getTags($_id, 2);
  266. $document["tagsUser"] = self::getTags($_id, 1);
  267. $files = self::getFiles($_id);
  268. return array("document" => $document, "files" => $files);
  269. }
  270. static public function getTags(float $_idDocument, float $_idTypeTags){
  271. db::query("SELECT "
  272. . "" . DB_T_TAGS . ".label "
  273. . "FROM " . DB_T_DOCUMENT_TAGS . " "
  274. . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags "
  275. . "WHERE " . DB_T_DOCUMENT_TAGS . ".id_documents = :idDocument AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = :idTypeTags "
  276. . "ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer ASC");
  277. db::bind(':idDocument', $_idDocument);
  278. db::bind(':idTypeTags', $_idTypeTags);
  279. $tmp = db::resultset();
  280. if(isset($tmp[0])){
  281. $return = NULL;
  282. foreach ($tmp as $value) {
  283. $return .= $value["label"].",";
  284. }
  285. $return = substr($return, 0, -1);
  286. return $return;
  287. } else {
  288. return NULL;
  289. }
  290. }
  291. static public function getFiles(float $_idDocument){
  292. db::query("SELECT "
  293. . "" . DB_T_FILES . ".id, "
  294. . "" . DB_T_FILES . ".name, "
  295. . "" . DB_T_FILES . ".size, "
  296. . "" . DB_T_FILES . ".creer, "
  297. . "" . DB_T_FILES . ".id_user, "
  298. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS user "
  299. . "FROM " . DB_T_DOCUMENT_FILES . " "
  300. . "INNER JOIN " . DB_T_FILES . " ON " . DB_T_FILES . ".id = " . DB_T_DOCUMENT_FILES . ".id_files "
  301. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_FILES . ".id_user "
  302. . "WHERE " . DB_T_DOCUMENT_FILES . ".id_documents = :id "
  303. . "ORDER BY " . DB_T_FILES . ".creer");
  304. db::bind(':id', $_idDocument);
  305. $tmp = db::resultset();
  306. if(isset($tmp[0])){
  307. return $tmp;
  308. } else {
  309. return NULL;
  310. }
  311. }
  312. static public function getAssignMe(){
  313. $tags = user::getIdTags(session::getId());
  314. $where = NULL;
  315. foreach ($tags AS $key => $value) {
  316. if($key == 0){
  317. $where = "WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  318. } else {
  319. $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  320. }
  321. }
  322. db::query("SELECT
  323. " . DB_T_DOCUMENTS . ".id,
  324. " . DB_T_DOCUMENTS . ".titre,
  325. " . DB_T_DOCUMENTS . ".date,
  326. " . DB_T_DOCUMENTS . ".deadline,
  327. " . DB_T_DOCUMENTS . ".description,
  328. " . DB_T_DOCUMENTS . ".montant,
  329. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  330. FROM " . DB_T_DOCUMENT_TAGS . "
  331. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  332. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 2
  333. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS tags,
  334. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  335. FROM " . DB_T_DOCUMENT_TAGS . "
  336. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  337. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 1
  338. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS assign,
  339. " . DB_T_TYPE_DOCUMENT . ".label
  340. FROM " . DB_T_DOCUMENT_TAGS . "
  341. INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents
  342. INNER JOIN " . DB_T_TYPE_DOCUMENT . " ON " . DB_T_TYPE_DOCUMENT . ".id = " . DB_T_DOCUMENTS . ".id_type
  343. " . $where);
  344. return db::resultset();
  345. }
  346. static public function printAttachement(array $_attachs){
  347. echo '<ol class="list-group list-group-numbered">';
  348. foreach ($_attachs as $key => $attach) {
  349. echo '<li class="list-group-item d-flex justify-content-between align-items-start" id="attach-'.$attach["id"].'">
  350. <div class="ms-2 me-auto">
  351. <div><span class="fw-bold">'.$attach["name"].'</span> ('.core::convertBytes($attach["size"]).')</div>
  352. Chargé le '.core::convertDate($attach["creer"]).' par '.$attach["user"].'
  353. <div id="select-attach-'.$attach["id"].'" style="color:red;"></div>
  354. </div>';
  355. if($key == 0){
  356. echo '
  357. <button type="button" class="btn btn btn-outline-primary" onclick="window.open(\'/document.php?id=' . $attach["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
  358. ';
  359. } else {
  360. echo '<div class="btn-group">
  361. <button type="button" class="btn btn btn-outline-primary" onclick="window.open(\'/document.php?id=' . $attach["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
  362. <button type="button" class="btn btn-outline-danger" onclick="deleteAttachment(\''.$attach["id"].'\')" id="button-attach-'.$attach["id"].'">' . icon::getFont(["icon" => "bi bi-trash"]) . '</button>
  363. </div>';
  364. }
  365. echo '</li>';
  366. }
  367. echo '</ol><br />';
  368. }
  369. static public function myAssign(?array $_tags = NULL){
  370. if($_tags == NULL){
  371. return NULL;
  372. } else {
  373. $where = NULL;
  374. foreach ($_tags AS $key => $value) {
  375. if($key == 0){
  376. $where = "WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  377. } else {
  378. $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  379. }
  380. }
  381. }
  382. db::query("SELECT "
  383. . "COUNT(" . DB_T_DOCUMENT_TAGS . ".id_tags) AS nb "
  384. . "FROM " . DB_T_DOCUMENT_TAGS . " "
  385. . "INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents "
  386. . $where);
  387. return db::single()["nb"];
  388. }
  389. static public function menu(){
  390. $tags = user::getIdTags(session::getId());
  391. $nb = document::myAssign($tags);
  392. if($tags != NULL){
  393. $badge = "";
  394. if($nb > 0){
  395. $badge = '<span class="position-absolute start-100 translate-middle badge rounded-pill bg-danger">' . $nb . '</span>';
  396. }
  397. echo '<a href="/documents-my-assign.html" class="dropdown-item">Vos assignations' . $badge . '</a>';
  398. }
  399. }
  400. static public function badge(){
  401. $tags = user::getIdTags(session::getId());
  402. $nb = document::myAssign($tags);
  403. if($tags != NULL){
  404. if($nb > 0){
  405. echo '<span class="position-absolute start-100 translate-middle p-1 bg-danger border border-light rounded-circle"></span>';
  406. }
  407. }
  408. }
  409. }