<?php
//----------------------------------------------------------------------
// src/Logging/MissionLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\Mission\Mission;
use App\Logging\Tools;
use App\Services\Location\AddressTools;
use App\Services\LogTools;
class MissionLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, AddressTools $addressTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->addressTools = $addressTools;
$this->tools = $tools;
}
public function logCreation(Mission $mission)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($mission);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
$receiver = $mission->getReceiver();
$args = array(
"action" => "mission_add",
"object_id" => $mission->getId(),
"object_bundle" => "Mission",
"object_entity" => "Mission",
"object_display" => $mission->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,
"info" => $info,
"society_group" => $mission->getSociety()->getGroup(),
"society" => $mission->getSociety(),
"special_author" => $specialAuthor,
);
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Mission $mission, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($mission);
$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($mission, $loggingData);
//----------------------------------------------------------------------
// When merging, do not log anything on the Mission being removed
if ($loggingData['action'] == "mission_merged_and_deleted")
{
return $pendingLogArgs;
}
$receiver = $mission->getReceiver();
// Handle sharing
$societyGroupAuthor = $mission->getSocietyGroupAuthor();
$societyGroupOwner = $mission->getSocietyGroupOwner();
$double = false;
if (!$societyGroupAuthor->equals($societyGroupOwner))
{
$double = true;
}
$societyOwner = $mission->getSocietyOwner();
$society = $mission->getSociety();
$societyGroup = $mission->getSociety()->getGroup();
if ($double)
{
$argsBis = $this->initArgs($mission, $loggingData);
$argsBis["society_group"] = $societyGroupOwner;
$argsBis["society"] = $societyOwner;
}
$basicChanges = array(
"ref",
"title",
'mainImageId',
"info",
"desiredRdvInfo"
);
$labelChanges = array(
"status",
"type",
"project",
"origin",
"target",
);
// This is triggered when we are actually changing
// the object referenced in Devis :: $interventionAddress
$addressChanges = array(
"interventionAddress",
"billingAddress",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"manager",
"emitter",
"society",
"societyOwner",
"department",
);
$dateChanges = array(
"desiredRdvDate",
);
$action = "mission_edit";
foreach ($changes as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$argsBis["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
// Handle Merging
if ($key == "mergeData")
{
if ($loggingData['action'] == "mission_merge")
{
$args["action"] = $loggingData['action'];
$pendingLogArgs[] = $args;
continue;
}
}
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
if ($double)
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($argsBis, $before, $after);
}
continue;
}
if (in_array($key, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
if ($double)
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($argsBis, $before, $after);
}
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
if ($double)
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($argsBis, $before, $after);
}
continue;
}
if (in_array($key, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
if ($double)
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($argsBis, $before, $after);
}
continue;
}
if (in_array($key, $dateChanges))
{
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
if ($double)
{
$changeLogs = $this->tools->handleDateChanges($argsBis, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
}
continue;
}
}
if ($mission->getPhones()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($mission->getPhones()->getSnapshot() as $phone)
{
$oldData .= $phone->getNumber();
$oldData .= ", ";
}
foreach ($mission->getPhones() as $phone)
{
$newData .= $phone->getNumber();
$newData .= ", ";
}
if ($oldData != "")
{
$oldData = substr($oldData, 0, -1);
$oldData = substr($oldData, 0, -1);
}
if ($newData != "")
{
$newData = substr($newData, 0, -1);
$newData = substr($newData, 0, -1);
}
$args["action"] = "mission_edit_phones";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
// Only for Merge for now
public function logRemoval(Mission $mission)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($mission);
$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($mission, $loggingData);
//----------------------------------------------------------------------
// Handle Merging
$action = $loggingData['action'];
if (empty($action)) return [];
if ($action == "mission_merged_and_deleted")
{
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
}
private function initArgs(Mission $mission, $loggingData)
{
$receiver = $mission->getReceiver();
$society = $mission->getSociety();
$societyGroup = $mission->getSociety()->getGroup();
$args = array(
"object_id" => $mission->getId(),
"object_bundle" => "Mission",
"object_entity" => "Mission",
"object_display" => $mission->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;
}
// When Missions are created the corresponding Mission ID is not available
// So set it in the postFlush event
public function handleMission($log, $object)
{
if ($object instanceof Mission)
{
if (empty($log->getObjectMissionId()))
{
$log->setObjectMissionId($object->getId());
return true;
}
}
return false;
}
}