src/Logging/Tools.php line 30

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/Tools.php
  4. // Plan.io Task #3922
  5. //----------------------------------------------------------------------
  6. namespace App\Logging;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\PropertyAccess\PropertyAccess;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use App\Entity\Common\Attachment;
  11. use App\Entity\Ikea\ServiceOrder;
  12. use App\Entity\Ikea\Template as IkeaTemplate;
  13. use App\Entity\Platform\Devis\Devis;
  14. use App\Entity\Platform\Invoice\Invoice;
  15. use App\Entity\ProjectManager\Blueprint;
  16. use App\Entity\ProjectManager\ProjectNotebookItem;
  17. use App\Entity\Webapp\Document;
  18. use App\Logging\DeletionContextLogger;
  19. use App\Services\LogTools;
  20. class Tools
  21. {
  22.     public function __construct(ManagerRegistry $doctrineTranslatorInterface $translatorLogTools $logToolsDeletionContextLogger $contextLogger)
  23.     {
  24.         $this->em $doctrine->getManager();
  25.         $this->translator $translator;
  26.         $this->logTools $logTools;
  27.         $this->contextLogger $contextLogger;
  28.     }
  29.     public function handleBasicChanges($args$before$after)
  30.     {
  31.         $args["old_value"] = $before;
  32.         $args["new_value"] = $after;
  33.         return $args;
  34.     }
  35.     public function handleBasicBoolChanges($args$before$after)
  36.     {
  37.         if ($before && !$after)
  38.         {
  39.             $args["old_value"] = null;
  40.             $args["new_value"] = $this->translator->trans("no");
  41.         }
  42.         if (!$before && $after)
  43.         {
  44.             $args["old_value"] = null;
  45.             $args["new_value"] = $this->translator->trans("yes");
  46.         }
  47.         return $args;
  48.     }
  49.     public function handleLabelChanges($args$before$after)
  50.     {
  51.         $args["old_value"] = null;
  52.         $args["new_value"] = null;
  53.         if ($before !== null$args["old_value"] = $before->getValue();
  54.         if ($after !== null$args["new_value"]    = $after->getValue();
  55.         return $args;
  56.     }
  57.     public function handleObjectChanges($args$before$after)
  58.     {
  59.         $args["old_value"] = null;
  60.         $args["new_value"] = null;
  61.         if ($before !== null)
  62.         {
  63.             if (method_exists($before"getLogLabel"))
  64.             {
  65.                 $args["old_value"] = $before->getLogLabel();
  66.             }
  67.             else
  68.             {
  69.                 $args["old_value"] = $before->display();
  70.             }
  71.         }
  72.         if ($after !== null)
  73.         {
  74.             if (method_exists($after"getLogLabel"))
  75.             {
  76.                 $args["new_value"]    = $after->getLogLabel();
  77.             }
  78.             else
  79.             {
  80.                 $args["new_value"]    = $after->display();
  81.             }
  82.         }
  83.         return $args;
  84.     }
  85.     public function handleAddressChanges($args$before$after)
  86.     {
  87.         $args["old_value"] = null;
  88.         $args["new_value"] = null;
  89.         if ($before === null && $after === null)
  90.         {
  91.             return $args;
  92.         }
  93.         if ($before !== null && $after !== null)
  94.         {
  95.             if ($before->differsInValue($after))
  96.             {
  97.                 $args["old_value"] = $before->display();
  98.                 $args["new_value"] = $after->display();
  99.                 return $args;
  100.             }
  101.         }
  102.         else
  103.         {
  104.             // before or after is null, but not both
  105.             if ($before !== null$args["old_value"] = $before->display();
  106.             if ($after !== null$args["new_value"] = $after->display();
  107.             return $args;
  108.         }
  109.         return $args;
  110.     }
  111.     public function handleDateChanges($args$before$after$displayHours false)
  112.     {
  113.         $testFormat "Ymd";
  114.         if ($displayHours$testFormat "YmdHi";
  115.         $displayFormat "d/m/Y";
  116.         if ($displayHours$displayFormat "d/m/Y H:i";
  117.         $args["old_value"] = null;
  118.         $args["new_value"] = null;
  119.         if ($before === null && $after === null)
  120.         {
  121.             return $args;
  122.         }
  123.         if ($before !== null && $after !== null)
  124.         {
  125.             if ($before->format($testFormat) != $after->format($testFormat))
  126.             {
  127.                 $args["old_value"] = $before->format($displayFormat);
  128.                 $args["new_value"] = $after->format($displayFormat);
  129.                 return $args;
  130.             }
  131.         }
  132.         else
  133.         {
  134.             // before or after is null, but not both
  135.             if ($before !== null)
  136.             {
  137.                 $args["old_value"] = $before->format($displayFormat);
  138.             }
  139.             if ($after !== null)
  140.             {
  141.                 $args["new_value"] = $after->format($displayFormat);
  142.             }
  143.             return $args;
  144.         }
  145.         
  146.         return null;
  147.     }
  148.     public function handleIkeaServiceOrder($OSNumber$object$action$info$specialAuthor)
  149.     {
  150.         if (!($object instanceof Attachment) &&
  151.             !($object instanceof Devis) &&
  152.             !($object instanceof Invoice) &&
  153.             !($object instanceof Document))
  154.         {
  155.             return [];
  156.         }
  157.         $allArgs = array();
  158.         $societyGroup $object->getSocietyGroup();
  159.         $serviceOrders $this->em->getRepository(ServiceOrder::class)->findBy(array(
  160.             'orderNumber'        =>    $OSNumber,
  161.             'societyGroup'        =>    $societyGroup,
  162.         ));
  163.         foreach ($serviceOrders as $serviceOrder)
  164.         {
  165.             $client $serviceOrder->getClient();
  166.             $society $serviceOrder->getSociety();
  167.             $args = array(
  168.                 "action"                =>    $action,
  169.                 "object_id"                =>    $serviceOrder->getId(),
  170.                 "object_bundle"            =>    "Ikea",
  171.                 "object_entity"            =>    "ServiceOrder",
  172.                 "object_display"        =>    $serviceOrder->display(),
  173.                 "object_client_id"        =>    $client !== null $client->getId() : null,
  174.                 "object_client_display"    =>    $client !== null $client->getName() : null,
  175.                 "old_value"                =>    $object->display(),
  176.                 "new_value"                =>    $object->display(),
  177.                 "info"                    =>    $info,
  178.                 "special_author"        =>    $specialAuthor,
  179.                 "society_group"            =>    $serviceOrder->getSocietyGroup(),
  180.                 "society"                =>    $society !== null $society null,
  181.             );
  182.             $allArgs[] = $args;
  183.         }
  184.         return $allArgs;
  185.     }
  186.     public function handleProjectNotebookItemAdd($args$notebookItem)
  187.     {
  188.         $type $notebookItem->getItemType();        
  189.         switch ($type
  190.         {
  191.             case ProjectNotebookItem::type_number:
  192.             {
  193.                 $args["new_value"] = $notebookItem->getItemData();
  194.                 return $args;
  195.                 // break;
  196.             }    
  197.             case ProjectNotebookItem::type_text:
  198.             {                
  199.                 $args["new_value"] = $notebookItem->getItemData();
  200.                 return $args;
  201.                 // break;
  202.             }                
  203.             case ProjectNotebookItem::type_textarea:
  204.             {
  205.                 $args["new_value"] = $notebookItem->getItemData();
  206.                 return $args;
  207.                 // break;
  208.             }                
  209.             case ProjectNotebookItem::type_checkbox:
  210.             {
  211.                 // Checkboxes are created with itemData = 1
  212.                 // Since we are creating them when we are first checking them
  213.                 $args["new_value"] = $this->translator->trans("yes");
  214.         
  215.                 return $args;
  216.                 // break;
  217.             }                
  218.             case ProjectNotebookItem::type_select_multiple:
  219.             {
  220.                 // Checkboxes are created with itemData = 1
  221.                 // Since we are creating them when we are first checking them
  222.                 $args["new_value"] = $this->translator->trans("yes");
  223.         
  224.                 return $args;
  225.                 // break;
  226.             }                
  227.             case ProjectNotebookItem::type_select:
  228.             {
  229.                 // Handle Special Cases
  230.                 if ($notebookItem->isSupportsWallSquareness())
  231.                 {
  232.                     // In this case both $before and $after are arrays of option item codes
  233.                     // The 1st item in the array is the line number                    
  234.                     $afterItems json_decode($notebookItem->getItemData(), true);
  235.                     
  236.                     $newValue "";
  237.                     if (!empty($afterItems) && is_array($afterItems))
  238.                     {
  239.                         foreach ($afterItems as $key => $iCode
  240.                         {
  241.                             if ($key == 0)
  242.                             {
  243.                                 $newValue $iCode;
  244.                             }
  245.                             else
  246.                             {
  247.                                 $i Blueprint::getItemForCode($iCode);
  248.                                 if ($i !== null)
  249.                                 {
  250.                                     $newValue .= " | ";
  251.                                     $newValue .= $i['label'];
  252.                                 }
  253.                             }
  254.                         }
  255.                     }
  256.                     $args["new_value"] = $newValue;
  257.                     return $args;
  258.                 }
  259.                 // In this case both $before and $after are option item codes                
  260.                 $afterItem Blueprint::getItemForCode($notebookItem->getItemData());
  261.                 if ($afterItem !== null)
  262.                 {
  263.                     $args["new_value"] = $afterItem['label'];
  264.                 }                
  265.                 return $args;
  266.                 // break;
  267.             }                
  268.             
  269.             default:
  270.             {
  271.                 // This code should not be reached
  272.                 $args["old_value"] = null;
  273.                 $args["new_value"] = null;
  274.                 return $args;
  275.                 // break;
  276.             }
  277.         }
  278.     }
  279.     public function handleProjectNotebookItemUpdate($args$notebookItem$before$after)
  280.     {
  281.         $type $notebookItem->getItemType();                
  282.         switch ($type
  283.         {
  284.             case ProjectNotebookItem::type_number:
  285.             {
  286.                 $args["old_value"] = $before;
  287.                 $args["new_value"] = $after;
  288.                 return $args;
  289.                 // break;
  290.             }                
  291.             case ProjectNotebookItem::type_text:
  292.             {
  293.                 $args["old_value"] = $before;
  294.                 $args["new_value"] = $after;
  295.                 return $args;
  296.                 // break;
  297.             }                
  298.             case ProjectNotebookItem::type_textarea:
  299.             {
  300.                 $args["old_value"] = $before;
  301.                 $args["new_value"] = $after;
  302.                 return $args;
  303.                 // break;
  304.             }                
  305.             case ProjectNotebookItem::type_checkbox:
  306.             {
  307.                 if ($before && !$after)
  308.                 {
  309.                     $args["old_value"] = null;
  310.                     $args["new_value"] = $this->translator->trans("no");
  311.                 }
  312.                 if (!$before && $after)
  313.                 {
  314.                     $args["old_value"] = null;
  315.                     $args["new_value"] = $this->translator->trans("yes");
  316.                 }
  317.         
  318.                 return $args;
  319.                 // break;
  320.             }                
  321.             case ProjectNotebookItem::type_select_multiple:
  322.             {
  323.                 if ($before && !$after)
  324.                 {
  325.                     $args["old_value"] = null;
  326.                     $args["new_value"] = $this->translator->trans("no");
  327.                 }
  328.                 if (!$before && $after)
  329.                 {
  330.                     $args["old_value"] = null;
  331.                     $args["new_value"] = $this->translator->trans("yes");
  332.                 }
  333.         
  334.                 return $args;
  335.                 // break;
  336.             }                
  337.             case ProjectNotebookItem::type_select:
  338.             {
  339.                 // Handle Special Cases
  340.                 if ($notebookItem->isSupportsWallSquareness() || $notebookItem->isAppliancesWater())
  341.                 {
  342.                     // In this case both $before and $after are arrays of option item codes
  343.                     // The 1st item in the array is the line number
  344.                     $beforeItems json_decode($beforetrue);
  345.                     $afterItems json_decode($aftertrue);
  346.                     $oldValue "";
  347.                     $newValue "";
  348.                     if (!empty($beforeItems) && is_array($beforeItems))
  349.                     {
  350.                         foreach ($beforeItems as $key => $iCode
  351.                         {
  352.                             if ($key == 0)
  353.                             {
  354.                                 $oldValue $iCode;
  355.                             }
  356.                             else
  357.                             {
  358.                                 $i Blueprint::getItemForCode($iCode);
  359.                                 if ($i !== null)
  360.                                 {
  361.                                     $oldValue .= " | ";
  362.                                     $oldValue .= $i['label'];
  363.                                 }                                
  364.                             }
  365.                         }
  366.                     }
  367.                     if (!empty($afterItems) && is_array($afterItems))
  368.                     {
  369.                         foreach ($afterItems as $key => $iCode
  370.                         {
  371.                             if ($key == 0)
  372.                             {
  373.                                 $newValue $iCode;
  374.                             }
  375.                             else
  376.                             {
  377.                                 $i Blueprint::getItemForCode($iCode);
  378.                                 if ($i !== null)
  379.                                 {
  380.                                     $newValue .= " | ";
  381.                                     $newValue .= $i['label'];
  382.                                 }
  383.                             }
  384.                         }
  385.                     }
  386.                     $args["old_value"] = $oldValue;
  387.                     $args["new_value"] = $newValue;
  388.                     return $args;
  389.                 }
  390.                 // In this case both $before and $after are option item codes
  391.                 $beforeItem Blueprint::getItemForCode($before);
  392.                 $afterItem Blueprint::getItemForCode($after);
  393.         
  394.                 $args["old_value"] = $beforeItem['label'];
  395.                 $args["new_value"] = $afterItem['label'];
  396.                 return $args;
  397.                 // break;
  398.             }                
  399.             
  400.             default:
  401.             {
  402.                 // This code should not be reached
  403.                 $args["old_value"] = null;
  404.                 $args["new_value"] = null;
  405.                 return $args;
  406.                 // break;
  407.             }
  408.         }
  409.     }
  410.     public function handleProjectNotebookIkeaMethodVersion($args$before$after)
  411.     {
  412.         $args["old_value"] = null;
  413.         $args["new_value"] = null;
  414.         if ($before == IkeaTemplate::code_custom)
  415.         {
  416.             $args["old_value"] = $this->translator->trans('ikea_template_method_custom');
  417.         }
  418.         elseif ($before == IkeaTemplate::code_complete)
  419.         {
  420.             $args["old_value"] = $this->translator->trans('ikea_template_method_complete');
  421.         }
  422.         if ($after == IkeaTemplate::code_custom)
  423.         {
  424.             $args["new_value"] = $this->translator->trans('ikea_template_method_custom');
  425.         }
  426.         elseif ($after == IkeaTemplate::code_complete)
  427.         {
  428.             $args["new_value"] = $this->translator->trans('ikea_template_method_complete');
  429.         }
  430.         $this->logTools->ploopLog("[handleProjectNotebookIkeaMethodVersion]");
  431.         $this->logTools->ploopLog("[handleProjectNotebookIkeaMethodVersion] before = ".$args["old_value"]);
  432.         $this->logTools->ploopLog("[handleProjectNotebookIkeaMethodVersion] after = ".$args["new_value"]);
  433.         return $args;
  434.     }
  435. }