src/Logging/Activity/AttachmentLog.php line 28

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/AttachmentLog.php
  4. // Plan.io Task #3922
  5. // Attachments are like Installments
  6. // We log on the main object, not the Attachment object itself
  7. //    => They require a DeletionContextLogger when deleted
  8. //----------------------------------------------------------------------
  9. namespace App\Logging\Activity;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\Access;
  12. use App\Entity\Common\Attachment;
  13. use App\Logging\DeletionContextLogger;
  14. use App\Logging\Tools;
  15. use App\Services\Location\AddressTools;
  16. use App\Services\LogTools;
  17. class AttachmentLog
  18. {
  19.     private array $pendingLogArgs = [];
  20.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsDeletionContextLogger $contextLoggerTools $tools)
  21.     {
  22.         $this->em $doctrine->getManager();
  23.         $this->logTools $logTools;
  24.         $this->contextLogger $contextLogger;
  25.         $this->tools $tools;
  26.     }
  27.     public function logCreation(Attachment $attachment)
  28.     {
  29.         $pendingLogArgs = [];
  30.         //----------------------------------------------------------------------
  31.         // Fetch eventual info stored in the object
  32.         $loggingData $this->logTools->handleLoggingData($attachment);
  33.         $info $loggingData['info'];
  34.         $specialAuthor $loggingData['special_author'];
  35.         $ignore $loggingData['ignore'];
  36.         if ($ignore) return $pendingLogArgs;
  37.         //----------------------------------------------------------------------
  38.         $objectArgs $this->extractData($attachment);
  39.         if (!empty($objectArgs))
  40.         {
  41.             // Fill the common arguments
  42.             $args $this->initArgs($objectArgs$info$specialAuthor);
  43.             // Fill the specific arguments
  44.             $args["new_value"]    = $attachment->getClientOriginalName();
  45.             $pendingLogArgs[] = $args;
  46.             return $pendingLogArgs;
  47.         }
  48.         // Handle Ikea
  49.         if (!empty($attachment->getIkeaOrderNumber()))
  50.         {
  51.             // Created by Ikea => $specialAuthor is set (ikea_service_order)
  52.             // Created by Rekapp user => $specialAuthor is not set
  53.             $orderNumber $attachment->getIkeaOrderNumber();
  54.             $ikeaAction "ikea_service_order_add_attachment";
  55.             $ikeaArgs $this->tools->handleIkeaServiceOrder($orderNumber$attachment$ikeaAction$info$specialAuthor);
  56.             foreach ($ikeaArgs as $argsItem)
  57.             {
  58.                 $pendingLogArgs[] = $argsItem;
  59.             }
  60.             return $pendingLogArgs;
  61.         }
  62.         return [];
  63.     }
  64.     public function logChanges(Attachment $attachment$changes)
  65.     {
  66.         // Only for Ikea
  67.         $pendingLogArgs = [];
  68.         //----------------------------------------------------------------------
  69.         // Fetch eventual info stored in the object
  70.         $loggingData $this->logTools->handleLoggingData($attachment);
  71.         $info $loggingData['info'];
  72.         $specialAuthor $loggingData['special_author'];
  73.         $ignore $loggingData['ignore'];
  74.         if ($ignore) return $pendingLogArgs;
  75.         //----------------------------------------------------------------------
  76.         foreach ($changes as $key => $change)
  77.         {
  78.             $before $change[0];
  79.             $after $change[1];
  80.             if ($key == "ikeaSent")
  81.             {
  82.                 $ikeaAction "ikea_service_order_attachment_sent_to_ikea";
  83.                 $ikeaArgs $this->tools->handleIkeaServiceOrder($after$attachment$ikeaAction$info$specialAuthor);
  84.                 foreach ($ikeaArgs as $argsItem)
  85.                 {
  86.                     $pendingLogArgs[] = $argsItem;
  87.                 }
  88.                 continue;
  89.             }
  90.         }
  91.         return $pendingLogArgs;
  92.     }
  93.     public function logRemoval(Attachment $attachment)
  94.     {
  95.         $pendingLogArgs = [];
  96.         //----------------------------------------------------------------------
  97.         // Fetch eventual info stored in the object
  98.         $loggingData $this->logTools->handleLoggingData($attachment);
  99.         $info $loggingData['info'];
  100.         $specialAuthor $loggingData['special_author'];
  101.         $ignore $loggingData['ignore'];
  102.         if ($ignore) return $pendingLogArgs;
  103.         //----------------------------------------------------------------------
  104.         // [later.edit] I am not sure that Attachments can be linked to both Missions and Service Orders
  105.         // but keep this, it works just fine
  106.         // Ikea is a special case
  107.         // Attachments can be added to Missions
  108.         //    - in this case attachment->getMission() !== null
  109.         // Attachments can be also linked to (multiple) Ikea Service Orders
  110.         //    - in this case attachment->getIkeaOrderNumber() !== null
  111.         // Handle normal cases
  112.         // (but leave the possibility of simultaneously handling Ikea cases)
  113.         $cachedData $this->contextLogger->getContext($attachment);
  114.         if ($cachedData !== null && !empty($cachedData))
  115.         {
  116.             $objectArgs $this->extractDataFromCache($cachedData);
  117.             if (!empty($objectArgs))
  118.             {
  119.                 // Fill the common arguments
  120.                 $args $this->initArgs($objectArgs$info$specialAuthor);
  121.                 // Fill the specific arguments
  122.                 $args["old_value"]    = $attachment->getClientOriginalName();
  123.                 $pendingLogArgs[] = $args;
  124.             }
  125.         }
  126.         // At this point $pendingLogArgs may already hold some data
  127.         // ... or not :)
  128.         // Handle Ikea
  129.         if (!empty($attachment->getIkeaOrderNumber()))
  130.         {
  131.             $orderNumber $attachment->getIkeaOrderNumber();
  132.             $ikeaAction "ikea_service_order_remove_attachment";
  133.             $ikeaArgs $this->tools->handleIkeaServiceOrder($orderNumber$attachment$ikeaAction$info$specialAuthor);
  134.             foreach ($ikeaArgs as $argsItem)
  135.             {
  136.                 $pendingLogArgs[] = $argsItem;
  137.             }
  138.         }
  139.         return $pendingLogArgs;
  140.     }
  141.     private function extractData(Attachment $attachment)
  142.     {
  143.         $args = [];
  144.         if ($attachment->getApplication() !== null)
  145.         {
  146.             $args['action'] = "application_add_attachment";
  147.             $args['object'] = $attachment->getApplication();
  148.             $args['objectClient'] = null;
  149.             $args['objectEntity'] = "Application";
  150.             $args['objectBundle'] = "HR";
  151.             $args['objectMission'] = null;
  152.             $args['objectHumanResource'] = null;
  153.             return $args;
  154.         }
  155.         if ($attachment->getClient() !== null)
  156.         {
  157.             $args['objectClient'] = $attachment->getClient();
  158.             $args['objectBundle'] = "Client";            
  159.             if ($attachment->getClient()->isIndividual())
  160.             {
  161.                 $args['action'] = "individual_add_attachment";
  162.                 $args['object'] = $attachment->getClient()->getIndividual();
  163.                 $args['objectEntity'] = "Individual";
  164.             }
  165.             else
  166.             {
  167.                 $args['action'] = "store_add_attachment";
  168.                 $args['object'] = $attachment->getClient()->getStore();
  169.                 $args['objectEntity'] = "Store";
  170.             }            
  171.             
  172.             $args['objectMission'] = null;
  173.             $args['objectHumanResource'] = null;
  174.             return $args;
  175.         }
  176.         if ($attachment->getCommand() !== null)
  177.         {
  178.             $args['action'] = "command_add_attachment";
  179.             $args['object'] = $attachment->getCommand();
  180.             $args['objectClient'] = $attachment->getCommand()->getReceiver();
  181.             $args['objectEntity'] = "Command";
  182.             $args['objectBundle'] = "Platform";
  183.             $args['objectMission'] = null;
  184.             $args['objectHumanResource'] = null;
  185.             return $args;
  186.         }
  187.         if ($attachment->getCost() !== null)
  188.         {
  189.             $args['action'] = "cost_add_attachment";
  190.             $args['object'] = $attachment->getCost();
  191.             $args['objectClient'] = null;
  192.             $args['objectEntity'] = "Cost";
  193.             $args['objectBundle'] = "Platform";
  194.             $args['objectMission'] = null;
  195.             $args['objectHumanResource'] = null;
  196.             return $args;
  197.         }
  198.         if ($attachment->getDamage() !== null)
  199.         {
  200.             $object $attachment->getDamage()->getEquipment();
  201.             if ($object !== null)
  202.             {
  203.                 if ($object->getVehicle() !== null)
  204.                 {
  205.                     $args['action'] = "vehicle_damage_add_attachment";
  206.                     $args['object'] = $object->getVehicle();
  207.                     $args['objectEntity'] = "Vehicle";
  208.                 }
  209.                 else
  210.                 {
  211.                     $args['action'] = "equipment_damage_add_attachment";
  212.                     $args['object'] = $object;
  213.                     $args['objectEntity'] = "Equipment";
  214.                 }
  215.                 $args['objectClient'] = null;
  216.                 $args['objectBundle'] = "Equipment";
  217.                 $args['objectMission'] = null;
  218.                 $args['objectHumanResource'] = $object->getHumanResource();
  219.                 return $args;
  220.             }
  221.         }
  222.         if ($attachment->getDevis() !== null)
  223.         {
  224.             $args['action'] = "devis_add_attachment";
  225.             $args['object'] = $attachment->getDevis();
  226.             $args['objectClient'] = $attachment->getDevis()->getReceiver();
  227.             $args['objectEntity'] = "Devis";
  228.             $args['objectBundle'] = "Platform";
  229.             $args['objectMission'] = $attachment->getDevis()->getMission();
  230.             return $args;
  231.         }
  232.         if ($attachment->getEquipment() !== null)
  233.         {
  234.             if ($attachment->getEquipment()->getVehicle() !== null)
  235.             {
  236.                 $args['action'] = "vehicle_equipment_add_attachment";
  237.                 $args['object'] = $attachment->getEquipment()->getVehicle();
  238.                 $args['objectEntity'] = "Vehicle";
  239.             }
  240.             else
  241.             {
  242.                 $args['action'] = "equipment_add_attachment";
  243.                 $args['object'] = $attachment->getEquipment();
  244.                 $args['objectEntity'] = "Equipment";
  245.             }
  246.             $args['objectClient'] = null;
  247.             $args['objectBundle'] = "Equipment";
  248.             $args['objectMission'] = null;
  249.             $args['objectHumanResource'] = $attachment->getEquipment()->getHumanResource();
  250.             return $args;
  251.         }
  252.         // TODO #3922 : Handle sharing in the future
  253.         if ($attachment->getMission() !== null)
  254.         {
  255.             $args['action'] = "mission_add_attachment";
  256.             $args['object'] = $attachment->getMission();
  257.             $args['objectClient'] = $attachment->getMission()->getReceiver();
  258.             $args['objectEntity'] = "Mission";
  259.             $args['objectBundle'] = "Mission";
  260.             $args['objectMission'] = $attachment->getMission();
  261.             $args['objectHumanResource'] = null;
  262.             return $args;
  263.         }
  264.         if ($attachment->getSupplier() !== null)
  265.         {
  266.             $args['action'] = "supplier_add_attachment";
  267.             $args['object'] = $attachment->getSupplier();
  268.             $args['objectClient'] = null;
  269.             $args['objectEntity'] = "Supplier";
  270.             $args['objectBundle'] = "Platform";
  271.             $args['objectMission'] = null;
  272.             $args['objectHumanResource'] = null;
  273.             return $args;
  274.         }
  275.         if ($attachment->getVehicleMaintenance() !== null)
  276.         {
  277.             if ($attachment->getVehicleMaintenance()->getVehicle() !== null)
  278.             {
  279.                 $args['action'] = "vehicle_maintenance_add_attachment";
  280.                 $args['object'] = $attachment->getVehicleMaintenance()->getVehicle();
  281.                 $args['objectClient'] = null;
  282.                 $args['objectEntity'] = "Vehicle";
  283.                 $args['objectBundle'] = "Equipment";
  284.                 $args['objectMission'] = null;
  285.                 $args['objectHumanResource'] = null;
  286.                 return $args;
  287.             }
  288.         }
  289.         return [];
  290.     }
  291.     private function extractDataFromCache($cachedData)
  292.     {
  293.         // TODO #3922
  294.         // Devis and Client can co-exist - How to implement this ?
  295.         $args = [];
  296.         if (array_key_exists('application'$cachedData) && $cachedData['application'] !== null)
  297.         {
  298.             $object $cachedData['application'];
  299.             $args['action'] = "application_remove_attachment";
  300.             $args['object'] = $object;
  301.             $args['objectClient'] = null;
  302.             $args['objectEntity'] = "Application";
  303.             $args['objectBundle'] = "HR";
  304.             $args['objectMission'] = null;
  305.             $args['objectHumanResource'] = null;
  306.             return $args;
  307.         }
  308.         if (array_key_exists('client'$cachedData) && $cachedData['client'] !== null)
  309.         {
  310.             $client $cachedData['client'];
  311.             $args['objectClient'] = $client;
  312.             $args['objectBundle'] = "Client";
  313.             if ($client->isIndividual())
  314.             {
  315.                 $args['action'] = "individual_remove_attachment";
  316.                 $args['object'] = $client->getIndividual();
  317.                 $args['objectEntity'] = "Individual";
  318.             }
  319.             else
  320.             {
  321.                 $args['action'] = "store_remove_attachment";
  322.                 $args['object'] = $client->getStore();
  323.                 $args['objectEntity'] = "Store";
  324.             }
  325.             $args['objectMission'] = null;
  326.             $args['objectHumanResource'] = null;
  327.             return $args;
  328.         }
  329.         if (array_key_exists('command'$cachedData) && $cachedData['command'] !== null)
  330.         {
  331.             $object $cachedData['command'];
  332.             $args['action'] = "command_remove_attachment";
  333.             $args['object'] = $object;
  334.             $args['objectClient'] = $object->getReceiver();
  335.             $args['objectEntity'] = "Command";
  336.             $args['objectBundle'] = "Platform";
  337.             $args['objectMission'] = null;
  338.             $args['objectHumanResource'] = null;
  339.             return $args;
  340.         }
  341.         if (array_key_exists('cost'$cachedData) && $cachedData['cost'] !== null)
  342.         {
  343.             $object $cachedData['cost'];
  344.             $args['action'] = "cost_remove_attachment";
  345.             $args['object'] = $object;
  346.             $args['objectClient'] = null;
  347.             $args['objectEntity'] = "Cost";
  348.             $args['objectBundle'] = "Platform";
  349.             $args['objectMission'] = null;
  350.             $args['objectHumanResource'] = null;
  351.             return $args;
  352.         }
  353.         if (array_key_exists('damage'$cachedData) && $cachedData['damage'] !== null)
  354.         {
  355.             $object $cachedData['damage']->getEquipment();
  356.             if ($object !== null)
  357.             {
  358.                 if ($object->getVehicle() !== null)
  359.                 {
  360.                     $args['action'] = "vehicle_damage_remove_attachment";
  361.                     $args['object'] = $object->getVehicle();
  362.                     $args['objectEntity'] = "Vehicle";
  363.                 }
  364.                 else
  365.                 {
  366.                     $args['action'] = "equipment_damage_remove_attachment";
  367.                     $args['object'] = $object;
  368.                     $args['objectEntity'] = "Equipment";
  369.                 }
  370.                 $args['objectClient'] = null;
  371.                 $args['objectBundle'] = "Equipment";
  372.                 $args['objectMission'] = null;
  373.                 $args['objectHumanResource'] = $object->getHumanResource();
  374.                 return $args;
  375.             }
  376.         }
  377.         if (array_key_exists('devis'$cachedData) && $cachedData['devis'] !== null)
  378.         {
  379.             $object $cachedData['devis'];
  380.             $args['action'] = "devis_remove_attachment";
  381.             $args['object'] = $object;
  382.             $args['objectClient'] = $object->getReceiver();
  383.             $args['objectEntity'] = "Devis";
  384.             $args['objectBundle'] = "Platform";
  385.             $args['objectMission'] = $object->getMission();
  386.             $args['objectHumanResource'] = null;
  387.             return $args;
  388.         }
  389.         if (array_key_exists('equipment'$cachedData) && $cachedData['equipment'] !== null)
  390.         {
  391.             if ($cachedData['equipment']->getVehicle() !== null)
  392.             {
  393.                 $object $cachedData['equipment']->getVehicle();
  394.                 $args['action'] = "vehicle_equipment_remove_attachment";
  395.                 $args['object'] = $object;
  396.                 $args['objectEntity'] = "Vehicle";
  397.             }
  398.             else
  399.             {
  400.                 $object $cachedData['equipment'];
  401.                 $args['action'] = "equipment_remove_attachment";
  402.                 $args['object'] = $object;
  403.                 $args['objectEntity'] = "Equipment";
  404.             }
  405.             $args['objectClient'] = null;
  406.             $args['objectBundle'] = "Equipment";
  407.             $args['objectMission'] = null;
  408.             $args['objectHumanResource'] = $cachedData['equipment']->getHumanResource();
  409.             return $args;
  410.         }
  411.         // TODO #3922 : Handle sharing in the future
  412.         if (array_key_exists('mission'$cachedData) && $cachedData['mission'] !== null)
  413.         {
  414.             $object $cachedData['mission'];
  415.             $args['action'] = "mission_remove_attachment";
  416.             $args['object'] = $object;
  417.             $args['objectClient'] = $object->getReceiver();
  418.             $args['objectEntity'] = "Mission";
  419.             $args['objectBundle'] = "Mission";
  420.             $args['objectMission'] = $object;
  421.             return $args;
  422.         }
  423.         if (array_key_exists('supplier'$cachedData) && $cachedData['supplier'] !== null)
  424.         {
  425.             $object $cachedData['supplier'];
  426.             $args['action'] = "supplier_remove_attachment";
  427.             $args['object'] = $object;
  428.             $args['objectClient'] = null;
  429.             $args['objectEntity'] = "Supplier";
  430.             $args['objectBundle'] = "Platform";
  431.             $args['objectMission'] = null;
  432.             $args['objectHumanResource'] = null;
  433.             return $args;
  434.         }
  435.         if (array_key_exists('vehicle_maintenance'$cachedData) && $cachedData['vehicle_maintenance'] !== null)
  436.         {
  437.             $object $cachedData['vehicle_maintenance']->getVehicle();
  438.             if ($object !== null)
  439.             {
  440.                 $args['action'] = "vehicle_maintenance_remove_attachment";
  441.                 $args['object'] = $object;
  442.                 $args['objectClient'] = null;
  443.                 $args['objectEntity'] = "Vehicle";
  444.                 $args['objectBundle'] = "Equipment";
  445.                 $args['objectMission'] = null;
  446.                 $args['objectHumanResource'] = null;
  447.                 return $args;
  448.             }
  449.         }
  450.         return [];
  451.     }
  452.     private function initArgs($objectArgs$info$specialAuthor)
  453.     {
  454.         $object $objectArgs['object'];
  455.         $objectClient null;
  456.         $objectMission null;
  457.         $objectHumanResource null;
  458.         if (array_key_exists('objectClient'$objectArgs))
  459.         {
  460.             $objectClient $objectArgs['objectClient'];
  461.         }
  462.         if (array_key_exists('objectMission'$objectArgs))
  463.         {
  464.             $objectMission $objectArgs['objectMission'];
  465.         }
  466.         if (array_key_exists('objectHumanResource'$objectArgs))
  467.         {
  468.             $objectHumanResource $objectArgs['objectHumanResource'];
  469.         }
  470.         $society null;
  471.         $societyGroup null;
  472.         if (method_exists($object"getSociety"))
  473.         {
  474.             $society $object->getSociety();
  475.         }
  476.         if (method_exists($object"getSocietyGroup"))
  477.         {
  478.             $societyGroup $object->getSocietyGroup();
  479.         }
  480.         else
  481.         {
  482.             if ($society !== null)
  483.             {
  484.                 $societyGroup $society->getSocietyGroup();
  485.             }
  486.         }
  487.         $args = array(
  488.             "action"                    =>    $objectArgs['action'],
  489.             "object_id"                    =>    $object->getId(),
  490.             "object_bundle"                =>    $objectArgs['objectBundle'],
  491.             "object_entity"                =>    $objectArgs['objectEntity'],
  492.             "object_display"            =>    $object->display(),
  493.             "object_client_id"            =>    $objectClient !== null $objectClient->getId() : null,
  494.             "object_client_display"        =>    $objectClient !== null $objectClient->getName() : null,
  495.             "object_mission_id"            =>    $objectMission !== null $objectMission->getId() : null,
  496.             "object_mission_display"    =>    $objectMission !== null $objectMission->getRef() : null,
  497.             "object_human_resource_id"        =>    $objectHumanResource !== null $objectHumanResource->getId() : null,
  498.             "object_human_resource_display"    =>    $objectHumanResource !== null $objectHumanResource->display() : null,
  499.             
  500.             "info"                        =>    $info,
  501.             "society_group"                =>    $societyGroup,
  502.             "society"                    =>    $society,
  503.             "special_author"            =>    $specialAuthor,
  504.         );
  505.         return $args;
  506.     }
  507. }