| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Notamedia\ConsoleJedi\Schema;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Bitrix\Main\Loader;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Notamedia\ConsoleJedi\Schema\Exception\ImportException;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class Import
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Notamedia\ConsoleJedi\Schema
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Import implements ActionInterface
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $config = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $session = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var \CIBlockXMLFile
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $xml;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var \CIBlockCMLImport
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $import;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->config = [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'type' => '',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'lids' => [],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'path' => '',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'action_section' => 'A',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'action_element' => 'A',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'preview' => '',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'interval' => 30
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         Loader::includeModule('iblock');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->xml = new \CIBlockXMLFile();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->import = new \CIBlockCMLImport();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $type
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return $this
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function setType($type)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->config['type'] = $type;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $path
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return $this
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function setPath($path)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->config['path'] = $path;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param array $lids
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return $this
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function setSites($lids)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->config['lids'] = $lids;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $action
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return $this
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */
 | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     public function setActionSection($action)
 | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $this->config['action_section'] = $action;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         return $this;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param string $action
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return $this
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function setActionElement($action)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->config['action_element'] = $action;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return $this
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function execute()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->session = [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             "section_map" => false,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             "prices_map" => false,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->read();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->import();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $this;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws ImportException
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     protected function read()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $handle = fopen($this->config['path'], "r");
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         if (!$handle)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             throw new ImportException('Unable to open file, or file not exist');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         if (!$this->import->CheckIfFileIsCML($this->config['path'])) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             throw new ImportException('File is not valid');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->xml->DropTemporaryTables();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $this->xml->CreateTemporaryTables();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $this->xml->ReadXMLToDatabase($handle, $this->session, $this->config['interval']);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->xml->IndexTemporaryTables();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     protected function import()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->import->Init($this->config, false, true, $this->config["preview"], false, true);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->import->ImportMetaData([1, 2], $this->config["type"], $this->config["lids"]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->import->ImportSections();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->import->DeactivateSections($this->config["action_section"]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->import->ReadCatalogData($this->session["section_map"], $this->session["prices_map"]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->import->ImportElements(time(), $this->config["interval"]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->import->DeactivateElement($this->config["action_element"], time(), $this->config["interval"]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->import->ImportProductSets();
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  |     }
 | 
            
                                                        
            
                                    
            
            
                | 155 |  |  | } |