document.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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(debug::isFile("debug")) { 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(debug::isFile("debug")) { 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(debug::isFile("debug")) { 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(debug::isFile("debug")) { 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. // Vérification si le fichier est de type XML
  249. if ($mime_type === 'application/xml' || $mime_type === 'text/xml') {
  250. // Chargement du contenu XML
  251. $xml_content = file_get_contents($filePatch);
  252. xml::print($xml_content);
  253. } else {
  254. // Si ce n'est pas un fichier XML, comportement normal pour servir le fichier
  255. header('Content-Type: ' . $mime_type);
  256. header('Content-Length: ' . filesize($filePatch));
  257. readfile($filePatch);
  258. }
  259. } else {
  260. echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
  261. }
  262. }
  263. static public function getFile(string $_id) {
  264. $filePatch = file::download($_id, DIR_DATAS_DOCS);
  265. if (file_exists($filePatch) && is_readable($filePatch)) {
  266. $file_info = new finfo(FILEINFO_MIME_TYPE);
  267. $mime_type = $file_info->file($filePatch);
  268. header('Content-Type: ' . $mime_type);
  269. header('Content-Length: ' . filesize($filePatch));
  270. readfile($filePatch);
  271. } else {
  272. echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
  273. }
  274. }
  275. static public function get(float $_id){
  276. db::query("SELECT "
  277. . "" . DB_T_DOCUMENTS . ".id, "
  278. . "" . DB_T_DOCUMENTS . ".id_type, "
  279. . "" . DB_T_DOCUMENTS . ".titre, "
  280. . "" . DB_T_DOCUMENTS . ".date, "
  281. . "" . DB_T_DOCUMENTS . ".deadline, "
  282. . "" . DB_T_DOCUMENTS . ".description, "
  283. . "" . DB_T_DOCUMENTS . ".montant, "
  284. . "" . DB_T_DOCUMENTS . ".id_user_done, "
  285. . "" . DB_T_DOCUMENTS . ".date_done, "
  286. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS doneUser "
  287. . "FROM " . DB_T_DOCUMENTS . " "
  288. . "LEFT JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_DOCUMENTS . ".id_user_done "
  289. . "WHERE " . DB_T_DOCUMENTS . ".id = :id");
  290. db::bind(':id', $_id);
  291. $document = db::single();
  292. $document["tagsSupplier"] = self::getTags($_id, 2);
  293. $document["tagsUser"] = self::getTags($_id, 1);
  294. $files = self::getFiles($_id);
  295. return array("document" => $document, "files" => $files);
  296. }
  297. static public function getTags(float $_idDocument, float $_idTypeTags){
  298. db::query("SELECT "
  299. . "" . DB_T_TAGS . ".label "
  300. . "FROM " . DB_T_DOCUMENT_TAGS . " "
  301. . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags "
  302. . "WHERE " . DB_T_DOCUMENT_TAGS . ".id_documents = :idDocument AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = :idTypeTags "
  303. . "ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer ASC");
  304. db::bind(':idDocument', $_idDocument);
  305. db::bind(':idTypeTags', $_idTypeTags);
  306. $tmp = db::resultset();
  307. if(isset($tmp[0])){
  308. $return = NULL;
  309. foreach ($tmp as $value) {
  310. $return .= $value["label"].",";
  311. }
  312. $return = substr($return, 0, -1);
  313. return $return;
  314. } else {
  315. return NULL;
  316. }
  317. }
  318. static public function getFiles(float $_idDocument){
  319. db::query("SELECT "
  320. . "" . DB_T_FILES . ".id, "
  321. . "" . DB_T_FILES . ".name, "
  322. . "" . DB_T_FILES . ".size, "
  323. . "" . DB_T_FILES . ".creer, "
  324. . "" . DB_T_FILES . ".id_user, "
  325. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS user, "
  326. . "" . DB_T_DOCUMENT_FILES . ".principal "
  327. . "FROM " . DB_T_DOCUMENT_FILES . " "
  328. . "INNER JOIN " . DB_T_FILES . " ON " . DB_T_FILES . ".id = " . DB_T_DOCUMENT_FILES . ".id_files "
  329. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_FILES . ".id_user "
  330. . "WHERE " . DB_T_DOCUMENT_FILES . ".id_documents = :id "
  331. . "ORDER BY " . DB_T_FILES . ".creer");
  332. db::bind(':id', $_idDocument);
  333. $tmp = db::resultset();
  334. if(isset($tmp[0])){
  335. foreach ($tmp as $file) {
  336. if($file["principal"] == TRUE){
  337. $return["principal"] = $file;
  338. } else {
  339. $return[] = $file;
  340. }
  341. }
  342. if(empty($return["principal"])){
  343. $return["principal"] = $return[0];
  344. self::principalFile($_idDocument, $return["principal"]["id"]);
  345. unset($return[0]);
  346. }
  347. return $return;
  348. } else {
  349. return NULL;
  350. }
  351. }
  352. static public function principalFile(float $_idDocument, string $_idFile){
  353. db::query("UPDATE " . DB_T_DOCUMENT_FILES . " SET principal = :principal WHERE id_documents = :id_documents");
  354. db::bind(':principal', 0);
  355. db::bind(':id_documents', $_idDocument);
  356. db::execute();
  357. db::query("UPDATE " . DB_T_DOCUMENT_FILES . " SET principal = :principal WHERE id_documents = :id_documents AND id_files = :id_files");
  358. db::bind(':principal', 1);
  359. db::bind(':id_documents', $_idDocument);
  360. db::bind(':id_files', $_idFile);
  361. db::execute();
  362. }
  363. static public function getAssign(float $_id = NULL){
  364. $idUser = is_null($_id) ? session::getId() : $_id;
  365. $tags = user::getIdTags($idUser);
  366. $where = NULL;
  367. foreach ($tags AS $key => $value) {
  368. if($key == 0){
  369. $where = "WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND (" . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  370. } else {
  371. $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  372. }
  373. }
  374. $where .= ")";
  375. db::query("SELECT
  376. " . DB_T_DOCUMENTS . ".id,
  377. " . DB_T_DOCUMENTS . ".titre,
  378. " . DB_T_DOCUMENTS . ".date,
  379. " . DB_T_DOCUMENTS . ".deadline,
  380. " . DB_T_DOCUMENTS . ".description,
  381. " . DB_T_DOCUMENTS . ".montant,
  382. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  383. FROM " . DB_T_DOCUMENT_TAGS . "
  384. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  385. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 2
  386. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS tags,
  387. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  388. FROM " . DB_T_DOCUMENT_TAGS . "
  389. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  390. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 1
  391. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS assign,
  392. " . DB_T_TYPE_DOCUMENT . ".label
  393. FROM " . DB_T_DOCUMENT_TAGS . "
  394. INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents
  395. INNER JOIN " . DB_T_TYPE_DOCUMENT . " ON " . DB_T_TYPE_DOCUMENT . ".id = " . DB_T_DOCUMENTS . ".id_type
  396. " . $where);
  397. return db::resultset();
  398. }
  399. static public function printAttachement(array $_attachs){
  400. $principal = $_attachs["principal"];
  401. echo '<ol class="list-group list-group-numbered">';
  402. echo ' <li class="list-group-item d-flex justify-content-between align-items-start" id="attach-'.$principal["id"].'">
  403. <div class="ms-2 me-auto">
  404. <div><span class="fw-bold">'.$principal["name"].'</span> ('.core::convertBytes($principal["size"]).')</div>
  405. Chargé le '.core::convertDate($principal["creer"]).' par '.$principal["user"].'
  406. <div id="select-attach-'.$principal["id"].'" style="color:red;"></div>
  407. </div>
  408. <div class="btn-group">
  409. <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>
  410. <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>
  411. </div>
  412. </li>';
  413. foreach ($_attachs as $key => $attach) {
  414. if($key != "principal"){
  415. echo ' <li class="list-group-item d-flex justify-content-between align-items-start" id="attach-'.$attach["id"].'">
  416. <div class="ms-2 me-auto">
  417. <div><span class="fw-bold">'.$attach["name"].'</span> ('.core::convertBytes($attach["size"]).')</div>
  418. Chargé le '.core::convertDate($attach["creer"]).' par '.$attach["user"].'
  419. <div id="select-attach-'.$attach["id"].'"></div>
  420. </div><div class="btn-group">
  421. <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>
  422. <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>';
  423. if (access::ifAccesss("add-document")) {
  424. 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>
  425. <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>';
  426. }
  427. echo '</div>
  428. </li>';
  429. }
  430. }
  431. echo '</ol><br />';
  432. }
  433. static public function myAssign(?array $_tags = NULL){
  434. if($_tags == NULL){
  435. return NULL;
  436. } else {
  437. $where = NULL;
  438. foreach ($_tags AS $key => $value) {
  439. if($key == 0){
  440. $where = "WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND (" . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  441. } else {
  442. $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . "";
  443. }
  444. }
  445. $where .= ")";
  446. }
  447. db::query("SELECT "
  448. . "COUNT(" . DB_T_DOCUMENT_TAGS . ".id_tags) AS nb "
  449. . "FROM " . DB_T_DOCUMENT_TAGS . " "
  450. . "INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents "
  451. . $where);
  452. return db::single()["nb"];
  453. }
  454. static public function badgeAlert(){
  455. $return = self::myAssign(user::getIdTags(session::getId()));
  456. return $return > 0 ? $return : NULL;
  457. }
  458. static public function assignMailDocument(){
  459. db::query("SELECT "
  460. . "" . DB_T_USER . ".id, "
  461. . "" . DB_T_USER . ".email, "
  462. . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS name "
  463. . "FROM " . DB_T_USER_TAGS . " "
  464. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_USER_TAGS . ".id_user "
  465. . "WHERE (" . DB_T_USER_TAGS . ".id_tags = 1 OR " . DB_T_USER_TAGS . ".id_tags = 2) AND " . DB_T_USER . ".deleted = 0 "
  466. . "GROUP BY " . DB_T_USER . ".id");
  467. return db::resultset();
  468. }
  469. public static function sendEmailCronAssign(array $_data){
  470. $list = self::getAssign($_data["id"]);
  471. $nb = count($list);
  472. $titre = $nb > 1 ? $nb . " documents en attentes de validation" : "Un document en attente de validation";
  473. $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 ";
  474. $message .= $nb > 1 ? $nb . " documents sont en attentes de validation." : "un seul document est en attente de validation.";
  475. $message .= "<br />Ce bilan sera mis à jour une fois par semaine et sera envoyé à l'ensemble des personnes assignées à ces documents.";
  476. $tmp = [
  477. "name" => $_data["name"],
  478. "subject" => $titre,
  479. "message" => $message,
  480. "table" => self::getMailArray($list)
  481. ];
  482. $data = [
  483. "to" => $_data["email"],
  484. "name" => $_data["name"],
  485. "subject" => $titre,
  486. "template" => self::templateMail($tmp)
  487. ];
  488. try {
  489. email::send($data);
  490. historique::recRef("script");
  491. historique::add(array(
  492. "idType" => historique::getIdRef("CRON"),
  493. "idUser" => NULL,
  494. "idPage" => historique::getIdRef("script"),
  495. "log" => "Email d'assignation envoyé à " . $data["name"])
  496. );
  497. } catch (\Throwable $th) {
  498. debug::log($th);
  499. }
  500. }
  501. public static function templateMail(array $_data)
  502. {
  503. $logo_url = empty($_data["logo_url"]) ? "https://" . DOMAIN_CMS . "/img/logo.png" : $_data["logo_url"];
  504. $date = empty($_data["date"]) ? core::printDateTxt() : $_data["date"];
  505. $name = empty($_data["name"]) ? NULL : $_data["name"];
  506. $subject = empty($_data["subject"]) ? NULL : $_data["subject"];
  507. $message = empty($_data["message"]) ? NULL : $_data["message"];
  508. $cms_url = empty($_data["cms_url"]) ? "https://" . DOMAIN_CMS : $_data["cms_url"];
  509. $table = empty($_data["table"]) ? NULL : $_data["table"];
  510. $template = email::loadTemplate('cms.documents.html');
  511. if ($template === false) {
  512. echo "Impossible de lire le template d'email.";
  513. return false;
  514. }
  515. // Remplacer les variables dans le template
  516. $template = str_replace([
  517. '{{logo_url}}',
  518. '{{date}}',
  519. '{{name}}',
  520. '{{subject}}',
  521. '{{message}}',
  522. '{{cms_url}}',
  523. '{{table}}'
  524. ], [
  525. $logo_url,
  526. $date,
  527. $name,
  528. $subject,
  529. $message,
  530. $cms_url,
  531. $table
  532. ], $template);
  533. // Si debug
  534. if(debug::isFile("email")){
  535. debug::log($template);
  536. }
  537. return $template;
  538. }
  539. private static function getMailArray(array $_array){
  540. $return = NULL;
  541. foreach ($_array as $value) {
  542. $return .= ' <tr>
  543. <td>' . core::convertDate($value["date"], FALSE) . '</td>
  544. <td>' . $value["titre"] . '</td>
  545. <td>' . $value["label"] . '</td>
  546. <td>' . $value["deadline"] . '</td>
  547. <td>' . $value["assign"] . '</td>
  548. <td style="text-align:right;">' . core::formatEuro($value["montant"]) . '</td>
  549. </tr>';
  550. }
  551. return $return;
  552. }
  553. }