<?php
//----------------------------------------------------------------------
// src/Logging/TaskLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Planning\Task;
use App\Logging\Tools;
use App\Services\LogTools;
class TaskLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, TranslatorInterface $translator, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->translator = $translator;
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(Task $task)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($task);
$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($task, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "task_add";
if ($info == "help")
{
$args["action"] = "task_add_help";
$args["info"] = null;
}
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Task $task, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($task);
$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($task, $loggingData);
//----------------------------------------------------------------------
$basicChanges = array(
"refOrder",
"name",
"visibleToClient", // Plan.io Task #4327
"totalInternalHT",
);
$labelChanges = array(
"status",
"type",
"subType",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"receiver",
"emitter",
);
$dateChanges = array(
"recurrenceEndDate",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = "task_edit_".$name;
if ($key == "status" && !empty($loggingData['action']))
{
$args["action"] = $loggingData['action'];
}
$before = $change[0];
$after = $change[1];
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, $dateChanges))
{
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
continue;
}
if ($key == "recurrenceType")
{
$args["old_value"] = "";
$args["new_value"] = "";
if ($change[0] !== null)
{
$text = Task::RECURRENCE_TRANS[$change[0]];
$args["old_value"] = $this->translator->trans($text);
}
if ($change[1] !== null)
{
$text = Task::RECURRENCE_TRANS[$change[1]];
$args["new_value"] = $this->translator->trans($text);
}
$pendingLogArgs[] = $args;
continue;
}
if ($key == "startDate")
{
if ($change[0]->format("YmdHi") != $change[1]->format("YmdHi"))
{
if ($info == "resize")
{
$args["action"] = "task_resize_start";
$args["info"] = null;
}
else
{
if ($info == "drop")
{
$args["action"] = "task_drop_start";
$args["info"] = null;
}
}
$args["old_value"] = $change[0]->format("d/m/Y H:i");
$args["new_value"] = $change[1]->format("d/m/Y H:i");
$pendingLogArgs[] = $args;
}
continue;
}
if ($key == "endDate")
{
if ($change[0]->format("YmdHi") != $change[1]->format("YmdHi"))
{
if ($info == "resize")
{
$args["action"] = "task_resize_end";
$args["info"] = null;
}
else
{
if ($info == "drop")
{
$args["action"] = "task_drop_end";
$args["info"] = null;
}
}
$args["old_value"] = $change[0]->format("d/m/Y H:i");
$args["new_value"] = $change[1]->format("d/m/Y H:i");
$pendingLogArgs[] = $args;
}
continue;
}
}
// See what changed and log (relationships OneToMany and ManyToMany second)
if ($task->getPlanningResources()->isDirty())
{
$old = "";
foreach ($task->getPlanningResources()->getSnapshot() as $r)
{
$old .= $r->getTitle();
$old .= ", ";
}
if ($old != "")
{
$old = substr($old, 0, -1);
$old = substr($old, 0, -1);
}
$new = "";
foreach ($task->getPlanningResources() as $r)
{
$new .= $r->getTitle();
$new .= ", ";
}
if ($new != "")
{
$new = substr($new, 0, -1);
$new = substr($new, 0, -1);
}
if ($info == "drop") $args["action"] = "task_drop_resource";
else $args["action"] = "task_edit_resources";
$args["old_value"] = $old;
$args["new_value"] = $new;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Task $task)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($task);
$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($task, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "task_delete";
if ($info == "help")
{
$args["action"] = "task_delete_help";
$args["info"] = null;
}
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Task $task, $loggingData)
{
$receiver = $task->getReceiver();
$society = $task->getSociety();
$societyGroup = $task->getSociety()->getGroup();
$mission = $task->getMission();
// If Help or Recurrence, the Client is attached to the parent, not the child task
if ($task->getParent() !== null)
{
$receiver = $task->getParent()->getReceiver();
}
$args = array(
"object_id" => $task->getId(),
"object_bundle" => "Planning",
"object_entity" => "Task",
"object_display" => $task->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;
}
}