138 lines
5.3 KiB
PHP
138 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* Calendar App
|
|
*
|
|
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
|
|
*
|
|
* @author Anna Larch <anna.larch@gmx.net>
|
|
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
* License as published by the Free Software Foundation; either
|
|
* version 3 of the License, or any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
namespace OCA\Gestion\Service;
|
|
|
|
use OCA\Gestion\Db\Bdd;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class GestionService {
|
|
/** @var Bdd */
|
|
private $gestionBdd;
|
|
|
|
/** @var LoggerInterface */
|
|
private $logger;
|
|
|
|
public function __construct(
|
|
Bdd $gestionBdd,
|
|
LoggerInterface $logger) {
|
|
$this->logger = $logger;
|
|
$this->gestionBdd = $gestionBdd;
|
|
}
|
|
|
|
private function GetValueFromKeyInVCalendarString(string $key, string $vCalendarString): string
|
|
{
|
|
$value = "";
|
|
preg_match("/$key:(.*)\r\n/", $vCalendarString, $matches);
|
|
if (isset($matches[1])) {
|
|
$value = trim($matches[1]);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string
|
|
{
|
|
$summaryValue = "Nom du défunt";
|
|
$value = $this->GetValueFromKeyInVCalendarString("SUMMARY", $vCalendarString);
|
|
if($value !== ""){
|
|
$summaryValue = trim($value);
|
|
}
|
|
return $summaryValue;
|
|
}
|
|
|
|
private function GetOrCreateThanatoFromVCalendarString(string $vCalendarString)
|
|
{
|
|
$thanatoName = "Nom du défunt";
|
|
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
|
|
if(count($thanatoNames) > 0){
|
|
$thanatoName = $thanatoNames[0];
|
|
}
|
|
$thanatoId = $this->gestionBdd->getOrCreateThanatoIdByLastName($thanatoName);
|
|
return $thanatoId;
|
|
}
|
|
private function GetAttendeesNameFromVCalendarString(string $vCalendarString): array
|
|
{
|
|
$names = [];
|
|
preg_match_all('/CN=([^;]+)/', $vCalendarString, $matches);
|
|
if (isset($matches[1])) {
|
|
$names = $matches[1];
|
|
}
|
|
return $names;
|
|
}
|
|
|
|
private function IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$defuntName){
|
|
$defuntId = $this->gestionBdd->getLastDefuntIdByName($defuntName);
|
|
$devisId = $this->gestionBdd->getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId);
|
|
return $devisId != null;
|
|
}
|
|
|
|
public function HandleCreatedCalendarObject(string $vCalendarString){
|
|
$calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
|
$clientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
|
$locationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
|
$thanatoId = $this->GetOrCreateThanatoFromVCalendarString($vCalendarString);
|
|
$devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary);
|
|
if($devisAlreadyCreated){
|
|
return;
|
|
}
|
|
$defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary);
|
|
$devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId);
|
|
$articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString);
|
|
if(!empty($articlesValue)){
|
|
$articlesId = $this->gestionBdd->getArticlesIdFromArticlesNameArray($articlesValue);
|
|
$this->gestionBdd->insertDevisArticleFromDevisIdAndArticlesIdArray($devisId, $articlesId);
|
|
}
|
|
$this->gestionBdd->createDevisTrajetFromVCalendar($devisId);
|
|
}
|
|
|
|
private function GetClientIdFromVCalendarString(string $vCalendarString){
|
|
$clientValue = $this->GetValueFromKeyInVCalendarString("CLIENT", $vCalendarString);
|
|
$clientId = $this->gestionBdd->getLastClientIdByName(name: $clientValue) ?? 0;
|
|
return $clientId;
|
|
}
|
|
|
|
private function GetLocationIdFromVCalendarString(string $vCalendarString){
|
|
$locationValue = $this->GetValueFromKeyInVCalendarString("LOCATION", $vCalendarString);
|
|
$locationId = $this->gestionBdd->getLastLocationIdByName($locationValue)?? 0;
|
|
return $locationId;
|
|
}
|
|
|
|
private function GetThanatoIdFromVCalendarString(string $vCalendarString){
|
|
$thanatoValue = $this->GetValueFromKeyInVCalendarString("EMBALMER", $vCalendarString);
|
|
$thanatoId = $this->gestionBdd->getLastThanatoIdByName($thanatoValue) ?? 0;
|
|
return $thanatoId;
|
|
}
|
|
|
|
private function GetArticlesNameFromVCalendarString(string $vCalendarString): array {
|
|
$devisArticleValue = $this->GetValueFromKeyInVCalendarString("DESCRIPTION", $vCalendarString);
|
|
$this->logger->debug('LIST OF ARTICLES',["" => $devisArticleValue]);
|
|
$articles = explode('\;', $devisArticleValue);
|
|
$mapped = array_map('trim', $articles);
|
|
$this->logger->debug('LIST OF ARTICLES',$mapped);
|
|
return $mapped;
|
|
}
|
|
}
|