<?php
//----------------------------------------------------------------------
// src/Logging/ProjectNotebookLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Logging\Tools;
use App\Services\LogTools;
use App\Entity\ProjectManager\ProjectNotebook;
class ProjectNotebookLog
{
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(ProjectNotebook $projectNotebook)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($projectNotebook);
$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($projectNotebook, $loggingData);
//----------------------------------------------------------------------
$action = "project_notebook_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(ProjectNotebook $projectNotebook, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($projectNotebook);
$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($projectNotebook, $loggingData);
//----------------------------------------------------------------------
$action = "project_notebook_edit";
$basicChanges = array(
// "ikeaMethodVersion",
);
$basicBoolChanges = array(
"visibleToClient",
);
$dateChanges = array(
);
$labelChanges = array(
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"group",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
// Handle Method Version
if ($key == "ikeaMethodVersion")
{
$pendingLogArgs[] = $this->tools->handleProjectNotebookIkeaMethodVersion($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;
}
}
return $pendingLogArgs;
}
public function logRemoval(ProjectNotebook $projectNotebook)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($projectNotebook);
$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($projectNotebook, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "project_notebook_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(ProjectNotebook $notebook, $loggingData)
{
$receiver = null;
$society = null;
$societyGroup = $notebook->getSocietyGroup();
$mission = $notebook->getMission();
$society = null;
$client = null;
if ($mission !== null)
{
$client = $mission->getReceiver();
$society = $mission->getSociety();
}
$module = null;
$item = null;
$args = array(
"object_id" => $notebook->getId(),
"object_bundle" => "ProjectManager",
"object_entity" => "ProjectNotebook",
"object_display" => $notebook->display(),
"society_group" => $societyGroup,
"society" => $society,
"object_client_id" => $client !== null ? $client->getId() : null,
"object_client_display" => $client !== null ? $client->getName() : null,
"object_mission_id" => $mission !== null ? $mission->getId() : null,
"object_mission_display" => $mission !== null ? $mission->getRef() : null,
"project_notebook_id" => $notebook !== null ? $notebook->getId() : null,
"module_code" => $module !== null ? $module['code'] : null,
"module_display" => $module !== null ? $module['label'] : null,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}