<?php
//----------------------------------------------------------------------
// src/Logging/IndividualLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\AccessClient\AccessClient;
use App\Entity\Client\Individual;
use App\Logging\Tools;
use App\Services\LogTools;
class IndividualLog
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(Individual $individual)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($individual);
$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($individual, $loggingData);
//----------------------------------------------------------------------
if ($individual->getClient() !== null && $individual->getClient()->isProspect())
{
$action = "prospect_add";
}
else
{
$action = "individual_add";
}
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Individual $individual, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($individual);
$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($individual, $loggingData);
//----------------------------------------------------------------------
// When merging, do not log anything on the Individual being removed
if ($loggingData['action'] == "individual_merged_and_deleted")
{
return $pendingLogArgs;
}
if ($individual->getClient() !== null && $individual->getClient()->isProspect())
{
$action = "prospect_edit";
$actionChange = "prospect_change";
}
else
{
$action = "individual_edit";
$actionChange = "individual_change";
}
$args["action"] = $action;
if ($info == 'convert_prospect')
{
$args["action"] = "prospect_convert_to_client";
$args["object_entity"] = "Client.Prospect";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
// This is triggered when we are actually changing
// the object referenced in Devis :: $interventionAddress
$addressChanges = array(
"address",
"billingAddress",
);
$basicChanges = array(
"lastname",
"firstname",
"company",
"info",
"email",
"phone",
);
$basicBoolChanges = array(
"commercialConsent",
);
$dateChanges = array(
"rgpdConsent",
"ccdDate",
);
$labelChanges = array(
"title",
"origin",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"defaultStore",
"society",
"societyOwner",
"manager",
"author",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
// Handle Merging
if ($key == "mergeData")
{
if ($loggingData['action'] == "individual_merge")
{
$args["action"] = $loggingData['action'];
$pendingLogArgs[] = $args;
continue;
}
}
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
if (in_array($key, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
continue;
}
if (in_array($key, $dateChanges))
{
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
continue;
}
if (in_array($key, $basicBoolChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args, $before, $after);
continue;
}
}
// See what changed and log (relationships OneToMany and ManyToMany second)
if ($individual->getStores()->isDirty())
{
$old = "";
foreach ($individual->getStores()->getSnapshot() as $store)
{
$old .= $store->getName();
$old .= ", ";
}
if ($old != "")
{
$old = substr($old, 0, -1);
$old = substr($old, 0, -1);
}
$new = "";
foreach ($individual->getStores() as $store)
{
$new .= $store->getName();
$new .= ", ";
}
if ($new != "")
{
$new = substr($new, 0, -1);
$new = substr($new, 0, -1);
}
$args["action"] = $action."_stores";
$args["old_value"] = $old;
$args["new_value"] = $new;
$pendingLogArgs[] = $args;
}
// Individual :: $address and Individual :: $billingAddress
// are handled in AddressLog
return $pendingLogArgs;
}
// Only for Merge for now
public function logRemoval(Individual $individual)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($individual);
$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($individual, $loggingData);
//----------------------------------------------------------------------
// Handle Merging
$action = $loggingData['action'];
if (empty($action)) return [];
if ($action == "individual_merged_and_deleted")
{
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
}
public function initArgs(Individual $individual, $loggingData)
{
if ($individual->getClient() !== null && $individual->getClient()->isProspect())
{
$objectEntity = "Client.Prospect";
}
else
{
$objectEntity = "Individual";
}
$args = array(
"object_id" => $individual->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $individual->displayForLogging(),
"object_client_id" => $individual->getClient()->getId(),
"object_client_display" => $individual->displayForLogging(),
"society_group" => $individual->getSociety()->getGroup(),
"society" => $individual->getSociety(),
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
// When Individuals are created the corresponding Client ID is not available
// So set it in the postFlush event
public function handleIndividual($log, $object)
{
if ($object instanceof Individual)
{
if (empty($log->getObjectClientId()))
{
$log->setObjectClientId($object->getClient()->getId());
return true;
}
}
return false;
}
}