<?php
//----------------------------------------------------------------------
// src/Logging/ApplicationLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\HR\Application\Application;
use App\Logging\Tools;
use App\Services\LogTools;
class ApplicationLog
{
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(Application $application)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($application);
$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($application, $loggingData);
//----------------------------------------------------------------------
$action = "application_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Application $application, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($application);
$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($application, $loggingData);
//----------------------------------------------------------------------
$action = "application_edit";
// This is triggered when we are actually changing the connected Address object
// The changes in the actual object are handled in AddressLog
$addressChanges = array(
"address",
);
$basicChanges = array(
"ref",
"firstname",
"lastname",
"email",
"phone",
"sponsor",
"company",
"companyCode",
"legalForm",
"qualification",
"nbStaff",
"townsDisplay",
"departmentsDisplay",
"info",
);
$basicBoolChanges = array(
);
$dateChanges = array(
);
$labelChanges = array(
"status",
"category",
"experience",
"mobility",
"origin",
"position",
'level',
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"society",
);
// 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;
$before = $change[0];
$after = $change[1];
if (in_array($key, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicBoolChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicBoolChanges($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, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
}
// Handle ManyToMany
if ($application->getActivities()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($application->getActivities()->getSnapshot() as $activity)
{
$oldData .= $activity->display();
$oldData .= ", ";
}
foreach ($application->getActivities() as $activity)
{
$newData .= $activity->display();
$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"] = "application_edit_activities";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Application $application)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($application);
$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($application, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "application_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Application $application, $loggingData)
{
$society = $application->getSociety();
$societyGroup = $application->getSocietyGroup();
// Add HumanResource logging
$humanResource = $application->getHumanResource();
$args = array(
"object_id" => $application->getId(),
"object_bundle" => "HR",
"object_entity" => "Application",
"object_display" => $application->display(),
"object_human_resource_id" => $humanResource !== null ? $humanResource->getId() : null,
"object_human_resource_display" => $humanResource !== null ? $humanResource->display() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}