document.class.php 28 KB

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