document.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. $checkFile = file::getErrorUpload($file);
  121. if(isset($checkFile["status"]) AND $checkFile["status"] == "error"){
  122. alert::recError($checkFile["description"]);
  123. return FALSE;
  124. }
  125. $md5 = md5_file($file["tmp_name"]);
  126. if(file::findM5($md5) == TRUE){
  127. alert::recError("Ce fichier a déjà été utilisé : " . $file["name"]);
  128. } else {
  129. db::query("INSERT INTO " . DB_T_DOCUMENTS . " (id_type, titre, date, deadline, description, montant, rapprochement, id_client, id_user) VALUES (:id_type, :titre, :date, :deadline, :description, :montant, :rapprochement, :id_client, :id_user)");
  130. db::bind(':id_type', core::getPost("id_type"));
  131. db::bind(':titre', core::getPost("titre"));
  132. db::bind(':date', core::getPost("date"));
  133. db::bind(':deadline', core::getPost("deadline"));
  134. db::bind(':description', core::getPost("description"));
  135. db::bind(':montant', !empty(core::getPost("montant")) ? floatval(core::getPost("montant")) : 0.0);
  136. db::bind(':rapprochement', core::getPost("rapprochement"));
  137. db::bind(':id_client', core::getPost("id_client") == "" ? NULL : core::getPost("id_client"));
  138. db::bind(':id_user', session::getId());
  139. try {
  140. db::execute();
  141. $lastId = db::lastInsertId();
  142. } catch (Exception $ex) {
  143. alert::recError("Erreur à l'enregistrement de la fiche : " . core::getPost("titre"));
  144. if(debug::isFile("debug")) { alert::recError("Stack : " . $ex); }
  145. return FALSE;
  146. }
  147. try {
  148. $idFile = self::uploadFile($file);
  149. } catch (Exception $ex) {
  150. alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $idFile);
  151. if(debug::isFile("debug")) { alert::recError("Stack : " . $ex); }
  152. return FALSE;
  153. }
  154. try {
  155. self::addFile($lastId, $idFile, 1);
  156. } catch (Exception $ex) {
  157. alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile);
  158. if(debug::isFile("debug")) { alert::recError("Stack : " . $ex); }
  159. return FALSE;
  160. }
  161. try {
  162. $tagsUser = tags::textToId(core::getPost("tagsUser"), 1);
  163. self::addTags($lastId, $tagsUser, 1);
  164. $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2);
  165. self::addTags($lastId, $tagsSupplier, 2);
  166. } catch (Exception $ex) {
  167. alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile);
  168. if(debug::isFile("debug")) { alert::recError("Stack : " . $ex); }
  169. return FALSE;
  170. }
  171. return $lastId;
  172. }
  173. }
  174. public static function update()
  175. {
  176. $file = core::getFiles("attachement-document");
  177. if($file != NULL AND $file["name"] != ""){
  178. $checkFile = file::getErrorUpload($file);
  179. if(isset($checkFile["status"]) AND $checkFile["status"] == "error"){
  180. alert::recError($checkFile["description"]);
  181. return FALSE;
  182. }
  183. $md5 = md5_file($file["tmp_name"]);
  184. }
  185. if(isset($md5) AND file::findM5($md5) == TRUE){
  186. alert::recError("Le fichier \"" . $file["name"] . "\" a déjà été utilisé");
  187. session::setTemp(core::getPost(), "document");
  188. } else {
  189. if(isset($md5)){
  190. try {
  191. $idFile = self::uploadFile($file);
  192. } catch (Exception $ex) {
  193. alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $idFile);
  194. return FALSE;
  195. }
  196. try {
  197. self::addFile(core::getPost("id"), $idFile, 0);
  198. } catch (Exception $ex) {
  199. alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile);
  200. return FALSE;
  201. }
  202. }
  203. if(core::getPost("delete-attachement")){
  204. foreach (core::getPost("delete-attachement") as $deleteAttach) {
  205. self::deleteFile($deleteAttach);
  206. }
  207. }
  208. if(core::getPost("default-attachement")){
  209. self::principalFile(core::getPost("id"), core::getPost("default-attachement"));
  210. }
  211. try {
  212. $tagsUser = tags::textToId(core::getPost("tagsUser"), 1);
  213. self::addTags(core::getPost("id"), $tagsUser, 1);
  214. $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2);
  215. self::addTags(core::getPost("id"), $tagsSupplier, 2);
  216. } catch (Exception $ex) {
  217. alert::recError("Erreur à l'enregistrement de la liaison : " . core::getPost("id"));
  218. return FALSE;
  219. }
  220. if(core::ifPost("date_done") AND core::getPost("date_done") != ""){
  221. $sql = ", id_user_done = :id_user_done, date_done = :date_done ";
  222. } else {
  223. $sql = "";
  224. }
  225. db::query("UPDATE " . DB_T_DOCUMENTS . " SET "
  226. . "id_type = :id_type, "
  227. . "titre = :titre, "
  228. . "date = :date, "
  229. . "deadline = :deadline, "
  230. . "description = :description, "
  231. . "montant = :montant, "
  232. . "rapprochement = :rapprochement, "
  233. . "id_client = :id_client "
  234. . $sql
  235. . "WHERE id = :id");
  236. db::bind(':id_type', core::getPost("id_type"));
  237. db::bind(':titre', core::getPost("titre"));
  238. db::bind(':date', core::getPost("date"));
  239. db::bind(':deadline', core::getPost("deadline"));
  240. db::bind(':description', core::getPost("description"));
  241. db::bind(':montant', core::getPost("montant"));
  242. db::bind(':rapprochement', core::getPost("rapprochement"));
  243. db::bind(':id_client', core::getPost("id_client") == "" ? NULL : core::getPost("id_client"));
  244. db::bind(':id', core::getPost("id"));
  245. if(core::ifPost("date_done") AND core::getPost("date_done") == TRUE){
  246. db::bind(':id_user_done', session::getId());
  247. db::bind(':date_done', core::getPost("date_done"));
  248. }
  249. try {
  250. db::execute();
  251. alert::recSuccess("Document mis à jour avec succès");
  252. return TRUE;
  253. } catch (Exception $ex) {
  254. alert::recError("Erreur de mise à jour du document : " . $ex);
  255. return FALSE;
  256. }
  257. }
  258. }
  259. static public function printFile(string $_id) {
  260. $filePatch = file::download($_id, DIR_DATAS_DOCS);
  261. if (file_exists($filePatch) && is_readable($filePatch)) {
  262. $file_info = new finfo(FILEINFO_MIME_TYPE);
  263. $mime_type = $file_info->file($filePatch);
  264. // Vérification si le fichier est de type XML
  265. if ($mime_type === 'application/xml' || $mime_type === 'text/xml') {
  266. // Chargement du contenu XML
  267. $xml_content = file_get_contents($filePatch);
  268. xml::print($xml_content);
  269. } else {
  270. // Si ce n'est pas un fichier XML, comportement normal pour servir le fichier
  271. header('Content-Type: ' . $mime_type);
  272. header('Content-Length: ' . filesize($filePatch));
  273. readfile($filePatch);
  274. }
  275. } else {
  276. echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
  277. }
  278. }
  279. static public function getFile(string $_id) {
  280. $filePatch = file::download($_id, DIR_DATAS_DOCS);
  281. if (file_exists($filePatch) && is_readable($filePatch)) {
  282. $file_info = new finfo(FILEINFO_MIME_TYPE);
  283. $mime_type = $file_info->file($filePatch);
  284. header('Content-Type: ' . $mime_type);
  285. header('Content-Length: ' . filesize($filePatch));
  286. readfile($filePatch);
  287. } else {
  288. echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
  289. }
  290. }
  291. static public function get(float $_id){
  292. db::query("SELECT "
  293. . "" . DB_T_DOCUMENTS . ".id, "
  294. . "" . DB_T_DOCUMENTS . ".id_type, "
  295. . "" . DB_T_DOCUMENTS . ".titre, "
  296. . "" . DB_T_DOCUMENTS . ".date, "
  297. . "" . DB_T_DOCUMENTS . ".deadline, "
  298. . "" . DB_T_DOCUMENTS . ".description, "
  299. . "" . DB_T_DOCUMENTS . ".montant, "
  300. . "" . DB_T_DOCUMENTS . ".rapprochement, "
  301. . "" . DB_T_DOCUMENTS . ".id_client, "
  302. . "" . DB_T_DOCUMENTS . ".id_user_done, "
  303. . "" . DB_T_DOCUMENTS . ".date_done, "
  304. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS doneUser "
  305. . "FROM " . DB_T_DOCUMENTS . " "
  306. . "LEFT JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_DOCUMENTS . ".id_user_done "
  307. . "WHERE " . DB_T_DOCUMENTS . ".id = :id");
  308. db::bind(':id', $_id);
  309. $document = db::single();
  310. if(empty($document)){
  311. $document = [];
  312. }
  313. $document["tagsSupplier"] = self::getTags($_id, 2);
  314. $document["tagsUser"] = self::getTags($_id, 1);
  315. $files = self::getFiles($_id);
  316. return array("document" => $document, "files" => $files);
  317. }
  318. static public function getTags(float $_idDocument, float $_idTypeTags){
  319. db::query("SELECT "
  320. . "" . DB_T_TAGS . ".label "
  321. . "FROM " . DB_T_DOCUMENT_TAGS . " "
  322. . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags "
  323. . "WHERE " . DB_T_DOCUMENT_TAGS . ".id_documents = :idDocument AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = :idTypeTags "
  324. . "ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer ASC");
  325. db::bind(':idDocument', $_idDocument);
  326. db::bind(':idTypeTags', $_idTypeTags);
  327. $tmp = db::resultset();
  328. if(isset($tmp[0])){
  329. $return = NULL;
  330. foreach ($tmp as $value) {
  331. $return .= $value["label"].",";
  332. }
  333. $return = substr($return, 0, -1);
  334. return $return;
  335. } else {
  336. return NULL;
  337. }
  338. }
  339. static public function getFiles(float $_idDocument){
  340. db::query("SELECT "
  341. . "" . DB_T_FILES . ".id, "
  342. . "" . DB_T_FILES . ".name, "
  343. . "" . DB_T_FILES . ".size, "
  344. . "" . DB_T_FILES . ".creer, "
  345. . "" . DB_T_FILES . ".id_user, "
  346. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS user, "
  347. . "" . DB_T_DOCUMENT_FILES . ".principal "
  348. . "FROM " . DB_T_DOCUMENT_FILES . " "
  349. . "INNER JOIN " . DB_T_FILES . " ON " . DB_T_FILES . ".id = " . DB_T_DOCUMENT_FILES . ".id_files "
  350. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_FILES . ".id_user "
  351. . "WHERE " . DB_T_DOCUMENT_FILES . ".id_documents = :id "
  352. . "ORDER BY " . DB_T_FILES . ".creer");
  353. db::bind(':id', $_idDocument);
  354. $tmp = db::resultset();
  355. if(isset($tmp[0])){
  356. foreach ($tmp as $file) {
  357. if($file["principal"] == TRUE){
  358. $return["principal"] = $file;
  359. } else {
  360. $return[] = $file;
  361. }
  362. }
  363. if(empty($return["principal"])){
  364. $return["principal"] = $return[0];
  365. self::principalFile($_idDocument, $return["principal"]["id"]);
  366. unset($return[0]);
  367. }
  368. return $return;
  369. } else {
  370. return NULL;
  371. }
  372. }
  373. static public function principalFile(float $_idDocument, string $_idFile){
  374. db::query("UPDATE " . DB_T_DOCUMENT_FILES . " SET principal = :principal WHERE id_documents = :id_documents");
  375. db::bind(':principal', 0);
  376. db::bind(':id_documents', $_idDocument);
  377. db::execute();
  378. db::query("UPDATE " . DB_T_DOCUMENT_FILES . " SET principal = :principal WHERE id_documents = :id_documents AND id_files = :id_files");
  379. db::bind(':principal', 1);
  380. db::bind(':id_documents', $_idDocument);
  381. db::bind(':id_files', $_idFile);
  382. db::execute();
  383. }
  384. static public function getAssign(float $_id = NULL){
  385. $idUser = is_null($_id) ? session::getId() : $_id;
  386. $tags = user::getIdTags($idUser);
  387. $where = NULL;
  388. foreach ($tags AS $key => $value) {
  389. if($key == 0){
  390. $where = " WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND (" . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  391. } else {
  392. $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  393. }
  394. }
  395. $where .= ")";
  396. db::query("SELECT
  397. " . DB_T_DOCUMENTS . ".id,
  398. " . DB_T_DOCUMENTS . ".titre,
  399. " . DB_T_DOCUMENTS . ".date,
  400. " . DB_T_DOCUMENTS . ".deadline,
  401. " . DB_T_DOCUMENTS . ".description,
  402. " . DB_T_DOCUMENTS . ".montant,
  403. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  404. FROM " . DB_T_DOCUMENT_TAGS . "
  405. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  406. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 2
  407. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS tags,
  408. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  409. FROM " . DB_T_DOCUMENT_TAGS . "
  410. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  411. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 1
  412. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS assign,
  413. " . DB_T_TYPE_DOCUMENT . ".label
  414. FROM " . DB_T_DOCUMENT_TAGS . "
  415. INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents
  416. INNER JOIN " . DB_T_TYPE_DOCUMENT . " ON " . DB_T_TYPE_DOCUMENT . ".id = " . DB_T_DOCUMENTS . ".id_type
  417. " . $where .
  418. " GROUP BY documents.id");
  419. return db::resultset();
  420. }
  421. static public function printAttachement(array $_attachs){
  422. $principal = $_attachs["principal"];
  423. echo '<ol class="list-group list-group-numbered">';
  424. echo ' <li class="list-group-item d-flex justify-content-between align-items-start" id="attach-'.$principal["id"].'">
  425. <div class="ms-2 me-auto">
  426. <div><span class="fw-bold">'.$principal["name"].'</span> ('.core::convertBytes($principal["size"]).')</div>
  427. Chargé le '.core::convertDate($principal["creer"]).' par '.$principal["user"].'
  428. <div id="select-attach-'.$principal["id"].'" style="color:red;"></div>
  429. </div>
  430. <div class="btn-group">
  431. <button type="button" title="Voir le document" class="btn btn btn-outline-secondary" onclick="window.open(\'/document.php?id=' . $principal["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
  432. <button type="button" title="Télécharger le document" class="btn btn btn-outline-secondary" onclick="downloadFile(\'/document.php?id=' . $principal["id"] . '&download=1\', \''.$principal["name"].'\')">' . icon::getFont(["icon" => "bi bi-arrow-down-square-fill"]) . '</button>
  433. </div>
  434. </li>';
  435. foreach ($_attachs as $key => $attach) {
  436. if($key != "principal"){
  437. echo ' <li class="list-group-item d-flex justify-content-between align-items-start" id="attach-'.$attach["id"].'">
  438. <div class="ms-2 me-auto">
  439. <div><span class="fw-bold">'.$attach["name"].'</span> ('.core::convertBytes($attach["size"]).')</div>
  440. Chargé le '.core::convertDate($attach["creer"]).' par '.$attach["user"].'
  441. <div id="select-attach-'.$attach["id"].'"></div>
  442. </div><div class="btn-group">
  443. <button type="button" title="Voir le document" class="btn btn btn-outline-secondary" onclick="window.open(\'/document.php?id=' . $attach["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
  444. <button type="button" title="Télécharger le document" class="btn btn btn-outline-secondary" onclick="downloadFile(\'/document.php?id=' . $attach["id"] . '&download=1\', \''.$attach["name"].'\')">' . icon::getFont(["icon" => "bi bi-arrow-down-square-fill"]) . '</button>';
  445. if (access::ifAccesss("add-document")) {
  446. echo '<button type="button" title="Document mis en avant" class="btn btn btn-outline-primary" onclick="defaultAttachment(\''.$attach["id"].'\')" id="button-default-'.$attach["id"].'">' . icon::getFont(["icon" => "bi bi-star-fill"]) . '</button>
  447. <button type="button" title="Supprimer ce document" class="btn btn-outline-danger" onclick="deleteAttachment(\''.$attach["id"].'\')" id="button-delete-'.$attach["id"].'">' . icon::getFont(["icon" => "bi bi-trash"]) . '</button>';
  448. }
  449. echo '</div>
  450. </li>';
  451. }
  452. }
  453. echo '</ol><br />';
  454. }
  455. static public function myAssign(?array $_tags = NULL){
  456. if($_tags == NULL){
  457. return NULL;
  458. } else {
  459. $where = NULL;
  460. foreach ($_tags AS $key => $value) {
  461. if($key == 0){
  462. $where = "WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND (" . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  463. } else {
  464. $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  465. }
  466. }
  467. $where .= ")";
  468. }
  469. db::query("SELECT "
  470. . "COUNT(" . DB_T_DOCUMENT_TAGS . ".id_tags) AS nb "
  471. . "FROM " . DB_T_DOCUMENT_TAGS . " "
  472. . "INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents "
  473. . $where);
  474. return db::single()["nb"];
  475. }
  476. static public function badgeAlert(){
  477. $return = self::myAssign(user::getIdTags(session::getId()));
  478. return $return > 0 ? $return : NULL;
  479. }
  480. static public function assignMailDocument(){
  481. db::query("SELECT "
  482. . "" . DB_T_USER . ".id, "
  483. . "" . DB_T_USER . ".email, "
  484. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS name "
  485. . "FROM " . DB_T_USER_TAGS . " "
  486. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_USER_TAGS . ".id_user "
  487. . "WHERE (" . DB_T_USER_TAGS . ".id_tags = 1 OR " . DB_T_USER_TAGS . ".id_tags = 2) AND " . DB_T_USER . ".deleted = 0 "
  488. . "GROUP BY " . DB_T_USER . ".id");
  489. return db::resultset();
  490. }
  491. public static function sendEmailCronAssign(array $_data)
  492. {
  493. $list = self::getAssign($_data["id"]);
  494. $nb = count($list);
  495. if ($nb > 0) {
  496. $titre = $nb > 1 ? $nb . " documents en attentes de validation" : "Un document en attente de validation";
  497. $message = "Cet email est un récapitulatif des documents qui vous ont été assignés sur le CMS du CSE Invent.<br />A ce jour ";
  498. $message .= $nb > 1 ? $nb . " documents sont en attentes de validation." : "un seul document est en attente de validation.";
  499. $message .= "<br />Ce bilan sera mis à jour une fois par semaine et sera envoyé à l'ensemble des personnes assignées à ces documents.";
  500. $tmp = [
  501. "name" => $_data["name"],
  502. "subject" => $titre,
  503. "message" => $message,
  504. "table" => self::getMailArray($list)
  505. ];
  506. $data = [
  507. "to" => $_data["email"],
  508. "name" => $_data["name"],
  509. "subject" => $titre,
  510. "template" => self::templateMail($tmp)
  511. ];
  512. try {
  513. email::send($data);
  514. historique::recRef("script");
  515. historique::add(
  516. array(
  517. "idType" => historique::getIdRef("CRON"),
  518. "idUser" => NULL,
  519. "idPage" => historique::getIdRef("script"),
  520. "log" => "Email d'assignation envoyé à " . $data["name"]
  521. )
  522. );
  523. } catch (\Throwable $th) {
  524. debug::log($th);
  525. }
  526. }
  527. }
  528. public static function templateMail(array $_data)
  529. {
  530. $logo_url = empty($_data["logo_url"]) ? "https://" . DOMAIN_CMS . "/img/logo.png" : $_data["logo_url"];
  531. $date = empty($_data["date"]) ? core::printDateTxt() : $_data["date"];
  532. $name = empty($_data["name"]) ? NULL : $_data["name"];
  533. $subject = empty($_data["subject"]) ? NULL : $_data["subject"];
  534. $message = empty($_data["message"]) ? NULL : $_data["message"];
  535. $cms_url = empty($_data["cms_url"]) ? "https://" . DOMAIN_CMS : $_data["cms_url"];
  536. $table = empty($_data["table"]) ? NULL : $_data["table"];
  537. $template = email::loadTemplate('cms.documents.html');
  538. if ($template === false) {
  539. echo "Impossible de lire le template d'email.";
  540. return false;
  541. }
  542. // Remplacer les variables dans le template
  543. $template = str_replace([
  544. '{{logo_url}}',
  545. '{{date}}',
  546. '{{name}}',
  547. '{{subject}}',
  548. '{{message}}',
  549. '{{cms_url}}',
  550. '{{table}}'
  551. ], [
  552. $logo_url,
  553. $date,
  554. $name,
  555. $subject,
  556. $message,
  557. $cms_url,
  558. $table
  559. ], $template);
  560. // Si debug
  561. if(debug::isFile("email")){
  562. debug::log($template);
  563. }
  564. return $template;
  565. }
  566. private static function getMailArray(array $_array){
  567. $return = NULL;
  568. foreach ($_array as $value) {
  569. $return .= ' <tr>
  570. <td>' . core::convertDate($value["date"], FALSE) . '</td>
  571. <td>' . $value["titre"] . '</td>
  572. <td>' . $value["label"] . '</td>
  573. <td>' . $value["deadline"] . '</td>
  574. <td>' . $value["assign"] . '</td>
  575. <td style="text-align:right;">' . core::formatEuro($value["montant"]) . '</td>
  576. </tr>';
  577. }
  578. return $return;
  579. }
  580. }