2
0

document.class.php 21 KB

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