file($filePatch); // Vérification si le fichier est de type XML if ($mime_type === 'application/xml' || $mime_type === 'text/xml') { // Chargement du contenu XML $xml_content = file_get_contents($filePatch); xml::print($xml_content); } else { // Si ce n'est pas un fichier XML, comportement normal pour servir le fichier header('Content-Type: ' . $mime_type); header('Content-Length: ' . filesize($filePatch)); readfile($filePatch); } } else { echo "Le fichier n'a pas été trouvé ou n'est pas lisible."; } } static public function getFile(string $_id) { $filePatch = file::download($_id, DIR_DATAS_DOCS); if (file_exists($filePatch) && is_readable($filePatch)) { $file_info = new finfo(FILEINFO_MIME_TYPE); $mime_type = $file_info->file($filePatch); header('Content-Type: ' . $mime_type); header('Content-Length: ' . filesize($filePatch)); readfile($filePatch); } else { echo "Le fichier n'a pas été trouvé ou n'est pas lisible."; } } static public function get(float $_id){ db::query("SELECT " . "" . DB_T_DOCUMENTS . ".id, " . "" . DB_T_DOCUMENTS . ".id_type, " . "" . DB_T_DOCUMENTS . ".titre, " . "" . DB_T_DOCUMENTS . ".date, " . "" . DB_T_DOCUMENTS . ".deadline, " . "" . DB_T_DOCUMENTS . ".description, " . "" . DB_T_DOCUMENTS . ".montant, " . "" . DB_T_DOCUMENTS . ".id_client, " . "" . DB_T_DOCUMENTS . ".id_user_done, " . "" . DB_T_DOCUMENTS . ".date_done, " . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS doneUser " . "FROM " . DB_T_DOCUMENTS . " " . "LEFT JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_DOCUMENTS . ".id_user_done " . "WHERE " . DB_T_DOCUMENTS . ".id = :id"); db::bind(':id', $_id); $document = db::single(); if(empty($document)){ $document = []; } $document["tagsSupplier"] = self::getTags($_id, 2); $document["tagsUser"] = self::getTags($_id, 1); $files = self::getFiles($_id); return array("document" => $document, "files" => $files); } static public function getTags(float $_idDocument, float $_idTypeTags){ db::query("SELECT " . "" . DB_T_TAGS . ".label " . "FROM " . DB_T_DOCUMENT_TAGS . " " . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags " . "WHERE " . DB_T_DOCUMENT_TAGS . ".id_documents = :idDocument AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = :idTypeTags " . "ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer ASC"); db::bind(':idDocument', $_idDocument); db::bind(':idTypeTags', $_idTypeTags); $tmp = db::resultset(); if(isset($tmp[0])){ $return = NULL; foreach ($tmp as $value) { $return .= $value["label"].","; } $return = substr($return, 0, -1); return $return; } else { return NULL; } } static public function getFiles(float $_idDocument){ db::query("SELECT " . "" . DB_T_FILES . ".id, " . "" . DB_T_FILES . ".name, " . "" . DB_T_FILES . ".size, " . "" . DB_T_FILES . ".creer, " . "" . DB_T_FILES . ".id_user, " . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS user, " . "" . DB_T_DOCUMENT_FILES . ".principal " . "FROM " . DB_T_DOCUMENT_FILES . " " . "INNER JOIN " . DB_T_FILES . " ON " . DB_T_FILES . ".id = " . DB_T_DOCUMENT_FILES . ".id_files " . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_FILES . ".id_user " . "WHERE " . DB_T_DOCUMENT_FILES . ".id_documents = :id " . "ORDER BY " . DB_T_FILES . ".creer"); db::bind(':id', $_idDocument); $tmp = db::resultset(); if(isset($tmp[0])){ foreach ($tmp as $file) { if($file["principal"] == TRUE){ $return["principal"] = $file; } else { $return[] = $file; } } if(empty($return["principal"])){ $return["principal"] = $return[0]; self::principalFile($_idDocument, $return["principal"]["id"]); unset($return[0]); } return $return; } else { return NULL; } } static public function principalFile(float $_idDocument, string $_idFile){ db::query("UPDATE " . DB_T_DOCUMENT_FILES . " SET principal = :principal WHERE id_documents = :id_documents"); db::bind(':principal', 0); db::bind(':id_documents', $_idDocument); db::execute(); db::query("UPDATE " . DB_T_DOCUMENT_FILES . " SET principal = :principal WHERE id_documents = :id_documents AND id_files = :id_files"); db::bind(':principal', 1); db::bind(':id_documents', $_idDocument); db::bind(':id_files', $_idFile); db::execute(); } static public function getAssign(float $_id = NULL){ $idUser = is_null($_id) ? session::getId() : $_id; $tags = user::getIdTags($idUser); $where = NULL; foreach ($tags AS $key => $value) { if($key == 0){ $where = " WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND (" . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . ""; } else { $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . ""; } } $where .= ")"; db::query("SELECT " . DB_T_DOCUMENTS . ".id, " . DB_T_DOCUMENTS . ".titre, " . DB_T_DOCUMENTS . ".date, " . DB_T_DOCUMENTS . ".deadline, " . DB_T_DOCUMENTS . ".description, " . DB_T_DOCUMENTS . ".montant, ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ') FROM " . DB_T_DOCUMENT_TAGS . " INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 2 ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS tags, ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ') FROM " . DB_T_DOCUMENT_TAGS . " INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 1 ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS assign, " . DB_T_TYPE_DOCUMENT . ".label FROM " . DB_T_DOCUMENT_TAGS . " INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents INNER JOIN " . DB_T_TYPE_DOCUMENT . " ON " . DB_T_TYPE_DOCUMENT . ".id = " . DB_T_DOCUMENTS . ".id_type " . $where . " GROUP BY documents.id"); return db::resultset(); } static public function printAttachement(array $_attachs){ $principal = $_attachs["principal"]; echo '
    '; echo '
  1. '.$principal["name"].' ('.core::convertBytes($principal["size"]).')
    Chargé le '.core::convertDate($principal["creer"]).' par '.$principal["user"].'
  2. '; foreach ($_attachs as $key => $attach) { if($key != "principal"){ echo '
  3. '.$attach["name"].' ('.core::convertBytes($attach["size"]).')
    Chargé le '.core::convertDate($attach["creer"]).' par '.$attach["user"].'
    '; if (access::ifAccesss("add-document")) { echo ' '; } echo '
  4. '; } } echo '

