<?php
//----------------------------------------------------------------------
// src/Logging/AttachmentLog.php
// Plan.io Task #3922
// Attachments are like Installments
// We log on the main object, not the Attachment object itself
// => They require a DeletionContextLogger when deleted
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\Common\Attachment;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\Location\AddressTools;
use App\Services\LogTools;
class AttachmentLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->contextLogger = $contextLogger;
$this->tools = $tools;
}
public function logCreation(Attachment $attachment)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($attachment);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
$objectArgs = $this->extractData($attachment);
if (!empty($objectArgs))
{
// Fill the common arguments
$args = $this->initArgs($objectArgs, $info, $specialAuthor);
// Fill the specific arguments
$args["new_value"] = $attachment->getClientOriginalName();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
// Handle Ikea
if (!empty($attachment->getIkeaOrderNumber()))
{
// Created by Ikea => $specialAuthor is set (ikea_service_order)
// Created by Rekapp user => $specialAuthor is not set
$orderNumber = $attachment->getIkeaOrderNumber();
$ikeaAction = "ikea_service_order_add_attachment";
$ikeaArgs = $this->tools->handleIkeaServiceOrder($orderNumber, $attachment, $ikeaAction, $info, $specialAuthor);
foreach ($ikeaArgs as $argsItem)
{
$pendingLogArgs[] = $argsItem;
}
return $pendingLogArgs;
}
return [];
}
public function logChanges(Attachment $attachment, $changes)
{
// Only for Ikea
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($attachment);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
foreach ($changes as $key => $change)
{
$before = $change[0];
$after = $change[1];
if ($key == "ikeaSent")
{
$ikeaAction = "ikea_service_order_attachment_sent_to_ikea";
$ikeaArgs = $this->tools->handleIkeaServiceOrder($after, $attachment, $ikeaAction, $info, $specialAuthor);
foreach ($ikeaArgs as $argsItem)
{
$pendingLogArgs[] = $argsItem;
}
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(Attachment $attachment)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($attachment);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// [later.edit] I am not sure that Attachments can be linked to both Missions and Service Orders
// but keep this, it works just fine
// Ikea is a special case
// Attachments can be added to Missions
// - in this case attachment->getMission() !== null
// Attachments can be also linked to (multiple) Ikea Service Orders
// - in this case attachment->getIkeaOrderNumber() !== null
// Handle normal cases
// (but leave the possibility of simultaneously handling Ikea cases)
$cachedData = $this->contextLogger->getContext($attachment);
if ($cachedData !== null && !empty($cachedData))
{
$objectArgs = $this->extractDataFromCache($cachedData);
if (!empty($objectArgs))
{
// Fill the common arguments
$args = $this->initArgs($objectArgs, $info, $specialAuthor);
// Fill the specific arguments
$args["old_value"] = $attachment->getClientOriginalName();
$pendingLogArgs[] = $args;
}
}
// At this point $pendingLogArgs may already hold some data
// ... or not :)
// Handle Ikea
if (!empty($attachment->getIkeaOrderNumber()))
{
$orderNumber = $attachment->getIkeaOrderNumber();
$ikeaAction = "ikea_service_order_remove_attachment";
$ikeaArgs = $this->tools->handleIkeaServiceOrder($orderNumber, $attachment, $ikeaAction, $info, $specialAuthor);
foreach ($ikeaArgs as $argsItem)
{
$pendingLogArgs[] = $argsItem;
}
}
return $pendingLogArgs;
}
private function extractData(Attachment $attachment)
{
$args = [];
if ($attachment->getApplication() !== null)
{
$args['action'] = "application_add_attachment";
$args['object'] = $attachment->getApplication();
$args['objectClient'] = null;
$args['objectEntity'] = "Application";
$args['objectBundle'] = "HR";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if ($attachment->getClient() !== null)
{
$args['objectClient'] = $attachment->getClient();
$args['objectBundle'] = "Client";
if ($attachment->getClient()->isIndividual())
{
$args['action'] = "individual_add_attachment";
$args['object'] = $attachment->getClient()->getIndividual();
$args['objectEntity'] = "Individual";
}
else
{
$args['action'] = "store_add_attachment";
$args['object'] = $attachment->getClient()->getStore();
$args['objectEntity'] = "Store";
}
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if ($attachment->getCommand() !== null)
{
$args['action'] = "command_add_attachment";
$args['object'] = $attachment->getCommand();
$args['objectClient'] = $attachment->getCommand()->getReceiver();
$args['objectEntity'] = "Command";
$args['objectBundle'] = "Platform";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if ($attachment->getCost() !== null)
{
$args['action'] = "cost_add_attachment";
$args['object'] = $attachment->getCost();
$args['objectClient'] = null;
$args['objectEntity'] = "Cost";
$args['objectBundle'] = "Platform";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if ($attachment->getDamage() !== null)
{
$object = $attachment->getDamage()->getEquipment();
if ($object !== null)
{
if ($object->getVehicle() !== null)
{
$args['action'] = "vehicle_damage_add_attachment";
$args['object'] = $object->getVehicle();
$args['objectEntity'] = "Vehicle";
}
else
{
$args['action'] = "equipment_damage_add_attachment";
$args['object'] = $object;
$args['objectEntity'] = "Equipment";
}
$args['objectClient'] = null;
$args['objectBundle'] = "Equipment";
$args['objectMission'] = null;
$args['objectHumanResource'] = $object->getHumanResource();
return $args;
}
}
if ($attachment->getDevis() !== null)
{
$args['action'] = "devis_add_attachment";
$args['object'] = $attachment->getDevis();
$args['objectClient'] = $attachment->getDevis()->getReceiver();
$args['objectEntity'] = "Devis";
$args['objectBundle'] = "Platform";
$args['objectMission'] = $attachment->getDevis()->getMission();
return $args;
}
if ($attachment->getEquipment() !== null)
{
if ($attachment->getEquipment()->getVehicle() !== null)
{
$args['action'] = "vehicle_equipment_add_attachment";
$args['object'] = $attachment->getEquipment()->getVehicle();
$args['objectEntity'] = "Vehicle";
}
else
{
$args['action'] = "equipment_add_attachment";
$args['object'] = $attachment->getEquipment();
$args['objectEntity'] = "Equipment";
}
$args['objectClient'] = null;
$args['objectBundle'] = "Equipment";
$args['objectMission'] = null;
$args['objectHumanResource'] = $attachment->getEquipment()->getHumanResource();
return $args;
}
// TODO #3922 : Handle sharing in the future
if ($attachment->getMission() !== null)
{
$args['action'] = "mission_add_attachment";
$args['object'] = $attachment->getMission();
$args['objectClient'] = $attachment->getMission()->getReceiver();
$args['objectEntity'] = "Mission";
$args['objectBundle'] = "Mission";
$args['objectMission'] = $attachment->getMission();
$args['objectHumanResource'] = null;
return $args;
}
if ($attachment->getSupplier() !== null)
{
$args['action'] = "supplier_add_attachment";
$args['object'] = $attachment->getSupplier();
$args['objectClient'] = null;
$args['objectEntity'] = "Supplier";
$args['objectBundle'] = "Platform";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if ($attachment->getVehicleMaintenance() !== null)
{
if ($attachment->getVehicleMaintenance()->getVehicle() !== null)
{
$args['action'] = "vehicle_maintenance_add_attachment";
$args['object'] = $attachment->getVehicleMaintenance()->getVehicle();
$args['objectClient'] = null;
$args['objectEntity'] = "Vehicle";
$args['objectBundle'] = "Equipment";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
}
return [];
}
private function extractDataFromCache($cachedData)
{
// TODO #3922
// Devis and Client can co-exist - How to implement this ?
$args = [];
if (array_key_exists('application', $cachedData) && $cachedData['application'] !== null)
{
$object = $cachedData['application'];
$args['action'] = "application_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = null;
$args['objectEntity'] = "Application";
$args['objectBundle'] = "HR";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if (array_key_exists('client', $cachedData) && $cachedData['client'] !== null)
{
$client = $cachedData['client'];
$args['objectClient'] = $client;
$args['objectBundle'] = "Client";
if ($client->isIndividual())
{
$args['action'] = "individual_remove_attachment";
$args['object'] = $client->getIndividual();
$args['objectEntity'] = "Individual";
}
else
{
$args['action'] = "store_remove_attachment";
$args['object'] = $client->getStore();
$args['objectEntity'] = "Store";
}
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if (array_key_exists('command', $cachedData) && $cachedData['command'] !== null)
{
$object = $cachedData['command'];
$args['action'] = "command_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = $object->getReceiver();
$args['objectEntity'] = "Command";
$args['objectBundle'] = "Platform";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if (array_key_exists('cost', $cachedData) && $cachedData['cost'] !== null)
{
$object = $cachedData['cost'];
$args['action'] = "cost_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = null;
$args['objectEntity'] = "Cost";
$args['objectBundle'] = "Platform";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if (array_key_exists('damage', $cachedData) && $cachedData['damage'] !== null)
{
$object = $cachedData['damage']->getEquipment();
if ($object !== null)
{
if ($object->getVehicle() !== null)
{
$args['action'] = "vehicle_damage_remove_attachment";
$args['object'] = $object->getVehicle();
$args['objectEntity'] = "Vehicle";
}
else
{
$args['action'] = "equipment_damage_remove_attachment";
$args['object'] = $object;
$args['objectEntity'] = "Equipment";
}
$args['objectClient'] = null;
$args['objectBundle'] = "Equipment";
$args['objectMission'] = null;
$args['objectHumanResource'] = $object->getHumanResource();
return $args;
}
}
if (array_key_exists('devis', $cachedData) && $cachedData['devis'] !== null)
{
$object = $cachedData['devis'];
$args['action'] = "devis_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = $object->getReceiver();
$args['objectEntity'] = "Devis";
$args['objectBundle'] = "Platform";
$args['objectMission'] = $object->getMission();
$args['objectHumanResource'] = null;
return $args;
}
if (array_key_exists('equipment', $cachedData) && $cachedData['equipment'] !== null)
{
if ($cachedData['equipment']->getVehicle() !== null)
{
$object = $cachedData['equipment']->getVehicle();
$args['action'] = "vehicle_equipment_remove_attachment";
$args['object'] = $object;
$args['objectEntity'] = "Vehicle";
}
else
{
$object = $cachedData['equipment'];
$args['action'] = "equipment_remove_attachment";
$args['object'] = $object;
$args['objectEntity'] = "Equipment";
}
$args['objectClient'] = null;
$args['objectBundle'] = "Equipment";
$args['objectMission'] = null;
$args['objectHumanResource'] = $cachedData['equipment']->getHumanResource();
return $args;
}
// TODO #3922 : Handle sharing in the future
if (array_key_exists('mission', $cachedData) && $cachedData['mission'] !== null)
{
$object = $cachedData['mission'];
$args['action'] = "mission_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = $object->getReceiver();
$args['objectEntity'] = "Mission";
$args['objectBundle'] = "Mission";
$args['objectMission'] = $object;
return $args;
}
if (array_key_exists('supplier', $cachedData) && $cachedData['supplier'] !== null)
{
$object = $cachedData['supplier'];
$args['action'] = "supplier_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = null;
$args['objectEntity'] = "Supplier";
$args['objectBundle'] = "Platform";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
if (array_key_exists('vehicle_maintenance', $cachedData) && $cachedData['vehicle_maintenance'] !== null)
{
$object = $cachedData['vehicle_maintenance']->getVehicle();
if ($object !== null)
{
$args['action'] = "vehicle_maintenance_remove_attachment";
$args['object'] = $object;
$args['objectClient'] = null;
$args['objectEntity'] = "Vehicle";
$args['objectBundle'] = "Equipment";
$args['objectMission'] = null;
$args['objectHumanResource'] = null;
return $args;
}
}
return [];
}
private function initArgs($objectArgs, $info, $specialAuthor)
{
$object = $objectArgs['object'];
$objectClient = null;
$objectMission = null;
$objectHumanResource = null;
if (array_key_exists('objectClient', $objectArgs))
{
$objectClient = $objectArgs['objectClient'];
}
if (array_key_exists('objectMission', $objectArgs))
{
$objectMission = $objectArgs['objectMission'];
}
if (array_key_exists('objectHumanResource', $objectArgs))
{
$objectHumanResource = $objectArgs['objectHumanResource'];
}
$society = null;
$societyGroup = null;
if (method_exists($object, "getSociety"))
{
$society = $object->getSociety();
}
if (method_exists($object, "getSocietyGroup"))
{
$societyGroup = $object->getSocietyGroup();
}
else
{
if ($society !== null)
{
$societyGroup = $society->getSocietyGroup();
}
}
$args = array(
"action" => $objectArgs['action'],
"object_id" => $object->getId(),
"object_bundle" => $objectArgs['objectBundle'],
"object_entity" => $objectArgs['objectEntity'],
"object_display" => $object->display(),
"object_client_id" => $objectClient !== null ? $objectClient->getId() : null,
"object_client_display" => $objectClient !== null ? $objectClient->getName() : null,
"object_mission_id" => $objectMission !== null ? $objectMission->getId() : null,
"object_mission_display" => $objectMission !== null ? $objectMission->getRef() : null,
"object_human_resource_id" => $objectHumanResource !== null ? $objectHumanResource->getId() : null,
"object_human_resource_display" => $objectHumanResource !== null ? $objectHumanResource->display() : null,
"info" => $info,
"society_group" => $societyGroup,
"society" => $society,
"special_author" => $specialAuthor,
);
return $args;
}
}