src/Event/EasyAdminSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Entity\Algorithm;
  4. use App\Entity\AlgorithmCondition;
  5. use App\Entity\App;
  6. use App\Entity\AppTranslations;
  7. use App\Entity\Category;
  8. use App\Entity\Disease;
  9. use App\Entity\Drug;
  10. use App\Entity\Flow;
  11. use App\Entity\Import;
  12. use App\Entity\Language;
  13. use App\Entity\ObjectType;
  14. use App\Entity\Page;
  15. use App\Entity\Test;
  16. use App\Entity\TestDefinition;
  17. use App\Entity\Therapy;
  18. use App\Entity\User;
  19. use App\Service\AlgorithmService;
  20. use App\Service\ImportService;
  21. use App\Service\PageGeneratorService;
  22. use App\Service\SlugService;
  23. use App\Service\TestService;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\EventDispatcher\GenericEvent;
  28. use Symfony\Component\Filesystem\Filesystem;
  29. use Symfony\Component\HttpFoundation\RequestStack;
  30. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  31. use Symfony\Component\HttpKernel\KernelInterface;
  32. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  33. class EasyAdminSubscriber implements EventSubscriberInterface {
  34.     private $entity_manager;
  35.     public function __constructEntityManagerInterface $entity_managerAlgorithmService $algorithmServiceTokenStorageInterface $token_storageSlugService $slugServiceTestService $testServiceKernelInterface $kernelImportService $importServiceRequestStack $requestSessionInterface $sessionPageGeneratorService $pageGeneratorService) {
  36.         $this->entity_manager $entity_manager;
  37.         $this->token_storage $token_storage;
  38.         $this->testService $testService;
  39.         $this->kernel $kernel;
  40.         $this->importService $importService;
  41.         $this->request $request;
  42.         $this->session $session;
  43.         $this->pageGeneratorService $pageGeneratorService;
  44.         $this->slugService $slugService;
  45.         $this->algorithmService $algorithmService;
  46.     }
  47.     public static function getSubscribedEvents(): ?array {
  48.         return [
  49.             EasyAdminEvents::POST_UPDATE => 'onPostUpdate',
  50.             EasyAdminEvents::POST_PERSIST => 'onPostPersist',
  51.             EasyAdminEvents::PRE_PERSIST => 'onPrePersist',
  52.             EasyAdminEvents::PRE_UPDATE => 'onPreUpdate',
  53.             EasyAdminEvents::POST_EDIT => 'onPostEdit',
  54.                 ];
  55.     }
  56.     public function onPreUpdate(GenericEvent $event) {
  57.         $entity $event->getSubject();
  58.         if ($entity instanceof App) {
  59.             $id $entity->getId();
  60.             // query manager to get the old entity, before update
  61.             $qb $this->entity_manager->createQueryBuilder();
  62.             $qb->select('a.Settings')
  63.                 ->from('App:App''a')
  64.                 ->where('a.id = :id')->setParameter('id'$id);
  65.             $qb->setMaxResults(1);
  66.             $oldSettingsVar $qb->getQuery()->getSingleResult();
  67.             $oldSettings $oldSettingsVar['Settings'];
  68.             $Settings $entity->getSettings();
  69.             $primaryColor $Settings['primaryColor'];
  70.             $oldPrimaryColor = isset($oldSettings['primaryColor']) ? $oldSettings['primaryColor'] : '';
  71.             $secondaryColor $Settings['secondaryColor'];
  72.             $oldSecondaryColor = isset($oldSettings['secondaryColor']) ? $oldSettings['secondaryColor'] : '';
  73.             $infoColor $Settings['infoColor'];
  74.             $oldInfoColor = isset($oldSettings['infoColor']) ? $oldSettings['infoColor'] : '';
  75.             $successColor $Settings['successColor'];
  76.             $oldSuccessColor = isset($oldSettings['successColor']) ? $oldSettings['successColor'] : '';
  77.             $dangerColor $Settings['dangerColor'];
  78.             $oldDangerColor = isset($oldSettings['dangerColor']) ? $oldSettings['dangerColor'] : '';
  79.             $warningColor $Settings['warningColor'];
  80.             $oldWarningColor = isset($oldSettings['warningColor']) ? $oldSettings['warningColor'] : '';
  81.             $request $this->request->getCurrentRequest();
  82.             $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  83.             $output null;
  84.             if (($primaryColor!=$oldPrimaryColor) || ($secondaryColor!=$oldSecondaryColor) || ($infoColor!=$oldInfoColor) || ($successColor!=$oldSuccessColor) || ($dangerColor!=$oldDangerColor) || ($warningColor!=$oldWarningColor)) { // if colors changes update the css
  85.                 $output $this->pageGeneratorService->compileCss($entity);
  86.                 $Settings['cssUpdatedAt'] = time();
  87.             }
  88.             $this->pageGeneratorService->storeCustomCssJs($entity);
  89.             $Web $entity->getWeb();
  90.             if ($Web) {
  91.                 $apiUserId =  isset($Settings['ApiUser']) ? $Settings['ApiUser'] : 0;
  92.                 $User $this->entity_manager->getRepository(User::class)->find($apiUserId);
  93.                 if (!empty($User->getApiToken())) {
  94.                     $webapp $this->pageGeneratorService->updateWebApp($entity$User->getApiToken());
  95.                     //dd($webapp);
  96.                 }
  97.             }
  98.        }
  99.     }
  100.     public function onPostEdit(GenericEvent $event) {
  101.         $token $this->token_storage->getToken();
  102.         $User $token->getUser();
  103.         $Customer $User->getCustomer();
  104.         $entity $event->getSubject();
  105.         $entityName = (isset($entity['name'])) ? $entity['name']  : null;
  106.         if ($entityName == 'Algorithm') {
  107.             $request $this->request->getCurrentRequest();
  108.             $id $request->query->get('id');
  109.             $Algorithm $this->entity_manager->getRepository(Algorithm::class)->find($id);
  110.             $errors $this->algorithmService->checkAlgorithm($Algorithm$Customer);
  111.             if (!empty($errors)) {
  112.                 foreach ($errors as $error) {
  113.                     $this->session->getFlashBag()->add('danger',$error);
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     public function onPrePersist(GenericEvent $event) {
  119.         $entity $event->getSubject();
  120.         if ($entity instanceof TestDefinition) {
  121.         }
  122.     }
  123.     public function onPostPersist(GenericEvent $event) {
  124.         $entity $event->getSubject();
  125.         $token $this->token_storage->getToken();
  126.         $User $token->getUser();
  127.         $Customer $User->getCustomer();
  128.         $lang $Customer->getDefaultLanguageCode();
  129.         if ($entity instanceof Page) {
  130.             // save tree
  131.             $parent_id=$entity->getParent();
  132.             $Parent = ($parent_id>0) ? $this->entity_manager->getRepository(Page::class)->find($parent_id) : null;
  133.             if (!empty($Parent)) {
  134.                 $entity->setChildNodeOf($Parent);
  135.                 $this->entity_manager->persist($Parent);
  136.                 $this->entity_manager->flush();
  137.             } else {
  138.                 $entity->setMaterializedPath('');
  139.             }
  140.             $translations $entity->getTranslations();
  141.             $imagesArray preg_match_all('/<img[^>]+>/i',$entity->get$result);
  142.             $this->entity_manager->persist($entity);
  143.             $this->entity_manager->flush();
  144.         }
  145.         if ($entity instanceof Algorithm) {
  146.             $algorithmType $entity->getType();
  147.             $algorithmObjectType  $entity->getObjectType();
  148.             if (!empty($algorithmObjectType)) {
  149.                 $Taxonomy $algorithmObjectType->getSlug();
  150.                 if ($algorithmType == "modifier") {
  151.                     $ObjectId $entity->getObjectId();
  152.                     if (($Taxonomy == "test") && ($ObjectId 0)) {
  153.                         $Test $this->entity_manager->getRepository(Test::class)->find($ObjectId);
  154.                         $tesDefinitions $this->testService->getAllVariables($Test);
  155.                         foreach ($tesDefinitions as $tesDefinition) {
  156.                             $algorithmCondition = new AlgorithmCondition();
  157.                             $algorithmCondition->setFormula($tesDefinition->getSlug());
  158.                             $algorithmCondition->setActive(true);
  159.                             $algorithmCondition->setObjectId($tesDefinition->getId());
  160.                             $entity->addAlgorithmCondition($algorithmCondition);
  161.                         }
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.     }
  167.     public function onPostUpdate(GenericEvent $event) {
  168.         $entity $event->getSubject();
  169.         $token $this->token_storage->getToken();
  170.         $User $token->getUser();
  171.         $Customer $User->getCustomer();
  172.         $lang $Customer->getDefaultLanguageCode();
  173.         if ($entity instanceof Category) {
  174.             $this->entity_manager->persist($entity);
  175.             $this->entity_manager->flush();
  176.         }
  177.         if ($entity instanceof Import) {
  178.             $projectRoot $this->kernel->getProjectDir();
  179.             $fileName urldecode($entity->getFilename());
  180.             $Taxonomy=$entity->getTaxonomy();
  181.             $Worksheet=$entity->getWorksheet();
  182.             $ObjectId=$entity->getObjectType()->getId();
  183.             $taxonomyName $Taxonomy->getSlug();
  184.             $fullPath $projectRoot.'/public'.$fileName;
  185.             $importSettings $entity->getSettings();
  186.             $importStatus $entity->getStatus();
  187.             if (file_exists($fullPath)) {
  188.                 if ($taxonomyName == 'country') {
  189.                     $str file_get_contents($fullPath);
  190.                     $this->importService->importCountriesJson($str$ObjectId);
  191.                     $entity->setStatus('finished');
  192.                     $importSettings = array('stage'=>2);
  193.                     $entity->setSettings($importSettings);
  194.                     $this->entity_manager->persist($entity);
  195.                     $this->entity_manager->flush();
  196.                 } elseif ($taxonomyName == 'test') {
  197.                     $stage = (isset($importSettings['stage'])) ? $importSettings['stage'] : 1;
  198.                     $startFrom = (isset($importSettings['startFrom'])) ? $importSettings['startFrom'] : 0;
  199.                     if (($importStatus=='initial') && (empty($Worksheet))) {
  200.                         $worksheets $this->importService->importIcd9Xls($fullPath$stage$ObjectId0,1000$Taxonomy$Worksheet);
  201.                         $importSettings = array('stage'=>1'worksheet'=>'''worksheets'=>$worksheets);
  202.                         $entity->setSettings($importSettings);
  203.                     } elseif (!empty($Worksheet)) {
  204.                         $stage=2;
  205.                         $importSettings['stage'] = $stage;
  206.                         $importSettings['worksheet'] = $Worksheet;
  207.                         $importSettings['ObjectId'] = $ObjectId;
  208.                         $entity->setStatus('finished');
  209.                         $entity->setSettings($importSettings);
  210.                         $imported $this->importService->importIcd9Xls($fullPath$stage$ObjectId$startFrom,1000$Taxonomy,$Worksheet);
  211.                     }
  212.                     $this->entity_manager->persist($entity);
  213.                     $this->entity_manager->flush();
  214.                 } elseif ($taxonomyName == 'disease') {
  215.                     $stage = (isset($importSettings['stage'])) ? $importSettings['stage'] : 1;
  216.                     $startFrom = (isset($importSettings['startFrom'])) ? $importSettings['startFrom'] : 0;
  217.                     $imported $this->importService->importIcd10($fullPath$ObjectId$startFrom50);
  218.                     $stage=1;
  219.                     $importSettings['stage'] = $stage;
  220.                     $importSettings['ObjectId'] = $ObjectId;
  221.                     $importSettings['imported'] = $imported;
  222.                     $importSettings['startFrom'] = $startFrom 50;
  223.                     if ($imported == 51) {
  224.                         $entity->setStatus('partial');
  225.                     } else {
  226.                         $entity->setStatus('finished');
  227.                     }
  228.                     $entity->setStatus('finished');
  229.                     $entity->setSettings($importSettings);
  230.                     $this->entity_manager->persist($entity);
  231.                     $this->entity_manager->flush();
  232.                 } elseif ($taxonomyName == 'drug') {
  233.                     $stage = (isset($importSettings['stage'])) ? $importSettings['stage'] : 1;
  234.                     $startFrom = (isset($importSettings['startFrom'])) ? $importSettings['startFrom'] : 0;
  235.                     if (($importStatus=='initial') && (empty($Worksheet))) {
  236.                         $worksheets $this->importService->importAtcXls($fullPath$stage$ObjectId01000$Taxonomy'');
  237.                         $importSettings = array('stage'=>1'worksheet'=>'''worksheets'=>$worksheets);
  238.                         $entity->setSettings($importSettings);
  239.                     } elseif (!empty($Worksheet)) {
  240.                         $stage 2;
  241.                         $importSettings['stage'] = $stage;
  242.                         $importSettings['worksheet'] = $Worksheet;
  243.                         $importSettings['ObjectId'] = $ObjectId;
  244.                         if ($ObjectId==10) {
  245.                             $imported $this->importService->importAtcXls($fullPath$stage$ObjectId$startFrom1000$Taxonomy$Worksheet);
  246.                         } elseif ($ObjectId == 11) {
  247.                             $imported $this->importService->importRplXls($fullPath$stage$ObjectId$startFrom1000$Taxonomy$Worksheet);
  248.                         }
  249.                         $importSettings['imported'] = $imported;
  250.                         $importSettings['startFrom'] = $startFrom 1000;
  251.                         if ($imported == 1001) {
  252.                             $entity->setStatus('partial');
  253.                         } else {
  254.                             $entity->setStatus('finished');
  255.                         }
  256.                         $entity->setSettings($importSettings);
  257.                     }
  258.                     $this->entity_manager->persist($entity);
  259.                     $this->entity_manager->flush();
  260.                 } elseif ($taxonomyName == 'page') {
  261.                 } elseif ($taxonomyName == 'procedure') {
  262.                     $stage = (isset($importSettings['stage'])) ? $importSettings['stage'] : 1;
  263.                     $startFrom = (isset($importSettings['startFrom'])) ? $importSettings['startFrom'] : 0;
  264.                     if (($importStatus=='initial') && (empty($Worksheet))) {
  265.                         $worksheets $this->importService->importIcd9Xls($fullPath$stage$ObjectId,0,1000,$Taxonomy'');
  266.                         $importSettings = array('stage'=>1'worksheet'=>'''worksheets'=>$worksheets);
  267.                         $entity->setSettings($importSettings);
  268.                     } elseif (!empty($Worksheet)) {
  269.                         $stage 2;
  270.                         $importSettings['stage'] = $stage;
  271.                         $importSettings['worksheet'] = $Worksheet;
  272.                         $importSettings['ObjectId'] = $ObjectId;
  273.                         $imported $this->importService->importIcd9Xls($fullPath$stage$ObjectId$startFrom1000,$Taxonomy$Worksheet);
  274.                         $importSettings['imported'] = $imported;
  275.                         $importSettings['startFrom'] = $startFrom 1000;
  276.                         if ($imported == 1001) {
  277.                             $entity->setStatus('partial');
  278.                         } else {
  279.                             $entity->setStatus('finished');
  280.                         }
  281.                         $entity->setSettings($importSettings);
  282.                     }
  283.                     $this->entity_manager->persist($entity);
  284.                     $this->entity_manager->flush();
  285.                 }
  286.             }
  287.         }
  288.         if ($entity instanceof TestDefinition) {
  289.             $this->entity_manager->persist($entity);
  290.             $this->entity_manager->flush();
  291.         }
  292.         if ($entity instanceof Test) {
  293.             //$entity->generateSlug();
  294.             $this->entity_manager->persist($entity);
  295.             $this->entity_manager->flush();
  296.         }
  297.         if ($entity instanceof Drug) {
  298.             $name $entity->translate($lang)->getName();
  299.             $slug $this->slugService->generateSlug($name);
  300.             $entity->setSlug($slug);
  301.             $this->entity_manager->persist($entity);
  302.             $this->entity_manager->flush();
  303.         }
  304.         if ($entity instanceof Disease) {
  305.             $entity->generateSlug();
  306.             $this->entity_manager->persist($entity);
  307.             $this->entity_manager->flush();
  308.         }
  309.         if ($entity instanceof Flow) {
  310.             $entity->generateSlug();
  311.             $this->entity_manager->persist($entity);
  312.             $this->entity_manager->flush();
  313.         }
  314.         if ($entity instanceof Page) {
  315.             $projectRoot $this->kernel->getProjectDir();
  316.             $translations $entity->getTranslations();
  317.             $request $this->request->getCurrentRequest();
  318.             $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  319.             foreach ($translations as $translation) { // get array of missing files
  320.                 $missingFilesArray $this->pageGeneratorService->getAttachmentsFromTranslation($entity$translationtrue);
  321.             }
  322.             $missingFiles implode(', ',$missingFilesArray);
  323.             $message 'There are missing files in the page body: '.$missingFiles;
  324.             if (!empty($missingFilesArray)) {
  325.                 $this->session->getFlashBag()->add('danger',$message);
  326.             }
  327.             $now = new \DateTime('now');
  328.             $entity->setUpdatedAt($now);
  329.             $this->entity_manager->persist($entity);
  330.             $this->entity_manager->flush();
  331.             // Update images / attachments array for current page and  whole app files array
  332.             $App $entity->getApp();
  333.             $AppSettings $App->getSettings();
  334.             $languages = isset($AppSettings['languages']) ? $AppSettings['languages'] : array();
  335.             $appFilesArray = array();
  336.             foreach ($languages as $language_id) {
  337.                 $language $this->entity_manager->getRepository(Language::class)->find($language_id);
  338.                 $appFilesArray[$language->getCode()]['images'] = array();
  339.                 $appFilesArray[$language->getCode()]['attachments'] = array();
  340.             }
  341.             if (empty($appFilesArray)) {
  342.                 $appFilesArray['en']['images'] = array();
  343.                 $appFilesArray['en']['attachments'] = array();
  344.             }
  345.             $pages $this->entity_manager->getRepository(Page::class)->findBy(array('App'=>$App));
  346.             // update pages
  347.             foreach ($pages as $page) {
  348.                 $translations $page->getTranslations();
  349.                 foreach ($translations as $translation) {
  350.                     $pageLocale $translation->getLocale();
  351.                     $translation $this->pageGeneratorService->getAttachmentsFromTranslation($page$translationfalse);
  352.                     $pageFiles $translation->getFiles();
  353.                     $pageImagesArray $pageFiles['images'];
  354.                     $pageAttachmentsArray $pageFiles['attachments'];
  355.                     if (isset($appFilesArray[$pageLocale]))  {
  356.                         if (!empty($pageImagesArray)) {
  357.                             $appFilesArray[$pageLocale]['images'][] = $pageImagesArray;
  358.                         }
  359.                         if (!empty($pageAttachmentsArray)) {
  360.                             $appFilesArray[$pageLocale]['attachments'][] = $pageAttachmentsArray;
  361.                         }
  362.                     }
  363.                 }
  364.                 $this->entity_manager->persist($page);
  365.                 $this->entity_manager->flush();
  366.             }
  367.             foreach ($appFilesArray as $locale=>$filesArray) { // rearrange array, make files unique
  368.                $imagesArray $filesArray['images'];
  369.                $attachmentsArray $filesArray['attachments'];
  370.                $newImagesArray = array();
  371.                foreach ($imagesArray as $value1) { // merge arrays from different pages
  372.                    $newImagesArray array_merge($newImagesArray$value1);
  373.                }
  374.                 $newImagesArray1 = array();
  375.                 foreach ($newImagesArray as $value2) {// create array with paths as keys to make unique data
  376.                     if (is_array($value2)) {
  377.                         $newImagesArray1[$value2['path']] = array('time'=>$value2['time'], 'size'=> $value2['size']);
  378.                     }
  379.                 }
  380.                 $newImagesArray2 = array();
  381.                 foreach ($newImagesArray1 as $imagePath=>$imageData) { // loop through imagesArray1 to make path/time keys back
  382.                     $newImagesArray2[] = array('path'=>(!empty($imagePath)) ? $baseurl.$imagePath '''time'=>$imageData['time'], 'size'=>$imageData['size']);
  383.                 }
  384.                 
  385.                 /// attachments
  386.                 $newattachmentsArray = array();
  387.                 foreach ($attachmentsArray as $value1) { // merge arrays from different pages
  388.                     $newattachmentsArray array_merge($newattachmentsArray$value1);
  389.                 }
  390.                 $newattachmentsArray1 = array();
  391.                 foreach ($newattachmentsArray as $value2) {// create array with paths as keys to make unique data
  392.                     if (is_array($value2)) {
  393.                          $newattachmentsArray1[$value2['path']] = array('time'=>$value2['time'], 'size'=> $value2['size'] );
  394.                     }
  395.                 }
  396.                 $newattachmentsArray2 = array();
  397.                 foreach ($newattachmentsArray1 as $imagePath=>$imageData) { // loop through attachmentsArray1 to make path/time keys back
  398.                     $newattachmentsArray2[] = array('path'=>(!empty($imagePath)) ? $baseurl.$imagePath '''time'=>$imageData['time'], 'size'=>$imageData['size']);
  399.                 }
  400.                 $webFiles $this->pageGeneratorService->returnPageFilesArray($App$baseurl); // get design files for App
  401.                 $appFilesArray[$locale] = array('images'=>$newImagesArray2'attachments'=>$newattachmentsArray2'webdesign'=>$webFiles);
  402.             }
  403.             $App->setFiles($appFilesArray);
  404.             $this->entity_manager->persist($App);
  405.             $this->entity_manager->flush();
  406.         }
  407.         if ($entity instanceof App) {
  408.             // update translations
  409.             $appTranslations $entity->getTranslations();
  410.             $Settings $entity->getSettings();
  411.             $selectedObjectsIds = isset($Settings['ObjectTypes']) ? $Settings['ObjectTypes'] : [];
  412.             $defaultSearchObjectType = isset($Settings['defaultSearchObjectType']) ? $Settings['defaultSearchObjectType'] : null;
  413.             foreach ($appTranslations as $appTranslation) {
  414.                 $appLocaleSettings $appTranslation->getSettings();
  415.                 $locale $appTranslation->getLocale();
  416.                 $objectTypesAray = array();
  417.                 foreach($selectedObjectsIds as $selectedObjectId) {
  418.                     $ObjectType $this->entity_manager->getRepository(ObjectType::class)->find($selectedObjectId);
  419.                     $objectTypeName $ObjectType->translate($locale)->getName();
  420.                     $objectTypesAray[$selectedObjectId] = array(
  421.                         'name'=>$objectTypeName,
  422.                         'selected'=> ($defaultSearchObjectType == $selectedObjectId) ? true false,
  423.                         'taxonomy'=>$ObjectType->getTaxonomy()->getId());
  424.                 }
  425.                 $translations $this->entity_manager->getRepository(AppTranslations::class)->findByTranslationKey('search_form'$entity);
  426.                 foreach ($translations as $translation) {
  427.                     $translatsionsArray[$translation->getTranslationKey()] = $translation->translate($locale)->getName();
  428.                 }
  429.                 $appLocaleSettings['searchObjectTypes'] = $objectTypesAray;
  430.                 $appLocaleSettings['searchFormTranslations'] = $translatsionsArray;
  431.                 $appTranslation->setSettings($appLocaleSettings);
  432.             }
  433.             // update google assetlinks.json and apple app-site-association.json
  434.             $projectRoot $this->kernel->getProjectDir();
  435.             $storePath $projectRoot.'/public';
  436.             $apps $this->entity_manager->getRepository(App::class)->findAll();
  437.             $appleSiteAssociationArray = array('applinks'=>array('details'=>null));
  438.             $googleAssetlinksArray = array();
  439.             foreach ($apps as $App) {
  440.                 $appSettings $App->getSettings();
  441.                 $appleTeamId = (isset($appSettings['appleTeamId'])) ? $appSettings['appleTeamId'] : '';
  442.                 $appleBundleId = (isset($appSettings['appleBundleId'])) ? $appSettings['appleBundleId'] : '';
  443.                 $googleCertFingerprint = (isset($appSettings['googleCertFingerprint'])) ? $appSettings['googleCertFingerprint'] : '';
  444.                 $googlePackageName = (isset($appSettings['googlePackageName'])) ? $appSettings['googlePackageName'] : '';
  445.                 if (stristr($googleCertFingerprint',')) {
  446.                     $googleCertFingerprint str_replace('"','',$googleCertFingerprint);
  447.                     $googleCertFingerprint preg_replace('/[\r\n\s]+/'''$googleCertFingerprint);
  448.                     $certFingerprintArray explode(',',$googleCertFingerprint);
  449.                 } else {
  450.                     $certFingerprintArray = array($googleCertFingerprint);
  451.                 }
  452.                 if (!empty($appleTeamId) && !empty($appleBundleId)) {
  453.                     $appleSingleSiteAssociationArray = array(
  454.                         'appID'=>$appleTeamId.'.'.$appleBundleId,
  455.                         'components'=>array(array('/'=>'/r/'.$App->getId().'/*',
  456.                                             'comment'=>'comment'))
  457.                     );
  458.                     $appleSiteAssociationArray['applinks']['details'][] = $appleSingleSiteAssociationArray;
  459.                 }
  460.                 if (!empty($googleCertFingerprint) && !empty($googlePackageName)) {
  461.                     $googleSingleSiteAssociationArray = array(
  462.                         'relation'=>array('delegate_permission/common.handle_all_urls'),
  463.                         'target'=>array('namespace'=>'android_app',
  464.                                         'package_name'=>$googlePackageName,
  465.                                         'sha256_cert_fingerprints'=>$certFingerprintArray)
  466.                     );
  467.                     $googleAssetlinksArray[] = $googleSingleSiteAssociationArray;
  468.                 }
  469.             }
  470.             $googleAssetlinksJson json_encode($googleAssetlinksArrayJSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  471.             $appleSiteAssociationJson json_encode($appleSiteAssociationArrayJSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  472.             $filesystem = new Filesystem();
  473.             $filesystem->dumpFile($storePath.'/.well-known/assetlinks.json'$googleAssetlinksJson);
  474.             $filesystem->dumpFile($storePath.'/.well-known/apple-app-site-association'$appleSiteAssociationJson);
  475.             $this->entity_manager->persist($entity);
  476.             $entity->mergeNewTranslations();
  477.             $this->entity_manager->flush();
  478.         }
  479.         if ($entity instanceof Algorithm) {
  480.             $algorithmType $entity->getType();
  481.             $algorithmObjectType  $entity->getObjectType();
  482.             if (!empty($algorithmObjectType)) {
  483.                 $Taxonomy $algorithmObjectType->getSlug();
  484.                 if ($algorithmType == "modifier") {
  485.                     $ObjectId $entity->getObjectId();
  486.                     if ($ObjectId 0) {
  487.                         if ($Taxonomy == "test")  {
  488.                             $Test $this->entity_manager->getRepository(Test::class)->find($ObjectId);
  489.                             $algorithmConditionRepository $this->entity_manager->getRepository(AlgorithmCondition::class);
  490.                             $testDefinitions $this->testService->getAllVariables($Test);
  491.                             $newAlgorithmConditionsArray = array();
  492.                             foreach ($testDefinitions as $testDefinition) {
  493.                                 $algorithmCondition = new AlgorithmCondition();
  494.                                 $algorithmCondition->setFormula($testDefinition->getSlug());
  495.                                 $algorithmCondition->setActive(true);
  496.                                 $algorithmCondition->setObjectId($testDefinition->getId());
  497.                                 $testDefinitionId $testDefinition->getId();
  498.                                 $storedCondition $algorithmConditionRepository->findOneBy(array('Algorithm' => $entity->getId(), 'ObjectId' => $testDefinitionId));
  499.                                 if (empty($storedCondition)) {
  500.                                     $entity->addAlgorithmCondition($algorithmCondition);
  501.                                 }
  502.                                 array_push($newAlgorithmConditionsArray$testDefinitionId); // create array for all new algorithm conditions
  503.                             }
  504.                             $StoredConditions $entity->getAlgorithmConditions();
  505.                             foreach ($StoredConditions as $storedCondition) {
  506.                                 $storedConditionId $storedCondition->getObjectId();
  507.                                 if (!in_array($storedConditionId$newAlgorithmConditionsArray)) { // check if stored conditions are in the $newAlgorithmConditionsArray array. If no, remove them.
  508.                                     $entity->removeAlgorithmCondition($storedCondition);
  509.                                 }
  510.                             }
  511.                             $this->entity_manager->persist($entity);
  512.                             $this->entity_manager->flush();
  513.                         } elseif ($Taxonomy == "therapy")  {
  514.                             $Therapy $this->entity_manager->getRepository(Therapy::class)->find($ObjectId);
  515.                             $algorithmConditionRepository $this->entity_manager->getRepository(AlgorithmCondition::class);
  516.                             $therapyDefinitions $Therapy->getTherapyDefinitions();
  517.                             $newAlgorithmConditionsArray = array();
  518.                             foreach ($therapyDefinitions as $therapyDefinition) {
  519.                                 $algorithmCondition = new AlgorithmCondition();
  520.                                 $algorithmCondition->setFormula('');
  521.                                 $algorithmCondition->setActive(false);
  522.                                 $algorithmCondition->setObjectId($therapyDefinition->getId());
  523.                                 $therapyDefinitionId $therapyDefinition->getId();
  524.                                 $storedCondition $algorithmConditionRepository->findOneBy(array('Algorithm' => $entity->getId(), 'ObjectId' => $therapyDefinitionId));
  525.                                 if (empty($storedCondition)) {
  526.                                     $entity->addAlgorithmCondition($algorithmCondition);
  527.                                 }
  528.                                 array_push($newAlgorithmConditionsArray$therapyDefinitionId); // create array for all new algorithm conditions
  529.                             }
  530.                         }
  531.                         $StoredConditions $entity->getAlgorithmConditions();
  532.                         foreach ($StoredConditions as $storedCondition) {
  533.                             $storedConditionId $storedCondition->getObjectId();
  534.                             if (!in_array($storedConditionId$newAlgorithmConditionsArray)) { // check if stored conditions are in the $newAlgorithmConditionsArray array. If no, remove them.
  535.                                 $entity->removeAlgorithmCondition($storedCondition);
  536.                             }
  537.                         }
  538.                         $this->entity_manager->persist($entity);
  539.                         $this->entity_manager->flush();
  540.                     }
  541.                 } elseif ($algorithmType == "scoring") {
  542.                     $Settings $entity->getSettings();
  543.                     $ObjectIds $entity->getObjectIds();
  544.                     if (empty($ObjectIds)) { // remove conditions if objectsids is empty
  545.                         $StoredConditions $entity->getAlgorithmConditions();
  546.                         foreach ($StoredConditions as $storedCondition) {
  547.                             $entity->removeAlgorithmCondition($storedCondition);
  548.                         }
  549.                         $this->entity_manager->persist($entity);
  550.                         $this->entity_manager->flush();
  551.                     }
  552.                     $SimpleScoring = isset($Settings['SimpleScore']) ? $Settings['SimpleScore'] : false;
  553.                     if ($SimpleScoring == false) {
  554.                         $StoredConditions $entity->getAlgorithmConditions();
  555.                         foreach ($StoredConditions as $storedCondition) {
  556.                             $storedCondition->setScore('');
  557.                         }
  558.                         $this->entity_manager->persist($entity);
  559.                         $this->entity_manager->flush();
  560.                     }
  561.                 }
  562.             }
  563.         }
  564.     }
  565. }