<?php
//----------------------------------------------------------------------
// src/Logging/DocumentLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Logging\Tools;
use App\Services\LogTools;
use App\Entity\Webapp\Document;
class DocumentLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(Document $document)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($document);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($document, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "document_add";
if ($document->getRfi() !== null)
{
$args['action'] = "rfi_add";
}
elseif ($document->getRfiGC() !== null)
{
$args['action'] = "rfi_gc_add";
}
elseif ($document->getAnomaly() !== null)
{
$args['action'] = "anomaly_add";
}
elseif ($document->getAnomalyGC() !== null)
{
$args['action'] = "anomaly_gc_add";
}
elseif ($document->getReport() !== null)
{
$args['action'] = "report_add";
}
elseif ($document->getKVisitReport() !== null)
{
$args['action'] = "kvisit_report_add";
}
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Document $document, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($document);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($document, $loggingData);
//----------------------------------------------------------------------
$action = "document_edit";
$args["action"] = $action;
foreach ($changes as $key => $change)
{
$before = $change[0];
$after = $change[1];
if ($key == "archived")
{
$newValue = $change[1];
if ($newValue)
{
// Document has been archived
$action = "document_archive";
}
else
{
// Archiving has been undone
$action = "document_archive_undo";
}
$args["action"] = $action;
$pendingLogArgs[] = $args;
continue;
}
if ($key == "visibleToClient")
{
$action = "document_edit_visible_to_client";
$args["action"] = $action;
$pendingLogArgs[] = $args;
continue;
}
if ($key == "ikeaSent")
{
$ikeaAction = "ikea_service_order_document_sent_to_ikea";
$args["action"] = $ikeaAction;
$args["new_value"] = $after;
$pendingLogArgs[] = $args;
$ikeaArgs = $this->tools->handleIkeaServiceOrder($after, $document, $ikeaAction, $info, $specialAuthor);
foreach ($ikeaArgs as $argsItem)
{
$pendingLogArgs[] = $argsItem;
}
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(Document $document)
{
return [];
}
private function initArgs(Document $document, $loggingData)
{
$receiver = $document->getReceiver();
$society = $document->getSociety();
$societyGroup = $society->getSocietyGroup();
$mission = $document->getMission();
$args = array(
"object_id" => $document->getId(),
"object_bundle" => "Webapp",
"object_entity" => "Document",
"object_display" => $document->getRef(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"object_mission_id" => $mission !== null ? $mission->getId() : null,
"object_mission_display" => $mission !== null ? $mission->getRef() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}
/*
// Handle sharing
// If devis.mission.author != devis.mission.owner log invoice creation to both society groups
$mission = $devis->getMission();
$double = false;
if ($mission !== null)
{
$societyGroupAuthor = $mission->getSocietyGroupAuthor();
$societyGroupOwner = $mission->getSocietyGroupOwner();
if (!$societyGroupAuthor->equals($societyGroupOwner))
{
$double = true;
}
}
if ($double)
{
// invoice.societyGroup is the invoice.mission.societyGroupOwner
// so we need to log to the invoice.mission.societyGroupAuthor
// and thus the society is the one of the mission
$argsBis = $args;
$argsBis["society_group"] = $societyGroupAuthor;
$argsBis["society"] = $mission->getSociety();
$pendingLogArgs[] = $argsBis;
}
*/