$value) { $return[$key] = $value["label"]; } return $return; } public static function delete(float $_id) { try { db::query("DELETE FROM ". DB_T_DOCUMENT_TAGS ." WHERE id_documents = :id_documents"); db::bind(':id_documents', $_id); db::execute(); self::deleteFiles($_id); db::query("DELETE FROM " . DB_T_DOCUMENTS . " WHERE id = :id"); db::bind(':id', $_id); db::execute(); alert::recSuccess("Le document vient d'être supprimé"); return TRUE; } catch (Exception $ex) { alert::recError("Erreur à la suppression du document"); return FALSE; } } public static function lastAdd() { db::query("SELECT MAX(id) AS id FROM " . DB_T_DOCUMENTS); return db::single()["id"]; } private static function addFile(float $_idDocument, string $_idFile) { db::query("INSERT INTO " . DB_T_DOCUMENT_FILES . " (id_documents, id_files) VALUES (:id_documents, :id_files)"); db::bind(':id_documents', $_idDocument); db::bind(':id_files', $_idFile); try { db::execute(); return TRUE; } catch (Exception $ex) { return FALSE; } } private static function addTags(float $_idDocument, string $_tags = NULL, float $_type) { db::query("DELETE FROM " . DB_T_DOCUMENT_TAGS . " WHERE id_documents = :id_documents AND id_type_tags = :id_type_tags"); db::bind(':id_documents', $_idDocument); db::bind(':id_type_tags', $_type); db::execute(); if($_tags != NULL){ $tags = explode(",", $_tags); $sqlMaj = ""; foreach ($tags as $tag) { $sqlMaj .= " (:id_documents, ".$tag.", :id_type_tags),"; } $sqlMaj = substr($sqlMaj, 0, -1); db::query("INSERT INTO " . DB_T_DOCUMENT_TAGS . " (id_documents, id_tags, id_type_tags) VALUES" . $sqlMaj); db::bind(':id_documents', $_idDocument); db::bind(':id_type_tags', $_type); try { db::execute(); return TRUE; } catch (Exception $ex) { return FALSE; } } } public static function getOrphanTags(){ db::query("SELECT " . DB_T_TAGS . ".id FROM " . DB_T_TAGS . " LEFT JOIN " . DB_T_DOCUMENT_TAGS . " ON tags.id = " . DB_T_DOCUMENT_TAGS . ".id_tags WHERE " . DB_T_DOCUMENT_TAGS . ".id_tags IS NULL AND " . DB_T_TAGS . ".id_type = 2"); return db::resultset(); } public static function cleanOrphanTags(){ foreach (self::getOrphanTags() as $value) { db::query("DELETE FROM ". DB_T_TAGS ." WHERE id = :id"); db::bind(':id', $value["id"]); db::execute(); } } public static function add() { session::setTemp(core::getPost(), "document"); $file = core::getFiles("document-import"); $md5 = md5_file($file["tmp_name"]); if(file::findM5($md5) == TRUE){ alert::recError("Ce fichier a déjà été utilisé : " . $file["name"]); session::setTemp(core::getPost(), "document"); } else { 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)"); db::bind(':id_type', core::getPost("id_type")); db::bind(':titre', core::getPost("titre")); db::bind(':date', core::getPost("date")); db::bind(':deadline', core::getPost("deadline")); db::bind(':description', core::getPost("description")); db::bind(':montant', core::getPost("montant")); db::bind(':id_user', session::getId()); try { db::execute(); $lastId = db::lastInsertId(); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la fiche : " . core::getPost("titre")); if(core::isDebug()) { alert::recError("Stack : " . $ex); } return FALSE; } try { $idFile = self::uploadFile($file); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $idFile); if(core::isDebug()) { alert::recError("Stack : " . $ex); } return FALSE; } try { self::addFile($lastId, $idFile); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile); if(core::isDebug()) { alert::recError("Stack : " . $ex); } return FALSE; } try { $tagsUser = tags::textToId(core::getPost("tagsUser"), 1); self::addTags($lastId, $tagsUser, 1); $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2); self::addTags($lastId, $tagsSupplier, 2); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile); if(core::isDebug()) { alert::recError("Stack : " . $ex); } return FALSE; } return $lastId; } } public static function update() { if(core::ifFiles("attachement-document") == TRUE){ $file = core::getFiles("attachement-document"); $md5 = md5_file($file["tmp_name"]); } if(isset($md5) AND file::findM5($md5) == TRUE){ alert::recError("Le fichier \"" . $file["name"] . "\" a déjà été utilisé"); session::setTemp(core::getPost(), "document"); } else { if(isset($md5)){ try { $idFile = self::uploadFile($file); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la pièce jointe : " . $idFile); return FALSE; } try { self::addFile(core::getPost("id"), $idFile); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la liaison : " . $idFile); return FALSE; } } if(core::getPost("delete-attachement")){ foreach (core::getPost("delete-attachement") as $deleteAttach) { self::deleteFile($deleteAttach); } } try { $tagsUser = tags::textToId(core::getPost("tagsUser"), 1); self::addTags(core::getPost("id"), $tagsUser, 1); $tagsSupplier = tags::textToId(core::getPost("tagsSupplier"), 2); self::addTags(core::getPost("id"), $tagsSupplier, 2); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement de la liaison : " . core::getPost("id")); return FALSE; } if(core::ifPost("date_done") AND core::getPost("date_done") != ""){ $sql = ", id_user_done = :id_user_done, date_done = :date_done "; } else { $sql = ""; } db::query("UPDATE " . DB_T_DOCUMENTS . " SET " . "id_type = :id_type, " . "titre = :titre, " . "date = :date, " . "deadline = :deadline, " . "description = :description, " . "montant = :montant " . $sql . "WHERE id = :id"); db::bind(':id_type', core::getPost("id_type")); db::bind(':titre', core::getPost("titre")); db::bind(':date', core::getPost("date")); db::bind(':deadline', core::getPost("deadline")); db::bind(':description', core::getPost("description")); db::bind(':montant', core::getPost("montant")); db::bind(':id', core::getPost("id")); if(core::ifPost("date_done") AND core::getPost("date_done") == TRUE){ db::bind(':id_user_done', session::getId()); db::bind(':date_done', core::getPost("date_done")); } try { db::execute(); alert::recSuccess("Document mis à jour avec succès"); return TRUE; } catch (Exception $ex) { alert::recError("Erreur de mise à jour du document : " . $ex); return FALSE; } } } static public function printFile(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 getList(){ } 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_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(); $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 " . "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])){ return $tmp; } else { return NULL; } } static public function getAssignMe(){ $tags = user::getIdTags(session::getId()); $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 . ""; } } 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); return db::resultset(); } static public function printAttachement(array $_attachs){ echo '
    '; foreach ($_attachs as $key => $attach) { echo '
  1. '.$attach["name"].' ('.core::convertBytes($attach["size"]).')
    Chargé le '.core::convertDate($attach["creer"]).' par '.$attach["user"].'
    '; if($key == 0){ echo ' '; } else { echo '
    '; } echo '
  2. '; } 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 . ""; } } } 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 menu(){ $tags = user::getIdTags(session::getId()); $nb = document::myAssign($tags); if($tags != NULL){ $badge = ""; if($nb > 0){ $badge = '' . $nb . ''; } echo 'Vos assignations' . $badge . ''; } } static public function badge(){ $tags = user::getIdTags(session::getId()); $nb = document::myAssign($tags); if($tags != NULL){ if($nb > 0){ echo ''; } } } }