JFIF $ d d 7 * Z qn )ZP 0 !1 A "#a$2Qr D8 a Ri [f \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H ~ , l s / 9 ć: [=/#f ; ) x { ٛE Q )~ = 𘙲r*2~ a _V = ' ku m F D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq 1dđ #̩ ͑ 0 ; H ] u 搂 @:~ ו K L N s h } O I R* 8 :2 ! lD JV o ( 3 = M ( z Ȱ+i *N A r6K n S l ) ! J J Ӂ* % ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c P A\IL Nj! J ۱+ O\ ͔ ] ޒ S J ȧ c%A N ol ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0 'G=i 2N +0 e 2 ]] 9 VT P O 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P < 0 x < !1AQ "Raq 02Br #S CTb ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV YT D I 6 2 '8Y2 7'\T P.6 d &˦ @V q i |8 -O Ε ] ʔ U= T L 8= ;6c | !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U#6w + +s &r+n ڐ { @ 29gL u " T Ù M=6 (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 ! OS r D 5 7 % | ?x> L | / nD 6?/8 w # [ ) L 7 +6〼T A T g ! %5 Mm Z /c -{1_Je "| ^$ 'O & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@ u6 BԒ i YN * f 0 _ GhKW u lT <nj- 0{ M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q $ H8o Q˼EV ~ k 0 h' L Pp M -M" m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @~p # !1QAa q ? AL0L x LU 4 d ; x " 16ad0 7.\ߋ 0 = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X &Q З non]я u ג./ ; gZ$ M QР 2M ]h ni 0 P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _ 1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3 6 #Ik+ v { v i ( ވ ? a e) X xy/*@Q Ǽ Ծ ) $ IWO l c G; !i k{ gOŀ L ('x T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ) X 5 c QɎMy W ? Q *!R > 6=7)Xj 5`J ] e 8%t ! + ' ! 1Q 5 !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $ v , ,m 㵜5u bV =sY+ @d { N ! dn O < .- B; _w J t 6;Q Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш e黅X ۢ F :)[ F G X ƹ / w _c MeD ,ʡc c . W D tA $ j@ : ) - #u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P 6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L ! ~ y5 g9T̅J kbM' +s :S +B) v @ M j e C f j E 0 Y\Qn z G 1 д~ W o{T 9?` Rm yhsy3 !HAD ] m c1 ~ 2 L S u7x T ;j$ `} 4-> L# vzŏ I L S ֭ T { r jGK C ; b pU= - ` Bs K. S F w4 M q] Z dH S 0 ) tLg
<?php
// controllers/ProjetsController.php
require_once __DIR__ . '/../models/Projet.php';
require_once __DIR__ . '/../models/ProjetCompteRendu.php';
require_once __DIR__ . '/../models/ProjetDocumentLink.php';
require_once __DIR__ . '/../models/Client.php';
require_once __DIR__ . '/../models/Database.php';
require_once __DIR__ . '/../models/Devis.php';
require_once __DIR__ . '/../models/Facture.php';
require_once __DIR__ . '/../models/RdvModel.php';
require_once __DIR__ . '/../models/Email.php';
require_once __DIR__ . '/../models/EmailAttachment.php';
require_once __DIR__ . '/../models/ReglagesEntreprise.php';
require_once __DIR__ . '/../lib/PdfBootstrap.php';
class ProjetsController {
private $projetModel;
private $projetCompteRenduModel;
private $projectLinkModel;
private $clientModel;
private $reglagesEntrepriseModel;
public function __construct() {
$this->projetModel = new Projet();
$this->projetCompteRenduModel = new ProjetCompteRendu();
$this->projectLinkModel = new ProjetDocumentLink();
$this->clientModel = new Client();
$this->reglagesEntrepriseModel = new ReglagesEntreprise(Database::getInstance()->getConnection());
}
public function handle() {
$action = $_GET['action'] ?? '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($action === 'add' || $action === 'save') {
$this->saveFromPost();
return;
}
if ($action === 'save-report') {
$this->saveCompteRendu();
return;
}
}
if ($action === 'view' && isset($_GET['id'])) {
$this->view((int) $_GET['id']);
return;
}
if ($action === 'edit' && isset($_GET['id'])) {
$this->edit((int) $_GET['id']);
return;
}
if ($action === 'delete' && isset($_GET['id'])) {
$this->delete((int) $_GET['id']);
return;
}
if ($action === 'detach-document' && isset($_GET['id'])) {
$this->detachDocument((int) $_GET['id']);
return;
}
$this->list();
}
private function list() {
$projets = $this->projetModel->getAll();
$clients = $this->clientModel->getAll();
require __DIR__ . '/../views/projets.php';
}
private function view(int $id): void {
$projet = $this->projetModel->getById($id);
if (!$projet) {
$this->redirect($this->buildUrl());
}
$comptesRendus = $this->projetCompteRenduModel->getByProjetId($id);
$projectDocuments = $this->getProjectDocuments($id);
$infosEntreprise = $this->reglagesEntrepriseModel->get();
require __DIR__ . '/../views/projet_view.php';
}
private function edit(int $id): void {
$projet = $this->projetModel->getById($id);
if (!$projet) {
$this->redirect($this->buildUrl());
}
$clients = $this->clientModel->getAll();
$comptesRendus = $this->projetCompteRenduModel->getByProjetId($id);
require __DIR__ . '/../views/projet_edit.php';
}
private function saveFromPost() {
$id = isset($_POST['projet_id']) && $_POST['projet_id'] !== '' ? (int) $_POST['projet_id'] : null;
$redirectTo = trim((string) ($_POST['redirect_to'] ?? 'list'));
$existingProject = $id ? $this->projetModel->getById($id) : null;
$data = [
'devis_id' => $existingProject['devis_id'] ?? null,
'client_id' => isset($_POST['client_id']) && $_POST['client_id'] !== '' ? (int) $_POST['client_id'] : null,
'titre' => trim((string) ($_POST['titre'] ?? '')),
'description' => trim((string) ($_POST['description'] ?? '')),
'date_projet' => trim((string) ($_POST['date_projet'] ?? date('Y-m-d'))),
'montant' => (float) ($_POST['montant'] ?? 0),
'statut' => trim((string) ($_POST['statut'] ?? 'Proposition')),
];
if ($data['titre'] === '') {
if ($id && $redirectTo === 'edit') {
$this->redirect($this->buildUrl('edit', ['id' => $id, 'missing_title' => 1]));
}
$this->list();
return;
}
$targetId = $id;
if ($id) {
$this->projetModel->update($id, $data);
} else {
$targetId = $this->projetModel->create($data);
}
if ($redirectTo === 'edit' && $targetId) {
$this->redirect($this->buildUrl('edit', ['id' => $targetId, 'saved' => 1]));
}
$this->redirect($this->buildUrl());
}
private function saveCompteRendu(): void {
$projetId = isset($_POST['projet_id']) ? (int) $_POST['projet_id'] : 0;
$reportId = isset($_POST['report_id']) ? (int) $_POST['report_id'] : 0;
$contenu = trim((string) ($_POST['contenu'] ?? ''));
if ($projetId <= 0) {
$this->redirect($this->buildUrl());
}
if (!$this->hasRichTextContent($contenu)) {
$this->redirect($this->buildUrl('edit', ['id' => $projetId, 'empty_report' => 1]));
}
if ($reportId > 0) {
$existing = $this->projetCompteRenduModel->getById($reportId);
if (!$existing || (int) $existing['projet_id'] !== $projetId) {
$this->redirect($this->buildUrl('edit', ['id' => $projetId, 'report_missing' => 1]));
}
$this->projetCompteRenduModel->update($reportId, $projetId, $contenu);
$this->redirect($this->buildUrl('edit', ['id' => $projetId, 'report_updated' => 1]));
}
$this->projetCompteRenduModel->create($projetId, $contenu);
$this->redirect($this->buildUrl('edit', ['id' => $projetId, 'report_saved' => 1]));
}
private function delete(int $id): void {
if ($id > 0) {
$this->projectLinkModel->deleteByProjectId($id);
$this->projetCompteRenduModel->deleteByProjetId($id);
$this->projetModel->delete($id);
}
$this->redirect($this->buildUrl());
}
private function getProjectDocuments(int $projectId): array {
$links = $this->projectLinkModel->getByProjectId($projectId);
if (!$links) {
return [];
}
$devisModel = new Devis();
$factureModel = new Facture();
$rdvModel = new RdvModel();
$emailModel = new Email();
$emailAttachmentModel = new EmailAttachment();
$documents = [];
foreach ($links as $link) {
$entityType = (string) ($link['entity_type'] ?? '');
$entityId = (int) ($link['entity_id'] ?? 0);
if ($entityId <= 0) {
continue;
}
if ($entityType === 'devis') {
$devis = $devisModel->getById($entityId);
if (!$devis) {
continue;
}
$documents[] = [
'type' => 'devis',
'entity_type' => 'devis',
'entity_id' => $entityId,
'title' => trim((string) ($devis['numero'] ?? '')) !== '' ? 'Devis ' . (string) $devis['numero'] : 'Devis #' . $entityId,
'meta' => trim((string) ($devis['date_devis'] ?? '')) !== '' ? 'Date : ' . date('d/m/Y', strtotime((string) $devis['date_devis'])) : '',
'status' => (string) ($devis['statut'] ?? ''),
'view_url' => 'index.php?page=devis&action=apercu&id=' . $entityId,
'view_label' => 'Aperçu du devis',
'attachments' => [],
'added_at' => (string) ($link['created_at'] ?? ''),
];
continue;
}
if ($entityType === 'facture') {
$facture = $factureModel->getById($entityId);
if (!$facture) {
continue;
}
$documents[] = [
'type' => 'facture',
'entity_type' => 'facture',
'entity_id' => $entityId,
'title' => trim((string) ($facture['numero'] ?? '')) !== '' ? 'Facture ' . (string) $facture['numero'] : 'Facture #' . $entityId,
'meta' => trim((string) ($facture['date_facture'] ?? '')) !== '' ? 'Date : ' . date('d/m/Y', strtotime((string) $facture['date_facture'])) : '',
'status' => (string) ($facture['statut'] ?? ''),
'view_url' => 'index.php?page=factures&action=apercu&id=' . $entityId,
'view_label' => 'Aperçu de la facture',
'attachments' => [],
'added_at' => (string) ($link['created_at'] ?? ''),
];
continue;
}
if ($entityType === 'rdv') {
$rdv = $rdvModel->getRdvById($entityId);
if (!$rdv) {
continue;
}
$meta = [];
if (!empty($rdv['date'])) {
$meta[] = 'Date : ' . date('d/m/Y', strtotime((string) $rdv['date']));
}
if (!empty($rdv['heure'])) {
$meta[] = 'Heure : ' . substr((string) $rdv['heure'], 0, 5);
}
$documents[] = [
'type' => 'rdv',
'entity_type' => 'rdv',
'entity_id' => $entityId,
'title' => trim((string) ($rdv['titre'] ?? '')) !== '' ? (string) $rdv['titre'] : 'Rendez-vous #' . $entityId,
'meta' => implode(' · ', $meta),
'status' => (string) ($rdv['statut'] ?? ''),
'view_url' => 'index.php?page=calendar&open_rdv_id=' . $entityId,
'view_label' => 'Aperçu du rendez-vous',
'attachments' => [],
'added_at' => (string) ($link['created_at'] ?? ''),
];
continue;
}
if ($entityType === 'email') {
$email = $emailModel->getById($entityId);
if (!$email) {
continue;
}
$attachments = [];
foreach ($emailAttachmentModel->getByEmailId($entityId) as $attachment) {
$attachments[] = [
'label' => (string) ($attachment['filename'] ?? 'Pièce jointe'),
'url' => 'index.php?page=boite-mail&action=att&att_id=' . (int) ($attachment['id'] ?? 0),
];
}
$meta = [];
if (!empty($email['destinataire'])) {
$meta[] = 'À : ' . (string) $email['destinataire'];
}
if (!empty($email['date_message'])) {
$meta[] = 'Le ' . date('d/m/Y H:i', strtotime((string) $email['date_message']));
}
$documents[] = [
'type' => 'email',
'entity_type' => 'email',
'entity_id' => $entityId,
'title' => trim((string) ($email['sujet'] ?? '')) !== '' ? (string) $email['sujet'] : 'E-mail #' . $entityId,
'meta' => implode(' · ', $meta),
'status' => (string) ($email['statut'] ?? ''),
'view_url' => 'index.php?page=boite-mail&open_email_id=' . $entityId,
'view_label' => 'Aperçu de l\'e-mail',
'attachments' => $attachments,
'added_at' => (string) ($link['created_at'] ?? ''),
];
}
}
return $documents;
}
private function detachDocument(int $projectId): void {
$entityType = trim((string) ($_GET['entity_type'] ?? ''));
$entityId = isset($_GET['entity_id']) ? (int) $_GET['entity_id'] : 0;
if ($projectId <= 0 || $entityId <= 0 || $entityType === '') {
$this->redirect($this->buildUrl('view', ['id' => $projectId, 'detach_error' => 1]));
}
$link = $this->projectLinkModel->getForEntity($entityType, $entityId);
if (!$link || (int) ($link['projet_id'] ?? 0) !== $projectId) {
$this->redirect($this->buildUrl('view', ['id' => $projectId, 'detach_error' => 1]));
}
$this->projectLinkModel->deleteByEntity($entityType, $entityId);
$this->redirect($this->buildUrl('view', ['id' => $projectId, 'detached' => 1]));
}
private function hasRichTextContent(string $html): bool {
$decoded = html_entity_decode($html, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$text = trim(str_replace("\xC2\xA0", ' ', strip_tags($decoded)));
return $text !== '';
}
private function buildUrl(string $action = '', array $params = []): string {
$query = ['page' => 'projets'];
if ($action !== '') {
$query['action'] = $action;
}
foreach ($params as $key => $value) {
$query[$key] = $value;
}
return 'index.php?' . http_build_query($query);
}
private function redirect(string $url): void {
header('Location: ' . $url);
exit;
}
}