stockinthechannel2012 - Version 2.2.1

Version Notes

Version 2.2.1:
* Category Description database table type limit extended
*EAN N/A fix
* Fixed Unknown column 'sku' in 'field list error message for stINch_products_mapping_temp

Download this release

Release Info

Developer stockinchannel
Extension stockinthechannel2012
Version 2.2.1
Comparing to
See all releases


Code changes from version 2.2.0 to 2.2.1

Files changed (25) hide show
  1. app/code/local/Bintime/Sinchimport/Block/Adminhtml/Catalog/Product/Sinchdistributors.php +25 -25
  2. app/code/local/Bintime/Sinchimport/Block/Backupbutton.php +23 -22
  3. app/code/local/Bintime/Sinchimport/Block/Importenvironment.php +210 -210
  4. app/code/local/Bintime/Sinchimport/Block/Importhistory.php +385 -385
  5. app/code/local/Bintime/Sinchimport/Block/Layer/Filter/Feature.php +17 -17
  6. app/code/local/Bintime/Sinchimport/Block/Layer/View.php +48 -48
  7. app/code/local/Bintime/Sinchimport/Block/List.php +159 -159
  8. app/code/local/Bintime/Sinchimport/Block/Product/View/Media.php +18 -18
  9. app/code/local/Bintime/Sinchimport/Block/Startimportbutton.php +343 -343
  10. app/code/local/Bintime/Sinchimport/Block/Startstockpriceimportbutton.php +137 -137
  11. app/code/local/Bintime/Sinchimport/Helper/Data.php +5 -5
  12. app/code/local/Bintime/Sinchimport/Helper/Getdata.php +185 -185
  13. app/code/local/Bintime/Sinchimport/Helper/Image.php +73 -73
  14. app/code/local/Bintime/Sinchimport/Model/Api.php +85 -85
  15. app/code/local/Bintime/Sinchimport/Model/Backup.php +79 -79
  16. app/code/local/Bintime/Sinchimport/Model/Category.php +19 -19
  17. app/code/local/Bintime/Sinchimport/Model/Image.php +118 -118
  18. app/code/local/Bintime/Sinchimport/Model/Layer.php +49 -49
  19. app/code/local/Bintime/Sinchimport/Model/Layer/Filter/Feature.php +248 -248
  20. app/code/local/Bintime/Sinchimport/Model/Layer/Filter/Price.php +141 -192
  21. app/code/local/Bintime/Sinchimport/Model/Product.php +64 -64
  22. app/code/local/Bintime/Sinchimport/Model/Resource/Layer/Filter/Price.php +78 -78
  23. app/code/local/Bintime/Sinchimport/Model/Resource/Mysql4/Layer/Filter/Feature.php +372 -399
  24. app/code/local/Bintime/Sinchimport/Model/Resource/Mysql4/Setup.php +3 -3
  25. app/code/local/Bintime/Sinchimport/Model/Sinch.php +6907 -10266
app/code/local/Bintime/Sinchimport/Block/Adminhtml/Catalog/Product/Sinchdistributors.php CHANGED
@@ -1,25 +1,25 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_Adminhtml_Catalog_Product_Sinchdistributors extends Mage_Core_Block_Template implements Mage_Adminhtml_Block_Widget_Tab_Interface
3
- {
4
- public function __construct(){
5
- $this->setTemplate('sinchimport/sinchdistributors.phtml');
6
- parent::__construct();
7
- }
8
-
9
- //Label to be shown in the tab
10
- public function getTabLabel(){
11
- return Mage::helper('core')->__('Suppliers');
12
- }
13
-
14
- public function getTabTitle(){
15
- return Mage::helper('core')->__('Suppliers');
16
- }
17
-
18
- public function canShowTab(){
19
- return true;
20
- }
21
-
22
- public function isHidden(){
23
- return false;
24
- }
25
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Block_Adminhtml_Catalog_Product_Sinchdistributors extends Mage_Core_Block_Template implements Mage_Adminhtml_Block_Widget_Tab_Interface
3
+ {
4
+ public function __construct(){
5
+ $this->setTemplate('sinchimport/sinchdistributors.phtml');
6
+ parent::__construct();
7
+ }
8
+
9
+ //Label to be shown in the tab
10
+ public function getTabLabel(){
11
+ return Mage::helper('core')->__('Suppliers');
12
+ }
13
+
14
+ public function getTabTitle(){
15
+ return Mage::helper('core')->__('Suppliers');
16
+ }
17
+
18
+ public function canShowTab(){
19
+ return true;
20
+ }
21
+
22
+ public function isHidden(){
23
+ return false;
24
+ }
25
+ }
app/code/local/Bintime/Sinchimport/Block/Backupbutton.php CHANGED
@@ -1,22 +1,23 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_Backupbutton extends Mage_Adminhtml_Block_System_Config_Form_Field {
3
-
4
- protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
5
- $this->setElement($element);
6
- $url = $this->getUrl('sinchimport/backup'); //
7
- $this->setElement($element);
8
-
9
- $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
10
- ->setType('button')
11
- ->setClass('scalable')
12
- ->setLabel('Backup Now')
13
- ->setOnClick("setLocation('$url')")
14
- ->toHtml();
15
-
16
- $html .= $start_import_button;
17
-
18
- return $html;
19
- }
20
-
21
-
22
- }
 
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Block_Backupbutton extends Mage_Adminhtml_Block_System_Config_Form_Field
4
+ {
5
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
6
+ $this->setElement($element);
7
+ $url = $this->getUrl('sinchimport/backup');
8
+ $this->setElement($element);
9
+
10
+ $html = '';
11
+
12
+ $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
13
+ ->setType('button')
14
+ ->setClass('scalable')
15
+ ->setLabel('Backup Now')
16
+ ->setOnClick("setLocation('$url')")
17
+ ->toHtml();
18
+
19
+ $html .= $start_import_button;
20
+
21
+ return $html;
22
+ }
23
+ }
app/code/local/Bintime/Sinchimport/Block/Importenvironment.php CHANGED
@@ -1,210 +1,210 @@
1
- <?php
2
-
3
-
4
-
5
-
6
- class Bintime_Sinchimport_Block_Importenvironment extends Mage_Adminhtml_Block_System_Config_Form_Field {
7
-
8
-
9
-
10
-
11
- protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
12
- $this->setElement($element);
13
- $url = $this->getUrl('sinchimport/index');
14
- $this->setElement($element);
15
-
16
- $html = '';
17
-
18
- //$html .= '<div class="comment"><H3>'.("Your Stock In The Channel Environment Check Summary" ).'</H3></div>';
19
-
20
- //$html .= '<div class="comment"><H3>'.("In order for this extension to work, your server needs to be configured in a particular way and have sufficient memory." ).'</H3></div>';
21
-
22
-
23
- $html .= '
24
- <table class="history">
25
- <thead>
26
- <tr>
27
- <th>Checked</th>
28
- <th>Necessary, Error And Fix</th>
29
-
30
- </tr>
31
- </thead><tbody>';
32
- // <th>Necessary</th>
33
- // <th>Fix</th>
34
-
35
- $errors_count = 0;
36
-
37
- // Memory total
38
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkMemory();
39
- if ($status == 'error') $errors_count++;
40
- if ($status == 'error') {
41
- $html .= // $caption: $value $measure
42
- "
43
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
44
- <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
45
- <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
46
- <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
47
- ";
48
- };
49
-
50
-
51
-
52
- // Mysql parameter LOCAL DATA LOCAL
53
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkLoaddata();
54
- if ($status == 'error') $errors_count++;
55
- if ($status == 'error') {
56
- $html .= // $caption: $value $measure
57
- "
58
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
59
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
60
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
61
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
62
- ";
63
- };
64
-
65
-
66
- // PHP safe mode
67
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkPhpsafemode();
68
- if ($status == 'error') $errors_count++;
69
- if ($status == 'error') {
70
- $html .= // $caption: $value $measure
71
- "
72
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
73
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
74
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
75
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
76
- ";
77
- };
78
-
79
-
80
- // Mysql parameter wait_timeout
81
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkWaittimeout();
82
- if ($status == 'error') $errors_count++;
83
- if ($status == 'error') {
84
- $html .= // $caption: $value $measure
85
- "
86
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
87
- <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
88
- <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
89
- <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
90
- ";
91
- };
92
-
93
-
94
- // Mysql parameter innodb_buffer_pool_size
95
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkInnodbbufferpoolsize();
96
- if ($status == 'error') $errors_count++;
97
- if ($status == 'error') {
98
- $html .= // $caption: $value $measure
99
- "
100
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
101
- <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
102
- <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
103
- <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
104
- ";
105
- };
106
-
107
-
108
- // Conflict with installed module
109
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkConflictsWithInstalledModules();
110
- if ($status == 'error') $errors_count++;
111
- if ($status == 'error') {
112
- $html .= // $caption: $value $measure
113
- "
114
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
115
- <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
116
- <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
117
- <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
118
- ";
119
- };
120
-
121
-
122
- // PHP run string
123
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkPhprunstring();
124
- if ($status == 'error') $errors_count++;
125
- if ($status == 'error') {
126
- $html .= // $caption: $value $measure
127
- "
128
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
129
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
130
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
131
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
132
- ";
133
- };
134
-
135
-
136
- // Chmod wget file
137
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkChmodwgetdatafile();
138
- if ($status == 'error') $errors_count++;
139
- if ($status == 'error') {
140
- $html .= // $caption: $value $measure
141
- "
142
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
143
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
144
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
145
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
146
- ";
147
- };
148
-
149
-
150
- // Chmod wget cron.php file
151
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkChmodwgetcronphpfile();
152
- if ($status == 'error') $errors_count++;
153
- if ($status == 'error') {
154
- $html .= // $caption: $value $measure
155
- "
156
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
157
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
158
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
159
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
160
- ";
161
- };
162
-
163
- // Chmod wget cron.sh file
164
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkChmodwgetcronshfile();
165
- if ($status == 'error') $errors_count++;
166
- if ($status == 'error') {
167
- $html .= // $caption: $value $measure
168
- "
169
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
170
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
171
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
172
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
173
- ";
174
- };
175
-
176
- // Mysql stored procedure filter_sinch_products_s
177
- list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkProcedure();
178
- if ($status == 'error') $errors_count++;
179
- if ($status == 'error') {
180
- $html .= // $caption: $value $measure
181
- "
182
- <tr> <td nowrap rowspan=4> $caption </td> </tr>
183
- <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
184
- <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
185
- <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
186
- ";
187
- };
188
-
189
-
190
- $html .= '
191
- </tbody>
192
- </table>';
193
-
194
- $html .= '<div class="comment"><H3>'.("There are $errors_count notices." ).'</H3></div>';
195
-
196
- // all checks
197
- return $html;
198
-
199
- } // protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
200
-
201
- protected function _colored($str, $color){
202
- return "<b><span style='color:{$color}'>{$str}</span></b>";
203
- }
204
-
205
-
206
- } // class Bintime_Sinchimport_Block_Importenvironment extends Mage_Adminhtml_Block_System_Config_Form_Field
207
-
208
-
209
-
210
-
1
+ <?php
2
+
3
+
4
+
5
+
6
+ class Bintime_Sinchimport_Block_Importenvironment extends Mage_Adminhtml_Block_System_Config_Form_Field {
7
+
8
+
9
+
10
+
11
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
12
+ $this->setElement($element);
13
+ $url = $this->getUrl('sinchimport/index');
14
+ $this->setElement($element);
15
+
16
+ $html = '';
17
+
18
+ //$html .= '<div class="comment"><H3>'.("Your Stock In The Channel Environment Check Summary" ).'</H3></div>';
19
+
20
+ //$html .= '<div class="comment"><H3>'.("In order for this extension to work, your server needs to be configured in a particular way and have sufficient memory." ).'</H3></div>';
21
+
22
+
23
+ $html .= '
24
+ <table class="history">
25
+ <thead>
26
+ <tr>
27
+ <th>Checked</th>
28
+ <th>Necessary, Error And Fix</th>
29
+
30
+ </tr>
31
+ </thead><tbody>';
32
+ // <th>Necessary</th>
33
+ // <th>Fix</th>
34
+
35
+ $errors_count = 0;
36
+
37
+ // Memory total
38
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkMemory();
39
+ if ($status == 'error') $errors_count++;
40
+ if ($status == 'error') {
41
+ $html .= // $caption: $value $measure
42
+ "
43
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
44
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
45
+ <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
46
+ <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
47
+ ";
48
+ };
49
+
50
+
51
+
52
+ // Mysql parameter LOCAL DATA LOCAL
53
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkLoaddata();
54
+ if ($status == 'error') $errors_count++;
55
+ if ($status == 'error') {
56
+ $html .= // $caption: $value $measure
57
+ "
58
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
59
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
60
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
61
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
62
+ ";
63
+ };
64
+
65
+
66
+ // PHP safe mode
67
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkPhpsafemode();
68
+ if ($status == 'error') $errors_count++;
69
+ if ($status == 'error') {
70
+ $html .= // $caption: $value $measure
71
+ "
72
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
73
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
74
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
75
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
76
+ ";
77
+ };
78
+
79
+
80
+ // Mysql parameter wait_timeout
81
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkWaittimeout();
82
+ if ($status == 'error') $errors_count++;
83
+ if ($status == 'error') {
84
+ $html .= // $caption: $value $measure
85
+ "
86
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
87
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
88
+ <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
89
+ <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
90
+ ";
91
+ };
92
+
93
+
94
+ // Mysql parameter innodb_buffer_pool_size
95
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkInnodbbufferpoolsize();
96
+ if ($status == 'error') $errors_count++;
97
+ if ($status == 'error') {
98
+ $html .= // $caption: $value $measure
99
+ "
100
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
101
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
102
+ <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
103
+ <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
104
+ ";
105
+ };
106
+
107
+
108
+ // Conflict with installed module
109
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkConflictsWithInstalledModules();
110
+ if ($status == 'error') $errors_count++;
111
+ if ($status == 'error') {
112
+ $html .= // $caption: $value $measure
113
+ "
114
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
115
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'blue')} $critical $measure </td> </tr>
116
+ <tr> <td nowrap> {$this->_colored('Notice:', 'blue')} $errmsg </td> </tr>
117
+ <tr> <td nowrap> {$this->_colored('Fix:', 'blue')} $fixmsg </td> </tr>
118
+ ";
119
+ };
120
+
121
+
122
+ // PHP run string
123
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkPhprunstring();
124
+ if ($status == 'error') $errors_count++;
125
+ if ($status == 'error') {
126
+ $html .= // $caption: $value $measure
127
+ "
128
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
129
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
130
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
131
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
132
+ ";
133
+ };
134
+
135
+
136
+ // Chmod wget file
137
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkChmodwgetdatafile();
138
+ if ($status == 'error') $errors_count++;
139
+ if ($status == 'error') {
140
+ $html .= // $caption: $value $measure
141
+ "
142
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
143
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
144
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
145
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
146
+ ";
147
+ };
148
+
149
+
150
+ // Chmod wget cron.php file
151
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkChmodwgetcronphpfile();
152
+ if ($status == 'error') $errors_count++;
153
+ if ($status == 'error') {
154
+ $html .= // $caption: $value $measure
155
+ "
156
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
157
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
158
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
159
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
160
+ ";
161
+ };
162
+
163
+ // Chmod wget cron.sh file
164
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkChmodwgetcronshfile();
165
+ if ($status == 'error') $errors_count++;
166
+ if ($status == 'error') {
167
+ $html .= // $caption: $value $measure
168
+ "
169
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
170
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
171
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
172
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
173
+ ";
174
+ };
175
+
176
+ // Mysql stored procedure filter_sinch_products_s
177
+ list($status, $caption, $critical, $value, $measure, $errmsg, $fixmsg) = Mage::getModel('sinchimport/sinch')->checkProcedure();
178
+ if ($status == 'error') $errors_count++;
179
+ if ($status == 'error') {
180
+ $html .= // $caption: $value $measure
181
+ "
182
+ <tr> <td nowrap rowspan=4> $caption </td> </tr>
183
+ <tr> <td nowrap> {$this->_colored('Necessary:', 'red')} $critical $measure </td> </tr>
184
+ <tr> <td nowrap> {$this->_colored('Error:', 'red')} $errmsg </td> </tr>
185
+ <tr> <td nowrap> {$this->_colored('Fix:', 'red')} $fixmsg </td> </tr>
186
+ ";
187
+ };
188
+
189
+
190
+ $html .= '
191
+ </tbody>
192
+ </table>';
193
+
194
+ $html .= '<div class="comment"><H3>'.("There are $errors_count notices." ).'</H3></div>';
195
+
196
+ // all checks
197
+ return $html;
198
+
199
+ } // protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
200
+
201
+ protected function _colored($str, $color){
202
+ return "<b><span style='color:{$color}'>{$str}</span></b>";
203
+ }
204
+
205
+
206
+ } // class Bintime_Sinchimport_Block_Importenvironment extends Mage_Adminhtml_Block_System_Config_Form_Field
207
+
208
+
209
+
210
+
app/code/local/Bintime/Sinchimport/Block/Importhistory.php CHANGED
@@ -1,385 +1,385 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_Importhistory extends Mage_Adminhtml_Block_System_Config_Form_Field
3
- {
4
-
5
- protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
6
- {
7
- $this->setElement($element);
8
- $url = $this->getUrl('sinchimport/index'); //
9
- $this->setElement($element);
10
-
11
- $html = $this->_appendJs();
12
-
13
- $html .= '<div id="sinchimport_status_template" name="sinchimport_status_template" style="display:none">';//none
14
- $html .= $this->_getStatusTemplateHtml();
15
- $html .= '</div>';
16
-
17
- $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
18
- ->setType('button')
19
- ->setClass('scalable')
20
- ->setLabel('Force Import now')
21
- ->setOnClick("start_sinch_import()") //setLocation('$url')
22
- ->toHtml();
23
-
24
- $html .= $start_import_button;
25
- // $html .= $url;
26
- $dataConf = Mage::getConfig();//('sinchimport_root');//getConfig()->getNode()->asXML();
27
- // $html .= "<pre>".var_export($dataConf, true)."</pre>";
28
-
29
-
30
-
31
- $import=Mage::getModel('sinchimport/sinch');
32
- if($import->is_imort_not_run()){
33
- $import->set_imports_failed();
34
- }
35
- $last_success_import=$import->getDateOfLatestSuccessImport();
36
- $import_history=$import->getImportStatusHistory();
37
-
38
- $css_arr=array(
39
- 'Failed' => 'sinch-error',
40
- 'Run' => 'sinch-run',
41
- 'Successful' => 'sinch-success'
42
- );
43
-
44
- $html=
45
- '
46
- <style type="text/css">
47
- .sinch-error {
48
- font-weight: bold;
49
- color: #D40707 ;
50
- text-align: center;
51
- margin: 5px 0;
52
- }
53
-
54
- .sinch-success {
55
- color: green;
56
- font-weight: bold;
57
- text-align: center;
58
- margin: 5px 0;
59
- }
60
-
61
- .sinch-run {
62
- color: blue;
63
- font-weight: bold;
64
- text-align: center;
65
- margin: 5px 0;
66
- }
67
-
68
-
69
- table.history {
70
- border-collapse: collapse;
71
- width: 100%;
72
- }
73
-
74
- table.history th {
75
- border: solid 1px #6F8992;
76
- background-color: #6F8992;
77
- color: #fff;
78
- font-weight: bold;
79
- padding: 2px 3px;
80
- }
81
-
82
- table.history td {
83
- border: 1px solid #333;
84
- padding: 2px 3px;
85
- }
86
- </style>
87
-
88
- <!--Table for import history-->
89
- <div class="comment">'.($last_success_import? "Your last successful feed import was at ".$last_success_import: "Your import never finished with success" ).'</div>
90
- <table class="history">
91
- <thead>
92
- <tr>
93
- <th>Import Start</th>
94
- <th>Import Finish</th>
95
- <th nowrap>Import Type</th>
96
- <th>Status</th>
97
-
98
- <th nowrap>Number of products</th>
99
- </tr>
100
- </thead>
101
- <tbody>';
102
- foreach($import_history as $item){
103
- $html.='
104
- <tr>
105
- <td nowrap>'.$item['start_import'].'</td>
106
- <td nowrap>'.$item['finish_import'].'</td>
107
- <td nowrap>'.$item['import_type'].'</td>
108
- <td class="'.$css_arr[$item['global_status_import']].'">'.$item['global_status_import'].'</td>
109
-
110
- <td>'.$item['number_of_products'].'</td>
111
- </tr>
112
- ';
113
- }
114
- $html.='
115
- </tbody>
116
- </table>
117
- ';
118
-
119
-
120
- return $html;
121
- }
122
-
123
- protected function _getStatusTemplateHtml()
124
- {
125
- $run_pic=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif";
126
- $html="
127
- <ul>
128
- <li>
129
- Start Import
130
- &nbsp
131
- <span id='sinchimport_start_import'>
132
- <img src='".$run_pic."'
133
- alt='Sinch Import run' />
134
- </span>
135
- </li>
136
- <li>
137
- Upload Files
138
- &nbsp
139
- <span id='sinchimport_upload_files'>
140
- <img src='".$run_pic."'
141
- alt='Upload Files' />
142
- </span>
143
- </li>
144
- <li>
145
- Parse Categories
146
- &nbsp
147
- <span id='sinchimport_parse_categories'>
148
- <img src='".$run_pic."'
149
- alt='Parse Categories' />
150
- </span>
151
- </li>
152
- <li>
153
- Parse Category Features
154
- &nbsp
155
- <span id='sinchimport_parse_category_features'>
156
- <img src='".$run_pic."'
157
- alt='Parse Category Features' />
158
- </span>
159
- </li>
160
- <li>
161
- Parse Distributors
162
- &nbsp
163
- <span id='sinchimport_parse_distributors'>
164
- <img src='".$run_pic."'
165
- alt='Parse Distributors' />
166
- </span>
167
- </li>
168
- <li>
169
- Parse EAN Codes
170
- &nbsp
171
- <span id='sinchimport_parse_ean_codes'>
172
- <img src='".$run_pic."'
173
- alt='Parse EAN Codes' />
174
- </span>
175
- </li>
176
- <li>
177
- Parse Manufacturers
178
- &nbsp
179
- <span id='sinchimport_parse_manufacturers'>
180
- <img src='".$run_pic."'
181
- alt='Parse Manufacturers'' />
182
- </span>
183
- </li>
184
- <li>
185
- Parse Related Products
186
- &nbsp
187
- <span id='sinchimport_parse_related_products'>
188
- <img src='".$run_pic."'
189
- alt='Parse Related Products' />
190
- </span>
191
- </li>
192
- <li>
193
- Parse Product Features
194
- &nbsp
195
- <span id='sinchimport_parse_product_features'>
196
- <img src='".$run_pic."'
197
- alt='Parse Product Features' />
198
- </span>
199
- </li>
200
- <li>
201
- Parse Products
202
- &nbsp
203
- <span id='sinchimport_parse_products'>
204
- <img src='".$run_pic."'
205
- alt='Parse Products' />
206
- </span>
207
- </li>
208
- <li>
209
- Parse Pictures Gallery
210
- &nbsp
211
- <span id='sinchimport_parse_pictures_gallery'>
212
- <img src='".$run_pic."'
213
- alt='Parse Pictures Gallery' />
214
- </span>
215
- </li>
216
- <li>
217
- Parse Restricted Values
218
- &nbsp
219
- <span id='sinchimport_parse_restricted_values'>
220
- <img src='".$run_pic."'
221
- alt='Parse Restricted Values' />
222
- </span>
223
- </li>
224
- <li>
225
- Parse Stock And Prices
226
- &nbsp
227
- <span id='sinchimport_parse_stock_and_prices'>
228
- <img src='".$run_pic."'
229
- alt='Parse Stock And Prices' />
230
- </span>
231
- </li>
232
- <li>
233
- Generate category filters
234
- &nbsp
235
- <span id='sinchimport_generate_category_filters'>
236
- <img src='".$run_pic."'
237
- alt='Generate category filters' />
238
- </span>
239
- </li>
240
- <li>
241
- Import finished
242
- &nbsp
243
- <span id='sinchimport_import_finished'>
244
- <img src='".$run_pic."'
245
- alt='Import finished' />
246
- </span>
247
- </li>
248
-
249
- </ul>
250
- ";
251
- return $html;
252
- }
253
-
254
- protected function _appendJs()
255
- {
256
- $post_url=$this->getUrl('sinchimport/ajax');
257
- $post_url_upd=$this->getUrl('sinchimport/ajax/UpdateStatus');
258
- $html = "
259
- <script>
260
- function start_sinch_import(){
261
- status_div=document.getElementById('sinchimport_status_template');
262
- status_div.style.display='';
263
- // status_div.innerHTML='';
264
- sinch = new Sinch('$post_url','$post_url_upd');
265
- sinch.startSinchImport();
266
-
267
- //
268
- }
269
- var Sinch = Class.create();
270
- Sinch.prototype = {
271
-
272
- initialize: function(postUrl, postUrlUpd) {
273
- this.postUrl = postUrl; //'https://techatcost.com/purchases/ajax/';
274
- this.postUrlUpd = postUrlUpd;
275
- this.failureUrl = document.URL;
276
- // unique user session ID
277
- this.SID = null;
278
- // object with event message data
279
- this.objectMsg = null;
280
- this.prevMsg = '';
281
- // interval object
282
- this.updateTimer = null;
283
- // default shipping code. Display on errors
284
-
285
- elem = 'checkoutSteps';
286
- clickableEntity = '.head';
287
-
288
- // overwrite Accordion class method
289
- var headers = $$('#' + elem + ' .section ' + clickableEntity);
290
- headers.each(function(header) {
291
- Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
292
- }.bind(this));
293
- },
294
- startSinchImport: function () {
295
- _this = this;
296
- new Ajax.Request(this.postUrl,
297
- {
298
- method:'post',
299
- parameters: '',
300
- requestTimeout: 10,
301
- /*
302
- onLoading:function(){
303
- alert('onLoading');
304
- },
305
- onLoaded:function(){
306
- alert('onLoaded');
307
- },
308
- */
309
- onSuccess: function(transport) {
310
- var response = transport.responseText || null;
311
- _this.SID = response;
312
- if (_this.SID) {
313
- _this.updateTimer = setInterval(function(){_this.updateEvent();},20000);
314
- $('session_id').value = _this.SID;
315
- } else {
316
- alert('Can not get your session ID. Please reload the page!');
317
- }
318
- },
319
- onTimeout: function() { alert('Can not get your session ID. Timeout!'); },
320
- onFailure: function() { alert('Something went wrong...') }
321
- });
322
-
323
- },
324
-
325
- updateEvent: function () {
326
- _this = this;
327
- new Ajax.Request(this.postUrlUpd,
328
- {
329
- method: 'post',
330
- parameters: {session_id: this.SID},
331
- onSuccess: function(transport) {
332
- _this.objectMsg = transport.responseText.evalJSON();
333
- _this.prevMsg = _this.objectMsg.message;
334
- if(_this.prevMsg!=''){
335
- _this.updateStatusHtml();
336
- }
337
-
338
- if (_this.objectMsg.error == 1) {
339
- // Do something on error
340
- _this.clearUpdateInterval();
341
- }
342
-
343
- if (_this.objectMsg.finished == 1) {
344
- _this.objectMsg.message='Import finished';
345
- _this.updateStatusHtml();
346
- _this.clearUpdateInterval();
347
-
348
- }
349
-
350
- },
351
- onFailure: this.ajaxFailure.bind(),
352
- });
353
- },
354
-
355
- updateStatusHtml: function(){
356
- message=this.objectMsg.message.toLowerCase();
357
- mess_id='sinchimport_'+message.replace(/\s+/g, '_');
358
- if(!document.getElementById(mess_id)){
359
- alert(mess_id+' - not exist');
360
- }
361
- else{
362
- // alert (mess_id+' - exist');
363
- $(mess_id).innerHTML='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_yes.gif"."\"/>'
364
- }
365
- htm=$('sinchimport_status_template').innerHTML;
366
- // $('sinchimport_status_template').innerHTML=htm+'<br>'+this.objectMsg.message;
367
- },
368
-
369
- ajaxFailure: function(){
370
- this.clearUpdateInterval();
371
- location.href = this.failureUrl;
372
- },
373
-
374
- clearUpdateInterval: function () {
375
- clearInterval(this.updateTimer);
376
- },
377
-
378
-
379
- }
380
- </script>
381
- ";
382
- return $html;
383
- }
384
-
385
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Block_Importhistory extends Mage_Adminhtml_Block_System_Config_Form_Field
3
+ {
4
+
5
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
6
+ {
7
+ $this->setElement($element);
8
+ $url = $this->getUrl('sinchimport/index'); //
9
+ $this->setElement($element);
10
+
11
+ $html = $this->_appendJs();
12
+
13
+ $html .= '<div id="sinchimport_status_template" name="sinchimport_status_template" style="display:none">';//none
14
+ $html .= $this->_getStatusTemplateHtml();
15
+ $html .= '</div>';
16
+
17
+ $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
18
+ ->setType('button')
19
+ ->setClass('scalable')
20
+ ->setLabel('Force Import now')
21
+ ->setOnClick("start_sinch_import()") //setLocation('$url')
22
+ ->toHtml();
23
+
24
+ $html .= $start_import_button;
25
+ // $html .= $url;
26
+ $dataConf = Mage::getConfig();//('sinchimport_root');//getConfig()->getNode()->asXML();
27
+ // $html .= "<pre>".var_export($dataConf, true)."</pre>";
28
+
29
+
30
+
31
+ $import=Mage::getModel('sinchimport/sinch');
32
+ if($import->is_imort_not_run()){
33
+ $import->set_imports_failed();
34
+ }
35
+ $last_success_import=$import->getDateOfLatestSuccessImport();
36
+ $import_history=$import->getImportStatusHistory();
37
+
38
+ $css_arr=array(
39
+ 'Failed' => 'sinch-error',
40
+ 'Run' => 'sinch-run',
41
+ 'Successful' => 'sinch-success'
42
+ );
43
+
44
+ $html=
45
+ '
46
+ <style type="text/css">
47
+ .sinch-error {
48
+ font-weight: bold;
49
+ color: #D40707 ;
50
+ text-align: center;
51
+ margin: 5px 0;
52
+ }
53
+
54
+ .sinch-success {
55
+ color: green;
56
+ font-weight: bold;
57
+ text-align: center;
58
+ margin: 5px 0;
59
+ }
60
+
61
+ .sinch-run {
62
+ color: blue;
63
+ font-weight: bold;
64
+ text-align: center;
65
+ margin: 5px 0;
66
+ }
67
+
68
+
69
+ table.history {
70
+ border-collapse: collapse;
71
+ width: 100%;
72
+ }
73
+
74
+ table.history th {
75
+ border: solid 1px #6F8992;
76
+ background-color: #6F8992;
77
+ color: #fff;
78
+ font-weight: bold;
79
+ padding: 2px 3px;
80
+ }
81
+
82
+ table.history td {
83
+ border: 1px solid #333;
84
+ padding: 2px 3px;
85
+ }
86
+ </style>
87
+
88
+ <!--Table for import history-->
89
+ <div class="comment">'.($last_success_import? "Your last successful feed import was at ".$last_success_import: "Your import never finished with success" ).'</div>
90
+ <table class="history">
91
+ <thead>
92
+ <tr>
93
+ <th>Import Start</th>
94
+ <th>Import Finish</th>
95
+ <th nowrap>Import Type</th>
96
+ <th>Status</th>
97
+
98
+ <th nowrap>Number of products</th>
99
+ </tr>
100
+ </thead>
101
+ <tbody>';
102
+ foreach($import_history as $item){
103
+ $html.='
104
+ <tr>
105
+ <td nowrap>'.$item['start_import'].'</td>
106
+ <td nowrap>'.$item['finish_import'].'</td>
107
+ <td nowrap>'.$item['import_type'].'</td>
108
+ <td class="'.$css_arr[$item['global_status_import']].'">'.$item['global_status_import'].'</td>
109
+
110
+ <td>'.$item['number_of_products'].'</td>
111
+ </tr>
112
+ ';
113
+ }
114
+ $html.='
115
+ </tbody>
116
+ </table>
117
+ ';
118
+
119
+
120
+ return $html;
121
+ }
122
+
123
+ protected function _getStatusTemplateHtml()
124
+ {
125
+ $run_pic=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif";
126
+ $html="
127
+ <ul>
128
+ <li>
129
+ Start Import
130
+ &nbsp
131
+ <span id='sinchimport_start_import'>
132
+ <img src='".$run_pic."'
133
+ alt='Sinch Import run' />
134
+ </span>
135
+ </li>
136
+ <li>
137
+ Upload Files
138
+ &nbsp
139
+ <span id='sinchimport_upload_files'>
140
+ <img src='".$run_pic."'
141
+ alt='Upload Files' />
142
+ </span>
143
+ </li>
144
+ <li>
145
+ Parse Categories
146
+ &nbsp
147
+ <span id='sinchimport_parse_categories'>
148
+ <img src='".$run_pic."'
149
+ alt='Parse Categories' />
150
+ </span>
151
+ </li>
152
+ <li>
153
+ Parse Category Features
154
+ &nbsp
155
+ <span id='sinchimport_parse_category_features'>
156
+ <img src='".$run_pic."'
157
+ alt='Parse Category Features' />
158
+ </span>
159
+ </li>
160
+ <li>
161
+ Parse Distributors
162
+ &nbsp
163
+ <span id='sinchimport_parse_distributors'>
164
+ <img src='".$run_pic."'
165
+ alt='Parse Distributors' />
166
+ </span>
167
+ </li>
168
+ <li>
169
+ Parse EAN Codes
170
+ &nbsp
171
+ <span id='sinchimport_parse_ean_codes'>
172
+ <img src='".$run_pic."'
173
+ alt='Parse EAN Codes' />
174
+ </span>
175
+ </li>
176
+ <li>
177
+ Parse Manufacturers
178
+ &nbsp
179
+ <span id='sinchimport_parse_manufacturers'>
180
+ <img src='".$run_pic."'
181
+ alt='Parse Manufacturers'' />
182
+ </span>
183
+ </li>
184
+ <li>
185
+ Parse Related Products
186
+ &nbsp
187
+ <span id='sinchimport_parse_related_products'>
188
+ <img src='".$run_pic."'
189
+ alt='Parse Related Products' />
190
+ </span>
191
+ </li>
192
+ <li>
193
+ Parse Product Features
194
+ &nbsp
195
+ <span id='sinchimport_parse_product_features'>
196
+ <img src='".$run_pic."'
197
+ alt='Parse Product Features' />
198
+ </span>
199
+ </li>
200
+ <li>
201
+ Parse Products
202
+ &nbsp
203
+ <span id='sinchimport_parse_products'>
204
+ <img src='".$run_pic."'
205
+ alt='Parse Products' />
206
+ </span>
207
+ </li>
208
+ <li>
209
+ Parse Pictures Gallery
210
+ &nbsp
211
+ <span id='sinchimport_parse_pictures_gallery'>
212
+ <img src='".$run_pic."'
213
+ alt='Parse Pictures Gallery' />
214
+ </span>
215
+ </li>
216
+ <li>
217
+ Parse Restricted Values
218
+ &nbsp
219
+ <span id='sinchimport_parse_restricted_values'>
220
+ <img src='".$run_pic."'
221
+ alt='Parse Restricted Values' />
222
+ </span>
223
+ </li>
224
+ <li>
225
+ Parse Stock And Prices
226
+ &nbsp
227
+ <span id='sinchimport_parse_stock_and_prices'>
228
+ <img src='".$run_pic."'
229
+ alt='Parse Stock And Prices' />
230
+ </span>
231
+ </li>
232
+ <li>
233
+ Generate category filters
234
+ &nbsp
235
+ <span id='sinchimport_generate_category_filters'>
236
+ <img src='".$run_pic."'
237
+ alt='Generate category filters' />
238
+ </span>
239
+ </li>
240
+ <li>
241
+ Import finished
242
+ &nbsp
243
+ <span id='sinchimport_import_finished'>
244
+ <img src='".$run_pic."'
245
+ alt='Import finished' />
246
+ </span>
247
+ </li>
248
+
249
+ </ul>
250
+ ";
251
+ return $html;
252
+ }
253
+
254
+ protected function _appendJs()
255
+ {
256
+ $post_url=$this->getUrl('sinchimport/ajax');
257
+ $post_url_upd=$this->getUrl('sinchimport/ajax/UpdateStatus');
258
+ $html = "
259
+ <script>
260
+ function start_sinch_import(){
261
+ status_div=document.getElementById('sinchimport_status_template');
262
+ status_div.style.display='';
263
+ // status_div.innerHTML='';
264
+ sinch = new Sinch('$post_url','$post_url_upd');
265
+ sinch.startSinchImport();
266
+
267
+ //
268
+ }
269
+ var Sinch = Class.create();
270
+ Sinch.prototype = {
271
+
272
+ initialize: function(postUrl, postUrlUpd) {
273
+ this.postUrl = postUrl; //'https://techatcost.com/purchases/ajax/';
274
+ this.postUrlUpd = postUrlUpd;
275
+ this.failureUrl = document.URL;
276
+ // unique user session ID
277
+ this.SID = null;
278
+ // object with event message data
279
+ this.objectMsg = null;
280
+ this.prevMsg = '';
281
+ // interval object
282
+ this.updateTimer = null;
283
+ // default shipping code. Display on errors
284
+
285
+ elem = 'checkoutSteps';
286
+ clickableEntity = '.head';
287
+
288
+ // overwrite Accordion class method
289
+ var headers = $$('#' + elem + ' .section ' + clickableEntity);
290
+ headers.each(function(header) {
291
+ Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
292
+ }.bind(this));
293
+ },
294
+ startSinchImport: function () {
295
+ _this = this;
296
+ new Ajax.Request(this.postUrl,
297
+ {
298
+ method:'post',
299
+ parameters: '',
300
+ requestTimeout: 10,
301
+ /*
302
+ onLoading:function(){
303
+ alert('onLoading');
304
+ },
305
+ onLoaded:function(){
306
+ alert('onLoaded');
307
+ },
308
+ */
309
+ onSuccess: function(transport) {
310
+ var response = transport.responseText || null;
311
+ _this.SID = response;
312
+ if (_this.SID) {
313
+ _this.updateTimer = setInterval(function(){_this.updateEvent();},20000);
314
+ $('session_id').value = _this.SID;
315
+ } else {
316
+ alert('Can not get your session ID. Please reload the page!');
317
+ }
318
+ },
319
+ onTimeout: function() { alert('Can not get your session ID. Timeout!'); },
320
+ onFailure: function() { alert('Something went wrong...') }
321
+ });
322
+
323
+ },
324
+
325
+ updateEvent: function () {
326
+ _this = this;
327
+ new Ajax.Request(this.postUrlUpd,
328
+ {
329
+ method: 'post',
330
+ parameters: {session_id: this.SID},
331
+ onSuccess: function(transport) {
332
+ _this.objectMsg = transport.responseText.evalJSON();
333
+ _this.prevMsg = _this.objectMsg.message;
334
+ if(_this.prevMsg!=''){
335
+ _this.updateStatusHtml();
336
+ }
337
+
338
+ if (_this.objectMsg.error == 1) {
339
+ // Do something on error
340
+ _this.clearUpdateInterval();
341
+ }
342
+
343
+ if (_this.objectMsg.finished == 1) {
344
+ _this.objectMsg.message='Import finished';
345
+ _this.updateStatusHtml();
346
+ _this.clearUpdateInterval();
347
+
348
+ }
349
+
350
+ },
351
+ onFailure: this.ajaxFailure.bind(),
352
+ });
353
+ },
354
+
355
+ updateStatusHtml: function(){
356
+ message=this.objectMsg.message.toLowerCase();
357
+ mess_id='sinchimport_'+message.replace(/\s+/g, '_');
358
+ if(!document.getElementById(mess_id)){
359
+ alert(mess_id+' - not exist');
360
+ }
361
+ else{
362
+ // alert (mess_id+' - exist');
363
+ $(mess_id).innerHTML='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_yes.gif"."\"/>'
364
+ }
365
+ htm=$('sinchimport_status_template').innerHTML;
366
+ // $('sinchimport_status_template').innerHTML=htm+'<br>'+this.objectMsg.message;
367
+ },
368
+
369
+ ajaxFailure: function(){
370
+ this.clearUpdateInterval();
371
+ location.href = this.failureUrl;
372
+ },
373
+
374
+ clearUpdateInterval: function () {
375
+ clearInterval(this.updateTimer);
376
+ },
377
+
378
+
379
+ }
380
+ </script>
381
+ ";
382
+ return $html;
383
+ }
384
+
385
+ }
app/code/local/Bintime/Sinchimport/Block/Layer/Filter/Feature.php CHANGED
@@ -1,17 +1,17 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Block_Layer_Filter_Feature extends Mage_Catalog_Block_Layer_Filter_Abstract
4
- {
5
-
6
- public function __construct()
7
- {
8
- parent::__construct();
9
- $this->_filterModelName = 'sinchimport/layer_filter_feature';
10
- }
11
-
12
- protected function _prepareFilter()
13
- {
14
- $this->_filter->setAttributeModel($this->getAttributeModel());
15
- return $this;
16
- }
17
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Block_Layer_Filter_Feature extends Mage_Catalog_Block_Layer_Filter_Abstract
4
+ {
5
+
6
+ public function __construct()
7
+ {
8
+ parent::__construct();
9
+ $this->_filterModelName = 'sinchimport/layer_filter_feature';
10
+ }
11
+
12
+ protected function _prepareFilter()
13
+ {
14
+ $this->_filter->setAttributeModel($this->getAttributeModel());
15
+ return $this;
16
+ }
17
+ }
app/code/local/Bintime/Sinchimport/Block/Layer/View.php CHANGED
@@ -1,48 +1,48 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Block_Layer_View extends Mage_Catalog_Block_Layer_View
4
- {
5
-
6
- protected $filterableFeatures = array();
7
-
8
- /**
9
- * Prepare child blocks
10
- *
11
- * @return Mage_Catalog_Block_Layer_View
12
- */
13
- protected function _prepareLayout()
14
- {
15
- //получение списка фич, по которым строися навигация
16
- $filterableFeatures = $this->getLayer()->getFilterableFeatures();
17
- $filterBlockName = 'sinchimport/layer_filter_feature'; //block
18
- foreach ($filterableFeatures as $feature) {
19
- $this->filterableFeatures[] = $feature;
20
- $featureBlock = $this->getLayout()->createBlock($filterBlockName)
21
- ->setLayer($this->getLayer())
22
- ->setAttributeModel($feature)
23
- ->init();
24
- $this->setChild('feature_' . $feature['feature_id'] . '_filter',
25
- $featureBlock
26
- );
27
- }
28
- /* ------------ */
29
- return parent::_prepareLayout();
30
- }
31
-
32
- /**
33
- * Get all layer filters
34
- *
35
- * @return array
36
- */
37
- public function getFilters()
38
- {
39
- $filters = parent::getFilters();
40
- /* ------------ */
41
- foreach ($this->filterableFeatures as $feature) {
42
- $filters[] = $this->getChild('feature_' . $feature['feature_id'] . '_filter');
43
- }
44
- /* ------------ */
45
-
46
- return $filters;
47
- }
48
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Block_Layer_View extends Mage_Catalog_Block_Layer_View
4
+ {
5
+
6
+ protected $filterableFeatures = array();
7
+
8
+ /**
9
+ * Prepare child blocks
10
+ *
11
+ * @return Mage_Catalog_Block_Layer_View
12
+ */
13
+ protected function _prepareLayout()
14
+ {
15
+ //получение списка фич, по которым строися навигация
16
+ $filterableFeatures = $this->getLayer()->getFilterableFeatures();
17
+ $filterBlockName = 'sinchimport/layer_filter_feature'; //block
18
+ foreach ($filterableFeatures as $feature) {
19
+ $this->filterableFeatures[] = $feature;
20
+ $featureBlock = $this->getLayout()->createBlock($filterBlockName)
21
+ ->setLayer($this->getLayer())
22
+ ->setAttributeModel($feature)
23
+ ->init();
24
+ $this->setChild('feature_' . $feature['feature_id'] . '_filter',
25
+ $featureBlock
26
+ );
27
+ }
28
+ /* ------------ */
29
+ return parent::_prepareLayout();
30
+ }
31
+
32
+ /**
33
+ * Get all layer filters
34
+ *
35
+ * @return array
36
+ */
37
+ public function getFilters()
38
+ {
39
+ $filters = parent::getFilters();
40
+ /* ------------ */
41
+ foreach ($this->filterableFeatures as $feature) {
42
+ $filters[] = $this->getChild('feature_' . $feature['feature_id'] . '_filter');
43
+ }
44
+ /* ------------ */
45
+
46
+ return $filters;
47
+ }
48
+ }
app/code/local/Bintime/Sinchimport/Block/List.php CHANGED
@@ -1,159 +1,159 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_List extends Mage_Catalog_Block_Product_Compare_List
3
- {
4
- var $goods;
5
- var $_compareSinchStoreProd;
6
- public function getSinchAttributes()
7
- {
8
- $items = array();
9
- foreach ($this->getItems() as $_item) {
10
- $items[] = $_item->getId();
11
- }
12
- $to_compare=implode(',', $items);
13
- $tmp_table_sorted = Mage::getSingleton('core/resource')->getTableName('$tmp_table_sorted_').time();
14
- $this->tep_db_query("DROP TABLE IF EXISTS $tmp_table_sorted");
15
- $this->tep_db_query("CREATE TABLE $tmp_table_sorted (
16
- id int(11) not null PRIMARY KEY AUTO_INCREMENT,
17
- store_category_id int(11) not null default 0,
18
- store_product_id int(11) not null default 0,
19
- sinch_product_id int(11) not null default 0,
20
- category_feature_id int(11) not null default 0,
21
- feature_name varchar(255) not null default '',
22
- restricted_value_id int(11) not null default 0,
23
- text varchar(255) not null default '',
24
- KEY(category_feature_id),
25
- KEY(store_product_id),
26
- unique key(sinch_product_id, feature_name)
27
- )");
28
-
29
-
30
- $query = "SELECT p.sinch_product_id, p.store_product_id, p.specifications, pm.entity_id
31
- FROM ".Mage::getSingleton('core/resource')->getTableName('stINch_products')." p
32
- JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping')." pm
33
- ON pm.shop_store_product_id = p.store_product_id AND
34
- pm.shop_sinch_product_id=p.sinch_product_id
35
- WHERE pm.entity_id IN ( $to_compare )";
36
- $sinch = $this->tep_db_query($query);
37
- while($data = $sinch->fetch(PDO::FETCH_ASSOC)){
38
- $this->ins_from_htm($data['sinch_product_id'],$data['store_product_id'],$data['specifications'],$tmp_table_sorted);
39
- $sinch_products_array[] = $data['sinch_product_id'];
40
- $this->_compareSinchStoreProd[$data['entity_id']]=$data['sinch_product_id'];
41
- }
42
- $sinch_products = implode(',', $sinch_products_array);
43
- if(!$sinch_products){
44
- $sinch_products = "''";
45
- }
46
- $this->tep_db_query("INSERT IGNORE INTO $tmp_table_sorted (
47
- store_category_id,
48
- sinch_product_id,
49
- category_feature_id,
50
- feature_name,
51
- restricted_value_id,
52
- text
53
- )(SELECT
54
- cf.store_category_id,
55
- pf.sinch_product_id,
56
- cf.category_feature_id,
57
- cf.feature_name,
58
- rv.restricted_value_id,
59
- rv.text
60
- FROM ".Mage::getSingleton('core/resource')->getTableName('stINch_categories_features')." cf
61
- JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values')." rv
62
- ON cf.category_feature_id=rv.category_feature_id
63
- JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_product_features')." pf
64
- ON rv.restricted_value_id=pf.restricted_value_id
65
- WHERE sinch_product_id in (".$sinch_products.")
66
- ORDER BY
67
- cf.display_order_number,
68
- pf.sinch_product_id)
69
- ");
70
- $this->tep_db_query("UPDATE $tmp_table_sorted t
71
- JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products')." p
72
- ON t.sinch_product_id=p.sinch_product_id
73
- SET t.store_product_id=p.store_product_id");
74
-
75
- $this->goods = array();
76
-
77
- $product_query = $this->tep_db_query("select store_product_id, sinch_product_id, category_feature_id, store_category_id, feature_name, text
78
- FROM $tmp_table_sorted
79
- ORDER BY id ASC");
80
- while($data = $product_query->fetch(PDO::FETCH_ASSOC)){
81
- $this->goods[$data['sinch_product_id']]['category_features'][$data['feature_name']] = array(
82
-
83
-
84
- 'category_feature_id' => $data['category_feature_id'],
85
- 'value' => $data['text'],
86
-
87
-
88
- 'feature_name' => $data['feature_name'],
89
- );
90
- }
91
- $catfeats = $this->tep_db_query("SELECT category_feature_id, feature_name FROM $tmp_table_sorted
92
- GROUP BY feature_name
93
- ORDER BY category_feature_id");
94
- while($fdata = $catfeats->fetch(PDO::FETCH_ASSOC)){
95
- $cf[] = $fdata;
96
- }
97
- return $cf;
98
- }
99
-
100
- public function GetSinchCompareProdAttributes(){
101
- return $this->goods;
102
- }
103
-
104
- public function getSinchAttributeName($cf)
105
- {
106
- return $cf['feature_name'];
107
- }
108
-
109
- public function getSinchProductAttributeValue($product, $cf)
110
- {
111
-
112
- $prod = $this->_compareSinchStoreProd[$product->getID()];
113
- $feature_name = $cf['feature_name'];
114
- $data = $this->goods[$prod]['category_features'][$feature_name]['value'];
115
- $data = Mage::getModel('sinchimport/sinch')->valid_utf($data);
116
- return $data;
117
- }
118
-
119
- private function ins_from_htm($sinch_product_id, $store_product_id, $htm, $tmp_table_sorted){
120
- if($htm){
121
- /** создаем новый dom-объект **/
122
- $dom = new domDocument;
123
- /** загружаем html в объект **/
124
- $dom->loadHTML($htm);
125
- $dom->preserveWhiteSpace = false;
126
-
127
- /** элемент по тэгу **/
128
- $tables = $dom->getElementsByTagName('table');
129
-
130
- /** получаем все строки таблицы **/
131
- $rows = $tables->item(0)->getElementsByTagName('tr');
132
-
133
- /** цикл по строкам **/
134
- $i=0;
135
- foreach ($rows as $row)
136
- {
137
- /** все ячейки по тэгу **/
138
- $cols = $row->getElementsByTagName('td');
139
- /** выводим значения **/
140
- $name = $cols->item(0)->nodeValue;
141
- $value = $cols->item(1)->nodeValue;
142
-
143
- if($value && $i){
144
- $q=" insert ignore into ".$tmp_table_sorted." (store_product_id, sinch_product_id, feature_name, text) values(".$store_product_id.",".$sinch_product_id.",'".$name."','".$value."')";
145
- $this->tep_db_query($q);
146
- }
147
- //mysqli_real_escape_string($name)
148
- $i++;
149
- }
150
- }
151
- }
152
- private function tep_db_query($q){
153
- $resource = Mage::getResourceSingleton('catalog/product');
154
- $connection = $resource->getReadConnection();
155
- $result = $connection->query($q);
156
- return($result);
157
- }
158
-
159
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Block_List extends Mage_Catalog_Block_Product_Compare_List
3
+ {
4
+ var $goods;
5
+ var $_compareSinchStoreProd;
6
+ public function getSinchAttributes()
7
+ {
8
+ $items = array();
9
+ foreach ($this->getItems() as $_item) {
10
+ $items[] = $_item->getId();
11
+ }
12
+ $to_compare=implode(',', $items);
13
+ $tmp_table_sorted = Mage::getSingleton('core/resource')->getTableName('$tmp_table_sorted_').time();
14
+ $this->tep_db_query("DROP TABLE IF EXISTS $tmp_table_sorted");
15
+ $this->tep_db_query("CREATE TABLE $tmp_table_sorted (
16
+ id int(11) not null PRIMARY KEY AUTO_INCREMENT,
17
+ store_category_id int(11) not null default 0,
18
+ store_product_id int(11) not null default 0,
19
+ sinch_product_id int(11) not null default 0,
20
+ category_feature_id int(11) not null default 0,
21
+ feature_name varchar(255) not null default '',
22
+ restricted_value_id int(11) not null default 0,
23
+ text varchar(255) not null default '',
24
+ KEY(category_feature_id),
25
+ KEY(store_product_id),
26
+ unique key(sinch_product_id, feature_name)
27
+ )");
28
+
29
+
30
+ $query = "SELECT p.sinch_product_id, p.store_product_id, p.specifications, pm.entity_id
31
+ FROM ".Mage::getSingleton('core/resource')->getTableName('stINch_products')." p
32
+ JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping')." pm
33
+ ON pm.shop_store_product_id = p.store_product_id AND
34
+ pm.shop_sinch_product_id=p.sinch_product_id
35
+ WHERE pm.entity_id IN ( $to_compare )";
36
+ $sinch = $this->tep_db_query($query);
37
+ while($data = $sinch->fetch(PDO::FETCH_ASSOC)){
38
+ $this->ins_from_htm($data['sinch_product_id'],$data['store_product_id'],$data['specifications'],$tmp_table_sorted);
39
+ $sinch_products_array[] = $data['sinch_product_id'];
40
+ $this->_compareSinchStoreProd[$data['entity_id']]=$data['sinch_product_id'];
41
+ }
42
+ $sinch_products = implode(',', $sinch_products_array);
43
+ if(!$sinch_products){
44
+ $sinch_products = "''";
45
+ }
46
+ $this->tep_db_query("INSERT IGNORE INTO $tmp_table_sorted (
47
+ store_category_id,
48
+ sinch_product_id,
49
+ category_feature_id,
50
+ feature_name,
51
+ restricted_value_id,
52
+ text
53
+ )(SELECT
54
+ cf.store_category_id,
55
+ pf.sinch_product_id,
56
+ cf.category_feature_id,
57
+ cf.feature_name,
58
+ rv.restricted_value_id,
59
+ rv.text
60
+ FROM ".Mage::getSingleton('core/resource')->getTableName('stINch_categories_features')." cf
61
+ JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values')." rv
62
+ ON cf.category_feature_id=rv.category_feature_id
63
+ JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_product_features')." pf
64
+ ON rv.restricted_value_id=pf.restricted_value_id
65
+ WHERE sinch_product_id in (".$sinch_products.")
66
+ ORDER BY
67
+ cf.display_order_number,
68
+ pf.sinch_product_id)
69
+ ");
70
+ $this->tep_db_query("UPDATE $tmp_table_sorted t
71
+ JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products')." p
72
+ ON t.sinch_product_id=p.sinch_product_id
73
+ SET t.store_product_id=p.store_product_id");
74
+
75
+ $this->goods = array();
76
+
77
+ $product_query = $this->tep_db_query("select store_product_id, sinch_product_id, category_feature_id, store_category_id, feature_name, text
78
+ FROM $tmp_table_sorted
79
+ ORDER BY id ASC");
80
+ while($data = $product_query->fetch(PDO::FETCH_ASSOC)){
81
+ $this->goods[$data['sinch_product_id']]['category_features'][$data['feature_name']] = array(
82
+
83
+
84
+ 'category_feature_id' => $data['category_feature_id'],
85
+ 'value' => $data['text'],
86
+
87
+
88
+ 'feature_name' => $data['feature_name'],
89
+ );
90
+ }
91
+ $catfeats = $this->tep_db_query("SELECT category_feature_id, feature_name FROM $tmp_table_sorted
92
+ GROUP BY feature_name
93
+ ORDER BY category_feature_id");
94
+ while($fdata = $catfeats->fetch(PDO::FETCH_ASSOC)){
95
+ $cf[] = $fdata;
96
+ }
97
+ return $cf;
98
+ }
99
+
100
+ public function GetSinchCompareProdAttributes(){
101
+ return $this->goods;
102
+ }
103
+
104
+ public function getSinchAttributeName($cf)
105
+ {
106
+ return $cf['feature_name'];
107
+ }
108
+
109
+ public function getSinchProductAttributeValue($product, $cf)
110
+ {
111
+
112
+ $prod = $this->_compareSinchStoreProd[$product->getID()];
113
+ $feature_name = $cf['feature_name'];
114
+ $data = $this->goods[$prod]['category_features'][$feature_name]['value'];
115
+ $data = Mage::getModel('sinchimport/sinch')->valid_utf($data);
116
+ return $data;
117
+ }
118
+
119
+ private function ins_from_htm($sinch_product_id, $store_product_id, $htm, $tmp_table_sorted){
120
+ if($htm){
121
+ /** создаем новый dom-объект **/
122
+ $dom = new domDocument;
123
+ /** загружаем html в объект **/
124
+ $dom->loadHTML($htm);
125
+ $dom->preserveWhiteSpace = false;
126
+
127
+ /** элемент по тэгу **/
128
+ $tables = $dom->getElementsByTagName('table');
129
+
130
+ /** получаем все строки таблицы **/
131
+ $rows = $tables->item(0)->getElementsByTagName('tr');
132
+
133
+ /** цикл по строкам **/
134
+ $i=0;
135
+ foreach ($rows as $row)
136
+ {
137
+ /** все ячейки по тэгу **/
138
+ $cols = $row->getElementsByTagName('td');
139
+ /** выводим значения **/
140
+ $name = $cols->item(0)->nodeValue;
141
+ $value = $cols->item(1)->nodeValue;
142
+
143
+ if($value && $i){
144
+ $q=" insert ignore into ".$tmp_table_sorted." (store_product_id, sinch_product_id, feature_name, text) values(".$store_product_id.",".$sinch_product_id.",'".$name."','".$value."')";
145
+ $this->tep_db_query($q);
146
+ }
147
+ //mysqli_real_escape_string($name)
148
+ $i++;
149
+ }
150
+ }
151
+ }
152
+ private function tep_db_query($q){
153
+ $resource = Mage::getResourceSingleton('catalog/product');
154
+ $connection = $resource->getReadConnection();
155
+ $result = $connection->query($q);
156
+ return($result);
157
+ }
158
+
159
+ }
app/code/local/Bintime/Sinchimport/Block/Product/View/Media.php CHANGED
@@ -1,18 +1,18 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_Product_View_Media extends Mage_Catalog_Block_Product_View_Media
3
- {
4
- public function getGalleryUrl($image=null)
5
- {
6
- if (substr($image['url'],0,4) != 'http') {
7
- $params = array('id'=>$this->getProduct()->getId());
8
- if ($image) {
9
- $params['image'] = $image->getValueId();
10
- return $this->getUrl('*/*/gallery', $params);
11
- }
12
- return $this->getUrl('*/*/gallery', $params);
13
- }else{
14
- return $image['url'];
15
- }
16
- }
17
-
18
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Block_Product_View_Media extends Mage_Catalog_Block_Product_View_Media
3
+ {
4
+ public function getGalleryUrl($image=null)
5
+ {
6
+ if (substr($image['url'],0,4) != 'http') {
7
+ $params = array('id'=>$this->getProduct()->getId());
8
+ if ($image) {
9
+ $params['image'] = $image->getValueId();
10
+ return $this->getUrl('*/*/gallery', $params);
11
+ }
12
+ return $this->getUrl('*/*/gallery', $params);
13
+ }else{
14
+ return $image['url'];
15
+ }
16
+ }
17
+
18
+ }
app/code/local/Bintime/Sinchimport/Block/Startimportbutton.php CHANGED
@@ -1,343 +1,343 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_Startimportbutton extends Mage_Adminhtml_Block_System_Config_Form_Field
3
- {
4
-
5
- protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
6
- {
7
- $this->setElement($element);
8
- $url = $this->getUrl('sinchimport/index'); //
9
- $this->setElement($element);
10
-
11
- $html = $this->_appendJs();
12
-
13
- $html .= '<div id="sinchimport_status_template" name="sinchimport_status_template" style="display:none">';//none
14
- $html .= $this->_getStatusTemplateHtml();
15
- $html .= '</div>';
16
-
17
- $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
18
- ->setType('button')
19
- ->setClass('scalable')
20
- ->setLabel('Force Import now')
21
- ->setOnClick("start_sinch_import()") //setLocation('$url')
22
- ->toHtml();
23
- $safe_mode_set = ini_get('safe_mode');
24
- if($safe_mode_set){
25
- $html .="<p class='sinch-error'><b>You can't start import (safe_mode is 'On'. set safe_mode = Off in php.ini )<b></p>";
26
- }else{
27
- $html .= $start_import_button;
28
- }
29
- // $html .= $url;
30
- $dataConf = Mage::getConfig();//('sinchimport_root');//getConfig()->getNode()->asXML();
31
- // $html .= "<pre>".var_export($dataConf, true)."</pre>";
32
- $import=Mage::getModel('sinchimport/sinch');
33
- $last_import=$import->getDataOfLatestImport();
34
- $last_imp_status=$last_import['global_status_import'];
35
- if($last_imp_status=='Failed'){
36
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-error">The import has failed. Please ensure that you are using the correct settings. Last step was "'.$last_import['detail_status_import'].'"<br> Error reporting : "'.$last_import['error_report_message'].'"</p></div>';
37
- }elseif($last_imp_status=='Successful'){
38
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-success">'.$last_import['number_of_products'].' products imported succesfully!</p></div>';
39
- }elseif($last_imp_status=='Run'){
40
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p>Import is running now</p></div>';
41
- }else{
42
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"></div>';
43
- }
44
-
45
- return $html;
46
- }
47
-
48
- protected function _getStatusTemplateHtml()
49
- {
50
- $run_pic=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif";
51
- $html="
52
- <ul>
53
- <li>
54
- Start Import
55
- &nbsp
56
- <span id='sinchimport_start_import'>
57
- <img src='".$run_pic."'
58
- alt='Sinch Import run' />
59
- </span>
60
- </li>
61
- <li>
62
- Download files
63
- &nbsp
64
- <span id='sinchimport_upload_files'>
65
- <img src='".$run_pic."'
66
- alt='Download files' />
67
- </span>
68
- </li>
69
- <li>
70
- Parse Categories
71
- &nbsp
72
- <span id='sinchimport_parse_categories'>
73
- <img src='".$run_pic."'
74
- alt='Parse Categories' />
75
- </span>
76
- </li>
77
- <li>
78
- Parse Category Features
79
- &nbsp
80
- <span id='sinchimport_parse_category_features'>
81
- <img src='".$run_pic."'
82
- alt='Parse Category Features' />
83
- </span>
84
- </li>
85
- <li>
86
- Parse Distributors
87
- &nbsp
88
- <span id='sinchimport_parse_distributors'>
89
- <img src='".$run_pic."'
90
- alt='Parse Distributors' />
91
- </span>
92
- </li>
93
- <li>
94
- Parse EAN Codes
95
- &nbsp
96
- <span id='sinchimport_parse_ean_codes'>
97
- <img src='".$run_pic."'
98
- alt='Parse EAN Codes' />
99
- </span>
100
- </li>
101
- <li>
102
- Parse Manufacturers
103
- &nbsp
104
- <span id='sinchimport_parse_manufacturers'>
105
- <img src='".$run_pic."'
106
- alt='Parse Manufacturers'' />
107
- </span>
108
- </li>
109
- <li>
110
- Parse Related Products
111
- &nbsp
112
- <span id='sinchimport_parse_related_products'>
113
- <img src='".$run_pic."'
114
- alt='Parse Related Products' />
115
- </span>
116
- </li>
117
- <li>
118
- Parse Product Features
119
- &nbsp
120
- <span id='sinchimport_parse_product_features'>
121
- <img src='".$run_pic."'
122
- alt='Parse Product Features' />
123
- </span>
124
- </li>
125
- <li>
126
- Parse Products
127
- &nbsp
128
- <span id='sinchimport_parse_products'>
129
- <img src='".$run_pic."'
130
- alt='Parse Products' />
131
- </span>
132
- </li>
133
- <li>
134
- Parse Pictures Gallery
135
- &nbsp
136
- <span id='sinchimport_parse_pictures_gallery'>
137
- <img src='".$run_pic."'
138
- alt='Parse Pictures Gallery' />
139
- </span>
140
- </li>
141
- <li>
142
- Parse Restricted Values
143
- &nbsp
144
- <span id='sinchimport_parse_restricted_values'>
145
- <img src='".$run_pic."'
146
- alt='Parse Restricted Values' />
147
- </span>
148
- </li>
149
- <li>
150
- Parse Stock And Prices
151
- &nbsp
152
- <span id='sinchimport_parse_stock_and_prices'>
153
- <img src='".$run_pic."'
154
- alt='Parse Stock And Prices' />
155
- </span>
156
- </li>
157
- <li>
158
- Generate category filters
159
- &nbsp
160
- <span id='sinchimport_generate_category_filters'>
161
- <img src='".$run_pic."'
162
- alt='Generate category filters' />
163
- </span>
164
- </li>
165
- <li>
166
- Indexing data
167
- &nbsp
168
- <span id='sinchimport_indexing_data'>
169
- <img src='".$run_pic."'
170
- alt='Indexing data' />
171
- </span>
172
- </li>
173
- <li>
174
- Import finished
175
- &nbsp
176
- <span id='sinchimport_import_finished'>
177
- <img src='".$run_pic."'
178
- alt='Import finished' />
179
- </span>
180
- </li>
181
-
182
- </ul>
183
- ";
184
- return $html;
185
- }
186
-
187
- protected function _appendJs()
188
- {
189
- $post_url=$this->getUrl('sinchimport/ajax');
190
- $post_url_upd=$this->getUrl('sinchimport/ajax/UpdateStatus');
191
- $html = "
192
- <script>
193
- function start_sinch_import(){
194
- set_run_icon();
195
- status_div=document.getElementById('sinchimport_status_template');
196
- curr_status_div=document.getElementById('sinchimport_current_status_message');
197
- curr_status_div.style.display='none';
198
- status_div.style.display='';
199
- // status_div.innerHTML='';
200
- sinch = new Sinch('$post_url','$post_url_upd');
201
- sinch.startSinchImport();
202
-
203
- //
204
- }
205
- function set_run_icon(){
206
- run_pic='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif\""."/>';
207
- document.getElementById('sinchimport_start_import').innerHTML=run_pic;
208
- document.getElementById('sinchimport_upload_files').innerHTML=run_pic;
209
- document.getElementById('sinchimport_parse_categories').innerHTML=run_pic;
210
- document.getElementById('sinchimport_parse_category_features').innerHTML=run_pic;
211
- document.getElementById('sinchimport_parse_distributors').innerHTML=run_pic;
212
- document.getElementById('sinchimport_parse_ean_codes').innerHTML=run_pic;
213
- document.getElementById('sinchimport_parse_manufacturers').innerHTML=run_pic;
214
- document.getElementById('sinchimport_parse_related_products').innerHTML=run_pic;
215
- document.getElementById('sinchimport_parse_product_features').innerHTML=run_pic;
216
- document.getElementById('sinchimport_parse_products').innerHTML=run_pic;
217
- document.getElementById('sinchimport_parse_pictures_gallery').innerHTML=run_pic;
218
- document.getElementById('sinchimport_parse_restricted_values').innerHTML=run_pic;
219
- document.getElementById('sinchimport_parse_stock_and_prices').innerHTML=run_pic;
220
- document.getElementById('sinchimport_generate_category_filters').innerHTML=run_pic;
221
- document.getElementById('sinchimport_indexing_data').innerHTML=run_pic;
222
- document.getElementById('sinchimport_import_finished').innerHTML=run_pic;
223
-
224
-
225
-
226
- }
227
- var Sinch = Class.create();
228
- Sinch.prototype = {
229
-
230
- initialize: function(postUrl, postUrlUpd) {
231
- this.postUrl = postUrl; //'https://techatcost.com/purchases/ajax/';
232
- this.postUrlUpd = postUrlUpd;
233
- this.failureUrl = document.URL;
234
- // unique user session ID
235
- this.SID = null;
236
- // object with event message data
237
- this.objectMsg = null;
238
- this.prevMsg = '';
239
- // interval object
240
- this.updateTimer = null;
241
- // default shipping code. Display on errors
242
-
243
- elem = 'checkoutSteps';
244
- clickableEntity = '.head';
245
-
246
- // overwrite Accordion class method
247
- var headers = $$('#' + elem + ' .section ' + clickableEntity);
248
- headers.each(function(header) {
249
- Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
250
- }.bind(this));
251
- },
252
- startSinchImport: function () {
253
- _this = this;
254
- new Ajax.Request(this.postUrl,
255
- {
256
- method:'post',
257
- parameters: '',
258
- requestTimeout: 10,
259
- /*
260
- onLoading:function(){
261
- alert('onLoading');
262
- },
263
- onLoaded:function(){
264
- alert('onLoaded');
265
- },
266
- */
267
- onSuccess: function(transport) {
268
- var response = transport.responseText || null;
269
- _this.SID = response;
270
- if (_this.SID) {
271
- _this.updateTimer = setInterval(function(){_this.updateEvent();},20000);
272
- $('session_id').value = _this.SID;
273
- } else {
274
- alert('Can not get your session ID. Please reload the page!');
275
- }
276
- },
277
- onTimeout: function() { alert('Can not get your session ID. Timeout!'); },
278
- onFailure: function() { alert('Something went wrong...') }
279
- });
280
-
281
- },
282
-
283
- updateEvent: function () {
284
- _this = this;
285
- new Ajax.Request(this.postUrlUpd,
286
- {
287
- method: 'post',
288
- parameters: {session_id: this.SID},
289
- onSuccess: function(transport) {
290
- _this.objectMsg = transport.responseText.evalJSON();
291
- _this.prevMsg = _this.objectMsg.message;
292
- if(_this.prevMsg!=''){
293
- _this.updateStatusHtml();
294
- }
295
-
296
- if (_this.objectMsg.error == 1) {
297
- // Do something on error
298
- _this.clearUpdateInterval();
299
- }
300
-
301
- if (_this.objectMsg.finished == 1) {
302
- _this.objectMsg.message='Import finished';
303
- _this.updateStatusHtml();
304
- _this.clearUpdateInterval();
305
-
306
- }
307
-
308
- },
309
- onFailure: this.ajaxFailure.bind(),
310
- });
311
- },
312
-
313
- updateStatusHtml: function(){
314
- message=this.objectMsg.message.toLowerCase();
315
- mess_id='sinchimport_'+message.replace(/\s+/g, '_');
316
- if(!document.getElementById(mess_id)){
317
- // alert(mess_id+' - not exist');
318
- }
319
- else{
320
- // alert (mess_id+' - exist');
321
- $(mess_id).innerHTML='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_yes.gif"."\"/>'
322
- }
323
- htm=$('sinchimport_status_template').innerHTML;
324
- // $('sinchimport_status_template').innerHTML=htm+'<br>'+this.objectMsg.message;
325
- },
326
-
327
- ajaxFailure: function(){
328
- this.clearUpdateInterval();
329
- location.href = this.failureUrl;
330
- },
331
-
332
- clearUpdateInterval: function () {
333
- clearInterval(this.updateTimer);
334
- },
335
-
336
-
337
- }
338
- </script>
339
- ";
340
- return $html;
341
- }
342
-
343
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Block_Startimportbutton extends Mage_Adminhtml_Block_System_Config_Form_Field
3
+ {
4
+
5
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
6
+ {
7
+ $this->setElement($element);
8
+ $url = $this->getUrl('sinchimport/index'); //
9
+ $this->setElement($element);
10
+
11
+ $html = $this->_appendJs();
12
+
13
+ $html .= '<div id="sinchimport_status_template" name="sinchimport_status_template" style="display:none">';//none
14
+ $html .= $this->_getStatusTemplateHtml();
15
+ $html .= '</div>';
16
+
17
+ $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
18
+ ->setType('button')
19
+ ->setClass('scalable')
20
+ ->setLabel('Force Import now')
21
+ ->setOnClick("start_sinch_import()") //setLocation('$url')
22
+ ->toHtml();
23
+ $safe_mode_set = ini_get('safe_mode');
24
+ if($safe_mode_set){
25
+ $html .="<p class='sinch-error'><b>You can't start import (safe_mode is 'On'. set safe_mode = Off in php.ini )<b></p>";
26
+ }else{
27
+ $html .= $start_import_button;
28
+ }
29
+ // $html .= $url;
30
+ $dataConf = Mage::getConfig();//('sinchimport_root');//getConfig()->getNode()->asXML();
31
+ // $html .= "<pre>".var_export($dataConf, true)."</pre>";
32
+ $import=Mage::getModel('sinchimport/sinch');
33
+ $last_import=$import->getDataOfLatestImport();
34
+ $last_imp_status=$last_import['global_status_import'];
35
+ if($last_imp_status=='Failed'){
36
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-error">The import has failed. Please ensure that you are using the correct settings. Last step was "'.$last_import['detail_status_import'].'"<br> Error reporting : "'.$last_import['error_report_message'].'"</p></div>';
37
+ }elseif($last_imp_status=='Successful'){
38
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-success">'.$last_import['number_of_products'].' products imported succesfully!</p></div>';
39
+ }elseif($last_imp_status=='Run'){
40
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p>Import is running now</p></div>';
41
+ }else{
42
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"></div>';
43
+ }
44
+
45
+ return $html;
46
+ }
47
+
48
+ protected function _getStatusTemplateHtml()
49
+ {
50
+ $run_pic=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif";
51
+ $html="
52
+ <ul>
53
+ <li>
54
+ Start Import
55
+ &nbsp
56
+ <span id='sinchimport_start_import'>
57
+ <img src='".$run_pic."'
58
+ alt='Sinch Import run' />
59
+ </span>
60
+ </li>
61
+ <li>
62
+ Download files
63
+ &nbsp
64
+ <span id='sinchimport_upload_files'>
65
+ <img src='".$run_pic."'
66
+ alt='Download files' />
67
+ </span>
68
+ </li>
69
+ <li>
70
+ Parse Categories
71
+ &nbsp
72
+ <span id='sinchimport_parse_categories'>
73
+ <img src='".$run_pic."'
74
+ alt='Parse Categories' />
75
+ </span>
76
+ </li>
77
+ <li>
78
+ Parse Category Features
79
+ &nbsp
80
+ <span id='sinchimport_parse_category_features'>
81
+ <img src='".$run_pic."'
82
+ alt='Parse Category Features' />
83
+ </span>
84
+ </li>
85
+ <li>
86
+ Parse Distributors
87
+ &nbsp
88
+ <span id='sinchimport_parse_distributors'>
89
+ <img src='".$run_pic."'
90
+ alt='Parse Distributors' />
91
+ </span>
92
+ </li>
93
+ <li>
94
+ Parse EAN Codes
95
+ &nbsp
96
+ <span id='sinchimport_parse_ean_codes'>
97
+ <img src='".$run_pic."'
98
+ alt='Parse EAN Codes' />
99
+ </span>
100
+ </li>
101
+ <li>
102
+ Parse Manufacturers
103
+ &nbsp
104
+ <span id='sinchimport_parse_manufacturers'>
105
+ <img src='".$run_pic."'
106
+ alt='Parse Manufacturers'' />
107
+ </span>
108
+ </li>
109
+ <li>
110
+ Parse Related Products
111
+ &nbsp
112
+ <span id='sinchimport_parse_related_products'>
113
+ <img src='".$run_pic."'
114
+ alt='Parse Related Products' />
115
+ </span>
116
+ </li>
117
+ <li>
118
+ Parse Product Features
119
+ &nbsp
120
+ <span id='sinchimport_parse_product_features'>
121
+ <img src='".$run_pic."'
122
+ alt='Parse Product Features' />
123
+ </span>
124
+ </li>
125
+ <li>
126
+ Parse Products
127
+ &nbsp
128
+ <span id='sinchimport_parse_products'>
129
+ <img src='".$run_pic."'
130
+ alt='Parse Products' />
131
+ </span>
132
+ </li>
133
+ <li>
134
+ Parse Pictures Gallery
135
+ &nbsp
136
+ <span id='sinchimport_parse_pictures_gallery'>
137
+ <img src='".$run_pic."'
138
+ alt='Parse Pictures Gallery' />
139
+ </span>
140
+ </li>
141
+ <li>
142
+ Parse Restricted Values
143
+ &nbsp
144
+ <span id='sinchimport_parse_restricted_values'>
145
+ <img src='".$run_pic."'
146
+ alt='Parse Restricted Values' />
147
+ </span>
148
+ </li>
149
+ <li>
150
+ Parse Stock And Prices
151
+ &nbsp
152
+ <span id='sinchimport_parse_stock_and_prices'>
153
+ <img src='".$run_pic."'
154
+ alt='Parse Stock And Prices' />
155
+ </span>
156
+ </li>
157
+ <li>
158
+ Generate category filters
159
+ &nbsp
160
+ <span id='sinchimport_generate_category_filters'>
161
+ <img src='".$run_pic."'
162
+ alt='Generate category filters' />
163
+ </span>
164
+ </li>
165
+ <li>
166
+ Indexing data
167
+ &nbsp
168
+ <span id='sinchimport_indexing_data'>
169
+ <img src='".$run_pic."'
170
+ alt='Indexing data' />
171
+ </span>
172
+ </li>
173
+ <li>
174
+ Import finished
175
+ &nbsp
176
+ <span id='sinchimport_import_finished'>
177
+ <img src='".$run_pic."'
178
+ alt='Import finished' />
179
+ </span>
180
+ </li>
181
+
182
+ </ul>
183
+ ";
184
+ return $html;
185
+ }
186
+
187
+ protected function _appendJs()
188
+ {
189
+ $post_url=$this->getUrl('sinchimport/ajax');
190
+ $post_url_upd=$this->getUrl('sinchimport/ajax/UpdateStatus');
191
+ $html = "
192
+ <script>
193
+ function start_sinch_import(){
194
+ set_run_icon();
195
+ status_div=document.getElementById('sinchimport_status_template');
196
+ curr_status_div=document.getElementById('sinchimport_current_status_message');
197
+ curr_status_div.style.display='none';
198
+ status_div.style.display='';
199
+ // status_div.innerHTML='';
200
+ sinch = new Sinch('$post_url','$post_url_upd');
201
+ sinch.startSinchImport();
202
+
203
+ //
204
+ }
205
+ function set_run_icon(){
206
+ run_pic='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif\""."/>';
207
+ document.getElementById('sinchimport_start_import').innerHTML=run_pic;
208
+ document.getElementById('sinchimport_upload_files').innerHTML=run_pic;
209
+ document.getElementById('sinchimport_parse_categories').innerHTML=run_pic;
210
+ document.getElementById('sinchimport_parse_category_features').innerHTML=run_pic;
211
+ document.getElementById('sinchimport_parse_distributors').innerHTML=run_pic;
212
+ document.getElementById('sinchimport_parse_ean_codes').innerHTML=run_pic;
213
+ document.getElementById('sinchimport_parse_manufacturers').innerHTML=run_pic;
214
+ document.getElementById('sinchimport_parse_related_products').innerHTML=run_pic;
215
+ document.getElementById('sinchimport_parse_product_features').innerHTML=run_pic;
216
+ document.getElementById('sinchimport_parse_products').innerHTML=run_pic;
217
+ document.getElementById('sinchimport_parse_pictures_gallery').innerHTML=run_pic;
218
+ document.getElementById('sinchimport_parse_restricted_values').innerHTML=run_pic;
219
+ document.getElementById('sinchimport_parse_stock_and_prices').innerHTML=run_pic;
220
+ document.getElementById('sinchimport_generate_category_filters').innerHTML=run_pic;
221
+ document.getElementById('sinchimport_indexing_data').innerHTML=run_pic;
222
+ document.getElementById('sinchimport_import_finished').innerHTML=run_pic;
223
+
224
+
225
+
226
+ }
227
+ var Sinch = Class.create();
228
+ Sinch.prototype = {
229
+
230
+ initialize: function(postUrl, postUrlUpd) {
231
+ this.postUrl = postUrl; //'https://techatcost.com/purchases/ajax/';
232
+ this.postUrlUpd = postUrlUpd;
233
+ this.failureUrl = document.URL;
234
+ // unique user session ID
235
+ this.SID = null;
236
+ // object with event message data
237
+ this.objectMsg = null;
238
+ this.prevMsg = '';
239
+ // interval object
240
+ this.updateTimer = null;
241
+ // default shipping code. Display on errors
242
+
243
+ elem = 'checkoutSteps';
244
+ clickableEntity = '.head';
245
+
246
+ // overwrite Accordion class method
247
+ var headers = $$('#' + elem + ' .section ' + clickableEntity);
248
+ headers.each(function(header) {
249
+ Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
250
+ }.bind(this));
251
+ },
252
+ startSinchImport: function () {
253
+ _this = this;
254
+ new Ajax.Request(this.postUrl,
255
+ {
256
+ method:'post',
257
+ parameters: '',
258
+ requestTimeout: 10,
259
+ /*
260
+ onLoading:function(){
261
+ alert('onLoading');
262
+ },
263
+ onLoaded:function(){
264
+ alert('onLoaded');
265
+ },
266
+ */
267
+ onSuccess: function(transport) {
268
+ var response = transport.responseText || null;
269
+ _this.SID = response;
270
+ if (_this.SID) {
271
+ _this.updateTimer = setInterval(function(){_this.updateEvent();},20000);
272
+ $('session_id').value = _this.SID;
273
+ } else {
274
+ alert('Can not get your session ID. Please reload the page!');
275
+ }
276
+ },
277
+ onTimeout: function() { alert('Can not get your session ID. Timeout!'); },
278
+ onFailure: function() { alert('Something went wrong...') }
279
+ });
280
+
281
+ },
282
+
283
+ updateEvent: function () {
284
+ _this = this;
285
+ new Ajax.Request(this.postUrlUpd,
286
+ {
287
+ method: 'post',
288
+ parameters: {session_id: this.SID},
289
+ onSuccess: function(transport) {
290
+ _this.objectMsg = transport.responseText.evalJSON();
291
+ _this.prevMsg = _this.objectMsg.message;
292
+ if(_this.prevMsg!=''){
293
+ _this.updateStatusHtml();
294
+ }
295
+
296
+ if (_this.objectMsg.error == 1) {
297
+ // Do something on error
298
+ _this.clearUpdateInterval();
299
+ }
300
+
301
+ if (_this.objectMsg.finished == 1) {
302
+ _this.objectMsg.message='Import finished';
303
+ _this.updateStatusHtml();
304
+ _this.clearUpdateInterval();
305
+
306
+ }
307
+
308
+ },
309
+ onFailure: this.ajaxFailure.bind(),
310
+ });
311
+ },
312
+
313
+ updateStatusHtml: function(){
314
+ message=this.objectMsg.message.toLowerCase();
315
+ mess_id='sinchimport_'+message.replace(/\s+/g, '_');
316
+ if(!document.getElementById(mess_id)){
317
+ // alert(mess_id+' - not exist');
318
+ }
319
+ else{
320
+ // alert (mess_id+' - exist');
321
+ $(mess_id).innerHTML='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_yes.gif"."\"/>'
322
+ }
323
+ htm=$('sinchimport_status_template').innerHTML;
324
+ // $('sinchimport_status_template').innerHTML=htm+'<br>'+this.objectMsg.message;
325
+ },
326
+
327
+ ajaxFailure: function(){
328
+ this.clearUpdateInterval();
329
+ location.href = this.failureUrl;
330
+ },
331
+
332
+ clearUpdateInterval: function () {
333
+ clearInterval(this.updateTimer);
334
+ },
335
+
336
+
337
+ }
338
+ </script>
339
+ ";
340
+ return $html;
341
+ }
342
+
343
+ }
app/code/local/Bintime/Sinchimport/Block/Startstockpriceimportbutton.php CHANGED
@@ -1,137 +1,137 @@
1
- <?php
2
- class Bintime_Sinchimport_Block_Startstockpriceimportbutton extends Mage_Adminhtml_Block_System_Config_Form_Field
3
- {
4
-
5
- protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
6
- {
7
- $this->setElement($element);
8
- $url = $this->getUrl('sinchimport/index'); //
9
- $this->setElement($element);
10
-
11
- $html = $this->_appendJs();
12
-
13
- $html .= '<div id="sinchimport_stock_price_status_template" name="sinchimport_stock_price_status_template" style="display:none">';//none
14
- $html .= $this->_getStatusTemplateHtml();
15
- $html .= '</div>';
16
-
17
- $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
18
- ->setType('button')
19
- ->setClass('scalable')
20
- ->setLabel('Force Stock & Prices Import now')
21
- ->setOnClick("start_stock_price_sinch_import()") //setLocation('$url')
22
- ->toHtml();
23
- // $html .= $url;
24
- $dataConf = Mage::getConfig();//('sinchimport_root');//getConfig()->getNode()->asXML();
25
- // $html .= "<pre>".var_export($dataConf, true)."</pre>";
26
- $import=Mage::getModel('sinchimport/sinch');
27
-
28
- $safe_mode_set = ini_get('safe_mode');
29
- if($safe_mode_set){
30
- $html .="<p class='sinch-error'><b>You can't start import (safe_mode is 'On'. set safe_mode = Off in php.ini )<b></p>";
31
- }elseif(!$import->is_full_import_have_been_run()){
32
- $html .="Full import have never finished with success";
33
- }else{
34
- $html .= $start_import_button;
35
- }
36
-
37
- $last_import=$import->getDataOfLatestImport();
38
- $last_imp_status=$last_import['global_status_import'];
39
- if($last_imp_status=='Failed'){
40
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-error">The import has failed. Please ensure that you are using the correct settings. Last step was "'.$last_import['detail_status_import'].'"<br> Error reporting : "'.$last_import['error_report_message'].'"</p></div>';
41
- }elseif($last_imp_status=='Successful'){
42
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-success">'.$last_import['number_of_products'].' products imported succesfully!</p></div>';
43
- }elseif($last_imp_status=='Run'){
44
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p>Import is running now</p></div>';
45
- }else{
46
- $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"></div>';
47
- }
48
-
49
- return $html;
50
- }
51
-
52
- protected function _getStatusTemplateHtml()
53
- {
54
- $run_pic=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif";
55
- $html="
56
- <ul>
57
- <li>
58
- Start Import
59
- &nbsp
60
- <span id='sinchimport_stock_price_start_import'>
61
- <img src='".$run_pic."'
62
- alt='Sinch Import run' />
63
- </span>
64
- </li>
65
- <li>
66
- Download files
67
- &nbsp
68
- <span id='sinchimport_stock_price_upload_files'>
69
- <img src='".$run_pic."'
70
- alt='Download files' />
71
- </span>
72
- </li>
73
- <li>
74
- Parse Stock And Prices
75
- &nbsp
76
- <span id='sinchimport_stock_price_parse_products'>
77
- <img src='".$run_pic."'
78
- alt='Parse Stock And Prices' />
79
- </span>
80
- </li>
81
- <li>
82
- Indexing data
83
- &nbsp
84
- <span id='sinchimport_stock_price_indexing_data'>
85
- <img src='".$run_pic."'
86
- alt='Indexing data' />
87
- </span>
88
- </li>
89
- <li>
90
- Import finished
91
- &nbsp
92
- <span id='sinchimport_stock_price_finish_import'>
93
- <img src='".$run_pic."'
94
- alt='Import finished' />
95
- </span>
96
- </li>
97
-
98
- </ul>
99
- ";
100
- return $html;
101
- }
102
-
103
- protected function _appendJs()
104
- {
105
- $post_url=$this->getUrl('sinchimport/ajax/stockPrice');
106
- $post_url_upd=$this->getUrl('sinchimport/ajax/UpdateStatus');
107
- $html = "
108
- <script>
109
- function start_stock_price_sinch_import(){
110
- set_stock_price_run_icon();
111
- st_pr_status_div=document.getElementById('sinchimport_stock_price_status_template');
112
- curr_status_div=document.getElementById('sinchimport_current_status_message');
113
- curr_status_div.style.display='none';
114
- st_pr_status_div.style.display='';
115
- // status_div.innerHTML='';
116
- sinch = new Sinch('$post_url','$post_url_upd');
117
- sinch.startSinchImport();
118
-
119
- //
120
- }
121
-
122
- function set_stock_price_run_icon(){
123
- run_pic='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif\""."/>';
124
- document.getElementById('sinchimport_stock_price_start_import').innerHTML=run_pic;
125
- document.getElementById('sinchimport_stock_price_upload_files').innerHTML=run_pic;
126
- document.getElementById('sinchimport_stock_price_parse_products').innerHTML=run_pic;
127
- document.getElementById('sinchimport_stock_price_indexing_data').innerHTML=run_pic;
128
- document.getElementById('sinchimport_stock_price_finish_import').innerHTML=run_pic;
129
-
130
- }
131
-
132
- </script>
133
- ";
134
- return $html;
135
- }
136
-
137
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Block_Startstockpriceimportbutton extends Mage_Adminhtml_Block_System_Config_Form_Field
3
+ {
4
+
5
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
6
+ {
7
+ $this->setElement($element);
8
+ $url = $this->getUrl('sinchimport/index'); //
9
+ $this->setElement($element);
10
+
11
+ $html = $this->_appendJs();
12
+
13
+ $html .= '<div id="sinchimport_stock_price_status_template" name="sinchimport_stock_price_status_template" style="display:none">';//none
14
+ $html .= $this->_getStatusTemplateHtml();
15
+ $html .= '</div>';
16
+
17
+ $start_import_button = $this->getLayout()->createBlock('adminhtml/widget_button')
18
+ ->setType('button')
19
+ ->setClass('scalable')
20
+ ->setLabel('Force Stock & Prices Import now')
21
+ ->setOnClick("start_stock_price_sinch_import()") //setLocation('$url')
22
+ ->toHtml();
23
+ // $html .= $url;
24
+ $dataConf = Mage::getConfig();//('sinchimport_root');//getConfig()->getNode()->asXML();
25
+ // $html .= "<pre>".var_export($dataConf, true)."</pre>";
26
+ $import=Mage::getModel('sinchimport/sinch');
27
+
28
+ $safe_mode_set = ini_get('safe_mode');
29
+ if($safe_mode_set){
30
+ $html .="<p class='sinch-error'><b>You can't start import (safe_mode is 'On'. set safe_mode = Off in php.ini )<b></p>";
31
+ }elseif(!$import->is_full_import_have_been_run()){
32
+ $html .="Full import have never finished with success";
33
+ }else{
34
+ $html .= $start_import_button;
35
+ }
36
+
37
+ $last_import=$import->getDataOfLatestImport();
38
+ $last_imp_status=$last_import['global_status_import'];
39
+ if($last_imp_status=='Failed'){
40
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-error">The import has failed. Please ensure that you are using the correct settings. Last step was "'.$last_import['detail_status_import'].'"<br> Error reporting : "'.$last_import['error_report_message'].'"</p></div>';
41
+ }elseif($last_imp_status=='Successful'){
42
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p class="sinch-success">'.$last_import['number_of_products'].' products imported succesfully!</p></div>';
43
+ }elseif($last_imp_status=='Run'){
44
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"><br><br><hr/><p>Import is running now</p></div>';
45
+ }else{
46
+ $html.='<div id="sinchimport_current_status_message" name="sinchimport_current_status_message" style="display:true"></div>';
47
+ }
48
+
49
+ return $html;
50
+ }
51
+
52
+ protected function _getStatusTemplateHtml()
53
+ {
54
+ $run_pic=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif";
55
+ $html="
56
+ <ul>
57
+ <li>
58
+ Start Import
59
+ &nbsp
60
+ <span id='sinchimport_stock_price_start_import'>
61
+ <img src='".$run_pic."'
62
+ alt='Sinch Import run' />
63
+ </span>
64
+ </li>
65
+ <li>
66
+ Download files
67
+ &nbsp
68
+ <span id='sinchimport_stock_price_upload_files'>
69
+ <img src='".$run_pic."'
70
+ alt='Download files' />
71
+ </span>
72
+ </li>
73
+ <li>
74
+ Parse Stock And Prices
75
+ &nbsp
76
+ <span id='sinchimport_stock_price_parse_products'>
77
+ <img src='".$run_pic."'
78
+ alt='Parse Stock And Prices' />
79
+ </span>
80
+ </li>
81
+ <li>
82
+ Indexing data
83
+ &nbsp
84
+ <span id='sinchimport_stock_price_indexing_data'>
85
+ <img src='".$run_pic."'
86
+ alt='Indexing data' />
87
+ </span>
88
+ </li>
89
+ <li>
90
+ Import finished
91
+ &nbsp
92
+ <span id='sinchimport_stock_price_finish_import'>
93
+ <img src='".$run_pic."'
94
+ alt='Import finished' />
95
+ </span>
96
+ </li>
97
+
98
+ </ul>
99
+ ";
100
+ return $html;
101
+ }
102
+
103
+ protected function _appendJs()
104
+ {
105
+ $post_url=$this->getUrl('sinchimport/ajax/stockPrice');
106
+ $post_url_upd=$this->getUrl('sinchimport/ajax/UpdateStatus');
107
+ $html = "
108
+ <script>
109
+ function start_stock_price_sinch_import(){
110
+ set_stock_price_run_icon();
111
+ st_pr_status_div=document.getElementById('sinchimport_stock_price_status_template');
112
+ curr_status_div=document.getElementById('sinchimport_current_status_message');
113
+ curr_status_div.style.display='none';
114
+ st_pr_status_div.style.display='';
115
+ // status_div.innerHTML='';
116
+ sinch = new Sinch('$post_url','$post_url_upd');
117
+ sinch.startSinchImport();
118
+
119
+ //
120
+ }
121
+
122
+ function set_stock_price_run_icon(){
123
+ run_pic='<img src=\"".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN)."adminhtml/default/default/images/sinchimport_run.gif\""."/>';
124
+ document.getElementById('sinchimport_stock_price_start_import').innerHTML=run_pic;
125
+ document.getElementById('sinchimport_stock_price_upload_files').innerHTML=run_pic;
126
+ document.getElementById('sinchimport_stock_price_parse_products').innerHTML=run_pic;
127
+ document.getElementById('sinchimport_stock_price_indexing_data').innerHTML=run_pic;
128
+ document.getElementById('sinchimport_stock_price_finish_import').innerHTML=run_pic;
129
+
130
+ }
131
+
132
+ </script>
133
+ ";
134
+ return $html;
135
+ }
136
+
137
+ }
app/code/local/Bintime/Sinchimport/Helper/Data.php CHANGED
@@ -1,5 +1,5 @@
1
- <?php
2
- class Bintime_Sinchimport_Helper_Data extends Mage_Core_Helper_Abstract
3
- {
4
-
5
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+
5
+ }
app/code/local/Bintime/Sinchimport/Helper/Getdata.php CHANGED
@@ -1,185 +1,185 @@
1
- <?php
2
- /**
3
- *
4
- * Class achieves icecat description from Model by recieved SKU and manufacturer
5
- * @author Sergey Gozhedrianov <info@bintime.com>
6
- *
7
- */
8
- class Bintime_Sinchimport_Helper_Getdata extends Mage_Core_Helper_Abstract
9
- {
10
- private $iceCatModel;
11
- private $sinchModel;
12
- private $error;
13
- private $systemError;
14
-
15
- /**
16
- * Gets product Data and delegates it to Model
17
- * @param Mage_Catalog_Model_Product $_product
18
- * @return Bintime_Icecatimport_Helper_Getdata
19
- */
20
- public function getProductDescription($_product){
21
- $entityId = $_product->getEntityId();
22
- /* $sku = $_product->getData(Mage::getStoreConfig('icecat_root/icecat/sku_field'));
23
- $manufacturerId = $_product->getData(Mage::getStoreConfig('icecat_root/icecat/manufacturer'));
24
- if (Mage::getStoreConfig('icecat_root/icecat/manufacturer') == 'manufacturer'){
25
- $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
26
- ->setEntityTypeFilter($_product->getResource()->getTypeId())
27
- ->addFieldToFilter('attribute_code', 'manufacturer');
28
- $attribute = $attributes->getFirstItem()->setEntity($_product->getResource());
29
- $manufacturer = $attribute->getSource()->getOptionText($manufacturerId);
30
- }
31
- else {
32
- $manufacturer = $manufacturerId;
33
- }
34
- $locale = Mage::getStoreConfig('icecat_root/icecat/language');
35
-
36
- if ($locale == '0'){
37
- $systemLocale = explode("_", Mage::app()->getLocale()->getLocaleCode());
38
- $locale = $systemLocale[0];
39
- }
40
- $userLogin = Mage::getStoreConfig('icecat_root/icecat/login');
41
- $userPass = Mage::getStoreConfig('icecat_root/icecat/password');
42
-
43
- $this->iceCatModel = Mage::getSingleton('icecatimport/import');
44
-
45
- if (!$this->iceCatModel->getProductDescription($sku, $manufacturer, $locale, $userLogin, $userPass, $entityId)){
46
- $this->error = $this->iceCatModel->getErrorMessage();
47
- $this->systemError = $this->iceCatModel->getSystemError();
48
- return $this;
49
- }
50
- */
51
- $this->sinchModel = Mage::getSingleton('sinchimport/sinch');
52
- if(!$this->sinchModel->getProductDescription($entityId)){
53
- $this->error = $this->sinchModel->getErrorMessage();
54
- $this->systemError = $this->sinchModel->getSystemError();
55
- }
56
- return $this;
57
- }
58
- /**
59
- * returns true if error during data fetch occured else false
60
- */
61
- public function hasError(){
62
- if ($this->error || $this->systemError){
63
- return true;
64
- }
65
- return false;
66
- }
67
-
68
- /**
69
- * return error message
70
- */
71
- public function getError(){
72
- //show icecat error on product page return $this->error;
73
- }
74
-
75
- /**
76
- * return system error
77
- */
78
- public function hasSystemError(){
79
- if ($this->systemError){
80
- return $this->systemError;
81
- }
82
- return false;
83
- }
84
-
85
- public function getProductDescriptionList(){
86
- return $this->sinchModel->getProductDescriptionList();
87
- }
88
- public function getProductSpecifications(){
89
- return $this->sinchModel->getProductSpecifications();
90
- }
91
-
92
- public function getShortProductDescription(){
93
- return $this->sinchModel->getShortProductDescription();
94
- }
95
-
96
- public function getLowPicUrl(){
97
- return $this->sinchModel->getLowPicUrl();
98
- }
99
-
100
- public function getGalleryPhotos(){
101
- return $this->sinchModel->getGalleryPhotos();
102
- }
103
-
104
- public function getProductName(){
105
- return $this->sinchModel->getProductName();
106
- }
107
- public function getVendor(){
108
- return $this->sinchModel->getVendor();
109
- }
110
-
111
- public function getFullProductDescription(){
112
- return $this->sinchModel->getFullProductDescription();
113
- }
114
-
115
- public function getMPN(){
116
- return $this->sinchModel->getMPN();
117
- }
118
- public function getEAN(){
119
- return $this->sinchModel->getEAN();
120
- }
121
-
122
- /**
123
- * Form related products list according to store products
124
- */
125
- public function getRelatedProducts(){
126
- $relatedProducts =$this->sinchModel->getRelatedProducts();
127
- if (empty($relatedProducts)){
128
- return array();
129
- }
130
- $sku = 'sku';
131
- $collection = Mage::getModel('catalog/product')->getCollection();
132
-
133
- $filterArray = array();
134
- foreach($relatedProducts as $mpn => $valueArray){
135
- array_push($filterArray, array('attribute'=>$sku,'eq'=>$mpn));
136
- }
137
- $collection->addFieldToFilter($filterArray);
138
-
139
- $collection->addAttributeToSelect($sku);
140
- $collection->addAttributeToSelect('category_ids');
141
-
142
- $relatedProductsList = array();
143
- foreach ($collection as $product) {
144
- $categoryIds = $product->getCategoryIds();
145
- if(!empty($categoryIds)){
146
- if (is_array($categoryIds)){
147
- $catogoriesArray = $categoryIds;
148
- }
149
- if (is_string($categoryIds)){
150
- $catogoriesArray = explode(",",$product->getCategoryIds());
151
- }
152
- foreach($catogoriesArray as $categoryId){
153
- if (!array_key_exists($product->getData($sku), $relatedProducts)){
154
- continue;
155
- }
156
- $relatedProductInfo = $relatedProducts[$product->getData($sku)];
157
- $relatedProductInfo['mpn'] = $product->getData($sku);
158
- $relatedProductInfo['url'] = $product->getProductUrl();//preg_replace( '/\/\d+\/$/',"/".$categoryId."/",$product->getProductUrl());;
159
- if (!array_key_exists($categoryId, $relatedProductsList)){
160
- $relatedProductsList[$categoryId]= array();
161
- }
162
-
163
- array_push($relatedProductsList[$categoryId], $relatedProductInfo);
164
- }
165
- }
166
- else {
167
- if (!array_key_exists($product->getData($sku), $relatedProducts)){
168
- continue;
169
- }
170
- $relatedProductInfo = $relatedProducts[$product->getData($sku)];
171
- $relatedProductInfo['mpn'] = $product->getData($sku);
172
- $relatedProductInfo['url'] = $product->getProductUrl();//preg_replace( '/category\/\d+\/$/','',$product->getProductUrl());;
173
- if (!array_key_exists('a', $relatedProductsList)){
174
- $relatedProductsList['a']= array();
175
- }
176
-
177
- array_push($relatedProductsList['a'], $relatedProductInfo);
178
- }
179
- }
180
-
181
- return $relatedProductsList;
182
-
183
- }
184
-
185
- }
1
+ <?php
2
+ /**
3
+ *
4
+ * Class achieves icecat description from Model by recieved SKU and manufacturer
5
+ * @author Sergey Gozhedrianov <info@bintime.com>
6
+ *
7
+ */
8
+ class Bintime_Sinchimport_Helper_Getdata extends Mage_Core_Helper_Abstract
9
+ {
10
+ private $iceCatModel;
11
+ private $sinchModel;
12
+ private $error;
13
+ private $systemError;
14
+
15
+ /**
16
+ * Gets product Data and delegates it to Model
17
+ * @param Mage_Catalog_Model_Product $_product
18
+ * @return Bintime_Icecatimport_Helper_Getdata
19
+ */
20
+ public function getProductDescription($_product){
21
+ $entityId = $_product->getEntityId();
22
+ /* $sku = $_product->getData(Mage::getStoreConfig('icecat_root/icecat/sku_field'));
23
+ $manufacturerId = $_product->getData(Mage::getStoreConfig('icecat_root/icecat/manufacturer'));
24
+ if (Mage::getStoreConfig('icecat_root/icecat/manufacturer') == 'manufacturer'){
25
+ $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
26
+ ->setEntityTypeFilter($_product->getResource()->getTypeId())
27
+ ->addFieldToFilter('attribute_code', 'manufacturer');
28
+ $attribute = $attributes->getFirstItem()->setEntity($_product->getResource());
29
+ $manufacturer = $attribute->getSource()->getOptionText($manufacturerId);
30
+ }
31
+ else {
32
+ $manufacturer = $manufacturerId;
33
+ }
34
+ $locale = Mage::getStoreConfig('icecat_root/icecat/language');
35
+
36
+ if ($locale == '0'){
37
+ $systemLocale = explode("_", Mage::app()->getLocale()->getLocaleCode());
38
+ $locale = $systemLocale[0];
39
+ }
40
+ $userLogin = Mage::getStoreConfig('icecat_root/icecat/login');
41
+ $userPass = Mage::getStoreConfig('icecat_root/icecat/password');
42
+
43
+ $this->iceCatModel = Mage::getSingleton('icecatimport/import');
44
+
45
+ if (!$this->iceCatModel->getProductDescription($sku, $manufacturer, $locale, $userLogin, $userPass, $entityId)){
46
+ $this->error = $this->iceCatModel->getErrorMessage();
47
+ $this->systemError = $this->iceCatModel->getSystemError();
48
+ return $this;
49
+ }
50
+ */
51
+ $this->sinchModel = Mage::getSingleton('sinchimport/sinch');
52
+ if(!$this->sinchModel->getProductDescription($entityId)){
53
+ $this->error = $this->sinchModel->getErrorMessage();
54
+ $this->systemError = $this->sinchModel->getSystemError();
55
+ }
56
+ return $this;
57
+ }
58
+ /**
59
+ * returns true if error during data fetch occured else false
60
+ */
61
+ public function hasError(){
62
+ if ($this->error || $this->systemError){
63
+ return true;
64
+ }
65
+ return false;
66
+ }
67
+
68
+ /**
69
+ * return error message
70
+ */
71
+ public function getError(){
72
+ //show icecat error on product page return $this->error;
73
+ }
74
+
75
+ /**
76
+ * return system error
77
+ */
78
+ public function hasSystemError(){
79
+ if ($this->systemError){
80
+ return $this->systemError;
81
+ }
82
+ return false;
83
+ }
84
+
85
+ public function getProductDescriptionList(){
86
+ return $this->sinchModel->getProductDescriptionList();
87
+ }
88
+ public function getProductSpecifications(){
89
+ return $this->sinchModel->getProductSpecifications();
90
+ }
91
+
92
+ public function getShortProductDescription(){
93
+ return $this->sinchModel->getShortProductDescription();
94
+ }
95
+
96
+ public function getLowPicUrl(){
97
+ return $this->sinchModel->getLowPicUrl();
98
+ }
99
+
100
+ public function getGalleryPhotos(){
101
+ return $this->sinchModel->getGalleryPhotos();
102
+ }
103
+
104
+ public function getProductName(){
105
+ return $this->sinchModel->getProductName();
106
+ }
107
+ public function getVendor(){
108
+ return $this->sinchModel->getVendor();
109
+ }
110
+
111
+ public function getFullProductDescription(){
112
+ return $this->sinchModel->getFullProductDescription();
113
+ }
114
+
115
+ public function getMPN(){
116
+ return $this->sinchModel->getMPN();
117
+ }
118
+ public function getEAN(){
119
+ return $this->sinchModel->getEAN();
120
+ }
121
+
122
+ /**
123
+ * Form related products list according to store products
124
+ */
125
+ public function getRelatedProducts(){
126
+ $relatedProducts =$this->sinchModel->getRelatedProducts();
127
+ if (empty($relatedProducts)){
128
+ return array();
129
+ }
130
+ $sku = 'sku';
131
+ $collection = Mage::getModel('catalog/product')->getCollection();
132
+
133
+ $filterArray = array();
134
+ foreach($relatedProducts as $mpn => $valueArray){
135
+ array_push($filterArray, array('attribute'=>$sku,'eq'=>$mpn));
136
+ }
137
+ $collection->addFieldToFilter($filterArray);
138
+
139
+ $collection->addAttributeToSelect($sku);
140
+ $collection->addAttributeToSelect('category_ids');
141
+
142
+ $relatedProductsList = array();
143
+ foreach ($collection as $product) {
144
+ $categoryIds = $product->getCategoryIds();
145
+ if(!empty($categoryIds)){
146
+ if (is_array($categoryIds)){
147
+ $catogoriesArray = $categoryIds;
148
+ }
149
+ if (is_string($categoryIds)){
150
+ $catogoriesArray = explode(",",$product->getCategoryIds());
151
+ }
152
+ foreach($catogoriesArray as $categoryId){
153
+ if (!array_key_exists($product->getData($sku), $relatedProducts)){
154
+ continue;
155
+ }
156
+ $relatedProductInfo = $relatedProducts[$product->getData($sku)];
157
+ $relatedProductInfo['mpn'] = $product->getData($sku);
158
+ $relatedProductInfo['url'] = $product->getProductUrl();//preg_replace( '/\/\d+\/$/',"/".$categoryId."/",$product->getProductUrl());;
159
+ if (!array_key_exists($categoryId, $relatedProductsList)){
160
+ $relatedProductsList[$categoryId]= array();
161
+ }
162
+
163
+ array_push($relatedProductsList[$categoryId], $relatedProductInfo);
164
+ }
165
+ }
166
+ else {
167
+ if (!array_key_exists($product->getData($sku), $relatedProducts)){
168
+ continue;
169
+ }
170
+ $relatedProductInfo = $relatedProducts[$product->getData($sku)];
171
+ $relatedProductInfo['mpn'] = $product->getData($sku);
172
+ $relatedProductInfo['url'] = $product->getProductUrl();//preg_replace( '/category\/\d+\/$/','',$product->getProductUrl());;
173
+ if (!array_key_exists('a', $relatedProductsList)){
174
+ $relatedProductsList['a']= array();
175
+ }
176
+
177
+ array_push($relatedProductsList['a'], $relatedProductInfo);
178
+ }
179
+ }
180
+
181
+ return $relatedProductsList;
182
+
183
+ }
184
+
185
+ }
app/code/local/Bintime/Sinchimport/Helper/Image.php CHANGED
@@ -1,73 +1,73 @@
1
- <?php
2
- /**
3
- * Magento
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Open Software License (OSL 3.0)
8
- * that is bundled with this package in the file LICENSE.txt.
9
- * It is also available through the world-wide-web at this URL:
10
- * http://opensource.org/licenses/osl-3.0.php
11
- * If you did not receive a copy of the license and are unable to
12
- * obtain it through the world-wide-web, please send an email
13
- * to license@magentocommerce.com so we can send you a copy immediately.
14
- *
15
- * DISCLAIMER
16
- *
17
- * Do not edit or add to this file if you wish to upgrade Magento to newer
18
- * versions in the future. If you wish to customize Magento for your
19
- * needs please refer to http://www.magentocommerce.com for more information.
20
- *
21
- * @category Mage
22
- * @package Mage_Catalog
23
- * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
- */
26
-
27
- /**
28
- * Catalog image helper
29
- *
30
- * @author Magento Core Team <core@magentocommerce.com>
31
- */
32
- class Bintime_Sinchimport_Helper_Image extends Mage_Catalog_Helper_Image
33
- {
34
-
35
- public function __toString()
36
- {
37
- try {
38
- if( $this->getImageFile() ) {
39
- $this->_getModel()->setBaseFile( $this->getImageFile() );
40
- } else {
41
- $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
42
- }
43
- if (substr($this->_getModel()->getNewFile(),0,4) != 'http') {
44
- if( $this->_getModel()->isCached() ) {
45
- return $this->_getModel()->getUrl();
46
- } else {
47
- if( $this->_scheduleRotate ) {
48
- $this->_getModel()->rotate( $this->getAngle() );
49
- }
50
-
51
- if ($this->_scheduleResize) {
52
- $this->_getModel()->resize();
53
- }
54
-
55
- if( $this->getWatermark() ) {
56
- $this->_getModel()->setWatermark($this->getWatermark());
57
- }
58
-
59
- $url = $this->_getModel()->saveFile()->getUrl();
60
- }
61
- }
62
- else {
63
- return $this->_getModel()->getNewFile();
64
- }
65
- } catch( Exception $e ) {
66
- $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
67
- }
68
- return $url;
69
- }
70
-
71
-
72
-
73
- }
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Mage
22
+ * @package Mage_Catalog
23
+ * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Catalog image helper
29
+ *
30
+ * @author Magento Core Team <core@magentocommerce.com>
31
+ */
32
+ class Bintime_Sinchimport_Helper_Image extends Mage_Catalog_Helper_Image
33
+ {
34
+
35
+ public function __toString()
36
+ {
37
+ try {
38
+ if( $this->getImageFile() ) {
39
+ $this->_getModel()->setBaseFile( $this->getImageFile() );
40
+ } else {
41
+ $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
42
+ }
43
+ if (substr($this->_getModel()->getNewFile(),0,4) != 'http') {
44
+ if( $this->_getModel()->isCached() ) {
45
+ return $this->_getModel()->getUrl();
46
+ } else {
47
+ if( $this->_scheduleRotate ) {
48
+ $this->_getModel()->rotate( $this->getAngle() );
49
+ }
50
+
51
+ if ($this->_scheduleResize) {
52
+ $this->_getModel()->resize();
53
+ }
54
+
55
+ if( $this->getWatermark() ) {
56
+ $this->_getModel()->setWatermark($this->getWatermark());
57
+ }
58
+
59
+ $url = $this->_getModel()->saveFile()->getUrl();
60
+ }
61
+ }
62
+ else {
63
+ return $this->_getModel()->getNewFile();
64
+ }
65
+ } catch( Exception $e ) {
66
+ $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
67
+ }
68
+ return $url;
69
+ }
70
+
71
+
72
+
73
+ }
app/code/local/Bintime/Sinchimport/Model/Api.php CHANGED
@@ -1,85 +1,85 @@
1
- <?php
2
- class Bintime_Sinchimport_Model_Api extends Mage_Api_Model_Resource_Abstract
3
- {
4
- public function test($arg)
5
- {
6
- return "!!!!!!!!!!Hello World! My argument is : ".$arg;
7
- }
8
-
9
- public function run_full_import($arg)
10
- {
11
- $import=Mage::getModel('sinchimport/sinch');
12
-
13
- if($import->is_imort_not_run()){
14
- $dir = dirname(__FILE__);
15
- exec("nohup ".$import->php_run_string.$dir."/../sinch_import_start_ajax.php > /dev/null & echo $!");
16
- sleep(5);
17
- if($import->is_imort_not_run()){
18
- $message_arr = $import->getDataOfLatestImport();
19
- $satus_arr=array('NOT OK', "Last Import finish with ".$message_arr['global_status_import']." at ".$message_arr['finish_import']."; Last status - ".$message_arr['detail_status_import']." ".$message_arr['error_report_message']);
20
- }else{
21
- $satus_arr=array('OK', "Import started");
22
- }
23
-
24
- }else{
25
- $message_arr = $import->getDataOfLatestImport();
26
- $satus_arr=array('NOT OK', "Import already started at ".$message_arr['start_import']." current_status - ".$message_arr['detail_status_import']);
27
- }
28
-
29
- return $satus_arr;
30
- }
31
- public function run_ps_import($arg)
32
- {
33
- $import=Mage::getModel('sinchimport/sinch');
34
-
35
- if($import->is_imort_not_run()){
36
- $dir = dirname(__FILE__);
37
- exec("nohup ".$import->php_run_string.$dir."/../stock_price_sinch_import_start_ajax.php > /dev/null & echo $!");
38
- sleep(5);
39
- if($import->is_imort_not_run()){
40
- $message_arr = $import->getDataOfLatestImport();
41
- $satus_arr=array('NOT OK', "Last ".$message_arr['import_type']." import finish with ".$message_arr['global_status_import']."; Last status '".$message_arr['detail_status_import']."' ".$message_arr['error_report_message']);
42
- }else{
43
- $satus_arr=array('OK', "Price Stock Import started");
44
- }
45
-
46
- }else{
47
- $message_arr = $import->getDataOfLatestImport();
48
- $satus_arr=array('NOT OK', $message_arr['import_type']." import already started at ".$message_arr['start_import']." current_status - '".$message_arr['detail_status_import']."'");
49
- }
50
-
51
- return $satus_arr;
52
- }
53
-
54
- public function get_import_status()
55
- {
56
- $import=Mage::getModel('sinchimport/sinch');
57
- $message_arr = $import->getDataOfLatestImport();
58
- if($import->is_imort_not_run()){
59
- if($message_arr['global_status_import']=='Successful'){
60
- $status=$message_arr['detail_status_import'];
61
- }elseif($message_arr['global_status_import']=='Failed'){
62
- $status=$message_arr['error_report_message'];
63
- }
64
- $satus_arr=array(
65
- 'is_import_running' => 'Not Running',
66
- 'status' => $status,
67
- 'last_finish_date' => $message_arr['finish_import'],
68
- 'count_of_products' => $message_arr['number_of_products'],
69
- 'import_type' => $message_arr['import_type'],
70
- 'message' => "Last ".$message_arr['import_type'].' import finished at '.$message_arr['finish_import'].' with '.$message_arr['global_status_import']."; Last status '".$message_arr['detail_status_import']."' ".$message_arr['error_report_message']
71
-
72
- );
73
- }else{
74
- $satus_arr=array( 'is_import_running' =>'Running',
75
- 'status' => '',
76
- 'last_finish_date' => $message_arr['finish_import'],
77
- 'count_of_products' => $message_arr['number_of_products'],
78
- 'import_type' => $message_arr['import_type'],
79
- 'message' =>$message_arr['import_type']." import already started at ".$message_arr['start_import']." current_status - '".$message_arr['detail_status_import']."'");
80
- }
81
-
82
- return $satus_arr;
83
- }
84
-
85
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Model_Api extends Mage_Api_Model_Resource_Abstract
3
+ {
4
+ public function test($arg)
5
+ {
6
+ return "!!!!!!!!!!Hello World! My argument is : ".$arg;
7
+ }
8
+
9
+ public function run_full_import($arg)
10
+ {
11
+ $import=Mage::getModel('sinchimport/sinch');
12
+
13
+ if($import->is_imort_not_run()){
14
+ $dir = dirname(__FILE__);
15
+ exec("nohup ".$import->php_run_string.$dir."/../sinch_import_start_ajax.php > /dev/null & echo $!");
16
+ sleep(5);
17
+ if($import->is_imort_not_run()){
18
+ $message_arr = $import->getDataOfLatestImport();
19
+ $satus_arr=array('NOT OK', "Last Import finish with ".$message_arr['global_status_import']." at ".$message_arr['finish_import']."; Last status - ".$message_arr['detail_status_import']." ".$message_arr['error_report_message']);
20
+ }else{
21
+ $satus_arr=array('OK', "Import started");
22
+ }
23
+
24
+ }else{
25
+ $message_arr = $import->getDataOfLatestImport();
26
+ $satus_arr=array('NOT OK', "Import already started at ".$message_arr['start_import']." current_status - ".$message_arr['detail_status_import']);
27
+ }
28
+
29
+ return $satus_arr;
30
+ }
31
+ public function run_ps_import($arg)
32
+ {
33
+ $import=Mage::getModel('sinchimport/sinch');
34
+
35
+ if($import->is_imort_not_run()){
36
+ $dir = dirname(__FILE__);
37
+ exec("nohup ".$import->php_run_string.$dir."/../stock_price_sinch_import_start_ajax.php > /dev/null & echo $!");
38
+ sleep(5);
39
+ if($import->is_imort_not_run()){
40
+ $message_arr = $import->getDataOfLatestImport();
41
+ $satus_arr=array('NOT OK', "Last ".$message_arr['import_type']." import finish with ".$message_arr['global_status_import']."; Last status '".$message_arr['detail_status_import']."' ".$message_arr['error_report_message']);
42
+ }else{
43
+ $satus_arr=array('OK', "Price Stock Import started");
44
+ }
45
+
46
+ }else{
47
+ $message_arr = $import->getDataOfLatestImport();
48
+ $satus_arr=array('NOT OK', $message_arr['import_type']." import already started at ".$message_arr['start_import']." current_status - '".$message_arr['detail_status_import']."'");
49
+ }
50
+
51
+ return $satus_arr;
52
+ }
53
+
54
+ public function get_import_status()
55
+ {
56
+ $import=Mage::getModel('sinchimport/sinch');
57
+ $message_arr = $import->getDataOfLatestImport();
58
+ if($import->is_imort_not_run()){
59
+ if($message_arr['global_status_import']=='Successful'){
60
+ $status=$message_arr['detail_status_import'];
61
+ }elseif($message_arr['global_status_import']=='Failed'){
62
+ $status=$message_arr['error_report_message'];
63
+ }
64
+ $satus_arr=array(
65
+ 'is_import_running' => 'Not Running',
66
+ 'status' => $status,
67
+ 'last_finish_date' => $message_arr['finish_import'],
68
+ 'count_of_products' => $message_arr['number_of_products'],
69
+ 'import_type' => $message_arr['import_type'],
70
+ 'message' => "Last ".$message_arr['import_type'].' import finished at '.$message_arr['finish_import'].' with '.$message_arr['global_status_import']."; Last status '".$message_arr['detail_status_import']."' ".$message_arr['error_report_message']
71
+
72
+ );
73
+ }else{
74
+ $satus_arr=array( 'is_import_running' =>'Running',
75
+ 'status' => '',
76
+ 'last_finish_date' => $message_arr['finish_import'],
77
+ 'count_of_products' => $message_arr['number_of_products'],
78
+ 'import_type' => $message_arr['import_type'],
79
+ 'message' =>$message_arr['import_type']." import already started at ".$message_arr['start_import']." current_status - '".$message_arr['detail_status_import']."'");
80
+ }
81
+
82
+ return $satus_arr;
83
+ }
84
+
85
+ }
app/code/local/Bintime/Sinchimport/Model/Backup.php CHANGED
@@ -1,80 +1,80 @@
1
- <?php
2
- ini_set('memory_limit','256M');
3
- $dir = Mage::getBaseDir('code')."/local/Bintime/Sinchimport/Model";//dirname(__FILE__);
4
- require_once ($dir.'/config.php');
5
-
6
- class Bintime_Sinchimport_Model_Backup extends Mage_Core_Model_Abstract {
7
- var $connection, $db;
8
-
9
- function __construct() {
10
- $this->connection=$this->db_connect();
11
- $this->backupProductIds();
12
- $this->backupCategoryIds();
13
- }
14
-
15
- private function backupProductIds() {
16
- $this->db_do("TRUNCATE ".Mage::getSingleton('core/resource')->getTableName('sinch_product_backup'));
17
- $result = $this->db_do("
18
- INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('sinch_product_backup')." (
19
- entity_id,
20
- sku,
21
- store_product_id,
22
- sinch_product_id
23
- )(SELECT
24
- entity_id,
25
- sku,
26
- store_product_id,
27
- sinch_product_id
28
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')."
29
- )
30
- ");
31
- }
32
-
33
- private function backupCategoryIds() {
34
- $this->db_do("TRUNCATE ".Mage::getSingleton('core/resource')->getTableName('sinch_category_backup'));
35
- $result = $this->db_do("
36
- INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('sinch_category_backup')." (
37
- entity_id,
38
- entity_type_id,
39
- attribute_set_id,
40
- parent_id,
41
- store_category_id,
42
- parent_store_category_id
43
- )(SELECT
44
- entity_id,
45
- entity_type_id,
46
- attribute_set_id,
47
- parent_id,
48
- store_category_id,
49
- parent_store_category_id
50
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_category_entity')."
51
- )
52
- ");
53
- }
54
-
55
- ////// Utility functions
56
- private function db_connect() {
57
- $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
58
- $dbConn = mysqli_init();
59
- mysqli_options($dbConn, MYSQLI_OPT_LOCAL_INFILE, true);
60
- if (mysqli_real_connect($dbConn, $dbConf->host, $dbConf->username, $dbConf->password)) {
61
- $this->db = $dbConn;
62
- if(!mysqli_select_db($this->db, $dbConf->dbname)) {
63
- die("Can't select the database: " . mysqli_error($this->db));
64
- }
65
- } else {
66
- die("Could not connect: " . mysqli_error($this->db));
67
- }
68
-
69
- }
70
-
71
- private function db_do($query) {
72
- $result = mysqli_query($this->db, $query) or die("Query failed: " . mysqli_error($this->db));
73
- if (!$result) {
74
- throw new Exception("Invalid query: $sql\n" . mysqli_error($this->db));
75
- } else {
76
- return $result;
77
- }
78
- return $result;
79
- }
80
  }
1
+ <?php
2
+ ini_set('memory_limit','256M');
3
+ $dir = Mage::getBaseDir('code')."/local/Bintime/Sinchimport/Model";//dirname(__FILE__);
4
+ require_once ($dir.'/config.php');
5
+
6
+ class Bintime_Sinchimport_Model_Backup extends Mage_Core_Model_Abstract {
7
+ var $connection, $db;
8
+
9
+ function __construct() {
10
+ $this->connection=$this->db_connect();
11
+ $this->backupProductIds();
12
+ $this->backupCategoryIds();
13
+ }
14
+
15
+ private function backupProductIds() {
16
+ $this->db_do("TRUNCATE ".Mage::getSingleton('core/resource')->getTableName('sinch_product_backup'));
17
+ $result = $this->db_do("
18
+ INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('sinch_product_backup')." (
19
+ entity_id,
20
+ sku,
21
+ store_product_id,
22
+ sinch_product_id
23
+ )(SELECT
24
+ entity_id,
25
+ sku,
26
+ store_product_id,
27
+ sinch_product_id
28
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')."
29
+ )
30
+ ");
31
+ }
32
+
33
+ private function backupCategoryIds() {
34
+ $this->db_do("TRUNCATE ".Mage::getSingleton('core/resource')->getTableName('sinch_category_backup'));
35
+ $result = $this->db_do("
36
+ INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('sinch_category_backup')." (
37
+ entity_id,
38
+ entity_type_id,
39
+ attribute_set_id,
40
+ parent_id,
41
+ store_category_id,
42
+ parent_store_category_id
43
+ )(SELECT
44
+ entity_id,
45
+ entity_type_id,
46
+ attribute_set_id,
47
+ parent_id,
48
+ store_category_id,
49
+ parent_store_category_id
50
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_category_entity')."
51
+ )
52
+ ");
53
+ }
54
+
55
+ ////// Utility functions
56
+ private function db_connect() {
57
+ $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
58
+ $dbConn = mysqli_init();
59
+ mysqli_options($dbConn, MYSQLI_OPT_LOCAL_INFILE, true);
60
+ if (mysqli_real_connect($dbConn, $dbConf->host, $dbConf->username, $dbConf->password)) {
61
+ $this->db = $dbConn;
62
+ if(!mysqli_select_db($this->db, $dbConf->dbname)) {
63
+ die("Can't select the database: " . mysqli_error($this->db));
64
+ }
65
+ } else {
66
+ die("Could not connect: " . mysqli_error($this->db));
67
+ }
68
+
69
+ }
70
+
71
+ private function db_do($query) {
72
+ $result = mysqli_query($this->db, $query) or die("Query failed: " . mysqli_error($this->db));
73
+ if (!$result) {
74
+ throw new Exception("Invalid query: $sql\n" . mysqli_error($this->db));
75
+ } else {
76
+ return $result;
77
+ }
78
+ return $result;
79
+ }
80
  }
app/code/local/Bintime/Sinchimport/Model/Category.php CHANGED
@@ -1,19 +1,19 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Model_Category extends Mage_Catalog_Model_Category {
4
-
5
- public function getImageUrl()
6
- {
7
- $url = false;
8
- if ($image = $this->getImage()) {
9
- if (substr($image,0,4) != 'http'){
10
- $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
11
- }else{
12
- $url = $image;
13
- }
14
- }
15
- return $url;
16
- }
17
-
18
-
19
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Model_Category extends Mage_Catalog_Model_Category {
4
+
5
+ public function getImageUrl()
6
+ {
7
+ $url = false;
8
+ if ($image = $this->getImage()) {
9
+ if (substr($image,0,4) != 'http'){
10
+ $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
11
+ }else{
12
+ $url = $image;
13
+ }
14
+ }
15
+ return $url;
16
+ }
17
+
18
+
19
+ }
app/code/local/Bintime/Sinchimport/Model/Image.php CHANGED
@@ -1,118 +1,118 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Model_Image extends Mage_Catalog_Model_Product_Image {
4
-
5
- public function setBaseFile($file)
6
- {
7
- $this->_isBaseFilePlaceholder = false;
8
- if (substr($file,0,4) != 'http') {
9
- if (($file) && (0 !== strpos($file, '/', 0))) {
10
- $file = '/' . $file;
11
- }
12
- $baseDir = Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath();
13
- if ('/no_selection' == $file) {
14
- $file = null;
15
- }
16
- if ($file) {
17
- if ((!file_exists($baseDir . $file)) || !$this->_checkMemory($baseDir . $file)) {
18
- $file = null;
19
- }
20
- }
21
- if (!$file) {
22
- // check if placeholder defined in config
23
- $isConfigPlaceholder = Mage::getStoreConfig("catalog/placeholder/{$this->getDestinationSubdir()}_placeholder");
24
- $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
25
- if ($isConfigPlaceholder && file_exists($baseDir . $configPlaceholder)) {
26
- $file = $configPlaceholder;
27
- }
28
- else {
29
- // replace file with skin or default skin placeholder
30
- $skinBaseDir = Mage::getDesign()->getSkinBaseDir();
31
- $skinPlaceholder = "/images/catalog/product/placeholder/{$this->getDestinationSubdir()}.jpg";
32
- $file = $skinPlaceholder;
33
- if (file_exists($skinBaseDir . $file)) {
34
- $baseDir = $skinBaseDir;
35
- }
36
- else {
37
- $baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default'));
38
- if (!file_exists($baseDir . $file)) {
39
- $baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default', '_package' => 'base'));
40
- }
41
- }
42
- }
43
- $this->_isBaseFilePlaceholder = true;
44
- }
45
-
46
- $baseFile = $baseDir . $file;
47
- }
48
- else {
49
- $baseFile =$file;
50
- }
51
- if ((!$file) AND (!file_exists($baseFile)) AND substr($baseFile,0,4) != 'http') {
52
- throw new Exception(Mage::helper('catalog')->__('Image file not found'));
53
- }
54
-
55
- $this->_baseFile = $baseFile;
56
- if (substr($baseFile,0,4) != 'http') {
57
- // build new filename (most important params)
58
- $path = array(
59
- Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
60
- 'cache',
61
- Mage::app()->getStore()->getId(),
62
- $path[] = $this->getDestinationSubdir()
63
- );
64
- if((!empty($this->_width)) || (!empty($this->_height)))
65
- $path[] = "{$this->_width}x{$this->_height}";
66
-
67
- // add misk params as a hash
68
- $miscParams = array(
69
- ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
70
- ($this->_keepFrame ? '' : 'no') . 'frame',
71
- ($this->_keepTransparency ? '' : 'no') . 'transparency',
72
- ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
73
- $this->_rgbToString($this->_backgroundColor),
74
- 'angle' . $this->_angle,
75
- 'quality' . $this->_quality
76
- );
77
-
78
- // if has watermark add watermark params to hash
79
- if ($this->getWatermarkFile()) {
80
- $miscParams[] = $this->getWatermarkFile();
81
- $miscParams[] = $this->getWatermarkImageOpacity();
82
- $miscParams[] = $this->getWatermarkPosition();
83
- $miscParams[] = $this->getWatermarkWidth();
84
- $miscParams[] = $this->getWatermarkHeigth();
85
- }
86
-
87
- $path[] = md5(implode('_', $miscParams));
88
- }
89
- else {
90
- $path[] = $file;
91
- }
92
-
93
-
94
- // append prepared filename
95
- if (substr($file,0,4) != 'http') {
96
- $this->_newFile = implode('/', $path) . $file; // the $file contains heading slash
97
- }
98
- else {
99
- $this->_newFile = $file;
100
- }
101
- return $this;
102
- }
103
-
104
- protected function _rgbToString($rgbArray)
105
- {
106
- $result = array();
107
- foreach ($rgbArray as $value) {
108
- if (null === $value) {
109
- $result[] = 'null';
110
- }
111
- else {
112
- $result[] = sprintf('%02s', dechex($value));
113
- }
114
- }
115
- return implode($result);
116
- }
117
-
118
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Model_Image extends Mage_Catalog_Model_Product_Image {
4
+
5
+ public function setBaseFile($file)
6
+ {
7
+ $this->_isBaseFilePlaceholder = false;
8
+ if (substr($file,0,4) != 'http') {
9
+ if (($file) && (0 !== strpos($file, '/', 0))) {
10
+ $file = '/' . $file;
11
+ }
12
+ $baseDir = Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath();
13
+ if ('/no_selection' == $file) {
14
+ $file = null;
15
+ }
16
+ if ($file) {
17
+ if ((!file_exists($baseDir . $file)) || !$this->_checkMemory($baseDir . $file)) {
18
+ $file = null;
19
+ }
20
+ }
21
+ if (!$file) {
22
+ // check if placeholder defined in config
23
+ $isConfigPlaceholder = Mage::getStoreConfig("catalog/placeholder/{$this->getDestinationSubdir()}_placeholder");
24
+ $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
25
+ if ($isConfigPlaceholder && file_exists($baseDir . $configPlaceholder)) {
26
+ $file = $configPlaceholder;
27
+ }
28
+ else {
29
+ // replace file with skin or default skin placeholder
30
+ $skinBaseDir = Mage::getDesign()->getSkinBaseDir();
31
+ $skinPlaceholder = "/images/catalog/product/placeholder/{$this->getDestinationSubdir()}.jpg";
32
+ $file = $skinPlaceholder;
33
+ if (file_exists($skinBaseDir . $file)) {
34
+ $baseDir = $skinBaseDir;
35
+ }
36
+ else {
37
+ $baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default'));
38
+ if (!file_exists($baseDir . $file)) {
39
+ $baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default', '_package' => 'base'));
40
+ }
41
+ }
42
+ }
43
+ $this->_isBaseFilePlaceholder = true;
44
+ }
45
+
46
+ $baseFile = $baseDir . $file;
47
+ }
48
+ else {
49
+ $baseFile =$file;
50
+ }
51
+ if ((!$file) AND (!file_exists($baseFile)) AND substr($baseFile,0,4) != 'http') {
52
+ throw new Exception(Mage::helper('catalog')->__('Image file not found'));
53
+ }
54
+
55
+ $this->_baseFile = $baseFile;
56
+ if (substr($baseFile,0,4) != 'http') {
57
+ // build new filename (most important params)
58
+ $path = array(
59
+ Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
60
+ 'cache',
61
+ Mage::app()->getStore()->getId(),
62
+ $path[] = $this->getDestinationSubdir()
63
+ );
64
+ if((!empty($this->_width)) || (!empty($this->_height)))
65
+ $path[] = "{$this->_width}x{$this->_height}";
66
+
67
+ // add misk params as a hash
68
+ $miscParams = array(
69
+ ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
70
+ ($this->_keepFrame ? '' : 'no') . 'frame',
71
+ ($this->_keepTransparency ? '' : 'no') . 'transparency',
72
+ ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
73
+ $this->_rgbToString($this->_backgroundColor),
74
+ 'angle' . $this->_angle,
75
+ 'quality' . $this->_quality
76
+ );
77
+
78
+ // if has watermark add watermark params to hash
79
+ if ($this->getWatermarkFile()) {
80
+ $miscParams[] = $this->getWatermarkFile();
81
+ $miscParams[] = $this->getWatermarkImageOpacity();
82
+ $miscParams[] = $this->getWatermarkPosition();
83
+ $miscParams[] = $this->getWatermarkWidth();
84
+ $miscParams[] = $this->getWatermarkHeigth();
85
+ }
86
+
87
+ $path[] = md5(implode('_', $miscParams));
88
+ }
89
+ else {
90
+ $path[] = $file;
91
+ }
92
+
93
+
94
+ // append prepared filename
95
+ if (substr($file,0,4) != 'http') {
96
+ $this->_newFile = implode('/', $path) . $file; // the $file contains heading slash
97
+ }
98
+ else {
99
+ $this->_newFile = $file;
100
+ }
101
+ return $this;
102
+ }
103
+
104
+ protected function _rgbToString($rgbArray)
105
+ {
106
+ $result = array();
107
+ foreach ($rgbArray as $value) {
108
+ if (null === $value) {
109
+ $result[] = 'null';
110
+ }
111
+ else {
112
+ $result[] = sprintf('%02s', dechex($value));
113
+ }
114
+ }
115
+ return implode($result);
116
+ }
117
+
118
+ }
app/code/local/Bintime/Sinchimport/Model/Layer.php CHANGED
@@ -1,49 +1,49 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Model_Layer extends Mage_Catalog_Model_Layer
4
- {
5
- /**
6
- * Возвращает фичи, по которым следует строить навигацию для данной категории.
7
- *
8
- * @return mixed
9
- */
10
- public function getFilterableFeatures()
11
- {
12
- Varien_Profiler::start(__METHOD__);
13
-
14
- $category = Mage::registry('current_category');
15
- if( empty($category) ) { $category = Mage::getModel('catalog/category')->load( Mage::app()->getStore()->getRootCategoryId() ); }
16
- $categoryId = $category->getEntityId();
17
- $resource = Mage::getSingleton('core/resource');
18
- $tCategor = $resource->getTableName('stINch_categories');
19
- $tCatFeature = $resource->getTableName('stINch_categories_features');
20
- $tRestrictedVal = $resource->getTableName('stINch_restricted_values');
21
- $tCategMapp = $resource->getTableName('stINch_categories_mapping');
22
-
23
- $select = new Varien_Db_Select(Mage::getSingleton('core/resource')->getConnection('core_read'));
24
- $select->from(array('cf' => $tCatFeature))
25
- ->joinInner(
26
- array('rv' => $tRestrictedVal),
27
- 'cf.category_feature_id = rv.category_feature_id'
28
- )
29
- ->joinInner(
30
- array('cm' => $tCategMapp),
31
- 'cf.store_category_id = cm.store_category_id'
32
- )
33
- ->where('cm.shop_entity_id = '.$categoryId)
34
- ->group('cf.feature_name')
35
- ->order('cf.display_order_number', 'asc')
36
- ->order('cf.feature_name', 'asc')
37
- ->order('rv.display_order_number', 'asc');
38
- ;
39
- $select->columns('cf.feature_name AS name');
40
- $select->columns('cf.category_feature_id as feature_id');
41
- $select->columns('GROUP_CONCAT(`rv`.`text` SEPARATOR "\n") as restricted_values');
42
- $result = $select->query();
43
- // echo $select->__toString();
44
- //exit;
45
- Varien_Profiler::stop(__METHOD__);
46
- return $result;
47
- }
48
-
49
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Model_Layer extends Mage_Catalog_Model_Layer
4
+ {
5
+ /**
6
+ * Возвращает фичи, по которым следует строить навигацию для данной категории.
7
+ *
8
+ * @return mixed
9
+ */
10
+ public function getFilterableFeatures()
11
+ {
12
+ Varien_Profiler::start(__METHOD__);
13
+
14
+ $category = Mage::registry('current_category');
15
+ if( empty($category) ) { $category = Mage::getModel('catalog/category')->load( Mage::app()->getStore()->getRootCategoryId() ); }
16
+ $categoryId = $category->getEntityId();
17
+ $resource = Mage::getSingleton('core/resource');
18
+ $tCategor = $resource->getTableName('stINch_categories');
19
+ $tCatFeature = $resource->getTableName('stINch_categories_features');
20
+ $tRestrictedVal = $resource->getTableName('stINch_restricted_values');
21
+ $tCategMapp = $resource->getTableName('stINch_categories_mapping');
22
+
23
+ $select = new Varien_Db_Select(Mage::getSingleton('core/resource')->getConnection('core_read'));
24
+ $select->from(array('cf' => $tCatFeature))
25
+ ->joinInner(
26
+ array('rv' => $tRestrictedVal),
27
+ 'cf.category_feature_id = rv.category_feature_id'
28
+ )
29
+ ->joinInner(
30
+ array('cm' => $tCategMapp),
31
+ 'cf.store_category_id = cm.store_category_id'
32
+ )
33
+ ->where('cm.shop_entity_id = '.$categoryId)
34
+ ->group('cf.feature_name')
35
+ ->order('cf.display_order_number', 'asc')
36
+ ->order('cf.feature_name', 'asc')
37
+ ->order('rv.display_order_number', 'asc');
38
+ ;
39
+ $select->columns('cf.feature_name AS name');
40
+ $select->columns('cf.category_feature_id as feature_id');
41
+ $select->columns('GROUP_CONCAT(`rv`.`text` SEPARATOR "\n") as restricted_values');
42
+ $result = $select->query();
43
+ // echo $select->__toString();
44
+ //exit;
45
+ Varien_Profiler::stop(__METHOD__);
46
+ return $result;
47
+ }
48
+
49
+ }
app/code/local/Bintime/Sinchimport/Model/Layer/Filter/Feature.php CHANGED
@@ -1,248 +1,248 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Model_Layer_Filter_Feature extends Mage_Catalog_Model_Layer_Filter_Abstract
4
- {
5
- protected $_resource;
6
-
7
- protected $dont_panic = true;
8
-
9
- const LESS = 1;
10
- const GREATER = 2;
11
- /**
12
- * Construct attribute filter
13
- *
14
- */
15
- public function __construct()
16
- {
17
- parent::__construct();
18
- $this->_requestVar = 'ice_feature';
19
- }
20
-
21
- /**
22
- * Задаёт атрибут и строку запроса для текущего фильтра
23
- * @param Фича из Icecat'a
24
- * @return Bintime_Icelayered_Model_Layer_Filter_Feature
25
- */
26
- public function setAttributeModel($attribute)
27
- {
28
- $this->setRequestVar('feature_' . $attribute['category_feature_id']);
29
- $this->setData('attribute_model', $attribute);
30
- return $this;
31
- }
32
-
33
- public function getName()
34
- {
35
- $attribute = $this->getAttributeModel();
36
- return $attribute['name'];
37
- }
38
-
39
- /**
40
- * Retrieve resource instance
41
- *
42
- * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute
43
- */
44
- protected function _getResource()
45
- {
46
-
47
- if (is_null($this->_resource)) {
48
- $this->_resource = Mage::getResourceModel('sinchimport/layer_filter_feature');
49
- }
50
- return $this->_resource;
51
- }
52
-
53
- /**
54
- * Get option text from frontend model by option id
55
- *
56
- * @param int $optionId
57
- * @return unknown
58
- */
59
- protected function _getOptionText($optionId)
60
- {
61
- $feature = $this->getAttributeModel();
62
- return $optionId;
63
- }
64
-
65
- /**
66
- * Apply attribute option filter to product collection
67
- *
68
- * @param Zend_Controller_Request_Abstract $request
69
- * @param Varien_Object $filterBlock
70
- * @return Mage_Catalog_Model_Layer_Filter_Attribute
71
- */
72
- public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
73
- {
74
- $filter = $request->getParam($this->_requestVar);
75
- if (is_array($filter)) {
76
- return $this;
77
- }
78
-
79
- $text = $this->_getOptionText($filter);
80
- if ($filter && $text) {
81
- $this->_getResource()->applyFilterToCollection($this, $filter);
82
- $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
83
- $this->_items = array();
84
- }
85
-
86
- return $this;
87
- }
88
-
89
- /**
90
- * Check whether specified attribute can be used in LN
91
- *
92
- * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
93
- * @return bool
94
- */
95
- protected function _getIsFilterableAttribute($attribute)
96
- {
97
- if ($this->dont_panic) return null;
98
- die(__METHOD__);
99
- return $attribute->getIsFilterable();
100
- }
101
-
102
- /**
103
- * Возвращает массив с информацией по опциям фильтра.
104
- * @return array
105
- */
106
- protected function _getItemsData()
107
- {
108
- Varien_Profiler::start(__METHOD__);
109
-
110
- $feature = $this->getAttributeModel();
111
- $this->_requestVar = 'feature_' . $feature['category_feature_id'];
112
- $limitDirection = isset($feature['limit_direction']) ? $feature['limit_direction'] : 0;
113
-
114
- $data = array();
115
- $options = explode("\n", $feature['restricted_values']);
116
- if (count($options) == 0) {
117
- Varien_Profiler::stop(__METHOD__);
118
- return $data;
119
- }
120
- if (isset($feature['order_val']) && $feature['order_val'] == '2') {
121
- $options = array_reverse($options);
122
- }
123
- if ($limitDirection != self::LESS && $limitDirection != self::GREATER) {
124
-
125
- $optionsCount = $this->_getResource()->getCount($this);
126
- foreach ($options as $option) {
127
- if ($pos = strpos($option, '::')) {
128
- $value = substr($option, 0, $pos);
129
- $presentation_value = substr($option, $pos + 2);
130
- }
131
- else {
132
- $value = $presentation_value = $option;
133
- }
134
- if (isset($optionsCount[$value]) && $optionsCount[$value] > 0) {
135
- $data[] = array(
136
- 'label' => $presentation_value,
137
- 'value' => $value,
138
- 'count' => $optionsCount[$value],
139
- );
140
- }
141
- }
142
- } else {
143
- $oCount = count($options);
144
- $intervals = array();
145
- if ($feature['order_val'] == '2') {
146
- for ($i = 0; $i < $oCount -1; $i++) {
147
- $intervals[$i]['high'] = $options[$i];
148
- $intervals[$i]['low'] = $options[$i +1];
149
- }
150
- } else {
151
- for ($i = 0; $i < $oCount -1; $i++) {
152
- $intervals[$i]['low'] = $options[$i];
153
- $intervals[$i]['high'] = $options[$i +1];
154
- }
155
- }
156
- //FIXME: this is ugly
157
- if ($feature['order_val'] == '2') {
158
- array_push ($intervals, array(
159
- 'high' => $options[$oCount -1],
160
- ));
161
- array_unshift($intervals, array(
162
- 'low' => $options[0],
163
- ));
164
- } else {
165
- array_push ($intervals, array(
166
- 'low' => $options[$oCount -1],
167
- ));
168
- array_unshift($intervals, array(
169
- 'high' => $options[0],
170
- ));
171
- }
172
-
173
- $this->setData('intervals', $intervals);
174
-
175
- $defaultSign = $feature['default_sign'];
176
- for($i = 0; $i < count($intervals); $i++) {
177
- if ($feature['order_val'] == '2') {
178
- $interval = $intervals[$i];
179
- $label = isset($interval['high']) ? $interval['high'] . " $defaultSign" : '>';
180
- if ($label == '>' && isset($intervals[$i + 1])) {
181
- $pad = strlen($intervals[$i + 1]['high'] . $defaultSign) + 2;
182
- $label = str_pad($label, $pad*2, ' ', STR_PAD_LEFT);
183
- $label = str_replace(' ', '&nbsp', $label);
184
- }
185
- $label .= isset($interval['high'], $interval['high']) ? ' - ' : ' ';
186
- $label .= isset($interval['low']) ? $interval['low'] . " $defaultSign" : '>';
187
- $value = isset($interval['low']) ? $interval['low'] : '-';
188
- $value .= ',';
189
- $value .= isset($interval['high']) ? $interval['high'] : '-';
190
- if (isset($interval['high']) AND !isset($interval['low'])) { $value = '-,'.$interval['high'];}
191
- if ($this->_getResource()->getIntervalsCountDescending($this, $interval) > 0){
192
- $data[] = array(
193
- 'label' => $label,
194
- 'value' => $value,
195
- 'count' => $this->_getResource()->getIntervalsCountDescending($this, $interval),
196
- );
197
- }
198
- } else {
199
- $interval = $intervals[$i];
200
- $label = isset($interval['low']) ? $interval['low'] . " $defaultSign" : '<';
201
- if ($label == '<' && isset($intervals[$i + 1])) {
202
- $pad = strlen($intervals[$i + 1]['low'] . $defaultSign) + 2;
203
- $label = str_pad($label, $pad*2, ' ', STR_PAD_LEFT);
204
- $label = str_replace(' ', '&nbsp', $label);
205
- }
206
- $label .= isset($interval['low'], $interval['high']) ? ' - ' : ' ';
207
- $label .= isset($interval['high']) ? $interval['high'] . " $defaultSign" : '<';
208
-
209
- $value = isset($interval['low']) ? $interval['low'] : '-';
210
- $value .= ',';
211
- $value .= isset($interval['high']) ? $interval['high'] : '-';
212
- if ($this->_getResource()->getIntervalsCount($this, $interval) > 0){
213
- $data[] = array(
214
- 'label' => $label,
215
- 'value' => $value,
216
- 'count' => $this->_getResource()->getIntervalsCount($this, $interval),
217
- );
218
- }
219
- }
220
- }
221
- }
222
-
223
- Varien_Profiler::stop(__METHOD__);
224
- return $data;
225
- }
226
-
227
- /*public function getOrderValues($category_feature_id,$categoryId)
228
- {
229
- $select = "
230
- SELECT COUNT(e.entity_id) AS count
231
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." AS e
232
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index')." AS cat_index
233
- ON cat_index.product_id=e.entity_id
234
- AND cat_index.store_id='1'
235
- AND cat_index.visibility IN(2, 4)
236
- AND cat_index.category_id='".$categoryId."'
237
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_index_price')." AS price_index
238
- ON price_index.entity_id = e.entity_id
239
- AND price_index.website_id = '1'
240
- AND price_index.customer_group_id = 0
241
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products_feature_'.$feature['category_feature_id'])." AS idx_".$category_feature_id."
242
- ON idx_".$category_feature_id.".entity_id = e.icecat_product_id;
243
- ";
244
-
245
- return Mage::getSingleton('core/resource')->getConnection('core_read')->fetchCol($select);
246
- }*/
247
-
248
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Model_Layer_Filter_Feature extends Mage_Catalog_Model_Layer_Filter_Abstract
4
+ {
5
+ protected $_resource;
6
+
7
+ protected $dont_panic = true;
8
+
9
+ const LESS = 1;
10
+ const GREATER = 2;
11
+ /**
12
+ * Construct attribute filter
13
+ *
14
+ */
15
+ public function __construct()
16
+ {
17
+ parent::__construct();
18
+ $this->_requestVar = 'ice_feature';
19
+ }
20
+
21
+ /**
22
+ * Задаёт атрибут и строку запроса для текущего фильтра
23
+ * @param Фича из Icecat'a
24
+ * @return Bintime_Icelayered_Model_Layer_Filter_Feature
25
+ */
26
+ public function setAttributeModel($attribute)
27
+ {
28
+ $this->setRequestVar('feature_' . $attribute['category_feature_id']);
29
+ $this->setData('attribute_model', $attribute);
30
+ return $this;
31
+ }
32
+
33
+ public function getName()
34
+ {
35
+ $attribute = $this->getAttributeModel();
36
+ return $attribute['name'];
37
+ }
38
+
39
+ /**
40
+ * Retrieve resource instance
41
+ *
42
+ * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute
43
+ */
44
+ protected function _getResource()
45
+ {
46
+
47
+ if (is_null($this->_resource)) {
48
+ $this->_resource = Mage::getResourceModel('sinchimport/layer_filter_feature');
49
+ }
50
+ return $this->_resource;
51
+ }
52
+
53
+ /**
54
+ * Get option text from frontend model by option id
55
+ *
56
+ * @param int $optionId
57
+ * @return unknown
58
+ */
59
+ protected function _getOptionText($optionId)
60
+ {
61
+ $feature = $this->getAttributeModel();
62
+ return $optionId;
63
+ }
64
+
65
+ /**
66
+ * Apply attribute option filter to product collection
67
+ *
68
+ * @param Zend_Controller_Request_Abstract $request
69
+ * @param Varien_Object $filterBlock
70
+ * @return Mage_Catalog_Model_Layer_Filter_Attribute
71
+ */
72
+ public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
73
+ {
74
+ $filter = $request->getParam($this->_requestVar);
75
+ if (is_array($filter)) {
76
+ return $this;
77
+ }
78
+
79
+ $text = $this->_getOptionText($filter);
80
+ if ($filter && $text) {
81
+ $this->_getResource()->applyFilterToCollection($this, $filter);
82
+ $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
83
+ $this->_items = array();
84
+ }
85
+
86
+ return $this;
87
+ }
88
+
89
+ /**
90
+ * Check whether specified attribute can be used in LN
91
+ *
92
+ * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
93
+ * @return bool
94
+ */
95
+ protected function _getIsFilterableAttribute($attribute)
96
+ {
97
+ if ($this->dont_panic) return null;
98
+ die(__METHOD__);
99
+ return $attribute->getIsFilterable();
100
+ }
101
+
102
+ /**
103
+ * Возвращает массив с информацией по опциям фильтра.
104
+ * @return array
105
+ */
106
+ protected function _getItemsData()
107
+ {
108
+ Varien_Profiler::start(__METHOD__);
109
+
110
+ $feature = $this->getAttributeModel();
111
+ $this->_requestVar = 'feature_' . $feature['category_feature_id'];
112
+ $limitDirection = isset($feature['limit_direction']) ? $feature['limit_direction'] : 0;
113
+
114
+ $data = array();
115
+ $options = explode("\n", $feature['restricted_values']);
116
+ if (count($options) == 0) {
117
+ Varien_Profiler::stop(__METHOD__);
118
+ return $data;
119
+ }
120
+ if (isset($feature['order_val']) && $feature['order_val'] == '2') {
121
+ $options = array_reverse($options);
122
+ }
123
+ if ($limitDirection != self::LESS && $limitDirection != self::GREATER) {
124
+
125
+ $optionsCount = $this->_getResource()->getCount($this);
126
+ foreach ($options as $option) {
127
+ if ($pos = strpos($option, '::')) {
128
+ $value = substr($option, 0, $pos);
129
+ $presentation_value = substr($option, $pos + 2);
130
+ }
131
+ else {
132
+ $value = $presentation_value = $option;
133
+ }
134
+ if (isset($optionsCount[$value]) && $optionsCount[$value] > 0) {
135
+ $data[] = array(
136
+ 'label' => $presentation_value,
137
+ 'value' => $value,
138
+ 'count' => $optionsCount[$value],
139
+ );
140
+ }
141
+ }
142
+ } else {
143
+ $oCount = count($options);
144
+ $intervals = array();
145
+ if ($feature['order_val'] == '2') {
146
+ for ($i = 0; $i < $oCount -1; $i++) {
147
+ $intervals[$i]['high'] = $options[$i];
148
+ $intervals[$i]['low'] = $options[$i +1];
149
+ }
150
+ } else {
151
+ for ($i = 0; $i < $oCount -1; $i++) {
152
+ $intervals[$i]['low'] = $options[$i];
153
+ $intervals[$i]['high'] = $options[$i +1];
154
+ }
155
+ }
156
+ //FIXME: this is ugly
157
+ if ($feature['order_val'] == '2') {
158
+ array_push ($intervals, array(
159
+ 'high' => $options[$oCount -1],
160
+ ));
161
+ array_unshift($intervals, array(
162
+ 'low' => $options[0],
163
+ ));
164
+ } else {
165
+ array_push ($intervals, array(
166
+ 'low' => $options[$oCount -1],
167
+ ));
168
+ array_unshift($intervals, array(
169
+ 'high' => $options[0],
170
+ ));
171
+ }
172
+
173
+ $this->setData('intervals', $intervals);
174
+
175
+ $defaultSign = $feature['default_sign'];
176
+ for($i = 0; $i < count($intervals); $i++) {
177
+ if ($feature['order_val'] == '2') {
178
+ $interval = $intervals[$i];
179
+ $label = isset($interval['high']) ? $interval['high'] . " $defaultSign" : '>';
180
+ if ($label == '>' && isset($intervals[$i + 1])) {
181
+ $pad = strlen($intervals[$i + 1]['high'] . $defaultSign) + 2;
182
+ $label = str_pad($label, $pad*2, ' ', STR_PAD_LEFT);
183
+ $label = str_replace(' ', '&nbsp', $label);
184
+ }
185
+ $label .= isset($interval['high'], $interval['high']) ? ' - ' : ' ';
186
+ $label .= isset($interval['low']) ? $interval['low'] . " $defaultSign" : '>';
187
+ $value = isset($interval['low']) ? $interval['low'] : '-';
188
+ $value .= ',';
189
+ $value .= isset($interval['high']) ? $interval['high'] : '-';
190
+ if (isset($interval['high']) AND !isset($interval['low'])) { $value = '-,'.$interval['high'];}
191
+ if ($this->_getResource()->getIntervalsCountDescending($this, $interval) > 0){
192
+ $data[] = array(
193
+ 'label' => $label,
194
+ 'value' => $value,
195
+ 'count' => $this->_getResource()->getIntervalsCountDescending($this, $interval),
196
+ );
197
+ }
198
+ } else {
199
+ $interval = $intervals[$i];
200
+ $label = isset($interval['low']) ? $interval['low'] . " $defaultSign" : '<';
201
+ if ($label == '<' && isset($intervals[$i + 1])) {
202
+ $pad = strlen($intervals[$i + 1]['low'] . $defaultSign) + 2;
203
+ $label = str_pad($label, $pad*2, ' ', STR_PAD_LEFT);
204
+ $label = str_replace(' ', '&nbsp', $label);
205
+ }
206
+ $label .= isset($interval['low'], $interval['high']) ? ' - ' : ' ';
207
+ $label .= isset($interval['high']) ? $interval['high'] . " $defaultSign" : '<';
208
+
209
+ $value = isset($interval['low']) ? $interval['low'] : '-';
210
+ $value .= ',';
211
+ $value .= isset($interval['high']) ? $interval['high'] : '-';
212
+ if ($this->_getResource()->getIntervalsCount($this, $interval) > 0){
213
+ $data[] = array(
214
+ 'label' => $label,
215
+ 'value' => $value,
216
+ 'count' => $this->_getResource()->getIntervalsCount($this, $interval),
217
+ );
218
+ }
219
+ }
220
+ }
221
+ }
222
+
223
+ Varien_Profiler::stop(__METHOD__);
224
+ return $data;
225
+ }
226
+
227
+ /*public function getOrderValues($category_feature_id,$categoryId)
228
+ {
229
+ $select = "
230
+ SELECT COUNT(e.entity_id) AS count
231
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." AS e
232
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index')." AS cat_index
233
+ ON cat_index.product_id=e.entity_id
234
+ AND cat_index.store_id='1'
235
+ AND cat_index.visibility IN(2, 4)
236
+ AND cat_index.category_id='".$categoryId."'
237
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_index_price')." AS price_index
238
+ ON price_index.entity_id = e.entity_id
239
+ AND price_index.website_id = '1'
240
+ AND price_index.customer_group_id = 0
241
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products_feature_'.$feature['category_feature_id'])." AS idx_".$category_feature_id."
242
+ ON idx_".$category_feature_id.".entity_id = e.icecat_product_id;
243
+ ";
244
+
245
+ return Mage::getSingleton('core/resource')->getConnection('core_read')->fetchCol($select);
246
+ }*/
247
+
248
+ }
app/code/local/Bintime/Sinchimport/Model/Layer/Filter/Price.php CHANGED
@@ -1,192 +1,141 @@
1
- <?php
2
-
3
- /**
4
- * price filter - override price breaks for customer price grouping
5
- *
6
- * @category Mage
7
- * @package Mage_Catalog
8
- * @author Sergey Stepanchuk
9
- */
10
- class Bintime_Sinchimport_Model_Layer_Filter_Price extends Mage_Catalog_Model_Layer_Filter_Price
11
- {
12
- /**
13
- * Get price range for building filter steps
14
- *
15
- * @return int
16
- *//*
17
- public function getPriceRange()
18
- {
19
-
20
- $range = $this->getData('price_range');
21
- if (is_null($range)) {
22
- $maxPrice = $this->getMaxPriceInt();
23
- $index = 2;
24
- if ($maxPrice<1000) {
25
- $index = 1;
26
- }
27
- do {
28
- $range = pow(10, (strlen(floor($maxPrice))-$index));
29
- $items = $this->getRangeItemCounts($range);
30
- $index++;
31
- }
32
- while($range>self::MIN_RANGE_POWER && count($items)<2);
33
-
34
- $this->setData('price_range', $range);
35
- }
36
- return $range;
37
- }
38
-
39
- */
40
- /**
41
- * Apply price range filter to collection
42
- *
43
- * @param Zend_Controller_Request_Abstract $request
44
- * @param $filterBlock
45
- *
46
- * @return Mage_Catalog_Model_Layer_Filter_Price
47
- */
48
- public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
49
- {
50
- /**
51
- * Filter must be string: $index,$range
52
- */
53
- $filter = $request->getParam($this->getRequestVar());
54
- if (!$filter) {
55
- return $this;
56
- }
57
- /* echo "<pre>";
58
- var_dump($filter);
59
- echo "</pre>";
60
- */
61
- if(strstr($filter, ',')){
62
- $filter = explode(',', $filter);
63
- if (count($filter) != 2) {
64
- return $this;
65
- }
66
- list($index, $range) = $filter;
67
- // echo $index.$range."AAAAAAAa";
68
- if ((int)$index && (int)$range) {
69
- $this->setPriceRange((int)$range);
70
-
71
- $this->_applyToCollection($range, $index);
72
- $this->getLayer()->getState()->addFilter(
73
- $this->_createItem($this->_renderItemLabel($range, $index), $filter)
74
- );
75
-
76
- $this->_items = array();
77
- }
78
-
79
- }elseif(strstr($filter, '-')){
80
- $filter = explode('-', $filter);
81
- if (count($filter) != 2) {
82
- return $this;
83
- }
84
-
85
- list($minPrice, $maxPrice) = $filter;
86
- // echo $minPrice."BBBBBB".$maxPrice;
87
-
88
- // if ((int)$minPrice && (int)$maxPrice) {
89
- if(((int)$minPrice || $minPrice==0) && ((int)$maxPrice || $maxPrice=='*')){
90
- // $this->setPriceRange((int)$range);
91
-
92
- $this->_applyToCollectionMinMaxPrice($minPrice, $maxPrice);
93
- $this->getLayer()->getState()->addFilter(
94
- $this->_createItem($this->_renderItemLabelMinMaxPrice($minPrice, $maxPrice), $filter)
95
- );
96
-
97
- $this->_items = array();
98
- }
99
-
100
- }
101
-
102
- return $this;
103
- }
104
- /**
105
- * Prepare text of item label
106
- *
107
- * @param int $fromPrice
108
- * @param int
109
- * @return string
110
- */
111
-
112
- protected function _renderItemLabelMinMaxPrice($fromPrice, $toPrice)
113
- {
114
- $store = Mage::app()->getStore();
115
- $toPriceLabel=$toPrice;
116
- $fromPrice = $store->formatPrice($fromPrice);
117
- $toPrice = $store->formatPrice($toPrice);
118
- $label= Mage::helper('catalog')->__('%s - %s', $fromPrice, $toPrice);
119
- if( $toPriceLabel=='' || $toPriceLabel=='*'){
120
- $label=$fromPrice." + ";
121
- }
122
-
123
- return $label;
124
- }
125
-
126
- /**
127
- * Get data for build price filter items
128
- *
129
- * @return array
130
- */
131
- protected function _getItemsData()
132
- {
133
- $import=Mage::getModel('sinchimport/sinch');
134
- $price_breaks=$import->price_breaks_filter;
135
-
136
- if(strpos($price_breaks, ';') !== false){
137
- $price_ranges = explode(';', $price_breaks);
138
- foreach ($price_ranges as $price_range) {
139
- $price_range_value=trim($price_range);
140
- if($price_range_value == '' || strpos($price_range_value, "-") === false) continue;
141
- $price_range_value = explode('-', $price_range_value);
142
- list($minPrice, $maxPrice) = $price_range_value;
143
- if(is_numeric($minPrice) && (is_numeric($maxPrice) || $maxPrice=='*')){
144
- $count=$this->_getResource()->getCountMinMaxPrice($this, $minPrice, $maxPrice);
145
- $fromPrice = $minPrice;
146
- $toPrice = ($maxPrice == '*') ? '' : ($maxPrice);
147
- if($count){
148
- $data[] = array(
149
- 'label' => $this->_renderItemLabelMinMaxPrice($minPrice, $maxPrice),
150
- 'value' =>$price_range_value,
151
- 'value' =>$minPrice . '-' . $maxPrice,
152
- 'count' => $count,
153
- );
154
- }
155
- }
156
- }
157
- if($data){
158
- return $data;
159
- }
160
-
161
- }
162
-
163
- $range = $this->getPriceRange();
164
- $dbRanges = $this->getRangeItemCounts($range);
165
- $data = array();
166
-
167
- foreach ($dbRanges as $index=>$count) {
168
- $data[] = array(
169
- 'label' => $this->_renderItemLabel($range, $index),
170
- 'value' => $index . ',' . $range,
171
- 'count' => $count,
172
- );
173
- }
174
-
175
- return $data;
176
- }
177
-
178
- /**
179
- * Apply filter value to product collection based on customer price breaks and selected value
180
- *
181
- * @param int $minPrice
182
- * @param int $maxPrice
183
- * @return Mage_Catalog_Model_Layer_Filter_Price
184
- */
185
-
186
- protected function _applyToCollectionMinMaxPrice($minPrice, $maxPrice)
187
- {
188
- $this->_getResource()->applyFilterToCollectionMinMaxPrice($this, $minPrice, $maxPrice);
189
- return $this;
190
- }
191
-
192
- }
1
+ <?php
2
+
3
+ /**
4
+ * price filter - override price breaks for customer price grouping
5
+ *
6
+ * @category Mage
7
+ * @package Mage_Catalog
8
+ * @author Sergey Stepanchuk
9
+ */
10
+ class Bintime_Sinchimport_Model_Layer_Filter_Price extends Mage_Catalog_Model_Layer_Filter_Price
11
+ {
12
+ /**
13
+ * Apply price range filter to collection
14
+ *
15
+ * @param Zend_Controller_Request_Abstract $request
16
+ * @param $filterBlock
17
+ *
18
+ * @return Mage_Catalog_Model_Layer_Filter_Price
19
+ */
20
+ public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
21
+ {
22
+ /**
23
+ * Filter must be string: $index,$range
24
+ */
25
+ $filter = $request->getParam($this->getRequestVar());
26
+ if (!$filter) {
27
+ return $this;
28
+ }
29
+
30
+ if (strstr($filter, ',')) {
31
+ $filter = explode(',', $filter);
32
+ if (count($filter) != 2) {
33
+ return $this;
34
+ }
35
+ list($index, $range) = $filter;
36
+
37
+ if ((int) $index && (int) $range) {
38
+ $this->setPriceRange((int) $range);
39
+
40
+ $this->_applyToCollection($range, $index);
41
+ $this->getLayer()->getState()->addFilter($this->_createItem($this->_renderItemLabel($range, $index), $filter));
42
+
43
+ $this->_items = array();
44
+ }
45
+
46
+ } elseif (strstr($filter, '-')) {
47
+ $filter = explode('-', $filter);
48
+ if (count($filter) != 2) {
49
+ return $this;
50
+ }
51
+
52
+ list($minPrice, $maxPrice) = $filter;
53
+
54
+ if (((int) $minPrice || $minPrice == 0) && ((int) $maxPrice || $maxPrice == '*')) {
55
+ $this->_applyToCollectionMinMaxPrice($minPrice, $maxPrice);
56
+
57
+ $this->getLayer()->getState()->addFilter($this->_createItem($this->_renderItemLabelMinMaxPrice($minPrice, $maxPrice), $filter));
58
+
59
+ $this->_items = array();
60
+ }
61
+
62
+ }
63
+
64
+ return $this;
65
+ }
66
+
67
+ /**
68
+ * Prepare text of item label
69
+ *
70
+ * @param int $fromPrice
71
+ * @param int
72
+ * @return string
73
+ */
74
+ protected function _renderItemLabelMinMaxPrice($fromPrice, $toPrice)
75
+ {
76
+ $store = Mage::app()->getStore();
77
+ $toPriceLabel = $toPrice;
78
+ $fromPrice = $store->formatPrice($fromPrice);
79
+ $toPrice = $store->formatPrice($toPrice);
80
+ $label = Mage::helper('catalog')->__('%s - %s', $fromPrice, $toPrice);
81
+ if ($toPriceLabel == '' || $toPriceLabel == '*') {
82
+ $label = $fromPrice . " + ";
83
+ }
84
+
85
+ return $label;
86
+ }
87
+
88
+ /**
89
+ * Get data for build price filter items
90
+ *
91
+ * @return array
92
+ */
93
+ protected function _getItemsData()
94
+ {
95
+ $import = Mage::getModel('sinchimport/sinch');
96
+ $price_breaks = $import->price_breaks_filter;
97
+
98
+ $data = array();
99
+
100
+ if (strpos($price_breaks, ';') !== false) {
101
+ $price_ranges = explode(';', $price_breaks);
102
+ foreach ($price_ranges as $price_range) {
103
+ $price_range_value = trim($price_range);
104
+ if ($price_range_value == '' || strpos($price_range_value, "-") === false)
105
+ continue;
106
+ $price_range_value = explode('-', $price_range_value);
107
+ list($minPrice, $maxPrice) = $price_range_value;
108
+ if (is_numeric($minPrice) && (is_numeric($maxPrice) || $maxPrice == '*')) {
109
+ $count = $this->_getResource()->getCountMinMaxPrice($this, $minPrice, $maxPrice);
110
+ if ($count) {
111
+ $data[] = array(
112
+ 'label' => $this->_renderItemLabelMinMaxPrice($minPrice, $maxPrice),
113
+ 'value' => implode('-', $price_range_value),
114
+ 'count' => $count
115
+ );
116
+ }
117
+ }
118
+ }
119
+ if ($data) {
120
+ return $data;
121
+ }
122
+
123
+ }
124
+
125
+ return parent::_getItemsData();
126
+ }
127
+
128
+ /**
129
+ * Apply filter value to product collection based on customer price breaks and selected value
130
+ *
131
+ * @param int $minPrice
132
+ * @param int $maxPrice
133
+ * @return Mage_Catalog_Model_Layer_Filter_Price
134
+ */
135
+ protected function _applyToCollectionMinMaxPrice($minPrice, $maxPrice)
136
+ {
137
+ $this->_getResource()->applyFilterToCollectionMinMaxPrice($this, $minPrice, $maxPrice);
138
+ return $this;
139
+ }
140
+
141
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/local/Bintime/Sinchimport/Model/Product.php CHANGED
@@ -1,64 +1,64 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Model_Product extends Mage_Catalog_Model_Product
4
- {
5
- public function getIcecatId()
6
- {
7
-
8
- $resource = $this->getResource();
9
- $connection = $resource->getReadConnection();
10
- $result = $connection->query("
11
- SELECT product_id FROM icecat_products WHERE mpn = '" . $this->getVendorProductId() . "' ");
12
- $id = 0;
13
- if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
14
- $id = $row['product_id'];
15
- }
16
- // echo 'rewrite!';
17
- return $id;
18
- }
19
- public function getMediaGalleryImages()
20
- {
21
- $entity_id=$this->getEntityId();
22
- $sinch=Mage::getModel('sinchimport/sinch');
23
- $sinch->loadGalleryPhotos($this->getEntityId());
24
- $gallery_photos=$sinch->getGalleryPhotos();
25
- if(is_array($gallery_photos)){
26
- $images = new Varien_Data_Collection();
27
- foreach($gallery_photos as $photo){
28
- $image['file']=$photo['thumb'];
29
- $image['url']=$photo['pic'];
30
- $images->addItem(new Varien_Object($image));
31
- }
32
- $this->setData('media_gallery_images', $images);
33
- }else{
34
- if(!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
35
- $images = new Varien_Data_Collection();
36
- foreach ($this->getMediaGallery('images') as $image) {
37
- if ($image['disabled']) {
38
- continue;
39
- }
40
- $image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
41
- $image['id'] = isset($image['value_id']) ? $image['value_id'] : null;
42
- $image['path'] = $this->getMediaConfig()->getMediaPath($image['file']);
43
- $images->addItem(new Varien_Object($image));
44
- }
45
- $this->setData('media_gallery_images', $images);
46
- }
47
-
48
- }
49
-
50
- return $this->getData('media_gallery_images');
51
- }
52
-
53
- function afterCommitCallback(){
54
- parent::afterCommitCallback();
55
- Mage::getSingleton('index/indexer')->processEntityAction(
56
- $this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
57
- );
58
- $entity_id=$this->getEntityId();
59
- $sinch=Mage::getModel('sinchimport/sinch');
60
- $sinch->reloadProductImage($entity_id);
61
- return $this;
62
-
63
- }
64
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Model_Product extends Mage_Catalog_Model_Product
4
+ {
5
+ public function getIcecatId()
6
+ {
7
+
8
+ $resource = $this->getResource();
9
+ $connection = $resource->getReadConnection();
10
+ $result = $connection->query("
11
+ SELECT product_id FROM icecat_products WHERE mpn = '" . $this->getVendorProductId() . "' ");
12
+ $id = 0;
13
+ if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
14
+ $id = $row['product_id'];
15
+ }
16
+ // echo 'rewrite!';
17
+ return $id;
18
+ }
19
+ public function getMediaGalleryImages()
20
+ {
21
+ $entity_id=$this->getEntityId();
22
+ $sinch=Mage::getModel('sinchimport/sinch');
23
+ $sinch->loadGalleryPhotos($this->getEntityId());
24
+ $gallery_photos=$sinch->getGalleryPhotos();
25
+ if(is_array($gallery_photos)){
26
+ $images = new Varien_Data_Collection();
27
+ foreach($gallery_photos as $photo){
28
+ $image['file']=$photo['thumb'];
29
+ $image['url']=$photo['pic'];
30
+ $images->addItem(new Varien_Object($image));
31
+ }
32
+ $this->setData('media_gallery_images', $images);
33
+ }else{
34
+ if(!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
35
+ $images = new Varien_Data_Collection();
36
+ foreach ($this->getMediaGallery('images') as $image) {
37
+ if ($image['disabled']) {
38
+ continue;
39
+ }
40
+ $image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
41
+ $image['id'] = isset($image['value_id']) ? $image['value_id'] : null;
42
+ $image['path'] = $this->getMediaConfig()->getMediaPath($image['file']);
43
+ $images->addItem(new Varien_Object($image));
44
+ }
45
+ $this->setData('media_gallery_images', $images);
46
+ }
47
+
48
+ }
49
+
50
+ return $this->getData('media_gallery_images');
51
+ }
52
+
53
+ function afterCommitCallback(){
54
+ parent::afterCommitCallback();
55
+ Mage::getSingleton('index/indexer')->processEntityAction(
56
+ $this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
57
+ );
58
+ $entity_id=$this->getEntityId();
59
+ $sinch=Mage::getModel('sinchimport/sinch');
60
+ $sinch->reloadProductImage($entity_id);
61
+ return $this;
62
+
63
+ }
64
+ }
app/code/local/Bintime/Sinchimport/Model/Resource/Layer/Filter/Price.php CHANGED
@@ -1,78 +1,78 @@
1
- <?php
2
- /**
3
- * Catalog Layer Price Filter resource model
4
- *
5
- * @category Mage
6
- * @package Mage_Catalog
7
- * @author Sergey Stepanchuk
8
- */
9
- class Bintime_Sinchimport_Model_Resource_Layer_Filter_Price extends Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Price
10
- {
11
- public function applyFilterToCollectionMinMaxPrice($filter, $minPrice, $maxPrice)
12
- {
13
- $collection = $filter->getLayer()->getProductCollection();
14
- $collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId());
15
-
16
- $select = $collection->getSelect();
17
- $response = $this->_dispatchPreparePriceEvent($filter, $select);
18
-
19
- $table = $this->_getIndexTableAlias();
20
- $additional = join('', $response->getAdditionalCalculations());
21
- $rate = $filter->getCurrencyRate();
22
- $priceExpr = new Zend_Db_Expr("(({$table}.min_price {$additional}) * {$rate})");
23
-
24
- $select->where($priceExpr . ' >= ?', $minPrice);
25
- if($maxPrice!='*'){
26
- $select ->where($priceExpr . ' < ?', $maxPrice);
27
- }
28
- return $this;
29
- }
30
-
31
- public function getCountMinMaxPrice($filter, $minPrice, $maxPrice)
32
- {
33
- $select = $this->_getSelect($filter);
34
- $connection = $this->_getReadAdapter();
35
- $response = $this->_dispatchPreparePriceEvent($filter, $select);
36
- $table = $this->_getIndexTableAlias();
37
-
38
- $additional = join('', $response->getAdditionalCalculations());
39
- $rate = $filter->getCurrencyRate();
40
-
41
- /**
42
- * Check and set correct variable values to prevent SQL-injections
43
- */
44
- $rate = floatval($rate);
45
-
46
-
47
-
48
-
49
- $countExpr = new Zend_Db_Expr('COUNT(*)');
50
- $rangeExpr = new Zend_Db_Expr("FLOOR(({$table}.min_price {$additional}) * {$rate}) ");
51
- $select->where($rangeExpr . ' >= ?', $minPrice);
52
- if($maxPrice!='*'){
53
- $select->where($rangeExpr . ' < ?', $maxPrice);
54
- }
55
- $select->columns(array(
56
- 'count' => $countExpr
57
- ));
58
-
59
- return $connection->fetchOne($select);
60
- }
61
-
62
- protected function _getSelect($filter)
63
- {
64
- $collection = $filter->getLayer()->getProductCollection();
65
- $collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId());
66
-
67
- // clone select from collection with filters
68
- $select = clone $collection->getSelect();
69
- // reset columns, order and limitation conditions
70
- $select->reset(Zend_Db_Select::COLUMNS);
71
- $select->reset(Zend_Db_Select::ORDER);
72
- $select->reset(Zend_Db_Select::LIMIT_COUNT);
73
- $select->reset(Zend_Db_Select::LIMIT_OFFSET);
74
-
75
- return $select;
76
- }
77
-
78
- }
1
+ <?php
2
+ /**
3
+ * Catalog Layer Price Filter resource model
4
+ *
5
+ * @category Mage
6
+ * @package Mage_Catalog
7
+ * @author Sergey Stepanchuk
8
+ */
9
+ class Bintime_Sinchimport_Model_Resource_Layer_Filter_Price extends Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Price
10
+ {
11
+ public function applyFilterToCollectionMinMaxPrice($filter, $minPrice, $maxPrice)
12
+ {
13
+ $collection = $filter->getLayer()->getProductCollection();
14
+ $collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId());
15
+
16
+ $select = $collection->getSelect();
17
+ $response = $this->_dispatchPreparePriceEvent($filter, $select);
18
+
19
+ $table = $this->_getIndexTableAlias();
20
+ $additional = join('', $response->getAdditionalCalculations());
21
+ $rate = $filter->getCurrencyRate();
22
+ $priceExpr = new Zend_Db_Expr("(({$table}.min_price {$additional}) * {$rate})");
23
+
24
+ $select->where($priceExpr . ' >= ?', $minPrice);
25
+ if ($maxPrice != '*') {
26
+ $select->where($priceExpr . ' < ?', $maxPrice);
27
+ }
28
+ return $this;
29
+ }
30
+
31
+ public function getCountMinMaxPrice($filter, $minPrice, $maxPrice)
32
+ {
33
+ $select = $this->_getSelect($filter, true);
34
+ $connection = $this->_getReadAdapter();
35
+ $response = $this->_dispatchPreparePriceEvent($filter, $select);
36
+ $table = $this->_getIndexTableAlias();
37
+
38
+ $additional = join('', $response->getAdditionalCalculations());
39
+ $rate = $filter->getCurrencyRate();
40
+
41
+ /**
42
+ * Check and set correct variable values to prevent SQL-injections
43
+ */
44
+ $rate = floatval($rate);
45
+
46
+ $countExpr = new Zend_Db_Expr('COUNT(*)');
47
+ $rangeExpr = new Zend_Db_Expr("FLOOR(({$table}.min_price {$additional}) * {$rate}) ");
48
+ $select->where($rangeExpr . ' >= ?', $minPrice);
49
+ if ($maxPrice != '*') {
50
+ $select->where($rangeExpr . ' < ?', $maxPrice);
51
+ }
52
+ $select->columns(array(
53
+ 'count' => $countExpr
54
+ ));
55
+
56
+ return $connection->fetchOne($select);
57
+ }
58
+
59
+ protected function _getSelect($filter, $isCountMinMax = false)
60
+ {
61
+ if ($isCountMinMax) {
62
+ $collection = $filter->getLayer()->getProductCollection();
63
+ $collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId());
64
+
65
+ // clone select from collection with filters
66
+ $select = clone $collection->getSelect();
67
+ // reset columns, order and limitation conditions
68
+ $select->reset(Zend_Db_Select::COLUMNS);
69
+ $select->reset(Zend_Db_Select::ORDER);
70
+ $select->reset(Zend_Db_Select::LIMIT_COUNT);
71
+ $select->reset(Zend_Db_Select::LIMIT_OFFSET);
72
+
73
+ return $select;
74
+ }
75
+
76
+ return parent::_getSelect($filter);
77
+ }
78
+ }
app/code/local/Bintime/Sinchimport/Model/Resource/Mysql4/Layer/Filter/Feature.php CHANGED
@@ -1,399 +1,372 @@
1
- <?php
2
-
3
- class Bintime_Sinchimport_Model_Resource_Mysql4_Layer_Filter_Feature extends Mage_Core_Model_Mysql4_Abstract
4
- {
5
- protected $resultTable = 'SinchFilterResult';
6
-
7
- protected static $lastResultTable = false;
8
-
9
- protected $filterAplied = false;
10
-
11
- /**
12
- * Initialize connection and define main table name
13
- *
14
- */
15
- protected function _construct()
16
- {
17
- $this->_init('catalog/ice_feature', 'category_feature_id');
18
- }
19
-
20
- protected function _getTableName($type, $id = 0)
21
- {
22
- $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
23
- switch ($type) {
24
- case 'result':
25
- $id = (int)$id;
26
- return $tablePrefix . $this->resultTable . "_$id";
27
- break;
28
-
29
- case 'search':
30
- return $tablePrefix . $this->searchTable;
31
- break;
32
- default:
33
- $resource = Mage::getSingleton('core/resource');
34
- return $resource->getTableName($type);
35
- }
36
-
37
- }
38
-
39
- /**
40
- * Подготавливает фильтр к поиску
41
- *
42
- * @param Bintime_Icelayered_Model_Layer_Filter_Feature $filter
43
- * @param string $value Значение, которму должен соответствовать атрибут
44
- * @return string
45
- */
46
- protected function _prepareSearch($filter, $value = null)
47
- {
48
- Varien_Profiler::start(__METHOD__);
49
- $catId = $filter->getLayer()->getCurrentCategory()->getId();
50
- $connection = $this->_getWriteAdapter();
51
-
52
- $cfid = 0;
53
- if (!is_null($value)) {
54
- $feature = $filter->getAttributeModel();
55
- $cfid = $feature['category_feature_id'];
56
- }
57
- $resultTable = $this->_getTableName('result', $cfid);
58
- // Toàn Handsome đã thêm đoạn code này để kiểm tra bảng dữ liệu đã tồn tại hay chưa
59
- $result = $connection->fetchPairs("SHOW TABLES LIKE '$resultTable'");
60
- if ($result) {
61
- return $resultTable;
62
- }
63
-
64
- //TODO: this table must be temporary
65
- $sql = "
66
- CREATE TABLE IF NOT EXISTS `{$resultTable}`(
67
- `entity_id` int(10) unsigned,
68
- `category_id` int(10) unsigned,
69
- `product_id` int,
70
- `sinch_category_id` int,
71
- `name` varchar(255),
72
- `image` varchar(255),
73
- `supplier_id` int,
74
- `category_feature_id` int,
75
- `feature_id` int,
76
- `feature_name` varchar(255),
77
- `feature_value` text
78
- );
79
- ";
80
- $connection->exec($sql);
81
- $sql = "TRUNCATE TABLE {$resultTable}";
82
- $connection->exec($sql);
83
-
84
- $featuresTable = $this->_getTableName('FilterListOfFeatures');
85
- $sql = "TRUNCATE TABLE `$featuresTable`";
86
- $connection->exec($sql);
87
-
88
- $feature = $filter->getAttributeModel();
89
- if (!isset($feature['limit_direction']) || ($feature['limit_direction'] != 1 && $feature['limit_direction'] != 2)) {
90
- if (!is_null($value)) {
91
- $sql = "INSERT INTO `$featuresTable` (category_feature_id, feature_value) VALUES (?)";
92
- $sql = $connection->quoteInto($sql, array($cfid, $value));
93
- $connection->exec($sql);
94
- }
95
- $params = 'null, null';
96
- } else {
97
- $bounds = explode(',', $value);
98
-
99
- $params = $bounds[0] != '-' ? (int)$bounds[0] : 'null';
100
- $params .= ', ';
101
- $params .= $bounds[1] != '-' ? (int)$bounds[1] : 'null';
102
- }
103
- $tablePrefix = (string)Mage::app()->getConfig()->getTablePrefix();
104
- $result = $connection->raw_query("CALL ".$this->_getTableName('filter_sinch_products_s')."($cfid, $catId,0, $cfid, $params, '$tablePrefix')");
105
- Varien_Profiler::stop(__METHOD__);
106
- return $resultTable;
107
- }
108
-
109
-
110
- /**
111
- * Apply attribute filter to product collection
112
- *
113
- * @param Bintime_Icelayered_Model_Layer_Filter_Feature $filter
114
- * @param string $value
115
- * @return Bintime_Icelayered_Model_Resource_Mysql4_Layer_Filter_Feature
116
- */
117
- public function applyFilterToCollection($filter, $value)
118
- {
119
- Varien_Profiler::start(__METHOD__);
120
- $searchTable = $this->_prepareSearch($filter, $value);
121
- self::$lastResultTable = $searchTable;
122
-
123
- $collection = $filter->getLayer()->getProductCollection();
124
- $feature = $filter->getAttributeModel();
125
- $connection = $this->_getReadAdapter();
126
-
127
- $collection->getSelect()->join(
128
- $searchTable,
129
- "{$searchTable}.entity_id = e.entity_id",
130
- array()
131
- );
132
-
133
- Varien_Profiler::stop(__METHOD__);
134
- return $this;
135
- }
136
-
137
- /**
138
- * Retrieve array with products counts per attribute option
139
- *
140
- * @param Bintime_Icelayered_Model_Layer_Filter_Feature $filter
141
- * @return array
142
- */
143
- public function getCount($filter)
144
- {
145
- Varien_Profiler::start(__METHOD__);
146
-
147
- // clone select from collection with filters
148
- $select = clone $filter->getLayer()->getProductCollection()->getSelect();
149
-
150
- // reset columns, order and limitation conditions
151
- $select->reset(Zend_Db_Select::COLUMNS);
152
- $select->reset(Zend_Db_Select::ORDER);
153
- $select->reset(Zend_Db_Select::LIMIT_COUNT);
154
- $select->reset(Zend_Db_Select::LIMIT_OFFSET);
155
-
156
- $connection = $this->_getReadAdapter();
157
- $feature = $filter->getAttributeModel();
158
- $featureId = $feature['category_feature_id'];
159
-
160
- $select->joinInner(
161
- array('sp' => $this->_getTableName('stINch_products')),
162
- "sp.store_product_id = e.store_product_id",
163
- array()
164
- )->joinInner(
165
- array('spf' => $this->_getTableName('stINch_product_features')),
166
- "spf.sinch_product_id = e.sinch_product_id",
167
- array()
168
- )->joinInner(
169
- array('srv' => $this->_getTableName('stINch_restricted_values')),
170
- "spf.restricted_value_id = srv.restricted_value_id AND srv.category_feature_id = $featureId",
171
- array('value' => 'srv.text', 'count' => 'COUNT(e.entity_id)')
172
- )
173
- ->group("srv.text");
174
-
175
- Varien_Profiler::stop(__METHOD__);
176
- return $connection->fetchPairs($select);
177
- }
178
-
179
- public function getIntervalsCount($filter, $interval)
180
- {
181
- Varien_Profiler::start(__METHOD__);
182
-
183
- // clone select from collection with filters
184
- $select = clone $filter->getLayer()->getProductCollection()->getSelect();
185
-
186
- // reset columns, order and limitation conditions
187
- $select->reset(Zend_Db_Select::COLUMNS);
188
- $select->reset(Zend_Db_Select::ORDER);
189
- $select->reset(Zend_Db_Select::LIMIT_COUNT);
190
- $select->reset(Zend_Db_Select::LIMIT_OFFSET);
191
-
192
- $connection = $this->_getReadAdapter();
193
- $feature = $filter->getAttributeModel();
194
- $select->joinInner(
195
- array('spf' => $this->_getTableName('stINch_product_features')),
196
- "spf.sinch_product_id = e.sinch_product_id",
197
- array()
198
- )->joinLeft(
199
- array('srv' => $this->_getTableName('stINch_restricted_values')),
200
- "srv.restricted_value_id = spf.restricted_value_id",
201
- array('value' => 'srv.text')
202
- )->joinLeft(
203
- array('scf' => $this->_getTableName('stINch_categories_features')),
204
- "scf.category_feature_id = srv.category_feature_id",
205
- array()
206
- )->joinLeft(
207
- array('scm' => $this->_getTableName('stINch_categories_mapping')),
208
- "scm.shop_store_category_id = scf.store_category_id",
209
- array('count' => "COUNT(e.entity_id)")
210
- )
211
- ->where('srv.category_feature_id = ?', $feature['category_feature_id']);
212
-
213
- if (isset($interval['low'], $interval['high'])) {
214
- $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low'])->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
215
- }
216
- else if (isset($interval['low'])) {
217
- $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low']);
218
- }
219
- else if (isset($interval['high'])) {
220
- $select->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
221
- }
222
- $count = $connection->fetchOne($select);
223
- Varien_Profiler::stop(__METHOD__);
224
- return $count;
225
- }
226
-
227
- function getIntervalsCountDescending($filter, $interval)
228
- {
229
- Varien_Profiler::start(__METHOD__);
230
-
231
- // clone select from collection with filters
232
- $select = clone $filter->getLayer()->getProductCollection()->getSelect();
233
-
234
- // reset columns, order and limitation conditions
235
- $select->reset(Zend_Db_Select::COLUMNS);
236
- $select->reset(Zend_Db_Select::ORDER);
237
- $select->reset(Zend_Db_Select::LIMIT_COUNT);
238
- $select->reset(Zend_Db_Select::LIMIT_OFFSET);
239
-
240
- $connection = $this->_getReadAdapter();
241
- $feature = $filter->getAttributeModel();
242
-
243
- $select->joinInner(
244
- array('spf' => $this->_getTableName('stINch_product_features')),
245
- "spf.sinch_product_id = e.sinch_product_id",
246
- array()
247
- )->joinLeft(
248
- array('srv' => $this->_getTableName('stINch_restricted_values')),
249
- "srv.restricted_value_id = spf.restricted_value_id",
250
- array('value' => 'srv.text')
251
- )->joinLeft(
252
- array('scf' => $this->_getTableName('stINch_categories_features')),
253
- "scf.category_feature_id = srv.category_feature_id",
254
- array()
255
- )->joinLeft(
256
- array('scm' => $this->_getTableName('stINch_categories_mapping')),
257
- "scm.shop_store_category_id = scf.store_category_id",
258
- array('count' => "COUNT(e.entity_id)")
259
- )
260
- ->where('srv.category_feature_id = ?', $feature['category_feature_id']);
261
-
262
- if (isset($interval['low'], $interval['high'])) {
263
- $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low'])->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
264
- }
265
- else if (isset($interval['low'])) {
266
- $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low']);
267
- }
268
- else if (isset($interval['high'])) {
269
- $select->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
270
- }
271
- $count = $connection->fetchOne($select);
272
-
273
- Varien_Profiler::stop(__METHOD__);
274
- return $count;
275
- }
276
-
277
- /*
278
- * INDEXES FOLLOW.
279
- */
280
-
281
- /**
282
- * Запускает процедуры индексации.
283
- * Слушает событие Mage::dispatchEvent('iceimport_model_import_after',..)
284
- * @param <type> $observer
285
- */
286
- public function reindex($observer)
287
- {
288
- $this->dropFeatureResultTables();
289
- }
290
-
291
- public function dropFeatureResultTables()
292
- {
293
- $connection = $this->_getWriteAdapter();
294
- $dbName = Mage::getConfig()->getResourceConnectionConfig('core_setup')->dbname;
295
- $filterResultTablePrefix = $this->_getTableName('SinchFilterResult_');
296
- $dropSql = "
297
- SET GROUP_CONCAT_MAX_LEN=10000;
298
- SET @tbls = (SELECT GROUP_CONCAT(TABLE_NAME)
299
- FROM information_schema.TABLES
300
- WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME LIKE '$filterResultTablePrefix%');
301
- SET @delStmt = CONCAT('DROP TABLE ', @tbls);
302
- PREPARE stmt FROM @delStmt;
303
- EXECUTE stmt;
304
- DEALLOCATE PREPARE stmt;
305
- ";
306
- $connection->exec($dropSql);
307
- }
308
-
309
- // Toàn Handsome đã rào đoạn này
310
- /**
311
- public function splitProductsFeature()
312
- {
313
- Mage::log(__METHOD__ . " start at ". date('d-m-Y H:i:s'), null, 'sinchlayered.log');
314
- $featureIds = $this->getProductFeatures4indexig();
315
-
316
- $resource = Mage::getSingleton('core/resource');
317
- //$tProudctsFeature = $resource->getTableName("icecat_products_feature");
318
- $connection = $this->_getWriteAdapter();
319
-
320
- //удаление таблиц с фичами не используемыми больше для навигации.
321
- $tablePattern = $resource->getTableName('stINch_products_feature_');
322
- $query = "SHOW TABLES LIKE '%$tablePattern%'";
323
- $featureTables = $connection->fetchCol($query);
324
- $presentFeatures = array();
325
- foreach($featureTables as $t) {
326
- if (preg_match("#$tablePattern(\d+)#", $t, $matches)) {
327
- $presentFeatures[] = $matches[1];
328
- }
329
- }
330
-
331
- $features2delete = array_diff($presentFeatures, $featureIds);
332
- if (count($features2delete)) {
333
- foreach ($features2delete as & $drop) {
334
- $drop = $tablePattern . $drop;
335
- }
336
- $dropSql = "DROP TABLE " . implode(',', $features2delete);
337
- $connection->exec($dropSql);
338
- }
339
-
340
- //Создание таблиц с фичами используемыми в навигации.
341
- $i = 0; $storeId = Mage::app()->getStore()->getId(); $websiteId = Mage::app()->getStore(true)->getWebsite()->getId();
342
- foreach ($featureIds as $featureId) {
343
- $tFeature = $resource->getTableName("stINch_products_feature_$featureId");
344
- $query = "DROP TABLE IF EXISTS $tFeature";
345
- $connection->exec($query);
346
-
347
- $query = "CREATE TABLE IF NOT EXISTS $tFeature (
348
- `entity_id` int(11) default NULL,
349
- `feature_id` int(11) NOT NULL,
350
- `product_id` int(11) default NULL,
351
- `category_feature_id` int(11) default NULL,
352
- `value` text,
353
- `presentation_value` text,
354
- INDEX (`feature_id`),
355
- KEY `category_feature_id` (`category_feature_id`)
356
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8
357
- ";
358
- $connection->exec($query);
359
-
360
- $query = "TRUNCATE TABLE $tFeature";
361
- $connection->exec($query);
362
-
363
- $query = "
364
- INSERT INTO $tFeature (feature_id, product_id, category_feature_id, value, presentation_value)
365
- SELECT feature_id, product_id, category_feature_id, value, presentation_value FROM $tProudctsFeature
366
- WHERE category_feature_id = $featureId
367
- ";
368
- $query = "
369
- REPLACE INTO $tFeature (entity_id,feature_id, product_id, category_feature_id, value, presentation_value)
370
- SELECT E.entity_id, RV.category_feature_id,E.entity_id, RV.category_feature_id, RV.text , RV.text
371
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." E
372
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index')." PCind
373
- ON (E.entity_id = PCind.product_id AND PCind.store_id='{$storeId}' AND PCind.visibility IN(2,4))
374
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_index_price')." AS price_index
375
- ON price_index.entity_id = E.entity_id AND price_index.website_id = '{$websiteId}' AND price_index.customer_group_id = 0
376
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products')." PR
377
- ON (PR.store_product_id = E.store_product_id)
378
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_product_features')." PF
379
- ON (PR.sinch_product_id = PF.sinch_product_id )
380
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values')." RV
381
- ON (PF.restricted_value_id=RV.restricted_value_id AND RV.category_feature_id= $featureId)
382
- GROUP BY E.entity_id;
383
- ";
384
- $connection->exec($query);
385
- }
386
- Mage::log(__METHOD__ . " end at ". date('d-m-Y H:i:s'), null, 'sinchlayered.log');
387
- }
388
-
389
- private function getProductFeatures4indexig()
390
- {
391
- $connection = $this->_getReadAdapter();
392
-
393
- $tCatFeature = Mage::getSingleton('core/resource')->getTableName('stINch_categories_features');
394
- $query = "SELECT category_feature_id FROM $tCatFeature ";
395
-
396
- return $connection->fetchCol($query);
397
- }
398
- */
399
- }
1
+ <?php
2
+
3
+ class Bintime_Sinchimport_Model_Resource_Mysql4_Layer_Filter_Feature extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ protected $resultTable = 'SinchFilterResult';
6
+
7
+ protected static $lastResultTable = false;
8
+
9
+ protected $filterAplied = false;
10
+
11
+ /**
12
+ * Initialize connection and define main table name
13
+ *
14
+ */
15
+ protected function _construct()
16
+ {
17
+ $this->_init('catalog/ice_feature', 'category_feature_id');
18
+ }
19
+
20
+ protected function _getTableName($type, $id = 0)
21
+ {
22
+ $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
23
+ switch ($type) {
24
+ case 'result':
25
+ $id = (int)$id;
26
+ return $tablePrefix . $this->resultTable . "_$id";
27
+ break;
28
+
29
+ case 'search':
30
+ return $tablePrefix . $this->searchTable;
31
+ break;
32
+ default:
33
+ $resource = Mage::getSingleton('core/resource');
34
+ return $resource->getTableName($type);
35
+ }
36
+
37
+ }
38
+
39
+ /**
40
+ * Подготавливает фильтр к поиску
41
+ *
42
+ * @param Bintime_Icelayered_Model_Layer_Filter_Feature $filter
43
+ * @param string $value Значение, которму должен соответствовать атрибут
44
+ * @return string
45
+ */
46
+ protected function _prepareSearch($filter, $value = null)
47
+ {
48
+ Varien_Profiler::start(__METHOD__);
49
+ $catId = $filter->getLayer()->getCurrentCategory()->getId();
50
+ $connection = $this->_getWriteAdapter();
51
+
52
+ $cfid = 0;
53
+ if (!is_null($value)) {
54
+ $feature = $filter->getAttributeModel();
55
+ $cfid = $feature['category_feature_id'];
56
+ }
57
+ $resultTable = $this->_getTableName('result', $cfid);
58
+ // Toàn Handsome đã thêm đoạn code này để kiểm tra bảng dữ liệu đã tồn tại hay chưa
59
+ $result = $connection->fetchPairs("SHOW TABLES LIKE '$resultTable'");
60
+ if ($result) {
61
+ return $resultTable;
62
+ }
63
+
64
+ //TODO: this table must be temporary
65
+ $sql = "
66
+ CREATE TABLE IF NOT EXISTS `{$resultTable}`(
67
+ `entity_id` int(10) unsigned,
68
+ `category_id` int(10) unsigned,
69
+ `product_id` int,
70
+ `sinch_category_id` int,
71
+ `name` varchar(255),
72
+ `image` varchar(255),
73
+ `supplier_id` int,
74
+ `category_feature_id` int,
75
+ `feature_id` int,
76
+ `feature_name` varchar(255),
77
+ `feature_value` varchar(255)
78
+ );
79
+ ";
80
+ $connection->exec($sql);
81
+
82
+ $addUniqueSql = "ALTER TABLE `{$resultTable}` ADD UNIQUE KEY (`entity_id`, `feature_id`, `feature_value`);";
83
+
84
+ $connection->exec($addUniqueSql);
85
+
86
+ $truncateSql = "TRUNCATE TABLE {$resultTable}";
87
+ $connection->exec($truncateSql);
88
+
89
+ $featuresTable = $this->_getTableName('FilterListOfFeatures');
90
+ $sql = "TRUNCATE TABLE `$featuresTable`";
91
+ $connection->exec($sql);
92
+
93
+ $feature = $filter->getAttributeModel();
94
+ if (!isset($feature['limit_direction']) || ($feature['limit_direction'] != 1 && $feature['limit_direction'] != 2)) {
95
+ if (!is_null($value)) {
96
+ $sql = "INSERT INTO `$featuresTable` (category_feature_id, feature_value) VALUES (?)";
97
+ $sql = $connection->quoteInto($sql, array($cfid, $value));
98
+ $connection->exec($sql);
99
+ }
100
+ $params = 'null, null';
101
+ } else {
102
+ $bounds = explode(',', $value);
103
+
104
+ $params = $bounds[0] != '-' ? (int)$bounds[0] : 'null';
105
+ $params .= ', ';
106
+ $params .= $bounds[1] != '-' ? (int)$bounds[1] : 'null';
107
+ }
108
+ $tablePrefix = (string)Mage::app()->getConfig()->getTablePrefix();
109
+ $result = $connection->raw_query("CALL ".$this->_getTableName('filter_sinch_products_s')."($cfid, $catId,0, $cfid, $params, '$tablePrefix')");
110
+ Varien_Profiler::stop(__METHOD__);
111
+ return $resultTable;
112
+ }
113
+
114
+
115
+ /**
116
+ * Apply attribute filter to product collection
117
+ *
118
+ * @param Bintime_Icelayered_Model_Layer_Filter_Feature $filter
119
+ * @param string $value
120
+ * @return Bintime_Icelayered_Model_Resource_Mysql4_Layer_Filter_Feature
121
+ */
122
+ public function applyFilterToCollection($filter, $value)
123
+ {
124
+ Varien_Profiler::start(__METHOD__);
125
+ $searchTable = $this->_prepareSearch($filter, $value);
126
+ self::$lastResultTable = $searchTable;
127
+
128
+ $collection = $filter->getLayer()->getProductCollection();
129
+ $feature = $filter->getAttributeModel();
130
+ $connection = $this->_getReadAdapter();
131
+
132
+ $collection->getSelect()->join(
133
+ $searchTable,
134
+ "{$searchTable}.entity_id = e.entity_id AND {$searchTable}.feature_value = '$value'",
135
+ array()
136
+ );
137
+
138
+ Varien_Profiler::stop(__METHOD__);
139
+ return $this;
140
+ }
141
+
142
+ /**
143
+ * Retrieve array with products counts per attribute option
144
+ *
145
+ * @param Bintime_Icelayered_Model_Layer_Filter_Feature $filter
146
+ * @return array
147
+ */
148
+ public function getCount($filter)
149
+ {
150
+ Varien_Profiler::start(__METHOD__);
151
+
152
+ // clone select from collection with filters
153
+ $select = clone $filter->getLayer()->getProductCollection()->getSelect();
154
+
155
+ // reset columns, order and limitation conditions
156
+ $select->reset(Zend_Db_Select::COLUMNS);
157
+ $select->reset(Zend_Db_Select::ORDER);
158
+ $select->reset(Zend_Db_Select::LIMIT_COUNT);
159
+ $select->reset(Zend_Db_Select::LIMIT_OFFSET);
160
+
161
+ $connection = $this->_getReadAdapter();
162
+ $feature = $filter->getAttributeModel();
163
+ $featureId = $feature['category_feature_id'];
164
+
165
+ $select->joinInner(
166
+ array('sp' => $this->_getTableName('stINch_products')),
167
+ "sp.store_product_id = e.store_product_id",
168
+ array()
169
+ )->joinInner(
170
+ array('spf' => $this->_getTableName('stINch_product_features')),
171
+ "spf.sinch_product_id = e.sinch_product_id",
172
+ array()
173
+ )->joinInner(
174
+ array('srv' => $this->_getTableName('stINch_restricted_values')),
175
+ "spf.restricted_value_id = srv.restricted_value_id AND srv.category_feature_id = $featureId",
176
+ array('value' => 'srv.text', 'count' => 'COUNT(DISTINCT e.entity_id)')
177
+ )
178
+ ->group("srv.text");
179
+
180
+ Varien_Profiler::stop(__METHOD__);
181
+ return $connection->fetchPairs($select);
182
+ }
183
+
184
+ public function getIntervalsCount($filter, $interval)
185
+ {
186
+ Varien_Profiler::start(__METHOD__);
187
+
188
+ // clone select from collection with filters
189
+ $select = clone $filter->getLayer()->getProductCollection()->getSelect();
190
+
191
+ // reset columns, order and limitation conditions
192
+ $select->reset(Zend_Db_Select::COLUMNS);
193
+ $select->reset(Zend_Db_Select::ORDER);
194
+ $select->reset(Zend_Db_Select::LIMIT_COUNT);
195
+ $select->reset(Zend_Db_Select::LIMIT_OFFSET);
196
+
197
+ $connection = $this->_getReadAdapter();
198
+ $feature = $filter->getAttributeModel();
199
+ $select->joinInner(
200
+ array('spf' => $this->_getTableName('stINch_product_features')),
201
+ "spf.sinch_product_id = e.sinch_product_id",
202
+ array()
203
+ )->joinLeft(
204
+ array('srv' => $this->_getTableName('stINch_restricted_values')),
205
+ "srv.restricted_value_id = spf.restricted_value_id",
206
+ array('value' => 'srv.text')
207
+ )->joinLeft(
208
+ array('scf' => $this->_getTableName('stINch_categories_features')),
209
+ "scf.category_feature_id = srv.category_feature_id",
210
+ array()
211
+ )->joinLeft(
212
+ array('scm' => $this->_getTableName('stINch_categories_mapping')),
213
+ "scm.shop_store_category_id = scf.store_category_id",
214
+ array('count' => "COUNT(DISTINCT e.entity_id)")
215
+ )
216
+ ->where('srv.category_feature_id = ?', $feature['category_feature_id']);
217
+
218
+ if (isset($interval['low'], $interval['high'])) {
219
+ $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low'])->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
220
+ }
221
+ else if (isset($interval['low'])) {
222
+ $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low']);
223
+ }
224
+ else if (isset($interval['high'])) {
225
+ $select->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
226
+ }
227
+ $count = $connection->fetchOne($select);
228
+ Varien_Profiler::stop(__METHOD__);
229
+ return $count;
230
+ }
231
+
232
+ function getIntervalsCountDescending($filter, $interval)
233
+ {
234
+ Varien_Profiler::start(__METHOD__);
235
+
236
+ // clone select from collection with filters
237
+ $select = clone $filter->getLayer()->getProductCollection()->getSelect();
238
+
239
+ // reset columns, order and limitation conditions
240
+ $select->reset(Zend_Db_Select::COLUMNS);
241
+ $select->reset(Zend_Db_Select::ORDER);
242
+ $select->reset(Zend_Db_Select::LIMIT_COUNT);
243
+ $select->reset(Zend_Db_Select::LIMIT_OFFSET);
244
+
245
+ $connection = $this->_getReadAdapter();
246
+ $feature = $filter->getAttributeModel();
247
+
248
+ $select->joinInner(
249
+ array('spf' => $this->_getTableName('stINch_product_features')),
250
+ "spf.sinch_product_id = e.sinch_product_id",
251
+ array()
252
+ )->joinLeft(
253
+ array('srv' => $this->_getTableName('stINch_restricted_values')),
254
+ "srv.restricted_value_id = spf.restricted_value_id",
255
+ array('value' => 'srv.text')
256
+ )->joinLeft(
257
+ array('scf' => $this->_getTableName('stINch_categories_features')),
258
+ "scf.category_feature_id = srv.category_feature_id",
259
+ array()
260
+ )->joinLeft(
261
+ array('scm' => $this->_getTableName('stINch_categories_mapping')),
262
+ "scm.shop_store_category_id = scf.store_category_id",
263
+ array('count' => "COUNT(DISTINCT e.entity_id)")
264
+ )
265
+ ->where('srv.category_feature_id = ?', $feature['category_feature_id']);
266
+
267
+ if (isset($interval['low'], $interval['high'])) {
268
+ $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low'])->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
269
+ }
270
+ else if (isset($interval['low'])) {
271
+ $select->where('CAST(srv.text AS SIGNED) >= ?', $interval['low']);
272
+ }
273
+ else if (isset($interval['high'])) {
274
+ $select->where('CAST(srv.text AS SIGNED) < ?', $interval['high']);
275
+ }
276
+ $count = $connection->fetchOne($select);
277
+
278
+ Varien_Profiler::stop(__METHOD__);
279
+ return $count;
280
+ }
281
+
282
+ // Toàn Handsome đã rào đoạn mã này
283
+ /**
284
+ public function splitProductsFeature()
285
+ {
286
+ Mage::log(__METHOD__ . " start at ". date('d-m-Y H:i:s'), null, 'sinchlayered.log');
287
+ $featureIds = $this->getProductFeatures4indexig();
288
+
289
+ $resource = Mage::getSingleton('core/resource');
290
+ //$tProudctsFeature = $resource->getTableName("icecat_products_feature");
291
+ $connection = $this->_getWriteAdapter();
292
+
293
+ //удаление таблиц с фичами не используемыми больше для навигации.
294
+ $tablePattern = $resource->getTableName('stINch_products_feature_');
295
+ $query = "SHOW TABLES LIKE '%$tablePattern%'";
296
+ $featureTables = $connection->fetchCol($query);
297
+ $presentFeatures = array();
298
+ foreach($featureTables as $t) {
299
+ if (preg_match("#$tablePattern(\d+)#", $t, $matches)) {
300
+ $presentFeatures[] = $matches[1];
301
+ }
302
+ }
303
+
304
+ $features2delete = array_diff($presentFeatures, $featureIds);
305
+ if (count($features2delete)) {
306
+ foreach ($features2delete as & $drop) {
307
+ $drop = $tablePattern . $drop;
308
+ }
309
+ $dropSql = "DROP TABLE " . implode(',', $features2delete);
310
+ $connection->exec($dropSql);
311
+ }
312
+
313
+ //Создание таблиц с фичами используемыми в навигации.
314
+ $i = 0; $storeId = Mage::app()->getStore()->getId(); $websiteId = Mage::app()->getStore(true)->getWebsite()->getId();
315
+ foreach ($featureIds as $featureId) {
316
+ $tFeature = $resource->getTableName("stINch_products_feature_$featureId");
317
+ $query = "DROP TABLE IF EXISTS $tFeature";
318
+ $connection->exec($query);
319
+
320
+ $query = "CREATE TABLE IF NOT EXISTS $tFeature (
321
+ `entity_id` int(11) default NULL,
322
+ `feature_id` int(11) NOT NULL,
323
+ `product_id` int(11) default NULL,
324
+ `category_feature_id` int(11) default NULL,
325
+ `value` text,
326
+ `presentation_value` text,
327
+ INDEX (`feature_id`),
328
+ KEY `category_feature_id` (`category_feature_id`)
329
+ ) ENGINE=MyISAM DEFAULT CHARSET=utf8
330
+ ";
331
+ $connection->exec($query);
332
+
333
+ $query = "TRUNCATE TABLE $tFeature";
334
+ $connection->exec($query);
335
+
336
+ $query = "
337
+ INSERT INTO $tFeature (feature_id, product_id, category_feature_id, value, presentation_value)
338
+ SELECT feature_id, product_id, category_feature_id, value, presentation_value FROM $tProudctsFeature
339
+ WHERE category_feature_id = $featureId
340
+ ";
341
+ $query = "
342
+ REPLACE INTO $tFeature (entity_id,feature_id, product_id, category_feature_id, value, presentation_value)
343
+ SELECT E.entity_id, RV.category_feature_id,E.entity_id, RV.category_feature_id, RV.text , RV.text
344
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." E
345
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index')." PCind
346
+ ON (E.entity_id = PCind.product_id AND PCind.store_id='{$storeId}' AND PCind.visibility IN(2,4))
347
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_index_price')." AS price_index
348
+ ON price_index.entity_id = E.entity_id AND price_index.website_id = '{$websiteId}' AND price_index.customer_group_id = 0
349
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_products')." PR
350
+ ON (PR.store_product_id = E.store_product_id)
351
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_product_features')." PF
352
+ ON (PR.sinch_product_id = PF.sinch_product_id )
353
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values')." RV
354
+ ON (PF.restricted_value_id=RV.restricted_value_id AND RV.category_feature_id= $featureId)
355
+ GROUP BY E.entity_id;
356
+ ";
357
+ $connection->exec($query);
358
+ }
359
+ Mage::log(__METHOD__ . " end at ". date('d-m-Y H:i:s'), null, 'sinchlayered.log');
360
+ }
361
+
362
+ private function getProductFeatures4indexig()
363
+ {
364
+ $connection = $this->_getReadAdapter();
365
+
366
+ $tCatFeature = Mage::getSingleton('core/resource')->getTableName('stINch_categories_features');
367
+ $query = "SELECT category_feature_id FROM $tCatFeature ";
368
+
369
+ return $connection->fetchCol($query);
370
+ }
371
+ */
372
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/local/Bintime/Sinchimport/Model/Resource/Mysql4/Setup.php CHANGED
@@ -1,3 +1,3 @@
1
- <?php
2
- class Bintime_Sinchimport_Model_Resource_Mysql4_Setup extends Mage_Sales_Model_Mysql4_Setup{
3
- }
1
+ <?php
2
+ class Bintime_Sinchimport_Model_Resource_Mysql4_Setup extends Mage_Sales_Model_Mysql4_Setup{
3
+ }
app/code/local/Bintime/Sinchimport/Model/Sinch.php CHANGED
@@ -1,10266 +1,9385 @@
1
- <?php
2
-
3
- ini_set('memory_limit', '256M');
4
- $dir = Mage::getBaseDir('code') . "/local/Bintime/Sinchimport/Model";//dirname(__FILE__);
5
- require_once($dir . '/config.php');
6
-
7
- class Bintime_Sinchimport_Model_Sinch extends Mage_Core_Model_Abstract
8
- {
9
- var
10
- $connection,
11
- $varDir,
12
- $shellDir,
13
- $files,
14
- $attributes,
15
- $db,
16
- $lang_id,
17
- $debug_mode = 1;
18
- public $php_run_string;
19
- public $php_run_strings;
20
- public $price_breaks_filter;
21
- private $productDescriptionList = array();
22
- private $specifications;
23
- private $productDescription;
24
- private $fullProductDescription;
25
- private $lowPicUrl;
26
- private $highPicUrl;
27
- private $errorMessage;
28
- private $galleryPhotos = array(); //depricated
29
- private $productName;
30
- private $relatedProducts = array();
31
- private $errorSystemMessage;
32
- private $sinchProductId;
33
- private $_productEntityTypeId = 0;
34
- private $defaultAttributeSetId = 0;
35
- private $field_terminated_char;
36
- private $import_status_table;
37
- private $import_status_statistic_table;
38
- private $current_import_status_statistic_id;
39
- private $import_log_table;
40
- private $_attributeId;
41
- private $_categoryEntityTypeId;
42
- private $_categoryDefault_attribute_set_id;
43
- private $_root_cat;
44
- private $import_run_type = 'MANUAL';
45
- private $_ignore_category_features = false;
46
- private $_ignore_product_features = false;
47
- private $_ignore_product_related = false;
48
- private $_ignore_product_categories = false;
49
- private $_ignore_product_contracts = false;
50
- private $_ignore_price_rules = false;
51
- private $product_file_format = "NEW";
52
- private $_ignore_restricted_values = false;
53
- private $_categoryMetaTitleAttrId;
54
- private $_categoryMetadescriptionAttrId;
55
- private $_categoryDescriptionAttrId;
56
- private $im_type;
57
-
58
- #################################################################################################
59
-
60
- function __construct()
61
- {
62
-
63
- $this->import_status_table = Mage::getSingleton('core/resource')->getTableName('stINch_import_status');
64
- $this->import_status_statistic_table = Mage::getSingleton('core/resource')->getTableName('stINch_import_status_statistic');
65
- $this->import_log_table = "stINch_import_log";
66
-
67
- $this->php_run_string = PHP_RUN_STRING;
68
- $this->php_run_strings = PHP_RUN_STRINGS;
69
-
70
- $this->price_breaks_filter = PRICE_BREAKS;
71
- /*$this->db_connect();
72
- $res = $this->db_do("select languages_id from languages where code='".LANG_CODE."'");
73
- $row = mysqli_fetch_assoc($res);
74
- $this->lang_id = $row['languages_id'];
75
- */
76
- $this->varDir = TEMPORARY_DIRECTORY_FOR_STORING_FILES;
77
- $this->shellDir = SHELL_DIRECTORY_FOR_INDEXER;
78
- $this->connection = $this->db_connect();
79
- $this->createTemporaryImportDerictory();
80
- $this->_logFile = "Sinch.log";
81
- $this->_LOG("constructor");
82
- $this->files = array(
83
- FILE_CATEGORIES,
84
- FILE_CATEGORY_TYPES,
85
- FILE_CATEGORIES_FEATURES,
86
- FILE_DISTRIBUTORS,
87
- FILE_DISTRIBUTORS_STOCK_AND_PRICES,
88
- FILE_EANCODES,
89
- FILE_MANUFACTURERS,
90
- FILE_PRODUCT_FEATURES,
91
- FILE_PRODUCT_CATEGORIES,
92
- FILE_PRODUCTS,
93
- FILE_RELATED_PRODUCTS,
94
- FILE_RESTRICTED_VALUES,
95
- FILE_STOCK_AND_PRICES,
96
- FILE_PRODUCTS_PICTURES_GALLERY,
97
- FILE_PRICE_RULES,
98
- FILE_PRODUCT_CONTRACTS
99
- );
100
- $this->attributes['manufacturer'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('manufacturer')->getFirstItem()->getId();
101
- $this->attributes['name'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('name')->getFirstItem()->getId();
102
- $this->attributes['is_active'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('is_active')->getFirstItem()->getId();
103
- $this->attributes['include_in_menu'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('include_in_menu')->getFirstItem()->getId();
104
- $this->attributes['url_key'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('url_key')->getFirstItem()->getId();
105
- $this->attributes['display_mode'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('display_mode')->getFirstItem()->getId();
106
- $this->attributes['status'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('status')->getFirstItem()->getId();
107
- $this->attributes['visibility'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('visibility')->getFirstItem()->getId();
108
- $this->attributes['price'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('price')->getFirstItem()->getId();
109
- $this->attributes['cost'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('cost')->getFirstItem()->getId();
110
- $this->attributes['weight'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('weight')->getFirstItem()->getId();
111
- $this->attributes['tax_class_id'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('tax_class_id')->getFirstItem()->getId();
112
-
113
- $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
114
- // if($dataConf['field_terminated_char']){
115
- // $this->field_terminated_char=$dataConf['field_terminated_char'];
116
- // }else{
117
- $this->field_terminated_char = DEFAULT_FILE_TERMINATED_CHAR;
118
- // }
119
- // $attributeOptions = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('manufacturer')->getFirstItem()->getSource()->getAllOptions(false);
120
- // echo "<pre>"; print_r($attributeOptions); echo "</pre>";
121
- }
122
-
123
- #################################################################################################
124
-
125
- private function db_connect()
126
- {
127
- // $connection = Mage::getModel('core/resource')->getConnection('core_write');
128
- $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
129
- $dbConn = mysqli_init();
130
- mysqli_options($dbConn, MYSQLI_OPT_LOCAL_INFILE, true);
131
- if (mysqli_real_connect($dbConn, $dbConf->host, $dbConf->username, $dbConf->password)) {
132
- $this->db = $dbConn;
133
- if (!mysqli_select_db($this->db, $dbConf->dbname)) {
134
- die("Can't select the database: " . mysqli_error($this->db));
135
- }
136
- } else {
137
- die("Could not connect: " . mysqli_error($this->db));
138
- }
139
-
140
- }
141
-
142
- ################################################################################################
143
-
144
- /**
145
- * Create the import directory Hierarchy
146
- * @return false if directory already exists
147
- */
148
- public function createTemporaryImportDerictory()
149
- {
150
- $dirArray = explode('/', $this->varDir);
151
- end($dirArray);
152
- // $this->_LOG('before :'.$this->varDir);
153
- if (prev($dirArray) == 'bintime') {
154
- return false;
155
- }
156
-
157
-
158
- $this->varDir = $this->varDir . 'bintime/sinchimport/';
159
- if (!is_dir($this->varDir)) {
160
- mkdir($this->varDir, 0777, true);
161
- }
162
- // $this->_LOG('after :'.$this->varDir);
163
- }
164
-
165
- ################################################################################################
166
-
167
- function _LOG($log)
168
- {
169
-
170
- if ($log) {
171
- // $q="insert into ".$this->import_log_table." (message_date, message) values(now(), '".$log."')";
172
- // $this->db_do($q);
173
- Mage::log($log, null, $this->_logFile);
174
- // list($usec, $sec) = explode(" ", microtime());
175
- // $time = ((float)$usec + (float)$sec);
176
- /* $time = date("D M j G:i:s T Y");
177
-
178
- if($_SERVER['REMOTE_ADDR']){
179
- $log = "[".getmypid()."] "."[".$_SERVER['REMOTE_ADDR']."] "."[".$time."] ".$log."\n";
180
- error_log($log,3,LOG_FILE);
181
- }else{
182
- $log = "[".getmypid()."] "."[".$time."] ".$log."\n";
183
- error_log($log,3,LOG_FILE . ".cli");
184
- */
185
- }
186
- }
187
-
188
- #################################################################################################
189
-
190
- function cron_start_import()
191
- {
192
- $this->_LOG("Start import from cron");
193
- $start_hr = Mage::getStoreConfig('sinchimport_root/sinch_cron/sinch_cron_time');
194
- $now_hr = date('H');
195
- $this->_LOG("Now $now_hr hr, scheduler time is $start_hr hr");
196
-
197
- if ($start_hr == $now_hr) {
198
- $this->run_sinch_import();
199
- } else {
200
- $this->_LOG(" it's NOT time for SINCH ");
201
- }
202
-
203
- $this->_LOG("Finish import from cron");
204
-
205
- }
206
-
207
- #################################################################################################
208
-
209
- function run_sinch_import()
210
- {
211
-
212
- $this->_categoryMetaTitleAttrId = $this->_getCategoryAttributeId('meta_title');
213
- $this->_categoryMetadescriptionAttrId = $this->_getCategoryAttributeId('meta_description');
214
- $this->_categoryDescriptionAttrId = $this->_getCategoryAttributeId('description');
215
-
216
- $safe_mode_set = ini_get('safe_mode');
217
-
218
- $this->InitImportStatuses('FULL');
219
- if ($safe_mode_set) {
220
- $this->_LOG('safe_mode is enable. import stoped.');
221
- $this->set_import_error_reporting_message('Safe_mode is enabled. Please check the documentation on how to fix this. Import stopped.');
222
- exit;
223
- }
224
- $store_proc = $this->check_store_procedure_exist();
225
-
226
- if (!$store_proc) {
227
- $this->_LOG('store prcedure "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '" is absent in this database. import stoped.');
228
- $this->set_import_error_reporting_message('Stored procedure "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '" is absent in this database. Import stopped.');
229
- exit;
230
- }
231
-
232
- $file_privileg = $this->check_db_privileges();
233
-
234
- if (!$file_privileg) {
235
- $this->_LOG("Loaddata option not set - please check the documentation on how to fix this. You dan't have privileges for LOAD DATA.");
236
- $this->set_import_error_reporting_message("Loaddata option not set - please check the documentation on how to fix this. Import stopped.");
237
- exit;
238
- }
239
- $local_infile = $this->check_local_infile();
240
- if (!$local_infile) {
241
- $this->_LOG("Loaddata option not set - please check the documentation on how to fix this. Add this string to 'set-variable=local-infile=0' in '/etc/my.cnf'");
242
- $this->set_import_error_reporting_message("Loaddata option not set - please check the documentation on how to fix this. Import stopped.");
243
- exit;
244
- }
245
- //STP TEST
246
- //$this->ApplyCustomerGroupPrice();
247
- //echo $children_cat=$this->get_all_children_cat(6);
248
-
249
-
250
- if ($this->is_imort_not_run()) {
251
- try {
252
- //$this->InitImportStatuses();
253
- $q = "SELECT GET_LOCK('sinchimport', 30)";
254
- $quer = $this->db_do($q);
255
- $import = $this;
256
- $import->addImportStatus('Start Import');
257
- echo "Upload Files <br>";
258
- $import->UploadFiles();
259
- $import->addImportStatus('Upload Files');
260
-
261
- echo "Parse Category Types <br>";
262
- $import->ParseCategoryTypes();
263
-
264
- echo "Parse Categories <br>";
265
- $coincidence = $import->ParseCategories();
266
- $import->addImportStatus('Parse Categories');
267
-
268
-
269
- //$import->_cleanCateoryProductFlatTable();
270
- //$import->runIndexer();
271
- //echo("\n\n\n==================RETURN=================\n\n\n");
272
-
273
-
274
- echo "Parse Category Features <br>";
275
- $import->ParseCategoryFeatures();
276
- $import->addImportStatus('Parse Category Features');
277
-
278
-
279
- echo "Parse Distributors <br>";
280
- $import->ParseDistributors();
281
- if ($this->product_file_format == "NEW") {
282
- $this->ParseDistributorsStockAndPrice();
283
- $this->ParseProductContracts();
284
- }
285
- $import->addImportStatus('Parse Distributors');
286
-
287
- echo "Parse EAN Codes <br>";
288
- $import->ParseEANCodes();
289
- $import->addImportStatus('Parse EAN Codes');
290
-
291
-
292
- echo "Parse Manufacturers <br>";
293
- $import->ParseManufacturers();
294
- $import->addImportStatus('Parse Manufacturers');
295
-
296
- echo "Parse Related Products <br>";
297
- $import->ParseRelatedProducts();
298
- $import->addImportStatus('Parse Related Products');
299
-
300
-
301
- echo "Parse Product Features <br>";
302
- $import->ParseProductFeatures();
303
- $import->addImportStatus('Parse Product Features');
304
-
305
- echo "Parse Product Categories <br>";
306
- $import->ParseProductCategories();
307
-
308
- echo "Parse Products <br>";
309
- $import->ParseProducts($coincidence);
310
- $import->addImportStatus('Parse Products');
311
-
312
- //echo("\n\n\n\n ##################################### \n\n\n\n"); return;
313
- //
314
-
315
- echo "Parse Pictures Gallery";
316
- $import->ParseProductsPicturesGallery();
317
- $import->addImportStatus('Parse Pictures Gallery');
318
-
319
-
320
- echo "Parse Restricted Values <br>";
321
- $import->ParseRestrictedValues();
322
- $import->addImportStatus('Parse Restricted Values');
323
-
324
- echo "Parse Stock And Prices <br>";
325
- $import->ParseStockAndPrices();
326
- $import->addImportStatus('Parse Stock And Prices');
327
-
328
- echo "Apply Customer Group Price <br>";
329
- //$import->ParsePriceRules();
330
- //$import->AddPriceRules();
331
- //$import->ApplyCustomerGroupPrice();
332
-
333
- if (file_exists($this->varDir . FILE_PRICE_RULES)) {
334
- $ftpCred = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
335
- Mage::dispatchEvent('sinch_pricerules_import_ftp', array(
336
- 'ftp_host' => $ftpCred["ftp_server"],
337
- 'ftp_username' => $ftpCred["login"],
338
- 'ftp_password' => $ftpCred["password"]
339
- ));
340
- }
341
-
342
-
343
- Mage::log("Finish Sinch import", null, $this->_logFile);
344
- echo "Finish Sinch import<br>";
345
-
346
- Mage::log("Start cleanin Sinch cache<br>", null, $this->_logFile);
347
- echo "Start cleanin Sinch cache<br>";
348
- Mage::app()->getCacheInstance()->cleanType('block_html');
349
- /*
350
- $indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price');
351
- if ($indexProcess) {
352
- $indexProcess->reindexAll();
353
- }
354
- */
355
-
356
- Mage::log("Start indexing Sinch features for filters", null, $this->_logFile);
357
- echo "Start indexing Sinch features for filters<br>";
358
-
359
- $resource = Mage::getResourceModel('sinchimport/layer_filter_feature');
360
- // Toàn Handsome thêm đoạn mã này
361
- $resource->dropFeatureResultTables();
362
-
363
- Mage::log("Finish indexing Sinch features for filters", null, $this->_logFile);
364
- $import->addImportStatus('Generate category filters');
365
- echo "Finish indexing Sinch features for filters<br>";
366
-
367
-
368
- Mage::log("Start indexing data", null, $this->_logFile);
369
- echo "Start indexing data";
370
- $import->_cleanCateoryProductFlatTable();
371
- $import->runIndexer();
372
- Mage::log("Finish indexing data", null, $this->_logFile);
373
- $import->addImportStatus('Indexing data', 1);
374
- echo "Finish indexing data";
375
-
376
- $q = "SELECT RELEASE_LOCK('sinchimport')";
377
- $quer = $this->db_do($q);
378
- } catch (Exception $e) {
379
- $this->set_import_error_reporting_message($e);
380
- }
381
- } else {
382
- Mage::log("Sinchimport already run", null, $this->_logFile);
383
- echo "Sinchimport already run<br>";
384
-
385
- }
386
-
387
- }
388
-
389
- #################################################################################################
390
-
391
- private function _getCategoryAttributeId($attributeCode)
392
- {
393
- return $this->_getAttributeId($attributeCode, 'catalog_category');
394
- }
395
-
396
- #################################################################################################
397
-
398
- private function _getAttributeId($attributeCode, $typeCode)
399
- {
400
- if ($typeCode == 'catalog_product') {
401
- $typeId = $this->_getProductEntityTypeId();
402
- } else {
403
- $typeId = $this->_getEntityTypeId($typeCode);
404
- }
405
- if (!isset($this->_attributeId[$typeCode]) OR !is_array($this->_attributeId[$typeCode])) {
406
- $sql = "
407
- SELECT attribute_id, attribute_code
408
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute') . "
409
- WHERE entity_type_id = '" . $typeId . "'
410
- ";
411
- $result = $this->db_do($sql);
412
- while ($row = mysqli_fetch_assoc($result)) {
413
- $this->_attributeId[$typeCode][$row['attribute_code']] = $row['attribute_id'];
414
- }
415
- }
416
- // echo 'attribute code: '.$attributeCode.','.$typeCode.' => '.$this->_attributeId[$typeCode][$attributeCode].PHP_EOL;
417
- return $this->_attributeId[$typeCode][$attributeCode];
418
- }
419
-
420
- #################################################################################################
421
-
422
- private function _getProductEntityTypeId()
423
- {
424
- if (!$this->_productEntityTypeId) {
425
- $this->_productEntityTypeId = $this->_getEntityTypeId('catalog_product');
426
- }
427
- return $this->_productEntityTypeId;
428
- }
429
-
430
- #################################################################################################
431
-
432
- private function _getEntityTypeId($code)
433
- {
434
- $sql = "
435
- SELECT entity_type_id
436
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_entity_type') . "
437
- WHERE entity_type_code = '" . $code . "'
438
- LIMIT 1
439
- ";
440
- $result = $this->db_do($sql);
441
- if ($row = mysqli_fetch_assoc($result)) {
442
- return $row['entity_type_id'];
443
- }
444
- return false;
445
- }
446
-
447
- #################################################################################################
448
-
449
- private function db_do($query)
450
- {
451
- if ($this->debug_mode) {
452
- Mage::log("Query: " . $query, null, $this->_logFile);
453
- }
454
- $result = mysqli_query($this->db, $query) or die("Query failed: " . mysqli_error($this->db));
455
- if (!$result) {
456
- throw new Exception("Invalid query: $sql\n" . mysqli_error($this->db));
457
- } else {
458
- return $result;
459
- }
460
- return $result;
461
- }
462
-
463
- #################################################################################################
464
-
465
- function InitImportStatuses($type)
466
- {
467
- $this->db_do("DROP TABLE IF EXISTS " . $this->import_status_table);
468
- $this->db_do("CREATE TABLE " . $this->import_status_table . "(
469
- id int(11) NOT NULL auto_increment PRIMARY KEY,
470
- message varchar(50),
471
- finished int(1) default 0
472
- )"
473
- );
474
- $this->db_do("INSERT INTO " . $this->import_status_statistic_table . " (
475
- start_import,
476
- finish_import,
477
- import_type,
478
- global_status_import,
479
- import_run_type,
480
- error_report_message)
481
- VALUES(
482
- now(),
483
- NULL,
484
- '$type',
485
- 'Run',
486
- '" . $this->import_run_type . "',
487
- ''
488
- )
489
- ");
490
- $q = "SELECT MAX(id) AS id FROM " . $this->import_status_statistic_table;
491
-
492
- $quer = $this->db_do($q);
493
- $row = mysqli_fetch_array($quer);
494
- $this->current_import_status_statistic_id = $row['id'];
495
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
496
- SET global_status_import='Failed'
497
- WHERE global_status_import='Run' AND id!=" . $this->current_import_status_statistic_id);
498
-
499
- }
500
- #################################################################################################
501
-
502
-
503
- ################################################################################################################################################################
504
-
505
- function set_import_error_reporting_message($message)
506
- {
507
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
508
- SET error_report_message='" . mysqli_real_escape_string($this->db, $message) . "'
509
- WHERE id=" . $this->current_import_status_statistic_id);
510
- } // function ParseCategories()
511
- ################################################################################################################################################################
512
-
513
-
514
- ################################################################################################################################################################
515
-
516
- function check_store_procedure_exist()
517
- {
518
- $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
519
- $q = 'SHOW PROCEDURE STATUS LIKE "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '"';
520
- $quer = $this->db_do($q);
521
- $result = false;
522
- While ($row = mysqli_fetch_array($quer)) {
523
- if (($row['Name'] == Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s')) && ($row['Db'] == $dbConf->dbname)) {
524
- $result = true;
525
- }
526
- }
527
- return $result;
528
- } // private function loadCategoriesTemp()
529
- ################################################################################################################################################################
530
-
531
-
532
- ################################################################################################################################################################
533
-
534
- function check_db_privileges()
535
- {
536
- $q = 'SHOW PRIVILEGES';
537
- $quer = $this->db_do($q);
538
- while ($row = mysqli_fetch_array($quer)) {
539
- if ($row['Privilege'] == 'File' && $row['Context'] == 'File access on server') {
540
- return true;
541
- }
542
- }
543
- return false;
544
- }
545
- ################################################################################################################################################################
546
-
547
-
548
- ################################################################################################################################################################
549
-
550
- function check_local_infile()
551
- {
552
- $q = 'SHOW VARIABLES LIKE "local_infile"';
553
- $quer = $this->db_do($q);
554
- $row = mysqli_fetch_array($quer);
555
- if ($row['Variable_name'] == 'local_infile' && $row['Value'] == "ON") {
556
- return true;
557
- } else {
558
- return false;
559
- }
560
- } // private function addCategoryDataMultistoreMerge(...)
561
- ################################################################################################################################################################
562
-
563
-
564
- ################################################################################################################################################################
565
-
566
- function is_imort_not_run()
567
- {
568
- $q = "SELECT IS_FREE_LOCK('sinchimport') as getlock";
569
- $quer = $this->db_do($q);
570
- $row = mysqli_fetch_array($quer);
571
- return $row['getlock'];
572
- } // private function deleteOldSinchCategoriesFromShopMerge()
573
- ################################################################################################################################################################
574
-
575
-
576
- ################################################################################################################################################################
577
-
578
- function addImportStatus($message, $finished = 0)
579
- {
580
- $q = "INSERT INTO " . $this->import_status_table . "
581
- (message, finished)
582
- VALUES('" . $message . "', $finished)";
583
- $this->db_do($q);
584
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
585
- SET detail_status_import='" . $message . "'
586
- WHERE id=" . $this->current_import_status_statistic_id);
587
- if ($finished == 1) {
588
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
589
- SET
590
- global_status_import='Successful',
591
- finish_import=now()
592
- WHERE
593
- error_report_message='' and
594
- id=" . $this->current_import_status_statistic_id);
595
- }
596
- } // public function mapSinchCategoriesMultistoreMerge($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type)
597
- ################################################################################################################################################################
598
-
599
-
600
- ################################################################################################################################################################
601
-
602
- function UploadFiles()
603
- {
604
- $this->_LOG("Start upload files");
605
- $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
606
- $login = $dataConf['login'];
607
- $passw = $dataConf['password'];
608
- $server = $dataConf['ftp_server'];
609
-
610
- //return;//stepan tes//stepan tes//stepan testtt
611
- if (!$login || !$passw) {
612
- $this->_LOG('ftp login or password dosent defined');
613
- $this->set_import_error_reporting_message('FTP login or password has not been defined. Import stopped.');
614
- exit;
615
-
616
- }
617
- $file_url_and_dir = $this->repl_ph(FILE_URL_AND_DIR, array(
618
- 'server' => $server,
619
- 'login' => $login,
620
- 'password' => $passw
621
- )
622
- );
623
- foreach ($this->files as $file) {
624
- $this->_LOG("Copy " . $file_url_and_dir . $file . " to " . $this->varDir . $file);
625
- if (strstr($file_url_and_dir, 'ftp://')) {
626
- preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $file_url_and_dir, $match);
627
- //var_dump($match);
628
- if ($conn = ftp_connect($match[3])) {
629
- if (!ftp_login($conn, $login, $passw)) {
630
- $this->set_import_error_reporting_message('Incorrect username or password for the Stock In The Channel server. Import stopped.');
631
- exit;
632
- }
633
- } else {
634
- $this->set_import_error_reporting_message('FTP connection failed. Unable to connect to the Stock In The Channel server');
635
- exit;
636
- }
637
- if (!$this->wget($file_url_and_dir . $file, $this->varDir . $file, 'system')) {
638
- $this->_LOG("wget Can't copy " . $file . ", will use old one");
639
- echo "copy Can't copy " . $file_url_and_dir . $file . " to " . $this->varDir . $file . ", will use old one<br>";
640
- }
641
- } else {
642
- if (!copy($file_url_and_dir . $file, $this->varDir . $file)) {
643
- $this->_LOG("copy Can't copy " . $file . ", will use old one");
644
- echo "copy Can't copy " . $file_url_and_dir . $file . " to " . $this->varDir . $file . " will use old one<br>";
645
- }
646
- }
647
- exec("chmod a+rw " . $this->varDir . $file);
648
- if (!filesize($this->varDir . $file)) {
649
- if ($file != FILE_CATEGORIES_FEATURES && $file != FILE_PRODUCT_FEATURES && $file != FILE_RELATED_PRODUCTS && $file != FILE_RESTRICTED_VALUES && $file != FILE_PRODUCT_CATEGORIES && $file != FILE_CATEGORY_TYPES && $file != FILE_DISTRIBUTORS_STOCK_AND_PRICES && $file != FILE_PRODUCT_CONTRACTS && $file != FILE_PRICE_RULES) {
650
- $this->_LOG("Can't copy " . $file_url_and_dir . $file . ". file $this->varDir.$file is emty");
651
- $this->set_import_error_reporting_message("Can't copy " . $file_url_and_dir . $file . ". file " . $this->varDir . $file . " is emty");
652
- $this->addImportStatus('Sinch import stoped. Impot file(s) empty', 1);
653
-
654
- exit;
655
- } else {
656
- if ($file == FILE_CATEGORIES_FEATURES) {
657
- $this->_LOG("Can't copy " . FILE_CATEGORIES_FEATURES . " file ignored");
658
- $this->_ignore_category_features = true;
659
- } elseif ($file == FILE_PRODUCT_FEATURES) {
660
- $this->_LOG("Can't copy " . FILE_PRODUCT_FEATURES . " file ignored");
661
- $this->_ignore_product_features = true;
662
- } elseif ($file == FILE_RELATED_PRODUCTS) {
663
- $this->_LOG("Can't copy " . FILE_RELATED_PRODUCTS . " file ignored");
664
- $this->_ignore_product_related = true;
665
- } elseif ($file == FILE_RESTRICTED_VALUES) {
666
- $this->_LOG("Can't copy " . FILE_RESTRICTED_VALUES . " file ignored");
667
- $this->_ignore_restricted_values = true;
668
- } elseif ($file == FILE_PRODUCT_CATEGORIES) {
669
- $this->_LOG("Can't copy " . FILE_PRODUCT_CATEGORIES . " file ignored");
670
- $this->_ignore_product_categories = true;
671
- $this->product_file_format = "OLD";
672
- } elseif ($file == FILE_CATEGORY_TYPES) {
673
- $this->_LOG("Can't copy " . FILE_CATEGORY_TYPES . " file ignored");
674
- $this->_ignore_category_types = true;
675
- } elseif ($file == FILE_DISTRIBUTORS_STOCK_AND_PRICES) {
676
- $this->_LOG("Can't copy " . FILE_DISTRIBUTORS_STOCK_AND_PRICES . " file ignored");
677
- $this->_ignore_category_types = true;
678
- } elseif ($file == FILE_PRODUCT_CONTRACTS) {
679
- $this->_LOG("Can't copy " . FILE_PRODUCT_CONTRACTS . " file ignored");
680
- $this->_ignore_product_contracts = true;
681
- } elseif ($file == FILE_PRICE_RULES) {
682
- $this->_LOG("Can't copy " . FILE_PRICE_RULES . " file ignored");
683
- $this->_ignore_price_rules = true;
684
- }
685
-
686
- }
687
- }
688
- }
689
- if (file_exists($file_url_and_dir . FILE_PRODUCT_CATEGORIES)) {
690
- $this->product_file_format = "NEW";
691
- $this->_LOG("File " . $file_url_and_dir . FILE_PRODUCT_CATEGORIES . " exist. Will used parser for NEW format product.csv");
692
- } else {
693
- $this->product_file_format = "OLD";
694
- $this->_LOG("File " . $file_url_and_dir . FILE_PRODUCT_CATEGORIES . " dosen't exist. Will used parser for OLD format product.csv");
695
- }
696
- $this->_LOG("Finish upload files");
697
- } // private function createNewDefaultCategories()
698
- ################################################################################################################################################################
699
-
700
-
701
- ################################################################################################################################################################
702
-
703
- private function repl_ph($content, $hash)
704
- {
705
- if ($hash) {
706
- foreach ($hash as $key => $val) {
707
- if ($key == "category_name") {
708
- if (strlen($val) > 25) {
709
- $val = substr($val, 0, 24) . "...";
710
- }
711
- }
712
- $content = preg_replace("/%%%$key%%%/", $val, $content);
713
- }
714
- }
715
- return $content;
716
- } // private function calculateCategoryCoincidence($categories_temp, $catalog_category_entity)
717
- ################################################################################################################################################################
718
-
719
-
720
- ################################################################################################################################################################
721
-
722
- function wget()
723
- {
724
-
725
- $got = func_num_args();
726
- $url = $file = $flag = false;
727
-
728
- if ($got < 1) {
729
- return false;
730
- } elseif ($got == 1) {
731
- $url = func_get_arg(0);
732
- } elseif ($got == 2) {
733
- $url = func_get_arg(0);
734
- $file = func_get_arg(1);
735
- } elseif ($got == 3) {
736
- $url = func_get_arg(0);
737
- $file = func_get_arg(1);
738
- $flag = func_get_arg(2);
739
- }
740
-
741
- if ($flag == 'copy') {
742
- if (copy($url, $file)) {
743
- return true;
744
- } else {
745
- return false;
746
- }
747
- } elseif ($flag == 'system') {
748
- exec("wget -O$file $url");
749
- return true;
750
- } else {
751
- $c = curl_init($url);
752
- curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
753
- curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
754
- curl_setopt($c, CURLOPT_HEADER, array("Accept-Encoding: gzip"));
755
- if (!$file) {
756
- $page = curl_exec($c);
757
- curl_close($c);
758
- return $page;
759
- } else {
760
- $FH = fopen($file, "wb");// or echo"Can't open for writing ".$file;
761
- fwrite($FH, curl_exec($c));
762
- fclose($FH);
763
- curl_close($c);
764
- return true;
765
- }
766
- }
767
- } // private function rewriteMultistoreCategories()
768
- ################################################################################################################################################################
769
-
770
-
771
- ################################################################################################################################################################
772
-
773
- function ParseCategoryTypes()
774
- {
775
- $parse_file = $this->varDir . FILE_CATEGORY_TYPES;
776
- if (filesize($parse_file)) {
777
- $this->_LOG("Start parse " . FILE_CATEGORY_TYPES);
778
-
779
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('category_types_temp'));
780
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('category_types_temp') . "(
781
- id int(11),
782
- name varchar(255),
783
- key(id)
784
- )");
785
-
786
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
787
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('category_types_temp') . "
788
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
789
- OPTIONALLY ENCLOSED BY '\"'
790
- LINES TERMINATED BY \"\r\n\"
791
- IGNORE 1 LINES ");
792
-
793
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_category_types'));
794
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('category_types_temp') . "
795
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_category_types'));
796
-
797
- $this->_LOG("Finish parse " . FILE_CATEGORY_TYPES);
798
- } else {
799
- $this->_LOG("Wrong file " . $parse_file);
800
- }
801
- $this->_LOG(' ');
802
-
803
- } // private function truncateAllCateriesAndCreateRoot(...)
804
- ################################################################################################################################################################
805
-
806
-
807
- ################################################################################################################################################################
808
-
809
- function ParseCategories()
810
- {
811
-
812
- $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
813
- $im_type = $dataConf['replace_category'];
814
- $parse_file = $this->varDir . FILE_CATEGORIES;
815
- //$parse_file = $this->varDir . FILE_CATEGORIES_TEST;
816
- $field_terminated_char = $this->field_terminated_char;
817
-
818
- $this->im_type = $im_type;
819
-
820
- if (filesize($parse_file)) {
821
- $this->_LOG("Start parse " . FILE_CATEGORIES);
822
-
823
- $this->_getCategoryEntityTypeIdAndDefault_attribute_set_id();
824
-
825
- $categories_temp = Mage::getSingleton('core/resource')->getTableName('categories_temp');
826
- $catalog_category_entity = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity');
827
- $catalog_category_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar');
828
- $catalog_category_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_int');
829
- $stINch_categories_mapping_temp = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping_temp');
830
- $stINch_categories_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping');
831
- $stINch_categories = Mage::getSingleton('core/resource')->getTableName('stINch_categories');
832
- $category_types = Mage::getSingleton('core/resource')->getTableName('stINch_category_types');
833
-
834
- $_categoryEntityTypeId = $this->_categoryEntityTypeId;
835
- $_categoryDefault_attribute_set_id = $this->_categoryDefault_attribute_set_id;
836
-
837
- $name_attrid = $this->_getCategoryAttributeId('name');
838
- $is_anchor_attrid = $this->_getCategoryAttributeId('is_anchor');
839
- $image_attrid = $this->_getCategoryAttributeId('image');
840
-
841
-
842
- $attr_url_key = $this->attributes['url_key'];
843
- $attr_display_mode = $this->attributes['display_mode'];
844
- $attr_is_active = $this->attributes['is_active'];
845
- $attr_include_in_menu = $this->attributes['include_in_menu'];
846
-
847
-
848
- $this->loadCategoriesTemp($categories_temp, $parse_file, $field_terminated_char);
849
- $coincidence = $this->calculateCategoryCoincidence($categories_temp, $catalog_category_entity, $catalog_category_entity_varchar, $im_type, $category_types);
850
-
851
- /**/
852
- if (!$this->check_loaded_data($parse_file, $categories_temp)) {
853
- $inf = mysqli_info();
854
- $this->set_import_error_reporting_message('The Stock In The Channel data files do not appear to be in the correct format. Check file' . $parse_file . "(LOAD DATA ... " . $inf . ")");
855
- exit;
856
- }/**/
857
-
858
-
859
- echo("\n\ncoincidence = [" . count($coincidence) . "]\n\n");
860
-
861
- if (count($coincidence) == 1) // one store logic
862
- {
863
- echo("\n\n\n\n\n\nOLD LOGIC\n\n\n\n\n\n\n\n\n");
864
- if ($im_type == "REWRITE") {
865
- $root_cat = 2;
866
-
867
- $root_cat = $this->truncateAllCateriesAndRecreateDefaults($root_cat, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
868
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id,
869
- $name_attrid, $attr_url_key, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu); // return $root_cat
870
- } else // if ($im_type == "MERGE")
871
- {
872
- $root_cat = $this->_getShopRootCategoryId();
873
- }
874
-
875
- $this->_root_cat = $root_cat;
876
-
877
- $this->setCategorySettings($categories_temp, $root_cat);
878
- $this->mapSinchCategories($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type, $root_cat);
879
- $this->addCategoryData($categories_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
880
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid, $im_type, $root_cat);
881
- } else if (count($coincidence) > 1) // multistore logic
882
- {
883
- echo("\n\n\n====================================\nmultistore logic\n====================================\n\n\n");
884
- switch ($im_type) {
885
- case "REWRITE":
886
- $this->rewriteMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
887
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
888
- $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
889
- $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp);
890
- break;
891
- case "MERGE" :
892
- $this->mergeMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
893
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
894
- $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
895
- $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp);
896
- break;
897
- default :
898
- $retcode = "error";
899
- };
900
- } else {
901
- echo("error");
902
- }
903
-
904
- $this->_LOG("Finish parse " . FILE_CATEGORIES);
905
- } else {
906
- $this->_LOG("Wrong file " . $parse_file);
907
- }
908
- $this->_LOG(' ');
909
- $this->_set_default_root_category();
910
- return $coincidence;
911
- } // private function truncateAllCateries()
912
- ################################################################################################################################################################
913
-
914
-
915
- ################################################################################################################################################################
916
-
917
- private function _getCategoryEntityTypeIdAndDefault_attribute_set_id()
918
- {
919
- if (!$this->_categoryEntityTypeId || !$this->_categoryDefault_attribute_set_id) {
920
- $sql = "
921
- SELECT entity_type_id, default_attribute_set_id
922
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_entity_type') . "
923
- WHERE entity_type_code = 'catalog_category'
924
- LIMIT 1
925
- ";
926
- $result = $this->db_do($sql);
927
- if ($row = mysqli_fetch_assoc($result)) {
928
- $this->_categoryEntityTypeId = $row['entity_type_id'];
929
- $this->_categoryDefault_attribute_set_id = $row['default_attribute_set_id'];
930
- }
931
-
932
- }
933
- } // public function mapSinchCategoriesMultistore($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type)
934
- ################################################################################################################################################################
935
-
936
-
937
- ################################################################################################################################################################
938
-
939
- private function loadCategoriesTemp($categories_temp, $parse_file, $field_terminated_char)
940
- {
941
- $this->db_do("DROP TABLE IF EXISTS $categories_temp");
942
-
943
-
944
- /** OLD !!!*
945
- * $this->db_do("CREATE TABLE $categories_temp (
946
- * store_category_id int(11),
947
- * parent_store_category_id int(11),
948
- * category_name varchar(50),
949
- * order_number int(11),
950
- * is_hidden boolean,
951
- * products_within_this_category int(11),
952
- * products_within_sub_categories int(11),
953
- * categories_image varchar(255),
954
- * level int(10) NOT NULL default 0,
955
- * children_count int(11) NOT NULL default 0,
956
- * KEY(store_category_id),
957
- * KEY(parent_store_category_id)
958
- * ) ENGINE=InnoDB DEFAULT CHARSET=utf8
959
- * ");
960
- * /**/
961
-
962
- /** NEW !!! */
963
- $this->db_do("
964
- CREATE TABLE $categories_temp
965
- (
966
- store_category_id INT(11),
967
- parent_store_category_id INT(11),
968
- category_name VARCHAR(50),
969
- order_number INT(11),
970
- is_hidden VARCHAR(10),
971
- products_within_sub_categories INT(11),
972
- products_within_this_category INT(11),
973
- categories_image VARCHAR(255),
974
- level INT(10) NOT NULL DEFAULT 0,
975
- children_count INT(11) NOT NULL DEFAULT 0,
976
- UNSPSC INT(10) DEFAULT NULL,
977
- RootName INT(10) DEFAULT NULL,
978
- MainImageURL VARCHAR(255),
979
- MetaTitle TEXT,
980
- MetaDescription TEXT,
981
- Description TEXT,
982
- KEY(store_category_id),
983
- KEY(parent_store_category_id)
984
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
985
- /**/
986
-
987
- $this->db_do("
988
- LOAD DATA LOCAL INFILE '$parse_file' INTO TABLE $categories_temp
989
- FIELDS TERMINATED BY '$field_terminated_char' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY \"\r\n\" IGNORE 1 LINES");
990
-
991
- $this->db_do("ALTER TABLE $categories_temp ADD COLUMN include_in_menu TINYINT(1) NOT NULL DEFAULT 1");
992
- $this->db_do("UPDATE $categories_temp SET include_in_menu = 0 WHERE UCASE(is_hidden)='TRUE'");
993
-
994
- $this->db_do("ALTER TABLE $categories_temp ADD COLUMN is_anchor TINYINT(1) NOT NULL DEFAULT 1");
995
- $this->db_do("UPDATE $categories_temp SET level = (level+2) WHERE level >= 0");
996
- # $this->db_do("UPDATE $categories_temp SET is_anchor = 0 WHERE level > 0");
997
-
998
-
999
- /** FOR TEST !!! *
1000
- * $this->db_do("ALTER TABLE $categories_temp ADD COLUMN UNSPSC INT(10) NOT NULL DEFAULT 0");
1001
- * $this->db_do("ALTER TABLE $categories_temp ADD COLUMN RootName VARCHAR(50) NOT NULL DEFAULT 0");
1002
- *
1003
- * //$this->db_do("UPDATE $categories_temp SET RootName = '3'"); // one store logic test
1004
- *
1005
- * $this->db_do("UPDATE $categories_temp SET RootName = 'KAMERY' WHERE store_category_id IN (93530, 93531, 93230, 93231, 175559, 175687)");
1006
- * $this->db_do("UPDATE $categories_temp SET RootName = 'PROJECTORS' WHERE store_category_id IN (151019, 151066, 175554, 175555, 175579, 175553)");
1007
- * $this->db_do("DELETE FROM $categories_temp WHERE store_category_id NOT IN (151019, 151066, 175554, 175555, 175579, 175553, 93530, 93531, 93230, 93231, 175559, 175687)");
1008
- *
1009
- *
1010
- * //$this->db_do("UPDATE $categories_temp SET RootName = 'PROJECTORS' WHERE store_category_id IN (151019, 151066, 175554, 175555, 175579, 175553)");
1011
- * //$this->db_do("DELETE FROM $categories_temp WHERE store_category_id NOT IN (151019, 151066, 175554, 175555, 175579, 175553)");
1012
- *
1013
- *
1014
- * //$this->db_do("DELETE FROM $categories_temp WHERE store_category_id IN (175687, 175553)"); // OLD CATS...//
1015
- *
1016
- * /**/
1017
- }
1018
- ################################################################################################################################################################
1019
-
1020
-
1021
- ################################################################################################################################################################
1022
-
1023
- private function calculateCategoryCoincidence($categories_temp, $catalog_category_entity, $catalog_category_entity_varchar, $im_type, $category_types)
1024
- {
1025
- $root_categories = $this->db_do("
1026
- SELECT
1027
- cce.entity_id,
1028
- ccev.value AS category_name
1029
- FROM $catalog_category_entity cce
1030
- JOIN $catalog_category_entity_varchar ccev
1031
- ON cce.entity_id = ccev.entity_id
1032
- AND ccev.store_id = 0
1033
- AND cce.entity_type_id = ccev.entity_type_id
1034
- AND ccev.attribute_id = 41
1035
- WHERE parent_id = 1"); // 41 - category name
1036
- $OLD = array();
1037
- while ($root_cat = mysqli_fetch_array($root_categories)) $OLD[] = $root_cat['category_name'];
1038
-
1039
- $new_categories = $this->db_do("SELECT DISTINCT RootName FROM $categories_temp");
1040
-
1041
- //STP $new_categories = $this->db_do("SELECT DISTINCT ctemp.RootName, ctype.name FROM $categories_temp ctemp LEFT JOIN $category_types ctypes on ctemp.RootName = ctype.name");
1042
-
1043
- $NEW = array();
1044
- while ($new_root_cat = mysqli_fetch_array($new_categories)) $exists_coincidence[$new_root_cat['RootName']] = TRUE;
1045
- /////STP while($new_root_cat = mysqli_fetch_array($new_categories)) $exists_coincidence[$new_root_cat['name']] = TRUE;
1046
- /**
1047
- * $exists_coincidence = array();
1048
- *
1049
- * switch ($im_type)
1050
- * {
1051
- * case "REWRITE":
1052
- * foreach($NEW as $item)
1053
- * {
1054
- * $exists_coincidence[$item] = TRUE;
1055
- * }
1056
- * break;
1057
- * case "MERGE" :
1058
- * foreach($OLD as $item)
1059
- * {
1060
- * $exists_coincidence[$item] = FALSE;
1061
- * }
1062
- * foreach($NEW as $item)
1063
- * {
1064
- * $exists_coincidence[$item] = TRUE;
1065
- * }
1066
- * break;
1067
- * default : $retcode = "error";
1068
- * };
1069
- * /**/
1070
-
1071
-
1072
- echo("\ncalculateCategoryCoincidence ...im_type = [$im_type]\n\n");
1073
- var_dump($exists_coincidence);
1074
-
1075
- return $exists_coincidence;
1076
- } // private function addCategoryDataMultistore(...)
1077
- ################################################################################################################################################################
1078
-
1079
-
1080
- ################################################################################################################################################################
1081
-
1082
- function check_loaded_data($file, $table)
1083
- {
1084
- $cnt_strings_in_file = $this->file_strings_count($file);
1085
- $cnt_rows_int_table = $this->table_rows_count($table);
1086
- $persent_cnt_strings_in_file = $cnt_strings_in_file / 10;
1087
- if ($cnt_rows_int_table > $persent_cnt_strings_in_file) {
1088
- return true;
1089
- } else {
1090
- return false;
1091
- }
1092
- } // function culcPathMultistore($parent_id, $ent_id, $catalog_category_entity)
1093
- ################################################################################################################################################################
1094
-
1095
-
1096
- ################################################################################################################################################################
1097
-
1098
- function file_strings_count($parse_file)
1099
- {
1100
- $files_str = count(file($parse_file));
1101
- return $files_str;
1102
- } //
1103
- ################################################################################################################################################################
1104
-
1105
-
1106
- ################################################################################################################################################################
1107
-
1108
- function table_rows_count($table)
1109
- {
1110
- $rows_count_res = $this->db_do("select count(*) as cnt from " . $table);
1111
- $rows_count = mysqli_fetch_array($rows_count_res);
1112
- return ($rows_count['cnt']);
1113
- } //
1114
- ################################################################################################################################################################
1115
-
1116
-
1117
- ################################################################################################################################################################
1118
-
1119
- private function truncateAllCateriesAndRecreateDefaults($root_cat, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1120
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id,
1121
- $name_attrid, $attr_url_key, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu)
1122
- {
1123
- $this->db_do('SET foreign_key_checks=0');
1124
-
1125
- $this->db_do("TRUNCATE $catalog_category_entity");
1126
- $this->db_do("
1127
- INSERT $catalog_category_entity
1128
- (
1129
- entity_id,
1130
- entity_type_id,
1131
- attribute_set_id,
1132
- parent_id,
1133
- created_at,
1134
- updated_at,
1135
- path,
1136
- position,
1137
- level,
1138
- children_count,
1139
- store_category_id,
1140
- parent_store_category_id
1141
- )
1142
- VALUES
1143
- (1, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 0, '0000-00-00 00:00:00', now(), '1', 0, 0, 1, null, null),
1144
- (2, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 1, now(), now(), '1/2', 1, 1, 1, null, null)");
1145
-
1146
- $this->db_do("TRUNCATE $catalog_category_entity_varchar");
1147
- $this->db_do("
1148
- INSERT $catalog_category_entity_varchar
1149
- (
1150
- value_id,
1151
- entity_type_id,
1152
- attribute_id,
1153
- store_id,
1154
- entity_id,
1155
- value
1156
- )
1157
- VALUES
1158
- (1, $_categoryEntityTypeId, $name_attrid, 0, 1, 'Root Catalog'),
1159
- (2, $_categoryEntityTypeId, $name_attrid, 1, 1, 'Root Catalog'),
1160
- (3, $_categoryEntityTypeId, $attr_url_key, 0, 1, 'root-catalog'),
1161
- (4, $_categoryEntityTypeId, $name_attrid, 0, 2, 'Default Category'),
1162
- (5, $_categoryEntityTypeId, $name_attrid, 1, 2, 'Default Category'),
1163
- (6, $_categoryEntityTypeId, $attr_display_mode, 1, 2, 'PRODUCTS'),
1164
- (7, $_categoryEntityTypeId, $attr_url_key, 0, 2, 'default-category')");
1165
-
1166
- $this->db_do("TRUNCATE $catalog_category_entity_int");
1167
- $this->db_do("
1168
- INSERT $catalog_category_entity_int
1169
- (
1170
- value_id,
1171
- entity_type_id,
1172
- attribute_id,
1173
- store_id,
1174
- entity_id,
1175
- value
1176
- )
1177
- VALUES
1178
- (1, $_categoryEntityTypeId, $attr_is_active, 0, 2, 1),
1179
- (2, $_categoryEntityTypeId, $attr_is_active, 1, 2, 1),
1180
- (3, $_categoryEntityTypeId, $attr_include_in_menu, 0, 1, 1),
1181
- (4, $_categoryEntityTypeId, $attr_include_in_menu, 0, 2, 1)");
1182
-
1183
- return $root_cat;
1184
- } // private function truncateAllCateriesAndRecreateDefaults(...)
1185
- ################################################################################################################################################################
1186
-
1187
-
1188
- ################################################################################################################################################################
1189
-
1190
- private function _getShopRootCategoryId($cat_id = 0)
1191
- {
1192
- if ($root_cat = Mage::app()->getStore()->getRootCategoryId()) {
1193
- return $root_cat;
1194
- } else {
1195
- $q = "SELECT
1196
- entity_id
1197
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar') . "
1198
- WHERE
1199
- value='default-category'";
1200
- $res = $this->db_do($q);
1201
- $row = mysqli_fetch_array($res);
1202
- if ($row['entity_id'] > 0) {
1203
- return $row['entity_id'];
1204
- } else {
1205
- $q = "SELECT entity_id
1206
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
1207
- WHERE parent_id=" . $cat_id;
1208
- $quer = $this->db_do($q);
1209
- $count = 0;
1210
- while ($row = mysqli_fetch_array($quer)) {
1211
- $count++;
1212
- $entity_id = $row['entity_id'];
1213
- }
1214
- if ($count > 1 || $count == 0) {
1215
- return ($cat_id);
1216
- } else {
1217
- return $this->_getShopRootCategoryId($entity_id);
1218
- }
1219
- }
1220
- }
1221
- } // private function setCategorySettings($categories_temp, $root_cat)
1222
- ################################################################################################################################################################
1223
-
1224
-
1225
- ################################################################################################################################################################
1226
-
1227
- private function setCategorySettings($categories_temp, $root_cat)
1228
- {
1229
- $this->db_do("
1230
- UPDATE $categories_temp
1231
- SET parent_store_category_id = $root_cat
1232
- WHERE parent_store_category_id = 0");
1233
-
1234
- $store_cat_ids = $this->db_do("SELECT store_category_id FROM $categories_temp");
1235
- while ($row = mysqli_fetch_array($store_cat_ids)) {
1236
- $store_category_id = $row['store_category_id'];
1237
-
1238
- $children_count = $this->count_children($store_category_id);
1239
- $level = $this->get_category_level($store_category_id);
1240
-
1241
- $this->db_do("
1242
- UPDATE $categories_temp
1243
- SET children_count = $children_count,
1244
- level = $level
1245
- WHERE store_category_id = $store_category_id");
1246
- }
1247
- } //
1248
- ################################################################################################################################################################
1249
-
1250
-
1251
- ################################################################################################################################################################
1252
-
1253
- function count_children($id)
1254
- {
1255
-
1256
- $q = "SELECT store_category_id
1257
- FROM " . Mage::getSingleton('core/resource')->getTableName('categories_temp') . "
1258
- WHERE parent_store_category_id=" . $id;
1259
- $quer = $this->db_do($q);
1260
- $count = 0;
1261
- while ($row = mysqli_fetch_array($quer)) {
1262
- $count += $this->count_children($row['store_category_id']);
1263
- $count++;
1264
- }
1265
- return ($count);
1266
- } // private function addCategoryData(...)
1267
-
1268
- ################################################################################################################################################################
1269
-
1270
- function get_category_level($id)
1271
- {
1272
- $q = "SELECT parent_store_category_id
1273
- FROM " . Mage::getSingleton('core/resource')->getTableName('categories_temp') . "
1274
- WHERE store_category_id=" . $id;
1275
- $quer = $this->db_do($q);
1276
- $level = 1;
1277
- $row = mysqli_fetch_array($quer);
1278
- while ($row['parent_store_category_id'] != 0) {
1279
- $q = "SELECT parent_store_category_id
1280
- FROM " . Mage::getSingleton('core/resource')->getTableName('categories_temp') . "
1281
- WHERE store_category_id=" . $row['parent_store_category_id'];
1282
- $quer = $this->db_do($q);
1283
- $row = mysqli_fetch_array($quer);
1284
- $level++;
1285
- if ($level > 20) {
1286
- break;
1287
- }
1288
- }
1289
-
1290
- return ($level);
1291
- }
1292
-
1293
- #################################################################################################
1294
-
1295
- public function mapSinchCategories($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type, $root_cat, $mapping_again = false)
1296
- {
1297
- $stINch_categories_mapping_temp = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping_temp');
1298
-
1299
- $this->db_do("DROP TABLE IF EXISTS $stINch_categories_mapping_temp");
1300
-
1301
- $this->db_do("
1302
- CREATE TABLE $stINch_categories_mapping_temp
1303
- (
1304
- shop_entity_id INT(11) UNSIGNED NOT NULL,
1305
- shop_entity_type_id INT(11),
1306
- shop_attribute_set_id INT(11),
1307
- shop_parent_id INT(11),
1308
- shop_store_category_id INT(11),
1309
- shop_parent_store_category_id INT(11),
1310
- store_category_id INT(11),
1311
- parent_store_category_id INT(11),
1312
- category_name VARCHAR(255),
1313
- order_number INT(11),
1314
- products_within_this_category INT(11),
1315
-
1316
- KEY shop_entity_id (shop_entity_id),
1317
- KEY shop_parent_id (shop_parent_id),
1318
- KEY store_category_id (store_category_id),
1319
- KEY parent_store_category_id (parent_store_category_id),
1320
- UNIQUE KEY(shop_entity_id)
1321
- )");
1322
-
1323
- $this->db_do("CREATE TABLE IF NOT EXISTS $stINch_categories_mapping LIKE $stINch_categories_mapping_temp");
1324
-
1325
- // !!!!!!!!!!!!!!!!!!!!!!!!!!! one shop ($this->_root_cat) => milti shop ($root_cat)
1326
-
1327
- // added for mapping new sinch categories in merge && !UPDATE_CATEGORY_DATA mode
1328
- if ((UPDATE_CATEGORY_DATA && $im_type == "MERGE") || ($im_type == "REWRITE")) {
1329
- // backup Category ID in REWRITE mode
1330
- if ($mapping_again) {
1331
- $this->db_do("
1332
- INSERT IGNORE INTO $stINch_categories_mapping_temp
1333
- (
1334
- shop_entity_id,
1335
- shop_entity_type_id,
1336
- shop_attribute_set_id,
1337
- shop_parent_id,
1338
- shop_store_category_id,
1339
- shop_parent_store_category_id
1340
- )
1341
- (SELECT
1342
- entity_id,
1343
- entity_type_id,
1344
- attribute_set_id,
1345
- parent_id,
1346
- store_category_id,
1347
- parent_store_category_id
1348
- FROM $catalog_category_entity)");
1349
-
1350
- $this->db_do("
1351
- UPDATE $stINch_categories_mapping_temp cmt
1352
- JOIN $categories_temp c
1353
- ON cmt.shop_store_category_id = c.store_category_id
1354
- SET
1355
- cmt.store_category_id = c.store_category_id,
1356
- cmt.parent_store_category_id = c.parent_store_category_id,
1357
- cmt.category_name = c.category_name,
1358
- cmt.order_number = c.order_number,
1359
- cmt.products_within_this_category = c.products_within_this_category");
1360
-
1361
- $this->db_do("
1362
- UPDATE $stINch_categories_mapping_temp cmt
1363
- JOIN $catalog_category_entity cce
1364
- ON cmt.parent_store_category_id = cce.store_category_id
1365
- SET cmt.shop_parent_id = cce.entity_id");
1366
-
1367
- $this->db_do("
1368
- UPDATE $stINch_categories_mapping_temp cmt
1369
- JOIN $categories_temp c
1370
- ON cmt.shop_store_category_id = c.store_category_id
1371
- SET shop_parent_id = " . $this->_root_cat . "
1372
- WHERE shop_parent_id = 0");
1373
-
1374
- $this->db_do("
1375
- UPDATE $stINch_categories_mapping_temp cmt
1376
- JOIN $catalog_category_entity cce
1377
- ON cmt.shop_entity_id = cce.entity_id
1378
- SET cce.parent_id = cmt.shop_parent_id");
1379
- } else {
1380
- $catalog_category_entity_backup = Mage::getSingleton('core/resource')->getTableName('sinch_category_backup');
1381
- if (!$this->_checkCategoryBackupExist($catalog_category_entity_backup)) {
1382
- $catalog_category_entity_backup = $catalog_category_entity;
1383
- }
1384
-
1385
- $this->db_do("
1386
- INSERT IGNORE INTO $stINch_categories_mapping_temp
1387
- (
1388
- shop_entity_id,
1389
- shop_entity_type_id,
1390
- shop_attribute_set_id,
1391
- shop_parent_id,
1392
- shop_store_category_id,
1393
- shop_parent_store_category_id
1394
- )
1395
- (SELECT
1396
- entity_id,
1397
- entity_type_id,
1398
- attribute_set_id,
1399
- parent_id,
1400
- store_category_id,
1401
- parent_store_category_id
1402
- FROM $catalog_category_entity_backup)");
1403
-
1404
- $this->db_do("
1405
- UPDATE $stINch_categories_mapping_temp cmt
1406
- JOIN $categories_temp c
1407
- ON cmt.shop_store_category_id = c.store_category_id
1408
- SET
1409
- cmt.store_category_id = c.store_category_id,
1410
- cmt.parent_store_category_id = c.parent_store_category_id,
1411
- cmt.category_name = c.category_name,
1412
- cmt.order_number = c.order_number,
1413
- cmt.products_within_this_category = c.products_within_this_category");
1414
-
1415
- $this->db_do("
1416
- UPDATE $stINch_categories_mapping_temp cmt
1417
- JOIN $catalog_category_entity_backup cce
1418
- ON cmt.parent_store_category_id = cce.store_category_id
1419
- SET cmt.shop_parent_id = cce.entity_id");
1420
-
1421
- $this->db_do("
1422
- UPDATE $stINch_categories_mapping_temp cmt
1423
- JOIN $categories_temp c
1424
- ON cmt.shop_store_category_id = c.store_category_id
1425
- SET shop_parent_id = " . $this->_root_cat . "
1426
- WHERE shop_parent_id = 0");
1427
-
1428
- $this->db_do("
1429
- UPDATE $stINch_categories_mapping_temp cmt
1430
- JOIN $catalog_category_entity cce
1431
- ON cmt.shop_entity_id = cce.entity_id
1432
- SET cce.parent_id = cmt.shop_parent_id");
1433
- }
1434
- // (end) backup Category ID in REWRITE mode
1435
- } else {
1436
- $this->db_do("
1437
- INSERT IGNORE INTO $stINch_categories_mapping_temp
1438
- (
1439
- shop_entity_id,
1440
- shop_entity_type_id,
1441
- shop_attribute_set_id,
1442
- shop_parent_id,
1443
- shop_store_category_id,
1444
- shop_parent_store_category_id
1445
- )
1446
- (SELECT
1447
- entity_id,
1448
- entity_type_id,
1449
- attribute_set_id,
1450
- parent_id,
1451
- store_category_id,
1452
- parent_store_category_id
1453
- FROM $catalog_category_entity)");
1454
-
1455
- $this->db_do("
1456
- UPDATE $stINch_categories_mapping_temp cmt
1457
- JOIN $categories_temp c
1458
- ON cmt.shop_store_category_id = c.store_category_id
1459
- SET
1460
- cmt.store_category_id = c.store_category_id,
1461
- cmt.parent_store_category_id = c.parent_store_category_id,
1462
- cmt.category_name = c.category_name,
1463
- cmt.order_number = c.order_number,
1464
- cmt.products_within_this_category = c.products_within_this_category");
1465
-
1466
- $this->db_do("
1467
- UPDATE $stINch_categories_mapping_temp cmt
1468
- JOIN $catalog_category_entity cce
1469
- ON cmt.parent_store_category_id = cce.store_category_id
1470
- SET cmt.shop_parent_id = cce.entity_id");
1471
-
1472
- $this->db_do("
1473
- UPDATE $stINch_categories_mapping_temp cmt
1474
- JOIN $categories_temp c
1475
- ON cmt.shop_store_category_id = c.store_category_id
1476
- SET shop_parent_id = " . $this->_root_cat . "
1477
- WHERE shop_parent_id = 0");
1478
-
1479
- $this->db_do("
1480
- UPDATE $stINch_categories_mapping_temp cmt
1481
- JOIN $catalog_category_entity cce
1482
- ON cmt.shop_entity_id = cce.entity_id
1483
- SET cce.parent_id = cmt.shop_parent_id
1484
- WHERE cce.parent_id = 0 AND cce.store_category_id IS NOT NULL");
1485
- }
1486
- $this->_LOG("Execute function mapSinchCategories");
1487
- $this->db_do("DROP TABLE IF EXISTS $stINch_categories_mapping");
1488
- $this->db_do("RENAME TABLE $stINch_categories_mapping_temp TO $stINch_categories_mapping");
1489
- }
1490
-
1491
- #################################################################################################
1492
-
1493
- private function addCategoryData($categories_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1494
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid, $im_type, $root_cat)
1495
- {
1496
- if (UPDATE_CATEGORY_DATA) {
1497
- echo "Update category_entity \n";
1498
-
1499
- $q = "
1500
- INSERT INTO $catalog_category_entity
1501
- (
1502
- entity_type_id,
1503
- attribute_set_id,
1504
- created_at,
1505
- updated_at,
1506
- level,
1507
- children_count,
1508
- entity_id,
1509
- position,
1510
- parent_id,
1511
- store_category_id,
1512
- parent_store_category_id
1513
- )
1514
- (SELECT
1515
- $_categoryEntityTypeId,
1516
- $_categoryDefault_attribute_set_id,
1517
- now(),
1518
- now(),
1519
- c.level,
1520
- c.children_count,
1521
- scm.shop_entity_id,
1522
- c.order_number,
1523
- scm.shop_parent_id,
1524
- c.store_category_id,
1525
- c.parent_store_category_id
1526
- FROM $categories_temp c
1527
- LEFT JOIN $stINch_categories_mapping scm
1528
- ON c.store_category_id = scm.store_category_id
1529
- )
1530
- ON DUPLICATE KEY UPDATE
1531
- updated_at = now(),
1532
- store_category_id = c.store_category_id,
1533
- level = c.level,
1534
- children_count = c.children_count,
1535
- position = c.order_number,
1536
- parent_store_category_id = c.parent_store_category_id";
1537
- //level=c.level,
1538
- //children_count=c.children_count
1539
- //position=c.order_number,
1540
- } else {
1541
- echo "Insert ignore category_entity \n";
1542
-
1543
- $q = "
1544
- INSERT IGNORE INTO $catalog_category_entity
1545
- (
1546
- entity_type_id,
1547
- attribute_set_id,
1548
- created_at,
1549
- updated_at,
1550
- level,
1551
- children_count,
1552
- entity_id,
1553
- position,
1554
- parent_id,
1555
- store_category_id,
1556
- parent_store_category_id
1557
- )
1558
- (SELECT
1559
- $_categoryEntityTypeId,
1560
- $_categoryDefault_attribute_set_id,
1561
- now(),
1562
- now(),
1563
- c.level,
1564
- c.children_count,
1565
- scm.shop_entity_id,
1566
- c.order_number,
1567
- scm.shop_parent_id,
1568
- c.store_category_id,
1569
- c.parent_store_category_id
1570
- FROM $categories_temp c
1571
- LEFT JOIN $stINch_categories_mapping scm
1572
- ON c.store_category_id = scm.store_category_id
1573
- )";
1574
- }
1575
- $this->db_do($q);
1576
-
1577
- $this->mapSinchCategories($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type, $root_cat, true);
1578
-
1579
-
1580
- $categories = $this->db_do("SELECT entity_id, parent_id FROM $catalog_category_entity ORDER BY parent_id");
1581
- while ($row = mysqli_fetch_array($categories)) {
1582
- $parent_id = $row['parent_id'];
1583
- $entity_id = $row['entity_id'];
1584
-
1585
- $path = $this->culc_path($parent_id, $entity_id);
1586
-
1587
- //echo("\n$path\n");
1588
-
1589
- $this->db_do("
1590
- UPDATE $catalog_category_entity
1591
- SET path = '$path'
1592
- WHERE entity_id = $entity_id");
1593
- }
1594
-
1595
- if (UPDATE_CATEGORY_DATA) {
1596
- echo "Update category_data \n";
1597
-
1598
- $q = "
1599
- INSERT INTO $catalog_category_entity_varchar
1600
- (
1601
- entity_type_id,
1602
- attribute_id,
1603
- store_id,
1604
- entity_id,
1605
- value
1606
- )
1607
- (SELECT
1608
- $_categoryEntityTypeId,
1609
- $name_attrid,
1610
- 0,
1611
- scm.shop_entity_id,
1612
- c.category_name
1613
- FROM $categories_temp c
1614
- JOIN $stINch_categories_mapping scm
1615
- ON c.store_category_id = scm.store_category_id
1616
- )
1617
- ON DUPLICATE KEY UPDATE
1618
- value = c.category_name";
1619
- $this->db_do($q);
1620
-
1621
- $q = "
1622
- INSERT INTO $catalog_category_entity_varchar
1623
- (
1624
- entity_type_id,
1625
- attribute_id,
1626
- store_id,
1627
- entity_id,
1628
- value
1629
- )
1630
- (SELECT
1631
- $_categoryEntityTypeId,
1632
- $name_attrid,
1633
- 1,
1634
- scm.shop_entity_id,
1635
- c.category_name
1636
- FROM $categories_temp c
1637
- JOIN $stINch_categories_mapping scm
1638
- ON c.store_category_id = scm.store_category_id
1639
- )
1640
- ON DUPLICATE KEY UPDATE
1641
- value = c.category_name";
1642
- $this->db_do($q);
1643
-
1644
- $q = "
1645
- INSERT INTO $catalog_category_entity
1646
- (
1647
- entity_type_id,
1648
- attribute_id,
1649
- store_id,
1650
- entity_id,
1651
- value
1652
- )
1653
- (SELECT
1654
- $_categoryEntityTypeId,
1655
- $attr_is_active,
1656
- 0,
1657
- scm.shop_entity_id,
1658
- 1
1659
- FROM $categories_temp c
1660
- JOIN $stINch_categories_mapping scm
1661
- ON c.store_category_id = scm.store_category_id
1662
- )
1663
- ON DUPLICATE KEY UPDATE
1664
- value = 1";
1665
- $this->db_do($q);
1666
-
1667
- $q = "
1668
- INSERT INTO $catalog_category_entity_int
1669
- (
1670
- entity_type_id,
1671
- attribute_id,
1672
- store_id,
1673
- entity_id,
1674
- value
1675
- )
1676
- (SELECT
1677
- $_categoryEntityTypeId,
1678
- $attr_is_active,
1679
- 1,
1680
- scm.shop_entity_id,
1681
- 1
1682
- FROM $categories_temp c
1683
- JOIN $stINch_categories_mapping scm
1684
- ON c.store_category_id = scm.store_category_id
1685
- )
1686
- ON DUPLICATE KEY UPDATE
1687
- value = 1";
1688
- $this->db_do($q);
1689
-
1690
- $q = "
1691
- INSERT INTO $catalog_category_entity_int
1692
- (
1693
- entity_type_id,
1694
- attribute_id,
1695
- store_id,
1696
- entity_id,
1697
- value
1698
- )
1699
- (SELECT
1700
- $_categoryEntityTypeId,
1701
- $attr_include_in_menu,
1702
- 0,
1703
- scm.shop_entity_id,
1704
- c.include_in_menu
1705
- FROM $categories_temp c
1706
- JOIN $stINch_categories_mapping scm
1707
- ON c.store_category_id = scm.store_category_id
1708
- )
1709
- ON DUPLICATE KEY UPDATE
1710
- value = c.include_in_menu";
1711
- $this->db_do($q);
1712
-
1713
- $q = "
1714
- INSERT INTO $catalog_category_entity_int
1715
- (
1716
- entity_type_id,
1717
- attribute_id,
1718
- store_id,
1719
- entity_id,
1720
- value
1721
- )
1722
- (SELECT
1723
- $_categoryEntityTypeId,
1724
- $is_anchor_attrid,
1725
- 1,
1726
- scm.shop_entity_id,
1727
- c.is_anchor
1728
- FROM $categories_temp c
1729
- JOIN $stINch_categories_mapping scm
1730
- ON c.store_category_id = scm.store_category_id
1731
- )
1732
- ON DUPLICATE KEY UPDATE
1733
- value = c.is_anchor";
1734
- $this->db_do($q);
1735
-
1736
- $q = "
1737
- INSERT INTO $catalog_category_entity_int
1738
- (
1739
- entity_type_id,
1740
- attribute_id,
1741
- store_id,
1742
- entity_id,
1743
- value
1744
- )
1745
- (SELECT
1746
- $_categoryEntityTypeId,
1747
- $is_anchor_attrid,
1748
- 0,
1749
- scm.shop_entity_id,
1750
- c.is_anchor
1751
- FROM $categories_temp c
1752
- JOIN $stINch_categories_mapping scm
1753
- ON c.store_category_id = scm.store_category_id
1754
- )
1755
- ON DUPLICATE KEY UPDATE
1756
- value = c.is_anchor";
1757
- $this->db_do($q);
1758
-
1759
- $q = "
1760
- INSERT INTO $catalog_category_entity_varchar
1761
- (
1762
- entity_type_id,
1763
- attribute_id,
1764
- store_id,
1765
- entity_id,
1766
- value
1767
- )
1768
- (SELECT
1769
- $_categoryEntityTypeId,
1770
- $image_attrid,
1771
- 0,
1772
- scm.shop_entity_id,
1773
- c.categories_image
1774
- FROM $categories_temp c
1775
- JOIN $stINch_categories_mapping scm
1776
- ON c.store_category_id = scm.store_category_id
1777
- )
1778
- ON DUPLICATE KEY UPDATE
1779
- value = c.categories_image";
1780
- $this->db_do($q);
1781
- //STP
1782
- $q = "
1783
- INSERT INTO $catalog_category_entity_varchar
1784
- (
1785
- entity_type_id,
1786
- attribute_id,
1787
- store_id,
1788
- entity_id,
1789
- value
1790
- )
1791
- (SELECT
1792
- $this->_categoryEntityTypeId,
1793
- $this->_categoryMetaTitleAttrId,
1794
- 0,
1795
- scm.shop_entity_id,
1796
- c.MetaTitle
1797
- FROM $categories_temp c
1798
- JOIN $stINch_categories_mapping scm
1799
- ON c.store_category_id = scm.store_category_id
1800
- )
1801
- ON DUPLICATE KEY UPDATE
1802
- value = c.MetaTitle";
1803
- $this->db_do($q);
1804
-
1805
- $q = "
1806
- INSERT INTO $catalog_category_entity_varchar
1807
- (
1808
- entity_type_id,
1809
- attribute_id,
1810
- store_id,
1811
- entity_id,
1812
- value
1813
- )
1814
- (SELECT
1815
- $this->_categoryEntityTypeId,
1816
- $this->_categoryMetadescriptionAttrId,
1817
- 0,
1818
- scm.shop_entity_id,
1819
- c.MetaDescription
1820
- FROM $categories_temp c
1821
- JOIN $stINch_categories_mapping scm
1822
- ON c.store_category_id = scm.store_category_id
1823
- )
1824
- ON DUPLICATE KEY UPDATE
1825
- value = c.MetaDescription";
1826
- $this->db_do($q);
1827
-
1828
- $q = "
1829
- INSERT INTO $catalog_category_entity_varchar
1830
- (
1831
- entity_type_id,
1832
- attribute_id,
1833
- store_id,
1834
- entity_id,
1835
- value
1836
- )
1837
- (SELECT
1838
- $this->_categoryEntityTypeId,
1839
- $this->_categoryDescriptionAttrId,
1840
- 0,
1841
- scm.shop_entity_id,
1842
- c.Description
1843
- FROM $categories_temp c
1844
- JOIN $stINch_categories_mapping scm
1845
- ON c.store_category_id = scm.store_category_id
1846
- )
1847
- ON DUPLICATE KEY UPDATE
1848
- value = c.Description";
1849
- $this->db_do($q);
1850
-
1851
-
1852
- //stp
1853
- } else {
1854
- echo "Insert ignore category_data \n";
1855
-
1856
- $q = "
1857
- INSERT IGNORE INTO $catalog_category_entity_varchar
1858
- (
1859
- entity_type_id,
1860
- attribute_id,
1861
- store_id,
1862
- entity_id,
1863
- value
1864
- )
1865
- (SELECT
1866
- $_categoryEntityTypeId,
1867
- $name_attrid,
1868
- 0,
1869
- scm.shop_entity_id,
1870
- c.category_name
1871
- FROM $categories_temp c
1872
- JOIN $stINch_categories_mapping scm
1873
- ON c.store_category_id = scm.store_category_id
1874
- )";
1875
- $this->db_do($q);
1876
-
1877
- $q = "
1878
- INSERT IGNORE INTO $catalog_category_entity_int
1879
- (
1880
- entity_type_id,
1881
- attribute_id,
1882
- store_id,
1883
- entity_id,
1884
- value
1885
- )
1886
- (SELECT
1887
- $_categoryEntityTypeId,
1888
- $attr_is_active,
1889
- 0,
1890
- scm.shop_entity_id,
1891
- 1
1892
- FROM $categories_temp c
1893
- JOIN $stINch_categories_mapping scm
1894
- ON c.store_category_id = scm.store_category_id
1895
- )";
1896
- $this->db_do($q);
1897
-
1898
- $q = "
1899
- INSERT IGNORE INTO $catalog_category_entity_int
1900
- (
1901
- entity_type_id,
1902
- attribute_id,
1903
- store_id,
1904
- entity_id,
1905
- value
1906
- )
1907
- (SELECT
1908
- $_categoryEntityTypeId,
1909
- $attr_include_in_menu,
1910
- 0,
1911
- scm.shop_entity_id,
1912
- c.include_in_menu
1913
- FROM $categories_temp c
1914
- JOIN $stINch_categories_mapping scm
1915
- ON c.store_category_id = scm.store_category_id
1916
- )";
1917
- $this->db_do($q);
1918
-
1919
- $q = "
1920
- INSERT IGNORE INTO $catalog_category_entity_int
1921
- (
1922
- entity_type_id,
1923
- attribute_id,
1924
- store_id,
1925
- entity_id,
1926
- value
1927
- )
1928
- (SELECT
1929
- $_categoryEntityTypeId,
1930
- $is_anchor_attrid,
1931
- 0,
1932
- scm.shop_entity_id,
1933
- c.is_anchor
1934
- FROM $categories_temp c
1935
- JOIN $stINch_categories_mapping scm
1936
- ON c.store_category_id = scm.store_category_id
1937
- )";
1938
- $this->db_do($q);
1939
-
1940
- $q = "
1941
- INSERT IGNORE INTO $catalog_category_entity_varchar
1942
- (
1943
- entity_type_id,
1944
- attribute_id,
1945
- store_id,
1946
- entity_id,
1947
- value
1948
- )
1949
- (SELECT
1950
- $_categoryEntityTypeId,
1951
- $image_attrid,
1952
- 0,
1953
- scm.shop_entity_id,
1954
- c.categories_image
1955
- FROM $categories_temp c
1956
- JOIN $stINch_categories_mapping scm
1957
- ON c.store_category_id = scm.store_category_id
1958
- )";
1959
- $this->db_do($q);
1960
- //STP
1961
- $q = "
1962
- INSERT IGNORE INTO $catalog_category_entity_varchar
1963
- (
1964
- entity_type_id,
1965
- attribute_id,
1966
- store_id,
1967
- entity_id,
1968
- value
1969
- )
1970
- (SELECT
1971
- $this->_categoryEntityTypeId,
1972
- $this->_categoryMetaTitleAttrId,
1973
- 0,
1974
- scm.shop_entity_id,
1975
- c.MetaTitle
1976
- FROM $categories_temp c
1977
- JOIN $stINch_categories_mapping scm
1978
- ON c.store_category_id = scm.store_category_id
1979
- )
1980
- ";
1981
- $this->db_do($q);
1982
-
1983
- $q = "
1984
- INSERT IGNORE INTO $catalog_category_entity_varchar
1985
- (
1986
- entity_type_id,
1987
- attribute_id,
1988
- store_id,
1989
- entity_id,
1990
- value
1991
- )
1992
- (SELECT
1993
- $this->_categoryEntityTypeId,
1994
- $this->_categoryMetadescriptionAttrId,
1995
- 0,
1996
- scm.shop_entity_id,
1997
- c.MetaDescription
1998
- FROM $categories_temp c
1999
- JOIN $stINch_categories_mapping scm
2000
- ON c.store_category_id = scm.store_category_id
2001
- )
2002
- ";
2003
- $this->db_do($q);
2004
-
2005
- $q = "
2006
- INSERT IGNORE INTO $catalog_category_entity_varchar
2007
- (
2008
- entity_type_id,
2009
- attribute_id,
2010
- store_id,
2011
- entity_id,
2012
- value
2013
- )
2014
- (SELECT
2015
- $this->_categoryEntityTypeId,
2016
- $this->_categoryDescriptionAttrId,
2017
- 0,
2018
- scm.shop_entity_id,
2019
- c.Description
2020
- FROM $categories_temp c
2021
- JOIN $stINch_categories_mapping scm
2022
- ON c.store_category_id = scm.store_category_id
2023
- )
2024
- ";
2025
- $this->db_do($q);
2026
-
2027
-
2028
- //stp
2029
-
2030
- }
2031
-
2032
- $this->delete_old_sinch_categories_from_shop();
2033
-
2034
- $this->db_do("DROP TABLE IF EXISTS $stINch_categories");
2035
- $this->db_do("RENAME TABLE $categories_temp TO $stINch_categories");
2036
- }
2037
-
2038
-
2039
- #################################################################################################
2040
-
2041
- function culc_path($parent_id, $ent_id)
2042
- {
2043
-
2044
- //echo("\nparent_id = [$parent_id] ent_id = [$ent_id]\n");
2045
-
2046
- $path = '';
2047
- $cat_id = $parent_id;
2048
- $q = "SELECT
2049
- parent_id
2050
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
2051
- WHERE entity_id=" . $cat_id;
2052
- $quer = $this->db_do($q);
2053
- $row = mysqli_fetch_array($quer);
2054
- while ($row['parent_id']) {
2055
- $path = $row['parent_id'] . '/' . $path;
2056
- $q = "SELECT
2057
- parent_id
2058
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
2059
- WHERE entity_id=" . $row['parent_id'];
2060
- $quer = $this->db_do($q);
2061
- $row = mysqli_fetch_array($quer);
2062
-
2063
- }
2064
- if ($cat_id) {
2065
- $path .= $cat_id . "/";
2066
- }
2067
-
2068
- if ($path) {
2069
- return ($path . $ent_id);
2070
- } else {
2071
- return ($ent_id);
2072
- }
2073
-
2074
- }
2075
-
2076
-
2077
- #################################################################################################
2078
-
2079
- private function delete_old_sinch_categories_from_shop()
2080
- {
2081
-
2082
- $q = "DELETE cat FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar') . " cat
2083
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
2084
- ON cat.entity_id=scm.shop_entity_id
2085
- WHERE
2086
- (scm.shop_store_category_id is not null) AND
2087
- (scm.store_category_id is null)";
2088
- $this->db_do($q);
2089
-
2090
- $q = "DELETE cat FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_int') . " cat
2091
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
2092
- ON cat.entity_id=scm.shop_entity_id
2093
- WHERE
2094
- (scm.shop_store_category_id is not null) AND
2095
- (scm.store_category_id is null)";
2096
- $this->db_do($q);
2097
-
2098
- $q = "DELETE cat FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cat
2099
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
2100
- ON cat.entity_id=scm.shop_entity_id
2101
- WHERE
2102
- (scm.shop_store_category_id is not null) AND
2103
- (scm.store_category_id is null)";
2104
- $this->db_do($q);
2105
-
2106
- }
2107
-
2108
- #################################################################################################
2109
-
2110
- private function rewriteMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2111
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2112
- $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
2113
- $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp)
2114
- {
2115
- echo("rewriteMultistoreCategories RUN\n");
2116
-
2117
-
2118
- echo(" truncateAllCateriesAndCreateRoot start...");
2119
- $this->truncateAllCateriesAndCreateRoot($catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2120
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active);
2121
- echo(" done.\n");
2122
-
2123
-
2124
- echo(" createDefaultCategories start...");
2125
- $this->createDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2126
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu);
2127
- echo(" done.\n");
2128
-
2129
-
2130
- echo(" mapSinchCategoriesMultistore start...");
2131
- $this->mapSinchCategoriesMultistore($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid);
2132
- echo(" done.\n");
2133
-
2134
-
2135
- echo(" addCategoryDataMultistore start...");
2136
- $this->addCategoryDataMultistore($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2137
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2138
- $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid);
2139
- echo(" done.\n");
2140
-
2141
-
2142
- echo("rewriteMultistoreCategories DONE\n");
2143
- }
2144
-
2145
- #################################################################################################
2146
-
2147
- private function truncateAllCateriesAndCreateRoot($catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2148
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active)
2149
- {
2150
- $this->db_do('SET foreign_key_checks=0');
2151
-
2152
-
2153
- $this->db_do("TRUNCATE $catalog_category_entity");
2154
- $this->db_do("TRUNCATE $catalog_category_entity_varchar");
2155
- $this->db_do("TRUNCATE $catalog_category_entity_int");
2156
-
2157
-
2158
- $this->db_do("INSERT $catalog_category_entity
2159
- (entity_id, entity_type_id, attribute_set_id, parent_id, created_at, updated_at,
2160
- path, position, level, children_count, store_category_id, parent_store_category_id)
2161
- VALUES
2162
- (1, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 0, '0000-00-00 00:00:00', NOW(), '1', 0, 0, 1, NULL, NULL)");
2163
-
2164
-
2165
- $this->db_do("INSERT $catalog_category_entity_varchar
2166
- (value_id, entity_type_id, attribute_id, store_id, entity_id, value)
2167
- VALUES
2168
- (1, $_categoryEntityTypeId, $name_attrid, 0, 1, 'Root Catalog'),
2169
- (2, $_categoryEntityTypeId, $name_attrid, 1, 1, 'Root Catalog'),
2170
- (3, $_categoryEntityTypeId, $attr_url_key, 0, 1, 'root-catalog')");
2171
-
2172
-
2173
- $this->db_do("INSERT $catalog_category_entity_int
2174
- (value_id, entity_type_id, attribute_id, store_id, entity_id, value)
2175
- VALUES
2176
- (1, $_categoryEntityTypeId, $attr_include_in_menu, 0, 1, 1)");
2177
- }
2178
-
2179
- #################################################################################################
2180
-
2181
- private function createDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2182
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu)
2183
- {
2184
- $i = 3; // 2 - is Default Category... not use.
2185
-
2186
- foreach ($coincidence as $key => $item) {
2187
- $this->db_do("INSERT $catalog_category_entity
2188
- (entity_id, entity_type_id, attribute_set_id, parent_id, created_at, updated_at,
2189
- path, position, level, children_count, store_category_id, parent_store_category_id)
2190
- VALUES
2191
- ($i, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 1, now(), now(), '1/$i', 1, 1, 1, NULL, NULL)");
2192
-
2193
-
2194
- $this->db_do("INSERT $catalog_category_entity_varchar
2195
- (entity_type_id, attribute_id, store_id, entity_id, value)
2196
- VALUES
2197
- ($_categoryEntityTypeId, $name_attrid, 0, $i, '$key'),
2198
- ($_categoryEntityTypeId, $name_attrid, 1, $i, '$key'),
2199
- ($_categoryEntityTypeId, $attr_display_mode, 1, $i, '$key'),
2200
- ($_categoryEntityTypeId, $attr_url_key, 0, $i, '$key')");
2201
-
2202
-
2203
- $this->db_do("INSERT $catalog_category_entity_int
2204
- (entity_type_id, attribute_id, store_id, entity_id, value)
2205
- VALUES
2206
- ($_categoryEntityTypeId, $attr_is_active, 0, $i, 1),
2207
- ($_categoryEntityTypeId, $attr_is_active, 1, $i, 1),
2208
- ($_categoryEntityTypeId, $attr_include_in_menu, 0, $i, 1),
2209
- ($_categoryEntityTypeId, $attr_include_in_menu, 1, $i, 1)");
2210
- $i++;
2211
- } // foreach($coincidence as $key => $item)
2212
- }
2213
-
2214
- #################################################################################################
2215
-
2216
- private function mapSinchCategoriesMultistore($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid, $mapping_again = false)
2217
- {
2218
- echo("\n\n\n\n==========================================================================\nmapSinchCategoriesMultistore start... \n");
2219
-
2220
- $this->createMappingSinchTables($stINch_categories_mapping_temp, $stINch_categories_mapping);
2221
-
2222
- // backup Category ID in REWRITE mode
2223
- if ($im_type == "REWRITE" || (UPDATE_CATEGORY_DATA && $im_type == "MERGE")) {
2224
- if ($mapping_again) {
2225
- $query = "
2226
- INSERT IGNORE INTO $stINch_categories_mapping_temp
2227
- (
2228
- shop_entity_id,
2229
- shop_entity_type_id,
2230
- shop_attribute_set_id,
2231
- shop_parent_id,
2232
- shop_store_category_id,
2233
- shop_parent_store_category_id
2234
- )
2235
- (SELECT
2236
- entity_id,
2237
- entity_type_id,
2238
- attribute_set_id,
2239
- parent_id,
2240
- store_category_id,
2241
- parent_store_category_id
2242
- FROM $catalog_category_entity)";
2243
- echo("\n\n$query\n\n");
2244
- $this->db_do($query);
2245
-
2246
-
2247
- $query = "
2248
- UPDATE $stINch_categories_mapping_temp cmt
2249
- JOIN $categories_temp c
2250
- ON cmt.shop_store_category_id = c.store_category_id
2251
- SET
2252
- cmt.store_category_id = c.store_category_id,
2253
- cmt.parent_store_category_id = c.parent_store_category_id,
2254
- cmt.category_name = c.category_name,
2255
- cmt.order_number = c.order_number,
2256
- cmt.products_within_this_category = c.products_within_this_category";
2257
- echo("\n\n$query\n\n");
2258
- $this->db_do($query);
2259
-
2260
-
2261
- $query = "
2262
- UPDATE $stINch_categories_mapping_temp cmt
2263
- JOIN $catalog_category_entity cce
2264
- ON cmt.parent_store_category_id = cce.store_category_id
2265
- SET cmt.shop_parent_id = cce.entity_id";
2266
- echo("\n\n$query\n\n");
2267
- $this->db_do($query);
2268
-
2269
-
2270
- $query = "
2271
- SELECT DISTINCT
2272
- c.RootName, cce.entity_id
2273
- FROM $categories_temp c
2274
- JOIN $catalog_category_entity_varchar ccev
2275
- ON c.RootName = ccev.value
2276
- AND ccev.entity_type_id = $_categoryEntityTypeId
2277
- AND ccev.attribute_id = $name_attrid
2278
- AND ccev.store_id = 0
2279
- JOIN $catalog_category_entity cce
2280
- ON ccev.entity_id = cce.entity_id";
2281
- echo("\n\n$query\n\n");
2282
- $root_categories = $this->db_do($query);
2283
-
2284
- while ($root_cat = mysqli_fetch_array($root_categories)) {
2285
- $root_id = $root_cat['entity_id'];
2286
- $root_name = $root_cat['RootName'];
2287
-
2288
- $query = "
2289
- UPDATE $stINch_categories_mapping_temp cmt
2290
- JOIN $categories_temp c
2291
- ON cmt.shop_store_category_id = c.store_category_id
2292
- SET
2293
- cmt.shop_parent_id = $root_id,
2294
- cmt.shop_parent_store_category_id = $root_id,
2295
- cmt.parent_store_category_id = $root_id,
2296
- c.parent_store_category_id = $root_id
2297
- WHERE RootName = '$root_name'
2298
- AND cmt.shop_parent_id = 0";
2299
- echo("\n\n$query\n\n");
2300
- $this->db_do($query);
2301
- }
2302
- } else {
2303
- $catalog_category_entity_backup = Mage::getSingleton('core/resource')->getTableName('sinch_category_backup');
2304
- if (!$this->_checkCategoryBackupExist($catalog_category_entity_backup)) {
2305
- $catalog_category_entity_backup = $catalog_category_entity;
2306
- }
2307
- $query = "
2308
- INSERT IGNORE INTO $stINch_categories_mapping_temp
2309
- (
2310
- shop_entity_id,
2311
- shop_entity_type_id,
2312
- shop_attribute_set_id,
2313
- shop_parent_id,
2314
- shop_store_category_id,
2315
- shop_parent_store_category_id
2316
- )
2317
- (SELECT
2318
- entity_id,
2319
- entity_type_id,
2320
- attribute_set_id,
2321
- parent_id,
2322
- store_category_id,
2323
- parent_store_category_id
2324
- FROM $catalog_category_entity_backup)";
2325
- echo("\n\n$query\n\n");
2326
- $this->db_do($query);
2327
-
2328
-
2329
- $query = "
2330
- UPDATE $stINch_categories_mapping_temp cmt
2331
- JOIN $categories_temp c
2332
- ON cmt.shop_store_category_id = c.store_category_id
2333
- SET
2334
- cmt.store_category_id = c.store_category_id,
2335
- cmt.parent_store_category_id = c.parent_store_category_id,
2336
- cmt.category_name = c.category_name,
2337
- cmt.order_number = c.order_number,
2338
- cmt.products_within_this_category = c.products_within_this_category";
2339
- echo("\n\n$query\n\n");
2340
- $this->db_do($query);
2341
-
2342
-
2343
- $query = "
2344
- UPDATE $stINch_categories_mapping_temp cmt
2345
- JOIN $catalog_category_entity_backup cce
2346
- ON cmt.parent_store_category_id = cce.store_category_id
2347
- SET cmt.shop_parent_id = cce.entity_id";
2348
- echo("\n\n$query\n\n");
2349
- $this->db_do($query);
2350
-
2351
-
2352
- $query = "
2353
- SELECT DISTINCT
2354
- c.RootName, cce.entity_id
2355
- FROM $categories_temp c
2356
- JOIN $catalog_category_entity_varchar ccev
2357
- ON c.RootName = ccev.value
2358
- AND ccev.entity_type_id = $_categoryEntityTypeId
2359
- AND ccev.attribute_id = $name_attrid
2360
- AND ccev.store_id = 0
2361
- JOIN $catalog_category_entity_backup cce
2362
- ON ccev.entity_id = cce.entity_id";
2363
- echo("\n\n$query\n\n");
2364
- $root_categories = $this->db_do($query);
2365
-
2366
- while ($root_cat = mysqli_fetch_array($root_categories)) {
2367
- $root_id = $root_cat['entity_id'];
2368
- $root_name = $root_cat['RootName'];
2369
-
2370
- $query = "
2371
- UPDATE $stINch_categories_mapping_temp cmt
2372
- JOIN $categories_temp c
2373
- ON cmt.shop_store_category_id = c.store_category_id
2374
- SET
2375
- cmt.shop_parent_id = $root_id,
2376
- cmt.shop_parent_store_category_id = $root_id,
2377
- cmt.parent_store_category_id = $root_id,
2378
- c.parent_store_category_id = $root_id
2379
- WHERE RootName = '$root_name'
2380
- AND cmt.shop_parent_id = 0";
2381
- echo("\n\n$query\n\n");
2382
- $this->db_do($query);
2383
- }
2384
- }
2385
- // (end) backup Category ID in REWRITE mode
2386
- } else {
2387
- $query = "
2388
- INSERT IGNORE INTO $stINch_categories_mapping_temp
2389
- (shop_entity_id, shop_entity_type_id, shop_attribute_set_id, shop_parent_id, shop_store_category_id, shop_parent_store_category_id)
2390
- (SELECT entity_id, entity_type_id, attribute_set_id, parent_id, store_category_id, parent_store_category_id
2391
- FROM $catalog_category_entity)";
2392
- $this->db_do($query);
2393
-
2394
- $query = "
2395
- UPDATE $stINch_categories_mapping_temp cmt
2396
- JOIN $categories_temp c
2397
- ON cmt.shop_store_category_id = c.store_category_id
2398
- SET
2399
- cmt.store_category_id = c.store_category_id,
2400
- cmt.parent_store_category_id = c.parent_store_category_id,
2401
- cmt.category_name = c.category_name,
2402
- cmt.order_number = c.order_number,
2403
- cmt.products_within_this_category = c.products_within_this_category";
2404
- echo("\n\n$query\n\n");
2405
- $this->db_do($query);
2406
-
2407
-
2408
- $query = "
2409
- UPDATE $stINch_categories_mapping_temp cmt
2410
- JOIN $catalog_category_entity cce
2411
- ON cmt.parent_store_category_id = cce.store_category_id
2412
- SET cmt.shop_parent_id = cce.entity_id";
2413
- echo("\n\n$query\n\n");
2414
- $this->db_do($query);
2415
-
2416
-
2417
- $query = "
2418
- SELECT DISTINCT
2419
- c.RootName, cce.entity_id
2420
- FROM $categories_temp c
2421
- JOIN $catalog_category_entity_varchar ccev
2422
- ON c.RootName = ccev.value
2423
- AND ccev.entity_type_id = $_categoryEntityTypeId
2424
- AND ccev.attribute_id = $name_attrid
2425
- AND ccev.store_id = 0
2426
- JOIN $catalog_category_entity cce
2427
- ON ccev.entity_id = cce.entity_id";
2428
- echo("\n\n$query\n\n");
2429
- $root_categories = $this->db_do($query);
2430
-
2431
- while ($root_cat = mysqli_fetch_array($root_categories)) {
2432
- $root_id = $root_cat['entity_id'];
2433
- $root_name = $root_cat['RootName'];
2434
-
2435
- $query = "
2436
- UPDATE $stINch_categories_mapping_temp cmt
2437
- JOIN $categories_temp c
2438
- ON cmt.shop_store_category_id = c.store_category_id
2439
- SET
2440
- cmt.shop_parent_id = $root_id,
2441
- cmt.shop_parent_store_category_id = $root_id,
2442
- cmt.parent_store_category_id = $root_id,
2443
- c.parent_store_category_id = $root_id
2444
- WHERE RootName = '$root_name'
2445
- AND cmt.shop_parent_id = 0";
2446
- echo("\n\n$query\n\n");
2447
- $this->db_do($query);
2448
- }
2449
- }
2450
-
2451
- // added for mapping new sinch categories in merge && !UPDATE_CATEGORY_DATA mode
2452
- if ((UPDATE_CATEGORY_DATA && $im_type == "MERGE") || ($im_type == "REWRITE")) $where = '';
2453
- else $where = 'WHERE cce.parent_id = 0 AND cce.store_category_id IS NOT NULL';
2454
-
2455
- $query = "
2456
- UPDATE $stINch_categories_mapping_temp cmt
2457
- JOIN $catalog_category_entity cce
2458
- ON cmt.shop_entity_id = cce.entity_id
2459
- SET cce.parent_id = cmt.shop_parent_id
2460
- $where";
2461
- echo("\n\n$query\n\n");
2462
- $this->db_do($query);
2463
- $this->_LOG("Execute function mapSinchCategoriesMultistore");
2464
- $query = "DROP TABLE IF EXISTS $stINch_categories_mapping";
2465
- echo("\n\n$query\n\n");
2466
- $this->db_do($query);
2467
-
2468
- $query = "RENAME TABLE $stINch_categories_mapping_temp TO $stINch_categories_mapping";
2469
- echo("\n\n$query\n\n");
2470
- $this->db_do($query);
2471
-
2472
- echo("\nmapSinchCategoriesMultistore done... \n==========================================================================\n\n\n\n");
2473
- }
2474
-
2475
- #################################################################################################
2476
-
2477
- private function createMappingSinchTables($stINch_categories_mapping_temp, $stINch_categories_mapping)
2478
- {
2479
- $this->db_do("DROP TABLE IF EXISTS $stINch_categories_mapping_temp");
2480
- $this->db_do("
2481
- CREATE TABLE $stINch_categories_mapping_temp
2482
- (
2483
- shop_entity_id INT(11) UNSIGNED NOT NULL,
2484
- shop_entity_type_id INT(11),
2485
- shop_attribute_set_id INT(11),
2486
- shop_parent_id INT(11),
2487
- shop_store_category_id INT(11),
2488
- shop_parent_store_category_id INT(11),
2489
- store_category_id INT(11),
2490
- parent_store_category_id INT(11),
2491
- category_name VARCHAR(255),
2492
- order_number INT(11),
2493
- products_within_this_category INT(11),
2494
-
2495
- KEY shop_entity_id (shop_entity_id),
2496
- KEY shop_parent_id (shop_parent_id),
2497
- KEY store_category_id (store_category_id),
2498
- KEY parent_store_category_id (parent_store_category_id),
2499
- UNIQUE KEY(shop_entity_id)
2500
- )");
2501
-
2502
-
2503
- $this->db_do("CREATE TABLE IF NOT EXISTS $stINch_categories_mapping LIKE $stINch_categories_mapping_temp");
2504
- }
2505
-
2506
- #################################################################################################
2507
-
2508
- private function addCategoryDataMultistore($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2509
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2510
- $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid)
2511
- {
2512
- echo("\n\n\n\n*************************************************************\nmapSinchCategoriesMultistore start... \n");
2513
- if (UPDATE_CATEGORY_DATA) {
2514
- $ignore = '';
2515
- $on_diplicate_key_update = "
2516
- ON DUPLICATE KEY UPDATE
2517
- updated_at = now(),
2518
- store_category_id = c.store_category_id,
2519
- level = c.level,
2520
- children_count = c.children_count,
2521
- position = c.order_number,
2522
- parent_store_category_id = c.parent_store_category_id";
2523
- //level=c.level,
2524
- //children_count=c.children_count
2525
- //position=c.order_number,
2526
- } else {
2527
- $ignore = 'IGNORE';
2528
- $on_diplicate_key_update = '';
2529
- }
2530
-
2531
- $query = "
2532
- INSERT $ignore INTO $catalog_category_entity
2533
- (
2534
- entity_type_id,
2535
- attribute_set_id,
2536
- created_at,
2537
- updated_at,
2538
- level,
2539
- children_count,
2540
- entity_id,
2541
- position,
2542
- parent_id,
2543
- store_category_id,
2544
- parent_store_category_id
2545
- )
2546
- (SELECT
2547
- $_categoryEntityTypeId,
2548
- $_categoryDefault_attribute_set_id,
2549
- NOW(),
2550
- NOW(),
2551
- c.level,
2552
- c.children_count,
2553
- scm.shop_entity_id,
2554
- c.order_number,
2555
- scm.shop_parent_id,
2556
- c.store_category_id,
2557
- c.parent_store_category_id
2558
- FROM $categories_temp c
2559
- LEFT JOIN $stINch_categories_mapping scm
2560
- ON c.store_category_id = scm.store_category_id
2561
- ) $on_diplicate_key_update";
2562
- echo("\n\n$query\n\n");
2563
- $this->db_do($query);
2564
-
2565
- //return; // !!!!!!!!!!!!!!!!!!!!!!!!!!!
2566
-
2567
-
2568
- $this->mapSinchCategoriesMultistore($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid, true);
2569
-
2570
-
2571
- $categories = $this->db_do("SELECT entity_id, parent_id FROM $catalog_category_entity ORDER BY parent_id");
2572
- while ($row = mysqli_fetch_array($categories)) {
2573
- $parent_id = $row['parent_id'];
2574
- $entity_id = $row['entity_id'];
2575
-
2576
- $path = $this->culcPathMultistore($parent_id, $entity_id, $catalog_category_entity);
2577
-
2578
- $this->db_do("
2579
- UPDATE $catalog_category_entity
2580
- SET path = '$path'
2581
- WHERE entity_id = $entity_id");
2582
- } // while ($row = mysqli_fetch_array($categories))
2583
-
2584
-
2585
- ///////////////////////////////////////////////////////
2586
-
2587
-
2588
- if (UPDATE_CATEGORY_DATA) {
2589
- echo "Update category_data \n";
2590
-
2591
- $q = "
2592
- INSERT INTO $catalog_category_entity_varchar
2593
- (
2594
- entity_type_id,
2595
- attribute_id,
2596
- store_id,
2597
- entity_id,
2598
- value
2599
- )
2600
- (SELECT
2601
- $_categoryEntityTypeId,
2602
- $name_attrid,
2603
- 0,
2604
- scm.shop_entity_id,
2605
- c.category_name
2606
- FROM $categories_temp c
2607
- JOIN $stINch_categories_mapping scm
2608
- ON c.store_category_id = scm.store_category_id
2609
- )
2610
- ON DUPLICATE KEY UPDATE
2611
- value = c.category_name";
2612
- $this->db_do($q);
2613
-
2614
-
2615
- $q = "
2616
- INSERT INTO $catalog_category_entity_varchar
2617
- (
2618
- entity_type_id,
2619
- attribute_id,
2620
- store_id,
2621
- entity_id,
2622
- value
2623
- )
2624
- (SELECT
2625
- $_categoryEntityTypeId,
2626
- $name_attrid,
2627
- 1,
2628
- scm.shop_entity_id,
2629
- c.category_name
2630
- FROM $categories_temp c
2631
- JOIN $stINch_categories_mapping scm
2632
- ON c.store_category_id = scm.store_category_id
2633
- )
2634
- ON DUPLICATE KEY UPDATE
2635
- value = c.category_name";
2636
- $this->db_do($q);
2637
-
2638
-
2639
- $q = "
2640
- INSERT INTO $catalog_category_entity
2641
- (
2642
- entity_type_id,
2643
- attribute_id,
2644
- store_id,
2645
- entity_id,
2646
- value
2647
- )
2648
- (SELECT
2649
- $_categoryEntityTypeId,
2650
- $attr_is_active,
2651
- 0,
2652
- scm.shop_entity_id,
2653
- 1
2654
- FROM $categories_temp c
2655
- JOIN $stINch_categories_mapping scm
2656
- ON c.store_category_id = scm.store_category_id
2657
- )
2658
- ON DUPLICATE KEY UPDATE
2659
- value = 1";
2660
- $this->db_do($q);
2661
-
2662
-
2663
- $q = "
2664
- INSERT INTO $catalog_category_entity_int
2665
- (
2666
- entity_type_id,
2667
- attribute_id,
2668
- store_id,
2669
- entity_id,
2670
- value
2671
- )
2672
- (SELECT
2673
- $_categoryEntityTypeId,
2674
- $attr_is_active,
2675
- 1,
2676
- scm.shop_entity_id,
2677
- 1
2678
- FROM $categories_temp c
2679
- JOIN $stINch_categories_mapping scm
2680
- ON c.store_category_id = scm.store_category_id
2681
- )
2682
- ON DUPLICATE KEY UPDATE
2683
- value = 1";
2684
- $this->db_do($q);
2685
-
2686
-
2687
- $q = "
2688
- INSERT INTO $catalog_category_entity_int
2689
- (
2690
- entity_type_id,
2691
- attribute_id,
2692
- store_id,
2693
- entity_id,
2694
- value
2695
- )
2696
- (SELECT
2697
- $_categoryEntityTypeId,
2698
- $attr_include_in_menu,
2699
- 0,
2700
- scm.shop_entity_id,
2701
- c.include_in_menu
2702
- FROM $categories_temp c
2703
- JOIN $stINch_categories_mapping scm
2704
- ON c.store_category_id = scm.store_category_id
2705
- )
2706
- ON DUPLICATE KEY UPDATE
2707
- value = c.include_in_menu";
2708
- $this->db_do($q);
2709
-
2710
-
2711
- $q = "
2712
- INSERT INTO $catalog_category_entity_int
2713
- (
2714
- entity_type_id,
2715
- attribute_id,
2716
- store_id,
2717
- entity_id,
2718
- value
2719
- )
2720
- (SELECT
2721
- $_categoryEntityTypeId,
2722
- $is_anchor_attrid,
2723
- 1,
2724
- scm.shop_entity_id,
2725
- c.is_anchor
2726
- FROM $categories_temp c
2727
- JOIN $stINch_categories_mapping scm
2728
- ON c.store_category_id = scm.store_category_id
2729
- )
2730
- ON DUPLICATE KEY UPDATE
2731
- value = c.is_anchor";
2732
- $this->db_do($q);
2733
-
2734
-
2735
- $q = "
2736
- INSERT INTO $catalog_category_entity_int
2737
- (
2738
- entity_type_id,
2739
- attribute_id,
2740
- store_id,
2741
- entity_id,
2742
- value
2743
- )
2744
- (SELECT
2745
- $_categoryEntityTypeId,
2746
- $is_anchor_attrid,
2747
- 0,
2748
- scm.shop_entity_id,
2749
- c.is_anchor
2750
- FROM $categories_temp c
2751
- JOIN $stINch_categories_mapping scm
2752
- ON c.store_category_id = scm.store_category_id
2753
- )
2754
- ON DUPLICATE KEY UPDATE
2755
- value = c.is_anchor";
2756
- $this->db_do($q);
2757
-
2758
- $q = "
2759
- INSERT INTO $catalog_category_entity_varchar
2760
- (
2761
- entity_type_id,
2762
- attribute_id,
2763
- store_id,
2764
- entity_id,
2765
- value
2766
- )
2767
- (SELECT
2768
- $_categoryEntityTypeId,
2769
- $image_attrid,
2770
- 0,
2771
- scm.shop_entity_id,
2772
- c.categories_image
2773
- FROM $categories_temp c
2774
- JOIN $stINch_categories_mapping scm
2775
- ON c.store_category_id = scm.store_category_id
2776
- )
2777
- ON DUPLICATE KEY UPDATE
2778
- value = c.categories_image";
2779
- $this->db_do($q);
2780
- //STP
2781
- $q = "
2782
- INSERT INTO $catalog_category_entity_varchar
2783
- (
2784
- entity_type_id,
2785
- attribute_id,
2786
- store_id,
2787
- entity_id,
2788
- value
2789
- )
2790
- (SELECT
2791
- $this->_categoryEntityTypeId,
2792
- $this->_categoryMetaTitleAttrId,
2793
- 0,
2794
- scm.shop_entity_id,
2795
- c.MetaTitle
2796
- FROM $categories_temp c
2797
- JOIN $stINch_categories_mapping scm
2798
- ON c.store_category_id = scm.store_category_id
2799
- )
2800
- ON DUPLICATE KEY UPDATE
2801
- value = c.MetaTitle";
2802
- $this->db_do($q);
2803
-
2804
- $q = "
2805
- INSERT INTO $catalog_category_entity_varchar
2806
- (
2807
- entity_type_id,
2808
- attribute_id,
2809
- store_id,
2810
- entity_id,
2811
- value
2812
- )
2813
- (SELECT
2814
- $this->_categoryEntityTypeId,
2815
- $this->_categoryMetadescriptionAttrId,
2816
- 0,
2817
- scm.shop_entity_id,
2818
- c.MetaDescription
2819
- FROM $categories_temp c
2820
- JOIN $stINch_categories_mapping scm
2821
- ON c.store_category_id = scm.store_category_id
2822
- )
2823
- ON DUPLICATE KEY UPDATE
2824
- value = c.MetaDescription";
2825
- $this->db_do($q);
2826
-
2827
- $q = "
2828
- INSERT INTO $catalog_category_entity_varchar
2829
- (
2830
- entity_type_id,
2831
- attribute_id,
2832
- store_id,
2833
- entity_id,
2834
- value
2835
- )
2836
- (SELECT
2837
- $this->_categoryEntityTypeId,
2838
- $this->_categoryDescriptionAttrId,
2839
- 0,
2840
- scm.shop_entity_id,
2841
- c.Description
2842
- FROM $categories_temp c
2843
- JOIN $stINch_categories_mapping scm
2844
- ON c.store_category_id = scm.store_category_id
2845
- )
2846
- ON DUPLICATE KEY UPDATE
2847
- value = c.Description";
2848
- $this->db_do($q);
2849
-
2850
-
2851
- //stp
2852
- } else {
2853
- echo "Insert ignore category_data \n";
2854
-
2855
- $q = "
2856
- INSERT IGNORE INTO $catalog_category_entity_varchar
2857
- (
2858
- entity_type_id,
2859
- attribute_id,
2860
- store_id,
2861
- entity_id,
2862
- value
2863
- )
2864
- (SELECT
2865
- $_categoryEntityTypeId,
2866
- $name_attrid,
2867
- 0,
2868
- scm.shop_entity_id,
2869
- c.category_name
2870
- FROM $categories_temp c
2871
- JOIN $stINch_categories_mapping scm
2872
- ON c.store_category_id = scm.store_category_id
2873
- )";
2874
- $this->db_do($q);
2875
-
2876
-
2877
- $q = "
2878
- INSERT IGNORE INTO $catalog_category_entity_int
2879
- (
2880
- entity_type_id,
2881
- attribute_id,
2882
- store_id,
2883
- entity_id,
2884
- value
2885
- )
2886
- (SELECT
2887
- $_categoryEntityTypeId,
2888
- $attr_is_active,
2889
- 0,
2890
- scm.shop_entity_id,
2891
- 1
2892
- FROM $categories_temp c
2893
- JOIN $stINch_categories_mapping scm
2894
- ON c.store_category_id = scm.store_category_id
2895
- )";
2896
- $this->db_do($q);
2897
-
2898
-
2899
- $q = "
2900
- INSERT IGNORE INTO $catalog_category_entity_int
2901
- (
2902
- entity_type_id,
2903
- attribute_id,
2904
- store_id,
2905
- entity_id,
2906
- value
2907
- )
2908
- (SELECT
2909
- $_categoryEntityTypeId,
2910
- $attr_include_in_menu,
2911
- 0,
2912
- scm.shop_entity_id,
2913
- c.include_in_menu
2914
- FROM $categories_temp c
2915
- JOIN $stINch_categories_mapping scm
2916
- ON c.store_category_id = scm.store_category_id
2917
- )";
2918
- $this->db_do($q);
2919
-
2920
-
2921
- $q = "
2922
- INSERT IGNORE INTO $catalog_category_entity_int
2923
- (
2924
- entity_type_id,
2925
- attribute_id,
2926
- store_id,
2927
- entity_id,
2928
- value
2929
- )
2930
- (SELECT
2931
- $_categoryEntityTypeId,
2932
- $is_anchor_attrid,
2933
- 0,
2934
- scm.shop_entity_id,
2935
- c.is_anchor
2936
- FROM $categories_temp c
2937
- JOIN $stINch_categories_mapping scm
2938
- ON c.store_category_id = scm.store_category_id
2939
- )";
2940
- $this->db_do($q);
2941
-
2942
-
2943
- $q = "
2944
- INSERT IGNORE INTO $catalog_category_entity_varchar
2945
- (
2946
- entity_type_id,
2947
- attribute_id,
2948
- store_id,
2949
- entity_id,
2950
- value
2951
- )
2952
- (SELECT
2953
- $_categoryEntityTypeId,
2954
- $image_attrid,
2955
- 0,
2956
- scm.shop_entity_id,
2957
- c.categories_image
2958
- FROM $categories_temp c
2959
- JOIN $stINch_categories_mapping scm
2960
- ON c.store_category_id = scm.store_category_id
2961
- )";
2962
- $this->db_do($q);
2963
- //STP
2964
- $q = "
2965
- INSERT IGNORE INTO $catalog_category_entity_varchar
2966
- (
2967
- entity_type_id,
2968
- attribute_id,
2969
- store_id,
2970
- entity_id,
2971
- value
2972
- )
2973
- (SELECT
2974
- $this->_categoryEntityTypeId,
2975
- $this->_categoryMetaTitleAttrId,
2976
- 0,
2977
- scm.shop_entity_id,
2978
- c.MetaTitle
2979
- FROM $categories_temp c
2980
- JOIN $stINch_categories_mapping scm
2981
- ON c.store_category_id = scm.store_category_id
2982
- )
2983
- ";
2984
- $this->db_do($q);
2985
-
2986
- $q = "
2987
- INSERT IGNORE INTO $catalog_category_entity_varchar
2988
- (
2989
- entity_type_id,
2990
- attribute_id,
2991
- store_id,
2992
- entity_id,
2993
- value
2994
- )
2995
- (SELECT
2996
- $this->_categoryEntityTypeId,
2997
- $this->_categoryMetadescriptionAttrId,
2998
- 0,
2999
- scm.shop_entity_id,
3000
- c.MetaDescription
3001
- FROM $categories_temp c
3002
- JOIN $stINch_categories_mapping scm
3003
- ON c.store_category_id = scm.store_category_id
3004
- )
3005
- ";
3006
- $this->db_do($q);
3007
-
3008
- $q = "
3009
- INSERT IGNORE INTO $catalog_category_entity_varchar
3010
- (
3011
- entity_type_id,
3012
- attribute_id,
3013
- store_id,
3014
- entity_id,
3015
- value
3016
- )
3017
- (SELECT
3018
- $this->_categoryEntityTypeId,
3019
- $this->_categoryDescriptionAttrId,
3020
- 0,
3021
- scm.shop_entity_id,
3022
- c.Description
3023
- FROM $categories_temp c
3024
- JOIN $stINch_categories_mapping scm
3025
- ON c.store_category_id = scm.store_category_id
3026
- )
3027
- ";
3028
- $this->db_do($q);
3029
-
3030
-
3031
- //stp
3032
-
3033
- }
3034
-
3035
- $this->delete_old_sinch_categories_from_shop();
3036
- $this->db_do("DROP TABLE IF EXISTS $stINch_categories\n\n");
3037
- $this->db_do("RENAME TABLE $categories_temp TO $stINch_categories");
3038
- }
3039
-
3040
- #################################################################################################
3041
-
3042
- function culcPathMultistore($parent_id, $ent_id, $catalog_category_entity)
3043
- {
3044
-
3045
- //echo("\nparent_id = [$parent_id] ent_id = [$ent_id]");
3046
-
3047
- $path = '';
3048
-
3049
- $cat_id = $parent_id;
3050
-
3051
- $q = "
3052
- SELECT
3053
- parent_id
3054
- FROM $catalog_category_entity
3055
- WHERE entity_id = $cat_id";
3056
- $quer = $this->db_do($q);
3057
- $row = mysqli_fetch_array($quer);
3058
- while ($row['parent_id']) {
3059
- $path = $row['parent_id'] . '/' . $path;
3060
- $parent_id = $row['parent_id'];
3061
-
3062
- $q = "
3063
- SELECT
3064
- parent_id
3065
- FROM $catalog_category_entity
3066
- WHERE entity_id = $parent_id";
3067
- $quer = $this->db_do($q);
3068
- $row = mysqli_fetch_array($quer);
3069
- }
3070
-
3071
- if ($cat_id) $path .= $cat_id . "/";
3072
-
3073
- if ($path) $path .= $ent_id;
3074
- else $path = $ent_id;
3075
-
3076
- //echo(" path = [$path]\n");
3077
-
3078
- return $path;
3079
- }
3080
-
3081
- #################################################################################################
3082
-
3083
- private function mergeMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
3084
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
3085
- $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
3086
- $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp)
3087
- {
3088
- echo("mergeMultistoreCategories RUN\n");
3089
-
3090
-
3091
- $this->createNewDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
3092
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu);
3093
-
3094
-
3095
- $this->mapSinchCategoriesMultistoreMerge($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid);
3096
-
3097
-
3098
- $this->addCategoryDataMultistoreMerge($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
3099
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
3100
- $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid);
3101
-
3102
-
3103
- echo("\n\n\nmergeMultistoreCategories DONE\n");
3104
- }
3105
-
3106
- #################################################################################################
3107
-
3108
- private function createNewDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
3109
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu)
3110
- {
3111
- echo("\n\n ==========================================================================\n createNewDefaultCategories start... \n");
3112
-
3113
- $old_cats = array();
3114
- $query = $this->db_do("
3115
- SELECT
3116
- cce.entity_id,
3117
- ccev.value AS category_name
3118
- FROM $catalog_category_entity cce
3119
- JOIN $catalog_category_entity_varchar ccev
3120
- ON cce.entity_id = ccev.entity_id
3121
- AND ccev.store_id = 0
3122
- AND cce.entity_type_id = ccev.entity_type_id
3123
- AND ccev.attribute_id = 41
3124
- WHERE parent_id = 1"); // 41 - category name
3125
- while ($row = mysqli_fetch_array($query)) $old_cats[] = $row['category_name'];
3126
-
3127
- //var_dump($old_cats);
3128
-
3129
-
3130
- $query = $this->db_do("SELECT MAX(entity_id) AS max_entity_id FROM $catalog_category_entity");
3131
- $max_entity_id = mysqli_fetch_array($query);
3132
-
3133
- //var_dump($max_entity_id);
3134
-
3135
- $i = $max_entity_id[max_entity_id] + 1;
3136
-
3137
- foreach ($coincidence as $key => $item) {
3138
- echo("\n coincidence: key = [$key]\n");
3139
-
3140
-
3141
- /**if ($item)
3142
- * {
3143
- * echo(">>>>>>>>>>>>>>>>>>>>>>>>>>>> CONTINUE: key = [$key] item = [$item]\n");
3144
- * //continue;
3145
- * }
3146
- * else
3147
- * {
3148
- * echo(">>>>>>>>>>>>>>>>>>>>>>>>>>>> NOT CONTINUE: key = [$key] item = [$item]\n");
3149
- * }/**/
3150
-
3151
-
3152
- if (in_array($key, $old_cats)) {
3153
- echo(" CONTINUE: key = [$key] item = [$item]\n");
3154
- continue;
3155
- } else {
3156
- echo(" CREATE NEW CATEGORY: key = [$key] item = [$item]\n");
3157
- }
3158
-
3159
-
3160
- $this->db_do("INSERT $catalog_category_entity
3161
- (entity_id, entity_type_id, attribute_set_id, parent_id, created_at, updated_at,
3162
- path, position, level, children_count, store_category_id, parent_store_category_id)
3163
- VALUES
3164
- ($i, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 1, now(), now(), '1/$i', 1, 1, 1, NULL, NULL)");
3165
-
3166
-
3167
- $this->db_do("INSERT $catalog_category_entity_varchar
3168
- (entity_type_id, attribute_id, store_id, entity_id, value)
3169
- VALUES
3170
- ($_categoryEntityTypeId, $name_attrid, 0, $i, '$key'),
3171
- ($_categoryEntityTypeId, $name_attrid, 1, $i, '$key'),
3172
- ($_categoryEntityTypeId, $attr_display_mode, 1, $i, '$key'),
3173
- ($_categoryEntityTypeId, $attr_url_key, 0, $i, '$key')");
3174
-
3175
-
3176
- $this->db_do("INSERT $catalog_category_entity_int
3177
- (entity_type_id, attribute_id, store_id, entity_id, value)
3178
- VALUES
3179
- ($_categoryEntityTypeId, $attr_is_active, 0, $i, 1),
3180
- ($_categoryEntityTypeId, $attr_is_active, 1, $i, 1),
3181
- ($_categoryEntityTypeId, $attr_include_in_menu, 0, $i, 1),
3182
- ($_categoryEntityTypeId, $attr_include_in_menu, 1, $i, 1)");
3183
- $i++;
3184
- } // foreach($coincidence as $key => $item)
3185
-
3186
- echo("\n createNewDefaultCategories done... \n ==========================================================================\n");
3187
-
3188
- }
3189
-
3190
- #################################################################################################
3191
-
3192
- private function mapSinchCategoriesMultistoreMerge($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid)
3193
- {
3194
- echo("\n\n\ ==========================================================================\n mapSinchCategoriesMultistore start... \n");
3195
-
3196
- $this->createMappingSinchTables($stINch_categories_mapping_temp, $stINch_categories_mapping);
3197
-
3198
- $query = "
3199
- INSERT IGNORE INTO $stINch_categories_mapping_temp
3200
- (shop_entity_id, shop_entity_type_id, shop_attribute_set_id, shop_parent_id, shop_store_category_id, shop_parent_store_category_id)
3201
- (SELECT entity_id, entity_type_id, attribute_set_id, parent_id, store_category_id, parent_store_category_id
3202
- FROM $catalog_category_entity)";
3203
- echo("\n $query\n");
3204
- $this->db_do($query);
3205
-
3206
-
3207
- $query = "
3208
- UPDATE $stINch_categories_mapping_temp cmt
3209
- JOIN $categories_temp c
3210
- ON cmt.shop_store_category_id = c.store_category_id
3211
- SET
3212
- cmt.store_category_id = c.store_category_id,
3213
- cmt.parent_store_category_id = c.parent_store_category_id,
3214
- cmt.category_name = c.category_name,
3215
- cmt.order_number = c.order_number,
3216
- cmt.products_within_this_category = c.products_within_this_category";
3217
- echo("\n $query\n");
3218
- $this->db_do($query);
3219
-
3220
-
3221
- $query = "
3222
- UPDATE $stINch_categories_mapping_temp cmt
3223
- JOIN $catalog_category_entity cce
3224
- ON cmt.parent_store_category_id = cce.store_category_id
3225
- SET cmt.shop_parent_id = cce.entity_id";
3226
- echo("\n $query\n");
3227
- $this->db_do($query);
3228
-
3229
-
3230
- $query = "
3231
- SELECT DISTINCT
3232
- c.RootName, cce.entity_id
3233
- FROM $categories_temp c
3234
- JOIN $catalog_category_entity_varchar ccev
3235
- ON c.RootName = ccev.value
3236
- AND ccev.entity_type_id = $_categoryEntityTypeId
3237
- AND ccev.attribute_id = $name_attrid
3238
- AND ccev.store_id = 0
3239
- JOIN $catalog_category_entity cce
3240
- ON ccev.entity_id = cce.entity_id";
3241
- echo("\n $query\n");
3242
- $root_categories = $this->db_do($query);
3243
-
3244
- while ($root_cat = mysqli_fetch_array($root_categories)) {
3245
- $root_id = $root_cat['entity_id'];
3246
- $root_name = $root_cat['RootName'];
3247
-
3248
- $query = "
3249
- UPDATE $stINch_categories_mapping_temp cmt
3250
- JOIN $categories_temp c
3251
- ON cmt.shop_store_category_id = c.store_category_id
3252
- SET
3253
- cmt.shop_parent_id = $root_id,
3254
- cmt.shop_parent_store_category_id = $root_id,
3255
- cmt.parent_store_category_id = $root_id,
3256
- c.parent_store_category_id = $root_id
3257
- WHERE RootName = '$root_name'
3258
- AND cmt.shop_parent_id = 0";
3259
- echo("\n $query\n");
3260
- $this->db_do($query);
3261
- }
3262
-
3263
-
3264
- // added for mapping new sinch categories in merge && !UPDATE_CATEGORY_DATA mode
3265
- if ((UPDATE_CATEGORY_DATA && $im_type == "MERGE") || ($im_type == "REWRITE")) $where = '';
3266
- else $where = 'WHERE cce.parent_id = 0 AND cce.store_category_id IS NOT NULL';
3267
-
3268
- $query = "
3269
- UPDATE $stINch_categories_mapping_temp cmt
3270
- JOIN $catalog_category_entity cce
3271
- ON cmt.shop_entity_id = cce.entity_id
3272
- SET cce.parent_id = cmt.shop_parent_id
3273
- $where";
3274
- echo("\n $query\n");
3275
- $this->db_do($query);
3276
-
3277
- $query = "DROP TABLE IF EXISTS $stINch_categories_mapping";
3278
- echo("\n $query\n");
3279
- $this->db_do($query);
3280
-
3281
- $query = "RENAME TABLE $stINch_categories_mapping_temp TO $stINch_categories_mapping";
3282
- echo("\n $query\n");
3283
- $this->db_do($query);
3284
-
3285
- echo("\n mapSinchCategoriesMultistore done... \n ==========================================================================\n\n\n\n");
3286
- }
3287
-
3288
- #################################################################################################
3289
-
3290
- private function addCategoryDataMultistoreMerge($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
3291
- $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
3292
- $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid)
3293
- {
3294
- echo("\n\n\n\n *************************************************************\n addCategoryDataMultistoreMerge start... \n");
3295
-
3296
-
3297
- if (UPDATE_CATEGORY_DATA) {
3298
- $ignore = '';
3299
- $on_diplicate_key_update = "
3300
- ON DUPLICATE KEY UPDATE
3301
- updated_at = now(),
3302
- store_category_id = c.store_category_id,
3303
- level = c.level,
3304
- children_count = c.children_count,
3305
- position = c.order_number,
3306
- parent_store_category_id = c.parent_store_category_id";
3307
- //level=c.level,
3308
- //children_count=c.children_count
3309
- //position=c.order_number,
3310
- } else {
3311
- $ignore = 'IGNORE';
3312
- $on_diplicate_key_update = '';
3313
- }
3314
-
3315
- $query = "
3316
- INSERT $ignore INTO $catalog_category_entity
3317
- (
3318
- entity_type_id,
3319
- attribute_set_id,
3320
- created_at,
3321
- updated_at,
3322
- level,
3323
- children_count,
3324
- entity_id,
3325
- position,
3326
- parent_id,
3327
- store_category_id,
3328
- parent_store_category_id
3329
- )
3330
- (SELECT
3331
- $_categoryEntityTypeId,
3332
- $_categoryDefault_attribute_set_id,
3333
- NOW(),
3334
- NOW(),
3335
- c.level,
3336
- c.children_count,
3337
- scm.shop_entity_id,
3338
- c.order_number,
3339
- scm.shop_parent_id,
3340
- c.store_category_id,
3341
- c.parent_store_category_id
3342
- FROM $categories_temp c
3343
- LEFT JOIN $stINch_categories_mapping scm
3344
- ON c.store_category_id = scm.store_category_id
3345
- ) $on_diplicate_key_update";
3346
- echo("\n\n $query\n\n");
3347
- $this->db_do($query);
3348
-
3349
-
3350
- $this->mapSinchCategoriesMultistoreMerge($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid);
3351
-
3352
-
3353
- $categories = $this->db_do("SELECT entity_id, parent_id FROM $catalog_category_entity ORDER BY parent_id");
3354
- while ($row = mysqli_fetch_array($categories)) {
3355
- $parent_id = $row['parent_id'];
3356
- $entity_id = $row['entity_id'];
3357
-
3358
- $path = $this->culcPathMultistore($parent_id, $entity_id, $catalog_category_entity);
3359
-
3360
- $this->db_do("
3361
- UPDATE $catalog_category_entity
3362
- SET path = '$path'
3363
- WHERE entity_id = $entity_id");
3364
- } // while ($row = mysqli_fetch_array($categories))
3365
-
3366
-
3367
- ///////////////////////////////////////////////////////
3368
-
3369
-
3370
- if (UPDATE_CATEGORY_DATA) {
3371
- echo "Update category_data \n";
3372
-
3373
- $q = "
3374
- INSERT INTO $catalog_category_entity_varchar
3375
- (
3376
- entity_type_id,
3377
- attribute_id,
3378
- store_id,
3379
- entity_id,
3380
- value
3381
- )
3382
- (SELECT
3383
- $_categoryEntityTypeId,
3384
- $name_attrid,
3385
- 0,
3386
- scm.shop_entity_id,
3387
- c.category_name
3388
- FROM $categories_temp c
3389
- JOIN $stINch_categories_mapping scm
3390
- ON c.store_category_id = scm.store_category_id
3391
- )
3392
- ON DUPLICATE KEY UPDATE
3393
- value = c.category_name";
3394
- $this->db_do($q);
3395
-
3396
-
3397
- $q = "
3398
- INSERT INTO $catalog_category_entity_varchar
3399
- (
3400
- entity_type_id,
3401
- attribute_id,
3402
- store_id,
3403
- entity_id,
3404
- value
3405
- )
3406
- (SELECT
3407
- $_categoryEntityTypeId,
3408
- $name_attrid,
3409
- 1,
3410
- scm.shop_entity_id,
3411
- c.category_name
3412
- FROM $categories_temp c
3413
- JOIN $stINch_categories_mapping scm
3414
- ON c.store_category_id = scm.store_category_id
3415
- )
3416
- ON DUPLICATE KEY UPDATE
3417
- value = c.category_name";
3418
- $this->db_do($q);
3419
-
3420
-
3421
- $q = "
3422
- INSERT INTO $catalog_category_entity
3423
- (
3424
- entity_type_id,
3425
- attribute_id,
3426
- store_id,
3427
- entity_id,
3428
- value
3429
- )
3430
- (SELECT
3431
- $_categoryEntityTypeId,
3432
- $attr_is_active,
3433
- 0,
3434
- scm.shop_entity_id,
3435
- 1
3436
- FROM $categories_temp c
3437
- JOIN $stINch_categories_mapping scm
3438
- ON c.store_category_id = scm.store_category_id
3439
- )
3440
- ON DUPLICATE KEY UPDATE
3441
- value = 1";
3442
- $this->db_do($q);
3443
-
3444
-
3445
- $q = "
3446
- INSERT INTO $catalog_category_entity_int
3447
- (
3448
- entity_type_id,
3449
- attribute_id,
3450
- store_id,
3451
- entity_id,
3452
- value
3453
- )
3454
- (SELECT
3455
- $_categoryEntityTypeId,
3456
- $attr_is_active,
3457
- 1,
3458
- scm.shop_entity_id,
3459
- 1
3460
- FROM $categories_temp c
3461
- JOIN $stINch_categories_mapping scm
3462
- ON c.store_category_id = scm.store_category_id
3463
- )
3464
- ON DUPLICATE KEY UPDATE
3465
- value = 1";
3466
- $this->db_do($q);
3467
-
3468
-
3469
- $q = "
3470
- INSERT INTO $catalog_category_entity_int
3471
- (
3472
- entity_type_id,
3473
- attribute_id,
3474
- store_id,
3475
- entity_id,
3476
- value
3477
- )
3478
- (SELECT
3479
- $_categoryEntityTypeId,
3480
- $attr_include_in_menu,
3481
- 0,
3482
- scm.shop_entity_id,
3483
- c.include_in_menu
3484
- FROM $categories_temp c
3485
- JOIN $stINch_categories_mapping scm
3486
- ON c.store_category_id = scm.store_category_id
3487
- )
3488
- ON DUPLICATE KEY UPDATE
3489
- value = c.include_in_menu";
3490
- $this->db_do($q);
3491
-
3492
-
3493
- $q = "
3494
- INSERT INTO $catalog_category_entity_int
3495
- (
3496
- entity_type_id,
3497
- attribute_id,
3498
- store_id,
3499
- entity_id,
3500
- value
3501
- )
3502
- (SELECT
3503
- $_categoryEntityTypeId,
3504
- $is_anchor_attrid,
3505
- 1,
3506
- scm.shop_entity_id,
3507
- c.is_anchor
3508
- FROM $categories_temp c
3509
- JOIN $stINch_categories_mapping scm
3510
- ON c.store_category_id = scm.store_category_id
3511
- )
3512
- ON DUPLICATE KEY UPDATE
3513
- value = c.is_anchor";
3514
- $this->db_do($q);
3515
-
3516
-
3517
- $q = "
3518
- INSERT INTO $catalog_category_entity_int
3519
- (
3520
- entity_type_id,
3521
- attribute_id,
3522
- store_id,
3523
- entity_id,
3524
- value
3525
- )
3526
- (SELECT
3527
- $_categoryEntityTypeId,
3528
- $is_anchor_attrid,
3529
- 0,
3530
- scm.shop_entity_id,
3531
- c.is_anchor
3532
- FROM $categories_temp c
3533
- JOIN $stINch_categories_mapping scm
3534
- ON c.store_category_id = scm.store_category_id
3535
- )
3536
- ON DUPLICATE KEY UPDATE
3537
- value = c.is_anchor";
3538
- $this->db_do($q);
3539
-
3540
- $q = "
3541
- INSERT INTO $catalog_category_entity_varchar
3542
- (
3543
- entity_type_id,
3544
- attribute_id,
3545
- store_id,
3546
- entity_id,
3547
- value
3548
- )
3549
- (SELECT
3550
- $_categoryEntityTypeId,
3551
- $image_attrid,
3552
- 0,
3553
- scm.shop_entity_id,
3554
- c.categories_image
3555
- FROM $categories_temp c
3556
- JOIN $stINch_categories_mapping scm
3557
- ON c.store_category_id = scm.store_category_id
3558
- )
3559
- ON DUPLICATE KEY UPDATE
3560
- value = c.categories_image";
3561
- $this->db_do($q);
3562
- //STP
3563
- $q = "
3564
- INSERT INTO $catalog_category_entity_varchar
3565
- (
3566
- entity_type_id,
3567
- attribute_id,
3568
- store_id,
3569
- entity_id,
3570
- value
3571
- )
3572
- (SELECT
3573
- $this->_categoryEntityTypeId,
3574
- $this->_categoryMetaTitleAttrId,
3575
- 0,
3576
- scm.shop_entity_id,
3577
- c.MetaTitle
3578
- FROM $categories_temp c
3579
- JOIN $stINch_categories_mapping scm
3580
- ON c.store_category_id = scm.store_category_id
3581
- )
3582
- ON DUPLICATE KEY UPDATE
3583
- value = c.MetaTitle";
3584
- $this->db_do($q);
3585
-
3586
- $q = "
3587
- INSERT INTO $catalog_category_entity_varchar
3588
- (
3589
- entity_type_id,
3590
- attribute_id,
3591
- store_id,
3592
- entity_id,
3593
- value
3594
- )
3595
- (SELECT
3596
- $this->_categoryEntityTypeId,
3597
- $this->_categoryMetadescriptionAttrId,
3598
- 0,
3599
- scm.shop_entity_id,
3600
- c.MetaDescription
3601
- FROM $categories_temp c
3602
- JOIN $stINch_categories_mapping scm
3603
- ON c.store_category_id = scm.store_category_id
3604
- )
3605
- ON DUPLICATE KEY UPDATE
3606
- value = c.MetaDescription";
3607
- $this->db_do($q);
3608
-
3609
- $q = "
3610
- INSERT INTO $catalog_category_entity_varchar
3611
- (
3612
- entity_type_id,
3613
- attribute_id,
3614
- store_id,
3615
- entity_id,
3616
- value
3617
- )
3618
- (SELECT
3619
- $this->_categoryEntityTypeId,
3620
- $this->_categoryDescriptionAttrId,
3621
- 0,
3622
- scm.shop_entity_id,
3623
- c.Description
3624
- FROM $categories_temp c
3625
- JOIN $stINch_categories_mapping scm
3626
- ON c.store_category_id = scm.store_category_id
3627
- )
3628
- ON DUPLICATE KEY UPDATE
3629
- value = c.Description";
3630
- $this->db_do($q);
3631
-
3632
-
3633
- //stp
3634
- } else {
3635
- echo "Insert ignore category_data \n";
3636
-
3637
-
3638
- $q = "
3639
- INSERT IGNORE INTO $catalog_category_entity_varchar
3640
- (
3641
- entity_type_id,
3642
- attribute_id,
3643
- store_id,
3644
- entity_id,
3645
- value
3646
- )
3647
- (SELECT
3648
- $_categoryEntityTypeId,
3649
- $name_attrid,
3650
- 0,
3651
- scm.shop_entity_id,
3652
- c.category_name
3653
- FROM $categories_temp c
3654
- JOIN $stINch_categories_mapping scm
3655
- ON c.store_category_id = scm.store_category_id
3656
- )";
3657
- $this->db_do($q);
3658
-
3659
-
3660
- $q = "
3661
- INSERT IGNORE INTO $catalog_category_entity_int
3662
- (
3663
- entity_type_id,
3664
- attribute_id,
3665
- store_id,
3666
- entity_id,
3667
- value
3668
- )
3669
- (SELECT
3670
- $_categoryEntityTypeId,
3671
- $attr_is_active,
3672
- 0,
3673
- scm.shop_entity_id,
3674
- 1
3675
- FROM $categories_temp c
3676
- JOIN $stINch_categories_mapping scm
3677
- ON c.store_category_id = scm.store_category_id
3678
- )";
3679
- $this->db_do($q);
3680
-
3681
-
3682
- $q = "
3683
- INSERT IGNORE INTO $catalog_category_entity_int
3684
- (
3685
- entity_type_id,
3686
- attribute_id,
3687
- store_id,
3688
- entity_id,
3689
- value
3690
- )
3691
- (SELECT
3692
- $_categoryEntityTypeId,
3693
- $attr_include_in_menu,
3694
- 0,
3695
- scm.shop_entity_id,
3696
- c.include_in_menu
3697
- FROM $categories_temp c
3698
- JOIN $stINch_categories_mapping scm
3699
- ON c.store_category_id = scm.store_category_id
3700
- )";
3701
- $this->db_do($q);
3702
-
3703
-
3704
- $q = "
3705
- INSERT IGNORE INTO $catalog_category_entity_int
3706
- (
3707
- entity_type_id,
3708
- attribute_id,
3709
- store_id,
3710
- entity_id,
3711
- value
3712
- )
3713
- (SELECT
3714
- $_categoryEntityTypeId,
3715
- $is_anchor_attrid,
3716
- 0,
3717
- scm.shop_entity_id,
3718
- c.is_anchor
3719
- FROM $categories_temp c
3720
- JOIN $stINch_categories_mapping scm
3721
- ON c.store_category_id = scm.store_category_id
3722
- )";
3723
- $this->db_do($q);
3724
-
3725
-
3726
- $q = "
3727
- INSERT IGNORE INTO $catalog_category_entity_varchar
3728
- (
3729
- entity_type_id,
3730
- attribute_id,
3731
- store_id,
3732
- entity_id,
3733
- value
3734
- )
3735
- (SELECT
3736
- $_categoryEntityTypeId,
3737
- $image_attrid,
3738
- 0,
3739
- scm.shop_entity_id,
3740
- c.categories_image
3741
- FROM $categories_temp c
3742
- JOIN $stINch_categories_mapping scm
3743
- ON c.store_category_id = scm.store_category_id
3744
- )";
3745
- $this->db_do($q);
3746
- //STP
3747
- $q = "
3748
- INSERT IGNORE INTO $catalog_category_entity_varchar
3749
- (
3750
- entity_type_id,
3751
- attribute_id,
3752
- store_id,
3753
- entity_id,
3754
- value
3755
- )
3756
- (SELECT
3757
- $this->_categoryEntityTypeId,
3758
- $this->_categoryMetaTitleAttrId,
3759
- 0,
3760
- scm.shop_entity_id,
3761
- c.MetaTitle
3762
- FROM $categories_temp c
3763
- JOIN $stINch_categories_mapping scm
3764
- ON c.store_category_id = scm.store_category_id
3765
- )
3766
- ";
3767
- $this->db_do($q);
3768
-
3769
- $q = "
3770
- INSERT IGNORE INTO $catalog_category_entity_varchar
3771
- (
3772
- entity_type_id,
3773
- attribute_id,
3774
- store_id,
3775
- entity_id,
3776
- value
3777
- )
3778
- (SELECT
3779
- $this->_categoryEntityTypeId,
3780
- $this->_categoryMetadescriptionAttrId,
3781
- 0,
3782
- scm.shop_entity_id,
3783
- c.MetaDescription
3784
- FROM $categories_temp c
3785
- JOIN $stINch_categories_mapping scm
3786
- ON c.store_category_id = scm.store_category_id
3787
- )
3788
- ";
3789
- $this->db_do($q);
3790
-
3791
- $q = "
3792
- INSERT IGNORE INTO $catalog_category_entity_varchar
3793
- (
3794
- entity_type_id,
3795
- attribute_id,
3796
- store_id,
3797
- entity_id,
3798
- value
3799
- )
3800
- (SELECT
3801
- $this->_categoryEntityTypeId,
3802
- $this->_categoryDescriptionAttrId,
3803
- 0,
3804
- scm.shop_entity_id,
3805
- c.Description
3806
- FROM $categories_temp c
3807
- JOIN $stINch_categories_mapping scm
3808
- ON c.store_category_id = scm.store_category_id
3809
- )
3810
- ";
3811
- $this->db_do($q);
3812
-
3813
-
3814
- //stp
3815
-
3816
- }
3817
-
3818
-
3819
- //return; // !!!!!!!!!!!!!!!!!!!!!!!!!!!
3820
-
3821
- $this->db_do("DROP TABLE IF EXISTS $stINch_categories\n\n");
3822
- $this->db_do("RENAME TABLE $categories_temp TO $stINch_categories");
3823
-
3824
- $this->deleteOldSinchCategoriesFromShopMerge($stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int);
3825
- /**/
3826
-
3827
- echo("\n addCategoryDataMultistoreMerge done... \n *************************************************************\n");
3828
-
3829
- }
3830
-
3831
- #################################################################################################
3832
-
3833
- private function deleteOldSinchCategoriesFromShopMerge($stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int)
3834
- {
3835
-
3836
- echo("\n\n\n\n +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n deleteOldSinchCategoriesFromShopMerge start... \n");
3837
-
3838
-
3839
- $query = "DROP TABLE IF EXISTS delete_cats";
3840
- echo("\n $query\n");
3841
- $this->db_do($query);
3842
-
3843
-
3844
- $delete_cats = Mage::getSingleton('core/resource')->getTableName('delete_cats');
3845
- $stINch_categories = Mage::getSingleton('core/resource')->getTableName('stINch_categories');
3846
-
3847
- $query = "
3848
- CREATE TABLE $delete_cats
3849
-
3850
- SELECT entity_id
3851
- FROM $catalog_category_entity cce
3852
- WHERE cce.entity_id NOT IN
3853
- (
3854
- SELECT cce2.entity_id
3855
- FROM $catalog_category_entity cce2
3856
- JOIN $stINch_categories sc
3857
- ON cce2.store_category_id = sc.store_category_id
3858
- )
3859
- AND cce.store_category_id IS NOT NULL
3860
- ;";
3861
-
3862
- echo("\n $query\n");
3863
- $this->db_do($query);
3864
-
3865
-
3866
- $query = "DELETE cce FROM $catalog_category_entity cce JOIN $delete_cats dc USING(entity_id)";
3867
- echo("\n $query\n");
3868
- $this->db_do($query);
3869
-
3870
-
3871
- $query = "DROP TABLE IF EXISTS $delete_cats";
3872
- echo("\n $query\n");
3873
- //$this->db_do($query );
3874
-
3875
-
3876
- /**
3877
- * $query = "
3878
- * DELETE cat FROM $catalog_category_entity_varchar cat
3879
- * JOIN $stINch_categories_mapping scm
3880
- * ON cat.entity_id = scm.shop_entity_id
3881
- * WHERE
3882
- * (scm.shop_store_category_id IS NOT NULL) AND
3883
- * (scm.store_category_id IS NULL)";
3884
- * echo("\n $query\n");
3885
- * // $this->db_do($query);
3886
- *
3887
- * $query = "
3888
- * DELETE cat FROM $catalog_category_entity_int cat
3889
- * JOIN $stINch_categories_mapping scm
3890
- * ON cat.entity_id = scm.shop_entity_id
3891
- * WHERE
3892
- * (scm.shop_store_category_id IS NOT NULL) AND
3893
- * (scm.store_category_id IS NULL)";
3894
- * echo("\n $query\n");
3895
- * // $this->db_do($query);
3896
- *
3897
- * $query = "
3898
- * DELETE cat FROM $catalog_category_entity cat
3899
- * JOIN $stINch_categories_mapping scm
3900
- * ON cat.entity_id=scm.shop_entity_id
3901
- * WHERE
3902
- * (scm.shop_store_category_id IS NOT NULL) AND
3903
- * (scm.store_category_id IS NULL)";
3904
- * echo("\n $query\n");
3905
- * // $this->db_do($query);
3906
- * /**/
3907
-
3908
- echo("\n deleteOldSinchCategoriesFromShopMerge done... \n +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n\n");
3909
-
3910
- }
3911
-
3912
- #################################################################################################
3913
-
3914
- private function _set_default_root_category()
3915
- {
3916
- $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('core_store_group') . " csg
3917
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
3918
- ON csg.root_category_id = cce.entity_id
3919
- SET csg.root_category_id=(SELECT entity_id FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " WHERE parent_id = 1 LIMIT 1)
3920
- WHERE csg.root_category_id > 0 AND cce.entity_id IS NULL";
3921
- $this->db_do($q);
3922
- }
3923
-
3924
- #################################################################################################
3925
-
3926
- function ParseCategoryFeatures()
3927
- {
3928
-
3929
- $parse_file = $this->varDir . FILE_CATEGORIES_FEATURES;
3930
- if (filesize($parse_file) || $this->_ignore_category_features) {
3931
- $this->_LOG("Start parse " . FILE_CATEGORIES_FEATURES);
3932
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp'));
3933
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp') . " (
3934
- category_feature_id int(11),
3935
- store_category_id int(11),
3936
- feature_name varchar(50),
3937
- display_order_number int(11),
3938
- KEY(store_category_id),
3939
- KEY(category_feature_id)
3940
- )
3941
- ");
3942
-
3943
- if (!$this->_ignore_category_features) {
3944
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3945
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp') . "
3946
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3947
- OPTIONALLY ENCLOSED BY '\"'
3948
- LINES TERMINATED BY \"\r\n\"
3949
- IGNORE 1 LINES ");
3950
- }
3951
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_features'));
3952
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp') . "
3953
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_features'));
3954
-
3955
- $this->_LOG("Finish parse " . FILE_CATEGORIES_FEATURES);
3956
- } else {
3957
- $this->_LOG("Wrong file " . $parse_file);
3958
- }
3959
- $this->_LOG(' ');
3960
- }
3961
-
3962
- #################################################################################################
3963
-
3964
- function ParseDistributors()
3965
- {
3966
-
3967
- $parse_file = $this->varDir . FILE_DISTRIBUTORS;
3968
- if (filesize($parse_file)) {
3969
- $this->_LOG("Start parse " . FILE_DISTRIBUTORS);
3970
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('distributors_temp'));
3971
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_temp') . "(
3972
- distributor_id int(11),
3973
- distributor_name varchar(255),
3974
- website varchar(255),
3975
- KEY(distributor_id)
3976
- )
3977
- ");
3978
-
3979
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3980
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_temp') . "
3981
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3982
- OPTIONALLY ENCLOSED BY '\"'
3983
- LINES TERMINATED BY \"\r\n\"
3984
- IGNORE 1 LINES ");
3985
-
3986
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors'));
3987
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_temp') . "
3988
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors'));
3989
-
3990
- $this->_LOG("Finish parse " . FILE_DISTRIBUTORS);
3991
- } else {
3992
- $this->_LOG("Wrong file " . $parse_file);
3993
- }
3994
- $this->_LOG(' ');
3995
- }
3996
-
3997
-
3998
- #################################################################################################
3999
-
4000
- function ParseDistributorsStockAndPrice()
4001
- {
4002
- $parse_file = $this->varDir . FILE_DISTRIBUTORS_STOCK_AND_PRICES;
4003
- if (filesize($parse_file)) {
4004
- $this->_LOG("Start parse " . FILE_DISTRIBUTORS_STOCK_AND_PRICES);
4005
-
4006
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp'));
4007
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp') . "(
4008
- `store_product_id` int(11) DEFAULT NULL,
4009
- `distributor_id` int(11) DEFAULT NULL,
4010
- `stock` int(11) DEFAULT NULL,
4011
- `cost` decimal(15,4) DEFAULT NULL,
4012
- `distributor_sku` varchar(255) DEFAULT NULL,
4013
- `distributor_category` varchar(50) DEFAULT NULL,
4014
- `eta` varchar(50) DEFAULT NULL,
4015
- UNIQUE KEY `product_distri` (store_product_id, distributor_id)
4016
- )");
4017
-
4018
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4019
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp') . "
4020
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4021
- OPTIONALLY ENCLOSED BY '\"'
4022
- LINES TERMINATED BY \"\r\n\"
4023
- IGNORE 1 LINES ");
4024
-
4025
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
4026
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp') . "
4027
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
4028
-
4029
- $this->_LOG("Finish parse " . FILE_DISTRIBUTORS_STOCK_AND_PRICES);
4030
- } else {
4031
- $this->_LOG("Wrong file " . $parse_file);
4032
- }
4033
- $this->_LOG(' ');
4034
-
4035
- }
4036
-
4037
- ############################### ##################################################################
4038
-
4039
- function ParseProductContracts()
4040
- {
4041
- $parse_file = $this->varDir . FILE_PRODUCT_CONTRACTS;
4042
- if (filesize($parse_file)) {
4043
- $this->_LOG("Start parse " . FILE_PRODUCT_CONTRACTS);
4044
-
4045
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp'));
4046
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp') . "(
4047
- `store_product_id` int(11) DEFAULT NULL,
4048
- `contract_id` varchar(50) DEFAULT NULL,
4049
- KEY `store_product_id` (store_product_id)
4050
- )");
4051
-
4052
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4053
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp') . "
4054
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4055
- OPTIONALLY ENCLOSED BY '\"'
4056
- LINES TERMINATED BY \"\r\n\"
4057
- IGNORE 1 LINES ");
4058
-
4059
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts'));
4060
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp') . "
4061
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts'));
4062
-
4063
- $this->_LOG("Finish parse " . FILE_PRODUCT_CONTRACTS);
4064
- } else {
4065
- $this->_LOG("Wrong file " . $parse_file);
4066
- }
4067
- $this->_LOG(' ');
4068
-
4069
- }
4070
-
4071
- #################################################################################################
4072
-
4073
- function ParseEANCodes()
4074
- {
4075
-
4076
- $parse_file = $this->varDir . FILE_EANCODES;
4077
- if (filesize($parse_file)) {
4078
- $this->_LOG("Start parse " . FILE_EANCODES);
4079
-
4080
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp'));
4081
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp') . "(
4082
- product_id int(11),
4083
- ean_code varchar(255),
4084
- KEY(product_id)
4085
- )");
4086
-
4087
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4088
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp') . "
4089
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4090
- OPTIONALLY ENCLOSED BY '\"'
4091
- LINES TERMINATED BY \"\r\n\"
4092
- IGNORE 1 LINES ");
4093
-
4094
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes'));
4095
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp') . "
4096
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes'));
4097
-
4098
- $this->_LOG("Finish parse " . FILE_EANCODES);
4099
- } else {
4100
- $this->_LOG("Wrong file " . $parse_file);
4101
- }
4102
- $this->_LOG(' ');
4103
- }
4104
-
4105
- #################################################################################################
4106
-
4107
- function ParseManufacturers()
4108
- {
4109
-
4110
- $parse_file = $this->varDir . FILE_MANUFACTURERS;
4111
- if (filesize($parse_file)) {
4112
- $this->_LOG("Start parse " . FILE_MANUFACTURERS);
4113
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp'));
4114
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . "(
4115
- sinch_manufacturer_id int(11),
4116
- manufacturer_name varchar(255),
4117
- manufacturers_image varchar(255),
4118
- shop_option_id int(11),
4119
- KEY(sinch_manufacturer_id),
4120
- KEY(shop_option_id),
4121
- KEY(manufacturer_name)
4122
- )");
4123
-
4124
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4125
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . "
4126
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4127
- OPTIONALLY ENCLOSED BY '\"'
4128
- LINES TERMINATED BY \"\r\n\"
4129
- IGNORE 1 LINES ");
4130
-
4131
- $q = "DELETE aov
4132
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . " ao
4133
- JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
4134
- ON ao.option_id=aov.option_id left
4135
- JOIN " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . " mt
4136
- ON aov.value=mt.manufacturer_name
4137
- WHERE
4138
- ao.attribute_id=" . $this->attributes['manufacturer'] . " AND
4139
- mt.manufacturer_name is null";
4140
- $this->db_do($q);
4141
-
4142
- $q = "DELETE ao
4143
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . " ao
4144
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
4145
- ON ao.option_id=aov.option_id
4146
- WHERE
4147
- attribute_id=" . $this->attributes['manufacturer'] . " AND
4148
- aov.option_id is null";
4149
- $this->db_do($q);
4150
-
4151
- $q = "SELECT
4152
- m.sinch_manufacturer_id,
4153
- m.manufacturer_name,
4154
- m.manufacturers_image
4155
- FROM " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . " m
4156
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
4157
- ON m.manufacturer_name=aov.value
4158
- WHERE aov.value IS NULL";
4159
- $quer = $this->db_do($q);
4160
-
4161
- while ($row = mysqli_fetch_array($quer)) {
4162
- $q0 = "INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . "
4163
- (attribute_id)
4164
- VALUES(" . $this->attributes['manufacturer'] . ")";
4165
- $quer0 = $this->db_do($q0);
4166
-
4167
- $q2 = "INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . "(
4168
- option_id,
4169
- value
4170
- )(
4171
- SELECT
4172
- max(option_id) as option_id,
4173
- " . "'" . mysqli_real_escape_string($this->db, $row['manufacturer_name']) . "'
4174
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . "
4175
- WHERE attribute_id=" . $this->attributes['manufacturer'] . "
4176
- )
4177
- ";
4178
- $quer2 = $this->db_do($q2);
4179
- // $option['attribute_id'] = $this->attributes['manufacturer'];
4180
- // $option['value'][$row['sinch_manufacturer_id']][0] = $row['manufacturer_name'];
4181
-
4182
- }
4183
-
4184
- $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . " mt
4185
- JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
4186
- ON mt.manufacturer_name=aov.value
4187
- JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . " ao
4188
- ON ao.option_id=aov.option_id
4189
- SET mt.shop_option_id=aov.option_id
4190
- WHERE ao.attribute_id=" . $this->attributes['manufacturer'];
4191
- $this->db_do($q);
4192
-
4193
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers'));
4194
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . "
4195
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers'));
4196
- $this->_LOG("Finish parse " . FILE_MANUFACTURERS);
4197
- } else {
4198
- $this->_LOG("Wrong file " . $parse_file);
4199
- }
4200
- $this->_LOG(' ');
4201
- }
4202
-
4203
- #################################################################################################
4204
-
4205
- function ParseRelatedProducts()
4206
- {
4207
-
4208
- $parse_file = $this->varDir . FILE_RELATED_PRODUCTS;
4209
- if (filesize($parse_file) || $this->_ignore_product_related) {
4210
- $this->_LOG("Start parse " . FILE_RELATED_PRODUCTS);
4211
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('related_products_temp'));
4212
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('related_products_temp') . "(
4213
- sinch_product_id int(11),
4214
- related_sinch_product_id int(11),
4215
- store_product_id int(11) default null,
4216
- store_related_product_id int(11) default null,
4217
- entity_id int(11),
4218
- related_entity_id int(11),
4219
- KEY(sinch_product_id),
4220
- KEY(related_sinch_product_id),
4221
- KEY(store_product_id)
4222
- )DEFAULT CHARSET=utf8");
4223
- if (!$this->_ignore_product_related) {
4224
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4225
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('related_products_temp') . "
4226
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4227
- OPTIONALLY ENCLOSED BY '\"'
4228
- LINES TERMINATED BY \"\r\n\"
4229
- IGNORE 1 LINES ");
4230
- }
4231
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products'));
4232
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('related_products_temp') . "
4233
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products'));
4234
-
4235
- $this->_LOG("Finish parse " . FILE_RELATED_PRODUCTS);
4236
- } else {
4237
- $this->_LOG("Wrong file " . $parse_file);
4238
- }
4239
- $this->_LOG(" ");
4240
- }
4241
-
4242
-
4243
- #################################################################################################
4244
-
4245
- function ParseProductFeatures()
4246
- {
4247
-
4248
- $parse_file = $this->varDir . FILE_PRODUCT_FEATURES;
4249
- if (filesize($parse_file) || $this->_ignore_product_features) {
4250
- $this->_LOG("Start parse " . FILE_PRODUCT_FEATURES);
4251
-
4252
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('product_features_temp'));
4253
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('product_features_temp') . "(
4254
- product_feature_id int(11),
4255
- sinch_product_id int(11),
4256
- restricted_value_id int(11),
4257
- KEY(sinch_product_id),
4258
- KEY(restricted_value_id)
4259
- )
4260
- ");
4261
- if (!$this->_ignore_product_features) {
4262
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4263
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('product_features_temp') . "
4264
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4265
- OPTIONALLY ENCLOSED BY '\"'
4266
- LINES TERMINATED BY \"\r\n\"
4267
- IGNORE 1 LINES ");
4268
- }
4269
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_features'));
4270
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('product_features_temp') . "
4271
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_features'));
4272
-
4273
- $this->_LOG("Finish parse " . FILE_PRODUCT_FEATURES);
4274
- } else {
4275
- $this->_LOG("Wrong file " . $parse_file);
4276
- }
4277
- $this->_LOG(" ");
4278
- }
4279
-
4280
- #################################################################################################
4281
-
4282
- function ParseProductCategories()
4283
- {
4284
- $parse_file = $this->varDir . FILE_PRODUCT_CATEGORIES;
4285
- if (filesize($parse_file)) {
4286
- $this->_LOG("Start parse " . FILE_PRODUCT_CATEGORIES);
4287
-
4288
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp'));
4289
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp') . "(
4290
- store_product_id int(11),
4291
- store_category_id int(11),
4292
- key(store_product_id),
4293
- key(store_category_id)
4294
- )");
4295
-
4296
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4297
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp') . "
4298
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4299
- OPTIONALLY ENCLOSED BY '\"'
4300
- LINES TERMINATED BY \"\r\n\"
4301
- IGNORE 1 LINES ");
4302
-
4303
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories'));
4304
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp') . "
4305
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories'));
4306
-
4307
- $this->_LOG("Finish parse " . FILE_PRODUCT_CATEGORIES);
4308
- } else {
4309
- $this->_LOG("Wrong file " . $parse_file);
4310
- }
4311
- $this->_LOG(' ');
4312
-
4313
- }
4314
-
4315
-
4316
- #################################################################################################
4317
-
4318
- function ParseProducts($coincidence)
4319
- {
4320
- echo("\nParseProducts 2\n");
4321
- $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
4322
- $replace_merge_product = $dataConf['replace_products'];
4323
-
4324
- $parse_file = $this->varDir . FILE_PRODUCTS;
4325
- //$parse_file = $this->varDir . FILE_PRODUCTS_TEST;
4326
- if (filesize($parse_file)) {
4327
- $this->_LOG("Start parse " . FILE_PRODUCTS);
4328
- echo("\nParseProducts 2\n");
4329
-
4330
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('products_temp'));
4331
- if ($this->product_file_format == "NEW") {
4332
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "(
4333
- store_product_id int(11),
4334
- product_sku varchar(255),
4335
- product_name varchar(255),
4336
- sinch_manufacturer_id int(11),
4337
- main_image_url varchar(255),
4338
- thumb_image_url varchar(255),
4339
- specifications text,
4340
- description text,
4341
- search_cache text,
4342
- description_type varchar(50),
4343
- medium_image_url varchar(255),
4344
- Title varchar(255),
4345
- Weight decimal(15,4),
4346
- Family varchar(255),
4347
- Reviews varchar(255),
4348
- pdf_url varchar(255),
4349
- product_short_description varchar(255),
4350
- products_date_added datetime default NULL,
4351
- products_last_modified datetime default NULL,
4352
- availability_id_in_stock int(11) default '1',
4353
- availability_id_out_of_stock int(11) default '2',
4354
- products_locate varchar(30) default NULL,
4355
- products_ordered int(11) NOT NULL default '0',
4356
- products_url varchar(255) default NULL,
4357
- products_viewed int(5) default '0',
4358
- products_seo_url varchar(100) NOT NULL,
4359
- manufacturer_name varchar(255) default NULL,
4360
- KEY(store_product_id),
4361
- KEY(sinch_manufacturer_id)
4362
- )DEFAULT CHARSET=utf8
4363
- ");
4364
- } elseif ($this->product_file_format == "OLD") {
4365
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "(
4366
- store_category_product_id int(11),
4367
- store_product_id int(11),
4368
- sinch_product_id int(11),
4369
- product_sku varchar(255),
4370
- product_name varchar(255),
4371
- sinch_manufacturer_id int(11),
4372
- store_category_id int(11),
4373
- main_image_url varchar(255),
4374
- thumb_image_url varchar(255),
4375
- specifications text,
4376
- description text,
4377
- search_cache text,
4378
- spec_characte_u_count int(11),
4379
- description_type varchar(50),
4380
- medium_image_url varchar(255),
4381
- products_date_added datetime default NULL,
4382
- products_last_modified datetime default NULL,
4383
- availability_id_in_stock int(11) default '1',
4384
- availability_id_out_of_stock int(11) default '2',
4385
- products_locate varchar(30) default NULL,
4386
- products_ordered int(11) NOT NULL default '0',
4387
- products_url varchar(255) default NULL,
4388
- products_viewed int(5) default '0',
4389
- products_seo_url varchar(100) NOT NULL,
4390
- manufacturer_name varchar(255) default NULL,
4391
- KEY(store_category_product_id),
4392
- KEY(store_product_id),
4393
- KEY(sinch_manufacturer_id),
4394
- KEY(store_category_id)
4395
- )DEFAULT CHARSET=utf8
4396
- ");
4397
-
4398
- }
4399
- echo("\nParseProducts 3\n");
4400
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
4401
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4402
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
4403
- OPTIONALLY ENCLOSED BY '\"'
4404
- LINES TERMINATED BY \"\r\n\"
4405
- IGNORE 1 LINES ");
4406
-
4407
- if ($this->product_file_format == "NEW") {
4408
- $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4409
- ADD COLUMN sinch_product_id int(11) AFTER store_product_id
4410
- ");
4411
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4412
- SET sinch_product_id=store_product_id
4413
- ");
4414
-
4415
- $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4416
- ADD COLUMN store_category_id int(11) AFTER sinch_manufacturer_id
4417
- ");
4418
- $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4419
- ADD KEY(store_category_id)
4420
- ");
4421
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4422
- SET product_name = Title WHERE Title != ''
4423
- ");
4424
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " pt
4425
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
4426
- SET pt.store_category_id=spc.store_category_id
4427
- WHERE pt.store_product_id=spc.store_product_id
4428
- ");
4429
- //http://redmine.bintime.com/issues/4127
4430
- //3.
4431
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4432
- SET main_image_url = medium_image_url WHERE main_image_url = ''
4433
- ");
4434
- //end
4435
-
4436
- }
4437
-
4438
- echo("\nParseProducts 4\n");
4439
-
4440
- echo("\nParseProducts 5\n");
4441
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4442
- SET products_date_added=now(), products_last_modified=now()");
4443
- echo("\nParseProducts 6\n");
4444
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4445
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " m
4446
- ON p.sinch_manufacturer_id=m.sinch_manufacturer_id
4447
- SET p.manufacturer_name=m.manufacturer_name");
4448
- echo("\nParseProducts 7\n");
4449
- if ($this->current_import_status_statistic_id) {
4450
- $res = $this->db_do("SELECT COUNT(*) AS cnt
4451
- FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp'));
4452
- $row = mysqli_fetch_assoc($res);
4453
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
4454
- SET number_of_products=" . $row['cnt'] . "
4455
- WHERE id=" . $this->current_import_status_statistic_id);
4456
- }
4457
-
4458
- if ($replace_merge_product == "REWRITE") {
4459
- $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity'));
4460
- $this->db_do("SET FOREIGN_KEY_CHECKS=0");
4461
- $this->db_do("TRUNCATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity'));
4462
- $this->db_do("SET FOREIGN_KEY_CHECKS=1");
4463
- }
4464
-
4465
- echo("\nParseProducts 8\n");
4466
- $this->addProductsWebsite();
4467
- $this->mapSinchProducts($replace_merge_product);
4468
- echo("\nParseProducts 9\n");
4469
-
4470
- if (count($coincidence) == 1) {
4471
- $this->replaceMagentoProducts();
4472
- } else {
4473
- echo("\n\n\n\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ [" . $this->im_type . "] $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n\n"); //exit;
4474
-
4475
-
4476
- switch ($this->im_type) {
4477
- case "REWRITE":
4478
- $this->replaceMagentoProductsMultistore($coincidence);
4479
- break;
4480
- case "MERGE":
4481
- $this->replaceMagentoProductsMultistoreMERGE($coincidence);
4482
- break;
4483
- }
4484
- }
4485
- echo("\nParseProducts 10\n");
4486
-
4487
- // Toàn đẹp trai đã rào đoạn mã này
4488
- $this->mapSinchProducts($replace_merge_product, true);
4489
- $this->addManufacturer_attribute();
4490
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products'));
4491
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4492
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_products'));
4493
- $this->_LOG("Finish parse " . FILE_PRODUCTS);
4494
- } else {
4495
- $this->_LOG("Wrong file " . $parse_file);
4496
- }
4497
- $this->_LOG(" ");
4498
- echo("\nParseProducts 11\n");
4499
- }
4500
-
4501
- #################################################################################################
4502
-
4503
- public function addProductsWebsite()
4504
- {
4505
- $this->db_do(" DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('products_website_temp'));
4506
- // TEMPORARY
4507
- $this->db_do("
4508
- CREATE TABLE `" . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . "` (
4509
- `id` int(10) unsigned NOT NULL auto_increment,
4510
- store_product_id int(11),
4511
- sinch_product_id int(11),
4512
- `website` int(11) default NULL,
4513
- `website_id` int(11) default NULL,
4514
- PRIMARY KEY (`id`),
4515
- KEY store_product_id (`store_product_id`)
4516
- )
4517
- ");
4518
- $result = $this->db_do("SELECT
4519
- website_id,
4520
- store_id as website
4521
- FROM " . Mage::getSingleton('core/resource')->getTableName('core_store') . "
4522
- WHERE code!='admin'
4523
- "); // where code!='admin' was adder for editing Featured products;
4524
- while ($row = mysqli_fetch_assoc($result)) {
4525
- $sql = "INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " (
4526
- store_product_id,
4527
- sinch_product_id,
4528
- website,
4529
- website_id
4530
- )(
4531
- SELECT
4532
- distinct
4533
- store_product_id,
4534
- sinch_product_id,
4535
- {$row['website']},
4536
- {$row['website_id']}
4537
- FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4538
- )";
4539
- $result2 = $this->db_do($sql);
4540
- }
4541
-
4542
-
4543
- }
4544
-
4545
- ################################################################################################
4546
-
4547
- public function mapSinchProducts($mode = 'MERGE', $mapping_again = false)
4548
- {
4549
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp'));
4550
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " (
4551
- entity_id int(11) unsigned NOT NULL,
4552
- manufacturer_option_id int(11),
4553
- manufacturer_name varchar(255),
4554
- shop_store_product_id int(11),
4555
- shop_sinch_product_id int(11),
4556
- sku varchar(64) default NULL,
4557
- store_product_id int(11),
4558
- sinch_product_id int(11),
4559
- product_sku varchar(255),
4560
- sinch_manufacturer_id int(11),
4561
- sinch_manufacturer_name varchar(255),
4562
- KEY entity_id (entity_id),
4563
- KEY manufacturer_option_id (manufacturer_option_id),
4564
- KEY manufacturer_name (manufacturer_name),
4565
- KEY store_product_id (store_product_id),
4566
- KEY sinch_product_id (sinch_product_id),
4567
- KEY sku (sku),
4568
- UNIQUE KEY(entity_id)
4569
- )
4570
- ");
4571
- $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . "
4572
- LIKE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp'));
4573
- $productEntityTable = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
4574
-
4575
- // backup Product ID in REWRITE mode
4576
- if ($mode == 'REWRITE' && !$mapping_again) {
4577
- $productEntityTable = Mage::getSingleton('core/resource')->getTableName('sinch_product_backup');
4578
- }
4579
- // (end) backup Product ID in REWRITE mode
4580
-
4581
- $result = $this->db_do("
4582
- INSERT ignore INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " (
4583
- entity_id,
4584
- sku,
4585
- shop_store_product_id,
4586
- shop_sinch_product_id
4587
- )(SELECT
4588
- entity_id,
4589
- sku,
4590
- store_product_id,
4591
- sinch_product_id
4592
- FROM " . $productEntityTable . "
4593
- )
4594
- ");
4595
-
4596
- $this->addManufacturers(1);
4597
-
4598
- $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " pmt
4599
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . " cpie
4600
- ON pmt.entity_id=cpie.entity_id
4601
- JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
4602
- ON cpie.value=aov.option_id
4603
- SET
4604
- manufacturer_option_id=cpie.value,
4605
- manufacturer_name=aov.value
4606
- WHERE cpie.attribute_id=" . $this->attributes['manufacturer'];
4607
- $this->db_do($q);
4608
-
4609
- $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " pmt
4610
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4611
- ON pmt.sku=p.product_sku
4612
- SET
4613
- pmt.store_product_id=p.store_product_id,
4614
- pmt.sinch_product_id=p.sinch_product_id,
4615
- pmt.product_sku=p.product_sku,
4616
- pmt.sinch_manufacturer_id=p.sinch_manufacturer_id,
4617
- pmt.sinch_manufacturer_name=p.manufacturer_name";
4618
-
4619
- $this->db_do($q);
4620
-
4621
- $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4622
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " pmt
4623
- ON cpe.entity_id=pmt.entity_id
4624
- SET cpe.store_product_id=pmt.store_product_id,
4625
- cpe.sinch_product_id=pmt.sinch_product_id
4626
- WHERE
4627
- cpe.sinch_product_id IS NULL
4628
- AND pmt.sinch_product_id IS NOT NULL
4629
- AND cpe.store_product_id IS NULL
4630
- AND pmt.store_product_id IS NOT NULL";
4631
- $this->db_do($q);
4632
-
4633
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping'));
4634
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . "
4635
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping'));
4636
- }
4637
-
4638
- function addManufacturers($delete_eav = null)
4639
- {
4640
- // this cleanup is not needed due to foreign keys
4641
- if (!$delete_eav) {
4642
- $result = $this->db_do("
4643
- DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . "
4644
- WHERE attribute_id = " . $this->_getProductAttributeId('manufacturer')//." AND store_id = ".$websiteId
4645
- );
4646
- }
4647
- $this->addManufacturer_attribute();
4648
- // todo: doesn't seems to work properly, should be inserted per visibility
4649
- // done, test now
4650
-
4651
- $result = $this->db_do("
4652
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . " (
4653
- entity_id,
4654
- attribute_id,
4655
- store_id,
4656
- value
4657
- )(
4658
- SELECT
4659
- a.entity_id,
4660
- " . $this->_getProductAttributeId('manufacturer') . ",
4661
- w.website,
4662
- mn.shop_option_id
4663
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4664
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4665
- ON a.store_product_id = b.store_product_id
4666
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4667
- ON a.store_product_id=w.store_product_id
4668
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " mn
4669
- ON b.sinch_manufacturer_id=mn.sinch_manufacturer_id
4670
- WHERE mn.shop_option_id IS NOT NULL
4671
- )
4672
- ON DUPLICATE KEY UPDATE
4673
- value = mn.shop_option_id
4674
- ");
4675
-
4676
- $result = $this->db_do("
4677
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . " (
4678
- entity_id,
4679
- attribute_id,
4680
- store_id,
4681
- value
4682
- )(
4683
- SELECT
4684
- a.entity_id,
4685
- " . $this->_getProductAttributeId('manufacturer') . ",
4686
- 0,
4687
- mn.shop_option_id
4688
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4689
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4690
- ON a.store_product_id = b.store_product_id
4691
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4692
- ON a.store_product_id=w.store_product_id
4693
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " mn
4694
- ON b.sinch_manufacturer_id=mn.sinch_manufacturer_id
4695
- WHERE mn.shop_option_id IS NOT NULL
4696
- )
4697
- ON DUPLICATE KEY UPDATE
4698
- value = mn.shop_option_id
4699
- ");
4700
-
4701
-
4702
- }
4703
-
4704
- #################################################################################################
4705
-
4706
- private function _getProductAttributeId($attributeCode)
4707
- {
4708
- return $this->_getAttributeId($attributeCode, 'catalog_product');
4709
- }
4710
-
4711
- #################################################################################################
4712
-
4713
- private function addManufacturer_attribute()
4714
- {
4715
- $result = $this->db_do("
4716
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4717
- entity_type_id,
4718
- attribute_id,
4719
- store_id,
4720
- entity_id,
4721
- value
4722
- )(
4723
- SELECT
4724
- " . $this->_getProductEntityTypeId() . ",
4725
- " . $this->_getProductAttributeId('manufacturer') . ",
4726
- 0,
4727
- a.entity_id,
4728
- pm.manufacturer_option_id
4729
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4730
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4731
- ON a.entity_id = pm.entity_id
4732
- )
4733
- ON DUPLICATE KEY UPDATE
4734
- value = pm.manufacturer_option_id
4735
- ");
4736
-
4737
-
4738
- }
4739
-
4740
- #################################################################################################
4741
-
4742
- public function replaceMagentoProducts()
4743
- {
4744
-
4745
- $connection = Mage::getModel('core/resource')->getConnection('core_write');
4746
-
4747
- $result = $this->db_do("DELETE cpe
4748
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4749
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4750
- ON cpe.entity_id=pm.entity_id
4751
- WHERE pm.shop_store_product_id IS NOT NULL
4752
- AND pm.store_product_id IS NULL
4753
- ");
4754
-
4755
- //Inserting new products and updating old others.
4756
- $this->_getProductDefaulAttributeSetId();
4757
- $result = $this->db_do("
4758
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " (
4759
- entity_id,
4760
- entity_type_id,
4761
- attribute_set_id,
4762
- type_id,
4763
- sku,
4764
- updated_at,
4765
- has_options,
4766
- store_product_id,
4767
- sinch_product_id
4768
- )(SELECT
4769
- pm.entity_id,
4770
- " . $this->_getProductEntityTypeId() . ",
4771
- $this->defaultAttributeSetId,
4772
- 'simple',
4773
- a.product_sku,
4774
- NOW(),
4775
- 0,
4776
- a.store_product_id,
4777
- a.sinch_product_id
4778
- FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " a
4779
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4780
- ON a.store_product_id=pm.store_product_id
4781
- AND a.sinch_product_id=pm.sinch_product_id
4782
- WHERE pm.entity_id IS NOT NULL
4783
- )
4784
- ON DUPLICATE KEY UPDATE
4785
- sku= a.product_sku,
4786
- store_product_id=a.store_product_id,
4787
- sinch_product_id=a.sinch_product_id
4788
- ");
4789
- // store_product_id=a.store_product_id,
4790
- // sinch_product_id=a.sinch_product_id
4791
-
4792
- $result = $this->db_do("
4793
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " (
4794
- entity_id,
4795
- entity_type_id,
4796
- attribute_set_id,
4797
- type_id,
4798
- sku,
4799
- updated_at,
4800
- has_options,
4801
- store_product_id,
4802
- sinch_product_id
4803
- )(SELECT
4804
- pm.entity_id,
4805
- " . $this->_getProductEntityTypeId() . ",
4806
- $this->defaultAttributeSetId,
4807
- 'simple',
4808
- a.product_sku,
4809
- NOW(),
4810
- 0,
4811
- a.store_product_id,
4812
- a.sinch_product_id
4813
- FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " a
4814
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4815
- ON a.store_product_id=pm.store_product_id
4816
- AND a.sinch_product_id=pm.sinch_product_id
4817
- WHERE pm.entity_id IS NULL
4818
- )
4819
- ON DUPLICATE KEY UPDATE
4820
- sku= a.product_sku,
4821
- store_product_id=a.store_product_id,
4822
- sinch_product_id=a.sinch_product_id
4823
- ");
4824
-
4825
- //Set enabled
4826
- $result = $this->db_do("DELETE cpei
4827
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " cpei
4828
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4829
- ON cpei.entity_id=cpe.entity_id
4830
- WHERE cpe.entity_id IS NULL");
4831
-
4832
- $result = $this->db_do("
4833
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4834
- entity_type_id,
4835
- attribute_id,
4836
- store_id,
4837
- entity_id,
4838
- value
4839
- )(
4840
- SELECT
4841
- " . $this->_getProductEntityTypeId() . ",
4842
- " . $this->_getProductAttributeId('status') . ",
4843
- w.website,
4844
- a.entity_id,
4845
- 1
4846
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4847
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4848
- ON a.store_product_id=w.store_product_id
4849
- )
4850
- ON DUPLICATE KEY UPDATE
4851
- value=1
4852
- ");
4853
- // set status = 1 for all stores
4854
- $result = $this->db_do("
4855
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4856
- entity_type_id,
4857
- attribute_id,
4858
- store_id,
4859
- entity_id,
4860
- value
4861
- )(SELECT
4862
- " . $this->_getProductEntityTypeId() . ",
4863
- " . $this->_getProductAttributeId('status') . ",
4864
- 0,
4865
- a.entity_id,
4866
- 1
4867
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4868
- )
4869
- ON DUPLICATE KEY UPDATE
4870
- value=1
4871
- ");
4872
-
4873
- //Unifying products with categories.
4874
- $result = $this->db_do("DELETE ccp
4875
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4876
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4877
- ON ccp.product_id=cpe.entity_id
4878
- WHERE cpe.entity_id IS NULL");
4879
-
4880
- echo("\n\n\nUPDATE IGNORE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4881
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4882
- ON ccp.category_id=cce.entity_id
4883
- SET ccp.category_id=" . $this->_root_cat . "
4884
- WHERE cce.entity_id IS NULL");
4885
- $result = $this->db_do("UPDATE IGNORE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4886
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4887
- ON ccp.category_id=cce.entity_id
4888
- SET ccp.category_id=" . $this->_root_cat . "
4889
- WHERE cce.entity_id IS NULL");
4890
- echo("\ndone\n");
4891
-
4892
-
4893
- echo("\n\n\nDELETE ccp FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4894
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4895
- ON ccp.category_id=cce.entity_id
4896
- WHERE cce.entity_id IS NULL");
4897
- $result = $this->db_do("DELETE ccp FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4898
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4899
- ON ccp.category_id=cce.entity_id
4900
- WHERE cce.entity_id IS NULL");
4901
- echo("\ndone\n");
4902
-
4903
-
4904
- $this->db_do(" DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp");
4905
- // TEMPORARY
4906
- $this->db_do("
4907
- CREATE TABLE `" . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp` (
4908
- `category_id` int(10) unsigned NOT NULL default '0',
4909
- `product_id` int(10) unsigned NOT NULL default '0',
4910
- `store_product_id` int(10) NOT NULL default '0',
4911
- `store_category_id` int(10) NOT NULL default '0',
4912
- `new_category_id` int(10) NOT NULL default '0',
4913
- UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`category_id`,`product_id`),
4914
- KEY `CATALOG_CATEGORY_PRODUCT_CATEGORY` (`category_id`),
4915
- KEY `CATALOG_CATEGORY_PRODUCT_PRODUCT` (`product_id`),
4916
- KEY `CATALOG_NEW_CATEGORY_PRODUCT_CATEGORY` (`new_category_id`)
4917
- )
4918
-
4919
- ");
4920
-
4921
- $result = $this->db_do("
4922
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp (
4923
- category_id,
4924
- product_id,
4925
- store_product_id
4926
- )(SELECT
4927
- ccp.category_id,
4928
- ccp.product_id,
4929
- cpe.store_product_id
4930
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4931
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4932
- ON ccp.product_id=cpe.entity_id
4933
- WHERE store_product_id is not null
4934
- )
4935
- ");
4936
-
4937
- $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp ccpfd
4938
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4939
- ON ccpfd.store_product_id=p.store_product_id
4940
- SET ccpfd.store_category_id=p.store_category_id
4941
- WHERE ccpfd.store_product_id!=0
4942
- ");
4943
-
4944
- $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp ccpfd
4945
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
4946
- ON ccpfd.store_category_id=scm.store_category_id
4947
- SET ccpfd.new_category_id=scm.shop_entity_id
4948
- WHERE ccpfd.store_category_id!=0
4949
- ");
4950
-
4951
- $result = $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp
4952
- WHERE category_id=new_category_id");
4953
-
4954
- $result = $this->db_do("
4955
- DELETE ccp
4956
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4957
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp ccpfd
4958
- ON ccp.product_id=ccpfd.product_id
4959
- AND ccp.category_id=ccpfd.category_id
4960
- ");
4961
-
4962
- $result = $this->db_do("
4963
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " (
4964
- category_id,
4965
- product_id
4966
- )(SELECT
4967
- scm.shop_entity_id,
4968
- cpe.entity_id
4969
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4970
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4971
- ON cpe.store_product_id=p.store_product_id
4972
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
4973
- ON p.store_category_id=scm.store_category_id
4974
- )
4975
- ON DUPLICATE KEY UPDATE
4976
- product_id = cpe.entity_id
4977
- ");
4978
-
4979
-
4980
- //add multi categories;
4981
-
4982
-
4983
- $result = $this->db_do("
4984
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "
4985
- (category_id, product_id)
4986
- (SELECT
4987
- scm.shop_entity_id,
4988
- cpe.entity_id
4989
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4990
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4991
- ON cpe.store_product_id = p.store_product_id
4992
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
4993
- ON p.store_product_id=spc.store_product_id
4994
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
4995
- ON spc.store_category_id = scm.store_category_id
4996
- )
4997
- ON DUPLICATE KEY UPDATE
4998
- product_id = cpe.entity_id
4999
- ");
5000
-
5001
-
5002
- //Indexing products and categories in the shop
5003
- $result = $this->db_do("DELETE ccpi
5004
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index') . " ccpi
5005
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
5006
- ON ccpi.product_id=cpe.entity_id
5007
- WHERE cpe.entity_id IS NULL");
5008
-
5009
- $result = $this->db_do("
5010
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index') . " (
5011
- category_id,
5012
- product_id,
5013
- position,
5014
- is_parent,
5015
- store_id,
5016
- visibility
5017
- )(
5018
- SELECT
5019
- a.category_id,
5020
- a.product_id,
5021
- a.position,
5022
- 1,
5023
- b.store_id,
5024
- 4
5025
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " a
5026
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " b
5027
- )
5028
- ON DUPLICATE KEY UPDATE
5029
- visibility = 4
5030
- ");
5031
-
5032
- $result = $this->db_do("
5033
- INSERT ignore INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index') . " (
5034
- category_id,
5035
- product_id,
5036
- position,
5037
- is_parent,
5038
- store_id,
5039
- visibility
5040
- )(
5041
- SELECT
5042
- " . $this->_root_cat . ",
5043
- a.product_id,
5044
- a.position,
5045
- 1,
5046
- b.store_id,
5047
- 4
5048
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " a
5049
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " b
5050
- )
5051
- ON DUPLICATE KEY UPDATE
5052
- visibility = 4
5053
- ");
5054
-
5055
- //Set product name for specific web sites
5056
- $result = $this->db_do("DELETE cpev
5057
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " cpev
5058
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
5059
- ON cpev.entity_id=cpe.entity_id
5060
- WHERE cpe.entity_id IS NULL");
5061
- $result = $this->db_do("
5062
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5063
- entity_type_id,
5064
- attribute_id,
5065
- store_id,
5066
- entity_id,
5067
- value
5068
- )(SELECT
5069
- " . $this->_getProductEntityTypeId() . ",
5070
- " . $this->_getProductAttributeId('name') . ",
5071
- w.website,
5072
- a.entity_id,
5073
- b.product_name
5074
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5075
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5076
- ON a.store_product_id= b.store_product_id
5077
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5078
- ON a.store_product_id=w.store_product_id
5079
- )
5080
- ON DUPLICATE KEY UPDATE
5081
- value = b.product_name
5082
- ");
5083
-
5084
- // product name for all web sites
5085
- $result = $this->db_do("
5086
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5087
- entity_type_id,
5088
- attribute_id,
5089
- store_id,
5090
- entity_id,
5091
- value
5092
- )(
5093
- SELECT
5094
- " . $this->_getProductEntityTypeId() . ",
5095
- " . $this->_getProductAttributeId('name') . ",
5096
- 0,
5097
- a.entity_id,
5098
- b.product_name
5099
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5100
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5101
- ON a.store_product_id = b.store_product_id
5102
- )
5103
- ON DUPLICATE KEY UPDATE
5104
- value = b.product_name
5105
- ");
5106
-
5107
- $this->dropHTMLentities($this->_getProductEntityTypeId(), $this->_getProductAttributeId('name'));
5108
- $this->addDescriptions();
5109
- $this->cleanProductDistributors();
5110
- if (!$this->_ignore_product_contracts) {
5111
- $this->cleanProductContracts();
5112
- }
5113
- if ($this->product_file_format == "NEW") {
5114
- $this->addReviews();
5115
- $this->addWeight();
5116
- $this->addSearchCache();
5117
- $this->addPdfUrl();
5118
- $this->addShortDescriptions();
5119
- $this->addProductDistributors();
5120
- if (!$this->_ignore_product_contracts) {
5121
- $this->addProductContracts();
5122
- }
5123
- }
5124
- $this->addEAN();
5125
- $this->addSpecification();
5126
- $this->addManufacturers();
5127
-
5128
- //Enabling product index.
5129
- $result = $this->db_do("DELETE cpei
5130
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index') . " cpei
5131
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
5132
- ON cpei.product_id=cpe.entity_id
5133
- WHERE cpe.entity_id IS NULL");
5134
-
5135
- $result = $this->db_do("
5136
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index') . " (
5137
- product_id,
5138
- store_id,
5139
- visibility
5140
- )(
5141
- SELECT
5142
- a.entity_id,
5143
- w.website,
5144
- 4
5145
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5146
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5147
- ON a.store_product_id=w.store_product_id
5148
- )
5149
- ON DUPLICATE KEY UPDATE
5150
- visibility = 4
5151
- ");
5152
- $result = $this->db_do("
5153
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index') . " (
5154
- product_id,
5155
- store_id,
5156
- visibility
5157
- )(
5158
- SELECT
5159
- a.entity_id,
5160
- 0,
5161
- 4
5162
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5163
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5164
- ON a.store_product_id=w.store_product_id
5165
- )
5166
- ON DUPLICATE KEY UPDATE
5167
- visibility = 4
5168
- ");
5169
-
5170
-
5171
- $result = $this->db_do("
5172
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
5173
- entity_type_id,
5174
- attribute_id,
5175
- store_id,
5176
- entity_id,
5177
- value
5178
- )(
5179
- SELECT
5180
- " . $this->_getProductEntityTypeId() . ",
5181
- " . $this->_getProductAttributeId('visibility') . ",
5182
- w.website,
5183
- a.entity_id,
5184
- 4
5185
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5186
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5187
- ON a.store_product_id=w.store_product_id
5188
- )
5189
- ON DUPLICATE KEY UPDATE
5190
- value = 4
5191
- ");
5192
-
5193
- $result = $this->db_do("
5194
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
5195
- entity_type_id,
5196
- attribute_id,
5197
- store_id,
5198
- entity_id,
5199
- value
5200
- )(
5201
- SELECT
5202
- " . $this->_getProductEntityTypeId() . ",
5203
- " . $this->_getProductAttributeId('visibility') . ",
5204
- 0,
5205
- a.entity_id,
5206
- 4
5207
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5208
- )
5209
- ON DUPLICATE KEY UPDATE
5210
- value = 4
5211
- ");
5212
-
5213
- $result = $this->db_do("DELETE cpw
5214
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_website') . " cpw
5215
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
5216
- ON cpw.product_id=cpe.entity_id
5217
- WHERE cpe.entity_id IS NULL");
5218
-
5219
- $result = $this->db_do("
5220
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_website') . " (
5221
- product_id,
5222
- website_id
5223
- )(
5224
- SELECT a.entity_id, w.website_id
5225
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5226
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5227
- ON a.store_product_id=w.store_product_id
5228
- )
5229
- ON DUPLICATE KEY UPDATE
5230
- product_id=a.entity_id,
5231
- website_id=w.website_id
5232
- ");
5233
-
5234
- // temporary disabled mart@bintime.com
5235
- //$result = $this->db_do("
5236
- // UPDATE catalog_category_entity_int a
5237
- // SET a.value = 0
5238
- // WHERE a.attribute_id = 32
5239
- //");
5240
-
5241
-
5242
- //Adding tax class "Taxable Goods"
5243
- $result = $this->db_do("
5244
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
5245
- entity_type_id,
5246
- attribute_id,
5247
- store_id,
5248
- entity_id,
5249
- value
5250
- )(
5251
- SELECT
5252
- " . $this->_getProductEntityTypeId() . ",
5253
- " . $this->_getProductAttributeId('tax_class_id') . ",
5254
- w.website,
5255
- a.entity_id,
5256
- 2
5257
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5258
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5259
- ON a.store_product_id=w.store_product_id
5260
- )
5261
- ON DUPLICATE KEY UPDATE
5262
- value = 2
5263
- ");
5264
- $result = $this->db_do("
5265
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
5266
- entity_type_id,
5267
- attribute_id,
5268
- store_id,
5269
- entity_id,
5270
- value
5271
- )(
5272
- SELECT
5273
- " . $this->_getProductEntityTypeId() . ",
5274
- " . $this->_getProductAttributeId('tax_class_id') . ",
5275
- 0,
5276
- a.entity_id,
5277
- 2
5278
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5279
- )
5280
- ON DUPLICATE KEY UPDATE
5281
- value = 2
5282
- ");
5283
-
5284
- // Load url Image
5285
- $result = $this->db_do("
5286
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5287
- entity_type_id,
5288
- attribute_id,
5289
- store_id,
5290
- entity_id,
5291
- value
5292
- )(
5293
- SELECT
5294
- " . $this->_getProductEntityTypeId() . ",
5295
- " . $this->_getProductAttributeId('image') . ",
5296
- w.store_id,
5297
- a.entity_id,
5298
- b.main_image_url
5299
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5300
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
5301
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5302
- ON a.store_product_id = b.store_product_id
5303
- )
5304
- ON DUPLICATE KEY UPDATE
5305
- value = b.main_image_url
5306
- ");
5307
-
5308
-
5309
- // image for specific web sites
5310
- $result = $this->db_do("
5311
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5312
- entity_type_id,
5313
- attribute_id,
5314
- store_id,
5315
- entity_id,
5316
- value
5317
- )(
5318
- SELECT
5319
- " . $this->_getProductEntityTypeId() . ",
5320
- " . $this->_getProductAttributeId('image') . ",
5321
- 0,
5322
- a.entity_id,
5323
- b.main_image_url
5324
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5325
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5326
- ON a.store_product_id = b.store_product_id
5327
- )
5328
- ON DUPLICATE KEY UPDATE
5329
- value = b.main_image_url
5330
- ");
5331
-
5332
-
5333
- // small_image for specific web sites
5334
- $result = $this->db_do("
5335
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5336
- entity_type_id,
5337
- attribute_id,
5338
- store_id,
5339
- entity_id,
5340
- value
5341
- )(
5342
- SELECT
5343
- " . $this->_getProductEntityTypeId() . ",
5344
- " . $this->_getProductAttributeId('small_image') . ",
5345
- w.store_id,
5346
- a.entity_id,
5347
- b.medium_image_url
5348
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5349
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
5350
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5351
- ON a.store_product_id = b.store_product_id
5352
- )
5353
- ON DUPLICATE KEY UPDATE
5354
- value = b.medium_image_url
5355
- ");
5356
-
5357
-
5358
- // small_image for all web sites
5359
- $result = $this->db_do("
5360
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5361
- entity_type_id,
5362
- attribute_id,
5363
- store_id,
5364
- entity_id,
5365
- value
5366
- )(
5367
- SELECT
5368
- " . $this->_getProductEntityTypeId() . ",
5369
- " . $this->_getProductAttributeId('small_image') . ",
5370
- 0,
5371
- a.entity_id,
5372
- b.medium_image_url
5373
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5374
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
5375
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5376
- ON a.store_product_id = b.store_product_id
5377
- )
5378
- ON DUPLICATE KEY UPDATE
5379
- value = b.medium_image_url
5380
- ");
5381
-
5382
-
5383
- // thumbnail for specific web site
5384
- $result = $this->db_do("
5385
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5386
- entity_type_id,
5387
- attribute_id,
5388
- store_id,
5389
- entity_id,
5390
- value
5391
- )(
5392
- SELECT
5393
- " . $this->_getProductEntityTypeId() . ",
5394
- " . $this->_getProductAttributeId('thumbnail') . ",
5395
- w.store_id,
5396
- a.entity_id,
5397
- b.thumb_image_url
5398
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5399
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
5400
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5401
- ON a.store_product_id = b.store_product_id
5402
- )
5403
- ON DUPLICATE KEY UPDATE
5404
- value = b.thumb_image_url
5405
- ");
5406
-
5407
-
5408
- // thumbnail for all web sites
5409
- $result = $this->db_do("
5410
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5411
- entity_type_id,
5412
- attribute_id,
5413
- store_id,
5414
- entity_id,
5415
- value
5416
- )(
5417
- SELECT
5418
- " . $this->_getProductEntityTypeId() . ",
5419
- " . $this->_getProductAttributeId('thumbnail') . ",
5420
- 0,
5421
- a.entity_id,
5422
- b.thumb_image_url
5423
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5424
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
5425
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5426
- ON a.store_product_id = b.store_product_id
5427
- )
5428
- ON DUPLICATE KEY UPDATE
5429
- value = b.thumb_image_url
5430
-
5431
- ");
5432
-
5433
- /*STP DELETE
5434
- //Refresh fulltext search
5435
- $result = $this->db_do("DROP TABLE IF EXISTS ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp");
5436
- $result = $this->db_do("CREATE TEMPORARY TABLE IF NOT EXISTS
5437
- ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp
5438
- LIKE ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext'));
5439
-
5440
- $result = $this->db_do("
5441
- INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp (
5442
- product_id,
5443
- store_id,
5444
- data_index
5445
- )(
5446
- SELECT
5447
- a.entity_id,
5448
- w.website,
5449
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
5450
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." a
5451
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('products_website_temp')." w
5452
- ON a.store_product_id=w.store_product_id
5453
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product')." b
5454
- ON a.entity_id = b.product_id
5455
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar')." c
5456
- ON b.category_id = c.entity_id
5457
- AND c.attribute_id = " . $this->_getCategoryAttributeId('name'). "
5458
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar')." e
5459
- ON a.entity_id = e.entity_id
5460
- AND e.attribute_id = " . $this->_getProductAttributeId('name'). "
5461
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_website')." j
5462
- ON a.entity_id = j.product_id
5463
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('products_temp')." f
5464
- ON a.store_product_id = f.store_product_id
5465
- )
5466
- ON DUPLICATE KEY UPDATE
5467
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
5468
- ");
5469
-
5470
- $result = $this->db_do("
5471
- INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp (
5472
- product_id,
5473
- store_id,
5474
- data_index
5475
- )(
5476
- SELECT
5477
- a.entity_id,
5478
- w.website,
5479
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
5480
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." a
5481
- INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('products_website_temp')." w
5482
- ON a.store_product_id=w.store_product_id
5483
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product')." b
5484
- ON a.entity_id = b.product_id
5485
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar')." c
5486
- ON b.category_id = c.entity_id
5487
- AND c.attribute_id = " . $this->_getCategoryAttributeId('name'). "
5488
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar')." e
5489
- ON a.entity_id = e.entity_id
5490
- AND e.attribute_id = " . $this->_getProductAttributeId('name'). "
5491
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('products_temp')." f
5492
- ON a.store_product_id = f.store_product_id
5493
- )
5494
- ON DUPLICATE KEY UPDATE
5495
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
5496
- ");
5497
-
5498
- $result = $this->db_do("DELETE cf
5499
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')." cf
5500
- LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." cpe
5501
- ON cf.product_id=cpe.entity_id
5502
- WHERE cpe.entity_id is null");
5503
-
5504
- $result = $this->db_do("
5505
- INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')." (
5506
- product_id,
5507
- store_id,
5508
- data_index
5509
- )(
5510
- SELECT
5511
- a.product_id,
5512
- a.store_id,
5513
- a.data_index
5514
- FROM ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp a
5515
- WHERE product_id = a.product_id
5516
- )
5517
- ON DUPLICATE KEY UPDATE
5518
- data_index = a.data_index
5519
- ");
5520
-
5521
- $this->db_do("UPDATE ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_query')." SET is_processed = 0");
5522
- //INNER JOIN eav_attribute_option_value d ON a.vendor_id = d.option_id
5523
- //TODO add something else
5524
- STP DELETE*/
5525
- $this->addRelatedProducts();
5526
- }
5527
-
5528
- #################################################################################################
5529
-
5530
- private function _getProductDefaulAttributeSetId()
5531
- {
5532
- if (!$this->defaultAttributeSetId) {
5533
- $sql = "
5534
- SELECT entity_type_id, default_attribute_set_id
5535
- FROM " . Mage::getSingleton('core/resource')->getTableName('eav_entity_type') . "
5536
- WHERE entity_type_code = 'catalog_product'
5537
- LIMIT 1
5538
- ";
5539
- $result = $this->db_do($sql);
5540
- if ($row = mysqli_fetch_assoc($result)) {
5541
-
5542
- $this->defaultAttributeSetId = $row['default_attribute_set_id'];
5543
- }
5544
- }
5545
- return $this->defaultAttributeSetId;
5546
- }
5547
-
5548
- #################################################################################################
5549
-
5550
- function dropHTMLentities($entity_type_id, $attribute_id)
5551
- {
5552
- // product name for all web sites
5553
- $result = $this->db_do("
5554
- SELECT value, entity_id
5555
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5556
- WHERE entity_type_id=" . $entity_type_id . "
5557
- AND attribute_id=" . $attribute_id
5558
- );
5559
- while ($row = mysqli_fetch_array($result)) {
5560
- $value = $this->valid_char($row['value']);
5561
- if ($value != '' and $value != $row['value']) {
5562
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5563
- SET value='" . mysqli_real_escape_string($this->db, $value) . "'
5564
- WHERE entity_id=" . $row['entity_id'] . "
5565
- AND entity_type_id=" . $entity_type_id . "
5566
- AND attribute_id=" . $attribute_id);
5567
- }
5568
-
5569
- }
5570
- }
5571
-
5572
- #################################################################################################
5573
-
5574
- function valid_char($string)
5575
- {
5576
- $string = preg_replace('/&#8482;/', ' ', $string);
5577
- $string = preg_replace('/&reg;/', ' ', $string);
5578
- $string = preg_replace('/&asymp;/', ' ', $string);
5579
- $string = preg_replace('/&quot;/', ' ', $string);
5580
- $string = preg_replace('/&prime;/', ' ', $string);
5581
- $string = preg_replace('/&deg;/', ' ', $string);
5582
- $string = preg_replace('/&plusmn;/', ' ', $string);
5583
- $string = preg_replace('/&micro;/', ' ', $string);
5584
- $string = preg_replace('/&sup2;/', ' ', $string);
5585
- $string = preg_replace('/&sup3;/', ' ', $string);
5586
- // $string = preg_replace('/\xe2\x80\x93/','-',$string);
5587
- // $string = preg_replace('/\xe2\x80\x99/','\'',$string);
5588
- // $string = preg_replace('/\xe2\x80\x9c/',' ',$string);
5589
- // $string = preg_replace('/\xe2\x80\x9d/',' ',$string);
5590
-
5591
- // return utf8_decode($string);
5592
-
5593
- return $string;
5594
- }
5595
-
5596
-
5597
- #################################################################################################
5598
-
5599
- function addDescriptions()
5600
- {
5601
- // product description for all web sites
5602
- $result = $this->db_do("
5603
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5604
- entity_type_id,
5605
- attribute_id,
5606
- store_id,
5607
- entity_id,
5608
- value
5609
- )(
5610
- SELECT
5611
- " . $this->_getProductEntityTypeId() . ",
5612
- " . $this->_getProductAttributeId('description') . ",
5613
- w.website,
5614
- a.entity_id,
5615
- b.description
5616
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5617
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5618
- ON a.store_product_id = b.store_product_id
5619
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5620
- ON a.store_product_id=w.store_product_id
5621
- )
5622
- ON DUPLICATE KEY UPDATE
5623
- value = b.description
5624
- ");
5625
-
5626
- // product description for all web sites
5627
- $result = $this->db_do("
5628
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5629
- entity_type_id,
5630
- attribute_id,
5631
- store_id,
5632
- entity_id,
5633
- value
5634
- )(
5635
- SELECT
5636
- " . $this->_getProductEntityTypeId() . ",
5637
- " . $this->_getProductAttributeId('description') . ",
5638
- 0,
5639
- a.entity_id,
5640
- b.description
5641
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5642
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5643
- ON a.store_product_id = b.store_product_id
5644
- )
5645
- ON DUPLICATE KEY UPDATE
5646
- value = b.description
5647
- ");
5648
-
5649
-
5650
- }
5651
-
5652
-
5653
- #################################################################################################
5654
-
5655
- function cleanProductDistributors()
5656
- {
5657
- for ($i = 1; $i <= 5; $i++) {
5658
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5659
- SET value = ''
5660
- WHERE entity_type_id=" . $this->_getProductEntityTypeId() . " AND attribute_id=" . $this->_getProductAttributeId('supplier_' . $i));
5661
- }
5662
- }
5663
-
5664
- #################################################################################################
5665
-
5666
- function cleanProductContracts()
5667
- {
5668
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5669
- SET value = ''
5670
- WHERE entity_type_id=" . $this->_getProductEntityTypeId() . " AND attribute_id=" . $this->_getProductAttributeId('contract_id'));
5671
- }
5672
-
5673
- #################################################################################################
5674
-
5675
- function addReviews()
5676
- {
5677
- // product reviews for all web sites
5678
- $result = $this->db_do("
5679
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5680
- entity_type_id,
5681
- attribute_id,
5682
- store_id,
5683
- entity_id,
5684
- value
5685
- )(
5686
- SELECT
5687
- " . $this->_getProductEntityTypeId() . ",
5688
- " . $this->_getProductAttributeId('reviews') . ",
5689
- w.website,
5690
- a.entity_id,
5691
- b.Reviews
5692
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5693
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5694
- ON a.store_product_id = b.store_product_id
5695
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5696
- ON a.store_product_id=w.store_product_id
5697
- )
5698
- ON DUPLICATE KEY UPDATE
5699
- value = b.Reviews
5700
- ");
5701
-
5702
- // product Reviews for all web sites
5703
- $result = $this->db_do("
5704
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5705
- entity_type_id,
5706
- attribute_id,
5707
- store_id,
5708
- entity_id,
5709
- value
5710
- )(
5711
- SELECT
5712
- " . $this->_getProductEntityTypeId() . ",
5713
- " . $this->_getProductAttributeId('reviews') . ",
5714
- 0,
5715
- a.entity_id,
5716
- b.Reviews
5717
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5718
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5719
- ON a.store_product_id = b.store_product_id
5720
- )
5721
- ON DUPLICATE KEY UPDATE
5722
- value = b.Reviews
5723
- ");
5724
-
5725
-
5726
- }
5727
-
5728
- #################################################################################################
5729
-
5730
- function addWeight()
5731
- {
5732
- // product weight for specific web site
5733
- $result = $this->db_do("
5734
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
5735
- entity_type_id,
5736
- attribute_id,
5737
- store_id,
5738
- entity_id,
5739
- value
5740
- )(
5741
- SELECT
5742
- " . $this->_getProductEntityTypeId() . ",
5743
- " . $this->_getProductAttributeId('weight') . ",
5744
- w.website,
5745
- a.entity_id,
5746
- b.Weight
5747
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5748
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5749
- ON a.store_product_id = b.store_product_id
5750
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5751
- ON a.store_product_id=w.store_product_id
5752
- )
5753
- ON DUPLICATE KEY UPDATE
5754
- value = b.Weight
5755
- ");
5756
- // product weight for all web sites
5757
- $result = $this->db_do("
5758
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
5759
- entity_type_id,
5760
- attribute_id,
5761
- store_id,
5762
- entity_id,
5763
- value
5764
- )(
5765
- SELECT
5766
- " . $this->_getProductEntityTypeId() . ",
5767
- " . $this->_getProductAttributeId('weight') . ",
5768
- 0,
5769
- a.entity_id,
5770
- b.Weight
5771
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5772
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5773
- ON a.store_product_id = b.store_product_id
5774
- )
5775
- ON DUPLICATE KEY UPDATE
5776
- value = b.Weight
5777
-
5778
-
5779
- ");
5780
-
5781
-
5782
- }
5783
-
5784
- #################################################################################################
5785
-
5786
- function addSearchCache()
5787
- {
5788
- // product search_cache for all web sites
5789
- $result = $this->db_do("
5790
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5791
- entity_type_id,
5792
- attribute_id,
5793
- store_id,
5794
- entity_id,
5795
- value
5796
- )(
5797
- SELECT
5798
- " . $this->_getProductEntityTypeId() . ",
5799
- " . $this->_getProductAttributeId('sinch_search_cache') . ",
5800
- w.website,
5801
- a.entity_id,
5802
- b.search_cache
5803
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5804
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5805
- ON a.store_product_id = b.store_product_id
5806
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5807
- ON a.store_product_id=w.store_product_id
5808
- )
5809
- ON DUPLICATE KEY UPDATE
5810
- value = b.search_cache
5811
- ");
5812
-
5813
- // product search_cache for all web sites
5814
- $result = $this->db_do("
5815
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5816
- entity_type_id,
5817
- attribute_id,
5818
- store_id,
5819
- entity_id,
5820
- value
5821
- )(
5822
- SELECT
5823
- " . $this->_getProductEntityTypeId() . ",
5824
- " . $this->_getProductAttributeId('sinch_search_cache') . ",
5825
- 0,
5826
- a.entity_id,
5827
- b.search_cache
5828
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5829
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5830
- ON a.store_product_id = b.store_product_id
5831
- )
5832
- ON DUPLICATE KEY UPDATE
5833
- value = b.search_cache
5834
- ");
5835
-
5836
-
5837
- }
5838
-
5839
- #################################################################################################
5840
-
5841
- function addPdfUrl()
5842
- {
5843
- // product PDF Url for all web sites
5844
- $result = $this->db_do("
5845
- UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
5846
- SET pdf_url = CONCAT(
5847
- '<a href=\"#\" onclick=\"popWin(',
5848
- \"'\",
5849
- pdf_url,
5850
- \"'\",
5851
- \", 'pdf', 'width=500,height=800,left=50,top=50, location=no,status=yes,scrollbars=yes,resizable=yes'); return false;\",
5852
- '\"',
5853
- '>',
5854
- pdf_url,
5855
- '</a>')
5856
- WHERE pdf_url != ''
5857
- ");
5858
- //<a title="" onclick="popWin('http://images.icecat.biz/img/gallery/14532248_4539.jpg', 'gallery', 'width=500,height=500,left=50,top=50,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;" href="#">
5859
- $result = $this->db_do("
5860
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5861
- entity_type_id,
5862
- attribute_id,
5863
- store_id,
5864
- entity_id,
5865
- value
5866
- )(
5867
- SELECT
5868
- " . $this->_getProductEntityTypeId() . ",
5869
- " . $this->_getProductAttributeId('pdf_url') . ",
5870
- w.website,
5871
- a.entity_id,
5872
- b.pdf_url
5873
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5874
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5875
- ON a.store_product_id = b.store_product_id
5876
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5877
- ON a.store_product_id=w.store_product_id
5878
- )
5879
- ON DUPLICATE KEY UPDATE
5880
- value = b.pdf_url
5881
- ");
5882
- // product PDF url for all web sites
5883
- $result = $this->db_do("
5884
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5885
- entity_type_id,
5886
- attribute_id,
5887
- store_id,
5888
- entity_id,
5889
- value
5890
- )(
5891
- SELECT
5892
- " . $this->_getProductEntityTypeId() . ",
5893
- " . $this->_getProductAttributeId('pdf_url') . ",
5894
- 0,
5895
- a.entity_id,
5896
- b.pdf_url
5897
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5898
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5899
- ON a.store_product_id = b.store_product_id
5900
- )
5901
- ON DUPLICATE KEY UPDATE
5902
- value = b.pdf_url
5903
- ");
5904
-
5905
- }
5906
-
5907
- #################################################################################################
5908
-
5909
- function addShortDescriptions()
5910
- {
5911
- // product short description for all web sites
5912
- $result = $this->db_do("
5913
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5914
- entity_type_id,
5915
- attribute_id,
5916
- store_id,
5917
- entity_id,
5918
- value
5919
- )(
5920
- SELECT
5921
- " . $this->_getProductEntityTypeId() . ",
5922
- " . $this->_getProductAttributeId('short_description') . ",
5923
- w.website,
5924
- a.entity_id,
5925
- b.product_short_description
5926
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5927
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5928
- ON a.store_product_id = b.store_product_id
5929
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5930
- ON a.store_product_id=w.store_product_id
5931
- )
5932
- ON DUPLICATE KEY UPDATE
5933
- value = b.product_short_description
5934
- ");
5935
- // product short description for all web sites
5936
- $result = $this->db_do("
5937
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5938
- entity_type_id,
5939
- attribute_id,
5940
- store_id,
5941
- entity_id,
5942
- value
5943
- )(
5944
- SELECT
5945
- " . $this->_getProductEntityTypeId() . ",
5946
- " . $this->_getProductAttributeId('short_description') . ",
5947
- 0,
5948
- a.entity_id,
5949
- b.product_short_description
5950
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5951
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5952
- ON a.store_product_id = b.store_product_id
5953
- )
5954
- ON DUPLICATE KEY UPDATE
5955
- value = b.product_short_description
5956
- ");
5957
-
5958
- }
5959
-
5960
- #################################################################################################
5961
-
5962
- function addProductDistributors()
5963
- {
5964
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary'));
5965
- $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . "
5966
- LIKE " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
5967
- $this->db_do("INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . " SELECT * FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
5968
- for ($i = 1; $i <= 5; $i++) {
5969
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier'));
5970
- $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . "
5971
- LIKE " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
5972
- $this->db_do("INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " SELECT * FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . " GROUP BY store_product_id");
5973
-
5974
- // product Distributors for all web sites
5975
- $result = $this->db_do("
5976
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5977
- entity_type_id,
5978
- attribute_id,
5979
- store_id,
5980
- entity_id,
5981
- value
5982
- )(
5983
- SELECT
5984
- " . $this->_getProductEntityTypeId() . ",
5985
- " . $this->_getProductAttributeId('supplier_' . $i) . ",
5986
- w.website,
5987
- a.entity_id,
5988
- d.distributor_name
5989
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5990
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " b
5991
- ON a.store_product_id = b.store_product_id
5992
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors') . " d
5993
- ON b.distributor_id = d.distributor_id
5994
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5995
- ON a.store_product_id=w.store_product_id
5996
- )
5997
- ON DUPLICATE KEY UPDATE
5998
- value = d.distributor_name
5999
- ");
6000
- // product Distributors for all web sites
6001
- $result = $this->db_do("
6002
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
6003
- entity_type_id,
6004
- attribute_id,
6005
- store_id,
6006
- entity_id,
6007
- value
6008
- )(
6009
- SELECT
6010
- " . $this->_getProductEntityTypeId() . ",
6011
- " . $this->_getProductAttributeId('supplier_' . $i) . ",
6012
- 0,
6013
- a.entity_id,
6014
- d.distributor_name
6015
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6016
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " b
6017
- ON a.store_product_id = b.store_product_id
6018
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors') . " d
6019
- ON b.distributor_id = d.distributor_id
6020
- )
6021
- ON DUPLICATE KEY UPDATE
6022
- value = d.distributor_name
6023
- ");
6024
-
6025
- $this->db_do("DELETE sdsapt FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . " sdsapt JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " sdsapts ON sdsapt.store_product_id = sdsapts.store_product_id AND sdsapt.distributor_id = sdsapts.distributor_id");
6026
-
6027
-
6028
- }
6029
-
6030
- }
6031
-
6032
- #################################################################################################
6033
-
6034
- function addProductContracts()
6035
- {
6036
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary'));
6037
- $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . "(
6038
- `store_product_id` int(11) DEFAULT NULL,
6039
- `contract_id_str` varchar(255) DEFAULT NULL,
6040
- KEY `store_product_id` (store_product_id)
6041
- )
6042
- ");
6043
- $this->db_do("INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . " SELECT store_product_id, group_concat(contract_id) FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts') . " GROUP BY store_product_id");
6044
- // product Distributors for all web sites
6045
- $result = $this->db_do("
6046
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
6047
- entity_type_id,
6048
- attribute_id,
6049
- store_id,
6050
- entity_id,
6051
- value
6052
- )(
6053
- SELECT
6054
- " . $this->_getProductEntityTypeId() . ",
6055
- " . $this->_getProductAttributeId('contract_id') . ",
6056
- w.website,
6057
- a.entity_id,
6058
- b.contract_id_str
6059
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6060
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . " b
6061
- ON a.store_product_id = b.store_product_id
6062
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
6063
- ON a.store_product_id=w.store_product_id
6064
- )
6065
- ON DUPLICATE KEY UPDATE
6066
- value = b.contract_id_str
6067
- ");
6068
- // product Distributors for all web sites
6069
- $result = $this->db_do("
6070
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
6071
- entity_type_id,
6072
- attribute_id,
6073
- store_id,
6074
- entity_id,
6075
- value
6076
- )(
6077
- SELECT
6078
- " . $this->_getProductEntityTypeId() . ",
6079
- " . $this->_getProductAttributeId('contract_id') . ",
6080
- 0,
6081
- a.entity_id,
6082
- b.contract_id_str
6083
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6084
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . " b
6085
- ON a.store_product_id = b.store_product_id
6086
- )
6087
- ON DUPLICATE KEY UPDATE
6088
- value = b.contract_id_str
6089
- ");
6090
-
6091
-
6092
- }
6093
-
6094
- #################################################################################################
6095
-
6096
- function addEAN()
6097
- {
6098
- //gather EAN codes for each product
6099
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('EANs_temp'));
6100
- $this->db_do("
6101
- CREATE TEMPORARY TABLE " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " (
6102
- sinch_product_id int(11),
6103
- store_product_id int(11),
6104
- EANs text,
6105
- KEY `sinch_product_id` (`sinch_product_id`),
6106
- KEY `store_product_id` (`store_product_id`)
6107
- )
6108
- ");
6109
- $this->db_do("
6110
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " (
6111
- sinch_product_id,
6112
- EANs
6113
- )(SELECT
6114
- sec.product_id,
6115
- GROUP_CONCAT(DISTINCT ean_code ORDER BY ean_code DESC SEPARATOR ', ') AS eans
6116
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes') . " sec
6117
- GROUP BY sec.product_id
6118
- )
6119
- ");
6120
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " e
6121
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
6122
- ON e.sinch_product_id=p.sinch_product_id
6123
- SET e.store_product_id=p.store_product_id");
6124
- // product EANs for all web sites
6125
- $result = $this->db_do("
6126
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
6127
- entity_type_id,
6128
- attribute_id,
6129
- store_id,
6130
- entity_id,
6131
- value
6132
- )(
6133
- SELECT
6134
- " . $this->_getProductEntityTypeId() . ",
6135
- " . $this->_getProductAttributeId('ean') . ",
6136
- w.website,
6137
- a.entity_id,
6138
- e.EANs
6139
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6140
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " e
6141
- ON a.store_product_id = e.store_product_id
6142
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
6143
- ON a.store_product_id=w.store_product_id
6144
- )
6145
- ON DUPLICATE KEY UPDATE
6146
- value = e.EANs
6147
- ");
6148
-
6149
- // product EANs for all web sites
6150
- $result = $this->db_do("
6151
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
6152
- entity_type_id,
6153
- attribute_id,
6154
- store_id,
6155
- entity_id,
6156
- value
6157
- )(
6158
- SELECT
6159
- " . $this->_getProductEntityTypeId() . ",
6160
- " . $this->_getProductAttributeId('ean') . ",
6161
- 0,
6162
- a.entity_id,
6163
- e.EANs
6164
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6165
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " e
6166
- ON a.store_product_id = e.store_product_id
6167
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
6168
- ON a.store_product_id=w.store_product_id
6169
- )
6170
- ON DUPLICATE KEY UPDATE
6171
- value = e.EANs
6172
- ");
6173
-
6174
- }
6175
-
6176
- #################################################################################################
6177
-
6178
- function addSpecification()
6179
- {
6180
- // product specification for all web sites
6181
- $result = $this->db_do("
6182
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
6183
- entity_type_id,
6184
- attribute_id,
6185
- store_id,
6186
- entity_id,
6187
- value
6188
- )(
6189
- SELECT
6190
- " . $this->_getProductEntityTypeId() . ",
6191
- " . $this->_getProductAttributeId('specification') . ",
6192
- w.website,
6193
- a.entity_id,
6194
- b.specifications
6195
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6196
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
6197
- ON a.store_product_id = b.store_product_id
6198
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
6199
- ON a.store_product_id=w.store_product_id
6200
- )
6201
- ON DUPLICATE KEY UPDATE
6202
- value = b.specifications
6203
- ");
6204
- // product specification for all web sites
6205
- $result = $this->db_do("
6206
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
6207
- entity_type_id,
6208
- attribute_id,
6209
- store_id,
6210
- entity_id,
6211
- value
6212
- )(
6213
- SELECT
6214
- " . $this->_getProductEntityTypeId() . ",
6215
- " . $this->_getProductAttributeId('specification') . ",
6216
- 0,
6217
- a.entity_id,
6218
- b.specifications
6219
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
6220
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
6221
- ON a.store_product_id = b.store_product_id
6222
- )
6223
- ON DUPLICATE KEY UPDATE
6224
- value = b.specifications
6225
- ");
6226
-
6227
-
6228
- }
6229
-
6230
- ################################################################################################
6231
-
6232
- function addRelatedProducts()
6233
- {
6234
-
6235
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . " rpt
6236
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
6237
- ON rpt.sinch_product_id=p.sinch_product_id
6238
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
6239
- ON p.store_product_id=cpe.store_product_id
6240
- SET rpt.store_product_id=p.store_product_id, rpt.entity_id=cpe.entity_id");
6241
-
6242
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . " rpt
6243
- JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
6244
- ON rpt.related_sinch_product_id=p.sinch_product_id
6245
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
6246
- ON p.store_product_id=cpe.store_product_id
6247
- SET rpt.store_related_product_id=p.store_product_id, rpt.related_entity_id=cpe.entity_id");
6248
-
6249
- $result = $this->db_do("SELECT
6250
- link_type_id,
6251
- code
6252
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_type')
6253
- );
6254
- $link_type = array();
6255
- while ($row = mysqli_fetch_array($result)) {
6256
- $link_type[$row['code']] = $row['link_type_id'];
6257
- }
6258
-
6259
- $result = $this->db_do("
6260
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link') . " (
6261
- product_id,
6262
- linked_product_id,
6263
- link_type_id
6264
- )(
6265
- SELECT
6266
- entity_id,
6267
- related_entity_id,
6268
- " . $link_type['relation'] . "
6269
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . "
6270
- WHERE store_product_id IS NOT NULL
6271
- AND store_related_product_id IS NOT NULL
6272
- )
6273
- ON DUPLICATE KEY UPDATE
6274
- product_id = entity_id,
6275
- linked_product_id = related_entity_id
6276
- ");
6277
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp");
6278
-
6279
- $this->db_do("CREATE TEMPORARY TABLE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp (
6280
- `value_id` int(11) default NULL,
6281
- `product_link_attribute_id` smallint(6) unsigned default NULL,
6282
- `link_id` int(11) unsigned default NULL,
6283
- `value` int(11) NOT NULL default '0',
6284
- KEY `FK_INT_PRODUCT_LINK_ATTRIBUTE` (`product_link_attribute_id`),
6285
- KEY `FK_INT_PRODUCT_LINK` (`link_id`)
6286
- )
6287
- ");
6288
-
6289
- $result = $this->db_do("
6290
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp(
6291
- product_link_attribute_id,
6292
- link_id,
6293
- value
6294
- )(
6295
- SELECT
6296
- 2,
6297
- cpl.link_id,
6298
- 0
6299
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link') . " cpl
6300
- )
6301
- ");
6302
-
6303
- $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp ct
6304
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . " c
6305
- ON ct.link_id=c.link_id
6306
- SET ct.value_id=c.value_id
6307
- WHERE c.product_link_attribute_id=2
6308
- ");
6309
-
6310
- $result = $this->db_do("
6311
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . " (
6312
- value_id,
6313
- product_link_attribute_id,
6314
- link_id,
6315
- value
6316
- )(
6317
- SELECT
6318
- value_id,
6319
- product_link_attribute_id,
6320
- link_id,
6321
- value
6322
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp ct
6323
- )
6324
- ON DUPLICATE KEY UPDATE
6325
- link_id=ct.link_id
6326
-
6327
- ");
6328
-
6329
- /* $q="select distinct store_product_id from stINch_related_products";
6330
- $quer=$this->db_do($q);
6331
- $prod = Mage::getModel('catalog/product');
6332
- while ($row = mysqli_fetch_assoc($quer)) {
6333
- $q1="select distinct store_related_product_id store_product_id from stINch_related_products where store_product_id=".$row['store_product_id'].;
6334
- $quer1=$this->db_do($q1);
6335
- $prod->load($row['store_product_id']);
6336
-
6337
- ###//get related product data (product id's and positions)
6338
- ###$relatedData = array();
6339
- ###foreach ($product->getRelatedLinkCollection() as $link) {
6340
- ### $relatedData[$link->getLinkedProductId()]['position'] = $link->getPosition();
6341
- ###}
6342
- ###//manipulate $relatedData array
6343
- ###// ...
6344
- ###//set and save related product data
6345
- ###$product->setRelatedLinkData($relatedData);
6346
- ###$product->save();
6347
- ###
6348
- $i=1;
6349
- while ($row1 = mysqli_fetch_assoc($quer1)) {
6350
- $param[$row1['store_related_product_id']]['position']=$i++;
6351
-
6352
- }
6353
- $prod->setRelatedLinkData($param);
6354
- //here ... some other product operations and in the end
6355
- $prod->save();
6356
-
6357
- }
6358
- */
6359
- }
6360
-
6361
- #################################################################################################
6362
-
6363
- public function replaceMagentoProductsMultistore($coincidence)
6364
- {
6365
-
6366
- echo("\n replaceMagentoProductsMultistore 1\n");
6367
-
6368
-
6369
- $connection = Mage::getModel('core/resource')->getConnection('core_write');
6370
-
6371
-
6372
- $products_temp = Mage::getSingleton('core/resource')->getTableName('products_temp');
6373
- $products_website_temp = Mage::getSingleton('core/resource')->getTableName('products_website_temp');
6374
- $catalog_product_entity = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
6375
- $catalog_product_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int');
6376
- $catalog_product_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar');
6377
- $catalog_category_product = Mage::getSingleton('core/resource')->getTableName('catalog_category_product');
6378
- $stINch_products_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping');
6379
- $catalog_category_entity = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity');
6380
- $stINch_categories_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping');
6381
- $catalog_category_product_index = Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index');
6382
- $core_store = Mage::getSingleton('core/resource')->getTableName('core_store');
6383
- $catalog_product_enabled_index = Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index');
6384
- $catalog_product_website = Mage::getSingleton('core/resource')->getTableName('catalog_product_website');
6385
- //STP DELETE $catalogsearch_fulltext = Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext');
6386
- // $catalogsearch_query = Mage::getSingleton('core/resource')->getTableName('catalogsearch_query');
6387
- $catalog_category_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar');
6388
-
6389
- $_getProductEntityTypeId = $this->_getProductEntityTypeId();
6390
- $_defaultAttributeSetId = $this->_getProductDefaulAttributeSetId();
6391
-
6392
- $attr_atatus = $this->_getProductAttributeId('status');
6393
- $attr_name = $this->_getProductAttributeId('name');
6394
- $attr_visibility = $this->_getProductAttributeId('visibility');
6395
- $attr_tax_class_id = $this->_getProductAttributeId('tax_class_id');
6396
- $attr_image = $this->_getProductAttributeId('image');
6397
- $attr_small_image = $this->_getProductAttributeId('small_image');
6398
- $attr_thumbnail = $this->_getProductAttributeId('thumbnail');
6399
-
6400
- $cat_attr_name = $this->_getCategoryAttributeId('name');
6401
- echo("\n replaceMagentoProductsMultistore 2\n");
6402
-
6403
-
6404
- //clear products, inserting new products and updating old others.
6405
- $query = "
6406
- DELETE cpe
6407
- FROM $catalog_product_entity cpe
6408
- JOIN $stINch_products_mapping pm
6409
- ON cpe.entity_id = pm.entity_id
6410
- WHERE pm.shop_store_product_id IS NOT NULL
6411
- AND pm.store_product_id IS NULL";
6412
- $result = $this->db_do($query);
6413
-
6414
-
6415
- echo("\n replaceMagentoProductsMultistore 3\n");
6416
-
6417
- $result = $this->db_do("
6418
- INSERT INTO $catalog_product_entity
6419
- (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
6420
- (SELECT
6421
- pm.entity_id,
6422
- $_getProductEntityTypeId,
6423
- $_defaultAttributeSetId,
6424
- 'simple',
6425
- a.product_sku,
6426
- NOW(),
6427
- 0,
6428
- a.store_product_id,
6429
- a.sinch_product_id
6430
- FROM $products_temp a
6431
- LEFT JOIN $stINch_products_mapping pm
6432
- ON a.store_product_id = pm.store_product_id
6433
- AND a.sinch_product_id = pm.sinch_product_id
6434
- WHERE pm.entity_id IS NULL
6435
- )
6436
- ON DUPLICATE KEY UPDATE
6437
- sku = a.product_sku,
6438
- store_product_id = a.store_product_id,
6439
- sinch_product_id = a.sinch_product_id");
6440
- // store_product_id = a.store_product_id,
6441
- // sinch_product_id = a.sinch_product_id
6442
-
6443
- $result = $this->db_do("
6444
- INSERT INTO $catalog_product_entity
6445
- (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
6446
- (SELECT
6447
- pm.entity_id,
6448
- $_getProductEntityTypeId,
6449
- $_defaultAttributeSetId,
6450
- 'simple',
6451
- a.product_sku,
6452
- NOW(),
6453
- 0,
6454
- a.store_product_id,
6455
- a.sinch_product_id
6456
- FROM $products_temp a
6457
- LEFT JOIN $stINch_products_mapping pm
6458
- ON a.store_product_id = pm.store_product_id
6459
- AND a.sinch_product_id = pm.sinch_product_id
6460
- WHERE pm.entity_id IS NOT NULL
6461
- )
6462
- ON DUPLICATE KEY UPDATE
6463
- sku = a.product_sku,
6464
- store_product_id = a.store_product_id,
6465
- sinch_product_id = a.sinch_product_id");
6466
- // store_product_id = a.store_product_id,
6467
- // sinch_product_id = a.sinch_product_id
6468
-
6469
- echo("\n replaceMagentoProductsMultistore 4\n");
6470
-
6471
-
6472
- //Set enabled
6473
- $result = $this->db_do("
6474
- DELETE cpei
6475
- FROM $catalog_product_entity_int cpei
6476
- LEFT JOIN $catalog_product_entity cpe
6477
- ON cpei.entity_id = cpe.entity_id
6478
- WHERE cpe.entity_id IS NULL");
6479
-
6480
- $result = $this->db_do("
6481
- INSERT INTO $catalog_product_entity_int
6482
- (entity_type_id, attribute_id, store_id, entity_id, value)
6483
- (SELECT
6484
- $_getProductEntityTypeId,
6485
- $attr_atatus,
6486
- w.website,
6487
- a.entity_id,
6488
- 1
6489
- FROM $catalog_product_entity a
6490
- JOIN $products_website_temp w
6491
- ON a.store_product_id = w.store_product_id
6492
- )
6493
- ON DUPLICATE KEY UPDATE
6494
- value = 1");
6495
-
6496
-
6497
- echo("\n replaceMagentoProductsMultistore 5\n");
6498
-
6499
-
6500
- // set status = 1 for all stores
6501
- $result = $this->db_do("
6502
- INSERT INTO $catalog_product_entity_int
6503
- (entity_type_id, attribute_id, store_id, entity_id, value)
6504
- (SELECT
6505
- $_getProductEntityTypeId,
6506
- $attr_atatus,
6507
- 0,
6508
- a.entity_id,
6509
- 1
6510
- FROM $catalog_product_entity a
6511
- )
6512
- ON DUPLICATE KEY UPDATE
6513
- value = 1");
6514
-
6515
-
6516
- echo("\n replaceMagentoProductsMultistore 6\n");
6517
-
6518
-
6519
- //Unifying products with categories.
6520
- $result = $this->db_do("
6521
- DELETE ccp
6522
- FROM $catalog_category_product ccp
6523
- LEFT JOIN $catalog_product_entity cpe
6524
- ON ccp.product_id = cpe.entity_id
6525
- WHERE cpe.entity_id IS NULL");
6526
-
6527
-
6528
- echo("\n replaceMagentoProductsMultistore 7\n");
6529
-
6530
-
6531
- $root_cats = Mage::getSingleton('core/resource')->getTableName('root_cats');
6532
-
6533
-
6534
- $result = $this->db_do("DROP TABLE IF EXISTS $root_cats");
6535
- $result = $this->db_do("
6536
- CREATE TABLE $root_cats
6537
- SELECT
6538
- entity_id,
6539
- path,
6540
- SUBSTRING(path, LOCATE('/', path)+1) AS short_path,
6541
- LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1)) AS end_pos,
6542
- SUBSTRING(SUBSTRING(path, LOCATE('/', path)+1), 1, LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1))-1) as root_cat
6543
- FROM $catalog_category_entity
6544
- ");
6545
- $result = $this->db_do("UPDATE $root_cats SET root_cat = entity_id WHERE CHAR_LENGTH(root_cat) = 0");
6546
-
6547
-
6548
- echo("\n replaceMagentoProductsMultistore 8\n");
6549
-
6550
-
6551
- // !!! $this->_root_cat
6552
- $result = $this->db_do("
6553
- UPDATE IGNORE $catalog_category_product ccp
6554
- LEFT JOIN $catalog_category_entity cce
6555
- ON ccp.category_id = cce.entity_id
6556
- JOIN $root_cats rc
6557
- ON cce.entity_id = rc.entity_id
6558
- SET ccp.category_id = rc.root_cat
6559
- WHERE cce.entity_id IS NULL");
6560
-
6561
-
6562
- echo("\n replaceMagentoProductsMultistore 9\n");
6563
-
6564
-
6565
- $result = $this->db_do("
6566
- DELETE ccp
6567
- FROM $catalog_category_product ccp
6568
- LEFT JOIN $catalog_category_entity cce
6569
- ON ccp.category_id = cce.entity_id
6570
- WHERE cce.entity_id IS NULL");
6571
-
6572
-
6573
- //echo("\n\nget out...\n\n");
6574
- //return;
6575
-
6576
-
6577
- echo("\n replaceMagentoProductsMultistore 10\n");
6578
-
6579
-
6580
- $catalog_category_product_for_delete_temp = $catalog_category_product . "_for_delete_temp";
6581
-
6582
- // TEMPORARY
6583
- $this->db_do(" DROP TABLE IF EXISTS $catalog_category_product_for_delete_temp");
6584
- $this->db_do("
6585
- CREATE TABLE $catalog_category_product_for_delete_temp
6586
- (
6587
- `category_id` int(10) unsigned NOT NULL default '0',
6588
- `product_id` int(10) unsigned NOT NULL default '0',
6589
- `store_product_id` int(10) NOT NULL default '0',
6590
- `store_category_id` int(10) NOT NULL default '0',
6591
- `new_category_id` int(10) NOT NULL default '0',
6592
-
6593
- UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`category_id`,`product_id`),
6594
- KEY `CATALOG_CATEGORY_PRODUCT_CATEGORY` (`category_id`),
6595
- KEY `CATALOG_CATEGORY_PRODUCT_PRODUCT` (`product_id`),
6596
- KEY `CATALOG_NEW_CATEGORY_PRODUCT_CATEGORY` (`new_category_id`)
6597
- )");
6598
-
6599
- echo("\n replaceMagentoProductsMultistore 11\n");
6600
-
6601
- $result = $this->db_do("
6602
- INSERT INTO $catalog_category_product_for_delete_temp
6603
- (category_id, product_id, store_product_id)
6604
- (SELECT
6605
- ccp.category_id,
6606
- ccp.product_id,
6607
- cpe.store_product_id
6608
- FROM $catalog_category_product ccp
6609
- JOIN $catalog_product_entity cpe
6610
- ON ccp.product_id = cpe.entity_id
6611
- WHERE store_product_id IS NOT NULL)");
6612
-
6613
- echo("\n replaceMagentoProductsMultistore 12\n");
6614
-
6615
- $result = $this->db_do("
6616
- UPDATE $catalog_category_product_for_delete_temp ccpfd
6617
- JOIN $products_temp p
6618
- ON ccpfd.store_product_id = p.store_product_id
6619
- SET ccpfd.store_category_id = p.store_category_id
6620
- WHERE ccpfd.store_product_id != 0");
6621
-
6622
- echo("\n replaceMagentoProductsMultistore 13\n");
6623
-
6624
- $result = $this->db_do("
6625
- UPDATE $catalog_category_product_for_delete_temp ccpfd
6626
- JOIN $stINch_categories_mapping scm
6627
- ON ccpfd.store_category_id = scm.store_category_id
6628
- SET ccpfd.new_category_id = scm.shop_entity_id
6629
- WHERE ccpfd.store_category_id != 0");
6630
-
6631
- echo("\n replaceMagentoProductsMultistore 14\n");
6632
-
6633
- $result = $this->db_do("DELETE FROM $catalog_category_product_for_delete_temp WHERE category_id = new_category_id");
6634
-
6635
-
6636
- $result = $this->db_do("
6637
- DELETE ccp
6638
- FROM $catalog_category_product ccp
6639
- JOIN $catalog_category_product_for_delete_temp ccpfd
6640
- ON ccp.product_id = ccpfd.product_id
6641
- AND ccp.category_id = ccpfd.category_id");
6642
-
6643
-
6644
- //echo("\n\nget out...\n\n");
6645
- //return;
6646
-
6647
-
6648
- echo("\n replaceMagentoProductsMultistore 15\n");
6649
-
6650
- $result = $this->db_do("
6651
- INSERT INTO $catalog_category_product
6652
- (category_id, product_id)
6653
- (SELECT
6654
- scm.shop_entity_id,
6655
- cpe.entity_id
6656
- FROM $catalog_product_entity cpe
6657
- JOIN $products_temp p
6658
- ON cpe.store_product_id = p.store_product_id
6659
- JOIN $stINch_categories_mapping scm
6660
- ON p.store_category_id = scm.store_category_id
6661
- )
6662
- ON DUPLICATE KEY UPDATE
6663
- product_id = cpe.entity_id");
6664
-
6665
-
6666
- echo("\n replaceMagentoProductsMultistore 15.1 (add multi categories)\n");
6667
-
6668
- $result = $this->db_do("
6669
- INSERT INTO $catalog_category_product
6670
- (category_id, product_id)
6671
- (SELECT
6672
- scm.shop_entity_id,
6673
- cpe.entity_id
6674
- FROM $catalog_product_entity cpe
6675
- JOIN $products_temp p
6676
- ON cpe.store_product_id = p.store_product_id
6677
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
6678
- ON p.store_product_id=spc.store_product_id
6679
- JOIN $stINch_categories_mapping scm
6680
- ON spc.store_category_id = scm.store_category_id
6681
- )
6682
- ON DUPLICATE KEY UPDATE
6683
- product_id = cpe.entity_id
6684
- ");
6685
-
6686
-
6687
- echo("\n replaceMagentoProductsMultistore 16\n");
6688
-
6689
- //Indexing products and categories in the shop
6690
- $result = $this->db_do("
6691
- DELETE ccpi
6692
- FROM $catalog_category_product_index ccpi
6693
- LEFT JOIN $catalog_product_entity cpe
6694
- ON ccpi.product_id = cpe.entity_id
6695
- WHERE cpe.entity_id IS NULL");
6696
-
6697
-
6698
- echo("\n replaceMagentoProductsMultistore 16.2\n");
6699
-
6700
-
6701
- $result = $this->db_do("
6702
- INSERT INTO $catalog_category_product_index
6703
- (category_id, product_id, position, is_parent, store_id, visibility)
6704
- (SELECT
6705
- a.category_id,
6706
- a.product_id,
6707
- a.position,
6708
- 1,
6709
- b.store_id,
6710
- 4
6711
- FROM $catalog_category_product a
6712
- JOIN $core_store b
6713
- )
6714
- ON DUPLICATE KEY UPDATE
6715
- visibility = 4");
6716
-
6717
-
6718
- echo("\n replaceMagentoProductsMultistore 17\n");
6719
-
6720
- // !!! $this->_root_cat
6721
- $result = $this->db_do("
6722
- INSERT ignore INTO $catalog_category_product_index
6723
- (category_id, product_id, position, is_parent, store_id, visibility)
6724
- (SELECT
6725
- rc.root_cat,
6726
- a.product_id,
6727
- a.position,
6728
- 1,
6729
- b.store_id,
6730
- 4
6731
- FROM $catalog_category_product a
6732
- JOIN $root_cats rc
6733
- ON a.category_id = rc.entity_id
6734
- JOIN $core_store b
6735
- )
6736
- ON DUPLICATE KEY UPDATE
6737
- visibility = 4");
6738
-
6739
- echo("\n replaceMagentoProductsMultistore 18\n");
6740
-
6741
-
6742
- //Set product name for specific web sites
6743
- $result = $this->db_do("
6744
- DELETE cpev
6745
- FROM $catalog_product_entity_varchar cpev
6746
- LEFT JOIN $catalog_product_entity cpe
6747
- ON cpev.entity_id = cpe.entity_id
6748
- WHERE cpe.entity_id IS NULL");
6749
-
6750
- $result = $this->db_do("
6751
- INSERT INTO $catalog_product_entity_varchar
6752
- (entity_type_id, attribute_id, store_id, entity_id, value)
6753
- (SELECT
6754
- $_getProductEntityTypeId,
6755
- $attr_name,
6756
- w.website,
6757
- a.entity_id,
6758
- b.product_name
6759
- FROM $catalog_product_entity a
6760
- JOIN $products_temp b
6761
- ON a.store_product_id = b.store_product_id
6762
- JOIN $products_website_temp w
6763
- ON a.store_product_id = w.store_product_id
6764
- )
6765
- ON DUPLICATE KEY UPDATE
6766
- value = b.product_name");
6767
-
6768
- echo("\n replaceMagentoProductsMultistore 19\n");
6769
-
6770
-
6771
- // product name for all web sites
6772
- $result = $this->db_do("
6773
- INSERT INTO $catalog_product_entity_varchar
6774
- (entity_type_id, attribute_id, store_id, entity_id, value)
6775
- (SELECT
6776
- $_getProductEntityTypeId,
6777
- $attr_name,
6778
- 0,
6779
- a.entity_id,
6780
- b.product_name
6781
- FROM $catalog_product_entity a
6782
- JOIN $products_temp b
6783
- ON a.store_product_id = b.store_product_id
6784
- )
6785
- ON DUPLICATE KEY UPDATE
6786
- value = b.product_name");
6787
-
6788
- echo("\n replaceMagentoProductsMultistore 20\n");
6789
-
6790
-
6791
- $this->dropHTMLentities($this->_getProductEntityTypeId(), $this->_getProductAttributeId('name'));
6792
- $this->addDescriptions();
6793
- $this->cleanProductDistributors();
6794
- if ($this->product_file_format == "NEW") {
6795
- $this->addReviews();
6796
- $this->addWeight();
6797
- $this->addSearchCache();
6798
- $this->addPdfUrl();
6799
- $this->addShortDescriptions();
6800
- $this->addProductDistributors();
6801
- }
6802
- $this->addEAN();
6803
- $this->addSpecification();
6804
- $this->addManufacturers();
6805
-
6806
- //echo(" .... DONE\n");return;
6807
-
6808
- echo("\n replaceMagentoProductsMultistore 21\n");
6809
-
6810
- //Enabling product index.
6811
- $result = $this->db_do("
6812
- DELETE cpei
6813
- FROM $catalog_product_enabled_index cpei
6814
- LEFT JOIN $catalog_product_entity cpe
6815
- ON cpei.product_id = cpe.entity_id
6816
- WHERE cpe.entity_id IS NULL");
6817
-
6818
- echo("\n replaceMagentoProductsMultistore 22\n");
6819
-
6820
- $result = $this->db_do("
6821
- INSERT INTO $catalog_product_enabled_index
6822
- (product_id, store_id, visibility)
6823
- (SELECT
6824
- a.entity_id,
6825
- w.website,
6826
- 4
6827
- FROM $catalog_product_entity a
6828
- JOIN $products_website_temp w
6829
- ON a.store_product_id = w.store_product_id
6830
- )
6831
- ON DUPLICATE KEY UPDATE
6832
- visibility = 4");
6833
-
6834
- echo("\n replaceMagentoProductsMultistore 23\n");
6835
-
6836
- $result = $this->db_do("
6837
- INSERT INTO $catalog_product_enabled_index
6838
- (product_id, store_id, visibility)
6839
- (SELECT
6840
- a.entity_id,
6841
- 0,
6842
- 4
6843
- FROM $catalog_product_entity a
6844
- JOIN $products_website_temp w
6845
- ON a.store_product_id = w.store_product_id
6846
- )
6847
- ON DUPLICATE KEY UPDATE
6848
- visibility = 4");
6849
-
6850
-
6851
- /////////////////////////////////////echo(" .... DONE\n");return;
6852
-
6853
-
6854
- echo("\n replaceMagentoProductsMultistore 24\n");
6855
-
6856
- $result = $this->db_do("
6857
- INSERT INTO $catalog_product_entity_int
6858
- (entity_type_id, attribute_id, store_id, entity_id, value)
6859
- (SELECT
6860
- $_getProductEntityTypeId,
6861
- $attr_visibility,
6862
- w.website,
6863
- a.entity_id,
6864
- 4
6865
- FROM $catalog_product_entity a
6866
- JOIN $products_website_temp w
6867
- ON a.store_product_id = w.store_product_id
6868
- )
6869
- ON DUPLICATE KEY UPDATE
6870
- value = 4");
6871
-
6872
- echo("\n replaceMagentoProductsMultistore 25\n");
6873
-
6874
- $result = $this->db_do("
6875
- INSERT INTO $catalog_product_entity_int
6876
- (entity_type_id, attribute_id, store_id, entity_id, value)
6877
- (SELECT
6878
- $_getProductEntityTypeId,
6879
- $attr_visibility,
6880
- 0,
6881
- a.entity_id,
6882
- 4
6883
- FROM $catalog_product_entity a
6884
- )
6885
- ON DUPLICATE KEY UPDATE
6886
- value = 4");
6887
-
6888
- echo("\n replaceMagentoProductsMultistore 26\n");
6889
-
6890
- $result = $this->db_do("
6891
- DELETE cpw
6892
- FROM $catalog_product_website cpw
6893
- LEFT JOIN $catalog_product_entity cpe
6894
- ON cpw.product_id = cpe.entity_id
6895
- WHERE cpe.entity_id IS NULL");
6896
-
6897
- echo("\n replaceMagentoProductsMultistore 27\n");
6898
-
6899
- $result = $this->db_do("
6900
- INSERT INTO $catalog_product_website
6901
- (product_id, website_id)
6902
- (SELECT
6903
- a.entity_id,
6904
- w.website_id
6905
- FROM $catalog_product_entity a
6906
- JOIN $products_website_temp w
6907
- ON a.store_product_id = w.store_product_id
6908
- )
6909
- ON DUPLICATE KEY UPDATE
6910
- product_id = a.entity_id,
6911
- website_id = w.website_id");
6912
-
6913
-
6914
- //echo(" .... DONE\n");return;
6915
-
6916
-
6917
- echo("\n replaceMagentoProductsMultistore 28\n");
6918
-
6919
- // temporary disabled mart@bintime.com
6920
- //$result = $this->db_do("
6921
- // UPDATE catalog_category_entity_int a
6922
- // SET a.value = 0
6923
- // WHERE a.attribute_id = 32
6924
- //");
6925
-
6926
-
6927
- //Adding tax class "Taxable Goods"
6928
- $result = $this->db_do("
6929
- INSERT INTO $catalog_product_entity_int
6930
- (entity_type_id, attribute_id, store_id, entity_id, value)
6931
- (SELECT
6932
- $_getProductEntityTypeId,
6933
- $attr_tax_class_id,
6934
- w.website,
6935
- a.entity_id,
6936
- 2
6937
- FROM $catalog_product_entity a
6938
- JOIN $products_website_temp w
6939
- ON a.store_product_id = w.store_product_id
6940
- )
6941
- ON DUPLICATE KEY UPDATE
6942
- value = 2");
6943
-
6944
- echo("\n replaceMagentoProductsMultistore 29\n");
6945
-
6946
- $result = $this->db_do("
6947
- INSERT INTO $catalog_product_entity_int
6948
- (entity_type_id, attribute_id, store_id, entity_id, value)
6949
- (SELECT
6950
- $_getProductEntityTypeId,
6951
- $attr_tax_class_id,
6952
- 0,
6953
- a.entity_id,
6954
- 2
6955
- FROM $catalog_product_entity a
6956
- )
6957
- ON DUPLICATE KEY UPDATE
6958
- value = 2");
6959
-
6960
- echo("\n replaceMagentoProductsMultistore 30\n");
6961
-
6962
- // Load url Image
6963
- $result = $this->db_do("
6964
- INSERT INTO $catalog_product_entity_varchar
6965
- (entity_type_id, attribute_id, store_id, entity_id, value)
6966
- (SELECT
6967
- $_getProductEntityTypeId,
6968
- $attr_image,
6969
- w.store_id,
6970
- a.entity_id,
6971
- b.main_image_url
6972
- FROM $catalog_product_entity a
6973
- JOIN $core_store w
6974
- JOIN $products_temp b
6975
- ON a.store_product_id = b.store_product_id
6976
- )
6977
- ON DUPLICATE KEY UPDATE
6978
- value = b.main_image_url");
6979
-
6980
- echo("\n replaceMagentoProductsMultistore 31\n");
6981
-
6982
- // image for specific web sites
6983
- $result = $this->db_do("
6984
- INSERT INTO $catalog_product_entity_varchar
6985
- (entity_type_id, attribute_id, store_id, entity_id, value)
6986
- (SELECT
6987
- $_getProductEntityTypeId,
6988
- $attr_image,
6989
- 0,
6990
- a.entity_id,
6991
- b.main_image_url
6992
- FROM $catalog_product_entity a
6993
- JOIN $products_temp b
6994
- ON a.store_product_id = b.store_product_id
6995
- )
6996
- ON DUPLICATE KEY UPDATE
6997
- value = b.main_image_url");
6998
-
6999
- echo("\n replaceMagentoProductsMultistore 32\n");
7000
-
7001
- // small_image for specific web sites
7002
- $result = $this->db_do("
7003
- INSERT INTO $catalog_product_entity_varchar
7004
- (entity_type_id, attribute_id, store_id, entity_id, value)
7005
- (SELECT
7006
- $_getProductEntityTypeId,
7007
- $attr_small_image,
7008
- w.store_id,
7009
- a.entity_id,
7010
- b.medium_image_url
7011
- FROM $catalog_product_entity a
7012
- JOIN $core_store w
7013
- JOIN $products_temp b
7014
- ON a.store_product_id = b.store_product_id
7015
- )
7016
- ON DUPLICATE KEY UPDATE
7017
- value = b.medium_image_url");
7018
-
7019
- echo("\n replaceMagentoProductsMultistore 33\n");
7020
-
7021
- // small_image for all web sites
7022
- $result = $this->db_do("
7023
- INSERT INTO $catalog_product_entity_varchar
7024
- (entity_type_id, attribute_id, store_id, entity_id, value)
7025
- (SELECT
7026
- $_getProductEntityTypeId,
7027
- $attr_small_image,
7028
- 0,
7029
- a.entity_id,
7030
- b.medium_image_url
7031
- FROM $catalog_product_entity a
7032
- JOIN $core_store w
7033
- JOIN $products_temp b
7034
- ON a.store_product_id = b.store_product_id
7035
- )
7036
- ON DUPLICATE KEY UPDATE
7037
- value = b.medium_image_url");
7038
-
7039
- echo("\n replaceMagentoProductsMultistore 34\n");
7040
-
7041
- // thumbnail for specific web site
7042
- $result = $this->db_do("
7043
- INSERT INTO $catalog_product_entity_varchar
7044
- (entity_type_id, attribute_id, store_id, entity_id, value)
7045
- (SELECT
7046
- $_getProductEntityTypeId,
7047
- $attr_thumbnail,
7048
- w.store_id,
7049
- a.entity_id,
7050
- b.thumb_image_url
7051
- FROM $catalog_product_entity a
7052
- JOIN $core_store w
7053
- JOIN $products_temp b
7054
- ON a.store_product_id = b.store_product_id
7055
- )
7056
- ON DUPLICATE KEY UPDATE
7057
- value = b.thumb_image_url");
7058
-
7059
- echo("\n replaceMagentoProductsMultistore 35\n");
7060
-
7061
- // thumbnail for all web sites
7062
- $result = $this->db_do("
7063
- INSERT INTO $catalog_product_entity_varchar
7064
- (entity_type_id, attribute_id, store_id, entity_id, value)
7065
- (SELECT
7066
- $_getProductEntityTypeId,
7067
- $attr_thumbnail,
7068
- 0,
7069
- a.entity_id,
7070
- b.thumb_image_url
7071
- FROM $catalog_product_entity a
7072
- JOIN $core_store w
7073
- JOIN $products_temp b
7074
- ON a.store_product_id = b.store_product_id
7075
- )
7076
- ON DUPLICATE KEY UPDATE
7077
- value = b.thumb_image_url");
7078
-
7079
- echo("\n replaceMagentoProductsMultistore 36\n");
7080
-
7081
-
7082
- /*STP DELETE
7083
- //Refresh fulltext search
7084
- $result = $this->db_do("DROP TABLE IF EXISTS {$catalogsearch_fulltext}_tmp");
7085
- $result = $this->db_do("CREATE TEMPORARY TABLE IF NOT EXISTS {$catalogsearch_fulltext}_tmp LIKE $catalogsearch_fulltext");
7086
-
7087
-
7088
- echo("\n replaceMagentoProductsMultistore 36.2\n");
7089
- $q = "
7090
- INSERT INTO {$catalogsearch_fulltext}_tmp
7091
- (product_id, store_id, data_index)
7092
- (SELECT
7093
- a.entity_id,
7094
- w.website,
7095
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
7096
- FROM $catalog_product_entity a
7097
- JOIN $products_website_temp w
7098
- ON a.store_product_id = w.store_product_id
7099
- LEFT JOIN $catalog_category_product b
7100
- ON a.entity_id = b.product_id
7101
- LEFT JOIN $catalog_category_entity_varchar c
7102
- ON b.category_id = c.entity_id
7103
- AND c.attribute_id = $cat_attr_name
7104
- LEFT JOIN $catalog_product_entity_varchar e
7105
- ON a.entity_id = e.entity_id
7106
- AND e.attribute_id = $attr_name
7107
- LEFT JOIN $catalog_product_website j
7108
- ON a.entity_id = j.product_id
7109
- LEFT JOIN $products_temp f
7110
- ON a.store_product_id = f.store_product_id
7111
- )
7112
- ON DUPLICATE KEY UPDATE
7113
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)";
7114
- echo("\n\n============================\n$q\n============================\n\n");
7115
-
7116
-
7117
- $result = $this->db_do("
7118
- INSERT INTO {$catalogsearch_fulltext}_tmp
7119
- (product_id, store_id, data_index)
7120
- (SELECT
7121
- a.entity_id,
7122
- w.website,
7123
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
7124
- FROM $catalog_product_entity a
7125
- JOIN $products_website_temp w
7126
- ON a.store_product_id = w.store_product_id
7127
- LEFT JOIN $catalog_category_product b
7128
- ON a.entity_id = b.product_id
7129
- LEFT JOIN $catalog_category_entity_varchar c
7130
- ON b.category_id = c.entity_id
7131
- AND c.attribute_id = $cat_attr_name
7132
- LEFT JOIN $catalog_product_entity_varchar e
7133
- ON a.entity_id = e.entity_id
7134
- AND e.attribute_id = $attr_name
7135
- LEFT JOIN $catalog_product_website j
7136
- ON a.entity_id = j.product_id
7137
- LEFT JOIN $products_temp f
7138
- ON a.store_product_id = f.store_product_id
7139
- )
7140
- ON DUPLICATE KEY UPDATE
7141
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)");
7142
-
7143
- echo("\n replaceMagentoProductsMultistore 37\n");
7144
-
7145
-
7146
- $result = $this->db_do("
7147
- INSERT INTO {$catalogsearch_fulltext}_tmp
7148
- (product_id, store_id, data_index)
7149
- (SELECT
7150
- a.entity_id,
7151
- w.website,
7152
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
7153
- FROM $catalog_product_entity a
7154
- JOIN $products_website_temp w
7155
- ON a.store_product_id = w.store_product_id
7156
- LEFT JOIN $catalog_category_product b
7157
- ON a.entity_id = b.product_id
7158
- LEFT JOIN $catalog_category_entity_varchar c
7159
- ON b.category_id = c.entity_id
7160
- AND c.attribute_id = $cat_attr_name
7161
- LEFT JOIN $catalog_product_entity_varchar e
7162
- ON a.entity_id = e.entity_id
7163
- AND e.attribute_id = $attr_name
7164
- LEFT JOIN $products_temp f
7165
- ON a.store_product_id = f.store_product_id
7166
- )
7167
- ON DUPLICATE KEY UPDATE
7168
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)");
7169
-
7170
- echo("\n replaceMagentoProductsMultistore 38\n");
7171
-
7172
- $result = $this->db_do("
7173
- DELETE cf
7174
- FROM $catalogsearch_fulltext cf
7175
- LEFT JOIN $catalog_product_entity cpe
7176
- ON cf.product_id = cpe.entity_id
7177
- WHERE cpe.entity_id IS NULL");
7178
-
7179
- echo("\n replaceMagentoProductsMultistore 39\n");
7180
-
7181
- $result = $this->db_do("
7182
- INSERT INTO $catalogsearch_fulltext
7183
- (product_id, store_id, data_index)
7184
- (SELECT
7185
- a.product_id,
7186
- a.store_id,
7187
- a.data_index
7188
- FROM {$catalogsearch_fulltext}_tmp a
7189
- )
7190
- ON DUPLICATE KEY UPDATE
7191
- data_index = a.data_index");
7192
-
7193
- echo("\n replaceMagentoProductsMultistore 40\n");
7194
-
7195
- $this->db_do("UPDATE $catalogsearch_query SET is_processed = 0");
7196
- //INNER JOIN eav_attribute_option_value d ON a.vendor_id = d.option_id
7197
- //TODO add something else
7198
- STP DELETE*/
7199
-
7200
- $this->addRelatedProducts();
7201
- echo("\n replaceMagentoProductsMultistore 41\n");
7202
- }
7203
-
7204
- #################################################################################################
7205
-
7206
- public function replaceMagentoProductsMultistoreMERGE($coincidence)
7207
- {
7208
-
7209
- echo("\n replaceMagentoProductsMultistoreMERGE 1\n");
7210
-
7211
-
7212
- $connection = Mage::getModel('core/resource')->getConnection('core_write');
7213
-
7214
-
7215
- $products_temp = Mage::getSingleton('core/resource')->getTableName('products_temp');
7216
- $products_website_temp = Mage::getSingleton('core/resource')->getTableName('products_website_temp');
7217
- $catalog_product_entity = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
7218
- $catalog_product_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int');
7219
- $catalog_product_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar');
7220
- $catalog_category_product = Mage::getSingleton('core/resource')->getTableName('catalog_category_product');
7221
- $stINch_products_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping');
7222
- $stINch_products = Mage::getSingleton('core/resource')->getTableName('stINch_products');
7223
- $catalog_category_entity = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity');
7224
- $stINch_categories_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping');
7225
- $catalog_category_product_index = Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index');
7226
- $core_store = Mage::getSingleton('core/resource')->getTableName('core_store');
7227
- $catalog_product_enabled_index = Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index');
7228
- $catalog_product_website = Mage::getSingleton('core/resource')->getTableName('catalog_product_website');
7229
- //STP DELETE $catalogsearch_fulltext = Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext');
7230
- // $catalogsearch_query = Mage::getSingleton('core/resource')->getTableName('catalogsearch_query');
7231
- $catalog_category_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar');
7232
-
7233
- $_getProductEntityTypeId = $this->_getProductEntityTypeId();
7234
- $_defaultAttributeSetId = $this->_getProductDefaulAttributeSetId();
7235
-
7236
- $attr_atatus = $this->_getProductAttributeId('status');
7237
- $attr_name = $this->_getProductAttributeId('name');
7238
- $attr_visibility = $this->_getProductAttributeId('visibility');
7239
- $attr_tax_class_id = $this->_getProductAttributeId('tax_class_id');
7240
- $attr_image = $this->_getProductAttributeId('image');
7241
- $attr_small_image = $this->_getProductAttributeId('small_image');
7242
- $attr_thumbnail = $this->_getProductAttributeId('thumbnail');
7243
-
7244
- $cat_attr_name = $this->_getCategoryAttributeId('name');
7245
- echo("\n replaceMagentoProductsMultistoreMERGE 2\n");
7246
-
7247
-
7248
- //clear products, inserting new products and updating old others.
7249
- $query = "
7250
- DELETE cpe
7251
- FROM $catalog_product_entity cpe
7252
- JOIN $stINch_products_mapping pm
7253
- ON cpe.entity_id = pm.entity_id
7254
- WHERE pm.shop_store_product_id IS NOT NULL
7255
- AND pm.store_product_id IS NULL";
7256
- $result = $this->db_do($query);
7257
-
7258
-
7259
- echo("\n replaceMagentoProductsMultistoreMERGE 3\n");
7260
-
7261
- $result = $this->db_do("
7262
- INSERT INTO $catalog_product_entity
7263
- (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
7264
- (SELECT
7265
- pm.entity_id,
7266
- $_getProductEntityTypeId,
7267
- $_defaultAttributeSetId,
7268
- 'simple',
7269
- a.product_sku,
7270
- NOW(),
7271
- 0,
7272
- a.store_product_id,
7273
- a.sinch_product_id
7274
- FROM $products_temp a
7275
- LEFT JOIN $stINch_products_mapping pm
7276
- ON a.store_product_id = pm.store_product_id
7277
- AND a.sinch_product_id = pm.sinch_product_id
7278
- WHERE pm.entity_id IS NOT NULL
7279
- )
7280
- ON DUPLICATE KEY UPDATE
7281
- sku = a.product_sku,
7282
- store_product_id = a.store_product_id,
7283
- sinch_product_id = a.sinch_product_id");
7284
- // store_product_id = a.store_product_id,
7285
- // sinch_product_id = a.sinch_product_id
7286
-
7287
- $result = $this->db_do("
7288
- INSERT INTO $catalog_product_entity
7289
- (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
7290
- (SELECT
7291
- pm.entity_id,
7292
- $_getProductEntityTypeId,
7293
- $_defaultAttributeSetId,
7294
- 'simple',
7295
- a.product_sku,
7296
- NOW(),
7297
- 0,
7298
- a.store_product_id,
7299
- a.sinch_product_id
7300
- FROM $products_temp a
7301
- LEFT JOIN $stINch_products_mapping pm
7302
- ON a.store_product_id = pm.store_product_id
7303
- AND a.sinch_product_id = pm.sinch_product_id
7304
- WHERE pm.entity_id IS NULL
7305
- )
7306
- ON DUPLICATE KEY UPDATE
7307
- sku = a.product_sku,
7308
- store_product_id = a.store_product_id,
7309
- sinch_product_id = a.sinch_product_id");
7310
- // store_product_id = a.store_product_id,
7311
- // sinch_product_id = a.sinch_product_id
7312
-
7313
-
7314
- echo("\n replaceMagentoProductsMultistoreMERGE 4\n");
7315
-
7316
-
7317
- //Set enabled
7318
- $result = $this->db_do("
7319
- DELETE cpei
7320
- FROM $catalog_product_entity_int cpei
7321
- LEFT JOIN $catalog_product_entity cpe
7322
- ON cpei.entity_id = cpe.entity_id
7323
- WHERE cpe.entity_id IS NULL");
7324
-
7325
- $result = $this->db_do("
7326
- INSERT INTO $catalog_product_entity_int
7327
- (entity_type_id, attribute_id, store_id, entity_id, value)
7328
- (SELECT
7329
- $_getProductEntityTypeId,
7330
- $attr_atatus,
7331
- w.website,
7332
- a.entity_id,
7333
- 1
7334
- FROM $catalog_product_entity a
7335
- JOIN $products_website_temp w
7336
- ON a.store_product_id = w.store_product_id
7337
- )
7338
- ON DUPLICATE KEY UPDATE
7339
- value = 1");
7340
-
7341
-
7342
- echo("\n replaceMagentoProductsMultistoreMERGE 5\n");
7343
-
7344
-
7345
- // set status = 1 for all stores
7346
- $result = $this->db_do("
7347
- INSERT INTO $catalog_product_entity_int
7348
- (entity_type_id, attribute_id, store_id, entity_id, value)
7349
- (SELECT
7350
- $_getProductEntityTypeId,
7351
- $attr_atatus,
7352
- 0,
7353
- a.entity_id,
7354
- 1
7355
- FROM $catalog_product_entity a
7356
- )
7357
- ON DUPLICATE KEY UPDATE
7358
- value = 1");
7359
-
7360
-
7361
- echo("\n replaceMagentoProductsMultistoreMERGE 6\n");
7362
-
7363
-
7364
- //Unifying products with categories.
7365
- $result = $this->db_do("
7366
- DELETE ccp
7367
- FROM $catalog_category_product ccp
7368
- LEFT JOIN $catalog_product_entity cpe
7369
- ON ccp.product_id = cpe.entity_id
7370
- WHERE cpe.entity_id IS NULL");
7371
-
7372
-
7373
- echo("\n replaceMagentoProductsMultistoreMERGE 7\n");
7374
-
7375
-
7376
- $root_cats = Mage::getSingleton('core/resource')->getTableName('root_cats');
7377
-
7378
- $result = $this->db_do("DROP TABLE IF EXISTS $root_cats");
7379
- $result = $this->db_do("
7380
- CREATE TABLE $root_cats
7381
- SELECT
7382
- entity_id,
7383
- path,
7384
- SUBSTRING(path, LOCATE('/', path)+1) AS short_path,
7385
- LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1)) AS end_pos,
7386
- SUBSTRING(SUBSTRING(path, LOCATE('/', path)+1), 1, LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1))-1) as root_cat
7387
- FROM $catalog_category_entity
7388
- ");
7389
- $result = $this->db_do("UPDATE $root_cats SET root_cat = entity_id WHERE CHAR_LENGTH(root_cat) = 0");
7390
-
7391
-
7392
- echo("\n replaceMagentoProductsMultistoreMERGE 8\n");
7393
-
7394
-
7395
- // !!! $this->_root_cat
7396
- $result = $this->db_do("
7397
- UPDATE IGNORE $catalog_category_product ccp
7398
- LEFT JOIN $catalog_category_entity cce
7399
- ON ccp.category_id = cce.entity_id
7400
- JOIN $root_cats rc
7401
- ON cce.entity_id = rc.entity_id
7402
- SET ccp.category_id = rc.root_cat
7403
- WHERE cce.entity_id IS NULL");
7404
-
7405
-
7406
- echo("\n replaceMagentoProductsMultistoreMERGE 9\n");
7407
-
7408
-
7409
- $result = $this->db_do("
7410
- DELETE ccp
7411
- FROM $catalog_category_product ccp
7412
- LEFT JOIN $catalog_category_entity cce
7413
- ON ccp.category_id = cce.entity_id
7414
- WHERE cce.entity_id IS NULL");
7415
-
7416
-
7417
- $stinch_products_delete = Mage::getSingleton('core/resource')->getTableName('stinch_products_delete');
7418
-
7419
- $result = $this->db_do("DROP TABLE IF EXISTS $stinch_products_delete");
7420
- $result = $this->db_do("
7421
- CREATE TABLE $stinch_products_delete
7422
- SELECT cpe.entity_id
7423
- FROM $catalog_product_entity cpe
7424
- WHERE cpe.entity_id NOT IN
7425
- (
7426
- SELECT cpe2.entity_id
7427
- FROM $catalog_product_entity cpe2
7428
- JOIN $stINch_products sp
7429
- ON cpe2.sinch_product_id = sp.sinch_product_id
7430
- )");
7431
-
7432
-
7433
- $result = $this->db_do("DELETE cpe FROM $catalog_product_entity cpe JOIN $stinch_products_delete spd USING(entity_id)");
7434
-
7435
- $result = $this->db_do("DROP TABLE IF EXISTS $stinch_products_delete");
7436
-
7437
-
7438
- //echo("\n\nget out...\n\n");
7439
- //return;
7440
-
7441
- /**
7442
- *
7443
- * echo("\n replaceMagentoProductsMultistoreMERGE 10\n");
7444
- *
7445
- *
7446
- * // TEMPORARY
7447
- * $this->db_do(" DROP TABLE IF EXISTS {$catalog_category_product}_for_delete_temp");
7448
- * $this->db_do("
7449
- * CREATE TABLE `{$catalog_category_product}_for_delete_temp`
7450
- * (
7451
- * `category_id` int(10) unsigned NOT NULL default '0',
7452
- * `product_id` int(10) unsigned NOT NULL default '0',
7453
- * `store_product_id` int(10) NOT NULL default '0',
7454
- * `store_category_id` int(10) NOT NULL default '0',
7455
- * `new_category_id` int(10) NOT NULL default '0',
7456
- *
7457
- * UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`category_id`,`product_id`),
7458
- * KEY `CATALOG_CATEGORY_PRODUCT_CATEGORY` (`category_id`),
7459
- * KEY `CATALOG_CATEGORY_PRODUCT_PRODUCT` (`product_id`),
7460
- * KEY `CATALOG_NEW_CATEGORY_PRODUCT_CATEGORY` (`new_category_id`)
7461
- * )");
7462
- *
7463
- * echo("\n replaceMagentoProductsMultistoreMERGE 11\n");
7464
- *
7465
- * $result = $this->db_do("
7466
- * INSERT INTO {$catalog_category_product}_for_delete_temp
7467
- * (category_id, product_id, store_product_id)
7468
- * (SELECT
7469
- * ccp.category_id,
7470
- * ccp.product_id,
7471
- * cpe.store_product_id
7472
- * FROM $catalog_category_product ccp
7473
- * JOIN $catalog_product_entity cpe
7474
- * ON ccp.product_id = cpe.entity_id
7475
- * WHERE store_product_id IS NOT NULL)");
7476
- *
7477
- * echo("\n replaceMagentoProductsMultistoreMERGE 12\n");
7478
- *
7479
- * $result = $this->db_do("
7480
- * UPDATE {$catalog_category_product}_for_delete_temp ccpfd
7481
- * JOIN $products_temp p
7482
- * ON ccpfd.store_product_id = p.store_product_id
7483
- * SET ccpfd.store_category_id = p.store_category_id
7484
- * WHERE ccpfd.store_product_id != 0");
7485
- *
7486
- * echo("\n replaceMagentoProductsMultistoreMERGE 13\n");
7487
- *
7488
- * $result = $this->db_do("
7489
- * UPDATE {$catalog_category_product}_for_delete_temp ccpfd
7490
- * JOIN $stINch_categories_mapping scm
7491
- * ON ccpfd.store_category_id = scm.store_category_id
7492
- * SET ccpfd.new_category_id = scm.shop_entity_id
7493
- * WHERE ccpfd.store_category_id != 0");
7494
- *
7495
- * echo("\n replaceMagentoProductsMultistoreMERGE 14\n");
7496
- *
7497
- * $result = $this->db_do("DELETE FROM {$catalog_category_product}_for_delete_temp WHERE category_id = new_category_id");
7498
- *
7499
- *
7500
- *
7501
- * $result = $this->db_do("
7502
- * DELETE ccp
7503
- * FROM $catalog_category_product ccp
7504
- * JOIN {$catalog_category_product}_for_delete_temp ccpfd
7505
- * ON ccp.product_id = ccpfd.product_id
7506
- * AND ccp.category_id = ccpfd.category_id");
7507
- *
7508
- * /**/
7509
-
7510
-
7511
- echo("\n replaceMagentoProductsMultistoreMERGE 15\n");
7512
-
7513
-
7514
- $result = $this->db_do("
7515
- INSERT INTO $catalog_category_product
7516
- (category_id, product_id)
7517
- (SELECT
7518
- scm.shop_entity_id,
7519
- cpe.entity_id
7520
- FROM $catalog_product_entity cpe
7521
- JOIN $products_temp p
7522
- ON cpe.store_product_id = p.store_product_id
7523
- JOIN $stINch_categories_mapping scm
7524
- ON p.store_category_id = scm.store_category_id
7525
- )
7526
- ON DUPLICATE KEY UPDATE
7527
- product_id = cpe.entity_id");
7528
-
7529
-
7530
- echo("\n replaceMagentoProductsMultistoreMERGE 15.1 (add multi categories)\n");
7531
-
7532
-
7533
- $result = $this->db_do("
7534
- INSERT INTO $catalog_category_product
7535
- (category_id, product_id)
7536
- (SELECT
7537
- scm.shop_entity_id,
7538
- cpe.entity_id
7539
- FROM $catalog_product_entity cpe
7540
- JOIN $products_temp p
7541
- ON cpe.store_product_id = p.store_product_id
7542
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
7543
- ON p.store_product_id=spc.store_product_id
7544
- JOIN $stINch_categories_mapping scm
7545
- ON spc.store_category_id = scm.store_category_id
7546
- )
7547
- ON DUPLICATE KEY UPDATE
7548
- product_id = cpe.entity_id
7549
- ");
7550
-
7551
-
7552
- echo("\n replaceMagentoProductsMultistoreMERGE 16\n");
7553
-
7554
- //Indexing products and categories in the shop
7555
- $result = $this->db_do("
7556
- DELETE ccpi
7557
- FROM $catalog_category_product_index ccpi
7558
- LEFT JOIN $catalog_product_entity cpe
7559
- ON ccpi.product_id = cpe.entity_id
7560
- WHERE cpe.entity_id IS NULL");
7561
-
7562
-
7563
- echo("\n replaceMagentoProductsMultistoreMERGE 16.2\n");
7564
-
7565
-
7566
- $result = $this->db_do("
7567
- INSERT INTO $catalog_category_product_index
7568
- (category_id, product_id, position, is_parent, store_id, visibility)
7569
- (SELECT
7570
- a.category_id,
7571
- a.product_id,
7572
- a.position,
7573
- 1,
7574
- b.store_id,
7575
- 4
7576
- FROM $catalog_category_product a
7577
- JOIN $core_store b
7578
- )
7579
- ON DUPLICATE KEY UPDATE
7580
- visibility = 4");
7581
-
7582
-
7583
- echo("\n replaceMagentoProductsMultistoreMERGE 17\n");
7584
- $root_cats = Mage::getSingleton('core/resource')->getTableName('root_cats');
7585
- // !!! $this->_root_cat
7586
- $result = $this->db_do("
7587
- INSERT ignore INTO $catalog_category_product_index
7588
- (category_id, product_id, position, is_parent, store_id, visibility)
7589
- (SELECT
7590
- rc.root_cat,
7591
- a.product_id,
7592
- a.position,
7593
- 1,
7594
- b.store_id,
7595
- 4
7596
- FROM $catalog_category_product a
7597
- JOIN $root_cats rc
7598
- ON a.category_id = rc.entity_id
7599
- JOIN $core_store b
7600
- )
7601
- ON DUPLICATE KEY UPDATE
7602
- visibility = 4");
7603
-
7604
- echo("\n replaceMagentoProductsMultistoreMERGE 18\n");
7605
-
7606
-
7607
- //Set product name for specific web sites
7608
- $result = $this->db_do("
7609
- DELETE cpev
7610
- FROM $catalog_product_entity_varchar cpev
7611
- LEFT JOIN $catalog_product_entity cpe
7612
- ON cpev.entity_id = cpe.entity_id
7613
- WHERE cpe.entity_id IS NULL");
7614
-
7615
- $result = $this->db_do("
7616
- INSERT INTO $catalog_product_entity_varchar
7617
- (entity_type_id, attribute_id, store_id, entity_id, value)
7618
- (SELECT
7619
- $_getProductEntityTypeId,
7620
- $attr_name,
7621
- w.website,
7622
- a.entity_id,
7623
- b.product_name
7624
- FROM $catalog_product_entity a
7625
- JOIN $products_temp b
7626
- ON a.store_product_id = b.store_product_id
7627
- JOIN $products_website_temp w
7628
- ON a.store_product_id = w.store_product_id
7629
- )
7630
- ON DUPLICATE KEY UPDATE
7631
- value = b.product_name");
7632
-
7633
- echo("\n replaceMagentoProductsMultistoreMERGE 19\n");
7634
-
7635
-
7636
- // product name for all web sites
7637
- $result = $this->db_do("
7638
- INSERT INTO $catalog_product_entity_varchar
7639
- (entity_type_id, attribute_id, store_id, entity_id, value)
7640
- (SELECT
7641
- $_getProductEntityTypeId,
7642
- $attr_name,
7643
- 0,
7644
- a.entity_id,
7645
- b.product_name
7646
- FROM $catalog_product_entity a
7647
- JOIN $products_temp b
7648
- ON a.store_product_id = b.store_product_id
7649
- )
7650
- ON DUPLICATE KEY UPDATE
7651
- value = b.product_name");
7652
-
7653
- echo("\n replaceMagentoProductsMultistoreMERGE 20\n");
7654
-
7655
-
7656
- $this->dropHTMLentities($this->_getProductEntityTypeId(), $this->_getProductAttributeId('name'));
7657
- $this->addDescriptions();
7658
- $this->cleanProductDistributors();
7659
- if (!$this->_ignore_product_contracts) {
7660
- $this->cleanProductContracts();
7661
- }
7662
- if ($this->product_file_format == "NEW") {
7663
- $this->addReviews();
7664
- $this->addWeight();
7665
- $this->addSearchCache();
7666
- $this->addPdfUrl();
7667
- $this->addShortDescriptions();
7668
- $this->addProductDistributors();
7669
- if (!$this->_ignore_product_contracts) {
7670
- $this->addProductContracts();
7671
- }
7672
- }
7673
- $this->addEAN();
7674
- $this->addSpecification();
7675
- $this->addManufacturers();
7676
-
7677
-
7678
- echo("\n replaceMagentoProductsMultistoreMERGE 21\n");
7679
-
7680
- //Enabling product index.
7681
- $result = $this->db_do("
7682
- DELETE cpei
7683
- FROM $catalog_product_enabled_index cpei
7684
- LEFT JOIN $catalog_product_entity cpe
7685
- ON cpei.product_id = cpe.entity_id
7686
- WHERE cpe.entity_id IS NULL");
7687
-
7688
-
7689
- echo("\n replaceMagentoProductsMultistoreMERGE 22\n");
7690
-
7691
- $result = $this->db_do("
7692
- INSERT INTO $catalog_product_enabled_index
7693
- (product_id, store_id, visibility)
7694
- (SELECT
7695
- a.entity_id,
7696
- w.website,
7697
- 4
7698
- FROM $catalog_product_entity a
7699
- JOIN $products_website_temp w
7700
- ON a.store_product_id = w.store_product_id
7701
- )
7702
- ON DUPLICATE KEY UPDATE
7703
- visibility = 4");
7704
-
7705
- echo("\n replaceMagentoProductsMultistoreMERGE 23\n");
7706
-
7707
- $result = $this->db_do("
7708
- INSERT INTO $catalog_product_enabled_index
7709
- (product_id, store_id, visibility)
7710
- (SELECT
7711
- a.entity_id,
7712
- 0,
7713
- 4
7714
- FROM $catalog_product_entity a
7715
- JOIN $products_website_temp w
7716
- ON a.store_product_id = w.store_product_id
7717
- )
7718
- ON DUPLICATE KEY UPDATE
7719
- visibility = 4");
7720
-
7721
-
7722
- /////////////////////////////////////echo(" .... DONE\n");return;
7723
-
7724
-
7725
- echo("\n replaceMagentoProductsMultistoreMERGE 24\n");
7726
-
7727
- $result = $this->db_do("
7728
- INSERT INTO $catalog_product_entity_int
7729
- (entity_type_id, attribute_id, store_id, entity_id, value)
7730
- (SELECT
7731
- $_getProductEntityTypeId,
7732
- $attr_visibility,
7733
- w.website,
7734
- a.entity_id,
7735
- 4
7736
- FROM $catalog_product_entity a
7737
- JOIN $products_website_temp w
7738
- ON a.store_product_id = w.store_product_id
7739
- )
7740
- ON DUPLICATE KEY UPDATE
7741
- value = 4");
7742
-
7743
- echo("\n replaceMagentoProductsMultistoreMERGE 25\n");
7744
-
7745
- $result = $this->db_do("
7746
- INSERT INTO $catalog_product_entity_int
7747
- (entity_type_id, attribute_id, store_id, entity_id, value)
7748
- (SELECT
7749
- $_getProductEntityTypeId,
7750
- $attr_visibility,
7751
- 0,
7752
- a.entity_id,
7753
- 4
7754
- FROM $catalog_product_entity a
7755
- )
7756
- ON DUPLICATE KEY UPDATE
7757
- value = 4");
7758
-
7759
- echo("\n replaceMagentoProductsMultistoreMERGE 26\n");
7760
-
7761
-
7762
- $result = $this->db_do("
7763
- DELETE cpw
7764
- FROM $catalog_product_website cpw
7765
- LEFT JOIN $catalog_product_entity cpe
7766
- ON cpw.product_id = cpe.entity_id
7767
- WHERE cpe.entity_id IS NULL");
7768
-
7769
- echo("\n replaceMagentoProductsMultistoreMERGE 27\n");
7770
-
7771
-
7772
- $result = $this->db_do("
7773
- INSERT INTO $catalog_product_website
7774
- (product_id, website_id)
7775
- (SELECT
7776
- a.entity_id,
7777
- w.website_id
7778
- FROM $catalog_product_entity a
7779
- JOIN $products_website_temp w
7780
- ON a.store_product_id = w.store_product_id
7781
- )
7782
- ON DUPLICATE KEY UPDATE
7783
- product_id = a.entity_id,
7784
- website_id = w.website_id");
7785
-
7786
-
7787
- //echo(" .... DONE\n");return;
7788
-
7789
-
7790
- echo("\n replaceMagentoProductsMultistoreMERGE 28\n");
7791
-
7792
- // temporary disabled mart@bintime.com
7793
- //$result = $this->db_do("
7794
- // UPDATE catalog_category_entity_int a
7795
- // SET a.value = 0
7796
- // WHERE a.attribute_id = 32
7797
- //");
7798
-
7799
-
7800
- //Adding tax class "Taxable Goods"
7801
- $result = $this->db_do("
7802
- INSERT INTO $catalog_product_entity_int
7803
- (entity_type_id, attribute_id, store_id, entity_id, value)
7804
- (SELECT
7805
- $_getProductEntityTypeId,
7806
- $attr_tax_class_id,
7807
- w.website,
7808
- a.entity_id,
7809
- 2
7810
- FROM $catalog_product_entity a
7811
- JOIN $products_website_temp w
7812
- ON a.store_product_id = w.store_product_id
7813
- )
7814
- ON DUPLICATE KEY UPDATE
7815
- value = 2");
7816
-
7817
- echo("\n replaceMagentoProductsMultistoreMERGE 29\n");
7818
-
7819
- $result = $this->db_do("
7820
- INSERT INTO $catalog_product_entity_int
7821
- (entity_type_id, attribute_id, store_id, entity_id, value)
7822
- (SELECT
7823
- $_getProductEntityTypeId,
7824
- $attr_tax_class_id,
7825
- 0,
7826
- a.entity_id,
7827
- 2
7828
- FROM $catalog_product_entity a
7829
- )
7830
- ON DUPLICATE KEY UPDATE
7831
- value = 2");
7832
-
7833
- echo("\n replaceMagentoProductsMultistoreMERGE 30\n");
7834
-
7835
- // Load url Image
7836
- $result = $this->db_do("
7837
- INSERT INTO $catalog_product_entity_varchar
7838
- (entity_type_id, attribute_id, store_id, entity_id, value)
7839
- (SELECT
7840
- $_getProductEntityTypeId,
7841
- $attr_image,
7842
- w.store_id,
7843
- a.entity_id,
7844
- b.main_image_url
7845
- FROM $catalog_product_entity a
7846
- JOIN $core_store w
7847
- JOIN $products_temp b
7848
- ON a.store_product_id = b.store_product_id
7849
- )
7850
- ON DUPLICATE KEY UPDATE
7851
- value = b.main_image_url");
7852
-
7853
- echo("\n replaceMagentoProductsMultistoreMERGE 31\n");
7854
-
7855
- // image for specific web sites
7856
- $result = $this->db_do("
7857
- INSERT INTO $catalog_product_entity_varchar
7858
- (entity_type_id, attribute_id, store_id, entity_id, value)
7859
- (SELECT
7860
- $_getProductEntityTypeId,
7861
- $attr_image,
7862
- 0,
7863
- a.entity_id,
7864
- b.main_image_url
7865
- FROM $catalog_product_entity a
7866
- JOIN $products_temp b
7867
- ON a.store_product_id = b.store_product_id
7868
- )
7869
- ON DUPLICATE KEY UPDATE
7870
- value = b.main_image_url");
7871
-
7872
- echo("\n replaceMagentoProductsMultistoreMERGE 32\n");
7873
-
7874
- // small_image for specific web sites
7875
- $result = $this->db_do("
7876
- INSERT INTO $catalog_product_entity_varchar
7877
- (entity_type_id, attribute_id, store_id, entity_id, value)
7878
- (SELECT
7879
- $_getProductEntityTypeId,
7880
- $attr_small_image,
7881
- w.store_id,
7882
- a.entity_id,
7883
- b.medium_image_url
7884
- FROM $catalog_product_entity a
7885
- JOIN $core_store w
7886
- JOIN $products_temp b
7887
- ON a.store_product_id = b.store_product_id
7888
- )
7889
- ON DUPLICATE KEY UPDATE
7890
- value = b.medium_image_url");
7891
-
7892
- echo("\n replaceMagentoProductsMultistoreMERGE 33\n");
7893
-
7894
- // small_image for all web sites
7895
- $result = $this->db_do("
7896
- INSERT INTO $catalog_product_entity_varchar
7897
- (entity_type_id, attribute_id, store_id, entity_id, value)
7898
- (SELECT
7899
- $_getProductEntityTypeId,
7900
- $attr_small_image,
7901
- 0,
7902
- a.entity_id,
7903
- b.medium_image_url
7904
- FROM $catalog_product_entity a
7905
- JOIN $core_store w
7906
- JOIN $products_temp b
7907
- ON a.store_product_id = b.store_product_id
7908
- )
7909
- ON DUPLICATE KEY UPDATE
7910
- value = b.medium_image_url");
7911
-
7912
- echo("\n replaceMagentoProductsMultistoreMERGE 34\n");
7913
-
7914
- // thumbnail for specific web site
7915
- $result = $this->db_do("
7916
- INSERT INTO $catalog_product_entity_varchar
7917
- (entity_type_id, attribute_id, store_id, entity_id, value)
7918
- (SELECT
7919
- $_getProductEntityTypeId,
7920
- $attr_thumbnail,
7921
- w.store_id,
7922
- a.entity_id,
7923
- b.thumb_image_url
7924
- FROM $catalog_product_entity a
7925
- JOIN $core_store w
7926
- JOIN $products_temp b
7927
- ON a.store_product_id = b.store_product_id
7928
- )
7929
- ON DUPLICATE KEY UPDATE
7930
- value = b.thumb_image_url");
7931
-
7932
- echo("\n replaceMagentoProductsMultistoreMERGE 35\n");
7933
-
7934
- // thumbnail for all web sites
7935
- $result = $this->db_do("
7936
- INSERT INTO $catalog_product_entity_varchar
7937
- (entity_type_id, attribute_id, store_id, entity_id, value)
7938
- (SELECT
7939
- $_getProductEntityTypeId,
7940
- $attr_thumbnail,
7941
- 0,
7942
- a.entity_id,
7943
- b.thumb_image_url
7944
- FROM $catalog_product_entity a
7945
- JOIN $core_store w
7946
- JOIN $products_temp b
7947
- ON a.store_product_id = b.store_product_id
7948
- )
7949
- ON DUPLICATE KEY UPDATE
7950
- value = b.thumb_image_url");
7951
-
7952
- echo("\n replaceMagentoProductsMultistoreMERGE 36\n");
7953
-
7954
-
7955
- /* STP DELETE
7956
- //Refresh fulltext search
7957
- $result = $this->db_do("DROP TABLE IF EXISTS {$catalogsearch_fulltext}_tmp");
7958
- $result = $this->db_do("CREATE TEMPORARY TABLE IF NOT EXISTS {$catalogsearch_fulltext}_tmp LIKE $catalogsearch_fulltext");
7959
-
7960
-
7961
- echo("\n replaceMagentoProductsMultistoreMERGE 36.2\n");
7962
- $q = "
7963
- INSERT INTO {$catalogsearch_fulltext}_tmp
7964
- (product_id, store_id, data_index)
7965
- (SELECT
7966
- a.entity_id,
7967
- w.website,
7968
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
7969
- FROM $catalog_product_entity a
7970
- JOIN $products_website_temp w
7971
- ON a.store_product_id = w.store_product_id
7972
- LEFT JOIN $catalog_category_product b
7973
- ON a.entity_id = b.product_id
7974
- LEFT JOIN $catalog_category_entity_varchar c
7975
- ON b.category_id = c.entity_id
7976
- AND c.attribute_id = $cat_attr_name
7977
- LEFT JOIN $catalog_product_entity_varchar e
7978
- ON a.entity_id = e.entity_id
7979
- AND e.attribute_id = $attr_name
7980
- LEFT JOIN $catalog_product_website j
7981
- ON a.entity_id = j.product_id
7982
- LEFT JOIN $products_temp f
7983
- ON a.store_product_id = f.store_product_id
7984
- )
7985
- ON DUPLICATE KEY UPDATE
7986
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)";
7987
- echo("\n\n============================\n$q\n============================\n\n");
7988
-
7989
-
7990
- $result = $this->db_do("
7991
- INSERT INTO {$catalogsearch_fulltext}_tmp
7992
- (product_id, store_id, data_index)
7993
- (SELECT
7994
- a.entity_id,
7995
- w.website,
7996
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
7997
- FROM $catalog_product_entity a
7998
- JOIN $products_website_temp w
7999
- ON a.store_product_id = w.store_product_id
8000
- LEFT JOIN $catalog_category_product b
8001
- ON a.entity_id = b.product_id
8002
- LEFT JOIN $catalog_category_entity_varchar c
8003
- ON b.category_id = c.entity_id
8004
- AND c.attribute_id = $cat_attr_name
8005
- LEFT JOIN $catalog_product_entity_varchar e
8006
- ON a.entity_id = e.entity_id
8007
- AND e.attribute_id = $attr_name
8008
- LEFT JOIN $catalog_product_website j
8009
- ON a.entity_id = j.product_id
8010
- LEFT JOIN $products_temp f
8011
- ON a.store_product_id = f.store_product_id
8012
- )
8013
- ON DUPLICATE KEY UPDATE
8014
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)");
8015
-
8016
- echo("\n replaceMagentoProductsMultistoreMERGE 37\n");
8017
-
8018
-
8019
- $result = $this->db_do("
8020
- INSERT INTO {$catalogsearch_fulltext}_tmp
8021
- (product_id, store_id, data_index)
8022
- (SELECT
8023
- a.entity_id,
8024
- w.website,
8025
- CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
8026
- FROM $catalog_product_entity a
8027
- JOIN $products_website_temp w
8028
- ON a.store_product_id = w.store_product_id
8029
- LEFT JOIN $catalog_category_product b
8030
- ON a.entity_id = b.product_id
8031
- LEFT JOIN $catalog_category_entity_varchar c
8032
- ON b.category_id = c.entity_id
8033
- AND c.attribute_id = $cat_attr_name
8034
- LEFT JOIN $catalog_product_entity_varchar e
8035
- ON a.entity_id = e.entity_id
8036
- AND e.attribute_id = $attr_name
8037
- LEFT JOIN $products_temp f
8038
- ON a.store_product_id = f.store_product_id
8039
- )
8040
- ON DUPLICATE KEY UPDATE
8041
- data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)");
8042
-
8043
- echo("\n replaceMagentoProductsMultistoreMERGE 38\n");
8044
-
8045
- $result = $this->db_do("
8046
- DELETE cf
8047
- FROM $catalogsearch_fulltext cf
8048
- LEFT JOIN $catalog_product_entity cpe
8049
- ON cf.product_id = cpe.entity_id
8050
- WHERE cpe.entity_id IS NULL");
8051
-
8052
- echo("\n replaceMagentoProductsMultistoreMERGE 39\n");
8053
-
8054
- $result = $this->db_do("
8055
- INSERT INTO $catalogsearch_fulltext
8056
- (product_id, store_id, data_index)
8057
- (SELECT
8058
- a.product_id,
8059
- a.store_id,
8060
- a.data_index
8061
- FROM {$catalogsearch_fulltext}_tmp a
8062
- )
8063
- ON DUPLICATE KEY UPDATE
8064
- data_index = a.data_index");
8065
-
8066
- echo("\n replaceMagentoProductsMultistoreMERGE 40\n");
8067
-
8068
- $this->db_do("UPDATE $catalogsearch_query SET is_processed = 0");
8069
- //INNER JOIN eav_attribute_option_value d ON a.vendor_id = d.option_id
8070
- //TODO add something else
8071
- STP DELETE*/
8072
-
8073
- $this->addRelatedProducts();
8074
- echo("\n replaceMagentoProductsMultistoreMERGE 41\n");
8075
- }
8076
-
8077
- #################################################################################################
8078
-
8079
- function ParseProductsPicturesGallery()
8080
- {
8081
-
8082
- $parse_file = $this->varDir . FILE_PRODUCTS_PICTURES_GALLERY;
8083
- if (filesize($parse_file)) {
8084
- $this->_LOG("Start parse " . FILE_PRODUCTS_PICTURES_GALLERY);
8085
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('products_pictures_gallery_temp'));
8086
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('products_pictures_gallery_temp') . " (
8087
- sinch_product_id int(11),
8088
- image_url varchar(255),
8089
- thumb_image_url varchar(255),
8090
- store_product_id int(11),
8091
- key(sinch_product_id),
8092
- key(store_product_id)
8093
- )");
8094
-
8095
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
8096
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('products_pictures_gallery_temp') . "
8097
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
8098
- OPTIONALLY ENCLOSED BY '\"'
8099
- LINES TERMINATED BY \"\r\n\"
8100
- IGNORE 1 LINES ");
8101
-
8102
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_pictures_gallery_temp') . " ppgt
8103
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " sp
8104
- ON ppgt.sinch_product_id=sp.sinch_product_id
8105
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
8106
- ON sp.store_product_id=cpe.store_product_id
8107
- SET ppgt.store_product_id=sp.store_product_id");
8108
-
8109
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_pictures_gallery'));
8110
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('products_pictures_gallery_temp') . "
8111
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_products_pictures_gallery'));
8112
-
8113
- $this->_LOG("Finish parse" . FILE_PRODUCTS_PICTURES_GALLERY);
8114
- } else {
8115
- $this->_LOG("Wrong file" . $parse_file);
8116
- }
8117
- $this->_LOG(" ");
8118
-
8119
- }
8120
- #################################################################################################
8121
-
8122
- function ParseRestrictedValues()
8123
- {
8124
-
8125
- $parse_file = $this->varDir . FILE_RESTRICTED_VALUES;
8126
- if (filesize($parse_file) || $this->_ignore_restricted_values) {
8127
- $this->_LOG("Start parse " . FILE_RESTRICTED_VALUES);
8128
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('restricted_values_temp'));
8129
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('restricted_values_temp') . " (
8130
- restricted_value_id int(11),
8131
- category_feature_id int(11),
8132
- text text,
8133
- display_order_number int(11),
8134
- KEY(restricted_value_id),
8135
- KEY(category_feature_id)
8136
- )");
8137
- if (!$this->_ignore_restricted_values) {
8138
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
8139
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('restricted_values_temp') . "
8140
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
8141
- OPTIONALLY ENCLOSED BY '\"'
8142
- LINES TERMINATED BY \"\r\n\"
8143
- IGNORE 1 LINES ");
8144
- }
8145
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values'));
8146
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('restricted_values_temp') . "
8147
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values'));
8148
-
8149
- $this->_LOG("Finish parse " . FILE_RESTRICTED_VALUES);
8150
- } else {
8151
- $this->_LOG("Wrong file " . $parse_file);
8152
- }
8153
- $this->_LOG(" ");
8154
- }
8155
-
8156
- #################################################################################################
8157
-
8158
- function ParseStockAndPrices()
8159
- {
8160
-
8161
- $parse_file = $this->varDir . FILE_STOCK_AND_PRICES;
8162
-
8163
- if (filesize($parse_file)) {
8164
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp_before'));
8165
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp_before') . " (
8166
- store_product_id int(11),
8167
- stock int(11),
8168
- price varchar(255),
8169
- cost varchar(255),
8170
- distributor_id int(11),
8171
- KEY(store_product_id),
8172
- KEY(distributor_id)
8173
- )");
8174
-
8175
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
8176
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp_before') . "
8177
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
8178
- OPTIONALLY ENCLOSED BY '\"'
8179
- LINES TERMINATED BY \"\r\n\"
8180
- IGNORE 1 LINES ");
8181
-
8182
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp'));
8183
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " (
8184
- store_product_id int(11),
8185
- stock int(11),
8186
- price decimal(15,4),
8187
- cost decimal(15,4),
8188
- distributor_id int(11),
8189
- KEY(store_product_id),
8190
- KEY(distributor_id)
8191
- )");
8192
-
8193
- $this->db_do("
8194
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " (
8195
- store_product_id,
8196
- stock,
8197
- price,
8198
- cost,
8199
- distributor_id
8200
- )(
8201
- SELECT
8202
- store_product_id,
8203
- stock,
8204
- REPLACE(price, ',', '.'),
8205
- REPLACE(cost, ',', '.' ),
8206
- distributor_id
8207
- FROM " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp_before') . "
8208
- )
8209
- ");
8210
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp_before'));
8211
-
8212
- $this->replaceMagentoProductsStockPrice();
8213
-
8214
- $res = $this->db_do("SELECT count(*) as cnt
8215
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8216
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8217
- ON a.store_product_id=b.store_product_id");
8218
- $row = mysqli_fetch_assoc($res);
8219
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
8220
- SET number_of_products=" . $row['cnt'] . "
8221
- WHERE id=" . $this->current_import_status_statistic_id);
8222
-
8223
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_stock_and_prices'));
8224
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . "
8225
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_stock_and_prices'));
8226
-
8227
- $this->_LOG("Finish parse" . FILE_RELATED_PRODUCTS);
8228
- } else {
8229
- $this->_LOG("Wrong file" . $parse_file);
8230
- }
8231
- $this->_LOG(" ");
8232
- }
8233
-
8234
- #################################################################################################
8235
-
8236
- function replaceMagentoProductsStockPrice()
8237
- {
8238
- //Add stock
8239
- $connection = Mage::getModel('core/resource')->getConnection('core_write');
8240
- $result = $this->db_do("DELETE csi
8241
- FROM " . Mage::getSingleton('core/resource')->getTableName('cataloginventory_stock_item') . " csi
8242
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
8243
- ON csi.product_id=cpe.entity_id
8244
- WHERE cpe.entity_id is null");
8245
- //set all sinch products stock=0 before upgrade (nedds for dayly stock&price import)
8246
-
8247
- $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
8248
- JOIN " . Mage::getSingleton('core/resource')->getTableName('cataloginventory_stock_item') . " csi
8249
- ON cpe.entity_id=csi.product_id
8250
- SET
8251
- csi.qty=0,
8252
- csi.is_in_stock=0
8253
- WHERE cpe.store_product_id IS NOT NULL");
8254
-
8255
- $result = $this->db_do("
8256
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('cataloginventory_stock_item') . " (
8257
- product_id,
8258
- stock_id,
8259
- qty,
8260
- is_in_stock,
8261
- manage_stock
8262
- )(
8263
- SELECT
8264
- a.entity_id,
8265
- 1,
8266
- b.stock,
8267
- IF(b.stock > 0, 1, 0),
8268
- 1
8269
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8270
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8271
- ON a.store_product_id=b.store_product_id
8272
- )
8273
- ON DUPLICATE KEY UPDATE
8274
- qty=b.stock,
8275
- is_in_stock = IF(b.stock > 0, 1, 0),
8276
- manage_stock = 1
8277
- ");
8278
-
8279
-
8280
- $result = $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('cataloginventory_stock_status'));
8281
-
8282
- $result = $this->db_do("
8283
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('cataloginventory_stock_status') . " (
8284
- product_id,
8285
- website_id,
8286
- stock_id,
8287
- qty,
8288
- stock_status
8289
- )(
8290
- SELECT
8291
- a.product_id,
8292
- w.website_id,
8293
- 1,
8294
- a.qty,
8295
- IF(qty > 0, 1, 0)
8296
- FROM " . Mage::getSingleton('core/resource')->getTableName('cataloginventory_stock_item') . " a
8297
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " b
8298
- ON a.product_id=b.entity_id
8299
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
8300
- ON b.store_product_id=w.store_product_id
8301
- )
8302
- ON DUPLICATE KEY UPDATE
8303
- qty=a.qty,
8304
- stock_status = IF(a.qty > 0, 1, 0)
8305
- ");
8306
-
8307
- //Add prices
8308
- //$result = $this->db_do("truncate catalog_product_entity_decimal");
8309
- $result = $this->db_do("DELETE cped
8310
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " cped
8311
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
8312
- ON cped.entity_id=cpe.entity_id
8313
- WHERE cpe.entity_id IS NULL");
8314
-
8315
- $result = $this->db_do("
8316
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
8317
- entity_type_id,
8318
- attribute_id,
8319
- store_id,
8320
- entity_id,
8321
- value
8322
- )(
8323
- SELECT
8324
- " . $this->_getProductEntityTypeId() . ",
8325
- " . $this->_getProductAttributeId('price') . ",
8326
- w.website,
8327
- a.entity_id,
8328
- b.price
8329
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8330
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8331
- ON a.store_product_id=b.store_product_id
8332
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
8333
- ON a.store_product_id=w.store_product_id
8334
- )
8335
- ON DUPLICATE KEY UPDATE
8336
- value = b.price
8337
- ");
8338
-
8339
- $result = $this->db_do("
8340
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
8341
- entity_type_id,
8342
- attribute_id,
8343
- store_id,
8344
- entity_id,
8345
- value
8346
- )(
8347
- SELECT
8348
- " . $this->_getProductEntityTypeId() . ",
8349
- " . $this->_getProductAttributeId('price') . ",
8350
- 0,
8351
- a.entity_id,
8352
- b.price
8353
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8354
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8355
- ON a.store_product_id=b.store_product_id
8356
- )
8357
- ON DUPLICATE KEY UPDATE
8358
- value = b.price
8359
- ");
8360
- //Add cost
8361
- // $result = $this->db_do("truncate catalog_product_entity_decimal");
8362
- $result = $this->db_do("
8363
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
8364
- entity_type_id,
8365
- attribute_id,
8366
- store_id,
8367
- entity_id,
8368
- value
8369
- )(
8370
- SELECT
8371
- " . $this->_getProductEntityTypeId() . ",
8372
- " . $this->_getProductAttributeId('cost') . ",
8373
- w.website,
8374
- a.entity_id,
8375
- b.cost
8376
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8377
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8378
- ON a.store_product_id=b.store_product_id
8379
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
8380
- ON a.store_product_id=w.store_product_id
8381
- )
8382
- ON DUPLICATE KEY UPDATE
8383
- value = b.cost
8384
- ");
8385
-
8386
- $result = $this->db_do("
8387
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
8388
- entity_type_id,
8389
- attribute_id,
8390
- store_id,
8391
- entity_id,
8392
- value
8393
- )(
8394
- SELECT
8395
- " . $this->_getProductEntityTypeId() . ",
8396
- " . $this->_getProductAttributeId('cost') . ",
8397
- 0,
8398
- a.entity_id,
8399
- b.cost
8400
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8401
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8402
- ON a.store_product_id=b.store_product_id
8403
- )
8404
- ON DUPLICATE KEY UPDATE
8405
- value = b.cost
8406
- ");
8407
-
8408
- //make products enable in FO
8409
- // $result = $this->db_do(" truncate catalog_product_index_price");
8410
- $result = $this->db_do("DELETE cpip
8411
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_price') . " cpip
8412
- LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
8413
- ON cpip.entity_id=cpe.entity_id
8414
- WHERE cpe.entity_id IS NULL");
8415
-
8416
- $q = "SELECT customer_group_id FROM " . Mage::getSingleton('core/resource')->getTableName('customer_group');
8417
- $quer = $this->db_do($q);
8418
-
8419
- while ($row = mysqli_fetch_assoc($quer)) {
8420
- $result = $this->db_do("
8421
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_price') . " (
8422
- entity_id,
8423
- customer_group_id,
8424
- website_id,
8425
- tax_class_id,
8426
- price,
8427
- final_price,
8428
- min_price,
8429
- max_price
8430
- )(SELECT
8431
- a.entity_id,
8432
- " . $row['customer_group_id'] . ",
8433
- w.website_id,
8434
- 2,
8435
- b.price ,
8436
- b.price ,
8437
- b.price ,
8438
- b.price
8439
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
8440
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stock_and_prices_temp') . " b
8441
- ON a.store_product_id=b.store_product_id
8442
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
8443
- ON a.store_product_id=w.store_product_id
8444
- )
8445
- ON DUPLICATE KEY UPDATE
8446
- tax_class_id = 2,
8447
- price = b.price,
8448
- final_price = b.price,
8449
- min_price = b.price,
8450
- max_price = b.price
8451
- ");
8452
- }
8453
- }
8454
-
8455
- #################################################################################################
8456
-
8457
- private function _cleanCateoryProductFlatTable()
8458
- {
8459
- $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
8460
- $q = 'SHOW TABLES LIKE "' . Mage::getSingleton('core/resource')->getTableName('catalog_product_flat_') . '%"';
8461
- $quer = $this->db_do($q);
8462
- $result = false;
8463
- While ($row = mysqli_fetch_array($quer)) {
8464
- if (is_array($row)) {
8465
- $catalog_product_flat = array_pop($row);
8466
- $q = 'DELETE pf1 FROM ' . $catalog_product_flat . ' pf1
8467
- LEFT JOIN ' . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . ' p
8468
- ON pf1.entity_id = p.entity_id
8469
- WHERE p.entity_id IS NULL;';
8470
- $this->db_do($q);
8471
- $this->_LOG('cleaned wrong rows from ' . $catalog_product_flat);
8472
- }
8473
- }
8474
- return $result;
8475
-
8476
- }
8477
-
8478
- #################################################################################################
8479
-
8480
- public function runIndexer()
8481
- {
8482
- $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('core_url_rewrite'));
8483
- $this->db_do("SET FOREIGN_KEY_CHECKS=0");
8484
- $this->db_do("TRUNCATE TABLE " . Mage::getSingleton('core/resource')->getTableName('core_url_rewrite'));
8485
- $this->db_do("SET FOREIGN_KEY_CHECKS=1");
8486
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
8487
- SET value = ''
8488
- WHERE entity_type_id=" . $this->_getProductEntityTypeId() . " AND attribute_id=" . $this->_getProductAttributeId('url_key'));
8489
- exec(PHP_RUN_STRING . ' ' . $this->shellDir . 'indexer.php reindexall');
8490
- }
8491
-
8492
- #################################################################################################
8493
-
8494
- function cron_start_full_import()
8495
- {
8496
- $this->import_run_type = 'CRON';
8497
- $this->run_sinch_import();
8498
- }
8499
-
8500
- #################################################################################################
8501
-
8502
- function cron_start_stock_price_import()
8503
- {
8504
- $this->import_run_type = 'CRON';
8505
- $this->run_stock_price_sinch_import();
8506
- }
8507
-
8508
- ##################################################################################################
8509
-
8510
- function run_stock_price_sinch_import()
8511
- {
8512
- $safe_mode_set = ini_get('safe_mode');
8513
-
8514
- $this->InitImportStatuses('PRICE STOCK');
8515
- if ($safe_mode_set) {
8516
- $this->_LOG('safe_mode is enable. import stoped.');
8517
- $this->set_import_error_reporting_message('Safe_mode is enabled. Please check the documentation on how to fix this. Import stopped.');
8518
- exit;
8519
- }
8520
- $store_proc = $this->check_store_procedure_exist();
8521
-
8522
- if (!$store_proc) {
8523
- $this->_LOG('store prcedure "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '" is absent in this database. import stoped.');
8524
- $this->set_import_error_reporting_message('Stored procedure "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '" is absent in this database. Import stopped.');
8525
- exit;
8526
- }
8527
-
8528
- $file_privileg = $this->check_db_privileges();
8529
-
8530
- if (!$file_privileg) {
8531
- $this->_LOG("Loaddata option not set - please check the documentation on how to fix this. You dan't have privileges for LOAD DATA.");
8532
- $this->set_import_error_reporting_message("Loaddata option not set - please check the documentation on how to fix this. Import stopped.");
8533
- exit;
8534
- }
8535
- $local_infile = $this->check_local_infile();
8536
- if (!$local_infile) {
8537
- $this->_LOG("Loaddata option not set - please check the documentation on how to fix this. Add this string to 'set-variable=local-infile=0' in '/etc/my.cnf'");
8538
- $this->set_import_error_reporting_message("Loaddata option not set - please check the documentation on how to fix this. Import stopped.");
8539
- exit;
8540
- }
8541
-
8542
- if ($this->is_imort_not_run() && $this->is_full_import_have_been_run()) {
8543
- try {
8544
- //$this->InitImportStatuses();
8545
- $q = "SELECT GET_LOCK('sinchimport', 30)";
8546
- $quer = $this->db_do($q);
8547
- $import = $this;
8548
- $import->addImportStatus('Stock Price Start Import');
8549
- echo "Upload Files <br>";
8550
- $this->files = array(
8551
- FILE_STOCK_AND_PRICES,
8552
- FILE_PRICE_RULES
8553
- );
8554
-
8555
- $import->UploadFiles();
8556
- $import->addImportStatus('Stock Price Upload Files');
8557
-
8558
- echo "Parse Stock And Prices <br>";
8559
- //exit;
8560
- $import->ParseStockAndPrices();
8561
- $import->addImportStatus('Stock Price Parse Products');
8562
-
8563
- echo "Apply Customer Group Price <br>";
8564
- //$import->ParsePriceRules();
8565
- //$import->AddPriceRules();
8566
- //$import->ApplyCustomerGroupPrice();
8567
-
8568
- $ftpCred = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
8569
- Mage::dispatchEvent('sinch_pricerules_import_ftp', array(
8570
- 'ftp_host' => $ftpCred["ftp_server"],
8571
- 'ftp_username' => $ftpCred["login"],
8572
- 'ftp_password' => $ftpCred["password"]
8573
- ));
8574
-
8575
-
8576
- Mage::log("Finish Stock & Price Sinch import", null, $this->_logFile);
8577
- echo "Finish Stock & Price Sinch import<br>";
8578
-
8579
- Mage::log("Start indexing Stock & Price", null, $this->_logFile);
8580
- echo "Start indexing Stock & Price<br>";
8581
- $import->_cleanCateoryProductFlatTable();
8582
- $import->runStockPriceIndexer();
8583
- Mage::log("Finish indexing Stock & Price", null, $this->_logFile);
8584
- $import->addImportStatus('Stock Price Indexing data');
8585
- $import->addImportStatus('Stock Price Finish import', 1);
8586
- echo "Finish indexing Stock & Price<br>";
8587
-
8588
- $q = "SELECT RELEASE_LOCK('sinchimport')";
8589
- $quer = $this->db_do($q);
8590
- } catch (Exception $e) {
8591
- $this->set_import_error_reporting_message($e);
8592
- }
8593
- } else {
8594
- if (!$this->is_imort_not_run()) {
8595
- Mage::log("Sinchimport already run", null, $this->_logFile);
8596
- echo "Sinchimport already run<br>";
8597
- } else {
8598
- Mage::log("Full import have never finished with success", null, $this->_logFile);
8599
- echo "Full import have never finished with success<br>";
8600
- }
8601
- }
8602
-
8603
- }
8604
-
8605
- ##################################################################################################
8606
-
8607
- function is_full_import_have_been_run()
8608
- {
8609
- $q = "SELECT COUNT(*) AS cnt
8610
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_import_status_statistic') . "
8611
- WHERE import_type='FULL' AND global_status_import='Successful'";
8612
- $quer = $this->db_do($q);
8613
- $row = mysqli_fetch_array($quer);
8614
- if ($row['cnt'] > 0) {
8615
- return true;
8616
- } else {
8617
- return false;
8618
- }
8619
- }
8620
-
8621
- ##################################################################################################
8622
-
8623
- public function runStockPriceIndexer()
8624
- {
8625
- exec(PHP_RUN_STRING . ' ' . $this->shellDir . 'indexer.php --reindex catalog_product_price,cataloginventory_stock');
8626
- }
8627
-
8628
- ##################################################################################################
8629
-
8630
- function ParsePriceRules()
8631
- {
8632
- $parse_file = $this->varDir . FILE_PRICE_RULES;
8633
- if (filesize($parse_file) || $this->_ignore_price_rules) {
8634
- $this->_LOG("Start parse " . FILE_PRICE_RULES);
8635
-
8636
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp'));
8637
- $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . "(
8638
- `id` int(11) NOT NULL,
8639
- `price_from` decimal(10,2) DEFAULT NULL,
8640
- `price_to` decimal(10,2) DEFAULT NULL,
8641
- `category_id` int(10) unsigned DEFAULT NULL,
8642
- `vendor_id` int(11) DEFAULT NULL,
8643
- `vendor_product_id` varchar(255) DEFAULT NULL,
8644
- `customergroup_id` varchar(32) DEFAULT NULL,
8645
- `marge` decimal(10,2) DEFAULT NULL,
8646
- `fixed` decimal(10,2) DEFAULT NULL,
8647
- `final_price` decimal(10,2) DEFAULT NULL,
8648
- PRIMARY KEY (`id`),
8649
- UNIQUE KEY `price_from` (`price_from`,`price_to`,`vendor_id`,`category_id`,`vendor_product_id`,`customergroup_id`),
8650
- KEY `vendor_product_id` (`vendor_product_id`),
8651
- KEY `category_id` (`category_id`)
8652
- )
8653
- ");
8654
- if (!$this->_ignore_price_rules) {
8655
-
8656
- $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
8657
- INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . "
8658
- FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
8659
- OPTIONALLY ENCLOSED BY '\"'
8660
- LINES TERMINATED BY \"\r\n\"
8661
- IGNORE 1 LINES
8662
- (id, @vprice_from, @vprice_to, @vcategory_id, @vvendor_id, @vvendor_product_id, @vcustomergroup_id, @vmarge, @vfixed, @vfinal_price)
8663
- SET price_from = nullif(@vprice_from,''),
8664
- price_to = nullif(@vprice_to,''),
8665
- category_id = nullif(@vcategory_id,''),
8666
- vendor_id = nullif(@vvendor_id,''),
8667
- vendor_product_id = nullif(@vvendor_product_id,''),
8668
- customergroup_id = nullif(@vcustomergroup_id,''),
8669
- marge = nullif(@vmarge,''),
8670
- fixed = nullif(@vfixed,''),
8671
- final_price = nullif(@vfinal_price,'')
8672
- ");
8673
- }
8674
-
8675
- $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . "
8676
- ADD COLUMN `shop_category_id` int(10) unsigned DEFAULT NULL,
8677
- ADD COLUMN `shop_vendor_id` int(11) DEFAULT NULL,
8678
- ADD COLUMN `shop_vendor_product_id` varchar(255) DEFAULT NULL,
8679
- ADD COLUMN `shop_customergroup_id` varchar(32) DEFAULT NULL
8680
- ");
8681
-
8682
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . " prt
8683
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
8684
- ON prt.category_id = cce.store_category_id
8685
- SET prt.shop_category_id = cce.entity_id");
8686
-
8687
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . " prt
8688
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " sicm
8689
- ON prt.vendor_id = sicm.sinch_manufacturer_id
8690
- SET prt.shop_vendor_id = sicm.shop_option_id");
8691
-
8692
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . " prt
8693
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " sicpm
8694
- ON prt.vendor_product_id = sicpm.product_sku
8695
- SET prt.shop_vendor_product_id = sicpm.sku");
8696
-
8697
- $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . " prt
8698
- JOIN " . Mage::getSingleton('core/resource')->getTableName('customer_group') . " cg
8699
- ON prt.customergroup_id = cg.customer_group_id
8700
- SET prt.shop_customergroup_id = cg.customer_group_id");
8701
-
8702
- $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . "
8703
- WHERE
8704
- (category_id IS NOT NULL AND shop_category_id IS NULL) OR
8705
- (vendor_id IS NOT NULL AND shop_vendor_id IS NULL) OR
8706
- (vendor_product_id IS NOT NULL AND shop_vendor_product_id IS NULL) OR
8707
- (customergroup_id IS NOT NULL AND shop_customergroup_id IS NULL)
8708
- ");
8709
-
8710
-
8711
- $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_price_rules'));
8712
- $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('price_rules_temp') . "
8713
- TO " . Mage::getSingleton('core/resource')->getTableName('stINch_price_rules'));
8714
-
8715
- $this->_LOG("Finish parse " . FILE_PRICE_RULES);
8716
- } else {
8717
- $this->_LOG("Wrong file " . $parse_file);
8718
- }
8719
- $this->_LOG(" ");
8720
- }
8721
-
8722
- #################################################################################################
8723
-
8724
- function AddPriceRules()
8725
- {
8726
- if (!$this->check_table_exist('import_pricerules_standards')) {
8727
- return;
8728
- }
8729
-
8730
- $result = $this->db_do("
8731
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('import_pricerules') . " (
8732
- id,
8733
- price_from,
8734
- price_to,
8735
- vendor_id,
8736
- category_id,
8737
- vendor_product_id,
8738
- customergroup_id,
8739
- marge,
8740
- fixed,
8741
- final_price
8742
- )(SELECT
8743
- id,
8744
- price_from,
8745
- price_to,
8746
- shop_vendor_id,
8747
- shop_category_id,
8748
- shop_vendor_product_id,
8749
- shop_customergroup_id,
8750
- marge,
8751
- fixed,
8752
- final_price
8753
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_price_rules') . " a
8754
- )
8755
- ON DUPLICATE KEY UPDATE
8756
- id = a.id,
8757
- price_from = a.price_from,
8758
- price_to = a.price_to,
8759
- vendor_id = a.shop_vendor_id,
8760
- category_id = a.shop_category_id,
8761
- vendor_product_id = a.shop_vendor_product_id,
8762
- customergroup_id = a.shop_customergroup_id,
8763
- marge = a.marge,
8764
- fixed = a.fixed,
8765
- final_price = a.final_price
8766
- ");
8767
-
8768
- }
8769
-
8770
- #################################################################################################
8771
-
8772
- private function check_table_exist($table)
8773
- {
8774
-
8775
- $q = "SHOW TABLES LIKE \"%" . Mage::getSingleton('core/resource')->getTableName($table) . "%\"";
8776
- // echo $q;
8777
- $quer = $this->db_do($q);
8778
- $i = 0;
8779
- while ($row = mysqli_fetch_array($quer)) {
8780
- $i++;
8781
- }
8782
- return ($i);
8783
- }
8784
-
8785
- #################################################################################################
8786
-
8787
- function ApplyCustomerGroupPrice()
8788
- {
8789
- if (!$this->check_table_exist('import_pricerules_standards')) {
8790
- return;
8791
- }
8792
- $this->_getProductsForCustomerGroupPrice();
8793
- $pricerulesArray = $this->_getPricerulesList();
8794
- if (is_array($pricerulesArray)) {
8795
- $this->db_do("TRUNCATE TABLE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_group_price'));
8796
- $this->db_do("TRUNCATE TABLE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_group_price'));
8797
-
8798
- }
8799
- // $i=1;
8800
- foreach ($pricerulesArray as $pricerule) {
8801
- $this->_LOG("Calculation group price for rule " . $pricerule['id'] . "
8802
- (\nname = " . $pricerule['name'] . "
8803
- \nfinal_price = " . $pricerule['final_price'] . "
8804
- \nprice_from = " . $pricerule['price_from'] . "
8805
- \nprice_to = " . $pricerule['price_to'] . "
8806
- \nvendor_id = " . $pricerule['vendor_id'] . "
8807
- \ncategory_id = " . $pricerule['category_id'] . "
8808
- \nproduct_entity_id = " . $pricerule['product_entity_id'] . "
8809
- \nvendor_product_id = " . $pricerule['vendor_product_id'] . "
8810
- \ncustomergroup_id = " . $pricerule['customergroup_id'] . "
8811
- \ndistributor_id = " . $pricerule['distributor_id'] . "
8812
- \nrating = " . $pricerule['rating'] . "
8813
- \nmarge = " . $pricerule['marge'] . "
8814
- \nfixed = " . $pricerule['fixed'] . "
8815
- \nallow_subcat = " . $pricerule['allow_subcat'] . "
8816
- \nstore_id = " . $pricerule['store_id'] . "
8817
- )");
8818
-
8819
- $vendor_product_id_str = "'" . str_replace(';', "','", $pricerule['vendor_product_id']) . "'";
8820
- $where = "";
8821
- if (empty($pricerule['marge'])) $marge = "NULL";
8822
- else $marge = $pricerule['marge'];
8823
-
8824
- if (empty($pricerule['fixed'])) $fixed = "NULL";
8825
- else $fixed = $pricerule['fixed'];
8826
-
8827
- if (empty($pricerule['final_price'])) $final_price = "NULL";
8828
- else $final_price = $pricerule['final_price'];
8829
-
8830
- if (!empty($pricerule['price_from'])) $where .= " AND a.price > " . $pricerule['price_from'];
8831
-
8832
- if (!empty($pricerule['price_to'])) $where .= " AND a.price < " . $pricerule['price_to'];
8833
-
8834
- if (!empty($pricerule['vendor_id'])) $where .= " AND a.manufacturer_id = " . $pricerule['vendor_id'];
8835
-
8836
- //if(!empty($pricerule['vendor_product_id']))
8837
- // $where.= " AND vendor_product_id = ".$pricerule['vendor_product_id'];
8838
- if (!empty($pricerule['product_entity_id'])) $where .= " AND a.product_id = '" . $pricerule['product_entity_id'] . "'";
8839
-
8840
- // if (!empty($pricerule['vendor_product_id'])) $where.= " AND a.sku = '".$pricerule['vendor_product_id']."'";
8841
- if (!empty($pricerule['vendor_product_id'])) $where .= " AND a.sku IN (" . $vendor_product_id_str . ")";
8842
-
8843
- if (!empty($pricerule['allow_subcat'])) {
8844
- if (!empty($pricerule['category_id'])) {
8845
- $children_cat = $this->get_all_children_cat($pricerule['category_id']);
8846
- $where .= " AND a.category_id IN (" . $children_cat . ")";
8847
- }
8848
- } else {
8849
- if (!empty($pricerule['category_id'])) $where .= " AND a.category_id = " . $pricerule['category_id'];
8850
- }
8851
-
8852
-
8853
- // if (!empty($pricerule['store_id'])) $where.= " AND store_id = ".$pricerule['store_id'];
8854
-
8855
- // if (!empty($pricerule['distributor_id'])) $where.= " AND distributor_id = ".$pricerule['distributor_id'];
8856
-
8857
- // $this->createCalcPriceFunc();
8858
- //echo "\n\nAAAAAAAAAAAAAAAAAAAAa".$pricerule['customergroup_id']."----------";
8859
- $customer_group_id_array = array();
8860
- if (strstr($pricerule['customergroup_id'], ",")) {
8861
- //echo "55555555555555555";
8862
- $customer_group_id_array = explode(",", $pricerule['customergroup_id']);
8863
- } else {
8864
- $customer_group_id_array[0] = $pricerule['customergroup_id'];
8865
- }
8866
- // var_dump($pricerule);
8867
- // echo "CCCCCCC\n";
8868
- // var_dump($customer_group_id_array);
8869
- foreach ($customer_group_id_array as $customer_group_id) {
8870
- if (isset($customer_group_id) && $customer_group_id >= 0) {
8871
- $query = "
8872
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_group_price') . " (entity_id,
8873
- all_groups,
8874
- customer_group_id,
8875
- value,
8876
- website_id
8877
- )
8878
- (SELECT
8879
- a.product_id,
8880
- 0,
8881
- " . $customer_group_id . ",
8882
- " . Mage::getSingleton('core/resource')->getTableName('func_calc_price') . "(
8883
- a.price,
8884
- " . $marge . " ,
8885
- " . $fixed . ",
8886
- " . $final_price . "),
8887
- 0
8888
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp') . " a
8889
- WHERE true " . $where . "
8890
- )
8891
- ON DUPLICATE KEY UPDATE
8892
- value =
8893
- " . Mage::getSingleton('core/resource')->getTableName('func_calc_price') . "(
8894
- a.price,
8895
- " . $marge . " ,
8896
- " . $fixed . ",
8897
- " . $final_price . ")
8898
- ";
8899
- // echo "\n\n".$query;
8900
- $this->db_do($query);
8901
- if (!empty($pricerule['store_id']) && $pricerule['store_id'] > 0) {
8902
- $query = "
8903
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_group_price') . " (entity_id,
8904
- customer_group_id,
8905
- price,
8906
- website_id
8907
- )
8908
- (SELECT
8909
- a.product_id,
8910
- " . $customer_group_id . ",
8911
- " . Mage::getSingleton('core/resource')->getTableName('func_calc_price') . "(
8912
- a.price,
8913
- " . $marge . " ,
8914
- " . $fixed . ",
8915
- " . $final_price . "),
8916
- " . $pricerule['store_id'] . "
8917
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp') . " a
8918
- WHERE true " . $where . "
8919
- )
8920
- ON DUPLICATE KEY UPDATE
8921
- price =
8922
- " . Mage::getSingleton('core/resource')->getTableName('func_calc_price') . "(
8923
- a.price,
8924
- " . $marge . " ,
8925
- " . $fixed . ",
8926
- " . $final_price . ")
8927
- ";
8928
- // echo "\n\n".$query;
8929
- $this->db_do($query);
8930
-
8931
- }
8932
- }
8933
- }
8934
- }
8935
-
8936
- }
8937
-
8938
- #################################################################################################
8939
-
8940
- function _getProductsForCustomerGroupPrice()
8941
- {
8942
- // TEMPORARY
8943
- $this->db_do(" DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp'));
8944
- $this->db_do("
8945
- CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp') . "
8946
- (
8947
- `category_id` int(10) unsigned NOT NULL default '0',
8948
- `product_id` int(10) unsigned NOT NULL default '0',
8949
- `store_product_id` int(10) NOT NULL default '0',
8950
- `sku` varchar(64) DEFAULT NULL COMMENT 'SKU',
8951
- `manufacturer_id` int(10) NOT NULL default '0',
8952
- `price` decimal(15,4) DEFAULT NULL,
8953
- UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`product_id`,`category_id`),
8954
- KEY `CATALOG_CATEGORY_PRODUCT_CATEGORY` (`category_id`),
8955
- KEY `CATALOG_CATEGORY_PRODUCT_PRODUCT` (`product_id`)
8956
- )");
8957
-
8958
- $result = $this->db_do("
8959
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp') . "
8960
- (category_id, product_id, store_product_id, sku)
8961
- (SELECT
8962
- ccp.category_id,
8963
- ccp.product_id,
8964
- cpe.store_product_id,
8965
- cpe.sku
8966
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
8967
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
8968
- ON ccp.product_id = cpe.entity_id
8969
- WHERE cpe.store_product_id IS NOT NULL)");
8970
-
8971
- $result = $this->db_do("
8972
- UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp') . " pfcgpt
8973
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " cpei
8974
- ON pfcgpt.product_id = cpei.entity_id
8975
- AND cpei.entity_type_id = " . $this->_getProductEntityTypeId() . "
8976
- AND cpei.attribute_id = " . $this->_getProductAttributeId('manufacturer') . "
8977
- SET pfcgpt.manufacturer_id = cpei.value
8978
- ");
8979
-
8980
- $result = $this->db_do("
8981
- UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_for_customer_group_price_temp') . " pfcgpt
8982
- JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " cped
8983
- ON pfcgpt.product_id = cped.entity_id
8984
- AND cped.entity_type_id = " . $this->_getProductEntityTypeId() . "
8985
- AND cped.attribute_id = " . $this->_getProductAttributeId('price') . "
8986
- SET pfcgpt.price = cped.value
8987
- ");
8988
-
8989
-
8990
- }
8991
- #################################################################################################
8992
-
8993
- protected function _getPricerulesList()
8994
- {
8995
- $rulesArray = array();
8996
- $result = $this->db_do("
8997
- SELECT *
8998
- FROM " . Mage::getSingleton('core/resource')->getTableName('import_pricerules') . "
8999
- ORDER BY rating DESC
9000
- ");
9001
- while ($row = mysqli_fetch_assoc($result)) {
9002
- $rulesArray[$row['id']] = $row;
9003
- }
9004
- return $rulesArray;
9005
- }
9006
-
9007
- #################################################################################################
9008
-
9009
- private function get_all_children_cat($entity_id)
9010
- {
9011
- $children_cat = "'" . $entity_id . "'" . $this->get_all_children_cat_recursive($entity_id);
9012
- return ($children_cat);
9013
- }
9014
-
9015
- #################################################################################################
9016
-
9017
- private function get_all_children_cat_recursive($entity_id)
9018
- {
9019
- $q = "SELECT entity_id
9020
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
9021
- WHERE parent_id=" . $entity_id;
9022
- $quer = $this->db_do($q);
9023
- $children_cat = '';
9024
- while ($row = mysqli_fetch_array($quer)) {
9025
- $children_cat .= ", '" . $row['entity_id'] . "'";
9026
- $children_cat .= $this->get_all_children_cat_recursive($row['entity_id']);
9027
- }
9028
- return ($children_cat);
9029
- }
9030
-
9031
- #################################################################################################
9032
-
9033
- function getProductDescription($entity_id)
9034
- {
9035
-
9036
- $this->loadProductParams($entity_id);
9037
- $this->loadProductStarfeatures($entity_id);
9038
- $this->loadGalleryPhotos($entity_id);
9039
- Varien_Profiler::start('Bintime FILE RELATED');
9040
- $this->loadRelatedProducts($entity_id);
9041
- Varien_Profiler::stop('Bintime FILE RELATED');
9042
-
9043
- return true;
9044
- }
9045
-
9046
- #################################################################################################
9047
-
9048
- private function loadProductParams($entity_id)
9049
- {
9050
- $store_product_id = $this->getStoreProductIdByEntity($entity_id);
9051
- if (!$store_product_id) {
9052
- // echo "AAAAAAA"; exit;
9053
- return;
9054
- }
9055
- $q = "SELECT
9056
- sinch_product_id,
9057
- product_sku,
9058
- product_name,
9059
- sinch_manufacturer_id,
9060
- store_category_id,
9061
- main_image_url,
9062
- thumb_image_url,
9063
- medium_image_url,
9064
- specifications,
9065
- description,
9066
- specifications
9067
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . "
9068
- WHERE store_product_id =" . $store_product_id;
9069
- $quer = $this->db_do($q);
9070
- $product = mysqli_fetch_array($quer);
9071
-
9072
- $this->productDescription = (string)substr($product['description'], 50, 0);
9073
- $this->fullProductDescription = (string)$product['description'];
9074
- $this->lowPicUrl = (string)$product["medium_image_url"];//thumb_image_url"];
9075
- $this->highPicUrl = (string)$product["main_image_url"];
9076
- $this->productName = (string)$product["product_name"];
9077
- $this->productId = (string)$product['product_sku'];
9078
- $this->specifications = (string)$product['specifications'];
9079
- $this->sinchProductId = (string)$product['sinch_product_id'];
9080
- if ($product['sinch_manufacturer_id']) {
9081
- $q = "SELECT manufacturer_name
9082
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . "
9083
- WHERE sinch_manufacturer_id=" . $product['sinch_manufacturer_id'];
9084
- $quer = $this->db_do($q);
9085
- $manufacturer = mysqli_fetch_array($quer);
9086
- $this->vendor = (string)$manufacturer['manufacturer_name'];
9087
- }
9088
- $q = "SELECT DISTINCT ean_code
9089
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes') . " sec
9090
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " sp
9091
- ON sec.product_id=sp.sinch_product_id
9092
- WHERE sp.store_product_id=" . $store_product_id;
9093
- $quer = $this->db_do($q);
9094
- while ($row = mysqli_fetch_array($quer)) {
9095
- $EANarr[] = $row['ean_code'];
9096
- }
9097
- // $prodEAN = $productTag->EANCode;
9098
- $EANstr = '';
9099
- /* $EANarr=null;
9100
- foreach($prodEAN as $ellEAN){
9101
- $EANarr[]=$ellEAN['EAN'];
9102
- }
9103
- */
9104
- $EANstr = implode(", ", $EANarr);
9105
- $this->EAN = (string)$EANstr;//$productTag->EANCode['EAN'];
9106
- }
9107
-
9108
- #################################################################################################
9109
-
9110
- private function getStoreProductIdByEntity($entity_id)
9111
- {
9112
- $q = $this->db_do("SELECT store_product_id
9113
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . "
9114
- WHERE entity_id=" . $entity_id);
9115
- $res = mysqli_fetch_array($q);
9116
- // echo $entity_id."AAAA".$res['store_product_id']; exit;
9117
- return ($res['store_product_id']);
9118
- }
9119
-
9120
- #################################################################################################
9121
-
9122
- private function loadProductStarfeatures($entity_id)
9123
- {
9124
- $descriptionArray = array();
9125
- $product_info_features = $this->db_do("
9126
- SELECT c.feature_name AS name, b.text AS value
9127
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_product_features') . " a
9128
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_restricted_values') . " b
9129
- ON a.restricted_value_id = b.restricted_value_id
9130
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_features') . " c
9131
- ON b.category_feature_id = c.category_feature_id
9132
- WHERE a.sinch_product_id = '" . $this->sinchProductId . "'");
9133
- while ($features = mysqli_fetch_array($product_info_features)) {
9134
- $descriptionArray[$features['name']] = $features['value'];
9135
- }
9136
-
9137
-
9138
- $this->productDescriptionList = $descriptionArray;
9139
- }
9140
-
9141
- #################################################################################################
9142
-
9143
- /**
9144
- * load Gallery array from XML
9145
- */
9146
- public function loadGalleryPhotos($entity_id)
9147
- {
9148
- /*$galleryPhotos = $this->simpleDoc->Product->ProductGallery->ProductPicture;
9149
- if (!count($galleryPhotos)){
9150
- return false;
9151
- }
9152
- */
9153
- $store_product_id = $this->getStoreProductIdByEntity($entity_id);
9154
- if (!$store_product_id) {
9155
- return;
9156
- }
9157
- $q = $this->db_do("SELECT COUNT(*) AS cnt
9158
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_products_pictures_gallery') . "
9159
- WHERE store_product_id=" . $store_product_id);
9160
-
9161
- $res = mysqli_fetch_array($q);
9162
- if (!$res || !$res['cnt']) {
9163
- return false;
9164
- }
9165
- $q = "SELECT
9166
- image_url as Pic,
9167
- thumb_image_url as ThumbPic
9168
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_products_pictures_gallery') . "
9169
- WHERE store_product_id=" . $store_product_id;
9170
-
9171
- $res = $this->db_do($q);
9172
-
9173
- while ($photo = mysqli_fetch_array($res)) {
9174
- $picHeight = (int)500;//$photo["PicHeight"];
9175
- $picWidth = (int)500;//$photo["PicWidth"];
9176
- $thumbUrl = (string)$photo["ThumbPic"];
9177
- $picUrl = (string)$photo["Pic"];
9178
-
9179
- array_push($this->galleryPhotos, array(
9180
- 'height' => $picHeight,
9181
- 'width' => $picWidth,
9182
- 'thumb' => $thumbUrl,
9183
- 'pic' => $picUrl
9184
- ));
9185
- }
9186
- }
9187
-
9188
- #################################################################################################
9189
-
9190
- private function loadRelatedProducts($entity_id)
9191
- {
9192
- $this->sinchProductId;
9193
- if (!$this->sinchProductId) {
9194
- return;
9195
- }
9196
- $q = "SELECT
9197
- st_prod.sinch_product_id,
9198
- st_prod.product_sku,
9199
- st_prod.product_name,
9200
- st_prod.sinch_manufacturer_id,
9201
- st_prod.store_category_id,
9202
- st_prod.main_image_url,
9203
- st_prod.thumb_image_url,
9204
- st_prod.medium_image_url,
9205
- st_prod.specifications,
9206
- st_prod.description,
9207
- st_prod.specifications,
9208
- st_manuf.manufacturer_name,
9209
- st_manuf.manufacturers_image
9210
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . " st_rel_prod
9211
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " st_prod
9212
- ON st_rel_prod.related_sinch_product_id=st_prod.sinch_product_id
9213
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " st_manuf
9214
- ON st_prod.sinch_manufacturer_id=st_manuf.sinch_manufacturer_id
9215
- WHERE st_rel_prod.sinch_product_id=" . $this->sinchProductId;
9216
-
9217
- // echo $q;
9218
- $quer = $this->db_do($q);
9219
- while ($row = mysqli_fetch_array($quer)) {
9220
-
9221
- $productArray = array();
9222
- $productArray['name'] = (string)$row['product_name'];
9223
- $productArray['thumb'] = (string)$row['thumb_image_url'];
9224
- $mpn = (string)$row['product_sku'];
9225
- $productSupplierId = (int)$row['sinch_manufacturer_id'];
9226
- $productArray['supplier_thumb'] = (string)($row['manufacturers_image']);
9227
- $productArray['supplier_name'] = (string)$row['manufacturer_name'];
9228
-
9229
- $this->relatedProducts[$mpn] = $productArray;
9230
- }
9231
- }
9232
-
9233
- #################################################################################################
9234
-
9235
- public function getProductName()
9236
- {
9237
- return $this->productName;
9238
- }
9239
-
9240
- #################################################################################################
9241
-
9242
- public function getProductDescriptionList()
9243
- {
9244
- return $this->productDescriptionList;
9245
- }
9246
-
9247
- #################################################################################################
9248
-
9249
- public function getProductSpecifications()
9250
- {
9251
- return $this->specifications;
9252
- }
9253
-
9254
- #################################################################################################
9255
-
9256
- public function getShortProductDescription()
9257
- {
9258
- return $this->productDescription;
9259
- }
9260
-
9261
- #################################################################################################
9262
-
9263
- public function getFullProductDescription()
9264
- {
9265
- return $this->fullProductDescription;
9266
- }
9267
-
9268
- #################################################################################################
9269
-
9270
- public function getLowPicUrl()
9271
- {
9272
- return $this->highPicUrl;
9273
- }
9274
-
9275
- #################################################################################
9276
-
9277
- public function getRelatedProducts()
9278
- {
9279
- return $this->relatedProducts;
9280
- }
9281
-
9282
- #################################################################################################
9283
-
9284
- public function getVendor()
9285
- {
9286
- return $this->vendor;
9287
- }
9288
-
9289
- ##################################################################################################
9290
-
9291
- public function getMPN()
9292
- {
9293
- return $this->productId;
9294
- }
9295
-
9296
- ##################################################################################################
9297
-
9298
- public function getEAN()
9299
- {
9300
- return $this->EAN;
9301
- }
9302
-
9303
- ##################################################################################################
9304
-
9305
- public function getGalleryPhotos()
9306
- {
9307
- return $this->galleryPhotos;
9308
- }
9309
-
9310
- ##################################################################################################
9311
-
9312
- public function reloadProductImage($entity_id)
9313
- {
9314
- $result = $this->db_do("
9315
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
9316
- entity_type_id,
9317
- attribute_id,
9318
- store_id,
9319
- entity_id,
9320
- value
9321
- )(
9322
- SELECT
9323
- " . $this->_getProductEntityTypeId() . ",
9324
- " . $this->_getProductAttributeId('image') . ",
9325
- w.store_id,
9326
- a.entity_id,
9327
- b.main_image_url
9328
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
9329
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
9330
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " b
9331
- ON a.store_product_id = b.store_product_id
9332
- WHERE a.entity_id=$entity_id
9333
- )
9334
- ON DUPLICATE KEY UPDATE
9335
- value = b.main_image_url
9336
- ");
9337
-
9338
-
9339
- // image for specific web sites
9340
- $result = $this->db_do("
9341
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
9342
- entity_type_id,
9343
- attribute_id,
9344
- store_id,
9345
- entity_id,
9346
- value
9347
- )(
9348
- SELECT
9349
- " . $this->_getProductEntityTypeId() . ",
9350
- " . $this->_getProductAttributeId('image') . ",
9351
- 0,
9352
- a.entity_id,
9353
- b.main_image_url
9354
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
9355
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " b
9356
- ON a.store_product_id = b.store_product_id
9357
- WHERE a.entity_id=$entity_id
9358
- )
9359
- ON DUPLICATE KEY UPDATE
9360
- value = b.main_image_url
9361
- ");
9362
-
9363
-
9364
- // small_image for specific web sites
9365
- $result = $this->db_do("
9366
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
9367
- entity_type_id,
9368
- attribute_id,
9369
- store_id,
9370
- entity_id,
9371
- value
9372
- )(
9373
- SELECT
9374
- " . $this->_getProductEntityTypeId() . ",
9375
- " . $this->_getProductAttributeId('small_image') . ",
9376
- w.store_id,
9377
- a.entity_id,
9378
- b.main_image_url
9379
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
9380
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
9381
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " b
9382
- ON a.store_product_id = b.store_product_id
9383
- WHERE a.entity_id=$entity_id
9384
- )
9385
- ON DUPLICATE KEY UPDATE
9386
- value = b.main_image_url
9387
- ");
9388
-
9389
-
9390
- // small_image for all web sites
9391
- $result = $this->db_do("
9392
- INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
9393
- entity_type_id,
9394
- attribute_id,
9395
- store_id,
9396
- entity_id,
9397
- value
9398
- )(
9399
- SELECT
9400
- " . $this->_getProductEntityTypeId() . ",
9401
- " . $this->_getProductAttributeId('small_image') . ",
9402
- 0,
9403
- a.entity_id,
9404
- b.main_image_url
9405
- FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
9406
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
9407
- INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products') . " b
9408
- ON a.store_product_id = b.store_product_id
9409
- WHERE a.entity_id=$entity_id
9410
- )
9411
- ON DUPLICATE KEY UPDATE
9412
- value = b.main_image_url
9413
- ");
9414
- }
9415
-
9416
- ##################################################################################################
9417
-
9418
- function valid_utf($string, $new_line = true)
9419
- {
9420
- /* if($new_line == true){
9421
- $string = preg_replace('/\\\n/',"\n",$string);
9422
- }
9423
- */
9424
- $string = preg_replace('/™/', '&#8482;', $string);
9425
- $string = preg_replace("/®/", '&reg;', $string);
9426
- $string = preg_replace("/≈/", '&asymp;', $string);
9427
- $string = preg_replace("/" . chr(226) . chr(128) . chr(157) . "/", '&quot;', $string);
9428
- $string = preg_replace("/" . chr(226) . chr(128) . chr(153) . "/", '&prime;', $string);
9429
- $string = preg_replace("/°/", '&deg;', $string);
9430
- $string = preg_replace("/±/", '&plusmn;', $string);
9431
- $string = preg_replace("/µ/", '&micro;', $string);
9432
- $string = preg_replace("/²/", '&sup2;', $string);
9433
- $string = preg_replace("/³/", '&sup3;', $string);
9434
- $string = preg_replace('/\xe2\x80\x93/', '-', $string);
9435
- $string = preg_replace('/\xe2\x80\x99/', '\'', $string);
9436
- $string = preg_replace('/\xe2\x80\x9c/', ' ', $string);
9437
- $string = preg_replace('/\xe2\x80\x9d/', ' ', $string);
9438
-
9439
- return utf8_decode($string);
9440
-
9441
- // return $string;
9442
- }
9443
-
9444
- ##################################################################################################
9445
-
9446
- function set_imports_failed()
9447
- {
9448
- $this->db_do("UPDATE " . $this->import_status_statistic_table . "
9449
- SET global_status_import='Failed'
9450
- WHERE global_status_import='Run'");
9451
- }
9452
- ##################################################################################################
9453
-
9454
-
9455
- ##################################################################################################
9456
-
9457
- function getImportStatusHistory()
9458
- {
9459
- $res = "SELECT COUNT(*) FROM " . $this->import_status_statistic_table;
9460
- $cnt_arr = mysqli_fetch_array($this->db_do($res));
9461
- $cnt = $cnt_arr[0];
9462
- $StatusHistory_arr = array();
9463
- if ($cnt > 0) {
9464
- $a = (($cnt > 7) ? ($cnt - 7) : 0);
9465
- $b = $cnt;
9466
- $q = "SELECT
9467
- id,
9468
- start_import,
9469
- finish_import,
9470
- import_type,
9471
- number_of_products,
9472
- global_status_import,
9473
- detail_status_import
9474
- FROM " . $this->import_status_statistic_table . "
9475
- ORDER BY start_import limit " . $a . ", " . $b;
9476
- $result = $this->db_do($q);
9477
- while ($row = mysqli_fetch_array($result)) {
9478
- $StatusHistory_arr[] = $row;
9479
- }
9480
- }
9481
- return $StatusHistory_arr;
9482
- } // public function getImportEnvironment()
9483
- ##################################################################################################
9484
-
9485
-
9486
- ##################################################################################################
9487
-
9488
- function getDateOfLatestSuccessImport()
9489
- {
9490
- $q = "SELECT start_import, finish_import
9491
- FROM " . $this->import_status_statistic_table . "
9492
- WHERE global_status_import='Successful'
9493
- ORDER BY id DESC LIMIT 1";
9494
- $imp_date = mysqli_fetch_array($this->db_do($q));
9495
- return $imp_date['start_import'];
9496
- } // public function getImportEnvironment()
9497
- ##################################################################################################
9498
-
9499
-
9500
- ##################################################################################################
9501
-
9502
- function getDataOfLatestImport()
9503
- {
9504
- $q = "SELECT
9505
- start_import,
9506
- finish_import,
9507
- import_type,
9508
- number_of_products,
9509
- global_status_import,
9510
- detail_status_import,
9511
- number_of_products,
9512
- error_report_message
9513
- FROM " . $this->import_status_statistic_table . "
9514
- ORDER BY id DESC LIMIT 1";
9515
- $imp_status = mysqli_fetch_array($this->db_do($q));
9516
- return $imp_status;
9517
- } // public function getImportEnvironment()
9518
- ##################################################################################################
9519
-
9520
-
9521
- ##################################################################################################
9522
-
9523
- function getImportStatuses()
9524
- {
9525
- $q = "SELECT id, message, finished
9526
- FROM " . $this->import_status_table . "
9527
- ORDER BY id LIMIT 1";
9528
- $quer = $this->db_do($q);
9529
- if ($row = mysqli_fetch_array($quer)) {
9530
- $messages = array('message' => $row['message'], 'id' => $row['id'], 'finished' => $row['finished']);
9531
- $id = $row['id'];
9532
- }
9533
- if ($id) {
9534
- $q = "DELETE FROM " . $this->import_status_table . " WHERE id=" . $id;
9535
- $this->db_do($q);
9536
- }
9537
- return $messages;
9538
- } // public function getImportEnvironment()
9539
- ##################################################################################################
9540
-
9541
-
9542
- ##################################################################################################
9543
-
9544
- public function checkMemory()
9545
- {
9546
- $check_code = 'memory';
9547
-
9548
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9549
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9550
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9551
- $row = $result->fetch(PDO::FETCH_ASSOC);
9552
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9553
-
9554
-
9555
- $Caption = $row['caption'];
9556
- $CheckValue = $row['check_value'];
9557
- $CheckMeasure = $row['check_measure'];
9558
- $ErrorMessage = $row['error_msg'];
9559
- $FixMessage = $row['fix_msg'];
9560
-
9561
- $retvalue = array();
9562
- $retvalue["'$check_code'"] = array();
9563
-
9564
- $memInfoContent = file_get_contents("/proc/meminfo");
9565
- if ($memInfoContent === false) {
9566
- return array(
9567
- 'error',
9568
- $Caption,
9569
- $CheckValue,
9570
- 0,
9571
- $CheckMeasure,
9572
- 'Cannot read /proc/meminfo for RAM information',
9573
- 'Make sure open_basedir permits access to /proc/meminfo (or is off) and that this is a *nix system'
9574
- );
9575
- }
9576
- $data = explode("\n", $memInfoContent);
9577
-
9578
- foreach ($data as $line) {
9579
- $lineParts = explode(":", $line);
9580
- if (count($lineParts) < 2) continue;
9581
- list($key, $val) = $lineParts;
9582
-
9583
- if ($key == 'MemTotal') {
9584
- $val = trim($val);
9585
- $value = (int)substr($val, 0, strpos($val, ' kB'));
9586
- $measure = substr($val, strpos($val, ' kB'));
9587
-
9588
- $retvalue['memory']['value'] = (integer)(((float)$value) / 1024); // (float)$value
9589
- $retvalue['memory']['measure'] = 'MB'; // $measure;
9590
- }
9591
- }
9592
-
9593
- $errmsg = '';
9594
- $fixmsg = '';
9595
- if ($retvalue['memory']['value'] <= $CheckValue) {
9596
- $errmsg = sprintf($ErrorMessage, $retvalue['memory']['value']); //." ".$retvalue['memory']['value']." ".$retvalue['memory']['measure'];
9597
- $fixmsg = sprintf($FixMessage, " " . $CheckValue . " " . $CheckMeasure);
9598
- $retvalue['memory']['status'] = 'error';
9599
- } else {
9600
- $retvalue['memory']['status'] = 'OK';
9601
- }
9602
-
9603
- return array(
9604
- $retvalue['memory']['status'],
9605
- $Caption,
9606
- $CheckValue,
9607
- $retvalue['memory']['value'],
9608
- $CheckMeasure,
9609
- $errmsg,
9610
- $fixmsg
9611
- );
9612
- } // public function getImportEnvironment()
9613
- ##################################################################################################
9614
-
9615
-
9616
- ##################################################################################################
9617
-
9618
- public function checkLoaddata()
9619
- {
9620
- $check_code = 'loaddata';
9621
-
9622
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9623
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9624
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9625
- $row = $result->fetch(PDO::FETCH_ASSOC);
9626
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9627
-
9628
- $Caption = $row['caption'];
9629
- $CheckValue = $row['check_value'];
9630
- $CheckMeasure = $row['check_measure'];
9631
- $ErrorMessage = $row['error_msg'];
9632
- $FixMessage = $row['fix_msg'];
9633
-
9634
- $retvalue = array();
9635
- $retvalue["'$check_code'"] = array();
9636
-
9637
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9638
- $result = $conn->query("SHOW VARIABLES LIKE 'local_infile'");
9639
- $row = $result->fetch(PDO::FETCH_ASSOC);
9640
- $value = $row['Value'];
9641
-
9642
-
9643
- $errmsg = '';
9644
- $fixmsg = '';
9645
- if ($value != $CheckValue) {
9646
- $errmsg .= $ErrorMessage . " " . $value . " " . $CheckMeasure;
9647
- $fixmsg .= $FixMessage; // ." ".$CheckValue." ".$CheckMeasure
9648
- $status = 'error';
9649
- } else {
9650
- $errmsg .= 'none';
9651
- $fixmsg .= 'none';
9652
- $status = 'OK';
9653
- }
9654
-
9655
- $ret = array();
9656
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
9657
-
9658
- return $ret;
9659
- } // public function getImportEnvironment()
9660
- ##################################################################################################
9661
-
9662
-
9663
- ##################################################################################################
9664
-
9665
- public function checkPhpsafemode()
9666
- {
9667
- $check_code = 'phpsafemode';
9668
-
9669
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9670
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9671
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9672
- $row = $result->fetch(PDO::FETCH_ASSOC);
9673
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9674
-
9675
- $Caption = $row['caption'];
9676
- $CheckValue = $row['check_value'];
9677
- $CheckMeasure = $row['check_measure'];
9678
- $ErrorMessage = $row['error_msg'];
9679
- $FixMessage = $row['fix_msg'];
9680
-
9681
- $retvalue = array();
9682
- $retvalue["'$check_code'"] = array();
9683
-
9684
- $a = ini_get('safe_mode');
9685
- if ($a) {
9686
- $value = 'ON';
9687
- } else {
9688
- $value = 'OFF';
9689
- }
9690
-
9691
- $errmsg = '';
9692
- $fixmsg = '';
9693
- if ($value != $CheckValue) {
9694
- $errmsg .= sprintf($ErrorMessage, " " . $value . " " . $CheckMeasure);
9695
- $fixmsg .= sprintf($FixMessage, " " . $CheckValue . " " . $CheckMeasure);
9696
- $status = 'error';
9697
- } else {
9698
- $errmsg .= 'none';
9699
- $fixmsg .= 'none';
9700
- $status = 'OK';
9701
- }
9702
-
9703
- $ret = array();
9704
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
9705
-
9706
- return $ret;
9707
- } // public function getImportEnvironment()
9708
- ##################################################################################################
9709
-
9710
-
9711
- ##################################################################################################
9712
-
9713
- public function checkWaittimeout()
9714
- {
9715
- $check_code = 'waittimeout';
9716
-
9717
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9718
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9719
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9720
- $row = $result->fetch(PDO::FETCH_ASSOC);
9721
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9722
-
9723
- $Caption = $row['caption'];
9724
- $CheckValue = $row['check_value'];
9725
- $CheckMeasure = $row['check_measure'];
9726
- $ErrorMessage = $row['error_msg'];
9727
- $FixMessage = $row['fix_msg'];
9728
-
9729
- $retvalue = array();
9730
- $retvalue["'$check_code'"] = array();
9731
-
9732
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9733
- $result = $conn->query("SHOW VARIABLES LIKE 'wait_timeout'");
9734
- $row = $result->fetch(PDO::FETCH_ASSOC);
9735
- $value = $row['Value'];
9736
-
9737
- $errmsg = '';
9738
- $fixmsg = '';
9739
- if ($value <= $CheckValue) {
9740
- $errmsg .= $ErrorMessage . " " . $value . " " . $CheckMeasure;
9741
- $fixmsg .= sprintf($FixMessage, " " . $CheckValue);
9742
- $status = 'error';
9743
- } else {
9744
- $errmsg .= 'none';
9745
- $fixmsg .= 'none';
9746
- $status = 'OK';
9747
- }
9748
-
9749
- $ret = array();
9750
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
9751
-
9752
- return $ret;
9753
- } // public function checkChmodwgetcronphpfile()
9754
- ##################################################################################################
9755
-
9756
-
9757
- ##################################################################################################
9758
-
9759
- public function checkInnodbbufferpoolsize()
9760
- {
9761
- $check_code = 'innodbbufpool';
9762
-
9763
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9764
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9765
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9766
- $row = $result->fetch(PDO::FETCH_ASSOC);
9767
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9768
-
9769
- $Caption = $row['caption'];
9770
- $CheckValue = $row['check_value'];
9771
- $CheckMeasure = $row['check_measure'];
9772
- $ErrorMessage = $row['error_msg'];
9773
- $FixMessage = $row['fix_msg'];
9774
-
9775
- $retvalue = array();
9776
- $retvalue["'$check_code'"] = array();
9777
-
9778
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9779
- $result = $conn->query("SHOW VARIABLES LIKE 'innodb_buffer_pool_size'");
9780
- $row = $result->fetch(PDO::FETCH_ASSOC);
9781
- $value = (int)($row['Value'] / (1024 * 1024));
9782
-
9783
- $errmsg = '';
9784
- $fixmsg = '';
9785
- if ($value < $CheckValue) {
9786
- $errmsg .= sprintf($ErrorMessage, " " . $value . " " . $CheckMeasure);
9787
- $fixmsg .= sprintf($FixMessage, " " . $CheckValue . " " . $CheckMeasure);
9788
- $status = 'error';
9789
- } else {
9790
- $errmsg .= 'none';
9791
- $fixmsg .= 'none';
9792
- $status = 'OK';
9793
- }
9794
-
9795
- $ret = array();
9796
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
9797
-
9798
- return $ret;
9799
- } // public function checkChmodwgetcronphpfile()
9800
- ##################################################################################################
9801
-
9802
-
9803
- ##################################################################################################
9804
-
9805
- public function checkPhprunstring()
9806
- {
9807
- $check_code = 'php5run';
9808
-
9809
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9810
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9811
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9812
- $row = $result->fetch(PDO::FETCH_ASSOC);
9813
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9814
-
9815
- $Caption = $row['caption'];
9816
- $CheckValue = $row['check_value'];
9817
- $CheckMeasure = $row['check_measure'];
9818
- $ErrorMessage = $row['error_msg'];
9819
- $FixMessage = $row['fix_msg'];
9820
-
9821
- $retvalue = array();
9822
- $retvalue["'$check_code'"] = array();
9823
-
9824
- $value = trim(PHP_RUN_STRING);
9825
- $errmsg = '';
9826
- $fixmsg = '';
9827
- $status = 'OK';
9828
-
9829
- if (!defined('PHP_RUN_STRING')) {
9830
- $errmsg .= "You haven't installed PHP CLI";
9831
- $fixmsg .= "Install PHP CLI."; // ." ".$CheckValue." ".$CheckMeasure
9832
- $status = 'error';
9833
- }
9834
-
9835
- return array(
9836
- $status,
9837
- $Caption,
9838
- $CheckValue,
9839
- $value,
9840
- $CheckMeasure,
9841
- $errmsg,
9842
- $fixmsg
9843
- );
9844
- } // public function getImportEnvironment()
9845
- ##################################################################################################
9846
-
9847
-
9848
- ##################################################################################################
9849
-
9850
- public function checkChmodwgetdatafile()
9851
- {
9852
- $check_code = 'chmodwget';
9853
-
9854
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9855
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9856
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9857
- $row = $result->fetch(PDO::FETCH_ASSOC);
9858
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9859
-
9860
- $Caption = $row['caption'];
9861
- $CheckValue = $row['check_value'];
9862
- $CheckMeasure = $row['check_measure'];
9863
- $ErrorMessage = $row['error_msg'];
9864
- $FixMessage = $row['fix_msg'];
9865
-
9866
- $retvalue = array();
9867
- $retvalue["'$check_code'"] = array();
9868
-
9869
- $datafile_csv = '/usr/bin/wget';
9870
-
9871
- $value = substr(sprintf('%o', fileperms($datafile_csv)), -4);
9872
-
9873
- $CheckValue_own = $CheckValue{1};
9874
- $CheckValue_group = $CheckValue{2};
9875
- $CheckValue_other = $CheckValue{3};
9876
-
9877
- $value_own = $value{1};
9878
- $value_group = $value{2};
9879
- $value_other = $value{3};
9880
-
9881
- $errmsg = '';
9882
- $fixmsg = '';
9883
- //if ($value <= $CheckValue) {
9884
- if (($value_own < $CheckValue_own) || ($value_group < $CheckValue_group) || ($value_other < $CheckValue_other)) {
9885
- $errmsg .= $ErrorMessage; // ." ".$value." ".$CheckMeasure
9886
- $fixmsg .= $FixMessage; // ." ".$CheckValue." ".$CheckMeasure
9887
- $status = 'error';
9888
- } else {
9889
- $errmsg .= 'none';
9890
- $fixmsg .= 'none';
9891
- $status = 'OK';
9892
- }
9893
-
9894
- $ret = array();
9895
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
9896
-
9897
- return $ret;
9898
- } // public function getImportEnvironment()
9899
- ##################################################################################################
9900
-
9901
-
9902
- ##################################################################################################
9903
-
9904
- public function checkChmodwgetcronphpfile()
9905
- {
9906
- $check_code = 'chmodcronphp';
9907
-
9908
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9909
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9910
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9911
- $row = $result->fetch(PDO::FETCH_ASSOC);
9912
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9913
-
9914
- $Caption = $row['caption'];
9915
- $CheckValue = $row['check_value'];
9916
- $CheckMeasure = $row['check_measure'];
9917
- $ErrorMessage = $row['error_msg'];
9918
- $FixMessage = $row['fix_msg'];
9919
-
9920
- $retvalue = array();
9921
- $retvalue["'$check_code'"] = array();
9922
-
9923
- $cronfile_php = Mage::getBaseDir() . '/cron.php';
9924
-
9925
- $value = substr(sprintf('%o', fileperms($cronfile_php)), -4);
9926
-
9927
- $CheckValue_own = $CheckValue{1};
9928
- $CheckValue_group = $CheckValue{2};
9929
- $CheckValue_other = $CheckValue{3};
9930
-
9931
- $value_own = $value{1};
9932
- $value_group = $value{2};
9933
- $value_other = $value{3};
9934
-
9935
- $errmsg = '';
9936
- $fixmsg = '';
9937
- //if ($value <= $CheckValue) {
9938
- if (($value_own < $CheckValue_own) || ($value_group < $CheckValue_group) || ($value_other < $CheckValue_other)) {
9939
- $errmsg .= $ErrorMessage; // ." ".$value." ".$CheckMeasure
9940
- $fixmsg .= $FixMessage; // ." ".$CheckValue." ".$CheckMeasure
9941
- $status = 'error';
9942
- } else {
9943
- $errmsg .= 'none';
9944
- $fixmsg .= 'none';
9945
- $status = 'OK';
9946
- }
9947
-
9948
- $ret = array();
9949
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
9950
-
9951
- return $ret;
9952
- }
9953
- ##################################################################################################
9954
-
9955
-
9956
- ##################################################################################################
9957
-
9958
- public function checkChmodwgetcronshfile()
9959
- {
9960
- $check_code = 'chmodcronsh';
9961
-
9962
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
9963
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
9964
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
9965
- $row = $result->fetch(PDO::FETCH_ASSOC);
9966
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
9967
-
9968
- $Caption = $row['caption'];
9969
- $CheckValue = $row['check_value'];
9970
- $CheckMeasure = $row['check_measure'];
9971
- $ErrorMessage = $row['error_msg'];
9972
- $FixMessage = $row['fix_msg'];
9973
-
9974
- $retvalue = array();
9975
- $retvalue["'$check_code'"] = array();
9976
-
9977
- $cronfile_sh = Mage::getBaseDir() . '/cron.sh';
9978
-
9979
- $value = substr(sprintf('%o', fileperms($cronfile_sh)), -4);
9980
-
9981
- $CheckValue_own = $CheckValue{1};
9982
- $CheckValue_group = $CheckValue{2};
9983
- $CheckValue_other = $CheckValue{3};
9984
-
9985
- $value_own = $value{1};
9986
- $value_group = $value{2};
9987
- $value_other = $value{3};
9988
-
9989
- $errmsg = '';
9990
- $fixmsg = '';
9991
- //if ($value <= $CheckValue) {
9992
- if (($value_own < $CheckValue_own) || ($value_group < $CheckValue_group) || ($value_other < $CheckValue_other)) {
9993
- $errmsg .= $ErrorMessage; // ." ".$value." ".$CheckMeasure
9994
- $fixmsg .= $FixMessage; // ." ".$CheckValue." ".$CheckMeasure
9995
- $status = 'error';
9996
- } else {
9997
- $errmsg .= 'none';
9998
- $fixmsg .= 'none';
9999
- $status = 'OK';
10000
- }
10001
-
10002
- $ret = array();
10003
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
10004
-
10005
- return $ret;
10006
- }
10007
-
10008
- #################################################################################################
10009
-
10010
- public function checkProcedure()
10011
- {
10012
- $check_code = 'routine';
10013
-
10014
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
10015
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
10016
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
10017
- $row = $result->fetch(PDO::FETCH_ASSOC);
10018
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
10019
-
10020
- $Caption = $row['caption'];
10021
- $CheckValue = $row['check_value'];
10022
- $CheckMeasure = $row['check_measure'];
10023
- $ErrorMessage = $row['error_msg'];
10024
- $FixMessage = $row['fix_msg'];
10025
-
10026
- $retvalue = array();
10027
- $retvalue["'$check_code'"] = array();
10028
-
10029
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
10030
- $storedFunctionName = Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s');
10031
- $result = $conn->query("SHOW PROCEDURE STATUS LIKE '$storedFunctionName'");
10032
- $row = $result->fetch(PDO::FETCH_ASSOC);
10033
- $value = $row['Name'];
10034
-
10035
- $errmsg = '';
10036
- $fixmsg = '';
10037
- if ($value != $CheckValue) {
10038
- $errmsg .= $ErrorMessage; // ." ".$value." ".$CheckMeasure
10039
- $fixmsg .= $FixMessage; // ." ".$CheckValue." ".$CheckMeasure
10040
- $status = 'error';
10041
- } else {
10042
- $errmsg .= 'none';
10043
- $fixmsg .= 'none';
10044
- $status = 'OK';
10045
- }
10046
-
10047
- $ret = array();
10048
- array_push($ret, $status, $Caption, $CheckValue, $value, $CheckMeasure, $errmsg, $fixmsg);
10049
-
10050
- return $ret;
10051
- }
10052
-
10053
- #################################################################################################
10054
-
10055
- public function checkConflictsWithInstalledModules()
10056
- {
10057
- $check_code = 'conflictwithinstalledmodules';
10058
-
10059
- $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
10060
- $tableName = Mage::getSingleton('core/resource')->getTableName('stINch_sinchcheck');
10061
- $result = $conn->query("SELECT * FROM $tableName WHERE check_code = '$check_code'");
10062
- $row = $result->fetch(PDO::FETCH_ASSOC);
10063
- //echo " [".$row['id']."] [".$row['caption']."] [".$row['descr']."] [".$row['check_code']."] [".$row['check_value']."] [".$row['check_measure']."] [".$row['error_msg']."] [".$row['fix_msg']."] <br>";
10064
-
10065
- $Caption = $row['caption'];
10066
- $CheckValue = $row['check_value'];
10067
- $CheckMeasure = $row['check_measure'];
10068
- $ErrorMessage = $row['error_msg'];
10069
- $FixMessage = $row['fix_msg'];
10070
-
10071
- $retvalue = array();
10072
- $retvalue["'$check_code'"] = array();
10073
-
10074
- /* $conn = Mage::getSingleton('core/resource')->getConnection('core_read');
10075
- $storedFunctionName = Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s');
10076
- $result = $conn->query("SHOW PROCEDURE STATUS LIKE '$storedFunctionName'");
10077
- $row = $result->fetch(PDO::FETCH_ASSOC);
10078
- $value = $row['Name'];
10079
- */
10080
- $config_file = (Mage::app()->getConfig()->getNode()->asXML());
10081
-
10082
- $errmsg = $ErrorMessage;
10083
- $fixmsg = $FixMessage;
10084
- /*
10085
- if ($value != $CheckValue) {
10086
- $errmsg .= $ErrorMessage; // ." ".$value." ".$CheckMeasure
10087
- $fixmsg .= $FixMessage; // ." ".$CheckValue." ".$CheckMeasure
10088
- $status = 'error';
10089
- */
10090
- $status = 'OK';
10091
-
10092
- if (!strstr($config_file, '<image>Bintime_Sinchimport_Helper_Image</image>')) {
10093
- $errmsg .= " Can't find <image>Bintime_Sinchimport_Helper_Image</image> in <helpers><catalog></catalog></helpers>"; // ." ".$value." ".$CheckMeasure
10094
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10095
- $status = 'error';
10096
- }
10097
-
10098
- if (!strstr($config_file, '<product_image>Bintime_Sinchimport_Model_Image</product_image>')) {
10099
- $errmsg .= " Can't find <product_image>Bintime_Sinchimport_Model_Image</product_image> in <models><catalog></catalog></models>"; // ." ".$value." ".$CheckMeasure
10100
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10101
- $status = 'error';
10102
- }
10103
-
10104
- if (!strstr($config_file, '<category>Bintime_Sinchimport_Model_Category</category>')) {
10105
- $errmsg .= " Can't find <category>Bintime_Sinchimport_Model_Category</category> in <models><catalog></catalog></models>"; // ." ".$value." ".$CheckMeasure
10106
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10107
- $status = 'error';
10108
- }
10109
-
10110
- if (!strstr($config_file, '<product_compare_list>Bintime_Sinchimport_Block_List</product_compare_list>')) {
10111
- $errmsg .= " Can't find <product_compare_list>Bintime_Sinchimport_Block_List</product_compare_list> in <blocks><catalog></catalog></blocks>"; // ." ".$value." ".$CheckMeasure
10112
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10113
- $status = 'error';
10114
- }
10115
-
10116
- if (!strstr($config_file, '<product_view_media>Bintime_Sinchimport_Block_Product_View_Media</product_view_media>')) {
10117
- $errmsg .= " Can't find <product_view_media>Bintime_Sinchimport_Block_Product_View_Media</product_view_media> in <blocks><catalog></catalog></blocks>"; // ." ".$value." ".$CheckMeasure
10118
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10119
- $status = 'error';
10120
- }
10121
-
10122
- if (!strstr($config_file, '<product>Bintime_Sinchimport_Model_Product</product>')) {
10123
- $errmsg .= " Can't find <product>Bintime_Sinchimport_Model_Product</product> in <models><catalog></catalog></models>"; // ." ".$value." ".$CheckMeasure
10124
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10125
- $status = 'error';
10126
- }
10127
-
10128
- if (!strstr($config_file, '<layer_filter_price>Bintime_Sinchimport_Model_Layer_Filter_Price</layer_filter_price>')) {
10129
- $errmsg .= " Can't find <layer_filter_price>Bintime_Sinchimport_Model_Layer_Filter_Price</layer_filter_price> in <models><catalog></catalog><models>"; // ." ".$value." ".$CheckMeasure
10130
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10131
- $status = 'error';
10132
- }
10133
-
10134
- if (!strstr($config_file, '<layer_view>Bintime_Sinchimport_Block_Layer_View</layer_view>')) {
10135
- $errmsg .= " Can't find <layer_view>Bintime_Sinchimport_Block_Layer_View</layer_view> in <blocks><catalog></catalog></blocks>"; // ." ".$value." ".$CheckMeasure
10136
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10137
- $status = 'error';
10138
- }
10139
-
10140
- if (!strstr($config_file, '<layer>Bintime_Sinchimport_Model_Layer</layer>')) {
10141
- $errmsg .= " Can't find <layer>Bintime_Sinchimport_Model_Layer</layer> in <models><catalog></catalog><models>"; // ." ".$value." ".$CheckMeasure
10142
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10143
- $status = 'error';
10144
- }
10145
-
10146
- if (!strstr($config_file, '<layer_filter_price>Bintime_Sinchimport_Model_Resource_Layer_Filter_Price</layer_filter_price>')) {
10147
- $errmsg .= " Can't find <layer_filter_price>Bintime_Sinchimport_Model_Resource_Layer_Filter_Price</layer_filter_price> in <models><catalog_resource_eav_mysql4></catalog_resource_eav_mysql4></models>"; // ." ".$value." ".$CheckMeasure
10148
- $fixmsg = $FixMessage;//.$CheckValue." ".$CheckMeasure
10149
- $status = 'error';
10150
- }
10151
-
10152
-
10153
- if ($status == 'OK') {
10154
- $errmsg = 'none';
10155
- $fixmsg = 'none';
10156
- }
10157
- return array(
10158
- $status,
10159
- $Caption,
10160
- $CheckValue,
10161
- '',
10162
- $CheckMeasure,
10163
- $errmsg,
10164
- $fixmsg
10165
- );
10166
- }
10167
-
10168
- #################################################################################################
10169
-
10170
- public function getSinchDistribotorsTableHtml($entity_id = null)
10171
- {
10172
- /*/ Load the collection
10173
- $collection = getResourceModel('sales/order_grid_collection');
10174
-
10175
- // Add custom data
10176
- $collection->addToAll('example', 'This is a test');
10177
-
10178
- // Set the collection
10179
- $this->setCollection($collection);
10180
- // return parent::_prepareCollection();
10181
- */
10182
- if (!$entity_id) {
10183
- $entity_id = Mage::registry('current_product')->getId();
10184
- }
10185
- if (!$entity_id) {
10186
- return '';
10187
- }
10188
-
10189
- $distributors_stock_price = $this->getDistributorStockPriceByProductid($entity_id);
10190
- $distributors_table = '
10191
- <table>
10192
- <thead>
10193
- <tr class="headings">
10194
- <th>Supplier</th>
10195
- <th>Stock</th>
10196
- <th>Price</th>
10197
- </tr>
10198
- </thead>
10199
- <tbody>';
10200
- $i = 1;
10201
- foreach ($distributors_stock_price as $offer) {
10202
- if ($i > 0) {
10203
- $class = "even pointer";
10204
- $i = 0;
10205
- } else {
10206
- $class = "pointer";
10207
- $i = 1;
10208
- }
10209
- $distributors_table .= '
10210
- <tr class="' . $class . '">
10211
- <td nowrap style="font-weight: normal">' . $offer['distributor_name'] . '</td>
10212
- <td style="font-weight: normal">' . $offer['stock'] . '</td>
10213
- <td style="font-weight: normal">' . Mage::helper('core')->currency($offer['cost']) . '</td>
10214
- </tr>';
10215
- }
10216
- $distributors_table .= '
10217
- </tbody>
10218
- </table>
10219
- ';
10220
- return $distributors_table;
10221
- }
10222
-
10223
- #################################################################################################
10224
-
10225
- private function getDistributorStockPriceByProductid($entity_id)
10226
- {
10227
- $store_product_id = $this->getStoreProductIdByEntity($entity_id);
10228
- if (!$store_product_id) {
10229
- // echo "AAAAAAA"; exit;
10230
- return;
10231
- }
10232
- $q = "SELECT
10233
- d.distributor_name,
10234
- d.website,
10235
- dsp.stock,
10236
- dsp.cost,
10237
- dsp.distributor_sku,
10238
- dsp.distributor_category,
10239
- dsp.eta
10240
- FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price') . " dsp
10241
- JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors') . " d
10242
- ON dsp.distributor_id = d.distributor_id
10243
- WHERE store_product_id =" . $store_product_id;
10244
- $quer = $this->db_do($q);
10245
- $offers = null;
10246
- while ($row = mysqli_fetch_array($quer)) {
10247
- $offers[] = $row;
10248
- }
10249
- return $offers;
10250
-
10251
- }
10252
-
10253
- protected function _checkCategoryBackupExist($catalog_category_entity_backup) {
10254
- $query = $this->db_do("
10255
- SELECT *
10256
- FROM $catalog_category_entity_backup
10257
- ");
10258
- while ($row = mysqli_fetch_array($query))
10259
- {
10260
- return true;
10261
- }
10262
-
10263
- return false;
10264
- }
10265
-
10266
- } // class Bintime_Sinchimport_Model_Sinch extends Mage_Core_Model_Abstract
1
+ <?php
2
+ ini_set('memory_limit', '256M');
3
+ $dir = Mage::getBaseDir('code') . "/local/Bintime/Sinchimport/Model";
4
+ require_once($dir . '/config.php');
5
+
6
+ class Bintime_Sinchimport_Model_Sinch extends Mage_Core_Model_Abstract
7
+ {
8
+ var
9
+ $connection,
10
+ $varDir,
11
+ $shellDir,
12
+ $files,
13
+ $attributes,
14
+ $db,
15
+ $lang_id,
16
+ $debug_mode = 1;
17
+ public $php_run_string;
18
+ public $php_run_strings;
19
+ public $price_breaks_filter;
20
+ private $field_terminated_char;
21
+
22
+ private $productDescriptionList = array();
23
+ private $specifications;
24
+ private $productDescription;
25
+ private $fullProductDescription;
26
+ private $lowPicUrl;
27
+ private $highPicUrl;
28
+ private $galleryPhotos = array();
29
+ private $productName;
30
+ private $relatedProducts = array();
31
+ private $sinchProductId;
32
+
33
+ private $_productEntityTypeId = 0;
34
+ private $defaultAttributeSetId = 0;
35
+ private $current_import_status_statistic_id;
36
+
37
+ private $import_status_table;
38
+ private $import_status_statistic_table;
39
+ private $import_log_table;
40
+
41
+ private $_attributeId;
42
+ private $_categoryEntityTypeId;
43
+ private $_categoryDefault_attribute_set_id;
44
+ private $_root_cat;
45
+ private $_categoryMetaTitleAttrId;
46
+ private $_categoryMetadescriptionAttrId;
47
+ private $_categoryDescriptionAttrId;
48
+
49
+ private $_ignore_category_features = false;
50
+ private $_ignore_product_features = false;
51
+ private $_ignore_product_related = false;
52
+ private $_ignore_product_categories = false;
53
+ private $_ignore_product_contracts = false;
54
+ private $_ignore_price_rules = false;
55
+ private $_ignore_restricted_values = false;
56
+
57
+ private $product_file_format = "NEW";
58
+ private $import_run_type = 'MANUAL';
59
+ private $im_type;
60
+
61
+ function __construct()
62
+ {
63
+ $this->import_status_table = Mage::getSingleton('core/resource')->getTableName('stINch_import_status');
64
+ $this->import_status_statistic_table = Mage::getSingleton('core/resource')->getTableName('stINch_import_status_statistic');
65
+ $this->import_log_table = "stINch_import_log";
66
+
67
+ $this->php_run_string = PHP_RUN_STRING;
68
+ $this->php_run_strings = PHP_RUN_STRINGS;
69
+
70
+ $this->price_breaks_filter = PRICE_BREAKS;
71
+ $this->field_terminated_char = DEFAULT_FILE_TERMINATED_CHAR;
72
+
73
+ $this->varDir = Mage::getBaseDir('var') . DS . 'bintime' . DS . 'sinchimport' . DS;
74
+ //$this->varDir = str_replace(DS, '/', $this->varDir);
75
+ if (!is_dir($this->varDir)) {
76
+ mkdir($this->varDir, 0777, true);
77
+ }
78
+
79
+ $this->shellDir = Mage::getBaseDir('base') . DS . 'shell' . DS;
80
+
81
+ $this->connection = $this->db_connect();
82
+
83
+ $this->_logFile = "Sinch.log";
84
+ $this->_errorLogFile = "SinchError.log";
85
+
86
+ $this->files = array(
87
+ FILE_CATEGORIES,
88
+ FILE_CATEGORY_TYPES,
89
+ FILE_CATEGORIES_FEATURES,
90
+ FILE_DISTRIBUTORS,
91
+ FILE_DISTRIBUTORS_STOCK_AND_PRICES,
92
+ FILE_EANCODES,
93
+ FILE_MANUFACTURERS,
94
+ FILE_PRODUCT_FEATURES,
95
+ FILE_PRODUCT_CATEGORIES,
96
+ FILE_PRODUCTS,
97
+ FILE_RELATED_PRODUCTS,
98
+ FILE_RESTRICTED_VALUES,
99
+ FILE_STOCK_AND_PRICES,
100
+ FILE_PRODUCTS_PICTURES_GALLERY,
101
+ FILE_PRICE_RULES,
102
+ FILE_PRODUCT_CONTRACTS
103
+ );
104
+ $this->attributes['manufacturer'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('manufacturer')->getFirstItem()->getId();
105
+ $this->attributes['name'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('name')->getFirstItem()->getId();
106
+ $this->attributes['is_active'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('is_active')->getFirstItem()->getId();
107
+ $this->attributes['include_in_menu'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('include_in_menu')->getFirstItem()->getId();
108
+ $this->attributes['url_key'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('url_key')->getFirstItem()->getId();
109
+ $this->attributes['display_mode'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('display_mode')->getFirstItem()->getId();
110
+ $this->attributes['status'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('status')->getFirstItem()->getId();
111
+ $this->attributes['visibility'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('visibility')->getFirstItem()->getId();
112
+ $this->attributes['price'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('price')->getFirstItem()->getId();
113
+ $this->attributes['cost'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('cost')->getFirstItem()->getId();
114
+ $this->attributes['weight'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('weight')->getFirstItem()->getId();
115
+ $this->attributes['tax_class_id'] = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('tax_class_id')->getFirstItem()->getId();
116
+ }
117
+
118
+ private function db_connect()
119
+ {
120
+ $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
121
+ $dbConn = mysqli_init();
122
+ mysqli_options($dbConn, MYSQLI_OPT_LOCAL_INFILE, true);
123
+ if (mysqli_real_connect($dbConn, $dbConf->host, $dbConf->username, $dbConf->password)) {
124
+ $this->db = $dbConn;
125
+ if (!mysqli_select_db($this->db, $dbConf->dbname)) {
126
+ die("Can't select the database: " . mysqli_error($this->db));
127
+ }
128
+ } else {
129
+ die("Could not connect: " . mysqli_error($this->db));
130
+ }
131
+
132
+ }
133
+
134
+ function _LOG($log, $isError = false)
135
+ {
136
+ if ($log) {
137
+ if ($isError) {
138
+ Mage::log($log, null, $this->_errorLogFile);
139
+ } else {
140
+ Mage::log($log, null, $this->_logFile);
141
+ }
142
+ }
143
+ }
144
+
145
+ function cron_start_import()
146
+ {
147
+ $this->_LOG("Start import from cron");
148
+ $start_hr = Mage::getStoreConfig('sinchimport_root/sinch_cron/sinch_cron_time');
149
+ $now_hr = date('H');
150
+ $this->_LOG("Now $now_hr hr, scheduler time is $start_hr hr");
151
+
152
+ if ($start_hr == $now_hr) {
153
+ $this->run_sinch_import();
154
+ } else {
155
+ $this->_LOG(" it's NOT time for SINCH ");
156
+ }
157
+
158
+ $this->_LOG("Finish import from cron");
159
+ }
160
+
161
+ function run_sinch_import()
162
+ {
163
+ $this->_categoryMetaTitleAttrId = $this->_getCategoryAttributeId('meta_title');
164
+ $this->_categoryMetadescriptionAttrId = $this->_getCategoryAttributeId('meta_description');
165
+ $this->_categoryDescriptionAttrId = $this->_getCategoryAttributeId('description');
166
+
167
+ $safe_mode_set = ini_get('safe_mode');
168
+
169
+ $this->InitImportStatuses('FULL');
170
+ if ($safe_mode_set) {
171
+ $this->_LOG('safe_mode is enable. import stoped.');
172
+ $this->set_import_error_reporting_message('Safe_mode is enabled. Please check the documentation on how to fix this. Import stopped.');
173
+ exit;
174
+ }
175
+ $store_proc = $this->check_store_procedure_exist();
176
+
177
+ if (!$store_proc) {
178
+ $this->_LOG('store prcedure "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '" is absent in this database. import stoped.');
179
+ $this->set_import_error_reporting_message('Stored procedure "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '" is absent in this database. Import stopped.');
180
+ exit;
181
+ }
182
+
183
+ $file_privileg = $this->check_db_privileges();
184
+
185
+ if (!$file_privileg) {
186
+ $this->_LOG("Loaddata option not set - please check the documentation on how to fix this. You dan't have privileges for LOAD DATA.");
187
+ $this->set_import_error_reporting_message("Loaddata option not set - please check the documentation on how to fix this. Import stopped.");
188
+ exit;
189
+ }
190
+ $local_infile = $this->check_local_infile();
191
+ if (!$local_infile) {
192
+ $this->_LOG("Loaddata option not set - please check the documentation on how to fix this. Add this string to 'set-variable=local-infile=0' in '/etc/my.cnf'");
193
+ $this->set_import_error_reporting_message("Loaddata option not set - please check the documentation on how to fix this. Import stopped.");
194
+ exit;
195
+ }
196
+
197
+ if ($this->is_imort_not_run()) {
198
+ try {
199
+ $q = "SELECT GET_LOCK('sinchimport', 30)";
200
+ $this->db_do($q);
201
+
202
+ $import = $this;
203
+ $import->addImportStatus('Start Import');
204
+ echo "Upload Files\n";
205
+ $import->UploadFiles();
206
+ $import->addImportStatus('Upload Files');
207
+
208
+ echo "Parse Category Types\n";
209
+ $import->ParseCategoryTypes();
210
+
211
+ echo "Parse Categories\n";
212
+ $coincidence = $import->ParseCategories();
213
+ $import->addImportStatus('Parse Categories');
214
+
215
+ echo "Parse Category Features\n";
216
+ $import->ParseCategoryFeatures();
217
+ $import->addImportStatus('Parse Category Features');
218
+
219
+ echo "Parse Distributors\n";
220
+ $import->ParseDistributors();
221
+ if ($this->product_file_format == "NEW") {
222
+ $this->ParseDistributorsStockAndPrice();
223
+ $this->ParseProductContracts();
224
+ }
225
+ $import->addImportStatus('Parse Distributors');
226
+
227
+ echo "Parse EAN Codes\n";
228
+ $import->ParseEANCodes();
229
+ $import->addImportStatus('Parse EAN Codes');
230
+
231
+
232
+ echo "Parse Manufacturers\n";
233
+ $import->ParseManufacturers();
234
+ $import->addImportStatus('Parse Manufacturers');
235
+
236
+ echo "Parse Related Products\n";
237
+ $import->ParseRelatedProducts();
238
+ $import->addImportStatus('Parse Related Products');
239
+
240
+ echo "Parse Product Features\n";
241
+ $import->ParseProductFeatures();
242
+ $import->addImportStatus('Parse Product Features');
243
+
244
+ echo "Parse Product Categories\n";
245
+ $import->ParseProductCategories();
246
+
247
+ echo "Parse Products\n";
248
+ $import->ParseProducts($coincidence);
249
+ $import->addImportStatus('Parse Products');
250
+
251
+ echo "Parse Pictures Gallery";
252
+ $import->ParseProductsPicturesGallery();
253
+ $import->addImportStatus('Parse Pictures Gallery');
254
+
255
+ echo "Parse Restricted Values\n";
256
+ $import->ParseRestrictedValues();
257
+ $import->addImportStatus('Parse Restricted Values');
258
+
259
+ echo "Parse Stock And Prices\n";
260
+ $import->ParseStockAndPrices();
261
+ $import->addImportStatus('Parse Stock And Prices');
262
+
263
+ echo "Apply Customer Group Price\n";
264
+ //$import->ParsePriceRules();
265
+ //$import->AddPriceRules();
266
+ //$import->ApplyCustomerGroupPrice();
267
+
268
+ if (file_exists($this->varDir . FILE_PRICE_RULES)) {
269
+ $ftpCred = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
270
+ Mage::dispatchEvent('sinch_pricerules_import_ftp', array(
271
+ 'ftp_host' => $ftpCred["ftp_server"],
272
+ 'ftp_username' => $ftpCred["login"],
273
+ 'ftp_password' => $ftpCred["password"]
274
+ ));
275
+ }
276
+
277
+ Mage::log("Finish Sinch import", null, $this->_logFile);
278
+ echo "Finish Sinch import\n";
279
+
280
+ Mage::log("Start cleanin Sinch cache\n", null, $this->_logFile);
281
+ echo "Start cleanin Sinch cache\n";
282
+ Mage::app()->getCacheInstance()->cleanType('block_html');
283
+
284
+ Mage::log("Start indexing Sinch features for filters", null, $this->_logFile);
285
+ echo "Start indexing Sinch features for filters\n";
286
+
287
+ // Drop feature result tables
288
+ $this->_dropFeatureResultTables();
289
+
290
+ Mage::log("Finish indexing Sinch features for filters", null, $this->_logFile);
291
+ $import->addImportStatus('Generate category filters');
292
+ echo "Finish indexing Sinch features for filters\n";
293
+
294
+
295
+ Mage::log("Start indexing data", null, $this->_logFile);
296
+ echo "Start indexing data";
297
+ $import->_cleanCateoryProductFlatTable();
298
+ $import->runIndexer();
299
+
300
+ // Reindex manufacturers
301
+ $import->reindexManufacturers();
302
+
303
+ Mage::log("Finish indexing data", null, $this->_logFile);
304
+ $import->addImportStatus('Indexing data', 1);
305
+ echo "Finish indexing data";
306
+
307
+ $q = "SELECT RELEASE_LOCK('sinchimport')";
308
+ $this->db_do($q);
309
+ } catch (Exception $e) {
310
+ $this->set_import_error_reporting_message($e);
311
+ }
312
+ } else {
313
+ Mage::log("Sinchimport already run", null, $this->_logFile);
314
+ echo "Sinchimport already run\n";
315
+ }
316
+
317
+ }
318
+
319
+ private function _getCategoryAttributeId($attributeCode)
320
+ {
321
+ return $this->_getAttributeId($attributeCode, 'catalog_category');
322
+ }
323
+
324
+ private function _getAttributeId($attributeCode, $typeCode)
325
+ {
326
+ if ($typeCode == 'catalog_product') {
327
+ $typeId = $this->_getProductEntityTypeId();
328
+ } else {
329
+ $typeId = $this->_getEntityTypeId($typeCode);
330
+ }
331
+ if (!isset($this->_attributeId[$typeCode]) OR !is_array($this->_attributeId[$typeCode])) {
332
+ $sql = "
333
+ SELECT attribute_id, attribute_code
334
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute') . "
335
+ WHERE entity_type_id = '" . $typeId . "'
336
+ ";
337
+ $result = $this->db_do($sql);
338
+ while ($row = mysqli_fetch_assoc($result)) {
339
+ $this->_attributeId[$typeCode][$row['attribute_code']] = $row['attribute_id'];
340
+ }
341
+ }
342
+ return $this->_attributeId[$typeCode][$attributeCode];
343
+ }
344
+
345
+ private function _getProductEntityTypeId()
346
+ {
347
+ if (!$this->_productEntityTypeId) {
348
+ $this->_productEntityTypeId = $this->_getEntityTypeId('catalog_product');
349
+ }
350
+ return $this->_productEntityTypeId;
351
+ }
352
+
353
+ private function _getEntityTypeId($code)
354
+ {
355
+ $sql = "
356
+ SELECT entity_type_id
357
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_entity_type') . "
358
+ WHERE entity_type_code = '" . $code . "'
359
+ LIMIT 1
360
+ ";
361
+ $result = $this->db_do($sql);
362
+ if ($row = mysqli_fetch_assoc($result)) {
363
+ return $row['entity_type_id'];
364
+ }
365
+ return false;
366
+ }
367
+
368
+ private function db_do($query, $stopLog = false)
369
+ {
370
+ if ($this->debug_mode && !$stopLog) {
371
+ Mage::log("Query: " . $query, null, $this->_logFile);
372
+ }
373
+ $result = mysqli_query($this->db, $query);
374
+ if (!$result) {
375
+ $this->_LOG("Invalid query: $query\n -->Exception: " . mysqli_error($this->db), true);
376
+ throw new Exception("Invalid query: $query\n" . mysqli_error($this->db));
377
+ } else {
378
+ return $result;
379
+ }
380
+ return $result;
381
+ }
382
+
383
+ function InitImportStatuses($type)
384
+ {
385
+ $this->db_do("DROP TABLE IF EXISTS " . $this->import_status_table);
386
+ $this->db_do("CREATE TABLE " . $this->import_status_table . "(
387
+ id int(11) NOT NULL auto_increment PRIMARY KEY,
388
+ message varchar(50),
389
+ finished int(1) default 0
390
+ )"
391
+ );
392
+ $this->db_do("INSERT INTO " . $this->import_status_statistic_table . " (
393
+ start_import,
394
+ finish_import,
395
+ import_type,
396
+ global_status_import,
397
+ import_run_type,
398
+ error_report_message)
399
+ VALUES(
400
+ now(),
401
+ now(),
402
+ '$type',
403
+ 'Run',
404
+ '" . $this->import_run_type . "',
405
+ ''
406
+ )
407
+ ");
408
+ $q = "SELECT MAX(id) AS id FROM " . $this->import_status_statistic_table;
409
+
410
+ $quer = $this->db_do($q);
411
+ $row = mysqli_fetch_array($quer);
412
+ $this->current_import_status_statistic_id = $row['id'];
413
+ $this->db_do("UPDATE " . $this->import_status_statistic_table . "
414
+ SET global_status_import='Failed'
415
+ WHERE global_status_import='Run' AND id!=" . $this->current_import_status_statistic_id);
416
+
417
+ }
418
+
419
+ function set_import_error_reporting_message($message)
420
+ {
421
+ $this->db_do("UPDATE " . $this->import_status_statistic_table . "
422
+ SET error_report_message='" . mysqli_real_escape_string($this->db, $message) . "'
423
+ WHERE id=" . $this->current_import_status_statistic_id);
424
+ }
425
+
426
+ function check_store_procedure_exist()
427
+ {
428
+ $dbConf = Mage::getConfig()->getResourceConnectionConfig('core_setup');
429
+ $q = 'SHOW PROCEDURE STATUS LIKE "' . Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s') . '"';
430
+ $quer = $this->db_do($q);
431
+ $result = false;
432
+ While ($row = mysqli_fetch_array($quer)) {
433
+ if (($row['Name'] == Mage::getSingleton('core/resource')->getTableName('filter_sinch_products_s')) && ($row['Db'] == $dbConf->dbname)) {
434
+ $result = true;
435
+ }
436
+ }
437
+ return $result;
438
+ }
439
+
440
+ function check_db_privileges()
441
+ {
442
+ $q = 'SHOW PRIVILEGES';
443
+ $quer = $this->db_do($q);
444
+ while ($row = mysqli_fetch_array($quer)) {
445
+ if ($row['Privilege'] == 'File' && $row['Context'] == 'File access on server') {
446
+ return true;
447
+ }
448
+ }
449
+ return false;
450
+ }
451
+
452
+ function check_local_infile()
453
+ {
454
+ $q = 'SHOW VARIABLES LIKE "local_infile"';
455
+ $quer = $this->db_do($q);
456
+ $row = mysqli_fetch_array($quer);
457
+ if ($row['Variable_name'] == 'local_infile' && $row['Value'] == "ON") {
458
+ return true;
459
+ } else {
460
+ return false;
461
+ }
462
+ }
463
+
464
+ function is_imort_not_run()
465
+ {
466
+ $q = "SELECT IS_FREE_LOCK('sinchimport') as getlock";
467
+ $quer = $this->db_do($q);
468
+ $row = mysqli_fetch_array($quer);
469
+ return $row['getlock'];
470
+ }
471
+
472
+ function addImportStatus($message, $finished = 0)
473
+ {
474
+ $q = "INSERT INTO " . $this->import_status_table . "
475
+ (message, finished)
476
+ VALUES('" . $message . "', $finished)";
477
+ $this->db_do($q);
478
+ $this->db_do("UPDATE " . $this->import_status_statistic_table . "
479
+ SET detail_status_import='" . $message . "'
480
+ WHERE id=" . $this->current_import_status_statistic_id);
481
+ if ($finished == 1) {
482
+ $this->db_do("UPDATE " . $this->import_status_statistic_table . "
483
+ SET
484
+ global_status_import='Successful',
485
+ finish_import=now()
486
+ WHERE
487
+ error_report_message='' and
488
+ id=" . $this->current_import_status_statistic_id);
489
+ }
490
+ }
491
+
492
+ function UploadFiles()
493
+ {
494
+ $this->_LOG("Start upload files");
495
+ $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
496
+ $login = $dataConf['login'];
497
+ $passw = $dataConf['password'];
498
+ $server = $dataConf['ftp_server'];
499
+
500
+ if (!$login || !$passw) {
501
+ $this->_LOG('ftp login or password dosent defined');
502
+ $this->set_import_error_reporting_message('FTP login or password has not been defined. Import stopped.');
503
+ exit;
504
+
505
+ }
506
+ $file_url_and_dir = $this->repl_ph(FILE_URL_AND_DIR, array(
507
+ 'server' => $server,
508
+ 'login' => $login,
509
+ 'password' => $passw
510
+ )
511
+ );
512
+ foreach ($this->files as $file) {
513
+ $this->_LOG("Copy " . $file_url_and_dir . $file . " to " . $this->varDir . $file);
514
+ if (strstr($file_url_and_dir, 'ftp://')) {
515
+ preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $file_url_and_dir, $match);
516
+ if ($conn = ftp_connect($match[3])) {
517
+ if (!ftp_login($conn, $login, $passw)) {
518
+ $this->set_import_error_reporting_message('Incorrect username or password for the Stock In The Channel server. Import stopped.');
519
+ exit;
520
+ }
521
+ } else {
522
+ $this->set_import_error_reporting_message('FTP connection failed. Unable to connect to the Stock In The Channel server');
523
+ exit;
524
+ }
525
+ if (!$this->wget($file_url_and_dir . $file, $this->varDir . $file, 'system')) {
526
+ $this->_LOG("wget Can't copy " . $file . ", will use old one");
527
+ echo "copy Can't copy " . $file_url_and_dir . $file . " to " . $this->varDir . $file . ", will use old one\n";
528
+ }
529
+ } else {
530
+ if (!copy($file_url_and_dir . $file, $this->varDir . $file)) {
531
+ $this->_LOG("copy Can't copy " . $file . ", will use old one");
532
+ echo "copy Can't copy " . $file_url_and_dir . $file . " to " . $this->varDir . $file . " will use old one\n";
533
+ }
534
+ }
535
+ exec("chmod a+rw " . $this->varDir . $file);
536
+ if (!filesize($this->varDir . $file)) {
537
+ if ($file != FILE_CATEGORIES_FEATURES && $file != FILE_PRODUCT_FEATURES && $file != FILE_RELATED_PRODUCTS && $file != FILE_RESTRICTED_VALUES && $file != FILE_PRODUCT_CATEGORIES && $file != FILE_CATEGORY_TYPES && $file != FILE_DISTRIBUTORS_STOCK_AND_PRICES && $file != FILE_PRODUCT_CONTRACTS && $file != FILE_PRICE_RULES) {
538
+ $this->_LOG("Can't copy " . $file_url_and_dir . $file . ". file $this->varDir.$file is emty");
539
+ $this->set_import_error_reporting_message("Can't copy " . $file_url_and_dir . $file . ". file " . $this->varDir . $file . " is emty");
540
+ $this->addImportStatus('Sinch import stoped. Impot file(s) empty', 1);
541
+
542
+ exit;
543
+ } else {
544
+ if ($file == FILE_CATEGORIES_FEATURES) {
545
+ $this->_LOG("Can't copy " . FILE_CATEGORIES_FEATURES . " file ignored");
546
+ $this->_ignore_category_features = true;
547
+ } elseif ($file == FILE_PRODUCT_FEATURES) {
548
+ $this->_LOG("Can't copy " . FILE_PRODUCT_FEATURES . " file ignored");
549
+ $this->_ignore_product_features = true;
550
+ } elseif ($file == FILE_RELATED_PRODUCTS) {
551
+ $this->_LOG("Can't copy " . FILE_RELATED_PRODUCTS . " file ignored");
552
+ $this->_ignore_product_related = true;
553
+ } elseif ($file == FILE_RESTRICTED_VALUES) {
554
+ $this->_LOG("Can't copy " . FILE_RESTRICTED_VALUES . " file ignored");
555
+ $this->_ignore_restricted_values = true;
556
+ } elseif ($file == FILE_PRODUCT_CATEGORIES) {
557
+ $this->_LOG("Can't copy " . FILE_PRODUCT_CATEGORIES . " file ignored");
558
+ $this->_ignore_product_categories = true;
559
+ $this->product_file_format = "OLD";
560
+ } elseif ($file == FILE_CATEGORY_TYPES) {
561
+ $this->_LOG("Can't copy " . FILE_CATEGORY_TYPES . " file ignored");
562
+ $this->_ignore_category_types = true;
563
+ } elseif ($file == FILE_DISTRIBUTORS_STOCK_AND_PRICES) {
564
+ $this->_LOG("Can't copy " . FILE_DISTRIBUTORS_STOCK_AND_PRICES . " file ignored");
565
+ $this->_ignore_category_types = true;
566
+ } elseif ($file == FILE_PRODUCT_CONTRACTS) {
567
+ $this->_LOG("Can't copy " . FILE_PRODUCT_CONTRACTS . " file ignored");
568
+ $this->_ignore_product_contracts = true;
569
+ } elseif ($file == FILE_PRICE_RULES) {
570
+ $this->_LOG("Can't copy " . FILE_PRICE_RULES . " file ignored");
571
+ $this->_ignore_price_rules = true;
572
+ }
573
+
574
+ }
575
+ }
576
+ }
577
+ if (file_exists($file_url_and_dir . FILE_PRODUCT_CATEGORIES)) {
578
+ $this->product_file_format = "NEW";
579
+ $this->_LOG("File " . $file_url_and_dir . FILE_PRODUCT_CATEGORIES . " exist. Will used parser for NEW format product.csv");
580
+ } else {
581
+ $this->product_file_format = "OLD";
582
+ $this->_LOG("File " . $file_url_and_dir . FILE_PRODUCT_CATEGORIES . " dosen't exist. Will used parser for OLD format product.csv");
583
+ }
584
+ $this->_LOG("Finish upload files");
585
+ }
586
+
587
+ private function repl_ph($content, $hash)
588
+ {
589
+ if ($hash) {
590
+ foreach ($hash as $key => $val) {
591
+ if ($key == "category_name") {
592
+ if (strlen($val) > 25) {
593
+ $val = substr($val, 0, 24) . "...";
594
+ }
595
+ }
596
+ $content = preg_replace("/%%%$key%%%/", $val, $content);
597
+ }
598
+ }
599
+ return $content;
600
+ }
601
+
602
+ function wget()
603
+ {
604
+
605
+ $got = func_num_args();
606
+ $url = $file = $flag = false;
607
+
608
+ if ($got < 1) {
609
+ return false;
610
+ } elseif ($got == 1) {
611
+ $url = func_get_arg(0);
612
+ } elseif ($got == 2) {
613
+ $url = func_get_arg(0);
614
+ $file = func_get_arg(1);
615
+ } elseif ($got == 3) {
616
+ $url = func_get_arg(0);
617
+ $file = func_get_arg(1);
618
+ $flag = func_get_arg(2);
619
+ }
620
+
621
+ if ($flag == 'copy') {
622
+ if (copy($url, $file)) {
623
+ return true;
624
+ } else {
625
+ return false;
626
+ }
627
+ } elseif ($flag == 'system') {
628
+ exec("wget -O$file $url");
629
+ return true;
630
+ } else {
631
+ $c = curl_init($url);
632
+ curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
633
+ curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
634
+ curl_setopt($c, CURLOPT_HEADER, array("Accept-Encoding: gzip"));
635
+ if (!$file) {
636
+ $page = curl_exec($c);
637
+ curl_close($c);
638
+ return $page;
639
+ } else {
640
+ $FH = fopen($file, "wb");
641
+ fwrite($FH, curl_exec($c));
642
+ fclose($FH);
643
+ curl_close($c);
644
+ return true;
645
+ }
646
+ }
647
+ }
648
+
649
+ function ParseCategoryTypes()
650
+ {
651
+ $parse_file = $this->varDir . FILE_CATEGORY_TYPES;
652
+ if (filesize($parse_file)) {
653
+ $this->_LOG("Start parse " . FILE_CATEGORY_TYPES);
654
+
655
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('category_types_temp'));
656
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('category_types_temp') . "(
657
+ id int(11),
658
+ name varchar(255),
659
+ key(id)
660
+ )");
661
+
662
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
663
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('category_types_temp') . "
664
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
665
+ OPTIONALLY ENCLOSED BY '\"'
666
+ LINES TERMINATED BY \"\r\n\"
667
+ IGNORE 1 LINES ");
668
+
669
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_category_types'));
670
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('category_types_temp') . "
671
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_category_types'));
672
+
673
+ $this->_LOG("Finish parse " . FILE_CATEGORY_TYPES);
674
+ } else {
675
+ $this->_LOG("Wrong file " . $parse_file);
676
+ }
677
+ $this->_LOG(' ');
678
+
679
+ }
680
+
681
+ function ParseCategories()
682
+ {
683
+
684
+ $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
685
+ $im_type = $dataConf['replace_category'];
686
+ $parse_file = $this->varDir . FILE_CATEGORIES;
687
+ //$parse_file = $this->varDir . FILE_CATEGORIES_TEST;
688
+ $field_terminated_char = $this->field_terminated_char;
689
+
690
+ $this->im_type = $im_type;
691
+
692
+ if (filesize($parse_file)) {
693
+ $this->_LOG("Start parse " . FILE_CATEGORIES);
694
+
695
+ $this->_getCategoryEntityTypeIdAndDefault_attribute_set_id();
696
+
697
+ $categories_temp = Mage::getSingleton('core/resource')->getTableName('categories_temp');
698
+ $catalog_category_entity = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity');
699
+ $catalog_category_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar');
700
+ $catalog_category_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_int');
701
+ $stINch_categories_mapping_temp = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping_temp');
702
+ $stINch_categories_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping');
703
+ $stINch_categories = Mage::getSingleton('core/resource')->getTableName('stINch_categories');
704
+ $category_types = Mage::getSingleton('core/resource')->getTableName('stINch_category_types');
705
+
706
+ $_categoryEntityTypeId = $this->_categoryEntityTypeId;
707
+ $_categoryDefault_attribute_set_id = $this->_categoryDefault_attribute_set_id;
708
+
709
+ $name_attrid = $this->_getCategoryAttributeId('name');
710
+ $is_anchor_attrid = $this->_getCategoryAttributeId('is_anchor');
711
+ $image_attrid = $this->_getCategoryAttributeId('image');
712
+
713
+ $attr_url_key = $this->attributes['url_key'];
714
+ $attr_display_mode = $this->attributes['display_mode'];
715
+ $attr_is_active = $this->attributes['is_active'];
716
+ $attr_include_in_menu = $this->attributes['include_in_menu'];
717
+
718
+ $this->loadCategoriesTemp($categories_temp, $parse_file, $field_terminated_char);
719
+ $coincidence = $this->calculateCategoryCoincidence($categories_temp, $catalog_category_entity, $catalog_category_entity_varchar, $im_type, $category_types);
720
+
721
+ if (!$this->check_loaded_data($parse_file, $categories_temp)) {
722
+ $inf = mysqli_info();
723
+ $this->set_import_error_reporting_message('The Stock In The Channel data files do not appear to be in the correct format. Check file' . $parse_file . "(LOAD DATA ... " . $inf . ")");
724
+ exit;
725
+ }
726
+
727
+ echo("\n\ncoincidence = [" . count($coincidence) . "]\n\n");
728
+
729
+ if (count($coincidence) == 1) // one store logic
730
+ {
731
+ echo("\n\n\n====================================\nSINGLE STORE\n====================================\n\n\n");
732
+ if ($im_type == "REWRITE") {
733
+ $root_cat = 2;
734
+
735
+ $root_cat = $this->truncateAllCateriesAndRecreateDefaults($root_cat, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
736
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id,
737
+ $name_attrid, $attr_url_key, $attr_display_mode, $attr_is_active, $attr_include_in_menu);
738
+ } else {
739
+ $root_cat = $this->_getShopRootCategoryId();
740
+ }
741
+
742
+ $this->_root_cat = $root_cat;
743
+
744
+ $this->setCategorySettings($categories_temp, $root_cat);
745
+ $this->mapSinchCategories($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type, $root_cat);
746
+ $this->addCategoryData($categories_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
747
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid, $im_type, $root_cat);
748
+ } else if (count($coincidence) > 1) // multistore logic
749
+ {
750
+ echo("\n\n\n====================================\nMULTIPESTORE\n====================================\n\n\n");
751
+ switch ($im_type) {
752
+ case "REWRITE":
753
+ $this->rewriteMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
754
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
755
+ $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
756
+ $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp);
757
+ break;
758
+ case "MERGE" :
759
+ $this->mergeMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
760
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
761
+ $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
762
+ $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp);
763
+ break;
764
+ default :
765
+ //do nothing
766
+ };
767
+ } else {
768
+ echo("error");
769
+ }
770
+
771
+ $this->_LOG("Finish parse " . FILE_CATEGORIES);
772
+ } else {
773
+ $this->_LOG("Wrong file " . $parse_file);
774
+ }
775
+ $this->_LOG(' ');
776
+ $this->_set_default_root_category();
777
+ return $coincidence;
778
+ }
779
+
780
+ private function _getCategoryEntityTypeIdAndDefault_attribute_set_id()
781
+ {
782
+ if (!$this->_categoryEntityTypeId || !$this->_categoryDefault_attribute_set_id) {
783
+ $sql = "
784
+ SELECT entity_type_id, default_attribute_set_id
785
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_entity_type') . "
786
+ WHERE entity_type_code = 'catalog_category'
787
+ LIMIT 1
788
+ ";
789
+ $result = $this->db_do($sql);
790
+ if ($row = mysqli_fetch_assoc($result)) {
791
+ $this->_categoryEntityTypeId = $row['entity_type_id'];
792
+ $this->_categoryDefault_attribute_set_id = $row['default_attribute_set_id'];
793
+ }
794
+ }
795
+ }
796
+
797
+ private function loadCategoriesTemp($categories_temp, $parse_file, $field_terminated_char)
798
+ {
799
+ $this->db_do("DROP TABLE IF EXISTS $categories_temp");
800
+
801
+ $this->db_do("
802
+ CREATE TABLE $categories_temp
803
+ (
804
+ store_category_id INT(11),
805
+ parent_store_category_id INT(11),
806
+ category_name VARCHAR(50),
807
+ order_number INT(11),
808
+ is_hidden VARCHAR(10),
809
+ products_within_sub_categories INT(11),
810
+ products_within_this_category INT(11),
811
+ categories_image VARCHAR(255),
812
+ level INT(10) NOT NULL DEFAULT 0,
813
+ children_count INT(11) NOT NULL DEFAULT 0,
814
+ UNSPSC INT(10) DEFAULT NULL,
815
+ RootName INT(10) DEFAULT NULL,
816
+ MainImageURL VARCHAR(255),
817
+ MetaTitle TEXT,
818
+ MetaDescription TEXT,
819
+ Description TEXT,
820
+ KEY(store_category_id),
821
+ KEY(parent_store_category_id)
822
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
823
+
824
+ $this->db_do("
825
+ LOAD DATA LOCAL INFILE '$parse_file' INTO TABLE $categories_temp
826
+ FIELDS TERMINATED BY '$field_terminated_char' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY \"\r\n\" IGNORE 1 LINES");
827
+
828
+ $this->db_do("ALTER TABLE $categories_temp ADD COLUMN include_in_menu TINYINT(1) NOT NULL DEFAULT 1");
829
+ $this->db_do("UPDATE $categories_temp SET include_in_menu = 0 WHERE UCASE(is_hidden)='TRUE'");
830
+
831
+ $this->db_do("ALTER TABLE $categories_temp ADD COLUMN is_anchor TINYINT(1) NOT NULL DEFAULT 1");
832
+ $this->db_do("UPDATE $categories_temp SET level = (level+2) WHERE level >= 0");
833
+ }
834
+
835
+ private function calculateCategoryCoincidence($categories_temp, $catalog_category_entity, $catalog_category_entity_varchar, $im_type, $category_types)
836
+ {
837
+ $root_categories = $this->db_do("
838
+ SELECT
839
+ cce.entity_id,
840
+ ccev.value AS category_name
841
+ FROM $catalog_category_entity cce
842
+ JOIN $catalog_category_entity_varchar ccev
843
+ ON cce.entity_id = ccev.entity_id
844
+ AND ccev.store_id = 0
845
+ AND cce.entity_type_id = ccev.entity_type_id
846
+ AND ccev.attribute_id = 41
847
+ WHERE parent_id = 1"); // 41 - category name
848
+ $OLD = array();
849
+ while ($root_cat = mysqli_fetch_array($root_categories)) $OLD[] = $root_cat['category_name'];
850
+
851
+ $new_categories = $this->db_do("SELECT DISTINCT RootName FROM $categories_temp");
852
+
853
+ while ($new_root_cat = mysqli_fetch_array($new_categories)) $exists_coincidence[$new_root_cat['RootName']] = TRUE;
854
+
855
+ echo("\ncalculateCategoryCoincidence ...im_type = [$im_type]\n\n");
856
+ var_dump($exists_coincidence);
857
+
858
+ return $exists_coincidence;
859
+ }
860
+
861
+ function check_loaded_data($file, $table)
862
+ {
863
+ $cnt_strings_in_file = $this->file_strings_count($file);
864
+ $cnt_rows_int_table = $this->table_rows_count($table);
865
+ $persent_cnt_strings_in_file = $cnt_strings_in_file / 10;
866
+ if ($cnt_rows_int_table > $persent_cnt_strings_in_file) {
867
+ return true;
868
+ } else {
869
+ return false;
870
+ }
871
+ }
872
+
873
+ function file_strings_count($parse_file)
874
+ {
875
+ $files_str = count(file($parse_file));
876
+ return $files_str;
877
+ }
878
+
879
+ function table_rows_count($table)
880
+ {
881
+ $rows_count_res = $this->db_do("select count(*) as cnt from " . $table);
882
+ $rows_count = mysqli_fetch_array($rows_count_res);
883
+ return ($rows_count['cnt']);
884
+ }
885
+
886
+ private function truncateAllCateriesAndRecreateDefaults($root_cat, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
887
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id,
888
+ $name_attrid, $attr_url_key, $attr_display_mode, $attr_is_active, $attr_include_in_menu)
889
+ {
890
+ $this->db_do('SET foreign_key_checks=0');
891
+
892
+ $this->db_do("TRUNCATE $catalog_category_entity");
893
+ $this->db_do("
894
+ INSERT $catalog_category_entity
895
+ (
896
+ entity_id,
897
+ entity_type_id,
898
+ attribute_set_id,
899
+ parent_id,
900
+ created_at,
901
+ updated_at,
902
+ path,
903
+ position,
904
+ level,
905
+ children_count,
906
+ store_category_id,
907
+ parent_store_category_id
908
+ )
909
+ VALUES
910
+ (1, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 0, '0000-00-00 00:00:00', now(), '1', 0, 0, 1, null, null),
911
+ (2, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 1, now(), now(), '1/2', 1, 1, 1, null, null)");
912
+
913
+ $this->db_do("TRUNCATE $catalog_category_entity_varchar");
914
+ $this->db_do("
915
+ INSERT $catalog_category_entity_varchar
916
+ (
917
+ value_id,
918
+ entity_type_id,
919
+ attribute_id,
920
+ store_id,
921
+ entity_id,
922
+ value
923
+ )
924
+ VALUES
925
+ (1, $_categoryEntityTypeId, $name_attrid, 0, 1, 'Root Catalog'),
926
+ (2, $_categoryEntityTypeId, $name_attrid, 1, 1, 'Root Catalog'),
927
+ (3, $_categoryEntityTypeId, $attr_url_key, 0, 1, 'root-catalog'),
928
+ (4, $_categoryEntityTypeId, $name_attrid, 0, 2, 'Default Category'),
929
+ (5, $_categoryEntityTypeId, $name_attrid, 1, 2, 'Default Category'),
930
+ (6, $_categoryEntityTypeId, $attr_display_mode, 1, 2, 'PRODUCTS'),
931
+ (7, $_categoryEntityTypeId, $attr_url_key, 0, 2, 'default-category')");
932
+
933
+ $this->db_do("TRUNCATE $catalog_category_entity_int");
934
+ $this->db_do("
935
+ INSERT $catalog_category_entity_int
936
+ (
937
+ value_id,
938
+ entity_type_id,
939
+ attribute_id,
940
+ store_id,
941
+ entity_id,
942
+ value
943
+ )
944
+ VALUES
945
+ (1, $_categoryEntityTypeId, $attr_is_active, 0, 2, 1),
946
+ (2, $_categoryEntityTypeId, $attr_is_active, 1, 2, 1),
947
+ (3, $_categoryEntityTypeId, $attr_include_in_menu, 0, 1, 1),
948
+ (4, $_categoryEntityTypeId, $attr_include_in_menu, 0, 2, 1)");
949
+
950
+ return $root_cat;
951
+ }
952
+
953
+ private function _getShopRootCategoryId($cat_id = 0)
954
+ {
955
+ if ($root_cat = Mage::app()->getStore()->getRootCategoryId()) {
956
+ return $root_cat;
957
+ } else {
958
+ $q = "SELECT
959
+ entity_id
960
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar') . "
961
+ WHERE
962
+ value='default-category'";
963
+ $res = $this->db_do($q);
964
+ $row = mysqli_fetch_array($res);
965
+ if ($row['entity_id'] > 0) {
966
+ return $row['entity_id'];
967
+ } else {
968
+ $q = "SELECT entity_id
969
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
970
+ WHERE parent_id=" . $cat_id;
971
+ $quer = $this->db_do($q);
972
+ $count = 0;
973
+ while ($row = mysqli_fetch_array($quer)) {
974
+ $count++;
975
+ $entity_id = $row['entity_id'];
976
+ }
977
+ if ($count > 1 || $count == 0) {
978
+ return ($cat_id);
979
+ } else {
980
+ return $this->_getShopRootCategoryId($entity_id);
981
+ }
982
+ }
983
+ }
984
+ }
985
+
986
+ private function setCategorySettings($categories_temp, $root_cat)
987
+ {
988
+ $this->db_do("
989
+ UPDATE $categories_temp
990
+ SET parent_store_category_id = $root_cat
991
+ WHERE parent_store_category_id = 0");
992
+
993
+ $store_cat_ids = $this->db_do("SELECT store_category_id FROM $categories_temp");
994
+ while ($row = mysqli_fetch_array($store_cat_ids)) {
995
+ $store_category_id = $row['store_category_id'];
996
+
997
+ $children_count = $this->count_children($store_category_id);
998
+ $level = $this->get_category_level($store_category_id);
999
+
1000
+ $this->db_do("
1001
+ UPDATE $categories_temp
1002
+ SET children_count = $children_count,
1003
+ level = $level
1004
+ WHERE store_category_id = $store_category_id");
1005
+ }
1006
+ }
1007
+
1008
+ function count_children($id)
1009
+ {
1010
+
1011
+ $q = "SELECT store_category_id
1012
+ FROM " . Mage::getSingleton('core/resource')->getTableName('categories_temp') . "
1013
+ WHERE parent_store_category_id=" . $id;
1014
+ $quer = $this->db_do($q);
1015
+ $count = 0;
1016
+ while ($row = mysqli_fetch_array($quer)) {
1017
+ $count += $this->count_children($row['store_category_id']);
1018
+ $count++;
1019
+ }
1020
+ return ($count);
1021
+ }
1022
+
1023
+ function get_category_level($id)
1024
+ {
1025
+ $q = "SELECT parent_store_category_id
1026
+ FROM " . Mage::getSingleton('core/resource')->getTableName('categories_temp') . "
1027
+ WHERE store_category_id=" . $id;
1028
+ $quer = $this->db_do($q);
1029
+ $level = 1;
1030
+ $row = mysqli_fetch_array($quer);
1031
+ while ($row['parent_store_category_id'] != 0) {
1032
+ $q = "SELECT parent_store_category_id
1033
+ FROM " . Mage::getSingleton('core/resource')->getTableName('categories_temp') . "
1034
+ WHERE store_category_id=" . $row['parent_store_category_id'];
1035
+ $quer = $this->db_do($q);
1036
+ $row = mysqli_fetch_array($quer);
1037
+ $level++;
1038
+ if ($level > 20) {
1039
+ break;
1040
+ }
1041
+ }
1042
+
1043
+ return ($level);
1044
+ }
1045
+
1046
+ public function mapSinchCategories($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type, $root_cat, $mapping_again = false)
1047
+ {
1048
+ $stINch_categories_mapping_temp = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping_temp');
1049
+
1050
+ $this->db_do("DROP TABLE IF EXISTS $stINch_categories_mapping_temp");
1051
+
1052
+ $this->db_do("
1053
+ CREATE TABLE $stINch_categories_mapping_temp
1054
+ (
1055
+ shop_entity_id INT(11) UNSIGNED NOT NULL,
1056
+ shop_entity_type_id INT(11),
1057
+ shop_attribute_set_id INT(11),
1058
+ shop_parent_id INT(11),
1059
+ shop_store_category_id INT(11),
1060
+ shop_parent_store_category_id INT(11),
1061
+ store_category_id INT(11),
1062
+ parent_store_category_id INT(11),
1063
+ category_name VARCHAR(255),
1064
+ order_number INT(11),
1065
+ products_within_this_category INT(11),
1066
+
1067
+ KEY shop_entity_id (shop_entity_id),
1068
+ KEY shop_parent_id (shop_parent_id),
1069
+ KEY store_category_id (store_category_id),
1070
+ KEY parent_store_category_id (parent_store_category_id),
1071
+ UNIQUE KEY(shop_entity_id)
1072
+ )");
1073
+
1074
+ $this->db_do("CREATE TABLE IF NOT EXISTS $stINch_categories_mapping LIKE $stINch_categories_mapping_temp");
1075
+
1076
+ // added for mapping new sinch categories in merge && !UPDATE_CATEGORY_DATA mode
1077
+ if ((UPDATE_CATEGORY_DATA && $im_type == "MERGE") || ($im_type == "REWRITE")) {
1078
+ // backup Category ID in REWRITE mode
1079
+ if ($mapping_again) {
1080
+ $this->db_do("
1081
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
1082
+ (
1083
+ shop_entity_id,
1084
+ shop_entity_type_id,
1085
+ shop_attribute_set_id,
1086
+ shop_parent_id,
1087
+ shop_store_category_id,
1088
+ shop_parent_store_category_id
1089
+ )
1090
+ (SELECT
1091
+ entity_id,
1092
+ entity_type_id,
1093
+ attribute_set_id,
1094
+ parent_id,
1095
+ store_category_id,
1096
+ parent_store_category_id
1097
+ FROM $catalog_category_entity)");
1098
+
1099
+ $this->db_do("
1100
+ UPDATE $stINch_categories_mapping_temp cmt
1101
+ JOIN $categories_temp c
1102
+ ON cmt.shop_store_category_id = c.store_category_id
1103
+ SET
1104
+ cmt.store_category_id = c.store_category_id,
1105
+ cmt.parent_store_category_id = c.parent_store_category_id,
1106
+ cmt.category_name = c.category_name,
1107
+ cmt.order_number = c.order_number,
1108
+ cmt.products_within_this_category = c.products_within_this_category");
1109
+
1110
+ $this->db_do("
1111
+ UPDATE $stINch_categories_mapping_temp cmt
1112
+ JOIN $catalog_category_entity cce
1113
+ ON cmt.parent_store_category_id = cce.store_category_id
1114
+ SET cmt.shop_parent_id = cce.entity_id");
1115
+
1116
+ $this->db_do("
1117
+ UPDATE $stINch_categories_mapping_temp cmt
1118
+ JOIN $categories_temp c
1119
+ ON cmt.shop_store_category_id = c.store_category_id
1120
+ SET shop_parent_id = " . $this->_root_cat . "
1121
+ WHERE shop_parent_id = 0");
1122
+
1123
+ $this->db_do("
1124
+ UPDATE $stINch_categories_mapping_temp cmt
1125
+ JOIN $catalog_category_entity cce
1126
+ ON cmt.shop_entity_id = cce.entity_id
1127
+ SET cce.parent_id = cmt.shop_parent_id");
1128
+ } else {
1129
+ $catalog_category_entity_backup = Mage::getSingleton('core/resource')->getTableName('sinch_category_backup');
1130
+ if (!$this->_checkDataExist($catalog_category_entity_backup)) {
1131
+ $catalog_category_entity_backup = $catalog_category_entity;
1132
+ }
1133
+
1134
+ $this->db_do("
1135
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
1136
+ (
1137
+ shop_entity_id,
1138
+ shop_entity_type_id,
1139
+ shop_attribute_set_id,
1140
+ shop_parent_id,
1141
+ shop_store_category_id,
1142
+ shop_parent_store_category_id
1143
+ )
1144
+ (SELECT
1145
+ entity_id,
1146
+ entity_type_id,
1147
+ attribute_set_id,
1148
+ parent_id,
1149
+ store_category_id,
1150
+ parent_store_category_id
1151
+ FROM $catalog_category_entity_backup)");
1152
+
1153
+ $this->db_do("
1154
+ UPDATE $stINch_categories_mapping_temp cmt
1155
+ JOIN $categories_temp c
1156
+ ON cmt.shop_store_category_id = c.store_category_id
1157
+ SET
1158
+ cmt.store_category_id = c.store_category_id,
1159
+ cmt.parent_store_category_id = c.parent_store_category_id,
1160
+ cmt.category_name = c.category_name,
1161
+ cmt.order_number = c.order_number,
1162
+ cmt.products_within_this_category = c.products_within_this_category");
1163
+
1164
+ $this->db_do("
1165
+ UPDATE $stINch_categories_mapping_temp cmt
1166
+ JOIN $catalog_category_entity_backup cce
1167
+ ON cmt.parent_store_category_id = cce.store_category_id
1168
+ SET cmt.shop_parent_id = cce.entity_id");
1169
+
1170
+ $this->db_do("
1171
+ UPDATE $stINch_categories_mapping_temp cmt
1172
+ JOIN $categories_temp c
1173
+ ON cmt.shop_store_category_id = c.store_category_id
1174
+ SET shop_parent_id = " . $this->_root_cat . "
1175
+ WHERE shop_parent_id = 0");
1176
+
1177
+ $this->db_do("
1178
+ UPDATE $stINch_categories_mapping_temp cmt
1179
+ JOIN $catalog_category_entity cce
1180
+ ON cmt.shop_entity_id = cce.entity_id
1181
+ SET cce.parent_id = cmt.shop_parent_id");
1182
+ }
1183
+ // (end) backup Category ID in REWRITE mode
1184
+ } else {
1185
+ $this->db_do("
1186
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
1187
+ (
1188
+ shop_entity_id,
1189
+ shop_entity_type_id,
1190
+ shop_attribute_set_id,
1191
+ shop_parent_id,
1192
+ shop_store_category_id,
1193
+ shop_parent_store_category_id
1194
+ )
1195
+ (SELECT
1196
+ entity_id,
1197
+ entity_type_id,
1198
+ attribute_set_id,
1199
+ parent_id,
1200
+ store_category_id,
1201
+ parent_store_category_id
1202
+ FROM $catalog_category_entity)");
1203
+
1204
+ $this->db_do("
1205
+ UPDATE $stINch_categories_mapping_temp cmt
1206
+ JOIN $categories_temp c
1207
+ ON cmt.shop_store_category_id = c.store_category_id
1208
+ SET
1209
+ cmt.store_category_id = c.store_category_id,
1210
+ cmt.parent_store_category_id = c.parent_store_category_id,
1211
+ cmt.category_name = c.category_name,
1212
+ cmt.order_number = c.order_number,
1213
+ cmt.products_within_this_category = c.products_within_this_category");
1214
+
1215
+ $this->db_do("
1216
+ UPDATE $stINch_categories_mapping_temp cmt
1217
+ JOIN $catalog_category_entity cce
1218
+ ON cmt.parent_store_category_id = cce.store_category_id
1219
+ SET cmt.shop_parent_id = cce.entity_id");
1220
+
1221
+ $this->db_do("
1222
+ UPDATE $stINch_categories_mapping_temp cmt
1223
+ JOIN $categories_temp c
1224
+ ON cmt.shop_store_category_id = c.store_category_id
1225
+ SET shop_parent_id = " . $this->_root_cat . "
1226
+ WHERE shop_parent_id = 0");
1227
+
1228
+ $this->db_do("
1229
+ UPDATE $stINch_categories_mapping_temp cmt
1230
+ JOIN $catalog_category_entity cce
1231
+ ON cmt.shop_entity_id = cce.entity_id
1232
+ SET cce.parent_id = cmt.shop_parent_id
1233
+ WHERE cce.parent_id = 0 AND cce.store_category_id IS NOT NULL");
1234
+ }
1235
+ $this->_LOG("Execute function mapSinchCategories");
1236
+ $this->db_do("DROP TABLE IF EXISTS $stINch_categories_mapping");
1237
+ $this->db_do("RENAME TABLE $stINch_categories_mapping_temp TO $stINch_categories_mapping");
1238
+ }
1239
+
1240
+ private function addCategoryData($categories_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1241
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid, $im_type, $root_cat)
1242
+ {
1243
+ if (UPDATE_CATEGORY_DATA) {
1244
+ echo "Update category_entity \n";
1245
+
1246
+ $q = "
1247
+ INSERT INTO $catalog_category_entity
1248
+ (
1249
+ entity_type_id,
1250
+ attribute_set_id,
1251
+ created_at,
1252
+ updated_at,
1253
+ level,
1254
+ children_count,
1255
+ entity_id,
1256
+ position,
1257
+ parent_id,
1258
+ store_category_id,
1259
+ parent_store_category_id
1260
+ )
1261
+ (SELECT
1262
+ $_categoryEntityTypeId,
1263
+ $_categoryDefault_attribute_set_id,
1264
+ now(),
1265
+ now(),
1266
+ c.level,
1267
+ c.children_count,
1268
+ scm.shop_entity_id,
1269
+ c.order_number,
1270
+ scm.shop_parent_id,
1271
+ c.store_category_id,
1272
+ c.parent_store_category_id
1273
+ FROM $categories_temp c
1274
+ LEFT JOIN $stINch_categories_mapping scm
1275
+ ON c.store_category_id = scm.store_category_id
1276
+ )
1277
+ ON DUPLICATE KEY UPDATE
1278
+ updated_at = now(),
1279
+ store_category_id = c.store_category_id,
1280
+ level = c.level,
1281
+ children_count = c.children_count,
1282
+ position = c.order_number,
1283
+ parent_store_category_id = c.parent_store_category_id";
1284
+ } else {
1285
+ echo "Insert ignore category_entity \n";
1286
+
1287
+ $q = "
1288
+ INSERT IGNORE INTO $catalog_category_entity
1289
+ (
1290
+ entity_type_id,
1291
+ attribute_set_id,
1292
+ created_at,
1293
+ updated_at,
1294
+ level,
1295
+ children_count,
1296
+ entity_id,
1297
+ position,
1298
+ parent_id,
1299
+ store_category_id,
1300
+ parent_store_category_id
1301
+ )
1302
+ (SELECT
1303
+ $_categoryEntityTypeId,
1304
+ $_categoryDefault_attribute_set_id,
1305
+ now(),
1306
+ now(),
1307
+ c.level,
1308
+ c.children_count,
1309
+ scm.shop_entity_id,
1310
+ c.order_number,
1311
+ scm.shop_parent_id,
1312
+ c.store_category_id,
1313
+ c.parent_store_category_id
1314
+ FROM $categories_temp c
1315
+ LEFT JOIN $stINch_categories_mapping scm
1316
+ ON c.store_category_id = scm.store_category_id
1317
+ )";
1318
+ }
1319
+ $this->db_do($q);
1320
+
1321
+ $this->mapSinchCategories($stINch_categories_mapping, $catalog_category_entity, $categories_temp, $im_type, $root_cat, true);
1322
+
1323
+
1324
+ $categories = $this->db_do("SELECT entity_id, parent_id FROM $catalog_category_entity ORDER BY parent_id");
1325
+ while ($row = mysqli_fetch_array($categories)) {
1326
+ $parent_id = $row['parent_id'];
1327
+ $entity_id = $row['entity_id'];
1328
+
1329
+ $path = $this->culc_path($parent_id, $entity_id);
1330
+
1331
+ $this->db_do("
1332
+ UPDATE $catalog_category_entity
1333
+ SET path = '$path'
1334
+ WHERE entity_id = $entity_id");
1335
+ }
1336
+
1337
+ if (UPDATE_CATEGORY_DATA) {
1338
+ echo "Update category_data \n";
1339
+
1340
+ $q = "
1341
+ INSERT INTO $catalog_category_entity_varchar
1342
+ (
1343
+ entity_type_id,
1344
+ attribute_id,
1345
+ store_id,
1346
+ entity_id,
1347
+ value
1348
+ )
1349
+ (SELECT
1350
+ $_categoryEntityTypeId,
1351
+ $name_attrid,
1352
+ 0,
1353
+ scm.shop_entity_id,
1354
+ c.category_name
1355
+ FROM $categories_temp c
1356
+ JOIN $stINch_categories_mapping scm
1357
+ ON c.store_category_id = scm.store_category_id
1358
+ )
1359
+ ON DUPLICATE KEY UPDATE
1360
+ value = c.category_name";
1361
+ $this->db_do($q);
1362
+
1363
+ $q = "
1364
+ INSERT INTO $catalog_category_entity_varchar
1365
+ (
1366
+ entity_type_id,
1367
+ attribute_id,
1368
+ store_id,
1369
+ entity_id,
1370
+ value
1371
+ )
1372
+ (SELECT
1373
+ $_categoryEntityTypeId,
1374
+ $name_attrid,
1375
+ 1,
1376
+ scm.shop_entity_id,
1377
+ c.category_name
1378
+ FROM $categories_temp c
1379
+ JOIN $stINch_categories_mapping scm
1380
+ ON c.store_category_id = scm.store_category_id
1381
+ )
1382
+ ON DUPLICATE KEY UPDATE
1383
+ value = c.category_name";
1384
+ $this->db_do($q);
1385
+
1386
+ $q = "
1387
+ INSERT INTO $catalog_category_entity
1388
+ (
1389
+ entity_type_id,
1390
+ attribute_id,
1391
+ store_id,
1392
+ entity_id,
1393
+ value
1394
+ )
1395
+ (SELECT
1396
+ $_categoryEntityTypeId,
1397
+ $attr_is_active,
1398
+ 0,
1399
+ scm.shop_entity_id,
1400
+ 1
1401
+ FROM $categories_temp c
1402
+ JOIN $stINch_categories_mapping scm
1403
+ ON c.store_category_id = scm.store_category_id
1404
+ )
1405
+ ON DUPLICATE KEY UPDATE
1406
+ value = 1";
1407
+ $this->db_do($q);
1408
+
1409
+ $q = "
1410
+ INSERT INTO $catalog_category_entity_int
1411
+ (
1412
+ entity_type_id,
1413
+ attribute_id,
1414
+ store_id,
1415
+ entity_id,
1416
+ value
1417
+ )
1418
+ (SELECT
1419
+ $_categoryEntityTypeId,
1420
+ $attr_is_active,
1421
+ 1,
1422
+ scm.shop_entity_id,
1423
+ 1
1424
+ FROM $categories_temp c
1425
+ JOIN $stINch_categories_mapping scm
1426
+ ON c.store_category_id = scm.store_category_id
1427
+ )
1428
+ ON DUPLICATE KEY UPDATE
1429
+ value = 1";
1430
+ $this->db_do($q);
1431
+
1432
+ $q = "
1433
+ INSERT INTO $catalog_category_entity_int
1434
+ (
1435
+ entity_type_id,
1436
+ attribute_id,
1437
+ store_id,
1438
+ entity_id,
1439
+ value
1440
+ )
1441
+ (SELECT
1442
+ $_categoryEntityTypeId,
1443
+ $attr_include_in_menu,
1444
+ 0,
1445
+ scm.shop_entity_id,
1446
+ c.include_in_menu
1447
+ FROM $categories_temp c
1448
+ JOIN $stINch_categories_mapping scm
1449
+ ON c.store_category_id = scm.store_category_id
1450
+ )
1451
+ ON DUPLICATE KEY UPDATE
1452
+ value = c.include_in_menu";
1453
+ $this->db_do($q);
1454
+
1455
+ $q = "
1456
+ INSERT INTO $catalog_category_entity_int
1457
+ (
1458
+ entity_type_id,
1459
+ attribute_id,
1460
+ store_id,
1461
+ entity_id,
1462
+ value
1463
+ )
1464
+ (SELECT
1465
+ $_categoryEntityTypeId,
1466
+ $is_anchor_attrid,
1467
+ 1,
1468
+ scm.shop_entity_id,
1469
+ c.is_anchor
1470
+ FROM $categories_temp c
1471
+ JOIN $stINch_categories_mapping scm
1472
+ ON c.store_category_id = scm.store_category_id
1473
+ )
1474
+ ON DUPLICATE KEY UPDATE
1475
+ value = c.is_anchor";
1476
+ $this->db_do($q);
1477
+
1478
+ $q = "
1479
+ INSERT INTO $catalog_category_entity_int
1480
+ (
1481
+ entity_type_id,
1482
+ attribute_id,
1483
+ store_id,
1484
+ entity_id,
1485
+ value
1486
+ )
1487
+ (SELECT
1488
+ $_categoryEntityTypeId,
1489
+ $is_anchor_attrid,
1490
+ 0,
1491
+ scm.shop_entity_id,
1492
+ c.is_anchor
1493
+ FROM $categories_temp c
1494
+ JOIN $stINch_categories_mapping scm
1495
+ ON c.store_category_id = scm.store_category_id
1496
+ )
1497
+ ON DUPLICATE KEY UPDATE
1498
+ value = c.is_anchor";
1499
+ $this->db_do($q);
1500
+
1501
+ $q = "
1502
+ INSERT INTO $catalog_category_entity_varchar
1503
+ (
1504
+ entity_type_id,
1505
+ attribute_id,
1506
+ store_id,
1507
+ entity_id,
1508
+ value
1509
+ )
1510
+ (SELECT
1511
+ $_categoryEntityTypeId,
1512
+ $image_attrid,
1513
+ 0,
1514
+ scm.shop_entity_id,
1515
+ c.categories_image
1516
+ FROM $categories_temp c
1517
+ JOIN $stINch_categories_mapping scm
1518
+ ON c.store_category_id = scm.store_category_id
1519
+ )
1520
+ ON DUPLICATE KEY UPDATE
1521
+ value = c.categories_image";
1522
+ $this->db_do($q);
1523
+
1524
+ $q = "
1525
+ INSERT INTO $catalog_category_entity_varchar
1526
+ (
1527
+ entity_type_id,
1528
+ attribute_id,
1529
+ store_id,
1530
+ entity_id,
1531
+ value
1532
+ )
1533
+ (SELECT
1534
+ $this->_categoryEntityTypeId,
1535
+ $this->_categoryMetaTitleAttrId,
1536
+ 0,
1537
+ scm.shop_entity_id,
1538
+ c.MetaTitle
1539
+ FROM $categories_temp c
1540
+ JOIN $stINch_categories_mapping scm
1541
+ ON c.store_category_id = scm.store_category_id
1542
+ )
1543
+ ON DUPLICATE KEY UPDATE
1544
+ value = c.MetaTitle";
1545
+ $this->db_do($q);
1546
+
1547
+ $q = "
1548
+ INSERT INTO $catalog_category_entity_varchar
1549
+ (
1550
+ entity_type_id,
1551
+ attribute_id,
1552
+ store_id,
1553
+ entity_id,
1554
+ value
1555
+ )
1556
+ (SELECT
1557
+ $this->_categoryEntityTypeId,
1558
+ $this->_categoryMetadescriptionAttrId,
1559
+ 0,
1560
+ scm.shop_entity_id,
1561
+ c.MetaDescription
1562
+ FROM $categories_temp c
1563
+ JOIN $stINch_categories_mapping scm
1564
+ ON c.store_category_id = scm.store_category_id
1565
+ )
1566
+ ON DUPLICATE KEY UPDATE
1567
+ value = c.MetaDescription";
1568
+ $this->db_do($q);
1569
+
1570
+ $q = "
1571
+ INSERT INTO $catalog_category_entity_varchar
1572
+ (
1573
+ entity_type_id,
1574
+ attribute_id,
1575
+ store_id,
1576
+ entity_id,
1577
+ value
1578
+ )
1579
+ (SELECT
1580
+ $this->_categoryEntityTypeId,
1581
+ $this->_categoryDescriptionAttrId,
1582
+ 0,
1583
+ scm.shop_entity_id,
1584
+ c.Description
1585
+ FROM $categories_temp c
1586
+ JOIN $stINch_categories_mapping scm
1587
+ ON c.store_category_id = scm.store_category_id
1588
+ )
1589
+ ON DUPLICATE KEY UPDATE
1590
+ value = c.Description";
1591
+ $this->db_do($q);
1592
+ } else {
1593
+ echo "Insert ignore category_data \n";
1594
+
1595
+ $q = "
1596
+ INSERT IGNORE INTO $catalog_category_entity_varchar
1597
+ (
1598
+ entity_type_id,
1599
+ attribute_id,
1600
+ store_id,
1601
+ entity_id,
1602
+ value
1603
+ )
1604
+ (SELECT
1605
+ $_categoryEntityTypeId,
1606
+ $name_attrid,
1607
+ 0,
1608
+ scm.shop_entity_id,
1609
+ c.category_name
1610
+ FROM $categories_temp c
1611
+ JOIN $stINch_categories_mapping scm
1612
+ ON c.store_category_id = scm.store_category_id
1613
+ )";
1614
+ $this->db_do($q);
1615
+
1616
+ $q = "
1617
+ INSERT IGNORE INTO $catalog_category_entity_int
1618
+ (
1619
+ entity_type_id,
1620
+ attribute_id,
1621
+ store_id,
1622
+ entity_id,
1623
+ value
1624
+ )
1625
+ (SELECT
1626
+ $_categoryEntityTypeId,
1627
+ $attr_is_active,
1628
+ 0,
1629
+ scm.shop_entity_id,
1630
+ 1
1631
+ FROM $categories_temp c
1632
+ JOIN $stINch_categories_mapping scm
1633
+ ON c.store_category_id = scm.store_category_id
1634
+ )";
1635
+ $this->db_do($q);
1636
+
1637
+ $q = "
1638
+ INSERT IGNORE INTO $catalog_category_entity_int
1639
+ (
1640
+ entity_type_id,
1641
+ attribute_id,
1642
+ store_id,
1643
+ entity_id,
1644
+ value
1645
+ )
1646
+ (SELECT
1647
+ $_categoryEntityTypeId,
1648
+ $attr_include_in_menu,
1649
+ 0,
1650
+ scm.shop_entity_id,
1651
+ c.include_in_menu
1652
+ FROM $categories_temp c
1653
+ JOIN $stINch_categories_mapping scm
1654
+ ON c.store_category_id = scm.store_category_id
1655
+ )";
1656
+ $this->db_do($q);
1657
+
1658
+ $q = "
1659
+ INSERT IGNORE INTO $catalog_category_entity_int
1660
+ (
1661
+ entity_type_id,
1662
+ attribute_id,
1663
+ store_id,
1664
+ entity_id,
1665
+ value
1666
+ )
1667
+ (SELECT
1668
+ $_categoryEntityTypeId,
1669
+ $is_anchor_attrid,
1670
+ 0,
1671
+ scm.shop_entity_id,
1672
+ c.is_anchor
1673
+ FROM $categories_temp c
1674
+ JOIN $stINch_categories_mapping scm
1675
+ ON c.store_category_id = scm.store_category_id
1676
+ )";
1677
+ $this->db_do($q);
1678
+
1679
+ $q = "
1680
+ INSERT IGNORE INTO $catalog_category_entity_varchar
1681
+ (
1682
+ entity_type_id,
1683
+ attribute_id,
1684
+ store_id,
1685
+ entity_id,
1686
+ value
1687
+ )
1688
+ (SELECT
1689
+ $_categoryEntityTypeId,
1690
+ $image_attrid,
1691
+ 0,
1692
+ scm.shop_entity_id,
1693
+ c.categories_image
1694
+ FROM $categories_temp c
1695
+ JOIN $stINch_categories_mapping scm
1696
+ ON c.store_category_id = scm.store_category_id
1697
+ )";
1698
+ $this->db_do($q);
1699
+ //STP
1700
+ $q = "
1701
+ INSERT IGNORE INTO $catalog_category_entity_varchar
1702
+ (
1703
+ entity_type_id,
1704
+ attribute_id,
1705
+ store_id,
1706
+ entity_id,
1707
+ value
1708
+ )
1709
+ (SELECT
1710
+ $this->_categoryEntityTypeId,
1711
+ $this->_categoryMetaTitleAttrId,
1712
+ 0,
1713
+ scm.shop_entity_id,
1714
+ c.MetaTitle
1715
+ FROM $categories_temp c
1716
+ JOIN $stINch_categories_mapping scm
1717
+ ON c.store_category_id = scm.store_category_id
1718
+ )
1719
+ ";
1720
+ $this->db_do($q);
1721
+
1722
+ $q = "
1723
+ INSERT IGNORE INTO $catalog_category_entity_varchar
1724
+ (
1725
+ entity_type_id,
1726
+ attribute_id,
1727
+ store_id,
1728
+ entity_id,
1729
+ value
1730
+ )
1731
+ (SELECT
1732
+ $this->_categoryEntityTypeId,
1733
+ $this->_categoryMetadescriptionAttrId,
1734
+ 0,
1735
+ scm.shop_entity_id,
1736
+ c.MetaDescription
1737
+ FROM $categories_temp c
1738
+ JOIN $stINch_categories_mapping scm
1739
+ ON c.store_category_id = scm.store_category_id
1740
+ )
1741
+ ";
1742
+ $this->db_do($q);
1743
+
1744
+ $q = "
1745
+ INSERT IGNORE INTO $catalog_category_entity_varchar
1746
+ (
1747
+ entity_type_id,
1748
+ attribute_id,
1749
+ store_id,
1750
+ entity_id,
1751
+ value
1752
+ )
1753
+ (SELECT
1754
+ $this->_categoryEntityTypeId,
1755
+ $this->_categoryDescriptionAttrId,
1756
+ 0,
1757
+ scm.shop_entity_id,
1758
+ c.Description
1759
+ FROM $categories_temp c
1760
+ JOIN $stINch_categories_mapping scm
1761
+ ON c.store_category_id = scm.store_category_id
1762
+ )
1763
+ ";
1764
+ $this->db_do($q);
1765
+ }
1766
+
1767
+ $this->delete_old_sinch_categories_from_shop();
1768
+
1769
+ $this->db_do("DROP TABLE IF EXISTS $stINch_categories");
1770
+ $this->db_do("RENAME TABLE $categories_temp TO $stINch_categories");
1771
+ }
1772
+
1773
+ function culc_path($parent_id, $ent_id)
1774
+ {
1775
+ $path = '';
1776
+ $cat_id = $parent_id;
1777
+ $q = "SELECT
1778
+ parent_id
1779
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
1780
+ WHERE entity_id=" . $cat_id;
1781
+ $quer = $this->db_do($q);
1782
+ $row = mysqli_fetch_array($quer);
1783
+ while ($row['parent_id']) {
1784
+ $path = $row['parent_id'] . '/' . $path;
1785
+ $q = "SELECT
1786
+ parent_id
1787
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . "
1788
+ WHERE entity_id=" . $row['parent_id'];
1789
+ $quer = $this->db_do($q);
1790
+ $row = mysqli_fetch_array($quer);
1791
+
1792
+ }
1793
+ if ($cat_id) {
1794
+ $path .= $cat_id . "/";
1795
+ }
1796
+
1797
+ if ($path) {
1798
+ return ($path . $ent_id);
1799
+ } else {
1800
+ return ($ent_id);
1801
+ }
1802
+ }
1803
+
1804
+ private function delete_old_sinch_categories_from_shop()
1805
+ {
1806
+ $q = "DELETE cat FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar') . " cat
1807
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
1808
+ ON cat.entity_id=scm.shop_entity_id
1809
+ WHERE
1810
+ (scm.shop_store_category_id is not null) AND
1811
+ (scm.store_category_id is null)";
1812
+ $this->db_do($q);
1813
+
1814
+ $q = "DELETE cat FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_int') . " cat
1815
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
1816
+ ON cat.entity_id=scm.shop_entity_id
1817
+ WHERE
1818
+ (scm.shop_store_category_id is not null) AND
1819
+ (scm.store_category_id is null)";
1820
+ $this->db_do($q);
1821
+
1822
+ $q = "DELETE cat FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cat
1823
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
1824
+ ON cat.entity_id=scm.shop_entity_id
1825
+ WHERE
1826
+ (scm.shop_store_category_id is not null) AND
1827
+ (scm.store_category_id is null)";
1828
+ $this->db_do($q);
1829
+ }
1830
+
1831
+
1832
+ private function rewriteMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1833
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
1834
+ $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
1835
+ $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp)
1836
+ {
1837
+ echo("rewriteMultistoreCategories RUN\n");
1838
+
1839
+ echo(" truncateAllCateriesAndCreateRoot start...");
1840
+ $this->truncateAllCateriesAndCreateRoot($catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1841
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active);
1842
+ echo(" done.\n");
1843
+
1844
+ echo(" createDefaultCategories start...");
1845
+ $this->createDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1846
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu);
1847
+ echo(" done.\n");
1848
+
1849
+ echo(" mapSinchCategoriesMultistore start...");
1850
+ $this->mapSinchCategoriesMultistore($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid);
1851
+ echo(" done.\n");
1852
+
1853
+ echo(" addCategoryDataMultistore start...");
1854
+ $this->addCategoryDataMultistore($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1855
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
1856
+ $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid);
1857
+ echo(" done.\n");
1858
+
1859
+ echo("rewriteMultistoreCategories DONE\n");
1860
+ }
1861
+
1862
+
1863
+ private function truncateAllCateriesAndCreateRoot($catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1864
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active)
1865
+ {
1866
+ $this->db_do('SET foreign_key_checks=0');
1867
+
1868
+
1869
+ $this->db_do("TRUNCATE $catalog_category_entity");
1870
+ $this->db_do("TRUNCATE $catalog_category_entity_varchar");
1871
+ $this->db_do("TRUNCATE $catalog_category_entity_int");
1872
+
1873
+
1874
+ $this->db_do("INSERT $catalog_category_entity
1875
+ (entity_id, entity_type_id, attribute_set_id, parent_id, created_at, updated_at,
1876
+ path, position, level, children_count, store_category_id, parent_store_category_id)
1877
+ VALUES
1878
+ (1, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 0, '0000-00-00 00:00:00', NOW(), '1', 0, 0, 1, NULL, NULL)");
1879
+
1880
+
1881
+ $this->db_do("INSERT $catalog_category_entity_varchar
1882
+ (value_id, entity_type_id, attribute_id, store_id, entity_id, value)
1883
+ VALUES
1884
+ (1, $_categoryEntityTypeId, $name_attrid, 0, 1, 'Root Catalog'),
1885
+ (2, $_categoryEntityTypeId, $name_attrid, 1, 1, 'Root Catalog'),
1886
+ (3, $_categoryEntityTypeId, $attr_url_key, 0, 1, 'root-catalog')");
1887
+
1888
+
1889
+ $this->db_do("INSERT $catalog_category_entity_int
1890
+ (value_id, entity_type_id, attribute_id, store_id, entity_id, value)
1891
+ VALUES
1892
+ (1, $_categoryEntityTypeId, $attr_include_in_menu, 0, 1, 1)");
1893
+ }
1894
+
1895
+ private function createDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
1896
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu)
1897
+ {
1898
+ $i = 3;
1899
+
1900
+ foreach ($coincidence as $key => $item) {
1901
+ $this->db_do("INSERT $catalog_category_entity
1902
+ (entity_id, entity_type_id, attribute_set_id, parent_id, created_at, updated_at,
1903
+ path, position, level, children_count, store_category_id, parent_store_category_id)
1904
+ VALUES
1905
+ ($i, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 1, now(), now(), '1/$i', 1, 1, 1, NULL, NULL)");
1906
+
1907
+
1908
+ $this->db_do("INSERT $catalog_category_entity_varchar
1909
+ (entity_type_id, attribute_id, store_id, entity_id, value)
1910
+ VALUES
1911
+ ($_categoryEntityTypeId, $name_attrid, 0, $i, '$key'),
1912
+ ($_categoryEntityTypeId, $name_attrid, 1, $i, '$key'),
1913
+ ($_categoryEntityTypeId, $attr_display_mode, 1, $i, '$key'),
1914
+ ($_categoryEntityTypeId, $attr_url_key, 0, $i, '$key')");
1915
+
1916
+
1917
+ $this->db_do("INSERT $catalog_category_entity_int
1918
+ (entity_type_id, attribute_id, store_id, entity_id, value)
1919
+ VALUES
1920
+ ($_categoryEntityTypeId, $attr_is_active, 0, $i, 1),
1921
+ ($_categoryEntityTypeId, $attr_is_active, 1, $i, 1),
1922
+ ($_categoryEntityTypeId, $attr_include_in_menu, 0, $i, 1),
1923
+ ($_categoryEntityTypeId, $attr_include_in_menu, 1, $i, 1)");
1924
+ $i++;
1925
+ }
1926
+ }
1927
+
1928
+ private function mapSinchCategoriesMultistore($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid, $mapping_again = false)
1929
+ {
1930
+ echo("\n\n\n\n==========================================================================\nmapSinchCategoriesMultistore start... \n");
1931
+
1932
+ $this->createMappingSinchTables($stINch_categories_mapping_temp, $stINch_categories_mapping);
1933
+
1934
+ // backup Category ID in REWRITE mode
1935
+ if ($im_type == "REWRITE" || (UPDATE_CATEGORY_DATA && $im_type == "MERGE")) {
1936
+ if ($mapping_again) {
1937
+ $query = "
1938
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
1939
+ (
1940
+ shop_entity_id,
1941
+ shop_entity_type_id,
1942
+ shop_attribute_set_id,
1943
+ shop_parent_id,
1944
+ shop_store_category_id,
1945
+ shop_parent_store_category_id
1946
+ )
1947
+ (SELECT
1948
+ entity_id,
1949
+ entity_type_id,
1950
+ attribute_set_id,
1951
+ parent_id,
1952
+ store_category_id,
1953
+ parent_store_category_id
1954
+ FROM $catalog_category_entity)";
1955
+ echo("\n\n$query\n\n");
1956
+ $this->db_do($query);
1957
+
1958
+
1959
+ $query = "
1960
+ UPDATE $stINch_categories_mapping_temp cmt
1961
+ JOIN $categories_temp c
1962
+ ON cmt.shop_store_category_id = c.store_category_id
1963
+ SET
1964
+ cmt.store_category_id = c.store_category_id,
1965
+ cmt.parent_store_category_id = c.parent_store_category_id,
1966
+ cmt.category_name = c.category_name,
1967
+ cmt.order_number = c.order_number,
1968
+ cmt.products_within_this_category = c.products_within_this_category";
1969
+ echo("\n\n$query\n\n");
1970
+ $this->db_do($query);
1971
+
1972
+
1973
+ $query = "
1974
+ UPDATE $stINch_categories_mapping_temp cmt
1975
+ JOIN $catalog_category_entity cce
1976
+ ON cmt.parent_store_category_id = cce.store_category_id
1977
+ SET cmt.shop_parent_id = cce.entity_id";
1978
+ echo("\n\n$query\n\n");
1979
+ $this->db_do($query);
1980
+
1981
+
1982
+ $query = "
1983
+ SELECT DISTINCT
1984
+ c.RootName, cce.entity_id
1985
+ FROM $categories_temp c
1986
+ JOIN $catalog_category_entity_varchar ccev
1987
+ ON c.RootName = ccev.value
1988
+ AND ccev.entity_type_id = $_categoryEntityTypeId
1989
+ AND ccev.attribute_id = $name_attrid
1990
+ AND ccev.store_id = 0
1991
+ JOIN $catalog_category_entity cce
1992
+ ON ccev.entity_id = cce.entity_id";
1993
+ echo("\n\n$query\n\n");
1994
+ $root_categories = $this->db_do($query);
1995
+
1996
+ while ($root_cat = mysqli_fetch_array($root_categories)) {
1997
+ $root_id = $root_cat['entity_id'];
1998
+ $root_name = $root_cat['RootName'];
1999
+
2000
+ $query = "
2001
+ UPDATE $stINch_categories_mapping_temp cmt
2002
+ JOIN $categories_temp c
2003
+ ON cmt.shop_store_category_id = c.store_category_id
2004
+ SET
2005
+ cmt.shop_parent_id = $root_id,
2006
+ cmt.shop_parent_store_category_id = $root_id,
2007
+ cmt.parent_store_category_id = $root_id,
2008
+ c.parent_store_category_id = $root_id
2009
+ WHERE RootName = '$root_name'
2010
+ AND cmt.shop_parent_id = 0";
2011
+ echo("\n\n$query\n\n");
2012
+ $this->db_do($query);
2013
+ }
2014
+ } else {
2015
+ $catalog_category_entity_backup = Mage::getSingleton('core/resource')->getTableName('sinch_category_backup');
2016
+ if (!$this->_checkDataExist($catalog_category_entity_backup)) {
2017
+ $catalog_category_entity_backup = $catalog_category_entity;
2018
+ }
2019
+ $query = "
2020
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
2021
+ (
2022
+ shop_entity_id,
2023
+ shop_entity_type_id,
2024
+ shop_attribute_set_id,
2025
+ shop_parent_id,
2026
+ shop_store_category_id,
2027
+ shop_parent_store_category_id
2028
+ )
2029
+ (SELECT
2030
+ entity_id,
2031
+ entity_type_id,
2032
+ attribute_set_id,
2033
+ parent_id,
2034
+ store_category_id,
2035
+ parent_store_category_id
2036
+ FROM $catalog_category_entity_backup)";
2037
+ echo("\n\n$query\n\n");
2038
+ $this->db_do($query);
2039
+
2040
+
2041
+ $query = "
2042
+ UPDATE $stINch_categories_mapping_temp cmt
2043
+ JOIN $categories_temp c
2044
+ ON cmt.shop_store_category_id = c.store_category_id
2045
+ SET
2046
+ cmt.store_category_id = c.store_category_id,
2047
+ cmt.parent_store_category_id = c.parent_store_category_id,
2048
+ cmt.category_name = c.category_name,
2049
+ cmt.order_number = c.order_number,
2050
+ cmt.products_within_this_category = c.products_within_this_category";
2051
+ echo("\n\n$query\n\n");
2052
+ $this->db_do($query);
2053
+
2054
+
2055
+ $query = "
2056
+ UPDATE $stINch_categories_mapping_temp cmt
2057
+ JOIN $catalog_category_entity_backup cce
2058
+ ON cmt.parent_store_category_id = cce.store_category_id
2059
+ SET cmt.shop_parent_id = cce.entity_id";
2060
+ echo("\n\n$query\n\n");
2061
+ $this->db_do($query);
2062
+
2063
+
2064
+ $query = "
2065
+ SELECT DISTINCT
2066
+ c.RootName, cce.entity_id
2067
+ FROM $categories_temp c
2068
+ JOIN $catalog_category_entity_varchar ccev
2069
+ ON c.RootName = ccev.value
2070
+ AND ccev.entity_type_id = $_categoryEntityTypeId
2071
+ AND ccev.attribute_id = $name_attrid
2072
+ AND ccev.store_id = 0
2073
+ JOIN $catalog_category_entity_backup cce
2074
+ ON ccev.entity_id = cce.entity_id";
2075
+ echo("\n\n$query\n\n");
2076
+ $root_categories = $this->db_do($query);
2077
+
2078
+ while ($root_cat = mysqli_fetch_array($root_categories)) {
2079
+ $root_id = $root_cat['entity_id'];
2080
+ $root_name = $root_cat['RootName'];
2081
+
2082
+ $query = "
2083
+ UPDATE $stINch_categories_mapping_temp cmt
2084
+ JOIN $categories_temp c
2085
+ ON cmt.shop_store_category_id = c.store_category_id
2086
+ SET
2087
+ cmt.shop_parent_id = $root_id,
2088
+ cmt.shop_parent_store_category_id = $root_id,
2089
+ cmt.parent_store_category_id = $root_id,
2090
+ c.parent_store_category_id = $root_id
2091
+ WHERE RootName = '$root_name'
2092
+ AND cmt.shop_parent_id = 0";
2093
+ echo("\n\n$query\n\n");
2094
+ $this->db_do($query);
2095
+ }
2096
+ }
2097
+ // (end) backup Category ID in REWRITE mode
2098
+ } else {
2099
+ $query = "
2100
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
2101
+ (shop_entity_id, shop_entity_type_id, shop_attribute_set_id, shop_parent_id, shop_store_category_id, shop_parent_store_category_id)
2102
+ (SELECT entity_id, entity_type_id, attribute_set_id, parent_id, store_category_id, parent_store_category_id
2103
+ FROM $catalog_category_entity)";
2104
+ $this->db_do($query);
2105
+
2106
+ $query = "
2107
+ UPDATE $stINch_categories_mapping_temp cmt
2108
+ JOIN $categories_temp c
2109
+ ON cmt.shop_store_category_id = c.store_category_id
2110
+ SET
2111
+ cmt.store_category_id = c.store_category_id,
2112
+ cmt.parent_store_category_id = c.parent_store_category_id,
2113
+ cmt.category_name = c.category_name,
2114
+ cmt.order_number = c.order_number,
2115
+ cmt.products_within_this_category = c.products_within_this_category";
2116
+ echo("\n\n$query\n\n");
2117
+ $this->db_do($query);
2118
+
2119
+
2120
+ $query = "
2121
+ UPDATE $stINch_categories_mapping_temp cmt
2122
+ JOIN $catalog_category_entity cce
2123
+ ON cmt.parent_store_category_id = cce.store_category_id
2124
+ SET cmt.shop_parent_id = cce.entity_id";
2125
+ echo("\n\n$query\n\n");
2126
+ $this->db_do($query);
2127
+
2128
+
2129
+ $query = "
2130
+ SELECT DISTINCT
2131
+ c.RootName, cce.entity_id
2132
+ FROM $categories_temp c
2133
+ JOIN $catalog_category_entity_varchar ccev
2134
+ ON c.RootName = ccev.value
2135
+ AND ccev.entity_type_id = $_categoryEntityTypeId
2136
+ AND ccev.attribute_id = $name_attrid
2137
+ AND ccev.store_id = 0
2138
+ JOIN $catalog_category_entity cce
2139
+ ON ccev.entity_id = cce.entity_id";
2140
+ echo("\n\n$query\n\n");
2141
+ $root_categories = $this->db_do($query);
2142
+
2143
+ while ($root_cat = mysqli_fetch_array($root_categories)) {
2144
+ $root_id = $root_cat['entity_id'];
2145
+ $root_name = $root_cat['RootName'];
2146
+
2147
+ $query = "
2148
+ UPDATE $stINch_categories_mapping_temp cmt
2149
+ JOIN $categories_temp c
2150
+ ON cmt.shop_store_category_id = c.store_category_id
2151
+ SET
2152
+ cmt.shop_parent_id = $root_id,
2153
+ cmt.shop_parent_store_category_id = $root_id,
2154
+ cmt.parent_store_category_id = $root_id,
2155
+ c.parent_store_category_id = $root_id
2156
+ WHERE RootName = '$root_name'
2157
+ AND cmt.shop_parent_id = 0";
2158
+ echo("\n\n$query\n\n");
2159
+ $this->db_do($query);
2160
+ }
2161
+ }
2162
+
2163
+ // added for mapping new sinch categories in merge && !UPDATE_CATEGORY_DATA mode
2164
+ if ((UPDATE_CATEGORY_DATA && $im_type == "MERGE") || ($im_type == "REWRITE")) $where = '';
2165
+ else $where = 'WHERE cce.parent_id = 0 AND cce.store_category_id IS NOT NULL';
2166
+
2167
+ $query = "
2168
+ UPDATE $stINch_categories_mapping_temp cmt
2169
+ JOIN $catalog_category_entity cce
2170
+ ON cmt.shop_entity_id = cce.entity_id
2171
+ SET cce.parent_id = cmt.shop_parent_id
2172
+ $where";
2173
+ echo("\n\n$query\n\n");
2174
+ $this->db_do($query);
2175
+ $this->_LOG("Execute function mapSinchCategoriesMultistore");
2176
+ $query = "DROP TABLE IF EXISTS $stINch_categories_mapping";
2177
+ echo("\n\n$query\n\n");
2178
+ $this->db_do($query);
2179
+
2180
+ $query = "RENAME TABLE $stINch_categories_mapping_temp TO $stINch_categories_mapping";
2181
+ echo("\n\n$query\n\n");
2182
+ $this->db_do($query);
2183
+
2184
+ echo("\nmapSinchCategoriesMultistore done... \n==========================================================================\n\n\n\n");
2185
+ }
2186
+
2187
+ private function createMappingSinchTables($stINch_categories_mapping_temp, $stINch_categories_mapping)
2188
+ {
2189
+ $this->db_do("DROP TABLE IF EXISTS $stINch_categories_mapping_temp");
2190
+ $this->db_do("
2191
+ CREATE TABLE $stINch_categories_mapping_temp
2192
+ (
2193
+ shop_entity_id INT(11) UNSIGNED NOT NULL,
2194
+ shop_entity_type_id INT(11),
2195
+ shop_attribute_set_id INT(11),
2196
+ shop_parent_id INT(11),
2197
+ shop_store_category_id INT(11),
2198
+ shop_parent_store_category_id INT(11),
2199
+ store_category_id INT(11),
2200
+ parent_store_category_id INT(11),
2201
+ category_name VARCHAR(255),
2202
+ order_number INT(11),
2203
+ products_within_this_category INT(11),
2204
+
2205
+ KEY shop_entity_id (shop_entity_id),
2206
+ KEY shop_parent_id (shop_parent_id),
2207
+ KEY store_category_id (store_category_id),
2208
+ KEY parent_store_category_id (parent_store_category_id),
2209
+ UNIQUE KEY(shop_entity_id)
2210
+ )");
2211
+
2212
+
2213
+ $this->db_do("CREATE TABLE IF NOT EXISTS $stINch_categories_mapping LIKE $stINch_categories_mapping_temp");
2214
+ }
2215
+
2216
+ private function addCategoryDataMultistore($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2217
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2218
+ $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid)
2219
+ {
2220
+ echo("\n\n\n\n*************************************************************\nmapSinchCategoriesMultistore start... \n");
2221
+ if (UPDATE_CATEGORY_DATA) {
2222
+ $ignore = '';
2223
+ $on_diplicate_key_update = "
2224
+ ON DUPLICATE KEY UPDATE
2225
+ updated_at = now(),
2226
+ store_category_id = c.store_category_id,
2227
+ level = c.level,
2228
+ children_count = c.children_count,
2229
+ position = c.order_number,
2230
+ parent_store_category_id = c.parent_store_category_id";
2231
+ } else {
2232
+ $ignore = 'IGNORE';
2233
+ $on_diplicate_key_update = '';
2234
+ }
2235
+
2236
+ $query = "
2237
+ INSERT $ignore INTO $catalog_category_entity
2238
+ (
2239
+ entity_type_id,
2240
+ attribute_set_id,
2241
+ created_at,
2242
+ updated_at,
2243
+ level,
2244
+ children_count,
2245
+ entity_id,
2246
+ position,
2247
+ parent_id,
2248
+ store_category_id,
2249
+ parent_store_category_id
2250
+ )
2251
+ (SELECT
2252
+ $_categoryEntityTypeId,
2253
+ $_categoryDefault_attribute_set_id,
2254
+ NOW(),
2255
+ NOW(),
2256
+ c.level,
2257
+ c.children_count,
2258
+ scm.shop_entity_id,
2259
+ c.order_number,
2260
+ scm.shop_parent_id,
2261
+ c.store_category_id,
2262
+ c.parent_store_category_id
2263
+ FROM $categories_temp c
2264
+ LEFT JOIN $stINch_categories_mapping scm
2265
+ ON c.store_category_id = scm.store_category_id
2266
+ ) $on_diplicate_key_update";
2267
+ echo("\n\n$query\n\n");
2268
+ $this->db_do($query);
2269
+
2270
+ $this->mapSinchCategoriesMultistore($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid, true);
2271
+
2272
+ $categories = $this->db_do("SELECT entity_id, parent_id FROM $catalog_category_entity ORDER BY parent_id");
2273
+ while ($row = mysqli_fetch_array($categories)) {
2274
+ $parent_id = $row['parent_id'];
2275
+ $entity_id = $row['entity_id'];
2276
+
2277
+ $path = $this->culcPathMultistore($parent_id, $entity_id, $catalog_category_entity);
2278
+
2279
+ $this->db_do("
2280
+ UPDATE $catalog_category_entity
2281
+ SET path = '$path'
2282
+ WHERE entity_id = $entity_id");
2283
+ }
2284
+
2285
+ if (UPDATE_CATEGORY_DATA) {
2286
+ echo "Update category_data \n";
2287
+
2288
+ $q = "
2289
+ INSERT INTO $catalog_category_entity_varchar
2290
+ (
2291
+ entity_type_id,
2292
+ attribute_id,
2293
+ store_id,
2294
+ entity_id,
2295
+ value
2296
+ )
2297
+ (SELECT
2298
+ $_categoryEntityTypeId,
2299
+ $name_attrid,
2300
+ 0,
2301
+ scm.shop_entity_id,
2302
+ c.category_name
2303
+ FROM $categories_temp c
2304
+ JOIN $stINch_categories_mapping scm
2305
+ ON c.store_category_id = scm.store_category_id
2306
+ )
2307
+ ON DUPLICATE KEY UPDATE
2308
+ value = c.category_name";
2309
+ $this->db_do($q);
2310
+
2311
+ $q = "
2312
+ INSERT INTO $catalog_category_entity_varchar
2313
+ (
2314
+ entity_type_id,
2315
+ attribute_id,
2316
+ store_id,
2317
+ entity_id,
2318
+ value
2319
+ )
2320
+ (SELECT
2321
+ $_categoryEntityTypeId,
2322
+ $name_attrid,
2323
+ 1,
2324
+ scm.shop_entity_id,
2325
+ c.category_name
2326
+ FROM $categories_temp c
2327
+ JOIN $stINch_categories_mapping scm
2328
+ ON c.store_category_id = scm.store_category_id
2329
+ )
2330
+ ON DUPLICATE KEY UPDATE
2331
+ value = c.category_name";
2332
+ $this->db_do($q);
2333
+
2334
+ $q = "
2335
+ INSERT INTO $catalog_category_entity
2336
+ (
2337
+ entity_type_id,
2338
+ attribute_id,
2339
+ store_id,
2340
+ entity_id,
2341
+ value
2342
+ )
2343
+ (SELECT
2344
+ $_categoryEntityTypeId,
2345
+ $attr_is_active,
2346
+ 0,
2347
+ scm.shop_entity_id,
2348
+ 1
2349
+ FROM $categories_temp c
2350
+ JOIN $stINch_categories_mapping scm
2351
+ ON c.store_category_id = scm.store_category_id
2352
+ )
2353
+ ON DUPLICATE KEY UPDATE
2354
+ value = 1";
2355
+ $this->db_do($q);
2356
+
2357
+ $q = "
2358
+ INSERT INTO $catalog_category_entity_int
2359
+ (
2360
+ entity_type_id,
2361
+ attribute_id,
2362
+ store_id,
2363
+ entity_id,
2364
+ value
2365
+ )
2366
+ (SELECT
2367
+ $_categoryEntityTypeId,
2368
+ $attr_is_active,
2369
+ 1,
2370
+ scm.shop_entity_id,
2371
+ 1
2372
+ FROM $categories_temp c
2373
+ JOIN $stINch_categories_mapping scm
2374
+ ON c.store_category_id = scm.store_category_id
2375
+ )
2376
+ ON DUPLICATE KEY UPDATE
2377
+ value = 1";
2378
+ $this->db_do($q);
2379
+
2380
+ $q = "
2381
+ INSERT INTO $catalog_category_entity_int
2382
+ (
2383
+ entity_type_id,
2384
+ attribute_id,
2385
+ store_id,
2386
+ entity_id,
2387
+ value
2388
+ )
2389
+ (SELECT
2390
+ $_categoryEntityTypeId,
2391
+ $attr_include_in_menu,
2392
+ 0,
2393
+ scm.shop_entity_id,
2394
+ c.include_in_menu
2395
+ FROM $categories_temp c
2396
+ JOIN $stINch_categories_mapping scm
2397
+ ON c.store_category_id = scm.store_category_id
2398
+ )
2399
+ ON DUPLICATE KEY UPDATE
2400
+ value = c.include_in_menu";
2401
+ $this->db_do($q);
2402
+
2403
+
2404
+ $q = "
2405
+ INSERT INTO $catalog_category_entity_int
2406
+ (
2407
+ entity_type_id,
2408
+ attribute_id,
2409
+ store_id,
2410
+ entity_id,
2411
+ value
2412
+ )
2413
+ (SELECT
2414
+ $_categoryEntityTypeId,
2415
+ $is_anchor_attrid,
2416
+ 1,
2417
+ scm.shop_entity_id,
2418
+ c.is_anchor
2419
+ FROM $categories_temp c
2420
+ JOIN $stINch_categories_mapping scm
2421
+ ON c.store_category_id = scm.store_category_id
2422
+ )
2423
+ ON DUPLICATE KEY UPDATE
2424
+ value = c.is_anchor";
2425
+ $this->db_do($q);
2426
+
2427
+
2428
+ $q = "
2429
+ INSERT INTO $catalog_category_entity_int
2430
+ (
2431
+ entity_type_id,
2432
+ attribute_id,
2433
+ store_id,
2434
+ entity_id,
2435
+ value
2436
+ )
2437
+ (SELECT
2438
+ $_categoryEntityTypeId,
2439
+ $is_anchor_attrid,
2440
+ 0,
2441
+ scm.shop_entity_id,
2442
+ c.is_anchor
2443
+ FROM $categories_temp c
2444
+ JOIN $stINch_categories_mapping scm
2445
+ ON c.store_category_id = scm.store_category_id
2446
+ )
2447
+ ON DUPLICATE KEY UPDATE
2448
+ value = c.is_anchor";
2449
+ $this->db_do($q);
2450
+
2451
+ $q = "
2452
+ INSERT INTO $catalog_category_entity_varchar
2453
+ (
2454
+ entity_type_id,
2455
+ attribute_id,
2456
+ store_id,
2457
+ entity_id,
2458
+ value
2459
+ )
2460
+ (SELECT
2461
+ $_categoryEntityTypeId,
2462
+ $image_attrid,
2463
+ 0,
2464
+ scm.shop_entity_id,
2465
+ c.categories_image
2466
+ FROM $categories_temp c
2467
+ JOIN $stINch_categories_mapping scm
2468
+ ON c.store_category_id = scm.store_category_id
2469
+ )
2470
+ ON DUPLICATE KEY UPDATE
2471
+ value = c.categories_image";
2472
+ $this->db_do($q);
2473
+
2474
+ $q = "
2475
+ INSERT INTO $catalog_category_entity_varchar
2476
+ (
2477
+ entity_type_id,
2478
+ attribute_id,
2479
+ store_id,
2480
+ entity_id,
2481
+ value
2482
+ )
2483
+ (SELECT
2484
+ $this->_categoryEntityTypeId,
2485
+ $this->_categoryMetaTitleAttrId,
2486
+ 0,
2487
+ scm.shop_entity_id,
2488
+ c.MetaTitle
2489
+ FROM $categories_temp c
2490
+ JOIN $stINch_categories_mapping scm
2491
+ ON c.store_category_id = scm.store_category_id
2492
+ )
2493
+ ON DUPLICATE KEY UPDATE
2494
+ value = c.MetaTitle";
2495
+ $this->db_do($q);
2496
+
2497
+ $q = "
2498
+ INSERT INTO $catalog_category_entity_varchar
2499
+ (
2500
+ entity_type_id,
2501
+ attribute_id,
2502
+ store_id,
2503
+ entity_id,
2504
+ value
2505
+ )
2506
+ (SELECT
2507
+ $this->_categoryEntityTypeId,
2508
+ $this->_categoryMetadescriptionAttrId,
2509
+ 0,
2510
+ scm.shop_entity_id,
2511
+ c.MetaDescription
2512
+ FROM $categories_temp c
2513
+ JOIN $stINch_categories_mapping scm
2514
+ ON c.store_category_id = scm.store_category_id
2515
+ )
2516
+ ON DUPLICATE KEY UPDATE
2517
+ value = c.MetaDescription";
2518
+ $this->db_do($q);
2519
+
2520
+ $q = "
2521
+ INSERT INTO $catalog_category_entity_varchar
2522
+ (
2523
+ entity_type_id,
2524
+ attribute_id,
2525
+ store_id,
2526
+ entity_id,
2527
+ value
2528
+ )
2529
+ (SELECT
2530
+ $this->_categoryEntityTypeId,
2531
+ $this->_categoryDescriptionAttrId,
2532
+ 0,
2533
+ scm.shop_entity_id,
2534
+ c.Description
2535
+ FROM $categories_temp c
2536
+ JOIN $stINch_categories_mapping scm
2537
+ ON c.store_category_id = scm.store_category_id
2538
+ )
2539
+ ON DUPLICATE KEY UPDATE
2540
+ value = c.Description";
2541
+ $this->db_do($q);
2542
+
2543
+ } else {
2544
+ echo "Insert ignore category_data \n";
2545
+
2546
+ $q = "
2547
+ INSERT IGNORE INTO $catalog_category_entity_varchar
2548
+ (
2549
+ entity_type_id,
2550
+ attribute_id,
2551
+ store_id,
2552
+ entity_id,
2553
+ value
2554
+ )
2555
+ (SELECT
2556
+ $_categoryEntityTypeId,
2557
+ $name_attrid,
2558
+ 0,
2559
+ scm.shop_entity_id,
2560
+ c.category_name
2561
+ FROM $categories_temp c
2562
+ JOIN $stINch_categories_mapping scm
2563
+ ON c.store_category_id = scm.store_category_id
2564
+ )";
2565
+ $this->db_do($q);
2566
+
2567
+
2568
+ $q = "
2569
+ INSERT IGNORE INTO $catalog_category_entity_int
2570
+ (
2571
+ entity_type_id,
2572
+ attribute_id,
2573
+ store_id,
2574
+ entity_id,
2575
+ value
2576
+ )
2577
+ (SELECT
2578
+ $_categoryEntityTypeId,
2579
+ $attr_is_active,
2580
+ 0,
2581
+ scm.shop_entity_id,
2582
+ 1
2583
+ FROM $categories_temp c
2584
+ JOIN $stINch_categories_mapping scm
2585
+ ON c.store_category_id = scm.store_category_id
2586
+ )";
2587
+ $this->db_do($q);
2588
+
2589
+
2590
+ $q = "
2591
+ INSERT IGNORE INTO $catalog_category_entity_int
2592
+ (
2593
+ entity_type_id,
2594
+ attribute_id,
2595
+ store_id,
2596
+ entity_id,
2597
+ value
2598
+ )
2599
+ (SELECT
2600
+ $_categoryEntityTypeId,
2601
+ $attr_include_in_menu,
2602
+ 0,
2603
+ scm.shop_entity_id,
2604
+ c.include_in_menu
2605
+ FROM $categories_temp c
2606
+ JOIN $stINch_categories_mapping scm
2607
+ ON c.store_category_id = scm.store_category_id
2608
+ )";
2609
+ $this->db_do($q);
2610
+
2611
+
2612
+ $q = "
2613
+ INSERT IGNORE INTO $catalog_category_entity_int
2614
+ (
2615
+ entity_type_id,
2616
+ attribute_id,
2617
+ store_id,
2618
+ entity_id,
2619
+ value
2620
+ )
2621
+ (SELECT
2622
+ $_categoryEntityTypeId,
2623
+ $is_anchor_attrid,
2624
+ 0,
2625
+ scm.shop_entity_id,
2626
+ c.is_anchor
2627
+ FROM $categories_temp c
2628
+ JOIN $stINch_categories_mapping scm
2629
+ ON c.store_category_id = scm.store_category_id
2630
+ )";
2631
+ $this->db_do($q);
2632
+
2633
+ $q = "
2634
+ INSERT IGNORE INTO $catalog_category_entity_varchar
2635
+ (
2636
+ entity_type_id,
2637
+ attribute_id,
2638
+ store_id,
2639
+ entity_id,
2640
+ value
2641
+ )
2642
+ (SELECT
2643
+ $_categoryEntityTypeId,
2644
+ $image_attrid,
2645
+ 0,
2646
+ scm.shop_entity_id,
2647
+ c.categories_image
2648
+ FROM $categories_temp c
2649
+ JOIN $stINch_categories_mapping scm
2650
+ ON c.store_category_id = scm.store_category_id
2651
+ )";
2652
+ $this->db_do($q);
2653
+
2654
+ $q = "
2655
+ INSERT IGNORE INTO $catalog_category_entity_varchar
2656
+ (
2657
+ entity_type_id,
2658
+ attribute_id,
2659
+ store_id,
2660
+ entity_id,
2661
+ value
2662
+ )
2663
+ (SELECT
2664
+ $this->_categoryEntityTypeId,
2665
+ $this->_categoryMetaTitleAttrId,
2666
+ 0,
2667
+ scm.shop_entity_id,
2668
+ c.MetaTitle
2669
+ FROM $categories_temp c
2670
+ JOIN $stINch_categories_mapping scm
2671
+ ON c.store_category_id = scm.store_category_id
2672
+ )
2673
+ ";
2674
+ $this->db_do($q);
2675
+
2676
+ $q = "
2677
+ INSERT IGNORE INTO $catalog_category_entity_varchar
2678
+ (
2679
+ entity_type_id,
2680
+ attribute_id,
2681
+ store_id,
2682
+ entity_id,
2683
+ value
2684
+ )
2685
+ (SELECT
2686
+ $this->_categoryEntityTypeId,
2687
+ $this->_categoryMetadescriptionAttrId,
2688
+ 0,
2689
+ scm.shop_entity_id,
2690
+ c.MetaDescription
2691
+ FROM $categories_temp c
2692
+ JOIN $stINch_categories_mapping scm
2693
+ ON c.store_category_id = scm.store_category_id
2694
+ )
2695
+ ";
2696
+ $this->db_do($q);
2697
+
2698
+ $q = "
2699
+ INSERT IGNORE INTO $catalog_category_entity_varchar
2700
+ (
2701
+ entity_type_id,
2702
+ attribute_id,
2703
+ store_id,
2704
+ entity_id,
2705
+ value
2706
+ )
2707
+ (SELECT
2708
+ $this->_categoryEntityTypeId,
2709
+ $this->_categoryDescriptionAttrId,
2710
+ 0,
2711
+ scm.shop_entity_id,
2712
+ c.Description
2713
+ FROM $categories_temp c
2714
+ JOIN $stINch_categories_mapping scm
2715
+ ON c.store_category_id = scm.store_category_id
2716
+ )
2717
+ ";
2718
+ $this->db_do($q);
2719
+
2720
+ }
2721
+
2722
+ $this->delete_old_sinch_categories_from_shop();
2723
+ $this->db_do("DROP TABLE IF EXISTS $stINch_categories\n\n");
2724
+ $this->db_do("RENAME TABLE $categories_temp TO $stINch_categories");
2725
+ }
2726
+
2727
+ function culcPathMultistore($parent_id, $ent_id, $catalog_category_entity)
2728
+ {
2729
+ $path = '';
2730
+ $cat_id = $parent_id;
2731
+
2732
+ $q = "
2733
+ SELECT
2734
+ parent_id
2735
+ FROM $catalog_category_entity
2736
+ WHERE entity_id = $cat_id";
2737
+ $quer = $this->db_do($q);
2738
+ $row = mysqli_fetch_array($quer);
2739
+ while ($row['parent_id']) {
2740
+ $path = $row['parent_id'] . '/' . $path;
2741
+ $parent_id = $row['parent_id'];
2742
+
2743
+ $q = "
2744
+ SELECT
2745
+ parent_id
2746
+ FROM $catalog_category_entity
2747
+ WHERE entity_id = $parent_id";
2748
+ $quer = $this->db_do($q);
2749
+ $row = mysqli_fetch_array($quer);
2750
+ }
2751
+
2752
+ if ($cat_id) $path .= $cat_id . "/";
2753
+
2754
+ if ($path) $path .= $ent_id;
2755
+ else $path = $ent_id;
2756
+
2757
+ return $path;
2758
+ }
2759
+
2760
+ private function mergeMultistoreCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2761
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2762
+ $name_attrid, $attr_display_mode, $attr_url_key, $attr_include_in_menu, $attr_is_active, $image_attrid, $is_anchor_attrid,
2763
+ $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $categories_temp)
2764
+ {
2765
+ echo("mergeMultistoreCategories RUN\n");
2766
+
2767
+ $this->createNewDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2768
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu);
2769
+
2770
+ $this->mapSinchCategoriesMultistoreMerge($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid);
2771
+
2772
+ $this->addCategoryDataMultistoreMerge($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2773
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2774
+ $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid);
2775
+
2776
+ echo("\n\n\nmergeMultistoreCategories DONE\n");
2777
+ }
2778
+
2779
+ private function createNewDefaultCategories($coincidence, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2780
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $name_attrid, $attr_display_mode, $attr_url_key, $attr_is_active, $attr_include_in_menu)
2781
+ {
2782
+ echo("\n\n ==========================================================================\n createNewDefaultCategories start... \n");
2783
+
2784
+ $old_cats = array();
2785
+ $query = $this->db_do("
2786
+ SELECT
2787
+ cce.entity_id,
2788
+ ccev.value AS category_name
2789
+ FROM $catalog_category_entity cce
2790
+ JOIN $catalog_category_entity_varchar ccev
2791
+ ON cce.entity_id = ccev.entity_id
2792
+ AND ccev.store_id = 0
2793
+ AND cce.entity_type_id = ccev.entity_type_id
2794
+ AND ccev.attribute_id = 41
2795
+ WHERE parent_id = 1"); // 41 - category name
2796
+ while ($row = mysqli_fetch_array($query)) $old_cats[] = $row['category_name'];
2797
+
2798
+ $query = $this->db_do("SELECT MAX(entity_id) AS max_entity_id FROM $catalog_category_entity");
2799
+ $max_entity_id = mysqli_fetch_array($query);
2800
+
2801
+ $i = $max_entity_id[max_entity_id] + 1;
2802
+
2803
+ foreach ($coincidence as $key => $item) {
2804
+ echo("\n coincidence: key = [$key]\n");
2805
+
2806
+ if (in_array($key, $old_cats)) {
2807
+ echo(" CONTINUE: key = [$key] item = [$item]\n");
2808
+ continue;
2809
+ } else {
2810
+ echo(" CREATE NEW CATEGORY: key = [$key] item = [$item]\n");
2811
+ }
2812
+
2813
+ $this->db_do("INSERT $catalog_category_entity
2814
+ (entity_id, entity_type_id, attribute_set_id, parent_id, created_at, updated_at,
2815
+ path, position, level, children_count, store_category_id, parent_store_category_id)
2816
+ VALUES
2817
+ ($i, $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, 1, now(), now(), '1/$i', 1, 1, 1, NULL, NULL)");
2818
+
2819
+ $this->db_do("INSERT $catalog_category_entity_varchar
2820
+ (entity_type_id, attribute_id, store_id, entity_id, value)
2821
+ VALUES
2822
+ ($_categoryEntityTypeId, $name_attrid, 0, $i, '$key'),
2823
+ ($_categoryEntityTypeId, $name_attrid, 1, $i, '$key'),
2824
+ ($_categoryEntityTypeId, $attr_display_mode, 1, $i, '$key'),
2825
+ ($_categoryEntityTypeId, $attr_url_key, 0, $i, '$key')");
2826
+
2827
+
2828
+ $this->db_do("INSERT $catalog_category_entity_int
2829
+ (entity_type_id, attribute_id, store_id, entity_id, value)
2830
+ VALUES
2831
+ ($_categoryEntityTypeId, $attr_is_active, 0, $i, 1),
2832
+ ($_categoryEntityTypeId, $attr_is_active, 1, $i, 1),
2833
+ ($_categoryEntityTypeId, $attr_include_in_menu, 0, $i, 1),
2834
+ ($_categoryEntityTypeId, $attr_include_in_menu, 1, $i, 1)");
2835
+ $i++;
2836
+ }
2837
+
2838
+ echo("\n createNewDefaultCategories done... \n ==========================================================================\n");
2839
+ }
2840
+
2841
+ private function mapSinchCategoriesMultistoreMerge($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid)
2842
+ {
2843
+ echo("\n\n\ ==========================================================================\n mapSinchCategoriesMultistore start... \n");
2844
+
2845
+ $this->createMappingSinchTables($stINch_categories_mapping_temp, $stINch_categories_mapping);
2846
+
2847
+ $query = "
2848
+ INSERT IGNORE INTO $stINch_categories_mapping_temp
2849
+ (shop_entity_id, shop_entity_type_id, shop_attribute_set_id, shop_parent_id, shop_store_category_id, shop_parent_store_category_id)
2850
+ (SELECT entity_id, entity_type_id, attribute_set_id, parent_id, store_category_id, parent_store_category_id
2851
+ FROM $catalog_category_entity)";
2852
+ echo("\n $query\n");
2853
+ $this->db_do($query);
2854
+
2855
+ $query = "
2856
+ UPDATE $stINch_categories_mapping_temp cmt
2857
+ JOIN $categories_temp c
2858
+ ON cmt.shop_store_category_id = c.store_category_id
2859
+ SET
2860
+ cmt.store_category_id = c.store_category_id,
2861
+ cmt.parent_store_category_id = c.parent_store_category_id,
2862
+ cmt.category_name = c.category_name,
2863
+ cmt.order_number = c.order_number,
2864
+ cmt.products_within_this_category = c.products_within_this_category";
2865
+ echo("\n $query\n");
2866
+ $this->db_do($query);
2867
+
2868
+ $query = "
2869
+ UPDATE $stINch_categories_mapping_temp cmt
2870
+ JOIN $catalog_category_entity cce
2871
+ ON cmt.parent_store_category_id = cce.store_category_id
2872
+ SET cmt.shop_parent_id = cce.entity_id";
2873
+ echo("\n $query\n");
2874
+ $this->db_do($query);
2875
+
2876
+ $query = "
2877
+ SELECT DISTINCT
2878
+ c.RootName, cce.entity_id
2879
+ FROM $categories_temp c
2880
+ JOIN $catalog_category_entity_varchar ccev
2881
+ ON c.RootName = ccev.value
2882
+ AND ccev.entity_type_id = $_categoryEntityTypeId
2883
+ AND ccev.attribute_id = $name_attrid
2884
+ AND ccev.store_id = 0
2885
+ JOIN $catalog_category_entity cce
2886
+ ON ccev.entity_id = cce.entity_id";
2887
+ echo("\n $query\n");
2888
+ $root_categories = $this->db_do($query);
2889
+
2890
+ while ($root_cat = mysqli_fetch_array($root_categories)) {
2891
+ $root_id = $root_cat['entity_id'];
2892
+ $root_name = $root_cat['RootName'];
2893
+
2894
+ $query = "
2895
+ UPDATE $stINch_categories_mapping_temp cmt
2896
+ JOIN $categories_temp c
2897
+ ON cmt.shop_store_category_id = c.store_category_id
2898
+ SET
2899
+ cmt.shop_parent_id = $root_id,
2900
+ cmt.shop_parent_store_category_id = $root_id,
2901
+ cmt.parent_store_category_id = $root_id,
2902
+ c.parent_store_category_id = $root_id
2903
+ WHERE RootName = '$root_name'
2904
+ AND cmt.shop_parent_id = 0";
2905
+ echo("\n $query\n");
2906
+ $this->db_do($query);
2907
+ }
2908
+
2909
+ // added for mapping new sinch categories in merge && !UPDATE_CATEGORY_DATA mode
2910
+ if ((UPDATE_CATEGORY_DATA && $im_type == "MERGE") || ($im_type == "REWRITE")) $where = '';
2911
+ else $where = 'WHERE cce.parent_id = 0 AND cce.store_category_id IS NOT NULL';
2912
+
2913
+ $query = "
2914
+ UPDATE $stINch_categories_mapping_temp cmt
2915
+ JOIN $catalog_category_entity cce
2916
+ ON cmt.shop_entity_id = cce.entity_id
2917
+ SET cce.parent_id = cmt.shop_parent_id
2918
+ $where";
2919
+ echo("\n $query\n");
2920
+ $this->db_do($query);
2921
+
2922
+ $query = "DROP TABLE IF EXISTS $stINch_categories_mapping";
2923
+ echo("\n $query\n");
2924
+ $this->db_do($query);
2925
+
2926
+ $query = "RENAME TABLE $stINch_categories_mapping_temp TO $stINch_categories_mapping";
2927
+ echo("\n $query\n");
2928
+ $this->db_do($query);
2929
+
2930
+ echo("\n mapSinchCategoriesMultistore done... \n ==========================================================================\n\n\n\n");
2931
+ }
2932
+
2933
+
2934
+ private function addCategoryDataMultistoreMerge($categories_temp, $stINch_categories_mapping_temp, $stINch_categories_mapping, $stINch_categories, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int,
2935
+ $_categoryEntityTypeId, $_categoryDefault_attribute_set_id, $im_type,
2936
+ $name_attrid, $attr_is_active, $attr_include_in_menu, $is_anchor_attrid, $image_attrid)
2937
+ {
2938
+ echo("\n\n\n\n *************************************************************\n addCategoryDataMultistoreMerge start... \n");
2939
+
2940
+ if (UPDATE_CATEGORY_DATA) {
2941
+ $ignore = '';
2942
+ $on_diplicate_key_update = "
2943
+ ON DUPLICATE KEY UPDATE
2944
+ updated_at = now(),
2945
+ store_category_id = c.store_category_id,
2946
+ level = c.level,
2947
+ children_count = c.children_count,
2948
+ position = c.order_number,
2949
+ parent_store_category_id = c.parent_store_category_id";
2950
+ } else {
2951
+ $ignore = 'IGNORE';
2952
+ $on_diplicate_key_update = '';
2953
+ }
2954
+
2955
+ $query = "
2956
+ INSERT $ignore INTO $catalog_category_entity
2957
+ (
2958
+ entity_type_id,
2959
+ attribute_set_id,
2960
+ created_at,
2961
+ updated_at,
2962
+ level,
2963
+ children_count,
2964
+ entity_id,
2965
+ position,
2966
+ parent_id,
2967
+ store_category_id,
2968
+ parent_store_category_id
2969
+ )
2970
+ (SELECT
2971
+ $_categoryEntityTypeId,
2972
+ $_categoryDefault_attribute_set_id,
2973
+ NOW(),
2974
+ NOW(),
2975
+ c.level,
2976
+ c.children_count,
2977
+ scm.shop_entity_id,
2978
+ c.order_number,
2979
+ scm.shop_parent_id,
2980
+ c.store_category_id,
2981
+ c.parent_store_category_id
2982
+ FROM $categories_temp c
2983
+ LEFT JOIN $stINch_categories_mapping scm
2984
+ ON c.store_category_id = scm.store_category_id
2985
+ ) $on_diplicate_key_update";
2986
+ echo("\n\n $query\n\n");
2987
+ $this->db_do($query);
2988
+
2989
+ $this->mapSinchCategoriesMultistoreMerge($stINch_categories_mapping_temp, $stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $categories_temp, $im_type, $_categoryEntityTypeId, $name_attrid);
2990
+
2991
+ $categories = $this->db_do("SELECT entity_id, parent_id FROM $catalog_category_entity ORDER BY parent_id");
2992
+ while ($row = mysqli_fetch_array($categories)) {
2993
+ $parent_id = $row['parent_id'];
2994
+ $entity_id = $row['entity_id'];
2995
+
2996
+ $path = $this->culcPathMultistore($parent_id, $entity_id, $catalog_category_entity);
2997
+
2998
+ $this->db_do("
2999
+ UPDATE $catalog_category_entity
3000
+ SET path = '$path'
3001
+ WHERE entity_id = $entity_id");
3002
+ }
3003
+
3004
+ if (UPDATE_CATEGORY_DATA) {
3005
+ echo "Update category_data \n";
3006
+
3007
+ $q = "
3008
+ INSERT INTO $catalog_category_entity_varchar
3009
+ (
3010
+ entity_type_id,
3011
+ attribute_id,
3012
+ store_id,
3013
+ entity_id,
3014
+ value
3015
+ )
3016
+ (SELECT
3017
+ $_categoryEntityTypeId,
3018
+ $name_attrid,
3019
+ 0,
3020
+ scm.shop_entity_id,
3021
+ c.category_name
3022
+ FROM $categories_temp c
3023
+ JOIN $stINch_categories_mapping scm
3024
+ ON c.store_category_id = scm.store_category_id
3025
+ )
3026
+ ON DUPLICATE KEY UPDATE
3027
+ value = c.category_name";
3028
+ $this->db_do($q);
3029
+
3030
+ $q = "
3031
+ INSERT INTO $catalog_category_entity_varchar
3032
+ (
3033
+ entity_type_id,
3034
+ attribute_id,
3035
+ store_id,
3036
+ entity_id,
3037
+ value
3038
+ )
3039
+ (SELECT
3040
+ $_categoryEntityTypeId,
3041
+ $name_attrid,
3042
+ 1,
3043
+ scm.shop_entity_id,
3044
+ c.category_name
3045
+ FROM $categories_temp c
3046
+ JOIN $stINch_categories_mapping scm
3047
+ ON c.store_category_id = scm.store_category_id
3048
+ )
3049
+ ON DUPLICATE KEY UPDATE
3050
+ value = c.category_name";
3051
+ $this->db_do($q);
3052
+
3053
+ $q = "
3054
+ INSERT INTO $catalog_category_entity
3055
+ (
3056
+ entity_type_id,
3057
+ attribute_id,
3058
+ store_id,
3059
+ entity_id,
3060
+ value
3061
+ )
3062
+ (SELECT
3063
+ $_categoryEntityTypeId,
3064
+ $attr_is_active,
3065
+ 0,
3066
+ scm.shop_entity_id,
3067
+ 1
3068
+ FROM $categories_temp c
3069
+ JOIN $stINch_categories_mapping scm
3070
+ ON c.store_category_id = scm.store_category_id
3071
+ )
3072
+ ON DUPLICATE KEY UPDATE
3073
+ value = 1";
3074
+ $this->db_do($q);
3075
+
3076
+ $q = "
3077
+ INSERT INTO $catalog_category_entity_int
3078
+ (
3079
+ entity_type_id,
3080
+ attribute_id,
3081
+ store_id,
3082
+ entity_id,
3083
+ value
3084
+ )
3085
+ (SELECT
3086
+ $_categoryEntityTypeId,
3087
+ $attr_is_active,
3088
+ 1,
3089
+ scm.shop_entity_id,
3090
+ 1
3091
+ FROM $categories_temp c
3092
+ JOIN $stINch_categories_mapping scm
3093
+ ON c.store_category_id = scm.store_category_id
3094
+ )
3095
+ ON DUPLICATE KEY UPDATE
3096
+ value = 1";
3097
+ $this->db_do($q);
3098
+
3099
+ $q = "
3100
+ INSERT INTO $catalog_category_entity_int
3101
+ (
3102
+ entity_type_id,
3103
+ attribute_id,
3104
+ store_id,
3105
+ entity_id,
3106
+ value
3107
+ )
3108
+ (SELECT
3109
+ $_categoryEntityTypeId,
3110
+ $attr_include_in_menu,
3111
+ 0,
3112
+ scm.shop_entity_id,
3113
+ c.include_in_menu
3114
+ FROM $categories_temp c
3115
+ JOIN $stINch_categories_mapping scm
3116
+ ON c.store_category_id = scm.store_category_id
3117
+ )
3118
+ ON DUPLICATE KEY UPDATE
3119
+ value = c.include_in_menu";
3120
+ $this->db_do($q);
3121
+
3122
+ $q = "
3123
+ INSERT INTO $catalog_category_entity_int
3124
+ (
3125
+ entity_type_id,
3126
+ attribute_id,
3127
+ store_id,
3128
+ entity_id,
3129
+ value
3130
+ )
3131
+ (SELECT
3132
+ $_categoryEntityTypeId,
3133
+ $is_anchor_attrid,
3134
+ 1,
3135
+ scm.shop_entity_id,
3136
+ c.is_anchor
3137
+ FROM $categories_temp c
3138
+ JOIN $stINch_categories_mapping scm
3139
+ ON c.store_category_id = scm.store_category_id
3140
+ )
3141
+ ON DUPLICATE KEY UPDATE
3142
+ value = c.is_anchor";
3143
+ $this->db_do($q);
3144
+
3145
+ $q = "
3146
+ INSERT INTO $catalog_category_entity_int
3147
+ (
3148
+ entity_type_id,
3149
+ attribute_id,
3150
+ store_id,
3151
+ entity_id,
3152
+ value
3153
+ )
3154
+ (SELECT
3155
+ $_categoryEntityTypeId,
3156
+ $is_anchor_attrid,
3157
+ 0,
3158
+ scm.shop_entity_id,
3159
+ c.is_anchor
3160
+ FROM $categories_temp c
3161
+ JOIN $stINch_categories_mapping scm
3162
+ ON c.store_category_id = scm.store_category_id
3163
+ )
3164
+ ON DUPLICATE KEY UPDATE
3165
+ value = c.is_anchor";
3166
+ $this->db_do($q);
3167
+
3168
+ $q = "
3169
+ INSERT INTO $catalog_category_entity_varchar
3170
+ (
3171
+ entity_type_id,
3172
+ attribute_id,
3173
+ store_id,
3174
+ entity_id,
3175
+ value
3176
+ )
3177
+ (SELECT
3178
+ $_categoryEntityTypeId,
3179
+ $image_attrid,
3180
+ 0,
3181
+ scm.shop_entity_id,
3182
+ c.categories_image
3183
+ FROM $categories_temp c
3184
+ JOIN $stINch_categories_mapping scm
3185
+ ON c.store_category_id = scm.store_category_id
3186
+ )
3187
+ ON DUPLICATE KEY UPDATE
3188
+ value = c.categories_image";
3189
+ $this->db_do($q);
3190
+
3191
+ $q = "
3192
+ INSERT INTO $catalog_category_entity_varchar
3193
+ (
3194
+ entity_type_id,
3195
+ attribute_id,
3196
+ store_id,
3197
+ entity_id,
3198
+ value
3199
+ )
3200
+ (SELECT
3201
+ $this->_categoryEntityTypeId,
3202
+ $this->_categoryMetaTitleAttrId,
3203
+ 0,
3204
+ scm.shop_entity_id,
3205
+ c.MetaTitle
3206
+ FROM $categories_temp c
3207
+ JOIN $stINch_categories_mapping scm
3208
+ ON c.store_category_id = scm.store_category_id
3209
+ )
3210
+ ON DUPLICATE KEY UPDATE
3211
+ value = c.MetaTitle";
3212
+ $this->db_do($q);
3213
+
3214
+ $q = "
3215
+ INSERT INTO $catalog_category_entity_varchar
3216
+ (
3217
+ entity_type_id,
3218
+ attribute_id,
3219
+ store_id,
3220
+ entity_id,
3221
+ value
3222
+ )
3223
+ (SELECT
3224
+ $this->_categoryEntityTypeId,
3225
+ $this->_categoryMetadescriptionAttrId,
3226
+ 0,
3227
+ scm.shop_entity_id,
3228
+ c.MetaDescription
3229
+ FROM $categories_temp c
3230
+ JOIN $stINch_categories_mapping scm
3231
+ ON c.store_category_id = scm.store_category_id
3232
+ )
3233
+ ON DUPLICATE KEY UPDATE
3234
+ value = c.MetaDescription";
3235
+ $this->db_do($q);
3236
+
3237
+ $q = "
3238
+ INSERT INTO $catalog_category_entity_varchar
3239
+ (
3240
+ entity_type_id,
3241
+ attribute_id,
3242
+ store_id,
3243
+ entity_id,
3244
+ value
3245
+ )
3246
+ (SELECT
3247
+ $this->_categoryEntityTypeId,
3248
+ $this->_categoryDescriptionAttrId,
3249
+ 0,
3250
+ scm.shop_entity_id,
3251
+ c.Description
3252
+ FROM $categories_temp c
3253
+ JOIN $stINch_categories_mapping scm
3254
+ ON c.store_category_id = scm.store_category_id
3255
+ )
3256
+ ON DUPLICATE KEY UPDATE
3257
+ value = c.Description";
3258
+ $this->db_do($q);
3259
+ } else {
3260
+ echo "Insert ignore category_data \n";
3261
+
3262
+ $q = "
3263
+ INSERT IGNORE INTO $catalog_category_entity_varchar
3264
+ (
3265
+ entity_type_id,
3266
+ attribute_id,
3267
+ store_id,
3268
+ entity_id,
3269
+ value
3270
+ )
3271
+ (SELECT
3272
+ $_categoryEntityTypeId,
3273
+ $name_attrid,
3274
+ 0,
3275
+ scm.shop_entity_id,
3276
+ c.category_name
3277
+ FROM $categories_temp c
3278
+ JOIN $stINch_categories_mapping scm
3279
+ ON c.store_category_id = scm.store_category_id
3280
+ )";
3281
+ $this->db_do($q);
3282
+
3283
+ $q = "
3284
+ INSERT IGNORE INTO $catalog_category_entity_int
3285
+ (
3286
+ entity_type_id,
3287
+ attribute_id,
3288
+ store_id,
3289
+ entity_id,
3290
+ value
3291
+ )
3292
+ (SELECT
3293
+ $_categoryEntityTypeId,
3294
+ $attr_is_active,
3295
+ 0,
3296
+ scm.shop_entity_id,
3297
+ 1
3298
+ FROM $categories_temp c
3299
+ JOIN $stINch_categories_mapping scm
3300
+ ON c.store_category_id = scm.store_category_id
3301
+ )";
3302
+ $this->db_do($q);
3303
+
3304
+ $q = "
3305
+ INSERT IGNORE INTO $catalog_category_entity_int
3306
+ (
3307
+ entity_type_id,
3308
+ attribute_id,
3309
+ store_id,
3310
+ entity_id,
3311
+ value
3312
+ )
3313
+ (SELECT
3314
+ $_categoryEntityTypeId,
3315
+ $attr_include_in_menu,
3316
+ 0,
3317
+ scm.shop_entity_id,
3318
+ c.include_in_menu
3319
+ FROM $categories_temp c
3320
+ JOIN $stINch_categories_mapping scm
3321
+ ON c.store_category_id = scm.store_category_id
3322
+ )";
3323
+ $this->db_do($q);
3324
+
3325
+ $q = "
3326
+ INSERT IGNORE INTO $catalog_category_entity_int
3327
+ (
3328
+ entity_type_id,
3329
+ attribute_id,
3330
+ store_id,
3331
+ entity_id,
3332
+ value
3333
+ )
3334
+ (SELECT
3335
+ $_categoryEntityTypeId,
3336
+ $is_anchor_attrid,
3337
+ 0,
3338
+ scm.shop_entity_id,
3339
+ c.is_anchor
3340
+ FROM $categories_temp c
3341
+ JOIN $stINch_categories_mapping scm
3342
+ ON c.store_category_id = scm.store_category_id
3343
+ )";
3344
+ $this->db_do($q);
3345
+
3346
+ $q = "
3347
+ INSERT IGNORE INTO $catalog_category_entity_varchar
3348
+ (
3349
+ entity_type_id,
3350
+ attribute_id,
3351
+ store_id,
3352
+ entity_id,
3353
+ value
3354
+ )
3355
+ (SELECT
3356
+ $_categoryEntityTypeId,
3357
+ $image_attrid,
3358
+ 0,
3359
+ scm.shop_entity_id,
3360
+ c.categories_image
3361
+ FROM $categories_temp c
3362
+ JOIN $stINch_categories_mapping scm
3363
+ ON c.store_category_id = scm.store_category_id
3364
+ )";
3365
+ $this->db_do($q);
3366
+
3367
+ $q = "
3368
+ INSERT IGNORE INTO $catalog_category_entity_varchar
3369
+ (
3370
+ entity_type_id,
3371
+ attribute_id,
3372
+ store_id,
3373
+ entity_id,
3374
+ value
3375
+ )
3376
+ (SELECT
3377
+ $this->_categoryEntityTypeId,
3378
+ $this->_categoryMetaTitleAttrId,
3379
+ 0,
3380
+ scm.shop_entity_id,
3381
+ c.MetaTitle
3382
+ FROM $categories_temp c
3383
+ JOIN $stINch_categories_mapping scm
3384
+ ON c.store_category_id = scm.store_category_id
3385
+ )
3386
+ ";
3387
+ $this->db_do($q);
3388
+
3389
+ $q = "
3390
+ INSERT IGNORE INTO $catalog_category_entity_varchar
3391
+ (
3392
+ entity_type_id,
3393
+ attribute_id,
3394
+ store_id,
3395
+ entity_id,
3396
+ value
3397
+ )
3398
+ (SELECT
3399
+ $this->_categoryEntityTypeId,
3400
+ $this->_categoryMetadescriptionAttrId,
3401
+ 0,
3402
+ scm.shop_entity_id,
3403
+ c.MetaDescription
3404
+ FROM $categories_temp c
3405
+ JOIN $stINch_categories_mapping scm
3406
+ ON c.store_category_id = scm.store_category_id
3407
+ )
3408
+ ";
3409
+ $this->db_do($q);
3410
+
3411
+ $q = "
3412
+ INSERT IGNORE INTO $catalog_category_entity_varchar
3413
+ (
3414
+ entity_type_id,
3415
+ attribute_id,
3416
+ store_id,
3417
+ entity_id,
3418
+ value
3419
+ )
3420
+ (SELECT
3421
+ $this->_categoryEntityTypeId,
3422
+ $this->_categoryDescriptionAttrId,
3423
+ 0,
3424
+ scm.shop_entity_id,
3425
+ c.Description
3426
+ FROM $categories_temp c
3427
+ JOIN $stINch_categories_mapping scm
3428
+ ON c.store_category_id = scm.store_category_id
3429
+ )
3430
+ ";
3431
+ $this->db_do($q);
3432
+ }
3433
+
3434
+ $this->db_do("DROP TABLE IF EXISTS $stINch_categories\n\n");
3435
+ $this->db_do("RENAME TABLE $categories_temp TO $stINch_categories");
3436
+
3437
+ $this->deleteOldSinchCategoriesFromShopMerge($stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int);
3438
+
3439
+ echo("\n addCategoryDataMultistoreMerge done... \n *************************************************************\n");
3440
+ }
3441
+
3442
+ private function deleteOldSinchCategoriesFromShopMerge($stINch_categories_mapping, $catalog_category_entity, $catalog_category_entity_varchar, $catalog_category_entity_int)
3443
+ {
3444
+ echo("\n\n\n\n +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n deleteOldSinchCategoriesFromShopMerge start... \n");
3445
+
3446
+ $query = "DROP TABLE IF EXISTS delete_cats";
3447
+ echo("\n $query\n");
3448
+ $this->db_do($query);
3449
+
3450
+ $delete_cats = Mage::getSingleton('core/resource')->getTableName('delete_cats');
3451
+ $stINch_categories = Mage::getSingleton('core/resource')->getTableName('stINch_categories');
3452
+
3453
+ $query = "
3454
+ CREATE TABLE $delete_cats
3455
+
3456
+ SELECT entity_id
3457
+ FROM $catalog_category_entity cce
3458
+ WHERE cce.entity_id NOT IN
3459
+ (
3460
+ SELECT cce2.entity_id
3461
+ FROM $catalog_category_entity cce2
3462
+ JOIN $stINch_categories sc
3463
+ ON cce2.store_category_id = sc.store_category_id
3464
+ )
3465
+ AND cce.store_category_id IS NOT NULL
3466
+ ;";
3467
+
3468
+ echo("\n $query\n");
3469
+ $this->db_do($query);
3470
+
3471
+
3472
+ $query = "DELETE cce FROM $catalog_category_entity cce JOIN $delete_cats dc USING(entity_id)";
3473
+ echo("\n $query\n");
3474
+ $this->db_do($query);
3475
+
3476
+ $query = "DROP TABLE IF EXISTS $delete_cats";
3477
+ echo("\n $query\n");
3478
+
3479
+ echo("\n deleteOldSinchCategoriesFromShopMerge done... \n +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n\n");
3480
+ }
3481
+
3482
+ private function _set_default_root_category()
3483
+ {
3484
+ $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('core_store_group') . " csg
3485
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
3486
+ ON csg.root_category_id = cce.entity_id
3487
+ SET csg.root_category_id=(SELECT entity_id FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " WHERE parent_id = 1 LIMIT 1)
3488
+ WHERE csg.root_category_id > 0 AND cce.entity_id IS NULL";
3489
+ $this->db_do($q);
3490
+ }
3491
+
3492
+ function ParseCategoryFeatures()
3493
+ {
3494
+
3495
+ $parse_file = $this->varDir . FILE_CATEGORIES_FEATURES;
3496
+ if (filesize($parse_file) || $this->_ignore_category_features) {
3497
+ $this->_LOG("Start parse " . FILE_CATEGORIES_FEATURES);
3498
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp'));
3499
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp') . " (
3500
+ category_feature_id int(11),
3501
+ store_category_id int(11),
3502
+ feature_name varchar(50),
3503
+ display_order_number int(11),
3504
+ KEY(store_category_id),
3505
+ KEY(category_feature_id)
3506
+ )
3507
+ ");
3508
+
3509
+ if (!$this->_ignore_category_features) {
3510
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3511
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp') . "
3512
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3513
+ OPTIONALLY ENCLOSED BY '\"'
3514
+ LINES TERMINATED BY \"\r\n\"
3515
+ IGNORE 1 LINES ");
3516
+ }
3517
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_features'));
3518
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('categories_features_temp') . "
3519
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_features'));
3520
+
3521
+ $this->_LOG("Finish parse " . FILE_CATEGORIES_FEATURES);
3522
+ } else {
3523
+ $this->_LOG("Wrong file " . $parse_file);
3524
+ }
3525
+ $this->_LOG(' ');
3526
+ }
3527
+
3528
+ function ParseDistributors()
3529
+ {
3530
+ $parse_file = $this->varDir . FILE_DISTRIBUTORS;
3531
+ if (filesize($parse_file)) {
3532
+ $this->_LOG("Start parse " . FILE_DISTRIBUTORS);
3533
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('distributors_temp'));
3534
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_temp') . "(
3535
+ distributor_id int(11),
3536
+ distributor_name varchar(255),
3537
+ website varchar(255),
3538
+ KEY(distributor_id)
3539
+ )
3540
+ ");
3541
+
3542
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3543
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_temp') . "
3544
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3545
+ OPTIONALLY ENCLOSED BY '\"'
3546
+ LINES TERMINATED BY \"\r\n\"
3547
+ IGNORE 1 LINES ");
3548
+
3549
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors'));
3550
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_temp') . "
3551
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors'));
3552
+
3553
+ $this->_LOG("Finish parse " . FILE_DISTRIBUTORS);
3554
+ } else {
3555
+ $this->_LOG("Wrong file " . $parse_file);
3556
+ }
3557
+ $this->_LOG(' ');
3558
+ }
3559
+
3560
+ function ParseDistributorsStockAndPrice()
3561
+ {
3562
+ $parse_file = $this->varDir . FILE_DISTRIBUTORS_STOCK_AND_PRICES;
3563
+ if (filesize($parse_file)) {
3564
+ $this->_LOG("Start parse " . FILE_DISTRIBUTORS_STOCK_AND_PRICES);
3565
+
3566
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp'));
3567
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp') . "(
3568
+ `store_product_id` int(11) DEFAULT NULL,
3569
+ `distributor_id` int(11) DEFAULT NULL,
3570
+ `stock` int(11) DEFAULT NULL,
3571
+ `cost` decimal(15,4) DEFAULT NULL,
3572
+ `distributor_sku` varchar(255) DEFAULT NULL,
3573
+ `distributor_category` varchar(50) DEFAULT NULL,
3574
+ `eta` varchar(50) DEFAULT NULL,
3575
+ UNIQUE KEY `product_distri` (store_product_id, distributor_id)
3576
+ )");
3577
+
3578
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3579
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp') . "
3580
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3581
+ OPTIONALLY ENCLOSED BY '\"'
3582
+ LINES TERMINATED BY \"\r\n\"
3583
+ IGNORE 1 LINES ");
3584
+
3585
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
3586
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('distributors_stock_and_price_temp') . "
3587
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
3588
+
3589
+ $this->_LOG("Finish parse " . FILE_DISTRIBUTORS_STOCK_AND_PRICES);
3590
+ } else {
3591
+ $this->_LOG("Wrong file " . $parse_file);
3592
+ }
3593
+ $this->_LOG(' ');
3594
+ }
3595
+
3596
+ function ParseProductContracts()
3597
+ {
3598
+ $parse_file = $this->varDir . FILE_PRODUCT_CONTRACTS;
3599
+ if (filesize($parse_file)) {
3600
+ $this->_LOG("Start parse " . FILE_PRODUCT_CONTRACTS);
3601
+
3602
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp'));
3603
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp') . "(
3604
+ `store_product_id` int(11) DEFAULT NULL,
3605
+ `contract_id` varchar(50) DEFAULT NULL,
3606
+ KEY `store_product_id` (store_product_id)
3607
+ )");
3608
+
3609
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3610
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp') . "
3611
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3612
+ OPTIONALLY ENCLOSED BY '\"'
3613
+ LINES TERMINATED BY \"\r\n\"
3614
+ IGNORE 1 LINES ");
3615
+
3616
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts'));
3617
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('product_contracts_temp') . "
3618
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts'));
3619
+
3620
+ $this->_LOG("Finish parse " . FILE_PRODUCT_CONTRACTS);
3621
+ } else {
3622
+ $this->_LOG("Wrong file " . $parse_file);
3623
+ }
3624
+ $this->_LOG(' ');
3625
+ }
3626
+
3627
+ function ParseEANCodes()
3628
+ {
3629
+
3630
+ $parse_file = $this->varDir . FILE_EANCODES;
3631
+ if (filesize($parse_file)) {
3632
+ $this->_LOG("Start parse " . FILE_EANCODES);
3633
+
3634
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp'));
3635
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp') . "(
3636
+ product_id int(11),
3637
+ ean_code varchar(255),
3638
+ KEY(product_id)
3639
+ )");
3640
+
3641
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3642
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp') . "
3643
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3644
+ OPTIONALLY ENCLOSED BY '\"'
3645
+ LINES TERMINATED BY \"\r\n\"
3646
+ IGNORE 1 LINES ");
3647
+
3648
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes'));
3649
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('ean_codes_temp') . "
3650
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes'));
3651
+
3652
+ $this->_LOG("Finish parse " . FILE_EANCODES);
3653
+ } else {
3654
+ $this->_LOG("Wrong file " . $parse_file);
3655
+ }
3656
+ $this->_LOG(' ');
3657
+ }
3658
+
3659
+ function ParseManufacturers()
3660
+ {
3661
+ $parse_file = $this->varDir . FILE_MANUFACTURERS;
3662
+ if (filesize($parse_file)) {
3663
+ $this->_LOG("Start parse " . FILE_MANUFACTURERS);
3664
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp'));
3665
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . "(
3666
+ sinch_manufacturer_id int(11),
3667
+ manufacturer_name varchar(255),
3668
+ manufacturers_image varchar(255),
3669
+ shop_option_id int(11),
3670
+ KEY(sinch_manufacturer_id),
3671
+ KEY(shop_option_id),
3672
+ KEY(manufacturer_name)
3673
+ )");
3674
+
3675
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3676
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . "
3677
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3678
+ OPTIONALLY ENCLOSED BY '\"'
3679
+ LINES TERMINATED BY \"\r\n\"
3680
+ IGNORE 1 LINES ");
3681
+
3682
+ $q = "DELETE aov
3683
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . " ao
3684
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
3685
+ ON ao.option_id=aov.option_id left
3686
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . " mt
3687
+ ON aov.value=mt.manufacturer_name
3688
+ WHERE
3689
+ ao.attribute_id=" . $this->attributes['manufacturer'] . " AND
3690
+ mt.manufacturer_name is null";
3691
+ $this->db_do($q);
3692
+
3693
+ $q = "DELETE ao
3694
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . " ao
3695
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
3696
+ ON ao.option_id=aov.option_id
3697
+ WHERE
3698
+ attribute_id=" . $this->attributes['manufacturer'] . " AND
3699
+ aov.option_id is null";
3700
+ $this->db_do($q);
3701
+
3702
+ $q = "SELECT
3703
+ m.sinch_manufacturer_id,
3704
+ m.manufacturer_name,
3705
+ m.manufacturers_image
3706
+ FROM " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . " m
3707
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
3708
+ ON m.manufacturer_name=aov.value
3709
+ WHERE aov.value IS NULL";
3710
+ $quer = $this->db_do($q);
3711
+
3712
+ while ($row = mysqli_fetch_array($quer)) {
3713
+ $q0 = "INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . "
3714
+ (attribute_id)
3715
+ VALUES(" . $this->attributes['manufacturer'] . ")";
3716
+ $quer0 = $this->db_do($q0);
3717
+
3718
+ $q2 = "INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . "(
3719
+ option_id,
3720
+ value
3721
+ )(
3722
+ SELECT
3723
+ max(option_id) as option_id,
3724
+ " . "'" . mysqli_real_escape_string($this->db, $row['manufacturer_name']) . "'
3725
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . "
3726
+ WHERE attribute_id=" . $this->attributes['manufacturer'] . "
3727
+ )
3728
+ ";
3729
+ $quer2 = $this->db_do($q2);
3730
+ // $option['attribute_id'] = $this->attributes['manufacturer'];
3731
+ // $option['value'][$row['sinch_manufacturer_id']][0] = $row['manufacturer_name'];
3732
+ }
3733
+
3734
+ $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . " mt
3735
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
3736
+ ON mt.manufacturer_name=aov.value
3737
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option') . " ao
3738
+ ON ao.option_id=aov.option_id
3739
+ SET mt.shop_option_id=aov.option_id
3740
+ WHERE ao.attribute_id=" . $this->attributes['manufacturer'];
3741
+ $this->db_do($q);
3742
+
3743
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers'));
3744
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('manufacturers_temp') . "
3745
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers'));
3746
+ $this->_LOG("Finish parse " . FILE_MANUFACTURERS);
3747
+ } else {
3748
+ $this->_LOG("Wrong file " . $parse_file);
3749
+ }
3750
+ $this->_LOG(' ');
3751
+ }
3752
+
3753
+ function ParseRelatedProducts()
3754
+ {
3755
+
3756
+ $parse_file = $this->varDir . FILE_RELATED_PRODUCTS;
3757
+ if (filesize($parse_file) || $this->_ignore_product_related) {
3758
+ $this->_LOG("Start parse " . FILE_RELATED_PRODUCTS);
3759
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('related_products_temp'));
3760
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('related_products_temp') . "(
3761
+ sinch_product_id int(11),
3762
+ related_sinch_product_id int(11),
3763
+ store_product_id int(11) default null,
3764
+ store_related_product_id int(11) default null,
3765
+ entity_id int(11),
3766
+ related_entity_id int(11),
3767
+ KEY(sinch_product_id),
3768
+ KEY(related_sinch_product_id),
3769
+ KEY(store_product_id)
3770
+ )DEFAULT CHARSET=utf8");
3771
+ if (!$this->_ignore_product_related) {
3772
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3773
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('related_products_temp') . "
3774
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3775
+ OPTIONALLY ENCLOSED BY '\"'
3776
+ LINES TERMINATED BY \"\r\n\"
3777
+ IGNORE 1 LINES ");
3778
+ }
3779
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products'));
3780
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('related_products_temp') . "
3781
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products'));
3782
+
3783
+ $this->_LOG("Finish parse " . FILE_RELATED_PRODUCTS);
3784
+ } else {
3785
+ $this->_LOG("Wrong file " . $parse_file);
3786
+ }
3787
+ $this->_LOG(" ");
3788
+ }
3789
+
3790
+ function ParseProductFeatures()
3791
+ {
3792
+ $parse_file = $this->varDir . FILE_PRODUCT_FEATURES;
3793
+ if (filesize($parse_file) || $this->_ignore_product_features) {
3794
+ $this->_LOG("Start parse " . FILE_PRODUCT_FEATURES);
3795
+
3796
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('product_features_temp'));
3797
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('product_features_temp') . "(
3798
+ product_feature_id int(11),
3799
+ sinch_product_id int(11),
3800
+ restricted_value_id int(11),
3801
+ KEY(sinch_product_id),
3802
+ KEY(restricted_value_id)
3803
+ )
3804
+ ");
3805
+ if (!$this->_ignore_product_features) {
3806
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3807
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('product_features_temp') . "
3808
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3809
+ OPTIONALLY ENCLOSED BY '\"'
3810
+ LINES TERMINATED BY \"\r\n\"
3811
+ IGNORE 1 LINES ");
3812
+ }
3813
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_features'));
3814
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('product_features_temp') . "
3815
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_features'));
3816
+
3817
+ $this->_LOG("Finish parse " . FILE_PRODUCT_FEATURES);
3818
+ } else {
3819
+ $this->_LOG("Wrong file " . $parse_file);
3820
+ }
3821
+ $this->_LOG(" ");
3822
+ }
3823
+
3824
+ function ParseProductCategories()
3825
+ {
3826
+ $parse_file = $this->varDir . FILE_PRODUCT_CATEGORIES;
3827
+ if (filesize($parse_file)) {
3828
+ $this->_LOG("Start parse " . FILE_PRODUCT_CATEGORIES);
3829
+
3830
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp'));
3831
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp') . "(
3832
+ store_product_id int(11),
3833
+ store_category_id int(11),
3834
+ key(store_product_id),
3835
+ key(store_category_id)
3836
+ )");
3837
+
3838
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3839
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp') . "
3840
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3841
+ OPTIONALLY ENCLOSED BY '\"'
3842
+ LINES TERMINATED BY \"\r\n\"
3843
+ IGNORE 1 LINES ");
3844
+
3845
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories'));
3846
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('product_categories_temp') . "
3847
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories'));
3848
+
3849
+ $this->_LOG("Finish parse " . FILE_PRODUCT_CATEGORIES);
3850
+ } else {
3851
+ $this->_LOG("Wrong file " . $parse_file);
3852
+ }
3853
+ $this->_LOG(' ');
3854
+ }
3855
+
3856
+ function ParseProducts($coincidence)
3857
+ {
3858
+ echo("\nParseProducts 1\n");
3859
+ $dataConf = Mage::getStoreConfig('sinchimport_root/sinch_ftp');
3860
+ $replace_merge_product = $dataConf['replace_products'];
3861
+
3862
+ $parse_file = $this->varDir . FILE_PRODUCTS;
3863
+ //$parse_file = $this->varDir . FILE_PRODUCTS_TEST;
3864
+ if (filesize($parse_file)) {
3865
+ $this->_LOG("Start parse " . FILE_PRODUCTS);
3866
+ echo("\nParseProducts 2\n");
3867
+
3868
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('products_temp'));
3869
+ if ($this->product_file_format == "NEW") {
3870
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "(
3871
+ store_product_id int(11),
3872
+ product_sku varchar(255),
3873
+ product_name varchar(255),
3874
+ sinch_manufacturer_id int(11),
3875
+ main_image_url varchar(255),
3876
+ thumb_image_url varchar(255),
3877
+ specifications text,
3878
+ description text,
3879
+ search_cache text,
3880
+ description_type varchar(50),
3881
+ medium_image_url varchar(255),
3882
+ Title varchar(255),
3883
+ Weight decimal(15,4),
3884
+ Family varchar(255),
3885
+ Reviews varchar(255),
3886
+ pdf_url varchar(512),
3887
+ product_short_description varchar(255),
3888
+ products_date_added datetime default NULL,
3889
+ products_last_modified datetime default NULL,
3890
+ availability_id_in_stock int(11) default '1',
3891
+ availability_id_out_of_stock int(11) default '2',
3892
+ products_locate varchar(30) default NULL,
3893
+ products_ordered int(11) NOT NULL default '0',
3894
+ products_url varchar(255) default NULL,
3895
+ products_viewed int(5) default '0',
3896
+ products_seo_url varchar(100) NOT NULL,
3897
+ manufacturer_name varchar(255) default NULL,
3898
+ KEY(store_product_id),
3899
+ KEY(sinch_manufacturer_id)
3900
+ )DEFAULT CHARSET=utf8
3901
+ ");
3902
+ } elseif ($this->product_file_format == "OLD") {
3903
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "(
3904
+ store_category_product_id int(11),
3905
+ store_product_id int(11),
3906
+ sinch_product_id int(11),
3907
+ product_sku varchar(255),
3908
+ product_name varchar(255),
3909
+ sinch_manufacturer_id int(11),
3910
+ store_category_id int(11),
3911
+ main_image_url varchar(255),
3912
+ thumb_image_url varchar(255),
3913
+ specifications text,
3914
+ description text,
3915
+ search_cache text,
3916
+ spec_characte_u_count int(11),
3917
+ description_type varchar(50),
3918
+ medium_image_url varchar(255),
3919
+ products_date_added datetime default NULL,
3920
+ products_last_modified datetime default NULL,
3921
+ availability_id_in_stock int(11) default '1',
3922
+ availability_id_out_of_stock int(11) default '2',
3923
+ products_locate varchar(30) default NULL,
3924
+ products_ordered int(11) NOT NULL default '0',
3925
+ products_url varchar(255) default NULL,
3926
+ products_viewed int(5) default '0',
3927
+ products_seo_url varchar(100) NOT NULL,
3928
+ manufacturer_name varchar(255) default NULL,
3929
+ KEY(store_category_product_id),
3930
+ KEY(store_product_id),
3931
+ KEY(sinch_manufacturer_id),
3932
+ KEY(store_category_id)
3933
+ )DEFAULT CHARSET=utf8
3934
+ ");
3935
+
3936
+ }
3937
+ echo("\nParseProducts 3\n");
3938
+ $this->db_do("LOAD DATA LOCAL INFILE '" . $parse_file . "'
3939
+ INTO TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3940
+ FIELDS TERMINATED BY '" . $this->field_terminated_char . "'
3941
+ OPTIONALLY ENCLOSED BY '\"'
3942
+ LINES TERMINATED BY \"\r\n\"
3943
+ IGNORE 1 LINES ");
3944
+
3945
+ if ($this->product_file_format == "NEW") {
3946
+ $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3947
+ ADD COLUMN sinch_product_id int(11) AFTER store_product_id
3948
+ ");
3949
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3950
+ SET sinch_product_id=store_product_id
3951
+ ");
3952
+
3953
+ $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3954
+ ADD COLUMN store_category_id int(11) AFTER sinch_manufacturer_id
3955
+ ");
3956
+ $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3957
+ ADD KEY(store_category_id)
3958
+ ");
3959
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3960
+ SET product_name = Title WHERE Title != ''
3961
+ ");
3962
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " pt
3963
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
3964
+ SET pt.store_category_id=spc.store_category_id
3965
+ WHERE pt.store_product_id=spc.store_product_id
3966
+ ");
3967
+
3968
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3969
+ SET main_image_url = medium_image_url WHERE main_image_url = ''
3970
+ ");
3971
+ }
3972
+
3973
+ echo("\nParseProducts 4\n");
3974
+
3975
+ echo("\nParseProducts 5\n");
3976
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
3977
+ SET products_date_added=now(), products_last_modified=now()");
3978
+ echo("\nParseProducts 6\n");
3979
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
3980
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " m
3981
+ ON p.sinch_manufacturer_id=m.sinch_manufacturer_id
3982
+ SET p.manufacturer_name=m.manufacturer_name");
3983
+ echo("\nParseProducts 7\n");
3984
+ if ($this->current_import_status_statistic_id) {
3985
+ $res = $this->db_do("SELECT COUNT(*) AS cnt
3986
+ FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp'));
3987
+ $row = mysqli_fetch_assoc($res);
3988
+ $this->db_do("UPDATE " . $this->import_status_statistic_table . "
3989
+ SET number_of_products=" . $row['cnt'] . "
3990
+ WHERE id=" . $this->current_import_status_statistic_id);
3991
+ }
3992
+
3993
+ if ($replace_merge_product == "REWRITE") {
3994
+ $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity'));
3995
+
3996
+ $this->db_do("SET FOREIGN_KEY_CHECKS=0");
3997
+ $this->db_do("TRUNCATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity'));
3998
+ $this->db_do("TRUNCATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar'));
3999
+ $this->db_do("TRUNCATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int'));
4000
+ $this->db_do("TRUNCATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text'));
4001
+ $this->db_do("TRUNCATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal'));
4002
+ $this->db_do("SET FOREIGN_KEY_CHECKS=1");
4003
+ }
4004
+
4005
+ // Modify catalog_product_entity_varchar - Kai
4006
+ $this->db_do("ALTER TABLE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " MODIFY value VARCHAR(512)");
4007
+
4008
+ echo("\nParseProducts 8\n");
4009
+ $this->addProductsWebsite();
4010
+ $this->mapSinchProducts($replace_merge_product);
4011
+ echo("\nParseProducts 9\n");
4012
+
4013
+ if (count($coincidence) == 1) {
4014
+ $this->replaceMagentoProducts();
4015
+ } else {
4016
+ echo("\n\n\n\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ [" . $this->im_type . "] $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n\n");
4017
+
4018
+
4019
+ switch ($this->im_type) {
4020
+ case "REWRITE":
4021
+ $this->replaceMagentoProductsMultistore($coincidence);
4022
+ break;
4023
+ case "MERGE":
4024
+ $this->replaceMagentoProductsMultistoreMERGE($coincidence);
4025
+ break;
4026
+ }
4027
+ }
4028
+ echo("\nParseProducts 10\n");
4029
+
4030
+ // Kai
4031
+ $this->mapSinchProducts($replace_merge_product, true);
4032
+ $this->addManufacturer_attribute();
4033
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products'));
4034
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4035
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_products'));
4036
+ $this->_LOG("Finish parse " . FILE_PRODUCTS);
4037
+ } else {
4038
+ $this->_LOG("Wrong file " . $parse_file);
4039
+ }
4040
+ $this->_LOG(" ");
4041
+ echo("\nParseProducts 11\n");
4042
+ }
4043
+
4044
+
4045
+ public function addProductsWebsite()
4046
+ {
4047
+ $this->db_do(" DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('products_website_temp'));
4048
+
4049
+ $this->db_do("
4050
+ CREATE TABLE `" . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . "` (
4051
+ `id` int(10) unsigned NOT NULL auto_increment,
4052
+ store_product_id int(11),
4053
+ sinch_product_id int(11),
4054
+ `website` int(11) default NULL,
4055
+ `website_id` int(11) default NULL,
4056
+ PRIMARY KEY (`id`),
4057
+ KEY store_product_id (`store_product_id`)
4058
+ )
4059
+ ");
4060
+ $result = $this->db_do("SELECT
4061
+ website_id,
4062
+ store_id as website
4063
+ FROM " . Mage::getSingleton('core/resource')->getTableName('core_store') . "
4064
+ WHERE code!='admin'
4065
+ "); // where code!='admin' was adder for editing Featured products;
4066
+ while ($row = mysqli_fetch_assoc($result)) {
4067
+ $sql = "INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " (
4068
+ store_product_id,
4069
+ sinch_product_id,
4070
+ website,
4071
+ website_id
4072
+ )(
4073
+ SELECT
4074
+ distinct
4075
+ store_product_id,
4076
+ sinch_product_id,
4077
+ {$row['website']},
4078
+ {$row['website_id']}
4079
+ FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
4080
+ )";
4081
+ $result2 = $this->db_do($sql);
4082
+ }
4083
+ }
4084
+
4085
+ public function mapSinchProducts($mode = 'MERGE', $mapping_again = false)
4086
+ {
4087
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp'));
4088
+ $this->db_do("CREATE TABLE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " (
4089
+ entity_id int(11) unsigned NOT NULL,
4090
+ manufacturer_option_id int(11),
4091
+ manufacturer_name varchar(255),
4092
+ shop_store_product_id int(11),
4093
+ shop_sinch_product_id int(11),
4094
+ sku varchar(64) default NULL,
4095
+ store_product_id int(11),
4096
+ sinch_product_id int(11),
4097
+ product_sku varchar(255),
4098
+ sinch_manufacturer_id int(11),
4099
+ sinch_manufacturer_name varchar(255),
4100
+ KEY entity_id (entity_id),
4101
+ KEY manufacturer_option_id (manufacturer_option_id),
4102
+ KEY manufacturer_name (manufacturer_name),
4103
+ KEY store_product_id (store_product_id),
4104
+ KEY sinch_product_id (sinch_product_id),
4105
+ KEY sku (sku),
4106
+ UNIQUE KEY(entity_id)
4107
+ )
4108
+ ");
4109
+ $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . "
4110
+ LIKE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp'));
4111
+ $productEntityTable = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
4112
+
4113
+ // backup Product ID in REWRITE mode
4114
+ if ($mode == 'REWRITE' && !$mapping_again) {
4115
+ $catalog_product_entity_backup = Mage::getSingleton('core/resource')->getTableName('sinch_product_backup');
4116
+ if ($this->_checkDataExist($catalog_product_entity_backup)) {
4117
+ $productEntityTable = $catalog_product_entity_backup;
4118
+ }
4119
+ }
4120
+ // (end) backup Product ID in REWRITE mode
4121
+
4122
+ $result = $this->db_do("
4123
+ INSERT ignore INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " (
4124
+ entity_id,
4125
+ sku,
4126
+ shop_store_product_id,
4127
+ shop_sinch_product_id
4128
+ )(SELECT
4129
+ entity_id,
4130
+ sku,
4131
+ store_product_id,
4132
+ sinch_product_id
4133
+ FROM " . $productEntityTable . "
4134
+ )
4135
+ ");
4136
+
4137
+ $this->addManufacturers(1);
4138
+
4139
+ $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " pmt
4140
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . " cpie
4141
+ ON pmt.entity_id=cpie.entity_id
4142
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value') . " aov
4143
+ ON cpie.value=aov.option_id
4144
+ SET
4145
+ manufacturer_option_id=cpie.value,
4146
+ manufacturer_name=aov.value
4147
+ WHERE cpie.attribute_id=" . $this->attributes['manufacturer'];
4148
+ $this->db_do($q);
4149
+
4150
+ $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " pmt
4151
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4152
+ ON pmt.sku=p.product_sku
4153
+ SET
4154
+ pmt.store_product_id=p.store_product_id,
4155
+ pmt.sinch_product_id=p.sinch_product_id,
4156
+ pmt.product_sku=p.product_sku,
4157
+ pmt.sinch_manufacturer_id=p.sinch_manufacturer_id,
4158
+ pmt.sinch_manufacturer_name=p.manufacturer_name";
4159
+
4160
+ $this->db_do($q);
4161
+
4162
+ $q = "UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4163
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . " pmt
4164
+ ON cpe.entity_id=pmt.entity_id
4165
+ SET cpe.store_product_id=pmt.store_product_id,
4166
+ cpe.sinch_product_id=pmt.sinch_product_id
4167
+ WHERE
4168
+ cpe.sinch_product_id IS NULL
4169
+ AND pmt.sinch_product_id IS NOT NULL
4170
+ AND cpe.store_product_id IS NULL
4171
+ AND pmt.store_product_id IS NOT NULL";
4172
+ $this->db_do($q);
4173
+
4174
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping'));
4175
+ $this->db_do("RENAME TABLE " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping_temp') . "
4176
+ TO " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping'));
4177
+ }
4178
+
4179
+ function addManufacturers($delete_eav = null)
4180
+ {
4181
+ // this cleanup is not needed due to foreign keys
4182
+ if (!$delete_eav) {
4183
+ $result = $this->db_do("
4184
+ DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . "
4185
+ WHERE attribute_id = " . $this->_getProductAttributeId('manufacturer')
4186
+ );
4187
+ }
4188
+ $this->addManufacturer_attribute();
4189
+
4190
+ $result = $this->db_do("
4191
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . " (
4192
+ entity_id,
4193
+ attribute_id,
4194
+ store_id,
4195
+ value
4196
+ )(
4197
+ SELECT
4198
+ a.entity_id,
4199
+ " . $this->_getProductAttributeId('manufacturer') . ",
4200
+ w.website,
4201
+ mn.shop_option_id
4202
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4203
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4204
+ ON a.store_product_id = b.store_product_id
4205
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4206
+ ON a.store_product_id=w.store_product_id
4207
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " mn
4208
+ ON b.sinch_manufacturer_id=mn.sinch_manufacturer_id
4209
+ WHERE mn.shop_option_id IS NOT NULL
4210
+ )
4211
+ ON DUPLICATE KEY UPDATE
4212
+ value = mn.shop_option_id
4213
+ ");
4214
+
4215
+ $result = $this->db_do("
4216
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_index_eav') . " (
4217
+ entity_id,
4218
+ attribute_id,
4219
+ store_id,
4220
+ value
4221
+ )(
4222
+ SELECT
4223
+ a.entity_id,
4224
+ " . $this->_getProductAttributeId('manufacturer') . ",
4225
+ 0,
4226
+ mn.shop_option_id
4227
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4228
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4229
+ ON a.store_product_id = b.store_product_id
4230
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4231
+ ON a.store_product_id=w.store_product_id
4232
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_manufacturers') . " mn
4233
+ ON b.sinch_manufacturer_id=mn.sinch_manufacturer_id
4234
+ WHERE mn.shop_option_id IS NOT NULL
4235
+ )
4236
+ ON DUPLICATE KEY UPDATE
4237
+ value = mn.shop_option_id
4238
+ ");
4239
+ }
4240
+
4241
+ private function _getProductAttributeId($attributeCode)
4242
+ {
4243
+ return $this->_getAttributeId($attributeCode, 'catalog_product');
4244
+ }
4245
+
4246
+ private function addManufacturer_attribute()
4247
+ {
4248
+ $result = $this->db_do("
4249
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4250
+ entity_type_id,
4251
+ attribute_id,
4252
+ store_id,
4253
+ entity_id,
4254
+ value
4255
+ )(
4256
+ SELECT
4257
+ " . $this->_getProductEntityTypeId() . ",
4258
+ " . $this->_getProductAttributeId('manufacturer') . ",
4259
+ 0,
4260
+ a.entity_id,
4261
+ pm.manufacturer_option_id
4262
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4263
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4264
+ ON a.entity_id = pm.entity_id
4265
+ )
4266
+ ON DUPLICATE KEY UPDATE
4267
+ value = pm.manufacturer_option_id
4268
+ ");
4269
+ }
4270
+
4271
+ public function replaceMagentoProducts()
4272
+ {
4273
+ $connection = Mage::getModel('core/resource')->getConnection('core_write');
4274
+
4275
+ $result = $this->db_do("DELETE cpe
4276
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4277
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4278
+ ON cpe.entity_id=pm.entity_id
4279
+ WHERE pm.shop_store_product_id IS NOT NULL
4280
+ AND pm.store_product_id IS NULL
4281
+ ");
4282
+
4283
+ //Inserting new products and updating old others.
4284
+ $this->_getProductDefaulAttributeSetId();
4285
+ $result = $this->db_do("
4286
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " (
4287
+ entity_id,
4288
+ entity_type_id,
4289
+ attribute_set_id,
4290
+ type_id,
4291
+ sku,
4292
+ updated_at,
4293
+ has_options,
4294
+ store_product_id,
4295
+ sinch_product_id
4296
+ )(SELECT
4297
+ pm.entity_id,
4298
+ " . $this->_getProductEntityTypeId() . ",
4299
+ $this->defaultAttributeSetId,
4300
+ 'simple',
4301
+ a.product_sku,
4302
+ NOW(),
4303
+ 0,
4304
+ a.store_product_id,
4305
+ a.sinch_product_id
4306
+ FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " a
4307
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4308
+ ON a.store_product_id=pm.store_product_id
4309
+ AND a.sinch_product_id=pm.sinch_product_id
4310
+ WHERE pm.entity_id IS NOT NULL
4311
+ )
4312
+ ON DUPLICATE KEY UPDATE
4313
+ sku= a.product_sku,
4314
+ store_product_id=a.store_product_id,
4315
+ sinch_product_id=a.sinch_product_id
4316
+ ");
4317
+ // store_product_id=a.store_product_id,
4318
+ // sinch_product_id=a.sinch_product_id
4319
+
4320
+ $result = $this->db_do("
4321
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " (
4322
+ entity_id,
4323
+ entity_type_id,
4324
+ attribute_set_id,
4325
+ type_id,
4326
+ sku,
4327
+ updated_at,
4328
+ has_options,
4329
+ store_product_id,
4330
+ sinch_product_id
4331
+ )(SELECT
4332
+ pm.entity_id,
4333
+ " . $this->_getProductEntityTypeId() . ",
4334
+ $this->defaultAttributeSetId,
4335
+ 'simple',
4336
+ a.product_sku,
4337
+ NOW(),
4338
+ 0,
4339
+ a.store_product_id,
4340
+ a.sinch_product_id
4341
+ FROM " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " a
4342
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping') . " pm
4343
+ ON a.store_product_id=pm.store_product_id
4344
+ AND a.sinch_product_id=pm.sinch_product_id
4345
+ WHERE pm.entity_id IS NULL
4346
+ )
4347
+ ON DUPLICATE KEY UPDATE
4348
+ sku= a.product_sku,
4349
+ store_product_id=a.store_product_id,
4350
+ sinch_product_id=a.sinch_product_id
4351
+ ");
4352
+
4353
+ //Set enabled
4354
+ $result = $this->db_do("DELETE cpei
4355
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " cpei
4356
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4357
+ ON cpei.entity_id=cpe.entity_id
4358
+ WHERE cpe.entity_id IS NULL");
4359
+
4360
+ $result = $this->db_do("
4361
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4362
+ entity_type_id,
4363
+ attribute_id,
4364
+ store_id,
4365
+ entity_id,
4366
+ value
4367
+ )(
4368
+ SELECT
4369
+ " . $this->_getProductEntityTypeId() . ",
4370
+ " . $this->_getProductAttributeId('status') . ",
4371
+ w.website,
4372
+ a.entity_id,
4373
+ 1
4374
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4375
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4376
+ ON a.store_product_id=w.store_product_id
4377
+ )
4378
+ ON DUPLICATE KEY UPDATE
4379
+ value=1
4380
+ ");
4381
+ // set status = 1 for all stores
4382
+ $result = $this->db_do("
4383
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4384
+ entity_type_id,
4385
+ attribute_id,
4386
+ store_id,
4387
+ entity_id,
4388
+ value
4389
+ )(SELECT
4390
+ " . $this->_getProductEntityTypeId() . ",
4391
+ " . $this->_getProductAttributeId('status') . ",
4392
+ 0,
4393
+ a.entity_id,
4394
+ 1
4395
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4396
+ )
4397
+ ON DUPLICATE KEY UPDATE
4398
+ value=1
4399
+ ");
4400
+
4401
+ //Unifying products with categories.
4402
+ $result = $this->db_do("DELETE ccp
4403
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4404
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4405
+ ON ccp.product_id=cpe.entity_id
4406
+ WHERE cpe.entity_id IS NULL");
4407
+
4408
+ echo("\n\n\nUPDATE IGNORE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4409
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4410
+ ON ccp.category_id=cce.entity_id
4411
+ SET ccp.category_id=" . $this->_root_cat . "
4412
+ WHERE cce.entity_id IS NULL");
4413
+ $result = $this->db_do("UPDATE IGNORE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4414
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4415
+ ON ccp.category_id=cce.entity_id
4416
+ SET ccp.category_id=" . $this->_root_cat . "
4417
+ WHERE cce.entity_id IS NULL");
4418
+ echo("\ndone\n");
4419
+
4420
+
4421
+ echo("\n\n\nDELETE ccp FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4422
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4423
+ ON ccp.category_id=cce.entity_id
4424
+ WHERE cce.entity_id IS NULL");
4425
+ $result = $this->db_do("DELETE ccp FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4426
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_entity') . " cce
4427
+ ON ccp.category_id=cce.entity_id
4428
+ WHERE cce.entity_id IS NULL");
4429
+ echo("\ndone\n");
4430
+
4431
+
4432
+ $this->db_do(" DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp");
4433
+ // TEMPORARY
4434
+ $this->db_do("
4435
+ CREATE TABLE `" . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp` (
4436
+ `category_id` int(10) unsigned NOT NULL default '0',
4437
+ `product_id` int(10) unsigned NOT NULL default '0',
4438
+ `store_product_id` int(10) NOT NULL default '0',
4439
+ `store_category_id` int(10) NOT NULL default '0',
4440
+ `new_category_id` int(10) NOT NULL default '0',
4441
+ UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`category_id`,`product_id`),
4442
+ KEY `CATALOG_CATEGORY_PRODUCT_CATEGORY` (`category_id`),
4443
+ KEY `CATALOG_CATEGORY_PRODUCT_PRODUCT` (`product_id`),
4444
+ KEY `CATALOG_NEW_CATEGORY_PRODUCT_CATEGORY` (`new_category_id`)
4445
+ )
4446
+
4447
+ ");
4448
+
4449
+ $result = $this->db_do("
4450
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp (
4451
+ category_id,
4452
+ product_id,
4453
+ store_product_id
4454
+ )(SELECT
4455
+ ccp.category_id,
4456
+ ccp.product_id,
4457
+ cpe.store_product_id
4458
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4459
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4460
+ ON ccp.product_id=cpe.entity_id
4461
+ WHERE store_product_id is not null
4462
+ )
4463
+ ");
4464
+
4465
+ $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp ccpfd
4466
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4467
+ ON ccpfd.store_product_id=p.store_product_id
4468
+ SET ccpfd.store_category_id=p.store_category_id
4469
+ WHERE ccpfd.store_product_id!=0
4470
+ ");
4471
+
4472
+ $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp ccpfd
4473
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
4474
+ ON ccpfd.store_category_id=scm.store_category_id
4475
+ SET ccpfd.new_category_id=scm.shop_entity_id
4476
+ WHERE ccpfd.store_category_id!=0
4477
+ ");
4478
+
4479
+ $result = $this->db_do("DELETE FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp
4480
+ WHERE category_id=new_category_id");
4481
+
4482
+ $result = $this->db_do("
4483
+ DELETE ccp
4484
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " ccp
4485
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "_for_delete_temp ccpfd
4486
+ ON ccp.product_id=ccpfd.product_id
4487
+ AND ccp.category_id=ccpfd.category_id
4488
+ ");
4489
+
4490
+ $result = $this->db_do("
4491
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " (
4492
+ category_id,
4493
+ product_id
4494
+ )(SELECT
4495
+ scm.shop_entity_id,
4496
+ cpe.entity_id
4497
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4498
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4499
+ ON cpe.store_product_id=p.store_product_id
4500
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
4501
+ ON p.store_category_id=scm.store_category_id
4502
+ )
4503
+ ON DUPLICATE KEY UPDATE
4504
+ product_id = cpe.entity_id
4505
+ ");
4506
+
4507
+
4508
+ //add multi categories
4509
+ $result = $this->db_do("
4510
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . "
4511
+ (category_id, product_id)
4512
+ (SELECT
4513
+ scm.shop_entity_id,
4514
+ cpe.entity_id
4515
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4516
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
4517
+ ON cpe.store_product_id = p.store_product_id
4518
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
4519
+ ON p.store_product_id=spc.store_product_id
4520
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping') . " scm
4521
+ ON spc.store_category_id = scm.store_category_id
4522
+ )
4523
+ ON DUPLICATE KEY UPDATE
4524
+ product_id = cpe.entity_id
4525
+ ");
4526
+
4527
+
4528
+ //Indexing products and categories in the shop
4529
+ $result = $this->db_do("DELETE ccpi
4530
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index') . " ccpi
4531
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4532
+ ON ccpi.product_id=cpe.entity_id
4533
+ WHERE cpe.entity_id IS NULL");
4534
+
4535
+ $result = $this->db_do("
4536
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index') . " (
4537
+ category_id,
4538
+ product_id,
4539
+ position,
4540
+ is_parent,
4541
+ store_id,
4542
+ visibility
4543
+ )(
4544
+ SELECT
4545
+ a.category_id,
4546
+ a.product_id,
4547
+ a.position,
4548
+ 1,
4549
+ b.store_id,
4550
+ 4
4551
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " a
4552
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " b
4553
+ )
4554
+ ON DUPLICATE KEY UPDATE
4555
+ visibility = 4
4556
+ ");
4557
+
4558
+ $result = $this->db_do("
4559
+ INSERT ignore INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index') . " (
4560
+ category_id,
4561
+ product_id,
4562
+ position,
4563
+ is_parent,
4564
+ store_id,
4565
+ visibility
4566
+ )(
4567
+ SELECT
4568
+ " . $this->_root_cat . ",
4569
+ a.product_id,
4570
+ a.position,
4571
+ 1,
4572
+ b.store_id,
4573
+ 4
4574
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_category_product') . " a
4575
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " b
4576
+ )
4577
+ ON DUPLICATE KEY UPDATE
4578
+ visibility = 4
4579
+ ");
4580
+
4581
+ //Set product name for specific web sites
4582
+ $result = $this->db_do("DELETE cpev
4583
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " cpev
4584
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4585
+ ON cpev.entity_id=cpe.entity_id
4586
+ WHERE cpe.entity_id IS NULL");
4587
+ $result = $this->db_do("
4588
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4589
+ entity_type_id,
4590
+ attribute_id,
4591
+ store_id,
4592
+ entity_id,
4593
+ value
4594
+ )(SELECT
4595
+ " . $this->_getProductEntityTypeId() . ",
4596
+ " . $this->_getProductAttributeId('name') . ",
4597
+ w.website,
4598
+ a.entity_id,
4599
+ b.product_name
4600
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4601
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4602
+ ON a.store_product_id= b.store_product_id
4603
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4604
+ ON a.store_product_id=w.store_product_id
4605
+ )
4606
+ ON DUPLICATE KEY UPDATE
4607
+ value = b.product_name
4608
+ ");
4609
+
4610
+ // product name for all web sites
4611
+ $result = $this->db_do("
4612
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4613
+ entity_type_id,
4614
+ attribute_id,
4615
+ store_id,
4616
+ entity_id,
4617
+ value
4618
+ )(
4619
+ SELECT
4620
+ " . $this->_getProductEntityTypeId() . ",
4621
+ " . $this->_getProductAttributeId('name') . ",
4622
+ 0,
4623
+ a.entity_id,
4624
+ b.product_name
4625
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4626
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4627
+ ON a.store_product_id = b.store_product_id
4628
+ )
4629
+ ON DUPLICATE KEY UPDATE
4630
+ value = b.product_name
4631
+ ");
4632
+
4633
+ $this->dropHTMLentities($this->_getProductEntityTypeId(), $this->_getProductAttributeId('name'));
4634
+ $this->addDescriptions();
4635
+ $this->cleanProductDistributors();
4636
+ if (!$this->_ignore_product_contracts) {
4637
+ $this->cleanProductContracts();
4638
+ }
4639
+ if ($this->product_file_format == "NEW") {
4640
+ $this->addReviews();
4641
+ $this->addWeight();
4642
+ $this->addSearchCache();
4643
+ $this->addPdfUrl();
4644
+ $this->addShortDescriptions();
4645
+ $this->addProductDistributors();
4646
+ if (!$this->_ignore_product_contracts) {
4647
+ $this->addProductContracts();
4648
+ }
4649
+ }
4650
+ $this->addEAN();
4651
+ $this->addSpecification();
4652
+ $this->addManufacturers();
4653
+
4654
+ //Enabling product index.
4655
+ $result = $this->db_do("DELETE cpei
4656
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index') . " cpei
4657
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4658
+ ON cpei.product_id=cpe.entity_id
4659
+ WHERE cpe.entity_id IS NULL");
4660
+
4661
+ $result = $this->db_do("
4662
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index') . " (
4663
+ product_id,
4664
+ store_id,
4665
+ visibility
4666
+ )(
4667
+ SELECT
4668
+ a.entity_id,
4669
+ w.website,
4670
+ 4
4671
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4672
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4673
+ ON a.store_product_id=w.store_product_id
4674
+ )
4675
+ ON DUPLICATE KEY UPDATE
4676
+ visibility = 4
4677
+ ");
4678
+ $result = $this->db_do("
4679
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index') . " (
4680
+ product_id,
4681
+ store_id,
4682
+ visibility
4683
+ )(
4684
+ SELECT
4685
+ a.entity_id,
4686
+ 0,
4687
+ 4
4688
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4689
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4690
+ ON a.store_product_id=w.store_product_id
4691
+ )
4692
+ ON DUPLICATE KEY UPDATE
4693
+ visibility = 4
4694
+ ");
4695
+
4696
+
4697
+ $result = $this->db_do("
4698
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4699
+ entity_type_id,
4700
+ attribute_id,
4701
+ store_id,
4702
+ entity_id,
4703
+ value
4704
+ )(
4705
+ SELECT
4706
+ " . $this->_getProductEntityTypeId() . ",
4707
+ " . $this->_getProductAttributeId('visibility') . ",
4708
+ w.website,
4709
+ a.entity_id,
4710
+ 4
4711
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4712
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4713
+ ON a.store_product_id=w.store_product_id
4714
+ )
4715
+ ON DUPLICATE KEY UPDATE
4716
+ value = 4
4717
+ ");
4718
+
4719
+ $result = $this->db_do("
4720
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4721
+ entity_type_id,
4722
+ attribute_id,
4723
+ store_id,
4724
+ entity_id,
4725
+ value
4726
+ )(
4727
+ SELECT
4728
+ " . $this->_getProductEntityTypeId() . ",
4729
+ " . $this->_getProductAttributeId('visibility') . ",
4730
+ 0,
4731
+ a.entity_id,
4732
+ 4
4733
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4734
+ )
4735
+ ON DUPLICATE KEY UPDATE
4736
+ value = 4
4737
+ ");
4738
+
4739
+ $result = $this->db_do("DELETE cpw
4740
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_website') . " cpw
4741
+ LEFT JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
4742
+ ON cpw.product_id=cpe.entity_id
4743
+ WHERE cpe.entity_id IS NULL");
4744
+
4745
+ $result = $this->db_do("
4746
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_website') . " (
4747
+ product_id,
4748
+ website_id
4749
+ )(
4750
+ SELECT a.entity_id, w.website_id
4751
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4752
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4753
+ ON a.store_product_id=w.store_product_id
4754
+ )
4755
+ ON DUPLICATE KEY UPDATE
4756
+ product_id=a.entity_id,
4757
+ website_id=w.website_id
4758
+ ");
4759
+
4760
+ //Adding tax class "Taxable Goods"
4761
+ $result = $this->db_do("
4762
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4763
+ entity_type_id,
4764
+ attribute_id,
4765
+ store_id,
4766
+ entity_id,
4767
+ value
4768
+ )(
4769
+ SELECT
4770
+ " . $this->_getProductEntityTypeId() . ",
4771
+ " . $this->_getProductAttributeId('tax_class_id') . ",
4772
+ w.website,
4773
+ a.entity_id,
4774
+ 2
4775
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4776
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
4777
+ ON a.store_product_id=w.store_product_id
4778
+ )
4779
+ ON DUPLICATE KEY UPDATE
4780
+ value = 2
4781
+ ");
4782
+ $result = $this->db_do("
4783
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int') . " (
4784
+ entity_type_id,
4785
+ attribute_id,
4786
+ store_id,
4787
+ entity_id,
4788
+ value
4789
+ )(
4790
+ SELECT
4791
+ " . $this->_getProductEntityTypeId() . ",
4792
+ " . $this->_getProductAttributeId('tax_class_id') . ",
4793
+ 0,
4794
+ a.entity_id,
4795
+ 2
4796
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4797
+ )
4798
+ ON DUPLICATE KEY UPDATE
4799
+ value = 2
4800
+ ");
4801
+
4802
+ // Load url Image
4803
+ $result = $this->db_do("
4804
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4805
+ entity_type_id,
4806
+ attribute_id,
4807
+ store_id,
4808
+ entity_id,
4809
+ value
4810
+ )(
4811
+ SELECT
4812
+ " . $this->_getProductEntityTypeId() . ",
4813
+ " . $this->_getProductAttributeId('image') . ",
4814
+ w.store_id,
4815
+ a.entity_id,
4816
+ b.main_image_url
4817
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4818
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
4819
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4820
+ ON a.store_product_id = b.store_product_id
4821
+ )
4822
+ ON DUPLICATE KEY UPDATE
4823
+ value = b.main_image_url
4824
+ ");
4825
+
4826
+ // image for specific web sites
4827
+ $result = $this->db_do("
4828
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4829
+ entity_type_id,
4830
+ attribute_id,
4831
+ store_id,
4832
+ entity_id,
4833
+ value
4834
+ )(
4835
+ SELECT
4836
+ " . $this->_getProductEntityTypeId() . ",
4837
+ " . $this->_getProductAttributeId('image') . ",
4838
+ 0,
4839
+ a.entity_id,
4840
+ b.main_image_url
4841
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4842
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4843
+ ON a.store_product_id = b.store_product_id
4844
+ )
4845
+ ON DUPLICATE KEY UPDATE
4846
+ value = b.main_image_url
4847
+ ");
4848
+
4849
+ // small_image for specific web sites
4850
+ $result = $this->db_do("
4851
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4852
+ entity_type_id,
4853
+ attribute_id,
4854
+ store_id,
4855
+ entity_id,
4856
+ value
4857
+ )(
4858
+ SELECT
4859
+ " . $this->_getProductEntityTypeId() . ",
4860
+ " . $this->_getProductAttributeId('small_image') . ",
4861
+ w.store_id,
4862
+ a.entity_id,
4863
+ b.medium_image_url
4864
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4865
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
4866
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4867
+ ON a.store_product_id = b.store_product_id
4868
+ )
4869
+ ON DUPLICATE KEY UPDATE
4870
+ value = b.medium_image_url
4871
+ ");
4872
+
4873
+ // small_image for all web sites
4874
+ $result = $this->db_do("
4875
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4876
+ entity_type_id,
4877
+ attribute_id,
4878
+ store_id,
4879
+ entity_id,
4880
+ value
4881
+ )(
4882
+ SELECT
4883
+ " . $this->_getProductEntityTypeId() . ",
4884
+ " . $this->_getProductAttributeId('small_image') . ",
4885
+ 0,
4886
+ a.entity_id,
4887
+ b.medium_image_url
4888
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4889
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
4890
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4891
+ ON a.store_product_id = b.store_product_id
4892
+ )
4893
+ ON DUPLICATE KEY UPDATE
4894
+ value = b.medium_image_url
4895
+ ");
4896
+
4897
+
4898
+ // thumbnail for specific web site
4899
+ $result = $this->db_do("
4900
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4901
+ entity_type_id,
4902
+ attribute_id,
4903
+ store_id,
4904
+ entity_id,
4905
+ value
4906
+ )(
4907
+ SELECT
4908
+ " . $this->_getProductEntityTypeId() . ",
4909
+ " . $this->_getProductAttributeId('thumbnail') . ",
4910
+ w.store_id,
4911
+ a.entity_id,
4912
+ b.thumb_image_url
4913
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4914
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
4915
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4916
+ ON a.store_product_id = b.store_product_id
4917
+ )
4918
+ ON DUPLICATE KEY UPDATE
4919
+ value = b.thumb_image_url
4920
+ ");
4921
+
4922
+ // thumbnail for all web sites
4923
+ $result = $this->db_do("
4924
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
4925
+ entity_type_id,
4926
+ attribute_id,
4927
+ store_id,
4928
+ entity_id,
4929
+ value
4930
+ )(
4931
+ SELECT
4932
+ " . $this->_getProductEntityTypeId() . ",
4933
+ " . $this->_getProductAttributeId('thumbnail') . ",
4934
+ 0,
4935
+ a.entity_id,
4936
+ b.thumb_image_url
4937
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
4938
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('core_store') . " w
4939
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
4940
+ ON a.store_product_id = b.store_product_id
4941
+ )
4942
+ ON DUPLICATE KEY UPDATE
4943
+ value = b.thumb_image_url
4944
+
4945
+ ");
4946
+
4947
+ /*STP DELETE
4948
+ //Refresh fulltext search
4949
+ $result = $this->db_do("DROP TABLE IF EXISTS ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp");
4950
+ $result = $this->db_do("CREATE TEMPORARY TABLE IF NOT EXISTS
4951
+ ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp
4952
+ LIKE ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext'));
4953
+
4954
+ $result = $this->db_do("
4955
+ INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp (
4956
+ product_id,
4957
+ store_id,
4958
+ data_index
4959
+ )(
4960
+ SELECT
4961
+ a.entity_id,
4962
+ w.website,
4963
+ CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
4964
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." a
4965
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('products_website_temp')." w
4966
+ ON a.store_product_id=w.store_product_id
4967
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product')." b
4968
+ ON a.entity_id = b.product_id
4969
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar')." c
4970
+ ON b.category_id = c.entity_id
4971
+ AND c.attribute_id = " . $this->_getCategoryAttributeId('name'). "
4972
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar')." e
4973
+ ON a.entity_id = e.entity_id
4974
+ AND e.attribute_id = " . $this->_getProductAttributeId('name'). "
4975
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_website')." j
4976
+ ON a.entity_id = j.product_id
4977
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('products_temp')." f
4978
+ ON a.store_product_id = f.store_product_id
4979
+ )
4980
+ ON DUPLICATE KEY UPDATE
4981
+ data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
4982
+ ");
4983
+
4984
+ $result = $this->db_do("
4985
+ INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp (
4986
+ product_id,
4987
+ store_id,
4988
+ data_index
4989
+ )(
4990
+ SELECT
4991
+ a.entity_id,
4992
+ w.website,
4993
+ CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
4994
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." a
4995
+ INNER JOIN ".Mage::getSingleton('core/resource')->getTableName('products_website_temp')." w
4996
+ ON a.store_product_id=w.store_product_id
4997
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_product')." b
4998
+ ON a.entity_id = b.product_id
4999
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar')." c
5000
+ ON b.category_id = c.entity_id
5001
+ AND c.attribute_id = " . $this->_getCategoryAttributeId('name'). "
5002
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar')." e
5003
+ ON a.entity_id = e.entity_id
5004
+ AND e.attribute_id = " . $this->_getProductAttributeId('name'). "
5005
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('products_temp')." f
5006
+ ON a.store_product_id = f.store_product_id
5007
+ )
5008
+ ON DUPLICATE KEY UPDATE
5009
+ data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
5010
+ ");
5011
+
5012
+ $result = $this->db_do("DELETE cf
5013
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')." cf
5014
+ LEFT JOIN ".Mage::getSingleton('core/resource')->getTableName('catalog_product_entity')." cpe
5015
+ ON cf.product_id=cpe.entity_id
5016
+ WHERE cpe.entity_id is null");
5017
+
5018
+ $result = $this->db_do("
5019
+ INSERT INTO ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')." (
5020
+ product_id,
5021
+ store_id,
5022
+ data_index
5023
+ )(
5024
+ SELECT
5025
+ a.product_id,
5026
+ a.store_id,
5027
+ a.data_index
5028
+ FROM ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext')."_tmp a
5029
+ WHERE product_id = a.product_id
5030
+ )
5031
+ ON DUPLICATE KEY UPDATE
5032
+ data_index = a.data_index
5033
+ ");
5034
+
5035
+ $this->db_do("UPDATE ".Mage::getSingleton('core/resource')->getTableName('catalogsearch_query')." SET is_processed = 0");
5036
+ //INNER JOIN eav_attribute_option_value d ON a.vendor_id = d.option_id
5037
+ //TODO add something else
5038
+ STP DELETE*/
5039
+ $this->addRelatedProducts();
5040
+ }
5041
+
5042
+ private function _getProductDefaulAttributeSetId()
5043
+ {
5044
+ if (!$this->defaultAttributeSetId) {
5045
+ $sql = "
5046
+ SELECT entity_type_id, default_attribute_set_id
5047
+ FROM " . Mage::getSingleton('core/resource')->getTableName('eav_entity_type') . "
5048
+ WHERE entity_type_code = 'catalog_product'
5049
+ LIMIT 1
5050
+ ";
5051
+ $result = $this->db_do($sql);
5052
+ if ($row = mysqli_fetch_assoc($result)) {
5053
+
5054
+ $this->defaultAttributeSetId = $row['default_attribute_set_id'];
5055
+ }
5056
+ }
5057
+ return $this->defaultAttributeSetId;
5058
+ }
5059
+
5060
+
5061
+ function dropHTMLentities($entity_type_id, $attribute_id)
5062
+ {
5063
+ // product name for all web sites
5064
+ $result = $this->db_do("
5065
+ SELECT value, entity_id
5066
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5067
+ WHERE entity_type_id=" . $entity_type_id . "
5068
+ AND attribute_id=" . $attribute_id
5069
+ );
5070
+ while ($row = mysqli_fetch_array($result)) {
5071
+ $value = $this->valid_char($row['value']);
5072
+ if ($value != '' and $value != $row['value']) {
5073
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5074
+ SET value='" . mysqli_real_escape_string($this->db, $value) . "'
5075
+ WHERE entity_id=" . $row['entity_id'] . "
5076
+ AND entity_type_id=" . $entity_type_id . "
5077
+ AND attribute_id=" . $attribute_id);
5078
+ }
5079
+ }
5080
+ }
5081
+
5082
+ function valid_char($string)
5083
+ {
5084
+ $string = preg_replace('/&#8482;/', ' ', $string);
5085
+ $string = preg_replace('/&reg;/', ' ', $string);
5086
+ $string = preg_replace('/&asymp;/', ' ', $string);
5087
+ $string = preg_replace('/&quot;/', ' ', $string);
5088
+ $string = preg_replace('/&prime;/', ' ', $string);
5089
+ $string = preg_replace('/&deg;/', ' ', $string);
5090
+ $string = preg_replace('/&plusmn;/', ' ', $string);
5091
+ $string = preg_replace('/&micro;/', ' ', $string);
5092
+ $string = preg_replace('/&sup2;/', ' ', $string);
5093
+ $string = preg_replace('/&sup3;/', ' ', $string);
5094
+ return $string;
5095
+ }
5096
+
5097
+ function addDescriptions()
5098
+ {
5099
+ // product description for all web sites
5100
+ $result = $this->db_do("
5101
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5102
+ entity_type_id,
5103
+ attribute_id,
5104
+ store_id,
5105
+ entity_id,
5106
+ value
5107
+ )(
5108
+ SELECT
5109
+ " . $this->_getProductEntityTypeId() . ",
5110
+ " . $this->_getProductAttributeId('description') . ",
5111
+ w.website,
5112
+ a.entity_id,
5113
+ b.description
5114
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5115
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5116
+ ON a.store_product_id = b.store_product_id
5117
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5118
+ ON a.store_product_id=w.store_product_id
5119
+ )
5120
+ ON DUPLICATE KEY UPDATE
5121
+ value = b.description
5122
+ ");
5123
+
5124
+ // product description for all web sites
5125
+ $result = $this->db_do("
5126
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5127
+ entity_type_id,
5128
+ attribute_id,
5129
+ store_id,
5130
+ entity_id,
5131
+ value
5132
+ )(
5133
+ SELECT
5134
+ " . $this->_getProductEntityTypeId() . ",
5135
+ " . $this->_getProductAttributeId('description') . ",
5136
+ 0,
5137
+ a.entity_id,
5138
+ b.description
5139
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5140
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5141
+ ON a.store_product_id = b.store_product_id
5142
+ )
5143
+ ON DUPLICATE KEY UPDATE
5144
+ value = b.description
5145
+ ");
5146
+ }
5147
+
5148
+ function cleanProductDistributors()
5149
+ {
5150
+ for ($i = 1; $i <= 5; $i++) {
5151
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5152
+ SET value = ''
5153
+ WHERE entity_type_id=" . $this->_getProductEntityTypeId() . " AND attribute_id=" . $this->_getProductAttributeId('supplier_' . $i));
5154
+ }
5155
+ }
5156
+
5157
+ function cleanProductContracts()
5158
+ {
5159
+ if ($contractIdAttr = $this->_getProductAttributeId('contract_id')) {
5160
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . "
5161
+ SET value = ''
5162
+ WHERE entity_type_id=" . $this->_getProductEntityTypeId() . " AND attribute_id=" . $contractIdAttr);
5163
+ }
5164
+ }
5165
+
5166
+ function addReviews()
5167
+ {
5168
+ // product reviews for all web sites
5169
+ $result = $this->db_do("
5170
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5171
+ entity_type_id,
5172
+ attribute_id,
5173
+ store_id,
5174
+ entity_id,
5175
+ value
5176
+ )(
5177
+ SELECT
5178
+ " . $this->_getProductEntityTypeId() . ",
5179
+ " . $this->_getProductAttributeId('reviews') . ",
5180
+ w.website,
5181
+ a.entity_id,
5182
+ b.Reviews
5183
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5184
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5185
+ ON a.store_product_id = b.store_product_id
5186
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5187
+ ON a.store_product_id=w.store_product_id
5188
+ )
5189
+ ON DUPLICATE KEY UPDATE
5190
+ value = b.Reviews
5191
+ ");
5192
+
5193
+ // product Reviews for all web sites
5194
+ $result = $this->db_do("
5195
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5196
+ entity_type_id,
5197
+ attribute_id,
5198
+ store_id,
5199
+ entity_id,
5200
+ value
5201
+ )(
5202
+ SELECT
5203
+ " . $this->_getProductEntityTypeId() . ",
5204
+ " . $this->_getProductAttributeId('reviews') . ",
5205
+ 0,
5206
+ a.entity_id,
5207
+ b.Reviews
5208
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5209
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5210
+ ON a.store_product_id = b.store_product_id
5211
+ )
5212
+ ON DUPLICATE KEY UPDATE
5213
+ value = b.Reviews
5214
+ ");
5215
+ }
5216
+
5217
+ function addWeight()
5218
+ {
5219
+ // product weight for specific web site
5220
+ $result = $this->db_do("
5221
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
5222
+ entity_type_id,
5223
+ attribute_id,
5224
+ store_id,
5225
+ entity_id,
5226
+ value
5227
+ )(
5228
+ SELECT
5229
+ " . $this->_getProductEntityTypeId() . ",
5230
+ " . $this->_getProductAttributeId('weight') . ",
5231
+ w.website,
5232
+ a.entity_id,
5233
+ b.Weight
5234
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5235
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5236
+ ON a.store_product_id = b.store_product_id
5237
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5238
+ ON a.store_product_id=w.store_product_id
5239
+ )
5240
+ ON DUPLICATE KEY UPDATE
5241
+ value = b.Weight
5242
+ ");
5243
+ // product weight for all web sites
5244
+ $result = $this->db_do("
5245
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_decimal') . " (
5246
+ entity_type_id,
5247
+ attribute_id,
5248
+ store_id,
5249
+ entity_id,
5250
+ value
5251
+ )(
5252
+ SELECT
5253
+ " . $this->_getProductEntityTypeId() . ",
5254
+ " . $this->_getProductAttributeId('weight') . ",
5255
+ 0,
5256
+ a.entity_id,
5257
+ b.Weight
5258
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5259
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5260
+ ON a.store_product_id = b.store_product_id
5261
+ )
5262
+ ON DUPLICATE KEY UPDATE
5263
+ value = b.Weight
5264
+
5265
+
5266
+ ");
5267
+ }
5268
+
5269
+ function addSearchCache()
5270
+ {
5271
+ // product search_cache for all web sites
5272
+ $result = $this->db_do("
5273
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5274
+ entity_type_id,
5275
+ attribute_id,
5276
+ store_id,
5277
+ entity_id,
5278
+ value
5279
+ )(
5280
+ SELECT
5281
+ " . $this->_getProductEntityTypeId() . ",
5282
+ " . $this->_getProductAttributeId('sinch_search_cache') . ",
5283
+ w.website,
5284
+ a.entity_id,
5285
+ b.search_cache
5286
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5287
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5288
+ ON a.store_product_id = b.store_product_id
5289
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5290
+ ON a.store_product_id=w.store_product_id
5291
+ )
5292
+ ON DUPLICATE KEY UPDATE
5293
+ value = b.search_cache
5294
+ ");
5295
+
5296
+ // product search_cache for all web sites
5297
+ $result = $this->db_do("
5298
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5299
+ entity_type_id,
5300
+ attribute_id,
5301
+ store_id,
5302
+ entity_id,
5303
+ value
5304
+ )(
5305
+ SELECT
5306
+ " . $this->_getProductEntityTypeId() . ",
5307
+ " . $this->_getProductAttributeId('sinch_search_cache') . ",
5308
+ 0,
5309
+ a.entity_id,
5310
+ b.search_cache
5311
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5312
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5313
+ ON a.store_product_id = b.store_product_id
5314
+ )
5315
+ ON DUPLICATE KEY UPDATE
5316
+ value = b.search_cache
5317
+ ");
5318
+ }
5319
+
5320
+ function addPdfUrl()
5321
+ {
5322
+ // product PDF Url for all web sites
5323
+ $result = $this->db_do("
5324
+ UPDATE " . Mage::getSingleton('core/resource')->getTableName('products_temp') . "
5325
+ SET pdf_url = CONCAT(
5326
+ '<a href=\"#\" onclick=\"popWin(',
5327
+ \"'\",
5328
+ pdf_url,
5329
+ \"'\",
5330
+ \", 'pdf', 'width=500,height=800,left=50,top=50, location=no,status=yes,scrollbars=yes,resizable=yes'); return false;\",
5331
+ '\"',
5332
+ '>',
5333
+ pdf_url,
5334
+ '</a>')
5335
+ WHERE pdf_url != ''
5336
+ ");
5337
+ $result = $this->db_do("
5338
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5339
+ entity_type_id,
5340
+ attribute_id,
5341
+ store_id,
5342
+ entity_id,
5343
+ value
5344
+ )(
5345
+ SELECT
5346
+ " . $this->_getProductEntityTypeId() . ",
5347
+ " . $this->_getProductAttributeId('pdf_url') . ",
5348
+ w.website,
5349
+ a.entity_id,
5350
+ b.pdf_url
5351
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5352
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5353
+ ON a.store_product_id = b.store_product_id
5354
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5355
+ ON a.store_product_id=w.store_product_id
5356
+ )
5357
+ ON DUPLICATE KEY UPDATE
5358
+ value = b.pdf_url
5359
+ ");
5360
+ // product PDF url for all web sites
5361
+ $result = $this->db_do("
5362
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5363
+ entity_type_id,
5364
+ attribute_id,
5365
+ store_id,
5366
+ entity_id,
5367
+ value
5368
+ )(
5369
+ SELECT
5370
+ " . $this->_getProductEntityTypeId() . ",
5371
+ " . $this->_getProductAttributeId('pdf_url') . ",
5372
+ 0,
5373
+ a.entity_id,
5374
+ b.pdf_url
5375
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5376
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5377
+ ON a.store_product_id = b.store_product_id
5378
+ )
5379
+ ON DUPLICATE KEY UPDATE
5380
+ value = b.pdf_url
5381
+ ");
5382
+ }
5383
+
5384
+ function addShortDescriptions()
5385
+ {
5386
+ // product short description for all web sites
5387
+ $result = $this->db_do("
5388
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5389
+ entity_type_id,
5390
+ attribute_id,
5391
+ store_id,
5392
+ entity_id,
5393
+ value
5394
+ )(
5395
+ SELECT
5396
+ " . $this->_getProductEntityTypeId() . ",
5397
+ " . $this->_getProductAttributeId('short_description') . ",
5398
+ w.website,
5399
+ a.entity_id,
5400
+ b.product_short_description
5401
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5402
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5403
+ ON a.store_product_id = b.store_product_id
5404
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5405
+ ON a.store_product_id=w.store_product_id
5406
+ )
5407
+ ON DUPLICATE KEY UPDATE
5408
+ value = b.product_short_description
5409
+ ");
5410
+ // product short description for all web sites
5411
+ $result = $this->db_do("
5412
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5413
+ entity_type_id,
5414
+ attribute_id,
5415
+ store_id,
5416
+ entity_id,
5417
+ value
5418
+ )(
5419
+ SELECT
5420
+ " . $this->_getProductEntityTypeId() . ",
5421
+ " . $this->_getProductAttributeId('short_description') . ",
5422
+ 0,
5423
+ a.entity_id,
5424
+ b.product_short_description
5425
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5426
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5427
+ ON a.store_product_id = b.store_product_id
5428
+ )
5429
+ ON DUPLICATE KEY UPDATE
5430
+ value = b.product_short_description
5431
+ ");
5432
+ }
5433
+
5434
+ function addProductDistributors()
5435
+ {
5436
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary'));
5437
+ $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . "
5438
+ LIKE " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
5439
+ $this->db_do("INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . " SELECT * FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
5440
+ for ($i = 1; $i <= 5; $i++) {
5441
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier'));
5442
+ $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . "
5443
+ LIKE " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price'));
5444
+ $this->db_do("INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " SELECT * FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . " GROUP BY store_product_id");
5445
+
5446
+ // product Distributors for all web sites
5447
+ $result = $this->db_do("
5448
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5449
+ entity_type_id,
5450
+ attribute_id,
5451
+ store_id,
5452
+ entity_id,
5453
+ value
5454
+ )(
5455
+ SELECT
5456
+ " . $this->_getProductEntityTypeId() . ",
5457
+ " . $this->_getProductAttributeId('supplier_' . $i) . ",
5458
+ w.website,
5459
+ a.entity_id,
5460
+ d.distributor_name
5461
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5462
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " b
5463
+ ON a.store_product_id = b.store_product_id
5464
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors') . " d
5465
+ ON b.distributor_id = d.distributor_id
5466
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5467
+ ON a.store_product_id=w.store_product_id
5468
+ )
5469
+ ON DUPLICATE KEY UPDATE
5470
+ value = d.distributor_name
5471
+ ");
5472
+ // product Distributors for all web sites
5473
+ $result = $this->db_do("
5474
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5475
+ entity_type_id,
5476
+ attribute_id,
5477
+ store_id,
5478
+ entity_id,
5479
+ value
5480
+ )(
5481
+ SELECT
5482
+ " . $this->_getProductEntityTypeId() . ",
5483
+ " . $this->_getProductAttributeId('supplier_' . $i) . ",
5484
+ 0,
5485
+ a.entity_id,
5486
+ d.distributor_name
5487
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5488
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " b
5489
+ ON a.store_product_id = b.store_product_id
5490
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors') . " d
5491
+ ON b.distributor_id = d.distributor_id
5492
+ )
5493
+ ON DUPLICATE KEY UPDATE
5494
+ value = d.distributor_name
5495
+ ");
5496
+
5497
+ $this->db_do("DELETE sdsapt FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary') . " sdsapt JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_distributors_stock_and_price_temporary_supplier') . " sdsapts ON sdsapt.store_product_id = sdsapts.store_product_id AND sdsapt.distributor_id = sdsapts.distributor_id");
5498
+ }
5499
+ }
5500
+
5501
+ function addProductContracts()
5502
+ {
5503
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary'));
5504
+ $this->db_do("CREATE TABLE IF NOT EXISTS " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . "(
5505
+ `store_product_id` int(11) DEFAULT NULL,
5506
+ `contract_id_str` varchar(255) DEFAULT NULL,
5507
+ KEY `store_product_id` (store_product_id)
5508
+ )
5509
+ ");
5510
+ $this->db_do("INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . " SELECT store_product_id, group_concat(contract_id) FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts') . " GROUP BY store_product_id");
5511
+ // product Distributors for all web sites
5512
+ $result = $this->db_do("
5513
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5514
+ entity_type_id,
5515
+ attribute_id,
5516
+ store_id,
5517
+ entity_id,
5518
+ value
5519
+ )(
5520
+ SELECT
5521
+ " . $this->_getProductEntityTypeId() . ",
5522
+ " . $this->_getProductAttributeId('contract_id') . ",
5523
+ w.website,
5524
+ a.entity_id,
5525
+ b.contract_id_str
5526
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5527
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . " b
5528
+ ON a.store_product_id = b.store_product_id
5529
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5530
+ ON a.store_product_id=w.store_product_id
5531
+ )
5532
+ ON DUPLICATE KEY UPDATE
5533
+ value = b.contract_id_str
5534
+ ");
5535
+ // product Distributors for all web sites
5536
+ $result = $this->db_do("
5537
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5538
+ entity_type_id,
5539
+ attribute_id,
5540
+ store_id,
5541
+ entity_id,
5542
+ value
5543
+ )(
5544
+ SELECT
5545
+ " . $this->_getProductEntityTypeId() . ",
5546
+ " . $this->_getProductAttributeId('contract_id') . ",
5547
+ 0,
5548
+ a.entity_id,
5549
+ b.contract_id_str
5550
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5551
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_contracts_temporary') . " b
5552
+ ON a.store_product_id = b.store_product_id
5553
+ )
5554
+ ON DUPLICATE KEY UPDATE
5555
+ value = b.contract_id_str
5556
+ ");
5557
+ }
5558
+
5559
+ function addEAN()
5560
+ {
5561
+ //gather EAN codes for each product
5562
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('EANs_temp'));
5563
+ $this->db_do("
5564
+ CREATE TEMPORARY TABLE " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " (
5565
+ sinch_product_id int(11),
5566
+ store_product_id int(11),
5567
+ EANs text,
5568
+ KEY `sinch_product_id` (`sinch_product_id`),
5569
+ KEY `store_product_id` (`store_product_id`)
5570
+ )
5571
+ ");
5572
+ $this->db_do("
5573
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " (
5574
+ sinch_product_id,
5575
+ EANs
5576
+ )(SELECT
5577
+ sec.product_id,
5578
+ GROUP_CONCAT(DISTINCT ean_code ORDER BY ean_code DESC SEPARATOR ', ') AS eans
5579
+ FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_ean_codes') . " sec
5580
+ GROUP BY sec.product_id
5581
+ )
5582
+ ");
5583
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " e
5584
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
5585
+ ON e.sinch_product_id=p.sinch_product_id
5586
+ SET e.store_product_id=p.store_product_id");
5587
+ // product EANs for all web sites
5588
+ $result = $this->db_do("
5589
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5590
+ entity_type_id,
5591
+ attribute_id,
5592
+ store_id,
5593
+ entity_id,
5594
+ value
5595
+ )(
5596
+ SELECT
5597
+ " . $this->_getProductEntityTypeId() . ",
5598
+ " . $this->_getProductAttributeId('ean') . ",
5599
+ w.website,
5600
+ a.entity_id,
5601
+ e.EANs
5602
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5603
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " e
5604
+ ON a.store_product_id = e.store_product_id
5605
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5606
+ ON a.store_product_id=w.store_product_id
5607
+ )
5608
+ ON DUPLICATE KEY UPDATE
5609
+ value = e.EANs
5610
+ ");
5611
+
5612
+ // product EANs for all web sites
5613
+ $result = $this->db_do("
5614
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar') . " (
5615
+ entity_type_id,
5616
+ attribute_id,
5617
+ store_id,
5618
+ entity_id,
5619
+ value
5620
+ )(
5621
+ SELECT
5622
+ " . $this->_getProductEntityTypeId() . ",
5623
+ " . $this->_getProductAttributeId('ean') . ",
5624
+ 0,
5625
+ a.entity_id,
5626
+ e.EANs
5627
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5628
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('EANs_temp') . " e
5629
+ ON a.store_product_id = e.store_product_id
5630
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5631
+ ON a.store_product_id=w.store_product_id
5632
+ )
5633
+ ON DUPLICATE KEY UPDATE
5634
+ value = e.EANs
5635
+ ");
5636
+
5637
+ }
5638
+
5639
+ function addSpecification()
5640
+ {
5641
+ // product specification for all web sites
5642
+ $result = $this->db_do("
5643
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5644
+ entity_type_id,
5645
+ attribute_id,
5646
+ store_id,
5647
+ entity_id,
5648
+ value
5649
+ )(
5650
+ SELECT
5651
+ " . $this->_getProductEntityTypeId() . ",
5652
+ " . $this->_getProductAttributeId('specification') . ",
5653
+ w.website,
5654
+ a.entity_id,
5655
+ b.specifications
5656
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5657
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5658
+ ON a.store_product_id = b.store_product_id
5659
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_website_temp') . " w
5660
+ ON a.store_product_id=w.store_product_id
5661
+ )
5662
+ ON DUPLICATE KEY UPDATE
5663
+ value = b.specifications
5664
+ ");
5665
+ // product specification for all web sites
5666
+ $result = $this->db_do("
5667
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_text') . " (
5668
+ entity_type_id,
5669
+ attribute_id,
5670
+ store_id,
5671
+ entity_id,
5672
+ value
5673
+ )(
5674
+ SELECT
5675
+ " . $this->_getProductEntityTypeId() . ",
5676
+ " . $this->_getProductAttributeId('specification') . ",
5677
+ 0,
5678
+ a.entity_id,
5679
+ b.specifications
5680
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " a
5681
+ INNER JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " b
5682
+ ON a.store_product_id = b.store_product_id
5683
+ )
5684
+ ON DUPLICATE KEY UPDATE
5685
+ value = b.specifications
5686
+ ");
5687
+ }
5688
+
5689
+ function addRelatedProducts()
5690
+ {
5691
+
5692
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . " rpt
5693
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
5694
+ ON rpt.sinch_product_id=p.sinch_product_id
5695
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
5696
+ ON p.store_product_id=cpe.store_product_id
5697
+ SET rpt.store_product_id=p.store_product_id, rpt.entity_id=cpe.entity_id");
5698
+
5699
+ $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . " rpt
5700
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('products_temp') . " p
5701
+ ON rpt.related_sinch_product_id=p.sinch_product_id
5702
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_entity') . " cpe
5703
+ ON p.store_product_id=cpe.store_product_id
5704
+ SET rpt.store_related_product_id=p.store_product_id, rpt.related_entity_id=cpe.entity_id");
5705
+
5706
+ $result = $this->db_do("SELECT
5707
+ link_type_id,
5708
+ code
5709
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_type')
5710
+ );
5711
+ $link_type = array();
5712
+ while ($row = mysqli_fetch_array($result)) {
5713
+ $link_type[$row['code']] = $row['link_type_id'];
5714
+ }
5715
+
5716
+ $result = $this->db_do("
5717
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link') . " (
5718
+ product_id,
5719
+ linked_product_id,
5720
+ link_type_id
5721
+ )(
5722
+ SELECT
5723
+ entity_id,
5724
+ related_entity_id,
5725
+ " . $link_type['relation'] . "
5726
+ FROM " . Mage::getSingleton('core/resource')->getTableName('stINch_related_products') . "
5727
+ WHERE store_product_id IS NOT NULL
5728
+ AND store_related_product_id IS NOT NULL
5729
+ )
5730
+ ON DUPLICATE KEY UPDATE
5731
+ product_id = entity_id,
5732
+ linked_product_id = related_entity_id
5733
+ ");
5734
+ $this->db_do("DROP TABLE IF EXISTS " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp");
5735
+
5736
+ $this->db_do("CREATE TEMPORARY TABLE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp (
5737
+ `value_id` int(11) default NULL,
5738
+ `product_link_attribute_id` smallint(6) unsigned default NULL,
5739
+ `link_id` int(11) unsigned default NULL,
5740
+ `value` int(11) NOT NULL default '0',
5741
+ KEY `FK_INT_PRODUCT_LINK_ATTRIBUTE` (`product_link_attribute_id`),
5742
+ KEY `FK_INT_PRODUCT_LINK` (`link_id`)
5743
+ )
5744
+ ");
5745
+
5746
+ $result = $this->db_do("
5747
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp(
5748
+ product_link_attribute_id,
5749
+ link_id,
5750
+ value
5751
+ )(
5752
+ SELECT
5753
+ 2,
5754
+ cpl.link_id,
5755
+ 0
5756
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link') . " cpl
5757
+ )
5758
+ ");
5759
+
5760
+ $result = $this->db_do("UPDATE " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp ct
5761
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . " c
5762
+ ON ct.link_id=c.link_id
5763
+ SET ct.value_id=c.value_id
5764
+ WHERE c.product_link_attribute_id=2
5765
+ ");
5766
+
5767
+ $result = $this->db_do("
5768
+ INSERT INTO " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . " (
5769
+ value_id,
5770
+ product_link_attribute_id,
5771
+ link_id,
5772
+ value
5773
+ )(
5774
+ SELECT
5775
+ value_id,
5776
+ product_link_attribute_id,
5777
+ link_id,
5778
+ value
5779
+ FROM " . Mage::getSingleton('core/resource')->getTableName('catalog_product_link_attribute_int') . "_tmp ct
5780
+ )
5781
+ ON DUPLICATE KEY UPDATE
5782
+ link_id=ct.link_id
5783
+
5784
+ ");
5785
+ }
5786
+
5787
+ public function replaceMagentoProductsMultistore($coincidence)
5788
+ {
5789
+ echo("\n replaceMagentoProductsMultistore 1\n");
5790
+
5791
+ $connection = Mage::getModel('core/resource')->getConnection('core_write');
5792
+
5793
+ $products_temp = Mage::getSingleton('core/resource')->getTableName('products_temp');
5794
+ $products_website_temp = Mage::getSingleton('core/resource')->getTableName('products_website_temp');
5795
+ $catalog_product_entity = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
5796
+ $catalog_product_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int');
5797
+ $catalog_product_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar');
5798
+ $catalog_category_product = Mage::getSingleton('core/resource')->getTableName('catalog_category_product');
5799
+ $stINch_products_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping');
5800
+ $catalog_category_entity = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity');
5801
+ $stINch_categories_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping');
5802
+ $catalog_category_product_index = Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index');
5803
+ $core_store = Mage::getSingleton('core/resource')->getTableName('core_store');
5804
+ $catalog_product_enabled_index = Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index');
5805
+ $catalog_product_website = Mage::getSingleton('core/resource')->getTableName('catalog_product_website');
5806
+ //$catalogsearch_fulltext = Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext');
5807
+ //$catalogsearch_query = Mage::getSingleton('core/resource')->getTableName('catalogsearch_query');
5808
+ $catalog_category_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar');
5809
+
5810
+ $_getProductEntityTypeId = $this->_getProductEntityTypeId();
5811
+ $_defaultAttributeSetId = $this->_getProductDefaulAttributeSetId();
5812
+
5813
+ $attr_atatus = $this->_getProductAttributeId('status');
5814
+ $attr_name = $this->_getProductAttributeId('name');
5815
+ $attr_visibility = $this->_getProductAttributeId('visibility');
5816
+ $attr_tax_class_id = $this->_getProductAttributeId('tax_class_id');
5817
+ $attr_image = $this->_getProductAttributeId('image');
5818
+ $attr_small_image = $this->_getProductAttributeId('small_image');
5819
+ $attr_thumbnail = $this->_getProductAttributeId('thumbnail');
5820
+
5821
+ $cat_attr_name = $this->_getCategoryAttributeId('name');
5822
+ echo("\n replaceMagentoProductsMultistore 2\n");
5823
+
5824
+ //clear products, inserting new products and updating old others.
5825
+ $query = "
5826
+ DELETE cpe
5827
+ FROM $catalog_product_entity cpe
5828
+ JOIN $stINch_products_mapping pm
5829
+ ON cpe.entity_id = pm.entity_id
5830
+ WHERE pm.shop_store_product_id IS NOT NULL
5831
+ AND pm.store_product_id IS NULL";
5832
+ $result = $this->db_do($query);
5833
+
5834
+
5835
+ echo("\n replaceMagentoProductsMultistore 3\n");
5836
+
5837
+ $result = $this->db_do("
5838
+ INSERT INTO $catalog_product_entity
5839
+ (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
5840
+ (SELECT
5841
+ pm.entity_id,
5842
+ $_getProductEntityTypeId,
5843
+ $_defaultAttributeSetId,
5844
+ 'simple',
5845
+ a.product_sku,
5846
+ NOW(),
5847
+ 0,
5848
+ a.store_product_id,
5849
+ a.sinch_product_id
5850
+ FROM $products_temp a
5851
+ LEFT JOIN $stINch_products_mapping pm
5852
+ ON a.store_product_id = pm.store_product_id
5853
+ AND a.sinch_product_id = pm.sinch_product_id
5854
+ WHERE pm.entity_id IS NULL
5855
+ )
5856
+ ON DUPLICATE KEY UPDATE
5857
+ sku = a.product_sku,
5858
+ store_product_id = a.store_product_id,
5859
+ sinch_product_id = a.sinch_product_id");
5860
+ // store_product_id = a.store_product_id,
5861
+ // sinch_product_id = a.sinch_product_id
5862
+
5863
+ $result = $this->db_do("
5864
+ INSERT INTO $catalog_product_entity
5865
+ (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
5866
+ (SELECT
5867
+ pm.entity_id,
5868
+ $_getProductEntityTypeId,
5869
+ $_defaultAttributeSetId,
5870
+ 'simple',
5871
+ a.product_sku,
5872
+ NOW(),
5873
+ 0,
5874
+ a.store_product_id,
5875
+ a.sinch_product_id
5876
+ FROM $products_temp a
5877
+ LEFT JOIN $stINch_products_mapping pm
5878
+ ON a.store_product_id = pm.store_product_id
5879
+ AND a.sinch_product_id = pm.sinch_product_id
5880
+ WHERE pm.entity_id IS NOT NULL
5881
+ )
5882
+ ON DUPLICATE KEY UPDATE
5883
+ sku = a.product_sku,
5884
+ store_product_id = a.store_product_id,
5885
+ sinch_product_id = a.sinch_product_id");
5886
+ // store_product_id = a.store_product_id,
5887
+ // sinch_product_id = a.sinch_product_id
5888
+
5889
+ echo("\n replaceMagentoProductsMultistore 4\n");
5890
+
5891
+ //Set enabled
5892
+ $result = $this->db_do("
5893
+ DELETE cpei
5894
+ FROM $catalog_product_entity_int cpei
5895
+ LEFT JOIN $catalog_product_entity cpe
5896
+ ON cpei.entity_id = cpe.entity_id
5897
+ WHERE cpe.entity_id IS NULL");
5898
+
5899
+ $result = $this->db_do("
5900
+ INSERT INTO $catalog_product_entity_int
5901
+ (entity_type_id, attribute_id, store_id, entity_id, value)
5902
+ (SELECT
5903
+ $_getProductEntityTypeId,
5904
+ $attr_atatus,
5905
+ w.website,
5906
+ a.entity_id,
5907
+ 1
5908
+ FROM $catalog_product_entity a
5909
+ JOIN $products_website_temp w
5910
+ ON a.store_product_id = w.store_product_id
5911
+ )
5912
+ ON DUPLICATE KEY UPDATE
5913
+ value = 1");
5914
+
5915
+ echo("\n replaceMagentoProductsMultistore 5\n");
5916
+
5917
+ // set status = 1 for all stores
5918
+ $result = $this->db_do("
5919
+ INSERT INTO $catalog_product_entity_int
5920
+ (entity_type_id, attribute_id, store_id, entity_id, value)
5921
+ (SELECT
5922
+ $_getProductEntityTypeId,
5923
+ $attr_atatus,
5924
+ 0,
5925
+ a.entity_id,
5926
+ 1
5927
+ FROM $catalog_product_entity a
5928
+ )
5929
+ ON DUPLICATE KEY UPDATE
5930
+ value = 1");
5931
+
5932
+ echo("\n replaceMagentoProductsMultistore 6\n");
5933
+
5934
+ //Unifying products with categories.
5935
+ $result = $this->db_do("
5936
+ DELETE ccp
5937
+ FROM $catalog_category_product ccp
5938
+ LEFT JOIN $catalog_product_entity cpe
5939
+ ON ccp.product_id = cpe.entity_id
5940
+ WHERE cpe.entity_id IS NULL");
5941
+
5942
+ echo("\n replaceMagentoProductsMultistore 7\n");
5943
+
5944
+ $root_cats = Mage::getSingleton('core/resource')->getTableName('root_cats');
5945
+
5946
+ $result = $this->db_do("DROP TABLE IF EXISTS $root_cats");
5947
+ $result = $this->db_do("
5948
+ CREATE TABLE $root_cats
5949
+ SELECT
5950
+ entity_id,
5951
+ path,
5952
+ SUBSTRING(path, LOCATE('/', path)+1) AS short_path,
5953
+ LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1)) AS end_pos,
5954
+ SUBSTRING(SUBSTRING(path, LOCATE('/', path)+1), 1, LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1))-1) as root_cat
5955
+ FROM $catalog_category_entity
5956
+ ");
5957
+ $result = $this->db_do("UPDATE $root_cats SET root_cat = entity_id WHERE CHAR_LENGTH(root_cat) = 0");
5958
+
5959
+ echo("\n replaceMagentoProductsMultistore 8\n");
5960
+
5961
+ $result = $this->db_do("
5962
+ UPDATE IGNORE $catalog_category_product ccp
5963
+ LEFT JOIN $catalog_category_entity cce
5964
+ ON ccp.category_id = cce.entity_id
5965
+ JOIN $root_cats rc
5966
+ ON cce.entity_id = rc.entity_id
5967
+ SET ccp.category_id = rc.root_cat
5968
+ WHERE cce.entity_id IS NULL");
5969
+
5970
+
5971
+ echo("\n replaceMagentoProductsMultistore 9\n");
5972
+
5973
+
5974
+ $result = $this->db_do("
5975
+ DELETE ccp
5976
+ FROM $catalog_category_product ccp
5977
+ LEFT JOIN $catalog_category_entity cce
5978
+ ON ccp.category_id = cce.entity_id
5979
+ WHERE cce.entity_id IS NULL");
5980
+
5981
+ echo("\n replaceMagentoProductsMultistore 10\n");
5982
+
5983
+ $catalog_category_product_for_delete_temp = $catalog_category_product . "_for_delete_temp";
5984
+
5985
+ // TEMPORARY
5986
+ $this->db_do(" DROP TABLE IF EXISTS $catalog_category_product_for_delete_temp");
5987
+ $this->db_do("
5988
+ CREATE TABLE $catalog_category_product_for_delete_temp
5989
+ (
5990
+ `category_id` int(10) unsigned NOT NULL default '0',
5991
+ `product_id` int(10) unsigned NOT NULL default '0',
5992
+ `store_product_id` int(10) NOT NULL default '0',
5993
+ `store_category_id` int(10) NOT NULL default '0',
5994
+ `new_category_id` int(10) NOT NULL default '0',
5995
+
5996
+ UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`category_id`,`product_id`),
5997
+ KEY `CATALOG_CATEGORY_PRODUCT_CATEGORY` (`category_id`),
5998
+ KEY `CATALOG_CATEGORY_PRODUCT_PRODUCT` (`product_id`),
5999
+ KEY `CATALOG_NEW_CATEGORY_PRODUCT_CATEGORY` (`new_category_id`)
6000
+ )");
6001
+
6002
+ echo("\n replaceMagentoProductsMultistore 11\n");
6003
+
6004
+ $result = $this->db_do("
6005
+ INSERT INTO $catalog_category_product_for_delete_temp
6006
+ (category_id, product_id, store_product_id)
6007
+ (SELECT
6008
+ ccp.category_id,
6009
+ ccp.product_id,
6010
+ cpe.store_product_id
6011
+ FROM $catalog_category_product ccp
6012
+ JOIN $catalog_product_entity cpe
6013
+ ON ccp.product_id = cpe.entity_id
6014
+ WHERE store_product_id IS NOT NULL)");
6015
+
6016
+ echo("\n replaceMagentoProductsMultistore 12\n");
6017
+
6018
+ $result = $this->db_do("
6019
+ UPDATE $catalog_category_product_for_delete_temp ccpfd
6020
+ JOIN $products_temp p
6021
+ ON ccpfd.store_product_id = p.store_product_id
6022
+ SET ccpfd.store_category_id = p.store_category_id
6023
+ WHERE ccpfd.store_product_id != 0");
6024
+
6025
+ echo("\n replaceMagentoProductsMultistore 13\n");
6026
+
6027
+ $result = $this->db_do("
6028
+ UPDATE $catalog_category_product_for_delete_temp ccpfd
6029
+ JOIN $stINch_categories_mapping scm
6030
+ ON ccpfd.store_category_id = scm.store_category_id
6031
+ SET ccpfd.new_category_id = scm.shop_entity_id
6032
+ WHERE ccpfd.store_category_id != 0");
6033
+
6034
+ echo("\n replaceMagentoProductsMultistore 14\n");
6035
+
6036
+ $result = $this->db_do("DELETE FROM $catalog_category_product_for_delete_temp WHERE category_id = new_category_id");
6037
+
6038
+
6039
+ $result = $this->db_do("
6040
+ DELETE ccp
6041
+ FROM $catalog_category_product ccp
6042
+ JOIN $catalog_category_product_for_delete_temp ccpfd
6043
+ ON ccp.product_id = ccpfd.product_id
6044
+ AND ccp.category_id = ccpfd.category_id");
6045
+
6046
+ echo("\n replaceMagentoProductsMultistore 15\n");
6047
+
6048
+ $result = $this->db_do("
6049
+ INSERT INTO $catalog_category_product
6050
+ (category_id, product_id)
6051
+ (SELECT
6052
+ scm.shop_entity_id,
6053
+ cpe.entity_id
6054
+ FROM $catalog_product_entity cpe
6055
+ JOIN $products_temp p
6056
+ ON cpe.store_product_id = p.store_product_id
6057
+ JOIN $stINch_categories_mapping scm
6058
+ ON p.store_category_id = scm.store_category_id
6059
+ )
6060
+ ON DUPLICATE KEY UPDATE
6061
+ product_id = cpe.entity_id");
6062
+
6063
+
6064
+ echo("\n replaceMagentoProductsMultistore 15.1 (add multi categories)\n");
6065
+
6066
+ $result = $this->db_do("
6067
+ INSERT INTO $catalog_category_product
6068
+ (category_id, product_id)
6069
+ (SELECT
6070
+ scm.shop_entity_id,
6071
+ cpe.entity_id
6072
+ FROM $catalog_product_entity cpe
6073
+ JOIN $products_temp p
6074
+ ON cpe.store_product_id = p.store_product_id
6075
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
6076
+ ON p.store_product_id=spc.store_product_id
6077
+ JOIN $stINch_categories_mapping scm
6078
+ ON spc.store_category_id = scm.store_category_id
6079
+ )
6080
+ ON DUPLICATE KEY UPDATE
6081
+ product_id = cpe.entity_id
6082
+ ");
6083
+
6084
+ echo("\n replaceMagentoProductsMultistore 16\n");
6085
+
6086
+ //Indexing products and categories in the shop
6087
+ $result = $this->db_do("
6088
+ DELETE ccpi
6089
+ FROM $catalog_category_product_index ccpi
6090
+ LEFT JOIN $catalog_product_entity cpe
6091
+ ON ccpi.product_id = cpe.entity_id
6092
+ WHERE cpe.entity_id IS NULL");
6093
+
6094
+ echo("\n replaceMagentoProductsMultistore 16.2\n");
6095
+
6096
+ $result = $this->db_do("
6097
+ INSERT INTO $catalog_category_product_index
6098
+ (category_id, product_id, position, is_parent, store_id, visibility)
6099
+ (SELECT
6100
+ a.category_id,
6101
+ a.product_id,
6102
+ a.position,
6103
+ 1,
6104
+ b.store_id,
6105
+ 4
6106
+ FROM $catalog_category_product a
6107
+ JOIN $core_store b
6108
+ )
6109
+ ON DUPLICATE KEY UPDATE
6110
+ visibility = 4");
6111
+
6112
+ echo("\n replaceMagentoProductsMultistore 17\n");
6113
+
6114
+ $result = $this->db_do("
6115
+ INSERT ignore INTO $catalog_category_product_index
6116
+ (category_id, product_id, position, is_parent, store_id, visibility)
6117
+ (SELECT
6118
+ rc.root_cat,
6119
+ a.product_id,
6120
+ a.position,
6121
+ 1,
6122
+ b.store_id,
6123
+ 4
6124
+ FROM $catalog_category_product a
6125
+ JOIN $root_cats rc
6126
+ ON a.category_id = rc.entity_id
6127
+ JOIN $core_store b
6128
+ )
6129
+ ON DUPLICATE KEY UPDATE
6130
+ visibility = 4");
6131
+
6132
+ echo("\n replaceMagentoProductsMultistore 18\n");
6133
+
6134
+ //Set product name for specific web sites
6135
+ $result = $this->db_do("
6136
+ DELETE cpev
6137
+ FROM $catalog_product_entity_varchar cpev
6138
+ LEFT JOIN $catalog_product_entity cpe
6139
+ ON cpev.entity_id = cpe.entity_id
6140
+ WHERE cpe.entity_id IS NULL");
6141
+
6142
+ $result = $this->db_do("
6143
+ INSERT INTO $catalog_product_entity_varchar
6144
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6145
+ (SELECT
6146
+ $_getProductEntityTypeId,
6147
+ $attr_name,
6148
+ w.website,
6149
+ a.entity_id,
6150
+ b.product_name
6151
+ FROM $catalog_product_entity a
6152
+ JOIN $products_temp b
6153
+ ON a.store_product_id = b.store_product_id
6154
+ JOIN $products_website_temp w
6155
+ ON a.store_product_id = w.store_product_id
6156
+ )
6157
+ ON DUPLICATE KEY UPDATE
6158
+ value = b.product_name");
6159
+
6160
+ echo("\n replaceMagentoProductsMultistore 19\n");
6161
+
6162
+ // product name for all web sites
6163
+ $result = $this->db_do("
6164
+ INSERT INTO $catalog_product_entity_varchar
6165
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6166
+ (SELECT
6167
+ $_getProductEntityTypeId,
6168
+ $attr_name,
6169
+ 0,
6170
+ a.entity_id,
6171
+ b.product_name
6172
+ FROM $catalog_product_entity a
6173
+ JOIN $products_temp b
6174
+ ON a.store_product_id = b.store_product_id
6175
+ )
6176
+ ON DUPLICATE KEY UPDATE
6177
+ value = b.product_name");
6178
+
6179
+ echo("\n replaceMagentoProductsMultistore 20\n");
6180
+
6181
+ $this->dropHTMLentities($this->_getProductEntityTypeId(), $this->_getProductAttributeId('name'));
6182
+ $this->addDescriptions();
6183
+ $this->cleanProductDistributors();
6184
+ if ($this->product_file_format == "NEW") {
6185
+ $this->addReviews();
6186
+ $this->addWeight();
6187
+ $this->addSearchCache();
6188
+ $this->addPdfUrl();
6189
+ $this->addShortDescriptions();
6190
+ $this->addProductDistributors();
6191
+ }
6192
+ $this->addEAN();
6193
+ $this->addSpecification();
6194
+ $this->addManufacturers();
6195
+
6196
+ echo("\n replaceMagentoProductsMultistore 21\n");
6197
+
6198
+ //Enabling product index.
6199
+ $result = $this->db_do("
6200
+ DELETE cpei
6201
+ FROM $catalog_product_enabled_index cpei
6202
+ LEFT JOIN $catalog_product_entity cpe
6203
+ ON cpei.product_id = cpe.entity_id
6204
+ WHERE cpe.entity_id IS NULL");
6205
+
6206
+ echo("\n replaceMagentoProductsMultistore 22\n");
6207
+
6208
+ $result = $this->db_do("
6209
+ INSERT INTO $catalog_product_enabled_index
6210
+ (product_id, store_id, visibility)
6211
+ (SELECT
6212
+ a.entity_id,
6213
+ w.website,
6214
+ 4
6215
+ FROM $catalog_product_entity a
6216
+ JOIN $products_website_temp w
6217
+ ON a.store_product_id = w.store_product_id
6218
+ )
6219
+ ON DUPLICATE KEY UPDATE
6220
+ visibility = 4");
6221
+
6222
+ echo("\n replaceMagentoProductsMultistore 23\n");
6223
+
6224
+ $result = $this->db_do("
6225
+ INSERT INTO $catalog_product_enabled_index
6226
+ (product_id, store_id, visibility)
6227
+ (SELECT
6228
+ a.entity_id,
6229
+ 0,
6230
+ 4
6231
+ FROM $catalog_product_entity a
6232
+ JOIN $products_website_temp w
6233
+ ON a.store_product_id = w.store_product_id
6234
+ )
6235
+ ON DUPLICATE KEY UPDATE
6236
+ visibility = 4");
6237
+
6238
+ echo("\n replaceMagentoProductsMultistore 24\n");
6239
+
6240
+ $result = $this->db_do("
6241
+ INSERT INTO $catalog_product_entity_int
6242
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6243
+ (SELECT
6244
+ $_getProductEntityTypeId,
6245
+ $attr_visibility,
6246
+ w.website,
6247
+ a.entity_id,
6248
+ 4
6249
+ FROM $catalog_product_entity a
6250
+ JOIN $products_website_temp w
6251
+ ON a.store_product_id = w.store_product_id
6252
+ )
6253
+ ON DUPLICATE KEY UPDATE
6254
+ value = 4");
6255
+
6256
+ echo("\n replaceMagentoProductsMultistore 25\n");
6257
+
6258
+ $result = $this->db_do("
6259
+ INSERT INTO $catalog_product_entity_int
6260
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6261
+ (SELECT
6262
+ $_getProductEntityTypeId,
6263
+ $attr_visibility,
6264
+ 0,
6265
+ a.entity_id,
6266
+ 4
6267
+ FROM $catalog_product_entity a
6268
+ )
6269
+ ON DUPLICATE KEY UPDATE
6270
+ value = 4");
6271
+
6272
+ echo("\n replaceMagentoProductsMultistore 26\n");
6273
+
6274
+ $result = $this->db_do("
6275
+ DELETE cpw
6276
+ FROM $catalog_product_website cpw
6277
+ LEFT JOIN $catalog_product_entity cpe
6278
+ ON cpw.product_id = cpe.entity_id
6279
+ WHERE cpe.entity_id IS NULL");
6280
+
6281
+ echo("\n replaceMagentoProductsMultistore 27\n");
6282
+
6283
+ $result = $this->db_do("
6284
+ INSERT INTO $catalog_product_website
6285
+ (product_id, website_id)
6286
+ (SELECT
6287
+ a.entity_id,
6288
+ w.website_id
6289
+ FROM $catalog_product_entity a
6290
+ JOIN $products_website_temp w
6291
+ ON a.store_product_id = w.store_product_id
6292
+ )
6293
+ ON DUPLICATE KEY UPDATE
6294
+ product_id = a.entity_id,
6295
+ website_id = w.website_id");
6296
+
6297
+ echo("\n replaceMagentoProductsMultistore 28\n");
6298
+
6299
+ //Adding tax class "Taxable Goods"
6300
+ $result = $this->db_do("
6301
+ INSERT INTO $catalog_product_entity_int
6302
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6303
+ (SELECT
6304
+ $_getProductEntityTypeId,
6305
+ $attr_tax_class_id,
6306
+ w.website,
6307
+ a.entity_id,
6308
+ 2
6309
+ FROM $catalog_product_entity a
6310
+ JOIN $products_website_temp w
6311
+ ON a.store_product_id = w.store_product_id
6312
+ )
6313
+ ON DUPLICATE KEY UPDATE
6314
+ value = 2");
6315
+
6316
+ echo("\n replaceMagentoProductsMultistore 29\n");
6317
+
6318
+ $result = $this->db_do("
6319
+ INSERT INTO $catalog_product_entity_int
6320
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6321
+ (SELECT
6322
+ $_getProductEntityTypeId,
6323
+ $attr_tax_class_id,
6324
+ 0,
6325
+ a.entity_id,
6326
+ 2
6327
+ FROM $catalog_product_entity a
6328
+ )
6329
+ ON DUPLICATE KEY UPDATE
6330
+ value = 2");
6331
+
6332
+ echo("\n replaceMagentoProductsMultistore 30\n");
6333
+
6334
+ // Load url Image
6335
+ $result = $this->db_do("
6336
+ INSERT INTO $catalog_product_entity_varchar
6337
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6338
+ (SELECT
6339
+ $_getProductEntityTypeId,
6340
+ $attr_image,
6341
+ w.store_id,
6342
+ a.entity_id,
6343
+ b.main_image_url
6344
+ FROM $catalog_product_entity a
6345
+ JOIN $core_store w
6346
+ JOIN $products_temp b
6347
+ ON a.store_product_id = b.store_product_id
6348
+ )
6349
+ ON DUPLICATE KEY UPDATE
6350
+ value = b.main_image_url");
6351
+
6352
+ echo("\n replaceMagentoProductsMultistore 31\n");
6353
+
6354
+ // image for specific web sites
6355
+ $result = $this->db_do("
6356
+ INSERT INTO $catalog_product_entity_varchar
6357
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6358
+ (SELECT
6359
+ $_getProductEntityTypeId,
6360
+ $attr_image,
6361
+ 0,
6362
+ a.entity_id,
6363
+ b.main_image_url
6364
+ FROM $catalog_product_entity a
6365
+ JOIN $products_temp b
6366
+ ON a.store_product_id = b.store_product_id
6367
+ )
6368
+ ON DUPLICATE KEY UPDATE
6369
+ value = b.main_image_url");
6370
+
6371
+ echo("\n replaceMagentoProductsMultistore 32\n");
6372
+
6373
+ // small_image for specific web sites
6374
+ $result = $this->db_do("
6375
+ INSERT INTO $catalog_product_entity_varchar
6376
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6377
+ (SELECT
6378
+ $_getProductEntityTypeId,
6379
+ $attr_small_image,
6380
+ w.store_id,
6381
+ a.entity_id,
6382
+ b.medium_image_url
6383
+ FROM $catalog_product_entity a
6384
+ JOIN $core_store w
6385
+ JOIN $products_temp b
6386
+ ON a.store_product_id = b.store_product_id
6387
+ )
6388
+ ON DUPLICATE KEY UPDATE
6389
+ value = b.medium_image_url");
6390
+
6391
+ echo("\n replaceMagentoProductsMultistore 33\n");
6392
+
6393
+ // small_image for all web sites
6394
+ $result = $this->db_do("
6395
+ INSERT INTO $catalog_product_entity_varchar
6396
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6397
+ (SELECT
6398
+ $_getProductEntityTypeId,
6399
+ $attr_small_image,
6400
+ 0,
6401
+ a.entity_id,
6402
+ b.medium_image_url
6403
+ FROM $catalog_product_entity a
6404
+ JOIN $core_store w
6405
+ JOIN $products_temp b
6406
+ ON a.store_product_id = b.store_product_id
6407
+ )
6408
+ ON DUPLICATE KEY UPDATE
6409
+ value = b.medium_image_url");
6410
+
6411
+ echo("\n replaceMagentoProductsMultistore 34\n");
6412
+
6413
+ // thumbnail for specific web site
6414
+ $result = $this->db_do("
6415
+ INSERT INTO $catalog_product_entity_varchar
6416
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6417
+ (SELECT
6418
+ $_getProductEntityTypeId,
6419
+ $attr_thumbnail,
6420
+ w.store_id,
6421
+ a.entity_id,
6422
+ b.thumb_image_url
6423
+ FROM $catalog_product_entity a
6424
+ JOIN $core_store w
6425
+ JOIN $products_temp b
6426
+ ON a.store_product_id = b.store_product_id
6427
+ )
6428
+ ON DUPLICATE KEY UPDATE
6429
+ value = b.thumb_image_url");
6430
+
6431
+ echo("\n replaceMagentoProductsMultistore 35\n");
6432
+
6433
+ // thumbnail for all web sites
6434
+ $result = $this->db_do("
6435
+ INSERT INTO $catalog_product_entity_varchar
6436
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6437
+ (SELECT
6438
+ $_getProductEntityTypeId,
6439
+ $attr_thumbnail,
6440
+ 0,
6441
+ a.entity_id,
6442
+ b.thumb_image_url
6443
+ FROM $catalog_product_entity a
6444
+ JOIN $core_store w
6445
+ JOIN $products_temp b
6446
+ ON a.store_product_id = b.store_product_id
6447
+ )
6448
+ ON DUPLICATE KEY UPDATE
6449
+ value = b.thumb_image_url");
6450
+
6451
+ echo("\n replaceMagentoProductsMultistore 36\n");
6452
+
6453
+ /*STP DELETE
6454
+ //Refresh fulltext search
6455
+ $result = $this->db_do("DROP TABLE IF EXISTS {$catalogsearch_fulltext}_tmp");
6456
+ $result = $this->db_do("CREATE TEMPORARY TABLE IF NOT EXISTS {$catalogsearch_fulltext}_tmp LIKE $catalogsearch_fulltext");
6457
+
6458
+
6459
+ echo("\n replaceMagentoProductsMultistore 36.2\n");
6460
+ $q = "
6461
+ INSERT INTO {$catalogsearch_fulltext}_tmp
6462
+ (product_id, store_id, data_index)
6463
+ (SELECT
6464
+ a.entity_id,
6465
+ w.website,
6466
+ CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
6467
+ FROM $catalog_product_entity a
6468
+ JOIN $products_website_temp w
6469
+ ON a.store_product_id = w.store_product_id
6470
+ LEFT JOIN $catalog_category_product b
6471
+ ON a.entity_id = b.product_id
6472
+ LEFT JOIN $catalog_category_entity_varchar c
6473
+ ON b.category_id = c.entity_id
6474
+ AND c.attribute_id = $cat_attr_name
6475
+ LEFT JOIN $catalog_product_entity_varchar e
6476
+ ON a.entity_id = e.entity_id
6477
+ AND e.attribute_id = $attr_name
6478
+ LEFT JOIN $catalog_product_website j
6479
+ ON a.entity_id = j.product_id
6480
+ LEFT JOIN $products_temp f
6481
+ ON a.store_product_id = f.store_product_id
6482
+ )
6483
+ ON DUPLICATE KEY UPDATE
6484
+ data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)";
6485
+ echo("\n\n============================\n$q\n============================\n\n");
6486
+
6487
+
6488
+ $result = $this->db_do("
6489
+ INSERT INTO {$catalogsearch_fulltext}_tmp
6490
+ (product_id, store_id, data_index)
6491
+ (SELECT
6492
+ a.entity_id,
6493
+ w.website,
6494
+ CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
6495
+ FROM $catalog_product_entity a
6496
+ JOIN $products_website_temp w
6497
+ ON a.store_product_id = w.store_product_id
6498
+ LEFT JOIN $catalog_category_product b
6499
+ ON a.entity_id = b.product_id
6500
+ LEFT JOIN $catalog_category_entity_varchar c
6501
+ ON b.category_id = c.entity_id
6502
+ AND c.attribute_id = $cat_attr_name
6503
+ LEFT JOIN $catalog_product_entity_varchar e
6504
+ ON a.entity_id = e.entity_id
6505
+ AND e.attribute_id = $attr_name
6506
+ LEFT JOIN $catalog_product_website j
6507
+ ON a.entity_id = j.product_id
6508
+ LEFT JOIN $products_temp f
6509
+ ON a.store_product_id = f.store_product_id
6510
+ )
6511
+ ON DUPLICATE KEY UPDATE
6512
+ data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)");
6513
+
6514
+ echo("\n replaceMagentoProductsMultistore 37\n");
6515
+
6516
+
6517
+ $result = $this->db_do("
6518
+ INSERT INTO {$catalogsearch_fulltext}_tmp
6519
+ (product_id, store_id, data_index)
6520
+ (SELECT
6521
+ a.entity_id,
6522
+ w.website,
6523
+ CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)
6524
+ FROM $catalog_product_entity a
6525
+ JOIN $products_website_temp w
6526
+ ON a.store_product_id = w.store_product_id
6527
+ LEFT JOIN $catalog_category_product b
6528
+ ON a.entity_id = b.product_id
6529
+ LEFT JOIN $catalog_category_entity_varchar c
6530
+ ON b.category_id = c.entity_id
6531
+ AND c.attribute_id = $cat_attr_name
6532
+ LEFT JOIN $catalog_product_entity_varchar e
6533
+ ON a.entity_id = e.entity_id
6534
+ AND e.attribute_id = $attr_name
6535
+ LEFT JOIN $products_temp f
6536
+ ON a.store_product_id = f.store_product_id
6537
+ )
6538
+ ON DUPLICATE KEY UPDATE
6539
+ data_index = CONCAT_WS(' ', a.sku, f.search_cache, c.value, e.value)");
6540
+
6541
+ echo("\n replaceMagentoProductsMultistore 38\n");
6542
+
6543
+ $result = $this->db_do("
6544
+ DELETE cf
6545
+ FROM $catalogsearch_fulltext cf
6546
+ LEFT JOIN $catalog_product_entity cpe
6547
+ ON cf.product_id = cpe.entity_id
6548
+ WHERE cpe.entity_id IS NULL");
6549
+
6550
+ echo("\n replaceMagentoProductsMultistore 39\n");
6551
+
6552
+ $result = $this->db_do("
6553
+ INSERT INTO $catalogsearch_fulltext
6554
+ (product_id, store_id, data_index)
6555
+ (SELECT
6556
+ a.product_id,
6557
+ a.store_id,
6558
+ a.data_index
6559
+ FROM {$catalogsearch_fulltext}_tmp a
6560
+ )
6561
+ ON DUPLICATE KEY UPDATE
6562
+ data_index = a.data_index");
6563
+
6564
+ echo("\n replaceMagentoProductsMultistore 40\n");
6565
+
6566
+ $this->db_do("UPDATE $catalogsearch_query SET is_processed = 0");
6567
+ //INNER JOIN eav_attribute_option_value d ON a.vendor_id = d.option_id
6568
+ //TODO add something else
6569
+ STP DELETE*/
6570
+
6571
+ $this->addRelatedProducts();
6572
+ echo("\n replaceMagentoProductsMultistore 41\n");
6573
+ }
6574
+
6575
+
6576
+ public function replaceMagentoProductsMultistoreMERGE($coincidence)
6577
+ {
6578
+ echo("\n replaceMagentoProductsMultistoreMERGE 1\n");
6579
+
6580
+ $connection = Mage::getModel('core/resource')->getConnection('core_write');
6581
+
6582
+ $products_temp = Mage::getSingleton('core/resource')->getTableName('products_temp');
6583
+ $products_website_temp = Mage::getSingleton('core/resource')->getTableName('products_website_temp');
6584
+ $catalog_product_entity = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity');
6585
+ $catalog_product_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int');
6586
+ $catalog_product_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar');
6587
+ $catalog_category_product = Mage::getSingleton('core/resource')->getTableName('catalog_category_product');
6588
+ $stINch_products_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_products_mapping');
6589
+ $stINch_products = Mage::getSingleton('core/resource')->getTableName('stINch_products');
6590
+ $catalog_category_entity = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity');
6591
+ $stINch_categories_mapping = Mage::getSingleton('core/resource')->getTableName('stINch_categories_mapping');
6592
+ $catalog_category_product_index = Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index');
6593
+ $core_store = Mage::getSingleton('core/resource')->getTableName('core_store');
6594
+ $catalog_product_enabled_index = Mage::getSingleton('core/resource')->getTableName('catalog_product_enabled_index');
6595
+ $catalog_product_website = Mage::getSingleton('core/resource')->getTableName('catalog_product_website');
6596
+ //$catalogsearch_fulltext = Mage::getSingleton('core/resource')->getTableName('catalogsearch_fulltext');
6597
+ //$catalogsearch_query = Mage::getSingleton('core/resource')->getTableName('catalogsearch_query');
6598
+ $catalog_category_entity_varchar = Mage::getSingleton('core/resource')->getTableName('catalog_category_entity_varchar');
6599
+
6600
+ $_getProductEntityTypeId = $this->_getProductEntityTypeId();
6601
+ $_defaultAttributeSetId = $this->_getProductDefaulAttributeSetId();
6602
+
6603
+ $attr_atatus = $this->_getProductAttributeId('status');
6604
+ $attr_name = $this->_getProductAttributeId('name');
6605
+ $attr_visibility = $this->_getProductAttributeId('visibility');
6606
+ $attr_tax_class_id = $this->_getProductAttributeId('tax_class_id');
6607
+ $attr_image = $this->_getProductAttributeId('image');
6608
+ $attr_small_image = $this->_getProductAttributeId('small_image');
6609
+ $attr_thumbnail = $this->_getProductAttributeId('thumbnail');
6610
+
6611
+ $cat_attr_name = $this->_getCategoryAttributeId('name');
6612
+ echo("\n replaceMagentoProductsMultistoreMERGE 2\n");
6613
+
6614
+ //clear products, inserting new products and updating old others.
6615
+ $query = "
6616
+ DELETE cpe
6617
+ FROM $catalog_product_entity cpe
6618
+ JOIN $stINch_products_mapping pm
6619
+ ON cpe.entity_id = pm.entity_id
6620
+ WHERE pm.shop_store_product_id IS NOT NULL
6621
+ AND pm.store_product_id IS NULL";
6622
+ $result = $this->db_do($query);
6623
+
6624
+ echo("\n replaceMagentoProductsMultistoreMERGE 3\n");
6625
+
6626
+ $result = $this->db_do("
6627
+ INSERT INTO $catalog_product_entity
6628
+ (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
6629
+ (SELECT
6630
+ pm.entity_id,
6631
+ $_getProductEntityTypeId,
6632
+ $_defaultAttributeSetId,
6633
+ 'simple',
6634
+ a.product_sku,
6635
+ NOW(),
6636
+ 0,
6637
+ a.store_product_id,
6638
+ a.sinch_product_id
6639
+ FROM $products_temp a
6640
+ LEFT JOIN $stINch_products_mapping pm
6641
+ ON a.store_product_id = pm.store_product_id
6642
+ AND a.sinch_product_id = pm.sinch_product_id
6643
+ WHERE pm.entity_id IS NOT NULL
6644
+ )
6645
+ ON DUPLICATE KEY UPDATE
6646
+ sku = a.product_sku,
6647
+ store_product_id = a.store_product_id,
6648
+ sinch_product_id = a.sinch_product_id");
6649
+ // store_product_id = a.store_product_id,
6650
+ // sinch_product_id = a.sinch_product_id
6651
+
6652
+ $result = $this->db_do("
6653
+ INSERT INTO $catalog_product_entity
6654
+ (entity_id, entity_type_id, attribute_set_id, type_id, sku, updated_at, has_options, store_product_id, sinch_product_id)
6655
+ (SELECT
6656
+ pm.entity_id,
6657
+ $_getProductEntityTypeId,
6658
+ $_defaultAttributeSetId,
6659
+ 'simple',
6660
+ a.product_sku,
6661
+ NOW(),
6662
+ 0,
6663
+ a.store_product_id,
6664
+ a.sinch_product_id
6665
+ FROM $products_temp a
6666
+ LEFT JOIN $stINch_products_mapping pm
6667
+ ON a.store_product_id = pm.store_product_id
6668
+ AND a.sinch_product_id = pm.sinch_product_id
6669
+ WHERE pm.entity_id IS NULL
6670
+ )
6671
+ ON DUPLICATE KEY UPDATE
6672
+ sku = a.product_sku,
6673
+ store_product_id = a.store_product_id,
6674
+ sinch_product_id = a.sinch_product_id");
6675
+ // store_product_id = a.store_product_id,
6676
+ // sinch_product_id = a.sinch_product_id
6677
+
6678
+ echo("\n replaceMagentoProductsMultistoreMERGE 4\n");
6679
+
6680
+ //Set enabled
6681
+ $result = $this->db_do("
6682
+ DELETE cpei
6683
+ FROM $catalog_product_entity_int cpei
6684
+ LEFT JOIN $catalog_product_entity cpe
6685
+ ON cpei.entity_id = cpe.entity_id
6686
+ WHERE cpe.entity_id IS NULL");
6687
+
6688
+ $result = $this->db_do("
6689
+ INSERT INTO $catalog_product_entity_int
6690
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6691
+ (SELECT
6692
+ $_getProductEntityTypeId,
6693
+ $attr_atatus,
6694
+ w.website,
6695
+ a.entity_id,
6696
+ 1
6697
+ FROM $catalog_product_entity a
6698
+ JOIN $products_website_temp w
6699
+ ON a.store_product_id = w.store_product_id
6700
+ )
6701
+ ON DUPLICATE KEY UPDATE
6702
+ value = 1");
6703
+
6704
+
6705
+ echo("\n replaceMagentoProductsMultistoreMERGE 5\n");
6706
+
6707
+
6708
+ // set status = 1 for all stores
6709
+ $result = $this->db_do("
6710
+ INSERT INTO $catalog_product_entity_int
6711
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6712
+ (SELECT
6713
+ $_getProductEntityTypeId,
6714
+ $attr_atatus,
6715
+ 0,
6716
+ a.entity_id,
6717
+ 1
6718
+ FROM $catalog_product_entity a
6719
+ )
6720
+ ON DUPLICATE KEY UPDATE
6721
+ value = 1");
6722
+
6723
+ echo("\n replaceMagentoProductsMultistoreMERGE 6\n");
6724
+
6725
+ //Unifying products with categories.
6726
+ $result = $this->db_do("
6727
+ DELETE ccp
6728
+ FROM $catalog_category_product ccp
6729
+ LEFT JOIN $catalog_product_entity cpe
6730
+ ON ccp.product_id = cpe.entity_id
6731
+ WHERE cpe.entity_id IS NULL");
6732
+
6733
+ echo("\n replaceMagentoProductsMultistoreMERGE 7\n");
6734
+
6735
+ $root_cats = Mage::getSingleton('core/resource')->getTableName('root_cats');
6736
+
6737
+ $result = $this->db_do("DROP TABLE IF EXISTS $root_cats");
6738
+ $result = $this->db_do("
6739
+ CREATE TABLE $root_cats
6740
+ SELECT
6741
+ entity_id,
6742
+ path,
6743
+ SUBSTRING(path, LOCATE('/', path)+1) AS short_path,
6744
+ LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1)) AS end_pos,
6745
+ SUBSTRING(SUBSTRING(path, LOCATE('/', path)+1), 1, LOCATE('/', SUBSTRING(path, LOCATE('/', path)+1))-1) as root_cat
6746
+ FROM $catalog_category_entity
6747
+ ");
6748
+ $result = $this->db_do("UPDATE $root_cats SET root_cat = entity_id WHERE CHAR_LENGTH(root_cat) = 0");
6749
+
6750
+ echo("\n replaceMagentoProductsMultistoreMERGE 8\n");
6751
+
6752
+ $result = $this->db_do("
6753
+ UPDATE IGNORE $catalog_category_product ccp
6754
+ LEFT JOIN $catalog_category_entity cce
6755
+ ON ccp.category_id = cce.entity_id
6756
+ JOIN $root_cats rc
6757
+ ON cce.entity_id = rc.entity_id
6758
+ SET ccp.category_id = rc.root_cat
6759
+ WHERE cce.entity_id IS NULL");
6760
+
6761
+ echo("\n replaceMagentoProductsMultistoreMERGE 9\n");
6762
+
6763
+ $result = $this->db_do("
6764
+ DELETE ccp
6765
+ FROM $catalog_category_product ccp
6766
+ LEFT JOIN $catalog_category_entity cce
6767
+ ON ccp.category_id = cce.entity_id
6768
+ WHERE cce.entity_id IS NULL");
6769
+
6770
+ $stinch_products_delete = Mage::getSingleton('core/resource')->getTableName('stinch_products_delete');
6771
+
6772
+ $result = $this->db_do("DROP TABLE IF EXISTS $stinch_products_delete");
6773
+ $result = $this->db_do("
6774
+ CREATE TABLE $stinch_products_delete
6775
+ SELECT cpe.entity_id
6776
+ FROM $catalog_product_entity cpe
6777
+ WHERE cpe.entity_id NOT IN
6778
+ (
6779
+ SELECT cpe2.entity_id
6780
+ FROM $catalog_product_entity cpe2
6781
+ JOIN $stINch_products sp
6782
+ ON cpe2.sinch_product_id = sp.sinch_product_id
6783
+ )");
6784
+
6785
+ $result = $this->db_do("DELETE cpe FROM $catalog_product_entity cpe JOIN $stinch_products_delete spd USING(entity_id)");
6786
+
6787
+ $result = $this->db_do("DROP TABLE IF EXISTS $stinch_products_delete");
6788
+
6789
+ echo("\n replaceMagentoProductsMultistoreMERGE 15\n");
6790
+
6791
+ $result = $this->db_do("
6792
+ INSERT INTO $catalog_category_product
6793
+ (category_id, product_id)
6794
+ (SELECT
6795
+ scm.shop_entity_id,
6796
+ cpe.entity_id
6797
+ FROM $catalog_product_entity cpe
6798
+ JOIN $products_temp p
6799
+ ON cpe.store_product_id = p.store_product_id
6800
+ JOIN $stINch_categories_mapping scm
6801
+ ON p.store_category_id = scm.store_category_id
6802
+ )
6803
+ ON DUPLICATE KEY UPDATE
6804
+ product_id = cpe.entity_id");
6805
+
6806
+ echo("\n replaceMagentoProductsMultistoreMERGE 15.1 (add multi categories)\n");
6807
+
6808
+ $result = $this->db_do("
6809
+ INSERT INTO $catalog_category_product
6810
+ (category_id, product_id)
6811
+ (SELECT
6812
+ scm.shop_entity_id,
6813
+ cpe.entity_id
6814
+ FROM $catalog_product_entity cpe
6815
+ JOIN $products_temp p
6816
+ ON cpe.store_product_id = p.store_product_id
6817
+ JOIN " . Mage::getSingleton('core/resource')->getTableName('stINch_product_categories') . " spc
6818
+ ON p.store_product_id=spc.store_product_id
6819
+ JOIN $stINch_categories_mapping scm
6820
+ ON spc.store_category_id = scm.store_category_id
6821
+ )
6822
+ ON DUPLICATE KEY UPDATE
6823
+ product_id = cpe.entity_id
6824
+ ");
6825
+
6826
+ echo("\n replaceMagentoProductsMultistoreMERGE 16\n");
6827
+
6828
+ //Indexing products and categories in the shop
6829
+ $result = $this->db_do("
6830
+ DELETE ccpi
6831
+ FROM $catalog_category_product_index ccpi
6832
+ LEFT JOIN $catalog_product_entity cpe
6833
+ ON ccpi.product_id = cpe.entity_id
6834
+ WHERE cpe.entity_id IS NULL");
6835
+
6836
+ echo("\n replaceMagentoProductsMultistoreMERGE 16.2\n");
6837
+
6838
+ $result = $this->db_do("
6839
+ INSERT INTO $catalog_category_product_index
6840
+ (category_id, product_id, position, is_parent, store_id, visibility)
6841
+ (SELECT
6842
+ a.category_id,
6843
+ a.product_id,
6844
+ a.position,
6845
+ 1,
6846
+ b.store_id,
6847
+ 4
6848
+ FROM $catalog_category_product a
6849
+ JOIN $core_store b
6850
+ )
6851
+ ON DUPLICATE KEY UPDATE
6852
+ visibility = 4");
6853
+
6854
+ echo("\n replaceMagentoProductsMultistoreMERGE 17\n");
6855
+ $root_cats = Mage::getSingleton('core/resource')->getTableName('root_cats');
6856
+ // !!! $this->_root_cat
6857
+ $result = $this->db_do("
6858
+ INSERT ignore INTO $catalog_category_product_index
6859
+ (category_id, product_id, position, is_parent, store_id, visibility)
6860
+ (SELECT
6861
+ rc.root_cat,
6862
+ a.product_id,
6863
+ a.position,
6864
+ 1,
6865
+ b.store_id,
6866
+ 4
6867
+ FROM $catalog_category_product a
6868
+ JOIN $root_cats rc
6869
+ ON a.category_id = rc.entity_id
6870
+ JOIN $core_store b
6871
+ )
6872
+ ON DUPLICATE KEY UPDATE
6873
+ visibility = 4");
6874
+
6875
+ echo("\n replaceMagentoProductsMultistoreMERGE 18\n");
6876
+
6877
+ //Set product name for specific web sites
6878
+ $result = $this->db_do("
6879
+ DELETE cpev
6880
+ FROM $catalog_product_entity_varchar cpev
6881
+ LEFT JOIN $catalog_product_entity cpe
6882
+ ON cpev.entity_id = cpe.entity_id
6883
+ WHERE cpe.entity_id IS NULL");
6884
+
6885
+ $result = $this->db_do("
6886
+ INSERT INTO $catalog_product_entity_varchar
6887
+ (entity_type_id, attribute_id, store_id, entity_id, value)
6888
+ (SELECT
6889
+ $_getProductEntityTypeId,
6890
+ $attr_name,
6891
+ w.website,
6892
+ a.entity_id,
6893
+ b.product_name
6894
+ FROM $catalog_product_entity a
6895
+ JOIN $products_temp b
6896
+ ON a.store_product_id = b.store_product_id
6897
+ JOIN $products_website_temp w
6898
+ ON a.store_product_id = w.store_product_id
6899
+ )
6900
+ ON DUPLICATE KEY UPDATE
6901
+ value = b.product_name");
6902
+
6903
+ echo("\n replaceMagentoProductsMultistoreMERGE 19\n");
6904
+
6905
+ // product name for all web sites
6906
+ $result = $this->db_do("
6907
+ INSERT INTO $catalog_