custom/plugins/UltraStoreSwitcher/src/UltraStoreSwitcher.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ultra\Store\Switcher;
  3. use Shopware\Core\Framework\Plugin;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;   
  6. use Shopware\Core\Framework\Plugin\Context\ActivateContext;  
  7. use Shopware\Core\Framework\Plugin\Context\DeactivateContext
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;  
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. class UltraStoreSwitcher extends Plugin
  14. {
  15.     public function install(InstallContext $context): void
  16.     {
  17.         if (!file_exists('../public/bundles/administration/static/css/ultra-config-styler.css')) {        
  18.         copy('../custom/plugins/UltraStoreSwitcher/src/Resources/datas/ultra-config-styler.css''../public/bundles/administration/static/css/ultra-config-styler.css');
  19.         }    
  20.         
  21.         if (!file_exists('../public/bundles/administration/static/js/ultra-config-styler.js')) {        
  22.         copy('../custom/plugins/UltraStoreSwitcher/src/Resources/datas/ultra-config-styler.js''../public/bundles/administration/static/js/ultra-config-styler.js');
  23.         }
  24.         
  25.         
  26.         
  27.     $connection $this->container->get(Connection::class);
  28.        
  29.        $securing="";
  30.        $firsturl="";
  31.        $opt="";
  32.        
  33.        $resulter $connection->fetchAll('
  34.         SELECT 
  35.     sct.name, lo.code, scd.url, sc.access_key
  36. FROM
  37.     sales_channel  sc
  38. INNER JOIN
  39.     sales_channel_translation sct 
  40.     ON sct.sales_channel_id = sc.id
  41. INNER JOIN    
  42.     sales_channel_domain scd
  43.     ON sc.id = scd.sales_channel_id
  44. INNER JOIN
  45.     language la 
  46.     ON la.id = sc.language_id
  47. INNER JOIN
  48.     locale lo 
  49.     ON lo.id = la.locale_id
  50. where sct.name != "Headless"
  51. group by sc.access_key
  52. order by sct.name ASC');
  53.              
  54.        $myarray="";
  55.        
  56.        $topper='<?xml version="1.0" encoding="UTF-8"?>
  57. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.     xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/master/src/Core/System/SystemConfig/Schema/config.xsd">
  59.     <!-- Card 1. Verkaufskanal Aktivierung -->
  60.     <card>
  61.         <title>Card 1</title>
  62.         <title lang="de-DE">Card 1</title>
  63.         
  64.         <input-field type="text">
  65.             <name>eingabefeld1</name>
  66.             <class>ultra-info-start-button</class>
  67.             <placeholder>1. Activate in this saleschannel</placeholder>
  68.             <placeholder lang="de-DE">1. Aktivierung in diesem Verkaufskanal</placeholder>                 
  69.             <disabled>true</disabled>
  70.         </input-field>
  71.         <!-- Feld 1.1 Verkaufskanal Aktivierung -->
  72.         <input-field type="bool">
  73.             <name>activeKanal</name>
  74.             <label>Sales channel Active</label>
  75.             <label lang="de-DE">Verkaufskanal Aktiv</label>
  76.              <helpText>If selected, plugin will be shown in this shop or subshop.</helpText>
  77.             <helpText lang="de-DE">Wenn gewählt, wird das Plugin in diesem Verkaufskanal angezeigt.</helpText>          
  78.         </input-field>
  79.     </card>
  80.     
  81.     ';
  82.        
  83.        $optionstop '
  84.        <!-- Card 2. Verkaufskanal Auswahl -->
  85.     <card>
  86.         <title>Card 2</title>
  87.         <title lang="de-DE">Card 2</title>
  88.         
  89.         <input-field type="text">
  90.             <name>eingabefeld2</name>
  91.             <class>ultra-info-start-button</class>
  92.             <placeholder>2. Turn on flag(s)</placeholder>
  93.             <placeholder lang="de-DE">2. Flagge(n) einschalten</placeholder>                 
  94.             <disabled>true</disabled>
  95.         </input-field>
  96.         
  97.         <input-field type="bool">
  98.             <name>flagonoff</name>
  99.             <label>Turn on flag(s)</label>
  100.             <label lang="de-DE">Flagge(n) einschalten</label>
  101.         </input-field>
  102.     </card>
  103. ';
  104.        
  105.        $optionsbottom '
  106. </config>';
  107.        
  108.        $i=0;
  109.        $file fopen("../custom/plugins/UltraStoreSwitcher/src/Resources/config/config.xml""w");
  110.        
  111.        foreach ($resulter as $value) {
  112.         $i=$i+1;
  113.         $opt "";
  114.         
  115.         $urli $connection->fetchAll('
  116.         SELECT 
  117.     sct1.name, lo1.code, scd1.url, sc1.access_key
  118. FROM
  119.     sales_channel  sc1
  120. INNER JOIN
  121.     sales_channel_translation sct1 
  122.     ON sct1.sales_channel_id = sc1.id
  123. INNER JOIN    
  124.     sales_channel_domain scd1
  125.     ON sc1.id = scd1.sales_channel_id
  126. INNER JOIN
  127.     language la1 
  128.     ON la1.id = sc1.language_id
  129. INNER JOIN
  130.     locale lo1 
  131.     ON lo1.id = la1.locale_id
  132. where sct1.name != "Headless" and sc1.access_key = "' .$value["access_key"] .'"
  133. order by scd1.url asc
  134.         ');
  135.         $firsturl="";
  136.         
  137.         foreach ($urli as $value1) {
  138.             if ($firsturl == "") {
  139.                 $firsturl $value1["url"];
  140.             }
  141.             $opt $opt .'
  142.             <option>
  143.                     <id>' .$value1["url"] .'</id>
  144.                     <name>' .$value1["url"] .'</name>
  145.                     <name lang="de-DE">' .$value1["url"] .'</name>
  146.                 </option>
  147.             ';
  148.         }
  149.         
  150.         
  151.         
  152.         
  153.         $myarray$myarray .'
  154.         <card>
  155.         
  156.         <title>Card ' .($i+2) .'</title>
  157.         <title lang="de-DE">Card ' .($i+2) .'</title>
  158.         
  159.         <input-field type="text">
  160.             <name>eingabefeld' .($i+2) .'</name>
  161.             <class>ultra-info-start-button</class>
  162.             <placeholder>' .$value["name"] .'</placeholder>
  163.             <placeholder lang="de-DE">' .$value["name"] .'</placeholder>                 
  164.             <disabled>true</disabled>
  165.         </input-field>
  166.         
  167.         <input-field type="bool">
  168.             <name>Kanal' .$i .'langonoff</name>
  169.             <label>Activate</label>
  170.             <label lang="de-DE">Einschalten</label>
  171.         </input-field>
  172.         
  173.         <input-field type="text">
  174.             <name>Kanal' .$i .'name</name>
  175.             <label>Label</label>
  176.             <label lang="de-DE">Bezeichnung</label>
  177.         </input-field>
  178.         
  179.         <input-field type="single-select">
  180.             <name>Kanal' .$i .'kanal</name>
  181.             <label>' .$value["name"] .'</label>
  182.             <label lang="de-DE">' .$value["name"] .'</label>
  183.             <options>
  184.                  <option>
  185.                     <id>' .$value["name"] .";;" .$value["code"] .';;' .$value["access_key"] .'</id>
  186.                     <name>' .$value["name"] .'</name>
  187.                     <name lang="de-DE">' .$value["name"] .'</name>
  188.                 </option>
  189.             </options>
  190.             <disabled>true</disabled>
  191.         </input-field>
  192.         
  193.         
  194.         <input-field type="single-select">
  195.             <name>Kanal' .$i .'url</name>
  196.             <label>Choose URL</label>
  197.             <label lang="de-DE">URL auswählen</label>
  198.             <options>
  199.                  ' .$opt .'
  200.             </options>
  201.         </input-field>
  202.         
  203.         <input-field type="text">
  204.             <name>Kanal' .$i .'title</name>
  205.             <label>Title</label>
  206.             <label lang="de-DE">Title</label>
  207.         </input-field>
  208.         
  209.         <input-field type="bool">
  210.             <name>Kanal' .$i .'ownflagonoff</name>
  211.             <label>Turn on own picture</label>
  212.             <label lang="de-DE">Eigenes Bild einschalten</label>
  213.         </input-field>
  214.         
  215.         <component name="sw-media-field">
  216.             <name>Kanal' .$i .'flagger</name>
  217.             <label>Own picture</label>
  218.             <label lang="de-DE">Eigenes Bild</label>
  219.         </component>
  220.     </card>';
  221.         $this->setValue('Kanal' .$i ."kanal"$value["name"] .";;" .$value["code"] .';;' .$value["access_key"]);
  222.         $this->setValue('Kanal' .$i .'langonoff'1);
  223.         $this->setValue('flagonoff'1);
  224.         $this->setValue('Kanal' .$i .'url'$firsturl);
  225.         
  226.         }
  227.         fwrite($file,$topper .$optionstop .$myarray .$optionsbottom);
  228.         fclose($file);
  229.         
  230.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  231.         $customFieldSetRepository->upsert([$this->getFieldSet()], $context->getContext());
  232.         
  233.         $sc_name $connection->fetchAll('SELECT * FROM sales_channel_translation') ;
  234.         foreach ($sc_name as $value2) {
  235.             if($value2["custom_fields"] == '{"custom_verkaufskanal_name": null}' or $value2["custom_fields"] == 'NULL' or $value2["custom_fields"] == '') {
  236.                 $ergebnis='{"custom_verkaufskanal_name": "' .$value2["name"] .'"}';
  237.                 $connection->executeUpdate('UPDATE sales_channel_translation SET custom_fields =' .chr(39) .$ergebnis .chr(39) .' WHERE name="' .$value2["name"] .'"');
  238.             }
  239.             
  240.         }
  241.         $connection->executeUpdate("UPDATE mail_template_translation SET sender_name = REPLACE(sender_name,'{{ salesChannel.name }}','{{salesChannel.customFields.custom_verkaufskanal_name}}')");
  242.         $connection->executeUpdate("UPDATE mail_template_translation SET subject = REPLACE(subject,'{{ salesChannel.translated.name }}','{{salesChannel.customFields.custom_verkaufskanal_name}}')");
  243.         
  244.         
  245.     }
  246.     public function activate(ActivateContext $context): void
  247.    {
  248.        
  249.         
  250.    }
  251.     public function deactivate(DeactivateContext $context): void
  252.     {
  253.         
  254.     }
  255.     
  256.     public function update(UpdateContext $context): void
  257.     {
  258.         copy('../custom/plugins/UltraSidebarCategoryOptimizer/src/Resources/datas/ultra-config-styler.css''../public/bundles/administration/static/css/ultra-config-styler.css');
  259.         copy('../custom/plugins/UltraSidebarCategoryOptimizer/src/Resources/datas/ultra-config-styler.js''../public/bundles/administration/static/js/ultra-config-styler.js');
  260.     }
  261.     
  262.     public function uninstall(UninstallContext $context): void
  263.     {
  264.         parent::uninstall($context);
  265.         $connection $this->container->get(Connection::class);
  266.         $connection->executeUpdate('DELETE FROM custom_field_set where name = "ultra_saleschannel"');
  267.         if ($context->keepUserData()) {
  268.             return;
  269.         }
  270.         $connection->executeUpdate('delete FROM `system_config` where configuration_key like "UltraStoreSwitcher%"');
  271.         $connection->executeUpdate("UPDATE mail_template_translation SET sender_name = REPLACE(sender_name,'{{salesChannel.customFields.custom_verkaufskanal_name}}','{{ salesChannel.name }}')");
  272.         $connection->executeUpdate("UPDATE mail_template_translation SET subject = REPLACE(subject,'{{salesChannel.customFields.custom_verkaufskanal_name}}','{{ salesChannel.translated.name }}')");
  273.         
  274.     }
  275.     
  276.     public function setValue(string $configName$default null) : void
  277.     {
  278.         $systemConfigService $this->container->get(SystemConfigService::class);
  279.         $domain $this->getName() . '.config.';
  280.         if( $systemConfigService->get($domain $configName) === null )
  281.         {
  282.             $systemConfigService->set($domain $configName$default);
  283.         }
  284.     }
  285.     
  286.     private function getFieldSet()
  287.     {
  288.         return [
  289.             'id' => Uuid::randomHex(),
  290.             'name' => 'ultra_saleschannel',
  291.             'config' => [
  292.                 "translated" => true,
  293.                 'label' => [
  294.                     'de-DE' => 'Verkaufskanal Header Dropdown Menü',
  295.                     'en-GB' => 'Sales channel header dropdown menu'
  296.                 ],
  297.             ],
  298.             'customFields' => [
  299.                 [
  300.                     'id' => Uuid::randomHex(),
  301.                     'name' => 'custom_verkaufskanal_name',
  302.                     'type' => CustomFieldTypes::TEXT,
  303.                     'config' => [
  304.                         'componentName' => 'sw-field',
  305.                         'customFieldType' => 'text',
  306.                         'label' => [
  307.                             'de-DE' => 'Verkaufskanal Name',
  308.                             'en-GB' => 'Saleschannel name',
  309.                         ],
  310.                         'customFieldPosition' => 1
  311.                     ],
  312.                 ],
  313.             ],
  314.             'relations' => [
  315.                 ['id' => Uuid::randomHex(), 'entityName' => 'sales_channel'],
  316.             ]
  317.         ];
  318.     }
  319.     
  320.     
  321. }