wip:mail-pjs

This commit is contained in:
Narindra ezway 2025-03-19 18:21:59 +03:00
parent 2e177f815d
commit 124a5ac324
7 changed files with 265 additions and 78 deletions

View File

@ -22,7 +22,6 @@ use OCP\AppFramework\Db\DoesNotExistException;
class EmailBoxController extends Controller {
private ?string $currentUserId;
private IMailManager $mailManager;
private IUserPreferences $preferences;
@ -71,7 +70,7 @@ class EmailBoxController extends Controller {
*/
public function loadEmailboxMessage(): JSONResponse {
//TODO
$mailboxId = 1;
$mailboxId = 9;
$lastMessageTimestamp = null;
$init = false;
@ -99,7 +98,6 @@ class EmailBoxController extends Controller {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$order = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? 'DESC': 'ASC';
return new JSONResponse(
$this->mailSearch->findMessages(

View File

@ -100,11 +100,10 @@
Revenir dans la listes des emails
</NcActionButton>
<div class="image-list-attachement " v-if="!loadingDataEmailBox && mailAttachments.length > 0" v-for="attachment in mailAttachments">
<div class="image-item-attachement " @click="pickEmailAttachement(attachment)">
<div v-if="attachment.isImage" class="image-item-attachement " @click="pickEmailAttachement(attachment)">
<img :src="attachment.downloadUrl" :alt="attachment.fileName ">
<label>{{ attachment.fileName }}</label>
</div>
</div>
<template v-if="!mailAttachments.length && !loadingDataEmailBox" >
<p> Pas d'Attachements</p>

View File

@ -205,6 +205,8 @@ return [
['name' => 'page#getUserConnectedGroups', 'url' => '/getUserConnectedGroups', 'verb' => 'GET'],
['name' => 'provider#moveAttachmentFile', 'url' => '/test-move', 'verb' => 'GET'],
['name' => 'provider#sendEmailAttachment', 'url' => '/test-send-mail-attach', 'verb' => 'GET'],
]

View File

@ -1,29 +1,34 @@
<?php
namespace OCA\Gestion\Controller;
use Exception;
use OCA\Gestion\Service\ConfigurationService;
use OCA\Gestion\Service\NavigationService;
use OCA\Gestion\Service\Provider\ProviderService;
use OCA\Gestion\Service\Provider\Statistic\ProviderStatisticService;
use Psr\Log\LoggerInterface;
defined("TAB1") or define("TAB1", "\t");
use OCP\IGroupManager;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Files\IRootFolder;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Gestion\Db\Bdd;
use OCP\IGroupManager;
defined("TAB1") or define("TAB1", "\t");
use OCP\IURLGenerator;
use OCP\IConfig;
use OCA\Gestion\Db\Bdd;
use OC\Files\Filesystem;
use OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;
use OCP\AppFramework\Controller;
use OCA\Gestion\Service\GestionService;
use OCP\AppFramework\Http\DataResponse;
use OCA\Gestion\Service\NavigationService;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\Gestion\Service\ConfigurationService;
use OCA\Gestion\Service\Provider\ProviderService;
use OCA\Gestion\Service\Provider\Statistic\ProviderStatisticService;
date_default_timezone_set('Europe/Paris');
class ProviderController extends Controller {
class ProviderController extends Controller
{
private $idNextcloud;
private $urlGenerator;
private $mailer;
@ -38,31 +43,36 @@ class ProviderController extends Controller {
/** @var \OCA\Gestion\Service\NavigationService */
private $navigationService;
private $providerService;
private $providerService;
private $providerStatisticService;
private $logger;
//Gestion service
private $gestionService;
/**
* Constructor
*/
public function __construct($AppName,
IRequest $request,
$UserId,
Bdd $myDb,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
IMailer $mailer,
Iconfig $config,
IUserSession $userSession,
IGroupManager $groupManager,
NavigationService $navigationService,
ConfigurationService $configurationService,
LoggerInterface $logger,
ProviderService $providerService,
ProviderStatisticService $providerStatisticService
){
public function __construct(
$AppName,
IRequest $request,
$UserId,
Bdd $myDb,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
IMailer $mailer,
Iconfig $config,
IUserSession $userSession,
IGroupManager $groupManager,
NavigationService $navigationService,
ConfigurationService $configurationService,
LoggerInterface $logger,
ProviderService $providerService,
ProviderStatisticService $providerStatisticService,
GestionService $gestionService
) {
parent::__construct($AppName, $request);
@ -74,41 +84,43 @@ class ProviderController extends Controller {
$this->navigationService = $navigationService;
$this->configurationService = $configurationService;
$this->logger = $logger;
$this->providerService = $providerService;
$this->providerService = $providerService;
$this->providerStatisticService = $providerStatisticService;
$this->gestionService = $gestionService;
if ($userSession->isLoggedIn()) {
$this->user = $userSession->getUser();
}
if ($userSession->isLoggedIn()) {
$this->user = $userSession->getUser();
}
if ($this->user != null) {
$groups = $groupManager->getUserGroups($this->user);
$this->groups = [];
foreach ($groups as $group) {
$this->groups[] = $group->getGID();
}
}
if ($this->user != null) {
$groups = $groupManager->getUserGroups($this->user);
$this->groups = [];
foreach ($groups as $group) {
$this->groups[] = $group->getGID();
}
}
try{
try {
$this->storage = $rootFolder->getUserFolder($this->idNextcloud);
}catch(\OC\User\NoUserException $e){
} catch (\OC\User\NoUserException $e) {
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function provider() {
*/
public function provider()
{
return new TemplateResponse('gestion', 'provider', array('groups' => $this->groups, 'user' => $this->user, 'path' => $this->idNextcloud, 'url' => $this->navigationService->getNavigationLink()));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getProviders() {
*/
public function getProviders()
{
$providers = $this->providerService->getProvidersAsArray();
return json_encode($providers);
}
@ -116,13 +128,13 @@ class ProviderController extends Controller {
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function createDefaultProvider() {
try{
*/
public function createDefaultProvider()
{
try {
$this->providerService->createDefaultProvider($this->idNextcloud);
return true;
}
catch(Exception $e){
} catch (Exception $e) {
return null;
}
}
@ -130,14 +142,84 @@ class ProviderController extends Controller {
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function exportProvidersStatistic(array $providerIds,$year){
try{
$filenames = $this->providerStatisticService->exportProvidersStatisticByYear($providerIds,$year,$this->idNextcloud);
*/
public function exportProvidersStatistic(array $providerIds, $year)
{
try {
$filenames = $this->providerStatisticService->exportProvidersStatisticByYear($providerIds, $year, $this->idNextcloud);
return $filenames;
}
catch(Exception $e){
} catch (Exception $e) {
return [];
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function moveAttachmentFile()
{
$tex = "BEGIN:VCALENDAR
PRODID:-//IDN nextcloud.com//Calendar app 4.7.16//EN
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
CREATED:20250319T070401Z
DTSTAMP:20250319T070538Z
LAST-MODIFIED:20250319T070538Z
SEQUENCE:2
UID:dc3ebd49-74c6-490a-afd2-2b61ef47c6f3
DTSTART;TZID=Africa/Nairobi:20250319T110001
DTEND;TZID=Africa/Nairobi:20250319T120001
STATUS:CONFIRMED
SUMMARY:test 5
LOCATION:2
DESCRIPTION:SOINS
TRANSP:TRANSPARENT
ATTACH;FMTTYPE=image/png;FILENAME=/sign.png;X-NC-FILE-ID=1426;X-NC-HAS-PREV
IEW=true:/f/1426
CLIENT:1
EMBALMER:
END:VEVENT
BEGIN:VTIMEZONE
TZID:Africa/Nairobi
BEGIN:STANDARD
TZOFFSETFROM:+0300
TZOFFSETTO:+0300
TZNAME:EAT
DTSTART:19700101T000000
END:STANDARD
END:VTIMEZONE
END:VCALENDAR";
$this->gestionService->moveCalendarAttachmentFile($tex, '/f');
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function sendEmailAttachment()
{
$this->storage->getFullPath("/");
$attachments = [
['file_id' => 1518, 'name' => 'Capture.JPG']
];
$to = "jonathan.goulet@nextcloud.com";
$subject = "Piece jointe";
$body = "<p>Bonjour.</p> <p>Vous trouverez en pièce jointe les fichiers votre défunt.</p>";
$message = $this->mailer->createMessage();
$message->setSubject($subject);
$message->setTo(recipients: [$to]);
foreach ($attachments as $attachment) {
$path = Filesystem::getPath($attachment['file_id']);
$content = $this->mailer->createAttachment(Filesystem::file_get_contents($path), $attachment['name'], 'image/png');
$message->attach($content);
}
$message->setHtmlBody($body);
$this->mailer->send($message);
return true;
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace OCA\Gestion\Helpers;
class FolderHelpers
{
public static function GetDefuntFolder(string $clientEntreprise,string $defuntNom){
$clientRacineFolder = 'CLIENTS/'.strtoupper($clientEntreprise).'/';
$defuntFolder = $clientRacineFolder.'DEFUNTS/'.strtoupper($defuntNom).'/';
return $defuntFolder;
}
}

View File

@ -61,4 +61,21 @@ class VCalendarHelpers
}
return $vCalendarData;
}
public static function hasAttachment(string $vCalendarString) : bool{
return str_contains($vCalendarString, 'ATTACH;FMTTYPE');
}
public static function extractAttachments(string $vCalendarString): array {
$attachments = [];
preg_match_all('/ATTACH;FMTTYPE=([^;]+);FILENAME=([^;]+);X-NC-FILE-ID=([^;]+);/', $vCalendarString, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$attachments[] = [
'type' =>$match[1], // Extrait 'image' de 'image/png'
'name' => trim($match[2], '/'),
'file_id' => (int) $match[3],
];
}
return $attachments;
}
}

View File

@ -26,18 +26,22 @@ declare(strict_types=1);
namespace OCA\Gestion\Service;
use OCP\DB\Exception;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCA\Gestion\Db\Bdd;
use OC\Files\Filesystem;
use OCP\Files\IRootFolder;
use OCA\Gestion\Db\OrderBdd;
use Psr\Log\LoggerInterface;
use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Constants\DevisMentionConstant;
use OCA\Gestion\Helpers\FolderHelpers;
use OCA\Gestion\Helpers\VCalendarHelpers;
use OCA\Gestion\Constants\OrderStatusConstant;
use OCA\Gestion\Constants\ThanatoTypeConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Db\OrderBdd;
use OCA\Gestion\Service\Devis\Pdf\DevisPdfService;
use OCA\Gestion\Service\Order\OrderPdfService;
use OCP\DB\Exception;
use Psr\Log\LoggerInterface;
use OCA\Gestion\Helpers\VCalendarHelpers;
use OCP\IUserSession;
use OCA\Gestion\Constants\DevisMentionConstant;
use OCA\Gestion\Service\Devis\Pdf\DevisPdfService;
class GestionService {
/** @var Bdd */
@ -58,6 +62,11 @@ class GestionService {
private string $userConnectedUuid;
private $userConnectedStorage;
private $mailer;
public function __construct(
Bdd $gestionBdd,
OrderBdd $orderBdd,
@ -65,7 +74,10 @@ class GestionService {
OrderPdfService $orderPdfService,
DevisPdfService $devisPdfService,
TalkService $talkService,
IUserSession $userSession
IUserSession $userSession,
IRootFolder $rootFolder,
IMailer $mailer,
) {
$this->orderBdd = $orderBdd;
$this->logger = $logger;
@ -75,6 +87,9 @@ class GestionService {
$this->talkService = $talkService;
$this->userSession = $userSession;
$this->userConnectedUuid = $userSession->getUser()?->getUID() ?? BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD;
$this->userConnectedStorage = $rootFolder->getUserFolder($this->userConnectedUuid);
$this->mailer = $mailer;
}
private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string
@ -184,6 +199,8 @@ class GestionService {
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
$devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid);
if($devisAlreadyCreated){
return;
}
@ -210,6 +227,20 @@ class GestionService {
$devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devisId,$userName);
$this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName,$this->userConnectedUuid);
$this->devisPdfService->generateDevisPdfByDevisId($devisId,$this->userConnectedUuid);
//Move calendar attachment file to defunt folder
if (VCalendarHelpers::hasAttachment($vCalendarString)) {
$devis = $this->gestionBdd->getDevisByDevisId($devisId);
if($devis != null && $devis["client_entreprise"] != null){
$destinationFolderAttachment = FolderHelpers::GetDefuntFolder($devis["client_entreprise"],$devis["defunt_nom"]);
$attachments = VCalendarHelpers::extractAttachments($vCalendarString);
$this->moveCalendarAttachmentFile($attachments,$destinationFolderAttachment);
if ($thanatoIsSubcontractor) {
$this->sendEmailAttachment($devis["defunt_nom"],$attachments);
}
}
}
$this->gestionBdd->createDevisTrajetFromVCalendar($devisId,$userName);
}
catch(Exception $e){
@ -217,6 +248,14 @@ class GestionService {
}
}
public function moveCalendarAttachmentFile(array $attachments ,string $destinationFolder ){
$this->userConnectedStorage->getFullPath("/");
foreach ($attachments as $attachment) {
$path = Filesystem::getPath($attachment['file_id']);
Filesystem::copy($path, $destinationFolder . $attachment['name']);
}
}
private function GetThanatoNameFromVCalendarString($vCalendarString){
$thanatoName = null;
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
@ -329,12 +368,50 @@ class GestionService {
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
$devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devis['id'],$userName);
$this->talkService->sendDevisTalkNotifications($devisTalkMessage,$userName,$this->userConnectedUuid);
$this->devisPdfService->generateDevisPdfByDevisId($devis['id'],$this->userConnectedUuid);
if (VCalendarHelpers::hasAttachment($vCalendarString)) {
$thanato = $this->gestionBdd->getThanatoByThanatoId($devis['id_thanato']);
$thanatoIsSubcontractor = $thanato["fk_thanato_type_key"] === ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR;
$devis = $this->gestionBdd->getDevisByDevisId($devis['id']);
if($devis != null && $devis["client_entreprise"] != null){
$destinationFolderAttachment = FolderHelpers::GetDefuntFolder($devis["client_entreprise"],$devis["defunt_nom"]);
$attachments = VCalendarHelpers::extractAttachments($vCalendarString);
$this->moveCalendarAttachmentFile($attachments , $destinationFolderAttachment);
if ($thanatoIsSubcontractor) {
$this->sendEmailAttachment($devis["defunt_nom"],$attachments);
}
}
}
}
$this->devisPdfService->generateDevisPdfByDevisId($devis['id'],$this->userConnectedUuid);
return true;
}
catch(Exception $e){
$this->logger->debug("error creating devis");
}
}
public function sendEmailAttachment($defunt_nom ,$attachments = []){
$this->userConnectedStorage->getFullPath("/");
$to = "jonathan.goulet@nextcloud.com";
$subject = "Piece jointe";
$body = "<p>Bonjour.</p> <p>Vous trouverez en pièce jointe les documents concernant de « $defunt_nom ».</p>";
$message = $this->mailer->createMessage();
$message->setSubject($subject);
$message->setTo(recipients: [$to]);
foreach ($attachments as $attachment) {
$path = Filesystem::getPath($attachment['file_id']);
$content = $this->mailer->createAttachment( Filesystem::file_get_contents($path),$attachment['name'],'image/png');
$message->attach($content);
}
$message->setHtmlBody( $body);
$this->mailer->send($message);
}
}