src/Logging/Activity/MissionLog.php line 22

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/MissionLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Entity\Mission\Mission;
  9. use App\Logging\Tools;
  10. use App\Services\Location\AddressTools;
  11. use App\Services\LogTools;
  12. class MissionLog
  13. {
  14.     private array $pendingLogArgs = [];
  15.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsAddressTools $addressToolsTools $tools)
  16.     {
  17.         $this->em $doctrine->getManager();
  18.         $this->logTools $logTools;
  19.         $this->addressTools $addressTools;
  20.         $this->tools $tools;
  21.     }
  22.     public function logCreation(Mission $mission)
  23.     {
  24.         $pendingLogArgs = [];
  25.         //----------------------------------------------------------------------
  26.         // Fetch eventual info stored in the object
  27.         $loggingData $this->logTools->handleLoggingData($mission);
  28.         $info $loggingData['info'];
  29.         $specialAuthor $loggingData['special_author'];
  30.         $ignore $loggingData['ignore'];
  31.         if ($ignore) return $pendingLogArgs;
  32.         //----------------------------------------------------------------------
  33.         $receiver $mission->getReceiver();
  34.         $args = array(
  35.             "action"                    =>    "mission_add",
  36.             "object_id"                    =>    $mission->getId(),
  37.             "object_bundle"                =>    "Mission",
  38.             "object_entity"                =>    "Mission",
  39.             "object_display"            =>    $mission->display(),
  40.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  41.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  42.             "object_mission_id"            =>    $mission !== null $mission->getId() : null,
  43.             "object_mission_display"    =>    $mission !== null $mission->getRef() : null,
  44.             "info"                        =>    $info,
  45.             "society_group"                =>    $mission->getSociety()->getGroup(),
  46.             "society"                    =>    $mission->getSociety(),
  47.             "special_author"            =>    $specialAuthor,
  48.         );
  49.         $pendingLogArgs[] = $args;
  50.         return $pendingLogArgs;
  51.     }
  52.     public function logChanges(Mission $mission$changes)
  53.     {
  54.         $pendingLogArgs = [];
  55.         //----------------------------------------------------------------------
  56.         // Fetch eventual info stored in the object
  57.         $loggingData $this->logTools->handleLoggingData($mission);
  58.         $info $loggingData['info'];
  59.         $specialAuthor $loggingData['special_author'];
  60.         $ignore $loggingData['ignore'];
  61.         if ($ignore) return $pendingLogArgs;
  62.         //----------------------------------------------------------------------
  63.         // Init base log args
  64.         // This does not contain action
  65.         $args $this->initArgs($mission$loggingData);
  66.         //----------------------------------------------------------------------
  67.         // When merging, do not log anything on the Mission being removed
  68.         if ($loggingData['action'] == "mission_merged_and_deleted")
  69.         {
  70.             return $pendingLogArgs;
  71.         }
  72.         $receiver $mission->getReceiver();
  73.         // Handle sharing
  74.         $societyGroupAuthor $mission->getSocietyGroupAuthor();
  75.         $societyGroupOwner $mission->getSocietyGroupOwner();
  76.         $double false;
  77.         if (!$societyGroupAuthor->equals($societyGroupOwner))
  78.         {
  79.             $double true;
  80.         }
  81.         $societyOwner $mission->getSocietyOwner();
  82.         $society $mission->getSociety();
  83.         $societyGroup $mission->getSociety()->getGroup();
  84.         if ($double)
  85.         {
  86.             $argsBis $this->initArgs($mission$loggingData);
  87.             $argsBis["society_group"] = $societyGroupOwner;
  88.             $argsBis["society"] = $societyOwner;
  89.         }
  90.         $basicChanges = array(
  91.             "ref",
  92.             "title",
  93.             'mainImageId',
  94.             "info",
  95.             "desiredRdvInfo"
  96.         );
  97.         $labelChanges = array(
  98.             "status",
  99.             "type",
  100.             "project",
  101.             "origin",
  102.             "target",
  103.         );
  104.         // This is triggered when we are actually changing
  105.         // the object referenced in Devis :: $interventionAddress
  106.         $addressChanges = array(
  107.             "interventionAddress",
  108.             "billingAddress",
  109.         );
  110.         // Method getLogLabel() should be defined for these objects/entities
  111.         $objectChanges = array(
  112.             "manager",
  113.             "emitter",
  114.             "society",
  115.             "societyOwner",
  116.             "department",            
  117.         );
  118.         $dateChanges = array(
  119.             "desiredRdvDate",
  120.         );
  121.         $action "mission_edit";
  122.         foreach ($changes as $key => $change)
  123.         {
  124.             $name $this->logTools->camelToSnakeCase($key);
  125.             $args["action"]    = $action."_".$name;
  126.             $argsBis["action"]    = $action."_".$name;
  127.             $before $change[0];
  128.             $after $change[1];
  129.             // Handle Merging
  130.             if ($key == "mergeData")
  131.             {
  132.                 if ($loggingData['action'] == "mission_merge")
  133.                 {
  134.                     $args["action"] = $loggingData['action'];
  135.                     $pendingLogArgs[] = $args;
  136.                     continue;
  137.                 }
  138.             }
  139.             if (in_array($key$basicChanges))
  140.             {
  141.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  142.                 if ($double)
  143.                 {
  144.                     $pendingLogArgs[] = $this->tools->handleBasicChanges($argsBis$before$after);
  145.                 }
  146.                 continue;
  147.             }
  148.             if (in_array($key$labelChanges))
  149.             {
  150.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  151.                 if ($double)
  152.                 {
  153.                     $pendingLogArgs[] = $this->tools->handleLabelChanges($argsBis$before$after);
  154.                 }
  155.                 continue;
  156.             }
  157.             if (in_array($key$objectChanges))
  158.             {
  159.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  160.                 if ($double)
  161.                 {
  162.                     $pendingLogArgs[] = $this->tools->handleObjectChanges($argsBis$before$after);
  163.                 }
  164.                 continue;
  165.             }
  166.             if (in_array($key$addressChanges))
  167.             {
  168.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  169.                 if ($double)
  170.                 {
  171.                     $pendingLogArgs[] = $this->tools->handleAddressChanges($argsBis$before$after);
  172.                 }
  173.                 continue;
  174.             }
  175.             if (in_array($key$dateChanges))
  176.             {
  177.                 // dateChanges can be null
  178.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  179.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  180.                 if ($double)
  181.                 {
  182.                     $changeLogs $this->tools->handleDateChanges($argsBis$before$after);
  183.                     if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  184.                 }
  185.                 continue;
  186.             }
  187.         }
  188.         if ($mission->getPhones()->isDirty())
  189.         {
  190.             $oldData "";
  191.             $newData "";
  192.             foreach ($mission->getPhones()->getSnapshot() as $phone)
  193.             {
  194.                 $oldData .= $phone->getNumber();
  195.                 $oldData .= ", ";
  196.             }
  197.             foreach ($mission->getPhones() as $phone)
  198.             {
  199.                 $newData .= $phone->getNumber();
  200.                 $newData .= ", ";
  201.             }
  202.             if ($oldData != "")
  203.             {
  204.                 $oldData substr($oldData0, -1);
  205.                 $oldData substr($oldData0, -1);
  206.             }
  207.             if ($newData != "")
  208.             {
  209.                 $newData substr($newData0, -1);
  210.                 $newData substr($newData0, -1);
  211.             }
  212.             $args["action"] = "mission_edit_phones";
  213.             $args["old_value"] = $oldData;
  214.             $args["new_value"] = $newData;
  215.             $pendingLogArgs[] = $args;
  216.         }
  217.         return $pendingLogArgs;
  218.     }
  219.     // Only for Merge for now
  220.     public function logRemoval(Mission $mission)
  221.     {
  222.         $pendingLogArgs = [];
  223.         //----------------------------------------------------------------------
  224.         // Fetch eventual info stored in the object
  225.         $loggingData $this->logTools->handleLoggingData($mission);
  226.         $info $loggingData['info'];
  227.         $specialAuthor $loggingData['special_author'];
  228.         $ignore $loggingData['ignore'];
  229.         if ($ignore) return $pendingLogArgs;
  230.         //----------------------------------------------------------------------
  231.         // Init base log args
  232.         // This does not contain action
  233.         $args $this->initArgs($mission$loggingData);
  234.         //----------------------------------------------------------------------
  235.         // Handle Merging
  236.         $action $loggingData['action'];
  237.         if (empty($action)) return [];
  238.         if ($action == "mission_merged_and_deleted")
  239.         {
  240.             $args["action"] = $action;
  241.             $pendingLogArgs[] = $args;
  242.             return $pendingLogArgs;
  243.         }
  244.     }
  245.     private function initArgs(Mission $mission$loggingData)
  246.     {
  247.         $receiver $mission->getReceiver();
  248.         $society $mission->getSociety();
  249.         $societyGroup $mission->getSociety()->getGroup();        
  250.         $args = array(
  251.             "object_id"                    =>    $mission->getId(),
  252.             "object_bundle"                =>    "Mission",
  253.             "object_entity"                =>    "Mission",
  254.             "object_display"            =>    $mission->display(),
  255.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  256.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  257.             "object_mission_id"            =>    $mission !== null $mission->getId() : null,
  258.             "object_mission_display"    =>    $mission !== null $mission->getRef() : null,
  259.             
  260.             "society_group"                =>    $societyGroup,
  261.             "society"                    =>    $society,
  262.             "info"                        =>    $loggingData['info'],
  263.             "special_author"            =>    $loggingData['special_author'],
  264.         );
  265.         return $args;
  266.     }
  267.     // When Missions are created the corresponding Mission ID is not available
  268.     // So set it in the postFlush event
  269.     public function handleMission($log$object)
  270.     {
  271.         if ($object instanceof Mission)
  272.         {
  273.             if (empty($log->getObjectMissionId()))
  274.             {
  275.                 $log->setObjectMissionId($object->getId());
  276.                 return true;
  277.             }
  278.         }
  279.         return false;
  280.     }
  281. }