'; } static public function myAssign(?array $_tags = NULL){ if($_tags == NULL){ return NULL; } else { $where = NULL; foreach ($_tags AS $key => $value) { if($key == 0){ $where = "WHERE " . DB_T_DOCUMENTS . ".id_user_done IS NULL AND (" . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . ""; } else { $where .= " OR " . DB_T_DOCUMENT_TAGS . ".id_tags = " . $value . ""; } } $where .= ")"; } db::query("SELECT " . "COUNT(" . DB_T_DOCUMENT_TAGS . ".id_tags) AS nb " . "FROM " . DB_T_DOCUMENT_TAGS . " " . "INNER JOIN " . DB_T_DOCUMENTS . " ON " . DB_T_DOCUMENTS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_documents " . $where); return db::single()["nb"]; } static public function badgeAlert(){ $return = self::myAssign(user::getIdTags(session::getId())); return $return > 0 ? $return : NULL; } static public function assignMailDocument(){ db::query("SELECT " . "" . DB_T_USER . ".id, " . "" . DB_T_USER . ".email, " . "CONCAT(" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS name " . "FROM " . DB_T_USER_TAGS . " " . "INNER JOIN " . DB_T_USER . " ON " . DB_T_USER . ".id = " . DB_T_USER_TAGS . ".id_user " . "WHERE (" . DB_T_USER_TAGS . ".id_tags = 1 OR " . DB_T_USER_TAGS . ".id_tags = 2) AND " . DB_T_USER . ".deleted = 0 " . "GROUP BY " . DB_T_USER . ".id"); return db::resultset(); } public static function sendEmailCronAssign(array $_data) { $list = self::getAssign($_data["id"]); $nb = count($list); if ($nb > 0) { $titre = $nb > 1 ? $nb . " documents en attentes de validation" : "Un document en attente de validation"; $message = "Cet email est un récapitulatif des documents qui vous ont été assignés sur le CMS du CSE Invent.
A ce jour "; $message .= $nb > 1 ? $nb . " documents sont en attentes de validation." : "un seul document est en attente de validation."; $message .= "
Ce bilan sera mis à jour une fois par semaine et sera envoyé à l'ensemble des personnes assignées à ces documents."; $tmp = [ "name" => $_data["name"], "subject" => $titre, "message" => $message, "table" => self::getMailArray($list) ]; $data = [ "to" => $_data["email"], "name" => $_data["name"], "subject" => $titre, "template" => self::templateMail($tmp) ]; try { email::send($data); historique::recRef("script"); historique::add( array( "idType" => historique::getIdRef("CRON"), "idUser" => NULL, "idPage" => historique::getIdRef("script"), "log" => "Email d'assignation envoyé à " . $data["name"] ) ); } catch (\Throwable $th) { debug::log($th); } } } public static function templateMail(array $_data) { $logo_url = empty($_data["logo_url"]) ? "https://" . DOMAIN_CMS . "/img/logo.png" : $_data["logo_url"]; $date = empty($_data["date"]) ? core::printDateTxt() : $_data["date"]; $name = empty($_data["name"]) ? NULL : $_data["name"]; $subject = empty($_data["subject"]) ? NULL : $_data["subject"]; $message = empty($_data["message"]) ? NULL : $_data["message"]; $cms_url = empty($_data["cms_url"]) ? "https://" . DOMAIN_CMS : $_data["cms_url"]; $table = empty($_data["table"]) ? NULL : $_data["table"]; $template = email::loadTemplate('cms.documents.html'); if ($template === false) { echo "Impossible de lire le template d'email."; return false; } // Remplacer les variables dans le template $template = str_replace([ '{{logo_url}}', '{{date}}', '{{name}}', '{{subject}}', '{{message}}', '{{cms_url}}', '{{table}}' ], [ $logo_url, $date, $name, $subject, $message, $cms_url, $table ], $template); // Si debug if(debug::isFile("email")){ debug::log($template); } return $template; } private static function getMailArray(array $_array){ $return = NULL; foreach ($_array as $value) { $return .= ' ' . core::convertDate($value["date"], FALSE) . ' ' . $value["titre"] . ' ' . $value["label"] . ' ' . $value["deadline"] . ' ' . $value["assign"] . ' ' . core::formatEuro($value["montant"]) . ' '; } return $return; } }