<?php
//----------------------------------------------------------------------
// src/Logging/NoteLog.php
// This is a special type of log since it handles
// both SHARING (double logging)
// and DELETION (using context logger)
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Platform\Note;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\LogTools;
class NoteLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools, DeletionContextLogger $contextLogger)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
$this->contextLogger = $contextLogger;
}
public function logCreation(Note $note)
{
// Get Note data considering sharing
$noteData = $this->getDataConsideringSharing($note);
if ($noteData === null) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($note);
$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($note, $loggingData, $noteData);
//----------------------------------------------------------------------
$action = "note_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
if ($noteData['double'])
{
$argsBis = $this->initArgs($note, $loggingData, $noteData);
$argsBis["society_group"] = $noteData['societyGroupAuthor'];
$argsBis["society"] = $noteData['societyAuthor'];
$argsBis["action"] = $action;
$pendingLogArgs[] = $argsBis;
}
return $pendingLogArgs;
}
public function logChanges(Note $note, $changes)
{
// Get Note data considering sharing
$noteData = $this->getDataConsideringSharing($note);
if ($noteData === null) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info noted in the object
$loggingData = $this->logTools->handleLoggingData($note);
$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($note, $loggingData, $noteData);
//----------------------------------------------------------------------
$action = "note_edit";
$double = $noteData['double'];
if ($double)
{
$argsBis = $this->initArgs($note, $loggingData, $noteData);
$argsBis["society_group"] = $noteData['societyGroupAuthor'];
$argsBis["society"] = $noteData['societyAuthor'];
}
$basicChanges = array(
"body",
"hidden",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
// Something to skip ?
if (false)
{
continue;
}
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
if ($double)
{
$argsBis["action"] = $args["action"];
}
$before = $change[0];
$after = $change[1];
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
if ($double)
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($argsBis, $before, $after);
}
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(Note $note)
{
// Get Note data considering sharing and context
$noteData = $this->getDataConsideringContext($note);
if ($noteData === null) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info noted in the object
$loggingData = $this->logTools->handleLoggingData($note);
$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($note, $loggingData, $noteData);
//----------------------------------------------------------------------
$action = "note_delete";
$args["action"] = $action;
$pendingLogArgs[] = $args;
if ($noteData['double'])
{
$argsBis = $this->initArgs($note, $loggingData, $noteData);
$argsBis["society_group"] = $noteData['societyGroupAuthor'];
$argsBis["society"] = $noteData['societyAuthor'];
$argsBis["action"] = $action;
$pendingLogArgs[] = $argsBis;
}
return $pendingLogArgs;
}
private function initArgs(Note $note, $loggingData, $noteData)
{
$receiver = $noteData['receiver'];
$society = $noteData['society'];
$societyGroup = $noteData['societyGroup'];
$mission = $noteData['mission'];
$args = array(
"object_id" => $note->getId(),
"object_bundle" => "Platform",
"object_entity" => "Note",
"object_display" => $note->display(),
"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;
}
private function getDataConsideringSharing(Note $note)
{
$mission = $note->getMission();
if ($mission === null)
{
// Simplest case
$receiver = $note->getClient();
if ($receiver === null)
{
return null;
}
return array(
'double' => false,
'receiver' => $receiver,
'mission' => null,
// Society is null if receiver is store
'society' => $receiver->getSociety(),
'societyGroup' => $receiver->getSocietyGroup(),
'societyAuthor' => null,
'societyGroupAuthor' => null,
);
}
// Handle sharing for Missions
// At this point we know that $mission is not null
$receiver = $mission->getReceiver();
if ($receiver === null)
{
return null;
}
$society = $receiver->getSociety();
$societyGroup = $receiver->getSocietyGroup();
// At this point we know that $mission and $receiver are not null
$double = false;
$societyGroupAuthor = $mission->getSocietyGroupAuthor();
$societyGroupOwner = $mission->getSocietyGroupOwner();
$societyAuthor = $mission->getSociety();
$societyOwner = $mission->getSocietyOwner();
if (!$societyGroupAuthor->equals($societyGroupOwner))
{
$double = true;
// Primary Log : Owner
// Secondary Log : Author
$society = $societyOwner;
$societyGroup = $societyGroupOwner;
}
return array(
'double' => true,
'receiver' => $receiver,
'mission' => $mission,
'society' => $society,
'societyGroup' => $societyGroup,
'societyAuthor' => $societyAuthor,
'societyGroupAuthor' => $societyGroupAuthor,
);
}
private function getDataConsideringContext(Note $note)
{
$cachedData = $this->contextLogger->getContext($note);
if ($cachedData === null || empty($cachedData))
{
return null;
}
// Check mission first
$mission = null;
if (array_key_exists('mission', $cachedData) && $cachedData['mission'] !== null)
{
$mission = $cachedData['mission'];
}
if ($mission === null)
{
// Simplest case
$receiver = null;
if (array_key_exists('client', $cachedData) && $cachedData['client'] !== null)
{
$receiver = $cachedData['client'];
}
if ($receiver === null)
{
return null;
}
return array(
'double' => false,
'receiver' => $receiver,
'mission' => null,
// Society is null if receiver is store
'society' => $receiver->getSociety(),
'societyGroup' => $receiver->getSocietyGroup(),
'societyAuthor' => null,
'societyGroupAuthor' => null,
);
}
// Handle sharing for Missions
// At this point we know that $mission is not null
$receiver = $mission->getReceiver();
if ($receiver === null)
{
return null;
}
$society = $receiver->getSociety();
$societyGroup = $receiver->getSocietyGroup();
// At this point we know that $mission and $receiver are not null
$double = false;
$societyGroupAuthor = $mission->getSocietyGroupAuthor();
$societyGroupOwner = $mission->getSocietyGroupOwner();
$societyAuthor = $mission->getSociety();
$societyOwner = $mission->getSocietyOwner();
if (!$societyGroupAuthor->equals($societyGroupOwner))
{
$double = true;
// Primary Log : Owner
// Secondary Log : Author
$society = $societyOwner;
$societyGroup = $societyGroupOwner;
}
return array(
'double' => $double,
'receiver' => $receiver,
'mission' => $mission,
'society' => $society,
'societyGroup' => $societyGroup,
'societyAuthor' => $societyAuthor,
'societyGroupAuthor' => $societyGroupAuthor,
);
}
}