src/Logging/Activity/IndividualLog.php line 19

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/IndividualLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Entity\AccessClient\AccessClient;
  9. use App\Entity\Client\Individual;
  10. use App\Logging\Tools;
  11. use App\Services\LogTools;
  12. class IndividualLog
  13. {
  14.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  15.     {
  16.         $this->em $doctrine->getManager();
  17.         $this->logTools $logTools;
  18.         $this->tools $tools;
  19.     }
  20.     public function logCreation(Individual $individual)
  21.     {
  22.         $pendingLogArgs = [];
  23.         //----------------------------------------------------------------------
  24.         // Fetch eventual info stored in the object
  25.         $loggingData $this->logTools->handleLoggingData($individual);
  26.         $info $loggingData['info'];
  27.         $specialAuthor $loggingData['special_author'];
  28.         $ignore $loggingData['ignore'];
  29.         if ($ignore) return $pendingLogArgs;
  30.         //----------------------------------------------------------------------
  31.         // Init base log args
  32.         // This does not contain action
  33.         $args $this->initArgs($individual$loggingData);
  34.         //----------------------------------------------------------------------
  35.         if ($individual->getClient() !== null && $individual->getClient()->isProspect())
  36.         {
  37.             $action "prospect_add";
  38.         }
  39.         else
  40.         {
  41.             $action "individual_add";
  42.         }
  43.         $args["action"] = $action;
  44.         $pendingLogArgs[] = $args;
  45.         return $pendingLogArgs;
  46.     }
  47.     public function logChanges(Individual $individual$changes)
  48.     {
  49.         $pendingLogArgs = [];
  50.         //----------------------------------------------------------------------
  51.         // Fetch eventual info stored in the object
  52.         $loggingData $this->logTools->handleLoggingData($individual);
  53.         $info $loggingData['info'];
  54.         $specialAuthor $loggingData['special_author'];
  55.         $ignore $loggingData['ignore'];
  56.         if ($ignore) return $pendingLogArgs;
  57.         //----------------------------------------------------------------------
  58.         // Init base log args
  59.         // This does not contain action
  60.         $args $this->initArgs($individual$loggingData);
  61.         //----------------------------------------------------------------------
  62.         // When merging, do not log anything on the Individual being removed
  63.         if ($loggingData['action'] == "individual_merged_and_deleted")
  64.         {
  65.             return $pendingLogArgs;
  66.         }
  67.         if ($individual->getClient() !== null && $individual->getClient()->isProspect())
  68.         {
  69.             $action "prospect_edit";
  70.             $actionChange "prospect_change";
  71.         }
  72.         else
  73.         {
  74.             $action "individual_edit";
  75.             $actionChange "individual_change";
  76.         }
  77.         $args["action"] = $action;
  78.         if ($info == 'convert_prospect')
  79.         {
  80.             $args["action"] = "prospect_convert_to_client";
  81.             $args["object_entity"] = "Client.Prospect";
  82.             $pendingLogArgs[] = $args;
  83.             return $pendingLogArgs;
  84.         }
  85.         // This is triggered when we are actually changing
  86.         // the object referenced in Devis :: $interventionAddress
  87.         $addressChanges = array(
  88.             "address",
  89.             "billingAddress",
  90.         );
  91.         $basicChanges = array(
  92.             "lastname",
  93.             "firstname",
  94.             "company",
  95.             "info",
  96.             "email",
  97.             "phone",
  98.         );
  99.         $basicBoolChanges = array(
  100.             "commercialConsent",
  101.         );
  102.         $dateChanges = array(
  103.             "rgpdConsent",
  104.             "ccdDate",
  105.         );
  106.         $labelChanges = array(
  107.             "title",
  108.             "origin",
  109.         );
  110.         // Method getLogLabel() should be defined for these objects/entities
  111.         $objectChanges = array(
  112.             "defaultStore",
  113.             "society",
  114.             "societyOwner",
  115.             "manager",
  116.             "author",
  117.         );
  118.         // See what changed and log (members first)
  119.         foreach ($changes as $key => $change)
  120.         {
  121.             $name $this->logTools->camelToSnakeCase($key);
  122.             $args["action"]    = $action."_".$name;
  123.             $before $change[0];
  124.             $after $change[1];
  125.             // Handle Merging
  126.             if ($key == "mergeData")
  127.             {
  128.                 if ($loggingData['action'] == "individual_merge")
  129.                 {
  130.                     $args["action"] = $loggingData['action'];
  131.                     $pendingLogArgs[] = $args;
  132.                     continue;
  133.                 }
  134.             }
  135.             if (in_array($key$basicChanges))
  136.             {
  137.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  138.                 continue;
  139.             }
  140.             if (in_array($key$labelChanges))
  141.             {
  142.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  143.                 continue;
  144.             }
  145.             if (in_array($key$objectChanges))
  146.             {
  147.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  148.                 continue;
  149.             }
  150.             if (in_array($key$addressChanges))
  151.             {
  152.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  153.                 continue;
  154.             }
  155.             if (in_array($key$dateChanges))
  156.             {
  157.                 // dateChanges can be null
  158.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  159.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  160.                 continue;
  161.             }
  162.             if (in_array($key$basicBoolChanges))
  163.             {
  164.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  165.                 continue;
  166.             }
  167.         }
  168.         // See what changed and log (relationships OneToMany and ManyToMany second)
  169.         if ($individual->getStores()->isDirty())
  170.         {
  171.             $old "";
  172.             foreach ($individual->getStores()->getSnapshot() as $store)
  173.             {
  174.                 $old .= $store->getName();
  175.                 $old .= ", ";
  176.             }
  177.             if ($old != "")
  178.             {
  179.                 $old substr($old0, -1);
  180.                 $old substr($old0, -1);
  181.             }
  182.             $new "";
  183.             foreach ($individual->getStores() as $store)
  184.             {
  185.                 $new .= $store->getName();
  186.                 $new .= ", ";
  187.             }
  188.             if ($new != "")
  189.             {
  190.                 $new substr($new0, -1);
  191.                 $new substr($new0, -1);
  192.             }
  193.             $args["action"]        = $action."_stores";
  194.             $args["old_value"]    = $old;
  195.             $args["new_value"]    = $new;
  196.             $pendingLogArgs[] = $args;
  197.         }
  198.         // Individual :: $address and Individual :: $billingAddress
  199.         // are handled in AddressLog
  200.         return $pendingLogArgs;
  201.     }
  202.     // Only for Merge for now
  203.     public function logRemoval(Individual $individual)
  204.     {
  205.         $pendingLogArgs = [];
  206.         //----------------------------------------------------------------------
  207.         // Fetch eventual info stored in the object
  208.         $loggingData $this->logTools->handleLoggingData($individual);
  209.         $info $loggingData['info'];
  210.         $specialAuthor $loggingData['special_author'];
  211.         $ignore $loggingData['ignore'];
  212.         if ($ignore) return $pendingLogArgs;
  213.         //----------------------------------------------------------------------
  214.         // Init base log args
  215.         // This does not contain action
  216.         $args $this->initArgs($individual$loggingData);
  217.         //----------------------------------------------------------------------
  218.         // Handle Merging
  219.         $action $loggingData['action'];
  220.         if (empty($action)) return [];
  221.         if ($action == "individual_merged_and_deleted")
  222.         {
  223.             $args["action"] = $action;
  224.             $pendingLogArgs[] = $args;
  225.             return $pendingLogArgs;
  226.         }
  227.     }
  228.     public function initArgs(Individual $individual$loggingData)
  229.     {
  230.         if ($individual->getClient() !== null && $individual->getClient()->isProspect())
  231.         {
  232.             $objectEntity "Client.Prospect";
  233.         }
  234.         else
  235.         {
  236.             $objectEntity "Individual";
  237.         }
  238.         $args = array(
  239.             "object_id"                    =>    $individual->getId(),
  240.             "object_bundle"                =>    "Platform",
  241.             "object_entity"                =>    $objectEntity,
  242.             "object_display"            =>    $individual->displayForLogging(),
  243.             "object_client_id"            =>    $individual->getClient()->getId(),
  244.             "object_client_display"        =>    $individual->displayForLogging(),
  245.             "society_group"                =>    $individual->getSociety()->getGroup(),
  246.             "society"                    =>    $individual->getSociety(),
  247.             "info"                        =>    $loggingData['info'],
  248.             "special_author"            =>    $loggingData['special_author'],
  249.         );
  250.         return $args;
  251.     }
  252.     // When Individuals are created the corresponding Client ID is not available
  253.     // So set it in the postFlush event
  254.     public function handleIndividual($log$object)
  255.     {
  256.         if ($object instanceof Individual)
  257.         {
  258.             if (empty($log->getObjectClientId()))
  259.             {
  260.                 $log->setObjectClientId($object->getClient()->getId());
  261.                 return true;
  262.             }
  263.         }
  264.         return false;
  265.     }
  266. }