Thanasoft-H2F/gestion/lib/Controller/ProviderController.php
Narindra ezway 124a5ac324 wip:mail-pjs
2025-03-19 18:21:59 +03:00

226 lines
5.2 KiB
PHP

<?php
namespace OCA\Gestion\Controller;
use Exception;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\IGroupManager;
defined("TAB1") or define("TAB1", "\t");
use OCP\IURLGenerator;
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
{
private $idNextcloud;
private $urlGenerator;
private $mailer;
private $config;
/** @var IRootStorage */
private $storage;
private $user;
private $groups = [];
/** @var \OCA\Gestion\Service\NavigationService */
private $navigationService;
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,
GestionService $gestionService
) {
parent::__construct($AppName, $request);
$this->idNextcloud = $UserId;
$this->myDb = $myDb;
$this->urlGenerator = $urlGenerator;
$this->mailer = $mailer;
$this->config = $config;
$this->navigationService = $navigationService;
$this->configurationService = $configurationService;
$this->logger = $logger;
$this->providerService = $providerService;
$this->providerStatisticService = $providerStatisticService;
$this->gestionService = $gestionService;
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();
}
}
try {
$this->storage = $rootFolder->getUserFolder($this->idNextcloud);
} catch (\OC\User\NoUserException $e) {
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
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()
{
$providers = $this->providerService->getProvidersAsArray();
return json_encode($providers);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function createDefaultProvider()
{
try {
$this->providerService->createDefaultProvider($this->idNextcloud);
return true;
} catch (Exception $e) {
return null;
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function exportProvidersStatistic(array $providerIds, $year)
{
try {
$filenames = $this->providerStatisticService->exportProvidersStatisticByYear($providerIds, $year, $this->idNextcloud);
return $filenames;
} 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;
}
}