custom/plugins/EconsorCrowdfunding/src/EconsorCrowdfunding.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace EconsorCrowdfunding;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  6. use Shopware\Core\Content\Media\DataAbstractionLayer\MediaFolderRepositoryDecorator;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  13. use Shopware\Core\Framework\Plugin;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Symfony\Component\Config\Definition\Exception\Exception;
  19. /**
  20.  * Class EconsorCrowdfunding
  21.  * @package EconsorCrowdfunding
  22.  */
  23. class EconsorCrowdfunding extends Plugin
  24. {
  25.     /**
  26.      * @param InstallContext $installContext
  27.      */
  28.     public function install(InstallContext $installContext): void
  29.     {
  30.         parent::install($installContext);
  31.         $configService $this->container->get(SystemConfigService::class);
  32.         $this->mediaFolderFunction(true'Econsor Crowdfunding Images''image');
  33.         $this->mediaFolderFunction(true'Econsor Crowdfunding Backgrounds''background');
  34.         $this->removeProducts();
  35.         $this->deleteMails();
  36.         $this->createProducts();
  37.         $this->createMails($installContext$configService);
  38.     }
  39.     /**
  40.      * @param UninstallContext $context
  41.      */
  42.     public function uninstall(UninstallContext $context): void
  43.     {
  44.         parent::uninstall($context);
  45.         $configService $this->container->get(SystemConfigService::class);
  46.         $productRepository $this->container->get('product.repository');
  47.         //$productRepository->delete([['id' => $configService->get('EconsorCrowdfunding.config.feeID')]], Context::createDefaultContext());
  48.         //$productRepository->delete([['id' => $configService->get('EconsorCrowdfunding.config.donationID')]], Context::createDefaultContext());
  49.         //$productRepository->delete([['id' => $configService->get('EconsorCrowdfunding.config.goodieID')]], Context::createDefaultContext());
  50.         $this->deleteMails();
  51.         if ($context->keepUserData()) {
  52.             return;
  53.         }
  54.         $this->removeMigrations();
  55.         $connection $this->container->get(Connection::class);
  56.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_orderhistory`');
  57.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_order_bundles`');
  58.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_sponsor_translation`');
  59.         $connection->executeQuery('DROP TABLE IF EXISTS `product_sponsors`');
  60.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_goodie`');
  61.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_campaign_validation`');
  62.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_order_shirt_configuration`');
  63.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_campaign`');
  64.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_own_sponsor`');
  65.         $connection->executeQuery('DROP TABLE IF EXISTS `ec_crowdfunding_sponsor`');
  66.         $this->mediaFolderFunction(false'Econsor Crowdfunding Images''image');
  67.         $this->mediaFolderFunction(false'Econsor Crowdfunding Backgrounds''background');
  68.     }
  69.     //######################################################MAILS#######################################################
  70.     /**
  71.      * @param $configService
  72.      */
  73.     private function deleteMails()
  74.     {
  75.         $ids = [
  76.             'econsor_crowdfunding_mail_success',
  77.             'econsor_crowdfunding_mail_failed',
  78.             'econsor_crowdfunding_mail_await_confirmation_admin',
  79.             'econsor_crowdfunding_mail_await_confirmation_trainer',
  80.             'econsor_crowdfunding_mail_await_confirmation_sponsor',
  81.             'econsor_crowdfunding_validate_campaign_trainer',
  82.             'econsor_crowdfunding_validate_campaign_confirmed',
  83.             'econsor_crowdfunding_validate_denied_confirmed',
  84.             'econsor_crowdfunding_validate_campaign_trainer_after_sponsor',
  85.             'econsor_crowdfunding_sponsor_confirmed',
  86.             'econsor_crowdfunding_notification_5_days_left',
  87.             'econsor_crowdfunding_notification_almost_there',
  88.             'econsor_crowdfunding_notification_halftime',
  89.             'econsor_crowdfunding_notification_goal_reached'
  90.         ];
  91.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  92.         $mailTemplateRepository $this->container->get('mail_template.repository');
  93.         $context Context::createDefaultContext();
  94.         foreach ($ids as $id) {
  95.             try {
  96.                 $c = new Criteria();
  97.                 $c->addFilter(new EqualsFilter('technicalName'$id));
  98.                 if ($mailTemplateTypeRepository->search($c$context)->getEntities()->first()) {
  99.                     $tplId $mailTemplateTypeRepository->search($c$context)->getEntities()->first()->getId();
  100.                     $cc = new Criteria();
  101.                     $cc->addFilter(new EqualsFilter('mailTemplateTypeId'$tplId));
  102.                     $ids $mailTemplateRepository->search($cc$context)->getElements();
  103.                     if ([] !== $ids) {
  104.                         foreach ($ids as $id => $array) {
  105.                             $mailTemplateRepository->delete([['id' => $id]], $context);
  106.                         }
  107.                     }
  108.                     $mailTemplateTypeRepository->delete([['id' => $tplId]], $context);
  109.                 }
  110.             } catch (\Exception $exception) {
  111.             }
  112.             continue;
  113.         }
  114.     }
  115.     /**
  116.      * @param $installContext
  117.      * @param $configService
  118.      */
  119.     private function createMails($installContext$configService)
  120.     {
  121.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  122.         $mailTemplateRepository $this->container->get('mail_template.repository');
  123.         $templates = [
  124.             'campaignSuccessMail' => [
  125.                 'type' => [
  126.                     'name' => 'CrowdFundingSuccessMail',
  127.                     'technicalName' => 'econsor_crowdfunding_mail_success',
  128.                     'availableEntities' => []
  129.                 ],
  130.                 'template' => [
  131.                     'subject' => [
  132.                         'en-GB' => 'Your Campaign was a success!',
  133.                         'de-DE' => 'Deine Kampagne war erfolgreich!'
  134.                     ],
  135.                     'contentPlain' => strip_tags(
  136.                         file_get_contents(
  137.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_success.html.twig'
  138.                         )
  139.                     ),
  140.                     'contentHtml' => file_get_contents(
  141.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_success.html.twig'
  142.                     )
  143.                 ]
  144.             ],
  145.             'campaignFailedMail' => [
  146.                 'type' => [
  147.                     'name' => 'CrowdFundingFailedMail',
  148.                     'technicalName' => 'econsor_crowdfunding_mail_failed',
  149.                     'availableEntities' => []
  150.                 ],
  151.                 'template' => [
  152.                     'subject' => [
  153.                         'en-GB' => "Your Campaign didn't quite make it!",
  154.                         'de-DE' => 'Deine Kampagne hat es leider nicht geschafft!'
  155.                     ],
  156.                     'contentPlain' => strip_tags(
  157.                         file_get_contents(
  158.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_failed.html.twig'
  159.                         )
  160.                     ),
  161.                     'contentHtml' => file_get_contents(
  162.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_failed.html.twig'
  163.                     )
  164.                 ]
  165.             ],
  166.             'campaignAwaitConfirmationAdmin' => [
  167.                 'type' => [
  168.                     'name' => 'CrowdFundingAwaitConfirmationAdmin',
  169.                     'technicalName' => 'econsor_crowdfunding_mail_await_confirmation_admin',
  170.                     'availableEntities' => []
  171.                 ],
  172.                 'template' => [
  173.                     'subject' => [
  174.                         'en-GB' => 'A new Campaign is awaiting confirmation',
  175.                         'de-DE' => 'Eine neue Kampagne erwartet freigabe!'
  176.                     ],
  177.                     'contentPlain' => strip_tags(
  178.                         file_get_contents(
  179.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_awaiting_confirmation_admin.html.twig'
  180.                         )
  181.                     ),
  182.                     'contentHtml' => file_get_contents(
  183.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_awaiting_confirmation_admin.html.twig'
  184.                     )
  185.                 ]
  186.             ],
  187.             'campaignAwaitConfirmationTrainer' => [
  188.                 'type' => [
  189.                     'name' => 'CrowdFundingAwaitConfirmationTrainer',
  190.                     'technicalName' => 'econsor_crowdfunding_mail_await_confirmation_trainer',
  191.                     'availableEntities' => []
  192.                 ],
  193.                 'template' => [
  194.                     'subject' => [
  195.                         'en-GB' => 'Your Campaign is awaiting confirmation!',
  196.                         'de-DE' => 'Ihre Kampagne erwartet momentan noch freigabe!'
  197.                     ],
  198.                     'contentPlain' => strip_tags(
  199.                         file_get_contents(
  200.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_awaiting_confirmation_trainer.html.twig'
  201.                         )
  202.                     ),
  203.                     'contentHtml' => file_get_contents(
  204.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_awaiting_confirmation_trainer.html.twig'
  205.                     )
  206.                 ]
  207.             ],
  208.             'campaignAwaitConfirmationSponsor' => [
  209.                 'type' => [
  210.                     'name' => 'CrowdFundingAwaitConfirmationSponsor',
  211.                     'technicalName' => 'econsor_crowdfunding_mail_await_confirmation_sponsor',
  212.                     'availableEntities' => []
  213.                 ],
  214.                 'template' => [
  215.                     'subject' => [
  216.                         'en-GB' => 'You were chosen as a sponsor!',
  217.                         'de-DE' => 'Sie wurden als Sponsor angegeben!'
  218.                     ],
  219.                     'contentPlain' => strip_tags(
  220.                         file_get_contents(
  221.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_awaiting_confirmation_sponsor.html.twig'
  222.                         )
  223.                     ),
  224.                     'contentHtml' => file_get_contents(
  225.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_awaiting_confirmation_sponsor.html.twig'
  226.                     )
  227.                 ]
  228.             ],
  229.             'campaignValidateCampaignTrainer' => [
  230.                 'type' => [
  231.                     'name' => 'CrowdFundingValidateCampaignTrainer',
  232.                     'technicalName' => 'econsor_crowdfunding_validate_campaign_trainer',
  233.                     'availableEntities' => []
  234.                 ],
  235.                 'template' => [
  236.                     'subject' => [
  237.                         'en-GB' => 'Your Campaign needs to be edited!',
  238.                         'de-DE' => 'Ihre Kampagne muss noch bearbeitet werden!'
  239.                     ],
  240.                     'contentPlain' => strip_tags(
  241.                         file_get_contents(
  242.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_confirm_campaign_trainer.html.twig'
  243.                         )
  244.                     ),
  245.                     'contentHtml' => file_get_contents(
  246.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_confirm_campaign_trainer.html.twig'
  247.                     )
  248.                 ]
  249.             ],
  250.             'campaignConfirmedMail' => [
  251.                 'type' => [
  252.                     'name' => 'CrowdFundingCampaignConfirmed',
  253.                     'technicalName' => 'econsor_crowdfunding_validate_campaign_confirmed',
  254.                     'availableEntities' => []
  255.                 ],
  256.                 'template' => [
  257.                     'subject' => [
  258.                         'en-GB' => 'Your Campaign was confirmed!',
  259.                         'de-DE' => 'Ihre Kampagne wurde zugelassen!'
  260.                     ],
  261.                     'contentPlain' => strip_tags(
  262.                         file_get_contents(
  263.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_confirmed_trainer.html.twig'
  264.                         )
  265.                     ),
  266.                     'contentHtml' => file_get_contents(
  267.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_confirmed_trainer.html.twig'
  268.                     )
  269.                 ]
  270.             ],
  271.             'campaignDeniedMail' => [
  272.                 'type' => [
  273.                     'name' => 'CrowdFundingCampaigDenied',
  274.                     'technicalName' => 'econsor_crowdfunding_validate_denied_confirmed',
  275.                     'availableEntities' => []
  276.                 ],
  277.                 'template' => [
  278.                     'subject' => [
  279.                         'en-GB' => 'Your Campaign was denied!',
  280.                         'de-DE' => 'Ihre Kampagne wurde nicht zugelassen!'
  281.                     ],
  282.                     'contentPlain' => strip_tags(
  283.                         file_get_contents(
  284.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_denied_trainer.html.twig'
  285.                         )
  286.                     ),
  287.                     'contentHtml' => file_get_contents(
  288.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_denied_trainer.html.twig'
  289.                     )
  290.                 ]
  291.             ],
  292.             'campaignValidateCampaignTrainerAfterSponsor' => [
  293.                 'type' => [
  294.                     'name' => 'CrowdFundingValidateCampaignTrainerAfterSponsor',
  295.                     'technicalName' => 'econsor_crowdfunding_validate_campaign_trainer_after_sponsor',
  296.                     'availableEntities' => []
  297.                 ],
  298.                 'template' => [
  299.                     'subject' => [
  300.                         'en-GB' => 'Your Campaign is awaiting confirmation!',
  301.                         'de-DE' => 'Ihre Kampagne erwartet momentan noch freigabe!'
  302.                     ],
  303.                     'contentPlain' => strip_tags(
  304.                         file_get_contents(
  305.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_confirm_campaign_trainer_after_sponsor.html.twig'
  306.                         )
  307.                     ),
  308.                     'contentHtml' => file_get_contents(
  309.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_confirm_campaign_trainer_after_sponsor.html.twig'
  310.                     )
  311.                 ]
  312.             ],
  313.             'campaignSponsorConfirmed' => [
  314.                 'type' => [
  315.                     'name' => 'CrowdFundingSponsorConfirmed',
  316.                     'technicalName' => 'econsor_crowdfunding_sponsor_confirmed',
  317.                     'availableEntities' => []
  318.                 ],
  319.                 'template' => [
  320.                     'subject' => [
  321.                         'en-GB' => 'You have confirmed your Sponsor Role!',
  322.                         'de-DE' => 'Sie haben ihre Sponsorenrolle bestätigt!'
  323.                     ],
  324.                     'contentPlain' => strip_tags(
  325.                         file_get_contents(
  326.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_sponsor_confirmed.html.twig'
  327.                         )
  328.                     ),
  329.                     'contentHtml' => file_get_contents(
  330.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_campaign_sponsor_confirmed.html.twig'
  331.                     )
  332.                 ]
  333.             ],
  334.             'notification5DaysLeft' => [
  335.                 'type' => [
  336.                     'name' => 'CrowdFundingNotification5DaysLeft',
  337.                     'technicalName' => 'econsor_crowdfunding_notification_5_days_left',
  338.                     'availableEntities' => []
  339.                 ],
  340.                 'template' => [
  341.                     'subject' => [
  342.                         'en-GB' => '5 Days left!',
  343.                         'de-DE' => 'Noch 5 Tage!'
  344.                     ],
  345.                     'contentPlain' => strip_tags(
  346.                         file_get_contents(
  347.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_5_days_left.html.twig'
  348.                         )
  349.                     ),
  350.                     'contentHtml' => file_get_contents(
  351.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_5_days_left.html.twig'
  352.                     )
  353.                 ]
  354.             ],
  355.             'notificationAlmostThere' => [
  356.                 'type' => [
  357.                     'name' => 'CrowdFundingNotificationAlmostThere',
  358.                     'technicalName' => 'econsor_crowdfunding_notification_almost_there',
  359.                     'availableEntities' => []
  360.                 ],
  361.                 'template' => [
  362.                     'subject' => [
  363.                         'en-GB' => 'Almost there!',
  364.                         'de-DE' => 'Fast geschafft!'
  365.                     ],
  366.                     'contentPlain' => strip_tags(
  367.                         file_get_contents(
  368.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_almost_there.html.twig'
  369.                         )
  370.                     ),
  371.                     'contentHtml' => file_get_contents(
  372.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_almost_there.html.twig'
  373.                     )
  374.                 ]
  375.             ],
  376.             'notificationHalftime' => [
  377.                 'type' => [
  378.                     'name' => 'CrowdFundingNotificationHalftime',
  379.                     'technicalName' => 'econsor_crowdfunding_notification_halftime',
  380.                     'availableEntities' => []
  381.                 ],
  382.                 'template' => [
  383.                     'subject' => [
  384.                         'en-GB' => 'Halftime!',
  385.                         'de-DE' => 'Halbzeit!'
  386.                     ],
  387.                     'contentPlain' => strip_tags(
  388.                         file_get_contents(
  389.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_halftime.html.twig'
  390.                         )
  391.                     ),
  392.                     'contentHtml' => file_get_contents(
  393.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_halftime.html.twig'
  394.                     )
  395.                 ]
  396.             ],
  397.             'notificationGoalReached' => [
  398.                 'type' => [
  399.                     'name' => 'CrowdFundingNotificationGoalReached',
  400.                     'technicalName' => 'econsor_crowdfunding_notification_goal_reached',
  401.                     'availableEntities' => []
  402.                 ],
  403.                 'template' => [
  404.                     'subject' => [
  405.                         'en-GB' => 'Goal reached!',
  406.                         'de-DE' => 'Ziel erreicht!'
  407.                     ],
  408.                     'contentPlain' => strip_tags(
  409.                         file_get_contents(
  410.                             __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_reached_goal.html.twig'
  411.                         )
  412.                     ),
  413.                     'contentHtml' => file_get_contents(
  414.                         __DIR__ '/Resources/custom/mail_templates/crowdfund_notifications_reached_goal.html.twig'
  415.                     )
  416.                 ]
  417.             ]
  418.         ];
  419.         try {
  420.             foreach ($templates as $configName => $data) {
  421.                 $id Uuid::randomHex();
  422.                 $this->addMailType($data['type'], $id$mailTemplateTypeRepository$installContext->getContext());
  423.                 $this->addMailTemplate($data['template'], $id$mailTemplateRepository$installContext->getContext());
  424.                 $configService->set('EconsorCrowdfunding.config.' $configName$id);
  425.             }
  426.         } catch (Exception $exception) {
  427.             $this->deleteMails($configService);
  428.             throw new Exception('Mails couldnt be added, try again'1);
  429.         }
  430.     }
  431.     /**
  432.      * @param $data
  433.      * @param $id
  434.      * @param $repo
  435.      * @param $context
  436.      */
  437.     private function addMailType($data$id$repo$context)
  438.     {
  439.         $mailTemplateType =
  440.             [
  441.                 [
  442.                     'id' => $id,
  443.                     'name' => $data['name'],
  444.                     'technicalName' => $data['technicalName'],
  445.                     'availableEntities' => $data['availableEntities']
  446.                 ]
  447.             ];
  448.         try {
  449.             $repo->create($mailTemplateType$context);
  450.         } catch (UniqueConstraintViolationException $exception) {
  451.         }
  452.     }
  453.     /**
  454.      * @param $data
  455.      * @param $id
  456.      * @param $repo
  457.      * @param $context
  458.      */
  459.     private function addMailTemplate($data$id$repo$context)
  460.     {
  461.         $mailTemplate =
  462.             [
  463.                 [
  464.                     'id' => Uuid::randomHex(),
  465.                     'mailTemplateTypeId' => $id,
  466.                     'subject' => $data['subject'],
  467.                     'contentPlain' => $data['contentPlain'],
  468.                     'contentHtml' => $data['contentHtml']
  469.                 ]
  470.             ];
  471.         try {
  472.             $repo->create($mailTemplate$context);
  473.         } catch (UniqueConstraintViolationException $exception) {
  474.         }
  475.     }
  476.     //#####################################################PRODUCTS#####################################################
  477.     private function createProducts()
  478.     {
  479.         try {
  480.             $configService $this->container->get(SystemConfigService::class);
  481.             $context Context::createDefaultContext();
  482.             $productRepository $this->container->get('product.repository');
  483.             $id $this->generate(
  484.                 [
  485.                     'price' => 39,
  486.                     'number' => 'CROWDFUNDING-FEE',
  487.                     'description' => 'Crowdfunding Gebühr',
  488.                     'name' => 'Crowdfunding Gebühr'
  489.                 ],
  490.                 $context,
  491.                 $productRepository
  492.             );
  493.             $configService->set('EconsorCrowdfunding.config.feeID'$id);
  494.             $id $this->generate(
  495.                 [
  496.                     'price' => 0,
  497.                     'number' => 'CROWDFUNDING-DONATION',
  498.                     'description' => 'Crowdfunding Spende',
  499.                     'name' => 'Crowdfunding Spende'
  500.                 ],
  501.                 $context,
  502.                 $productRepository
  503.             );
  504.             $configService->set('EconsorCrowdfunding.config.donationID'$id);
  505.             $id $this->generate(
  506.                 [
  507.                     'price' => 0,
  508.                     'number' => 'CROWDFUNDING-GOODIE',
  509.                     'description' => 'Crowdfunding Goodie',
  510.                     'name' => 'Crowdfunding Goodie'
  511.                 ],
  512.                 $context,
  513.                 $productRepository
  514.             );
  515.             $configService->set('EconsorCrowdfunding.config.goodieID'$id);
  516.         } catch (Exception $exception) {
  517.             $this->removeProducts();
  518.             throw new Exception('Products couldnt be added, try again'1);
  519.         }
  520.     }
  521.     /**
  522.      * @param $params
  523.      * @param $context
  524.      * @param $productRepository
  525.      * @return string
  526.      */
  527.     private function generate($params$context$productRepository): string
  528.     {
  529.         $id Uuid::randomHex();
  530.         $taxRepository $this->container->get('tax.repository');
  531.         $taxObj $taxRepository->search(
  532.             (new Criteria())->addSorting(new FieldSorting('taxRate'FieldSorting::DESCENDING)),
  533.             $context
  534.         )->first();
  535.         $tax $taxObj->getTaxRate();
  536.         $net $params['price'] / (+ ($tax 100));
  537.         $data = [
  538.             'id' => $id,
  539.             'productNumber' => $params['number'],
  540.             'description' => $params['description'],
  541.             'stock' => 999999,
  542.             'name' => $params['name'],
  543.             'price' => [
  544.                 [
  545.                     'linked' => false,
  546.                     'net' => (float)$net,
  547.                     'gross' => (float)$params['price'],
  548.                     'currencyId' => Defaults::CURRENCY,
  549.                 ]
  550.             ],
  551.             'tax' => [
  552.                 'id' => $taxObj->getId()
  553.             ]
  554.         ];
  555.         $productRepository->upsert([$data], $context);
  556.         $this->getVisibility($id);
  557.         return $id;
  558.     }
  559.     private function removeProducts()
  560.     {
  561.         $connection $this->container->get(Connection::class);
  562.         $connection->executeQuery("DELETE FROM product WHERE product_number = 'CROWDFUNDING-GOODIE'");
  563.         $connection->executeQuery("DELETE FROM product WHERE product_number = 'CROWDFUNDING-FEE'");
  564.         $connection->executeQuery("DELETE FROM product WHERE product_number = 'CROWDFUNDING-DONATION'");
  565.     }
  566.     /**
  567.      * @param $id
  568.      */
  569.     private function getVisibility($id)
  570.     {
  571.         $salesChannelRepository $this->container->get('sales_channel.repository');
  572.         $visibilityRepository $this->container->get('product_visibility.repository');
  573.         $context Context::createDefaultContext();
  574.         $channels $salesChannelRepository->search(new Criteria(), $context);
  575.         foreach ($channels as $index => $channel) {
  576.             $payload = [
  577.                 'id' => Uuid::randomHex(),
  578.                 'productId' => $id,
  579.                 'productVersionId' => Defaults::LIVE_VERSION,
  580.                 'salesChannelId' => $index,
  581.                 'visibility' => 30
  582.             ];
  583.             $visibilityRepository->upsert([$payload], $context);
  584.         }
  585.     }
  586.     //#######################################################REST#######################################################
  587.     /**
  588.      * @param $flag
  589.      * @param $name
  590.      * @param $folderType
  591.      */
  592.     private function mediaFolderFunction($flag$name$folderType)
  593.     {
  594.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  595.         $mediaFolderRepository $this->container->get('media_folder.repository');
  596.         $configService $this->container->get(SystemConfigService::class);
  597.         $c = new Criteria();
  598.         //$c->addFilter(new EqualsFilter('name', 'Econsor Crowdfunding Images'));
  599.         $c->addFilter(new EqualsFilter('name'$name));
  600.         $folder $mediaFolderRepository->search($cContext::createDefaultContext())->getEntities()->first();
  601.         if($flag)
  602.         {
  603.             //create
  604.             if($folder)
  605.             {
  606.                 $configService->set('EconsorCrowdfunding.config.'.$folderType.'Folder'$folder->getId());
  607.             } else {
  608.                 $id Uuid::randomHex();
  609.                 $mediaFolderRepository->create(
  610.                     [
  611.                         [
  612.                             'name' => $name,
  613.                             'configuration' => [
  614.                                 'id' => $id
  615.                             ],
  616.                             'id' => $id
  617.                         ]
  618.                     ],
  619.                     Context::createDefaultContext()
  620.                 );
  621.                 $configService->set('EconsorCrowdfunding.config.'.$folderType.'Folder'$id);
  622.             }
  623.         }
  624.         //delete
  625.         if($folder && !$flag)
  626.         {
  627.             $mediaFolderRepository $this->container->get('media_folder.repository');
  628.             $mediaFolderRepository->delete(
  629.                 [
  630.                     [
  631.                         'id' => $folder->getId()
  632.                     ]
  633.                 ],
  634.                 Context::createDefaultContext()
  635.             );
  636.         }
  637.     }
  638. }