src/Logging/Activity/TaskLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/TaskLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use App\Entity\Planning\Task;
  9. use App\Logging\Tools;
  10. use App\Services\LogTools;
  11. class TaskLog
  12. {
  13.     private array $pendingLogArgs = [];
  14.     public function __construct(ManagerRegistry $doctrineTranslatorInterface $translatorLogTools $logToolsTools $tools)
  15.     {
  16.         $this->em $doctrine->getManager();
  17.         $this->translator $translator;
  18.         $this->logTools $logTools;
  19.         $this->tools $tools;
  20.     }
  21.     public function logCreation(Task $task)
  22.     {
  23.         $pendingLogArgs = [];
  24.         //----------------------------------------------------------------------
  25.         // Fetch eventual info stored in the object
  26.         $loggingData $this->logTools->handleLoggingData($task);
  27.         $info $loggingData['info'];
  28.         $specialAuthor $loggingData['special_author'];
  29.         $ignore $loggingData['ignore'];
  30.         if ($ignore) return $pendingLogArgs;
  31.         //----------------------------------------------------------------------
  32.         // Init base log args
  33.         // This does not contain action
  34.         $args $this->initArgs($task$loggingData);
  35.         //----------------------------------------------------------------------
  36.         $args["action"] = "task_add";
  37.         if ($info == "help")
  38.         {
  39.             $args["action"] = "task_add_help";
  40.             $args["info"] = null;
  41.         }
  42.         $pendingLogArgs[] = $args;
  43.         return $pendingLogArgs;
  44.     }
  45.     public function logChanges(Task $task$changes)
  46.     {
  47.         $pendingLogArgs = [];
  48.         //----------------------------------------------------------------------
  49.         // Fetch eventual info stored in the object
  50.         $loggingData $this->logTools->handleLoggingData($task);
  51.         $info $loggingData['info'];
  52.         $specialAuthor $loggingData['special_author'];
  53.         $ignore $loggingData['ignore'];
  54.         if ($ignore) return $pendingLogArgs;
  55.         //----------------------------------------------------------------------
  56.         // Init base log args
  57.         // This does not contain action
  58.         $args $this->initArgs($task$loggingData);
  59.         //----------------------------------------------------------------------
  60.         $basicChanges = array(
  61.             "refOrder",
  62.             "name",
  63.             "visibleToClient",            // Plan.io Task #4327
  64.             "totalInternalHT",
  65.         );
  66.         $labelChanges = array(
  67.             "status",
  68.             "type",
  69.             "subType",
  70.         );
  71.         // Method getLogLabel() should be defined for these objects/entities
  72.         $objectChanges = array(
  73.             "receiver",
  74.             "emitter",
  75.         );
  76.         $dateChanges = array(
  77.             "recurrenceEndDate",
  78.         );
  79.         // See what changed and log (members first)
  80.         foreach ($changes as $key => $change)
  81.         {
  82.             $name $this->logTools->camelToSnakeCase($key);
  83.             $args["action"]    = "task_edit_".$name;
  84.             if ($key == "status" && !empty($loggingData['action']))
  85.             {
  86.                 $args["action"] = $loggingData['action'];
  87.             }
  88.             $before $change[0];
  89.             $after $change[1];
  90.             if (in_array($key$basicChanges))
  91.             {
  92.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  93.                 continue;
  94.             }
  95.             if (in_array($key$labelChanges))
  96.             {
  97.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  98.                 continue;
  99.             }
  100.             if (in_array($key$objectChanges))
  101.             {
  102.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  103.                 continue;
  104.             }
  105.             if (in_array($key$dateChanges))
  106.             {
  107.                 // dateChanges can be null
  108.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  109.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  110.                 continue;
  111.             }
  112.             if ($key == "recurrenceType")
  113.             {
  114.                 $args["old_value"]    = "";
  115.                 $args["new_value"]    = "";
  116.                 if ($change[0] !== null)
  117.                 {
  118.                     $text Task::RECURRENCE_TRANS[$change[0]];
  119.                     $args["old_value"]    = $this->translator->trans($text);
  120.                 }
  121.                 if ($change[1] !== null)
  122.                 {
  123.                     $text Task::RECURRENCE_TRANS[$change[1]];
  124.                     $args["new_value"]    = $this->translator->trans($text);
  125.                 }
  126.                 $pendingLogArgs[] = $args;
  127.                 continue;
  128.             }
  129.             if ($key == "startDate")
  130.             {
  131.                 if ($change[0]->format("YmdHi") != $change[1]->format("YmdHi"))
  132.                 {
  133.                     if ($info == "resize")
  134.                     {
  135.                         $args["action"] = "task_resize_start";
  136.                         $args["info"] = null;
  137.                     }
  138.                     else
  139.                     {
  140.                         if ($info == "drop")
  141.                         {
  142.                             $args["action"] = "task_drop_start";
  143.                             $args["info"] = null;
  144.                         }
  145.                     }
  146.                     $args["old_value"]    = $change[0]->format("d/m/Y H:i");
  147.                     $args["new_value"]    = $change[1]->format("d/m/Y H:i");
  148.                     $pendingLogArgs[] = $args;
  149.                 }
  150.                 continue;
  151.             }
  152.             if ($key == "endDate")
  153.             {
  154.                 if ($change[0]->format("YmdHi") != $change[1]->format("YmdHi"))
  155.                 {
  156.                     if ($info == "resize")
  157.                     {
  158.                         $args["action"] = "task_resize_end";
  159.                         $args["info"] = null;
  160.                     }
  161.                     else
  162.                     {
  163.                         if ($info == "drop")
  164.                         {
  165.                             $args["action"] = "task_drop_end";
  166.                             $args["info"] = null;
  167.                         }
  168.                     }
  169.                     $args["old_value"]    = $change[0]->format("d/m/Y H:i");
  170.                     $args["new_value"]    = $change[1]->format("d/m/Y H:i");
  171.                     $pendingLogArgs[] = $args;
  172.                 }
  173.                 continue;
  174.             }
  175.         }
  176.         // See what changed and log (relationships OneToMany and ManyToMany second)
  177.         if ($task->getPlanningResources()->isDirty())
  178.         {
  179.             $old "";
  180.             foreach ($task->getPlanningResources()->getSnapshot() as $r)
  181.             {
  182.                 $old .= $r->getTitle();
  183.                 $old .= ", ";
  184.             }
  185.             if ($old != "")
  186.             {
  187.                 $old substr($old0, -1);
  188.                 $old substr($old0, -1);
  189.             }
  190.             $new "";
  191.             foreach ($task->getPlanningResources() as $r)
  192.             {
  193.                 $new .= $r->getTitle();
  194.                 $new .= ", ";
  195.             }
  196.             if ($new != "")
  197.             {
  198.                 $new substr($new0, -1);
  199.                 $new substr($new0, -1);
  200.             }
  201.             if ($info == "drop")    $args["action"]     = "task_drop_resource";
  202.             else                    $args["action"]     = "task_edit_resources";
  203.             $args["old_value"]    = $old;
  204.             $args["new_value"]    = $new;
  205.             $pendingLogArgs[] = $args;
  206.         }
  207.         return $pendingLogArgs;
  208.     }
  209.     public function logRemoval(Task $task)
  210.     {
  211.         $pendingLogArgs = [];
  212.         //----------------------------------------------------------------------
  213.         // Fetch eventual info stored in the object
  214.         $loggingData $this->logTools->handleLoggingData($task);
  215.         $info $loggingData['info'];
  216.         $specialAuthor $loggingData['special_author'];
  217.         $ignore $loggingData['ignore'];
  218.         if ($ignore) return $pendingLogArgs;
  219.         //----------------------------------------------------------------------
  220.         // Init base log args
  221.         // This does not contain action
  222.         $args $this->initArgs($task$loggingData);
  223.         //----------------------------------------------------------------------
  224.         $args["action"] = "task_delete";
  225.         if ($info == "help")
  226.         {
  227.             $args["action"] = "task_delete_help";
  228.             $args["info"] = null;
  229.         }
  230.         $pendingLogArgs[] = $args;
  231.         return $pendingLogArgs;
  232.     }
  233.     private function initArgs(Task $task$loggingData)
  234.     {
  235.         $receiver $task->getReceiver();
  236.         $society $task->getSociety();
  237.         $societyGroup $task->getSociety()->getGroup();
  238.         $mission $task->getMission();
  239.         // If Help or Recurrence, the Client is attached to the parent, not the child task
  240.         if ($task->getParent() !== null)
  241.         {
  242.             $receiver $task->getParent()->getReceiver();
  243.         }
  244.         $args = array(
  245.             "object_id"                    =>    $task->getId(),
  246.             "object_bundle"                =>    "Planning",
  247.             "object_entity"                =>    "Task",
  248.             "object_display"            =>    $task->display(),
  249.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  250.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  251.             "object_mission_id"            =>    $mission !== null $mission->getId() : null,
  252.             "object_mission_display"    =>    $mission !== null $mission->getRef() : null,
  253.             
  254.             "society_group"                =>    $societyGroup,
  255.             "society"                    =>    $society,
  256.             "info"                        =>    $loggingData['info'],
  257.             "special_author"            =>    $loggingData['special_author'],
  258.         );
  259.         return $args;
  260.     }
  261. }