document.class.php 19 KB

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