<?php
//----------------------------------------------------------------------
// src/Logging/SocietyGroupLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\SocietyGroup;
use App\Logging\Tools;
use App\Services\LogTools;
class SocietyGroupLog
{
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(SocietyGroup $societyGroup)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($societyGroup);
$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($societyGroup, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "society_group_add";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(SocietyGroup $societyGroup, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($societyGroup);
$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($societyGroup, $loggingData);
//----------------------------------------------------------------------
$action = "society_group_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",
"billingAddress",
);
$basicChanges = array(
"ref",
"name",
"contactEmail",
"noreplyEmail",
"siret",
);
$basicBoolChanges = array(
"isActive",
);
$dateChanges = array(
);
$labelChanges = array(
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"admin",
"defaultManager",
);
// 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))
{
$args = $this->tools->handleBasicBoolChanges($args, $before, $after);
if ($key == "isActive")
{
if (!$before && $after) $args['action'] = "society_group_activate";
if ($before && !$after) $args['action'] = "society_group_deactivate";
$args['old_value'] = null;
$args['new_value'] = null;
}
$pendingLogArgs[] = $args;
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 ($societyGroup->getSocieties()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($societyGroup->getSocieties()->getSnapshot() as $society)
{
$oldData .= $society->display();
$oldData .= ", ";
}
foreach ($societyGroup->getSocieties() as $society)
{
$newData .= $society->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"] = "society_group_edit_societies";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(SocietyGroup $societyGroup)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(SocietyGroup $societyGroup, $loggingData)
{
$args = array(
"object_id" => $societyGroup->getId(),
"object_bundle" => "Platform",
"object_entity" => "SocietyGroup",
"object_display" => $societyGroup->display(),
"society_group" => $societyGroup,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
public function handleSocietyGroupCreation($log, $object)
{
if ($log->getAction() == "society_group_add")
{
$log->setSocietyGroupId($object->getId());
}
}
}