document.class.php 27 KB

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