<?php
//----------------------------------------------------------------------
// src/Logging/Activity/ProjectNotebookItemLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use App\Entity\ProjectManager\Blueprint;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\ProjectManager\ProjectNotebookItem;
use App\Logging\Tools;
use App\Services\LogTools;
class ProjectNotebookItemLog
{
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(ProjectNotebookItem $notebookItem)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($notebookItem);
$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($notebookItem, $loggingData);
//----------------------------------------------------------------------
$action = "project_notebook_item_add";
$args["action"] = $action;
$pendingLogArgs[] = $this->tools->handleProjectNotebookItemAdd($args, $notebookItem);
// $pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(ProjectNotebookItem $notebookItem, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($notebookItem);
$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($notebookItem, $loggingData);
//----------------------------------------------------------------------
$action = "project_notebook_item_edit";
$basicChanges = array(
"itemData",
);
// 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];
if (in_array($key, $basicChanges))
{
if ($before == $after) continue;
$pendingLogArgs[] = $this->tools->handleProjectNotebookItemUpdate($args, $notebookItem, $before, $after);
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(ProjectNotebookItem $notebookItem)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($notebookItem);
$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($notebookItem, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "project_notebook_item_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(ProjectNotebookItem $notebookItem, $loggingData)
{
$notebook = $notebookItem->getNotebook();
$societyGroup = $notebook->getSocietyGroup();
$mission = $notebook->getMission();
$society = null;
$client = null;
if ($mission !== null)
{
$client = $mission->getReceiver();
$society = $mission->getSociety();
}
$module = null;
$itemCode = $notebookItem->getItemCode();
$item = Blueprint::getItemForCode($notebookItem->getItemCode());
if ($item !== null)
{
$module = Blueprint::getModuleForCode($item['module_code']);
}
$args = array(
"object_id" => $notebookItem->getId(),
"object_bundle" => "ProjectManager",
"object_entity" => "ProjectNotebookItem",
"object_display" => $notebookItem->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;
}
}