<?php
//----------------------------------------------------------------------
// src/Logging/Activity/IkeaServiceOrderLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Ikea\ServiceOrder;
use App\Logging\Tools;
use App\Services\LogTools;
class IkeaServiceOrderLog
{
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(ServiceOrder $serviceOrder)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($serviceOrder);
$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($serviceOrder, $loggingData);
//----------------------------------------------------------------------
$action = "ikea_service_order_add";
$args["action"] = $action;
// Plan.io Task #3745
$status = null;
if ($serviceOrder->getStatus() !== null)
{
$status = $serviceOrder->getStatus()->getValue();
$args["new_value"] = $status;
}
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(ServiceOrder $serviceOrder, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($serviceOrder);
$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($serviceOrder, $loggingData);
//----------------------------------------------------------------------
$action = "ikea_service_order_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(
);
$basicChanges = array(
);
$basicBoolChanges = array(
);
$dateChanges = array(
"statusSentDate",
);
$labelChanges = array(
"status",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"mission",
"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))
{
// Display d/m/Y H:i => Add a fourth param true to handleDateChanges
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after, true);
if ($changeLogs !== null)
{
if ($key == "statusSentDate")
{
$changeLogs["action"] = "ikea_service_order_status_sent_to_ikea";
if ($serviceOrder->getStatus() !== null)
{
$changeLogs["old_value"] = $serviceOrder->getStatus()->getValue();
$changeLogs["new_value"] = $serviceOrder->getStatus()->getValue();
}
}
$pendingLogArgs[] = $changeLogs;
}
continue;
}
if (in_array($key, $labelChanges))
{
if ($key == "status")
{
if (!empty($loggingData["action"]))
{
// Log Status Forced requires the author to be formated a certain way
$args["action"] = $loggingData["action"];
$args["special_author"] = "ikea_service_order";
}
}
$changeLogs = $this->tools->handleLabelChanges($args, $before, $after);
$pendingLogArgs[] = $changeLogs;
continue;
}
if (in_array($key, $objectChanges))
{
$changeLogs = $this->tools->handleObjectChanges($args, $before, $after);
if ($key == "mission")
{
$changeLogs["action"] = "ikea_service_order_link_to_mission";
}
$pendingLogArgs[] = $changeLogs;
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(ServiceOrder $serviceOrder)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(ServiceOrder $serviceOrder, $loggingData)
{
$receiver = $serviceOrder->getClient();
$society = $serviceOrder->getSociety();
$societyGroup = $serviceOrder->getSocietyGroup();
$mission = $serviceOrder->getMission();
$args = array(
"object_id" => $serviceOrder->getId(),
"object_bundle" => "Ikea",
"object_entity" => "ServiceOrder",
"object_display" => $serviceOrder->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;
}
}