PluginProbe
Welcart e-Commerce / 1.3.8
Welcart e-Commerce v1.3.8
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
usc-e-shop / functions / define_function.php

define_function.php in Welcart e-Commerce 1.3.8, at functions/define_function.php

1,297 lines 49.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 function usces_define_functions(){
3
4 //20101111ysk start
5 if( !function_exists('usces_item_uploadcsv') ):
6 function usces_item_uploadcsv(){
7 global $wpdb, $usces, $user_ID;
8
9 if( !current_user_can( 'import' ) ){
10 $res['status'] = 'error';
11 $res['message'] = __('You do not have permission to do that.');
12 $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
13 wp_redirect($url);
14 exit;
15 }
16
17 //check data SELECT id, title FROM table GROUP BY id HAVING COUNT(id) > 1
18 $query = $wpdb->prepare("SELECT post_id, meta_value FROM {$wpdb->postmeta}
19 LEFT JOIN {$wpdb->posts} ON ID = post_id
20 WHERE meta_key = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')
21 GROUP BY meta_value HAVING COUNT(meta_value) > 1",
22 '_itemCode');
23 $db_check = $wpdb->get_results( $query, ARRAY_A );
24 if( $db_check ) {
25 $res['status'] = 'error';
26 $res['message'] .= __('The same product cord is registered.', 'usces');
27 foreach($db_check as $d_item){
28 $res['message'] .= ' , ' . $d_item['meta_value'];
29 }
30 $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
31 wp_redirect($url);
32 exit;
33 }
34
35
36 $path = WP_CONTENT_DIR . '/uploads/';
37 /*********************************************************************/
38 // Upload
39 /**********************************************************************/
40 if( isset($_REQUEST['action']) && 'itemcsv' == $_REQUEST['action'] ){
41 $workfile = $_FILES["usces_upcsv"]["tmp_name"];
42
43 if ( !is_uploaded_file($workfile) ) {
44 $res['status'] = 'error';
45 $res['message'] = __('The file was not uploaded.', 'usces');
46 $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
47 wp_redirect($url);
48 exit;
49 }
50
51 //check ext
52 list($fname, $fext) = explode('.', $_FILES["usces_upcsv"]["name"], 2);
53 if( $fext != 'csv' ) {
54 $res['status'] = 'error';
55 $res['message'] = __('The file is not supported.', 'usces').$fname.'.'.$fext;
56 $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
57 wp_redirect($url);
58 exit;
59 }
60
61 $new_filename = base64_encode($fname . '_' . time() . '.' .$fext);
62 if ( ! move_uploaded_file($_FILES['usces_upcsv']['tmp_name'], $path.$new_filename) ) {
63 $res['status'] = 'error';
64 $res['message'] = __('The file was not stored.', 'usces').$fname.'.'.$fext;
65 $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
66 wp_redirect($url);
67 exit;
68 }
69 return $new_filename;
70 }
71
72
73 $yn = "\r\n";
74 $br = "<br />";
75 /*********************************************************************/
76 // Register
77 /**********************************************************************/
78 if( isset($_REQUEST['regfile']) && !WCUtils::is_blank($_REQUEST['regfile']) && isset($_REQUEST['action']) && 'upload_register' == $_REQUEST['action'] ){
79 $file_name = $_REQUEST['regfile'];
80 $decode_filename = base64_decode($file_name);
81 if( ! file_exists($path.$file_name) ){
82 $res['status'] = 'error';
83 $res['message'] = __('CSV file does not exist.', 'usces').esc_html($decode_filename);
84 //echo $res['status'] . ' : ' . $res['message'];
85 //return;
86 return( $res );
87 }
88 }
89 /*////////////////////////////////////////*/
90 // ready
91 /*////////////////////////////////////////*/
92 $start = microtime(true);
93
94 //$wpdb->show_errors();
95 $wpdb->query( 'SET SQL_BIG_SELECTS=1' );
96 set_time_limit(3600);
97
98 define('USCES_COL_POST_ID', 0);
99 define('USCES_COL_POST_AUTHOR', 1);
100 define('USCES_COL_POST_CONTENT', 2);
101 define('USCES_COL_POST_TITLE', 3);
102 define('USCES_COL_POST_EXCERPT', 4);
103 define('USCES_COL_POST_STATUS', 5);
104 define('USCES_COL_POST_COMMENT_STATUS', 6);
105 define('USCES_COL_POST_PASSWORD', 7);
106 define('USCES_COL_POST_NAME', 8);
107 define('USCES_COL_POST_MODIFIED', 9);
108
109 define('USCES_COL_ITEM_CODE', 10);
110 define('USCES_COL_ITEM_NAME', 11);
111 define('USCES_COL_ITEM_RESTRICTION', 12);
112 define('USCES_COL_ITEM_POINTRATE', 13);
113 define('USCES_COL_ITEM_GPNUM1', 14);
114 define('USCES_COL_ITEM_GPDIS1', 15);
115 define('USCES_COL_ITEM_GPNUM2', 16);
116 define('USCES_COL_ITEM_GPDIS2', 17);
117 define('USCES_COL_ITEM_GPNUM3', 18);
118 define('USCES_COL_ITEM_GPDIS3', 19);
119 define('USCES_COL_ITEM_SHIPPING', 20);
120 define('USCES_COL_ITEM_DELIVERYMETHOD', 21);
121 define('USCES_COL_ITEM_SHIPPINGCHARGE', 22);
122 define('USCES_COL_ITEM_INDIVIDUALSCHARGE', 23);
123
124 define('USCES_COL_CATEGORY', 24);
125 define('USCES_COL_POST_TAG', 25);
126 define('USCES_COL_CUSTOM_FIELD', 26);
127
128 define('USCES_COL_SKU_CODE', 27);
129 define('USCES_COL_SKU_NAME', 28);
130 define('USCES_COL_SKU_CPRICE', 29);
131 define('USCES_COL_SKU_PRICE', 30);
132 define('USCES_COL_SKU_ZAIKONUM', 31);
133 define('USCES_COL_SKU_ZAIKO', 32);
134 define('USCES_COL_SKU_UNIT', 33);
135 define('USCES_COL_SKU_GPTEKIYO', 34);
136 // define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
137
138 $lines = array();
139 $total_num = 0;
140 $comp_num = 0;
141 $err_num = 0;
142 $min_field_num = 35;//29
143 $log = '';
144 $pre_code = '';
145 $sku_index = 0;
146 $res = array();
147 $date_pattern = "/(\d{4})-(\d{2}|\d)-(\d{2}|\d) (\d{2}):(\d{2}|\d):(\d{2}|\d)/";
148 $linereadytime = 0;
149 $checktime = 0;
150 $insertposttime = 0;
151 $metadeletetime = 0;
152 $addmetatime = 0;
153 $AddOptiontime = 0;
154 $AddSKUtime = 0;
155 $onelinetime = 0;
156
157 //log
158 if ( ! ($fpi = fopen (USCES_PLUGIN_DIR.'/logs/itemcsv_log.txt', "w"))) {
159 $res['status'] = 'error';
160 $res['message'] = __('The log file was not prepared for.', 'usces').esc_html($decode_filename);
161 echo $res['status'] . ' : ' . $res['message'];
162 return;
163 }
164 //read data
165 if ( ! ($fpo = fopen ($path.$file_name, "r"))) {
166 $res['status'] = 'error';
167 $res['message'] = __('A file does not open.', 'usces').esc_html($decode_filename);
168 echo $res['status'] . ' : ' . $res['message'];
169 return;
170 }
171
172
173 $orglines = array();
174 $sp = ",";
175 $fname_parts = explode('.', $decode_filename);
176 if('csv' !== end($fname_parts)) {
177 $res['status'] = 'error';
178 $res['message'] = __('This file is not in the CSV file.', 'usces').esc_html($decode_filename);
179 echo $res['status'] . ' : ' . $res['message'];
180 return;
181
182 } else {
183 $buf = '';
184 while (! feof ($fpo)) {
185 $temp = fgets ($fpo, 10240);
186 if( 0 == strlen($temp) ) continue;
187
188 $num = substr_count($temp, '"');
189 if( 0 == $num % 2 && '' == $buf ){
190 $orglines[] = $temp;
191 }elseif( 1 == $num % 2 && '' == $buf ){
192 $buf .= $temp;
193 }elseif( 0 == $num % 2 && '' != $buf ){
194 $buf .= $temp;
195 }elseif( 1 == $num % 2 && '' != $buf ){
196 $buf .= $temp;
197 $orglines[] = $buf;
198 $buf = '';
199 }
200 }
201 }
202 fclose($fpo);
203
204 echo '<script type="text/javascript">changeMsg("' . __('Processing...', 'usces') . '");</script>'.$yn;
205 echo '<div class="error_log">'.$yn;
206 ob_flush();
207 flush();
208
209 foreach($orglines as $line){
210 $line = trim($line);
211 if( 0 !== strpos( $line, 'Post ID' ) && !empty($line) )
212 $lines[] = $line;
213 }
214 $total_num = count($lines);
215
216 $readytime = microtime(true);
217 //reg loop
218 foreach($lines as $rows_num => $line){
219
220 /*////////////////////////////////////////*/
221 // lineReady
222 /*////////////////////////////////////////*/
223 $linestart = microtime(true);
224 $datas = array();
225
226 $logtemp = '';
227 $mestemp = '';
228 $line = trim($line);
229 if( empty($line) ) continue;
230
231 $d = explode($sp, $line);
232 $buf = '';
233 foreach($d as $data) {
234 $num = substr_count($data, '"');
235 if( 0 == $num % 2 && '' == $buf ){
236 if( '"' == substr($data, 0, 1) )
237 $data = substr($data, 1);
238 if( '"' == substr($data, -1) )
239 $data = substr($data, 0, -1);
240 $data = str_replace(array('""'), '"', $data);
241 $datas[] = ( false !== $data ) ? $data : '';
242 }elseif( 1 == $num % 2 && '' == $buf ){
243 $buf .= $data;
244 }elseif( 0 == $num % 2 && '' != $buf ){
245 $buf .= $sp.$data;
246 }elseif( 1 == $num % 2 && '' != $buf ){
247 $buf .= $sp.$data;
248 if( '"' == substr($buf, 0, 1) )
249 $buf = substr($buf, 1);
250 if( '"' == substr($buf, -1) )
251 $buf = substr($buf, 0, -1);
252 $buf = str_replace(array('""'), '"', $buf);
253 $datas[] = ( false !== $buf ) ? $buf : '';
254 $buf = '';
255 }
256 }
257
258 if( 'Post ID' == $datas[USCES_COL_POST_ID] )
259 continue;
260
261
262 if( $min_field_num > count($datas) ){
263 $err_num++;
264 $mes = "No." . ($rows_num+1)." ".count($datas) . "\t".__('The number of the columns is abnormal.', 'usces');
265 $logtemp .= $mes.$yn;
266 $log .= $logtemp;
267 echo $mes.$br.$yn;
268 continue;
269 }
270
271 if( $pre_code == $datas[USCES_COL_ITEM_CODE] && WCUtils::is_blank($datas[USCES_COL_POST_ID]) ){
272 $mode = 'add';
273
274 }else{
275 $post_id = ( !WCUtils::is_blank($datas[USCES_COL_POST_ID]) ) ? (int)$datas[USCES_COL_POST_ID] : NULL;
276 if( $post_id ){
277 $db_res = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d", $post_id) );
278 if( !$db_res ){
279 $err_num++;
280 $mes = "No." . ($rows_num+1) . "\t".__("Post-ID {$post_id} does not exist in the database.", 'usces');
281 $logtemp .= $mes.$yn;
282 $log .= $logtemp;
283 echo $mes.$br.$yn;
284 continue;
285 }
286 }
287 if( $post_id ){
288 $mode = 'upd';
289 }else{
290 $mode = 'add';
291 }
292 }
293
294 $lineready = microtime(true);
295 $linereadytime += $lineready-$linestart;
296 /*////////////////////////////////////////*/
297 // dataCheck
298 /*////////////////////////////////////////*/
299 //data check loop
300 foreach($datas as $key => $data){
301 $data = trim(mb_convert_encoding($data, 'UTF-8', 'SJIS'));
302 switch($key){
303 case USCES_COL_ITEM_CODE:
304 if( 0 == strlen($data) ){
305 $mes = "No." . ($rows_num+1) . "\t".__('An item cord is non-input.', 'usces');
306 $logtemp .= $mes.$yn;
307 $mestemp .= $mes.$br.$yn;
308 }else{
309 $query = $wpdb->prepare("SELECT meta_id, post_id FROM {$wpdb->postmeta}
310 LEFT JOIN {$wpdb->posts} ON ID = post_id
311 WHERE meta_key = %s AND meta_value = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')",
312 '_itemCode', $data);
313 $db_res1 = $wpdb->get_results( $query, ARRAY_A );
314 if( 'upd' == $mode ){
315 if( 1 < count($db_res1) ){
316 $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has been duplicated.', 'usces');
317 $logtemp .= $mes.$yn;
318 $mestemp .= $mes.$br.$yn;
319 $mes = '';
320 foreach($db_res1 as $res_val){
321 $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />";
322 }
323 $logtemp .= $mes.$yn;
324 $mestemp .= $mes.$br.$yn;
325 }
326 $query = $wpdb->prepare("SELECT meta_id, post_id FROM {$wpdb->postmeta}
327 LEFT JOIN {$wpdb->posts} ON ID = post_id
328 WHERE post_id <> %d AND meta_key = %s AND meta_value = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')",
329 $post_id, '_itemCode', $data);
330 $db_res2 = $wpdb->get_results( $query, ARRAY_A );
331 if( 0 < count($db_res2) ){
332 $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has already been used.', 'usces');
333 $logtemp .= $mes.$yn;
334 $mestemp .= $mes.$br.$yn;
335 $mes = '';
336 foreach($db_res2 as $res_val){
337 $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />";
338 }
339 $logtemp .= $mes.$yn;
340 $mestemp .= $mes.$br.$yn;
341 }
342 }else if( 'add' == $mode ){
343 if( $data != $pre_code ) {
344 if( 0 < count($db_res1) ){
345 $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has already been used.', 'usces');
346 $logtemp .= $mes.$yn;
347 $mestemp .= $mes.$br.$yn;
348 $mes = '';
349 foreach($db_res1 as $res_val){
350 $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />";
351 }
352 $logtemp .= $mes.$yn;
353 $mestemp .= $mes.$br.$yn;
354 }
355 }
356 }
357 }
358 break;
359 case USCES_COL_ITEM_NAME:
360 if( 0 == strlen($data) ){
361 $mes = "No." . ($rows_num+1) . "\t".__('An item name is non-input.', 'usces');
362 $logtemp .= $mes.$yn;
363 $mestemp .= $mes.$br.$yn;
364 }
365 break;
366 case USCES_COL_ITEM_RESTRICTION:
367 if( !preg_match("/^[0-9]+$/", $data) && 0 != strlen($data) ){
368 $mes = "No." . ($rows_num+1) . "\t".__('A value of the purchase limit number is abnormal.', 'usces');
369 $logtemp .= $mes.$yn;
370 $mestemp .= $mes.$br.$yn;
371 }
372 break;
373 case USCES_COL_ITEM_POINTRATE:
374 if( !preg_match("/^[0-9]+$/", $data) ){
375 $mes = "No." . ($rows_num+1) . "\t".__('A value of the point rate is abnormal.', 'usces');
376 $logtemp .= $mes.$yn;
377 $mestemp .= $mes.$br.$yn;
378 }
379 break;
380 case USCES_COL_ITEM_GPNUM1:
381 if( !preg_match("/^[0-9]+$/", $data) ){
382 $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."1-".__('umerical value is abnormality.', 'usces');
383 $logtemp .= $mes.$yn;
384 $mestemp .= $mes.$br.$yn;
385 }
386 break;
387 case USCES_COL_ITEM_GPDIS1:
388 if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM1] && 1 > $data ) ){
389 $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."1-".__('rate is abnormal.', 'usces');
390 $logtemp .= $mes.$yn;
391 $mestemp .= $mes.$br.$yn;
392 }
393 break;
394 case USCES_COL_ITEM_GPNUM2:
395 if( !preg_match("/^[0-9]+$/", $data) || ($datas[USCES_COL_ITEM_GPNUM1] >= $data && 0 != $data) ){
396 $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."2-".__('umerical value is abnormality.', 'usces');
397 $logtemp .= $mes.$yn;
398 $mestemp .= $mes.$br.$yn;
399 }
400 break;
401 case USCES_COL_ITEM_GPDIS2:
402 if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM2] && 1 > $data ) ){
403 $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."2-".__('rate is abnormal.', 'usces');
404 $logtemp .= $mes.$yn;
405 $mestemp .= $mes.$br.$yn;
406 }
407 break;
408 case USCES_COL_ITEM_GPNUM3:
409 if( !preg_match("/^[0-9]+$/", $data) || ($datas[USCES_COL_ITEM_GPNUM2] >= $data && 0 != $data) ){
410 $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."3-".__('umerical value is abnormality.', 'usces');
411 $logtemp .= $mes.$yn;
412 $mestemp .= $mes.$br.$yn;
413 }
414 break;
415 case USCES_COL_ITEM_GPDIS3:
416 if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM3] && 1 > $data ) ){
417 $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."3-".__('rate is abnormal.', 'usces');
418 $logtemp .= $mes.$yn;
419 $mestemp .= $mes.$br.$yn;
420 }
421 break;
422 case USCES_COL_ITEM_SHIPPING:
423 if( !preg_match("/^[0-9]+$/", $data) || 9 < $data ){
424 $mes = "No." . ($rows_num+1) . "\t".__('A value of the shipment day is abnormal.', 'usces');
425 $logtemp .= $mes.$yn;
426 $mestemp .= $mes.$br.$yn;
427 }
428 break;
429 case USCES_COL_ITEM_DELIVERYMETHOD:
430 if( 0 === strlen($data) || !preg_match("/^[0-9;]+$/", $data) ){
431 $mes = "No." . ($rows_num+1) . "\t".__('Invalid value of Delivery method.', 'usces');
432 $logtemp .= $mes.$yn;
433 $mestemp .= $mes.$br.$yn;
434 }
435 break;
436 case USCES_COL_ITEM_SHIPPINGCHARGE:
437 if( 0 === strlen($data) || !preg_match("/^[0-9]+$/", $data) ){
438 $mes = "No." . ($rows_num+1) . "\t".__('Invalid type of shipping charge.', 'usces');
439 $logtemp .= $mes.$yn;
440 $mestemp .= $mes.$br.$yn;
441 }
442 break;
443 case USCES_COL_ITEM_INDIVIDUALSCHARGE:
444 if( !preg_match("/^[0-9]+$/", $data) || 1 < $data ){
445 $mes = "No." . ($rows_num+1) . "\t".__('A value of the postage individual charging is abnormal.', 'usces');
446 $logtemp .= $mes.$yn;
447 $mestemp .= $mes.$br.$yn;
448 }
449 break;
450 case USCES_COL_POST_ID:
451 if( !preg_match("/^[0-9]+$/", $data) && 0 != strlen($data) ){
452 $mes = "No." . ($rows_num+1) . "\t".__('A value of the Post-ID is abnormal.', 'usces');
453 $logtemp .= $mes.$yn;
454 $mestemp .= $mes.$br.$yn;
455 }
456 break;
457 case USCES_COL_POST_AUTHOR:
458 case USCES_COL_POST_COMMENT_STATUS:
459 case USCES_COL_POST_PASSWORD:
460 case USCES_COL_POST_NAME:
461 case USCES_COL_POST_TITLE:
462 case USCES_COL_POST_CONTENT:
463 case USCES_COL_POST_EXCERPT:
464 break;
465 case USCES_COL_POST_STATUS:
466 $array17 = array('publish', 'future', 'draft', 'pending', 'private');
467 if( !in_array($data, $array17) || WCUtils::is_blank($data) ){
468 $mes = "No." . ($rows_num+1) . "\t".__('A value of the display status is abnormal.', 'usces');
469 $logtemp .= $mes.$yn;
470 $mestemp .= $mes.$br.$yn;
471 }
472 break;
473 case USCES_COL_POST_MODIFIED:
474 if( 'future' == $datas[USCES_COL_POST_STATUS] && (WCUtils::is_blank($data) || '0000-00-00 00:00:00' === $data) ){
475 if( preg_match($date_pattern, $data, $match) ){
476 if( checkdate($match[2], $match[3], $match[1]) &&
477 (0 < $match[4] && 24 > $match[4]) &&
478 (0 < $match[5] && 60 > $match[5]) &&
479 (0 < $match[6] && 60 > $match[6]) ){
480 $mes = "";
481 }else{
482 $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
483 $logtemp .= $mes.$yn;
484 $mestemp .= $mes.$br.$yn;
485 }
486
487 }else{
488 $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
489 $logtemp .= $mes.$yn;
490 $mestemp .= $mes.$br.$yn;
491 }
492 }else if( !WCUtils::is_blank($data) && '0000-00-00 00:00:00' !== $data ){
493 if(preg_match("/^[0-9]+$/", substr($data,0,4))) {//�
494 �頭4桁が数値のみ
495 if(strtotime($data) === false){
496 $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
497 $logtemp .= $mes.$yn;
498 $mestemp .= $mes.$br.$yn;
499 }
500 } else {
501 $datetime = explode(' ', $data);
502 $date_str = usces_dates_interconv($datetime[0]).' '.$datetime[1];
503 if(strtotime($date_str) === false){
504 $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
505 $logtemp .= $mes.$yn;
506 $mestemp .= $mes.$br.$yn;
507 }
508 }
509 }
510 break;
511 case USCES_COL_CATEGORY:
512 if( 0 == strlen($data) ){
513 $mes = "No." . ($rows_num+1) . "\t".__('A category is non-input.', 'usces');
514 $logtemp .= $mes.$yn;
515 $mestemp .= $mes.$br.$yn;
516 }
517 break;
518 case USCES_COL_POST_TAG:
519 break;
520 case USCES_COL_CUSTOM_FIELD:
521 break;
522 case USCES_COL_SKU_CODE:
523 if( 0 == strlen($data) ){
524 $mes = "No." . ($rows_num+1) . "\t".__('A SKU cord is non-input.', 'usces');
525 $logtemp .= $mes.$yn;
526 $mestemp .= $mes.$br.$yn;
527 }
528 break;
529 case USCES_COL_SKU_NAME:
530 break;
531 case USCES_COL_SKU_CPRICE:
532 if( 0 < strlen($data) and !preg_match("/^\d$|^\d+\.?\d+$/", $data) ){
533 $mes = "No." . ($rows_num+1) . "\t".__('A value of the price is abnormal.', 'usces');
534 $logtemp .= $mes.$yn;
535 $mestemp .= $mes.$br.$yn;
536 }
537 break;
538 case USCES_COL_SKU_PRICE:
539 if( !preg_match("/^\d$|^\d+\.?\d+$/", $data) || 0 == strlen($data) ){
540 $mes = "No." . ($rows_num+1) . "\t".__('A value of the normal price is abnormal.', 'usces');
541 $logtemp .= $mes.$yn;
542 $mestemp .= $mes.$br.$yn;
543 }
544 break;
545 case USCES_COL_SKU_ZAIKONUM:
546 if( 0 < strlen($data) and !preg_match("/^[0-9]+$/", $data) ){
547 $mes = "No." . ($rows_num+1) . "\t".__('A value of the stock amount is abnormal.', 'usces');
548 $logtemp .= $mes.$yn;
549 $mestemp .= $mes.$br.$yn;
550 }
551 break;
552 case USCES_COL_SKU_ZAIKO:
553 if( !preg_match("/^[0-9]+$/", $data) || 4 < $data ){
554 $mes = "No." . ($rows_num+1) . "\t".__('A value of the stock status is abnormal.', 'usces');
555 $logtemp .= $mes.$yn;
556 $mestemp .= $mes.$br.$yn;
557 }
558 break;
559 case USCES_COL_SKU_UNIT:
560 break;
561 case USCES_COL_SKU_GPTEKIYO:
562 if( !preg_match("/^[0-9]+$/", $data) || 1 < $data ){
563 $mes = "No." . ($rows_num+1) . "\t".__('The value of the duties pack application is abnormal.', 'usces');
564 $logtemp .= $mes.$yn;
565 $mestemp .= $mes.$br.$yn;
566 }
567 break;
568 }
569 }
570 $opnum = ceil((count($datas) - $min_field_num) / 4);
571 for($i=0; $i<$opnum; $i++){
572 $val = array();
573 $oplogtemp = '';
574 $opmestemp = '';
575 for($o=1; $o<=4; $o++){
576 $key = ($min_field_num-1)+$o+($i*4);
577 if( isset($datas[$key]) ){
578 $value = trim($datas[$key]);
579 }else{
580 $value = NULL;
581 }
582 switch($o){
583 case 1:
584 if( empty($value) ){
585 $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option name of No.%s option is non-input.', ($i+1)), 'usces').$yn;
586 $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option name of No.%s option is non-input.', ($i+1)), 'usces').$br.$yn;
587 }
588 $val['name'] = $value;
589 break;
590 case 2:
591 if( $value != NULL && ((0 != (int)$value) and (1 != (int)$value) and (2 != (int)$value) and (5 != (int)$value)) ){
592 $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-entry-field of No.%s option is abnormal.', ($i+1)), 'usces').$yn;
593 $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-entry-field of No.%s option is abnormal.', ($i+1)), 'usces').$br.$yn;
594 }
595 $val['mean'] = $value;
596 break;
597 case 3:
598 if( $value != NULL && (!preg_match("/^[0-9]+$/", $value) || 1 < (int)$value) ){
599 $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-required-item of No.%s option is abnormal.', ($i+1)), 'usces').$yn;
600 $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-required-item of No.%s option is abnormal.', ($i+1)), 'usces').$br.$yn;
601 }
602 $val['essential'] = $value;
603 break;
604 case 4:
605 if( ($value != NULL && $value == '') && (2 > $datas[($key-2)] && 0 < strlen($datas[($key-2)])) ){
606 $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-select of No.%s option is non-input.', ($i+1)), 'usces').$yn;
607 $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-select of No.%s option is non-input.', ($i+1)), 'usces').$br.$yn;
608 }
609 $val['value'] = $value;
610 break;
611 }
612 }
613 if( !WCUtils::is_blank($val['name']) || !WCUtils::is_blank($val['mean']) || !WCUtils::is_blank($val['essential']) || !WCUtils::is_blank($val['value']) ){
614 $logtemp .= $oplogtemp;
615 $mestemp .= $opmestemp;
616 }
617 }
618 if( 0 < strlen($logtemp) ){
619 $err_num++;
620 $log .= $logtemp;
621 echo $mestemp;
622 continue;
623 }
624
625
626 /******************/
627 $checkend = microtime(true);
628 $checktime += $checkend-$lineready;
629 /*////////////////////////////////////////*/
630 // insPost
631 /*////////////////////////////////////////*/
632 //wp_posts data reg;
633 $cdatas = array();
634 $post_fields = array();
635 $sku = array();
636 $opt = array();
637 $valstr = '';
638
639 if( $pre_code != $datas[USCES_COL_ITEM_CODE] ){
640 $sku_index = 0;
641
642 $cdatas['ID'] = $post_id;
643
644 $data = $datas[USCES_COL_POST_MODIFIED];
645 if( $data == '' || $data == '0000-00-00 00:00:00' ){
646 $cdatas['post_date'] = current_time( 'mysql' );
647 $cdatas['post_date_gmt'] = current_time( 'mysql', 1 );
648 $cdatas['post_modified'] = current_time( 'mysql' );
649 $cdatas['post_modified_gmt'] = current_time( 'mysql', 1 );
650 }else{
651 if(preg_match("/^[0-9]+$/", substr($data,0,4))) {//�
652 �頭4桁が数値のみ
653 $time_data =strtotime($data);
654 } else {
655 $datetime = explode(' ', $data);
656 $date_str = usces_dates_interconv( $datetime[0] ).' '.$datetime[1];
657 $time_data =strtotime($date_str);
658 }
659 $cdatas['post_date'] = date('Y-m-d H:i:s', $time_data);
660 $cdatas['post_date_gmt'] = gmdate('Y-m-d H:i:s', $time_data);
661 $cdatas['post_modified'] = date('Y-m-d H:i:s', $time_data);
662 $cdatas['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $time_data);
663 }
664 //usces_log('mysql2date : '.mysql2date('U', $cdatas['post_modified'], false) . '/' . mysql2date('U', current_time( 'mysql' ), false), 'acting_transaction.log');
665 if ( 'publish' == $datas[USCES_COL_POST_STATUS] ) {
666 $now = current_time( 'mysql' );
667 if ( mysql2date('U', $cdatas['post_modified'], false) > mysql2date('U', $now, false) )
668 $datas[USCES_COL_POST_STATUS] = 'future';
669 } elseif( 'future' == $datas[USCES_COL_POST_STATUS] ) {
670 $now = current_time( 'mysql' );
671 if ( mysql2date('U', $cdatas['post_modified'], false) <= mysql2date('U', $now, false) )
672 $datas[USCES_COL_POST_STATUS] = 'publish';
673 }
674
675 $cdatas['ID'] = $post_id;
676 $cdatas['post_author'] = ( !WCUtils::is_blank($datas[USCES_COL_POST_AUTHOR]) ) ? $datas[USCES_COL_POST_AUTHOR] : 1;
677 $cdatas['post_content'] = trim(mb_convert_encoding($datas[USCES_COL_POST_CONTENT], 'UTF-8', 'SJIS'));
678 $cdatas['post_title'] = trim(mb_convert_encoding($datas[USCES_COL_POST_TITLE], 'UTF-8', 'SJIS'));
679 $cdatas['post_excerpt'] = trim(mb_convert_encoding($datas[USCES_COL_POST_EXCERPT], 'UTF-8', 'SJIS'));
680 $cdatas['post_status'] = $datas[USCES_COL_POST_STATUS];
681 $cdatas['comment_status'] = ( !WCUtils::is_blank($datas[USCES_COL_POST_COMMENT_STATUS]) ) ? $datas[USCES_COL_POST_COMMENT_STATUS] : 'close';
682 $cdatas['ping_status'] = 'close';
683 $cdatas['post_password'] = ( 'private' == $cdatas['post_status'] ) ? '' : $datas[USCES_COL_POST_PASSWORD];
684 $cdatas['post_type'] = 'post';
685 $cdatas['post_parent'] = 0;
686 $spname = sanitize_title(trim(mb_convert_encoding($datas[USCES_COL_POST_NAME], 'UTF-8', 'SJIS')));
687 $cdatas['post_name'] = wp_unique_post_slug($spname, $cdatas['ID'], $cdatas['post_status'], $cdatas['post_type'], $cdatas['post_parent']);
688 $cdatas['to_ping'] = '';
689 $cdatas['pinged'] = '';
690 $cdatas['menu_order'] = 0;
691 $cdatas['post_mime_type'] = 'item';
692 $cdatas['post_content_filtered'] = '';
693
694
695 if ( empty($cdatas['post_name']) && !in_array( $cdatas['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
696 $cdatas['post_name'] = sanitize_title($cdatas['post_title'], $post_id);
697 }
698
699 if($mode == 'add') {
700
701 $cdatas['guid'] = '';
702 if ( false === $wpdb->insert( $wpdb->posts, $cdatas ) ) {
703 $err_num++;
704 $mes = "No." . ($rows_num+1) . "\t".__('This data was not registered in the database.', 'usces');
705 $log .= $mes.$yn;
706 echo $mes.$br.$yn;
707 $pre_code = $datas[USCES_COL_ITEM_CODE];
708 continue;
709 }
710 $post_id = $wpdb->insert_id;
711 $where = array( 'ID' => $post_id );
712 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where );
713
714 /******************/
715 $insert_post = microtime(true);
716 $insertposttime += $insert_post-$checkend;
717
718 }elseif($mode == 'upd') {
719
720 $where = array( 'ID' => $post_id );
721 if ( false === $wpdb->update( $wpdb->posts, $cdatas, $where ) ) {
722 $err_num++;
723 $mes = "No." . ($rows_num+1) . "\t".__('The data were not registered with a database.', 'usces');
724 $log .= $mes.$yn;
725 echo $mes.$br.$yn;
726 $pre_code = $datas[USCES_COL_ITEM_CODE];
727 continue;
728 }
729
730 /******************/
731 $insert_post = microtime(true);
732 $insertposttime += $insert_post-$checkend;
733 //end of wp_insert_post
734 /************************************************************************************************/
735 /*////////////////////////////////////////*/
736 // delMeta
737 /*////////////////////////////////////////*/
738
739 //delete all metas of Item
740 //20130723ysk start 0000734
741 $meta_key_table = array( '_itemCode', '_itemName', '_itemRestriction', '_itemPointrate', '_itemGpNum1', '_itemGpDis1', '_itemGpNum2', '_itemGpDis2', '_itemGpNum3', '_itemGpDis3', '_itemShipping', '_itemDeliveryMethod', '_itemShippingCharge', '_itemIndividualSCharge', '_iopt_', '_isku_' );
742 $cfrows = explode( ';', trim(mb_convert_encoding($datas[USCES_COL_CUSTOM_FIELD], 'UTF-8', 'SJIS')) );
743 if( !(1 === count($cfrows) && '' == reset($cfrows)) ) {
744 foreach( $cfrows as $row ) {
745 list( $meta_key, $meta_value ) = explode( '=', $row, 2 );
746 if( !WCUtils::is_blank($meta_key) )
747 array_push( $meta_key_table, trim($meta_key) );
748 }
749 }
750 $meta_key_table = apply_filters( 'usces_filter_uploadcsv_delete_postmeta', $meta_key_table );
751 $query = $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key IN ( %s ) AND post_id = %d", implode( "','", $meta_key_table ), $post_id );
752 $query = stripslashes( $query );
753 //$query = $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key <> '_thumbnail_id' AND post_id = %d", $post_id);
754 //20130723ysk end
755 $dbres = $wpdb->query( $query );
756 if( $dbres === false ) {
757 $err_num++;
758 $mes = "No." . ($rows_num+1) . "\t".__('Error : delete postmeta', 'usces');
759 $log .= $mes.$yn;
760 echo $mes.$br.$yn;
761 $pre_code = $datas[USCES_COL_ITEM_CODE];
762 continue;
763 }
764 // delete Item revisions
765 $query = $wpdb->prepare("DELETE FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $post_id, 'revision');
766 $dbres = $wpdb->query( $query );
767 if( $dbres === false ) {
768 $err_num++;
769 $mes = "No." . ($rows_num+1) . "\t".__('Error : delete revisions', 'usces');
770 $log .= $mes.$yn;
771 echo $mes.$br.$yn;
772 $pre_code = $datas[USCES_COL_ITEM_CODE];
773 continue;
774 }
775 // delete relationships of category
776 $query = $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d", $post_id);
777 $dbres = $wpdb->query( $query );
778 if( $dbres === false ) {
779 $err_num++;
780 $mes = "No." . ($rows_num+1) . "\t".__('Error : delete term_relationships(category)', 'usces');
781 $log .= $mes.$yn;
782 echo $mes.$br.$yn;
783 $pre_code = $datas[USCES_COL_ITEM_CODE];
784 continue;
785 }
786 // delete relationships of tag
787 $query = "SELECT term_taxonomy_id, COUNT(*) AS ct FROM $wpdb->term_relationships GROUP BY term_taxonomy_id";
788 $relation_data = $wpdb->get_results( $query, ARRAY_A );
789 foreach((array)$relation_data as $relation_rows) {
790 $term_taxonomy_ids['term_taxonomy_id'] = $relation_rows['term_taxonomy_id'];
791 $term_taxonomy_updatas['count'] = $relation_rows['ct'];
792 $dbres = $wpdb->update( $wpdb->term_taxonomy, $term_taxonomy_updatas, $term_taxonomy_ids );
793 if( $dbres === false ) {
794 $err_num++;
795 $mes = "No." . ($rows_num+1) . "\t".__('Error : delete term_relationships(tag)', 'usces');
796 $log .= $mes.$yn;
797 echo $mes.$br.$yn;
798 $pre_code = $datas[USCES_COL_ITEM_CODE];
799 continue;
800 }
801 }
802 }
803
804 /******************/
805 $metadelete = microtime(true);
806 $metadeletetime += $metadelete-$insert_post;
807 /*////////////////////////////////////////*/
808 // addMeta
809 /*////////////////////////////////////////*/
810 //add postmeta
811 $itemDeliveryMethod = explode(';', $datas[USCES_COL_ITEM_DELIVERYMETHOD]);
812 $valstr .= '(' . $post_id . ", '_itemCode','" . mysql_real_escape_string(trim(mb_convert_encoding($datas[USCES_COL_ITEM_CODE], 'UTF-8', 'SJIS'))) . "'),";
813 $valstr .= '(' . $post_id . ", '_itemName','" . mysql_real_escape_string(trim(mb_convert_encoding($datas[USCES_COL_ITEM_NAME], 'UTF-8', 'SJIS'))) . "'),";
814 $valstr .= '(' . $post_id . ", '_itemRestriction','" . $datas[USCES_COL_ITEM_RESTRICTION] . "'),";
815 $valstr .= '(' . $post_id . ", '_itemPointrate','" . $datas[USCES_COL_ITEM_POINTRATE] . "'),";
816 $valstr .= '(' . $post_id . ", '_itemGpNum1','" . $datas[USCES_COL_ITEM_GPNUM1] . "'),";
817 $valstr .= '(' . $post_id . ", '_itemGpDis1','" . $datas[USCES_COL_ITEM_GPDIS1] . "'),";
818 $valstr .= '(' . $post_id . ", '_itemGpNum2','" . $datas[USCES_COL_ITEM_GPNUM2] . "'),";
819 $valstr .= '(' . $post_id . ", '_itemGpDis2','" . $datas[USCES_COL_ITEM_GPDIS2] . "'),";
820 $valstr .= '(' . $post_id . ", '_itemGpNum3','" . $datas[USCES_COL_ITEM_GPNUM3] . "'),";
821 $valstr .= '(' . $post_id . ", '_itemGpDis3','" . $datas[USCES_COL_ITEM_GPDIS3] . "'),";
822 $valstr .= '(' . $post_id . ", '_itemShipping','" . $datas[USCES_COL_ITEM_SHIPPING] . "'),";
823 $valstr .= '(' . $post_id . ", '_itemDeliveryMethod','" . mysql_real_escape_string(serialize($itemDeliveryMethod)) . "'),";
824 $valstr .= '(' . $post_id . ", '_itemShippingCharge','" . $datas[USCES_COL_ITEM_SHIPPINGCHARGE] . "'),";
825 $valstr .= '(' . $post_id . ", '_itemIndividualSCharge','" . $datas[USCES_COL_ITEM_INDIVIDUALSCHARGE] . "'),";
826
827 //$valstr .= '(' . $post_id . ", '".mysql_real_escape_string($meta_key)."', '" . mysql_real_escape_string(serialize($sku)) . "'),";
828
829 // print_r($valstr);
830 $valstr = rtrim($valstr, ',');
831 $dbres = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $valstr");
832 // $dbres = mysql_query($query) or die(mysql_error());
833
834 //add term_relationships, edit term_taxonomy
835 //category
836 $categories = explode(';', $datas[USCES_COL_CATEGORY]);
837 foreach((array)$categories as $category){
838 $query = $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy
839 WHERE term_id = %d", $category);
840 $term_taxonomy_id = $wpdb->get_var( $query );
841 if($term_taxonomy_id == NULL) continue;
842
843 $query = $wpdb->prepare("INSERT INTO $wpdb->term_relationships
844 (object_id, term_taxonomy_id, term_order) VALUES
845 (%d, %d, 0)",
846 $post_id, $term_taxonomy_id
847 );
848 $dbres = $wpdb->query($query);
849 if( !$dbres ) continue;
850
851 $query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships
852 WHERE term_taxonomy_id = %d", $term_taxonomy_id);
853 $tct = $wpdb->get_var( $query );
854
855 $query = $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d
856 WHERE term_taxonomy_id = %d",
857 $tct, $term_taxonomy_id
858 );
859 $dbres = $wpdb->query($query);
860 }
861 //tag
862 $tags = explode(';', trim(mb_convert_encoding($datas[USCES_COL_POST_TAG], 'UTF-8', 'SJIS')));
863 // wp_set_object_terms($post_id, (array)$tags, 'post_tag');
864 foreach((array)$tags as $tag){
865 $tag = trim($tag, " \n\t\r\0\x0B,");
866 if( empty($tag) )
867 continue;
868 $query = $wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE name = %s", $tag);
869 $term_id = $wpdb->get_var( $query );
870 //new tag
871 if( empty($term_id) ){
872 $query = $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES
873 (%s, %s, 0)", $tag, urlencode($tag));
874 $dbres = $wpdb->query( $query );
875 if( !$dbres )
876 continue;
877 $term_id = $wpdb->insert_id;
878 }
879 $query = $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy
880 WHERE term_id = %d", $term_id);
881 $term_taxonomy_id = $wpdb->get_var( $query );
882 if( !$term_taxonomy_id ){
883 $query = $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy
884 (term_id, taxonomy, description, parent, count) VALUES
885 (%d, %s, %s, 0, 0)", $term_id, 'post_tag', '');
886 $dbres = $wpdb->query( $query );
887 if( !$dbres )
888 continue;
889 $term_taxonomy_id = $wpdb->insert_id;
890 }
891 $query = $wpdb->prepare("INSERT INTO $wpdb->term_relationships
892 (object_id, term_taxonomy_id, term_order) VALUES
893 (%d, %d, 0)",
894 $post_id, $term_taxonomy_id
895 );
896 $dbres = $wpdb->query($query);
897 if( !$dbres ) continue;
898
899 $query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships
900 WHERE term_taxonomy_id = %d", $term_taxonomy_id);
901 $tct = $wpdb->get_var( $query );
902
903 $query = $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d
904 WHERE term_taxonomy_id = %d",
905 $tct, $term_taxonomy_id
906 );
907 $dbres = $wpdb->query($query);
908 }
909
910 /*////////////////////////////////////////*/
911 // Add Custom Field
912 /*////////////////////////////////////////*/
913 $cfrows = explode(';', trim(mb_convert_encoding($datas[USCES_COL_CUSTOM_FIELD], 'UTF-8', 'SJIS')));
914 if( !(1 === count($cfrows) && '' == reset($cfrows)) ){
915 $valstr = '';
916 foreach( $cfrows as $row ){
917 list($meta_key, $meta_value) = explode( '=', $row, 2 );
918 if( !WCUtils::is_blank($meta_key) )
919 $valstr .= $wpdb->prepare("( %d, %s, %s),", $post_id, trim($meta_key), trim($meta_value));
920 }
921 if( !WCUtils::is_blank($valstr) ){
922 $valstr = rtrim($valstr, ',');
923 $dbres = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $valstr");
924 }
925 }
926
927 /******************/
928 $add_postmeta = microtime(true);
929 $addmetatime += $add_postmeta-$metadelete;
930 /*////////////////////////////////////////*/
931 // addOption
932 /*////////////////////////////////////////*/
933 // Add Item Option
934 for($i=0; $i<$opnum; $i++){
935 $opflg = true;
936 $optvalue = array();
937 for($o=1; $o<=4; $o++){
938 $key = ($min_field_num-1)+$o+($i*4);
939 // if( !isset($datas[$key]) ){
940 // break 2;
941 // }
942 if( $o === 1 && $datas[$key] == '' ){
943 $opflg = false;
944 break 1;
945 }
946 switch($o){
947 case 1:
948 $optvalue['name'] = trim(mb_convert_encoding($datas[$key], 'UTF-8', 'SJIS'));
949 break;
950 case 2:
951 $optvalue['means'] = (int)$datas[$key];
952 break;
953 case 3:
954 $optvalue['essential'] = (int)$datas[$key];
955 break;
956 case 4:
957 if( !empty($datas[$key]) ) {
958 $optvalue['value'] = str_replace(';', "\n", trim(mb_convert_encoding($datas[$key], 'UTF-8', 'SJIS')));
959 }else{
960 $optvalue['value'] = "";
961 }
962 break;
963 }
964 }
965
966 if( $opflg && !empty($optvalue) ){
967 $optvalue['sort'] = $i;
968 $resopt = usces_add_opt($post_id, $optvalue, false);
969 }
970 //$valstr .= '(' . $post_id . ", '".mysql_real_escape_string($ometa_key)."', '" . mysql_real_escape_string(maybe_serialize($opt)) . "'),";
971 }
972
973 }else{
974 $sku_index++;
975 }
976
977 /******************/
978 $optionmeta = microtime(true);
979 $AddOptiontime += $optionmeta-$add_postmeta;
980 /*////////////////////////////////////////*/
981 // addSku
982 /*////////////////////////////////////////*/
983 // Add Item SKU
984 $sku_code = trim(mb_convert_encoding($datas[USCES_COL_SKU_CODE], 'UTF-8', 'SJIS'));
985 $skuvalue['code'] = $sku_code;
986 $skuvalue['name'] = trim(mb_convert_encoding($datas[USCES_COL_SKU_NAME], 'UTF-8', 'SJIS'));
987 $skuvalue['cprice'] = $datas[USCES_COL_SKU_CPRICE];
988 $skuvalue['price'] = $datas[USCES_COL_SKU_PRICE];
989 $skuvalue['unit'] = trim(mb_convert_encoding($datas[USCES_COL_SKU_UNIT], 'UTF-8', 'SJIS'));
990 $skuvalue['stocknum'] = $datas[USCES_COL_SKU_ZAIKONUM];
991 $skuvalue['stock'] = $datas[USCES_COL_SKU_ZAIKO];
992 $skuvalue['gp'] = $datas[USCES_COL_SKU_GPTEKIYO];
993 $skuvalue['sort'] = $sku_index;
994 usces_add_sku( $post_id, $skuvalue, false );
995
996 // usces_log('skuvalue : '.$post_id.print_r($skuvalue,true), 'acting_transaction.log');
997 $comp_num++;
998 $pre_code = $datas[USCES_COL_ITEM_CODE];
999 clean_post_cache($post_id);
1000 wp_cache_delete($post_id, 'posts');
1001 wp_cache_delete($post_id, 'post_meta');
1002 clean_object_term_cache($post_id, 'post');
1003 /******************/
1004 $addsku = microtime(true);
1005 $AddSKUtime += $addsku-$optionmeta;
1006 $onelinetime += $addsku-$linestart;
1007
1008 if( 0 === (($rows_num+1) % 10) ){
1009 $av = $onelinetime/$rows_num;
1010 $nt = round($av*($total_num-$rows_num));
1011 if( 60 < $nt ){
1012 $mtime = ceil($nt/60) . __('min', 'usces') . ($nt%60) . __('sec', 'usces') . ' ';
1013 }else{
1014 $mtime = $nt . __('sec', 'usces') . ' ';
1015 }
1016 echo '<script type="text/javascript">changeMsg("' . __('Processing...', 'usces') . __('Remaining time:', 'usces').$mtime.'");</script>'.$yn;
1017 echo '<script type="text/javascript">setProgress(', ($rows_num+1) . ',' . $total_num, ');</script>'.$yn;
1018 ob_flush();
1019 flush();
1020 }
1021 }
1022
1023 flock($fpi, LOCK_EX);
1024 fputs($fpi, mb_convert_encoding($log, 'SJIS', 'UTF-8'));
1025 flock($fpi, LOCK_UN);
1026 fclose($fpi);
1027
1028 $res['status'] = 'success';
1029 $res['message'] = __(sprintf('%2$s of %1$s lines registration completion, error on %3$s lines.',$total_num,$comp_num,$err_num), 'usces');
1030 /******************/
1031 $finish = microtime(true);
1032 usces_log('ready : '.round($readytime-$start, 4), 'acting_transaction.log');
1033 usces_log('lineReady : '.round($linereadytime/$total_num, 4), 'acting_transaction.log');
1034 usces_log('dataCheck : '.round($checktime/$total_num, 4), 'acting_transaction.log');
1035 usces_log('insPost : '.round($insertposttime/$total_num, 4), 'acting_transaction.log');
1036 usces_log('delMeta : '.round($metadeletetime/$total_num, 4), 'acting_transaction.log');
1037 usces_log('addMeta : '.round($addmetatime/$total_num, 4), 'acting_transaction.log');
1038 usces_log('addOption : '.round($AddOptiontime/$total_num, 4), 'acting_transaction.log');
1039 usces_log('addSku : '.round($AddSKUtime/$total_num, 4), 'acting_transaction.log');
1040 usces_log('oneline : '.round($onelinetime/$total_num, 4), 'acting_transaction.log');
1041 usces_log('finish : '.round($finish-$start, 4), 'acting_transaction.log');
1042 usces_log('totalLines: '.$total_num, 'acting_transaction.log');
1043 usces_log('--------------------------------------------------------------', 'acting_transaction.log');
1044
1045 echo '<script type="text/javascript">changeMsg("', __(sprintf('Successful %1$s lines, Failed %2$s lines.',$comp_num,$err_num), 'usces'), ' ");setProgress(', ($rows_num+1) . ',' . $total_num, ');</script>'.$yn;
1046 if( $log ){
1047 //echo '<div class="error_log">' . $log . '</div>'.$yn;
1048 }else{
1049 echo __('Finished', 'usces').$yn;
1050 }
1051 echo '</div>'.$yn;
1052 unlink($path.$file_name);
1053 return $res;
1054 }
1055 endif;
1056
1057 // item list download
1058 if( !function_exists('usces_download_item_list') ):
1059 function usces_download_item_list() {
1060 require_once( USCES_PLUGIN_DIR . "/classes/itemList.class.php" );
1061 global $wpdb, $usces;
1062
1063 $ext = 'csv';
1064 if($ext == 'csv') {//CSV
1065 $table_h = "";
1066 $table_f = "";
1067 $tr_h = "";
1068 $tr_f = "";
1069 $th_h1 = '"';
1070 $th_h = ',"';
1071 $th_f = '"';
1072 $td_h1 = '"';
1073 $td_h = ',"';
1074 $td_f = '"';
1075 $sp = ";";
1076 $eq = "=";
1077 $lf = "\n";
1078 } else {
1079 exit();
1080 }
1081
1082 //==========================================================================
1083 $usces_opt_item = get_option('usces_opt_item');
1084 if(!is_array($usces_opt_item)){
1085 $usces_opt_item = array();
1086 }
1087 $usces_opt_item['chk_header'] = (isset($_REQUEST['chk_header'])) ? 1 : 0;
1088 $usces_opt_item['ftype_item'] = $ext;
1089 update_option('usces_opt_item', $usces_opt_item);
1090 //==========================================================================
1091
1092 $tableName = $wpdb->posts;
1093 if( USCES_MYSQL_VERSION >= 5 ) {
1094 $arr_column = array(
1095 __('item code', 'usces') => 'item_code',
1096 __('item name', 'usces') => 'item_name',
1097 __('SKU code', 'usces') => 'sku_key',
1098 __('selling price', 'usces') => 'price',
1099 __('stock', 'usces') => 'zaiko_num',
1100 __('stock status', 'usces') => 'zaiko',
1101 __('Categories', 'usces') => 'category',
1102 __('display status', 'usces') => 'display_status');
1103 } else {
1104 $arr_column = array(
1105 __('item code', 'usces') => 'item_code',
1106 __('page title', 'usces') => 'post_title',
1107 __('SKU code', 'usces') => 'sku_key',
1108 __('selling price', 'usces') => 'price',
1109 __('stock', 'usces') => 'zaiko_num',
1110 __('stock status', 'usces') => 'zaiko',
1111 __('Categories', 'usces') => 'category',
1112 __('display status', 'usces') => 'display_status');
1113 }
1114
1115 $_REQUEST['searchIn'] = "searchIn";
1116 $DT = new dataList($tableName, $arr_column);
1117 $DT->pageLimit = 'off';
1118 $DT->exportMode = true;
1119 $res = $DT->MakeTable();
1120 $rows = $DT->rows;
1121 //==========================================================================
1122 $line = $table_h;
1123 if($usces_opt_item['chk_header'] == 1) {
1124 $line .= $tr_h;
1125 $line .= $th_h1.'Post ID'.$th_f;
1126 $line .= $th_h.__('Post Author', 'usces').$th_f;
1127 $line .= $th_h.__('explanation', 'usces').$th_f;
1128 $line .= $th_h.__('Title', 'usces').$th_f;
1129 $line .= $th_h.__('excerpt', 'usces').$th_f;
1130 $line .= $th_h.__('display status', 'usces').$th_f;
1131 $line .= $th_h.__('Comment Status', 'usces').$th_f;
1132 $line .= $th_h.__('Post PAssword', 'usces').$th_f;
1133 $line .= $th_h.__('Post Name', 'usces').$th_f;
1134 $line .= $th_h.__('Publish on:').$th_f;
1135
1136 $line .= $th_h.__('item code', 'usces').$th_f;
1137 $line .= $th_h.__('item name', 'usces').$th_f;
1138 $line .= $th_h.__('Limited amount for purchase', 'usces').$th_f;
1139 $line .= $th_h.__('Percentage of points', 'usces').$th_f;
1140 $line .= $th_h.__('Business package discount', 'usces').'1-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'1-'.__('rate', 'usces').$th_f;
1141 $line .= $th_h.__('Business package discount', 'usces').'2-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'2-'.__('rate', 'usces').$th_f;
1142 $line .= $th_h.__('Business package discount', 'usces').'3-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'3-'.__('rate', 'usces').$th_f;
1143 $line .= $th_h.__('estimated shipping date', 'usces').$th_f;
1144 $line .= $th_h.__('shipping option', 'usces').$th_f;
1145 $line .= $th_h.__('Shipping', 'usces').$th_f;
1146 $line .= $th_h.__('Postage individual charging', 'usces').$th_f;
1147
1148 $line .= $th_h.__('Categories', 'usces').$th_f;
1149 $line .= $th_h.__('tag', 'usces').$th_f;
1150 $line .= $th_h.__('Custom Field', 'usces').$th_f;
1151
1152 $line .= $th_h.__('SKU code', 'usces').$th_f;
1153 $line .= $th_h.__('SKU display name ', 'usces').$th_f;
1154 $line .= $th_h.__('normal price', 'usces').$th_f;
1155 $line .= $th_h.__('Sale price', 'usces').$th_f;
1156 $line .= $th_h.__('stock', 'usces').$th_f;
1157 $line .= $th_h.__('stock status', 'usces').$th_f;
1158 $line .= $th_h.__('unit', 'usces').$th_f;
1159 $line .= $th_h.__('Apply business package', 'usces').$th_f;
1160 $line .= $th_h.__('option name', 'usces').$th_f.$th_h.__('Field type', 'usces').$th_f.$th_h.__('Required', 'usces').$th_f.$th_h.__('selected amount', 'usces').$th_f;
1161 $line .= $tr_f.$lf;
1162 }
1163 //==========================================================================
1164 mb_http_output('pass');
1165 set_time_limit(3600);
1166 header("Content-Type: application/octet-stream");
1167 header("Content-Disposition: attachment; filename=usces_item_list.".$ext);
1168 ob_end_flush();
1169 flush();
1170
1171 foreach((array)$rows as $array) {
1172 $post_id = $array['ID'];
1173 $post = get_post($post_id);
1174
1175 //Post Data
1176 $line_item = $td_h1.$post->ID.$td_f;
1177 $line_item .= $td_h.$post->post_author.$td_f;
1178 $line_item .= $td_h.usces_entity_decode($post->post_content, $ext).$td_f;
1179 $line_item .= $td_h.usces_entity_decode($post->post_title, $ext).$td_f;
1180 $line_item .= $td_h.usces_entity_decode($post->post_excerpt, $ext).$td_f;
1181 $line_item .= $td_h.$post->post_status.$td_f;
1182 $line_item .= $td_h.$post->comment_status.$td_f;
1183 $line_item .= $td_h.$post->post_password.$td_f;
1184 $line_item .= $td_h.urldecode($post->post_name).$td_f;
1185 $line_item .= $td_h.$post->post_date.$td_f;
1186
1187 //Item Meta
1188 $line_item .= $td_h.$usces->getItemCode($post_id).$td_f;
1189 $line_item .= $td_h.usces_entity_decode($usces->getItemName($post_id), $ext).$td_f;
1190 $line_item .= $td_h.$usces->getItemRestriction($post_id).$td_f;
1191 $line_item .= $td_h.$usces->getItemPointrate($post_id).$td_f;
1192 $line_item .= $td_h.$usces->getItemGpNum1($post_id).$td_f.$td_h.$usces->getItemGpDis1($post_id).$td_f;
1193 $line_item .= $td_h.$usces->getItemGpNum2($post_id).$td_f.$td_h.$usces->getItemGpDis2($post_id).$td_f;
1194 $line_item .= $td_h.$usces->getItemGpNum3($post_id).$td_f.$td_h.$usces->getItemGpDis3($post_id).$td_f;
1195 $line_item .= $td_h.$usces->getItemShipping($post_id).$td_f;
1196 $delivery_method = '';
1197 $itemDeliveryMethod = $usces->getItemDeliveryMethod($post_id);
1198 foreach((array)$itemDeliveryMethod as $k => $v) {
1199 $delivery_method .= $v.$sp;
1200 }
1201 $delivery_method = rtrim($delivery_method, $sp);
1202 $line_item .= $td_h.$delivery_method.$td_f;
1203 $line_item .= $td_h.$usces->getItemShippingCharge($post_id).$td_f;
1204 $line_item .= $td_h.$usces->getItemIndividualSCharge($post_id).$td_f;
1205
1206 //Categories
1207 $category = '';
1208 $cat_ids = wp_get_post_categories($post_id);
1209 if(!empty($cat_ids)) {
1210 foreach($cat_ids as $id) {
1211 $category .= $id.$sp;
1212 }
1213 $category = rtrim($category, $sp);
1214 }
1215 $line_item .= $td_h.$category.$td_f;
1216
1217 //Tags
1218 $tag = '';
1219 $tags_ob = wp_get_object_terms($post_id, 'post_tag');
1220 foreach($tags_ob as $ob) {
1221 $tag .= $ob->name.$sp;
1222 }
1223 $tag = rtrim($tag, $sp);
1224 $line_item .= $td_h.$tag.$td_f;
1225
1226 //Custom Fields
1227 $cfield = '';
1228 $custom_fields = $usces->get_post_user_custom($post_id);
1229 if( is_array($custom_fields) && 0 < count($custom_fields) ){
1230 foreach($custom_fields as $cfkey => $cfvalues ) {
1231 if( is_array($cfvalues) )
1232 $cfield .= usces_entity_decode($cfkey, $ext) . $eq . usces_entity_decode($cfvalues[0], $ext) . $sp;
1233 else
1234 $cfield .= usces_entity_decode($cfkey, $ext) . $eq . usces_entity_decode($cfvalues, $ext) . $sp;
1235 }
1236 $cfield = rtrim($cfield, $sp);
1237 }
1238 $line_item .= $td_h.$cfield.$td_f;
1239
1240 //Item Options
1241 $line_options = '';
1242 $option_meta = usces_get_opts( $post_id, 'sort' );
1243 foreach($option_meta as $option_value) {
1244 $value = '';
1245 if(is_array($option_value['value'])) {
1246 foreach($option_value['value'] as $k => $v) {
1247 $values = explode("\n", $v);
1248 foreach($values as $val) {
1249 $value .= $val.$sp;
1250 }
1251 }
1252 $value = rtrim($value, $sp);
1253 } else {
1254 $value = trim($option_value['value']);
1255 $value = str_replace("\n", ';', $value);
1256 }
1257
1258 $line_options .= $td_h.usces_entity_decode($option_value['name'], $ext).$td_f;
1259 $line_options .= $td_h.$option_value['means'].$td_f;
1260 $line_options .= $td_h.$option_value['essential'].$td_f;
1261 $line_options .= $td_h.usces_entity_decode($value, $ext).$td_f;
1262
1263 }
1264
1265 //SKUs
1266 $sku_meta = $usces->get_skus( $post_id, 'sort' );
1267 foreach($sku_meta as $sku_value) {
1268
1269 $line_sku = $td_h.$sku_value['code'].$td_f;
1270 $line_sku .= $td_h.usces_entity_decode($sku_value['name'], $ext).$td_f;
1271 $line_sku .= $td_h.usces_crform($sku_value['cprice'], false, false, 'return', false).$td_f;
1272 $line_sku .= $td_h.usces_crform($sku_value['price'], false, false, 'return', false).$td_f;
1273 $line_sku .= $td_h.$sku_value['stocknum'].$td_f;
1274 $line_sku .= $td_h.$sku_value['stock'].$td_f;
1275 $line_sku .= $td_h.usces_entity_decode($sku_value['unit'], $ext).$td_f;
1276 $line_sku .= $td_h.$sku_value['gp'].$td_f;
1277
1278 $line .= $tr_h.$line_item.$line_sku.$line_options.$tr_f.$lf;
1279 }
1280 print(mb_convert_encoding($line, "SJIS-win", "UTF-8"));
1281 if(ob_get_contents ()){
1282 ob_flush();
1283 flush();
1284 }
1285 $line = '';
1286 wp_cache_flush();
1287 }
1288 $end = $table_f.$lf;
1289 //==========================================================================
1290 print(mb_convert_encoding($end, "SJIS-win", "UTF-8"));
1291 unset($rows, $DT, $line, $line_item, $line_options, $line_sku);
1292 exit();
1293 }
1294 endif;
1295
1296 }
1297 ?>