2
0

document.class.php 19 KB

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