document.class.php 27 KB

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