PluginProbe
Welcart e-Commerce / 1.3.3
Welcart e-Commerce v1.3.3
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.3, at functions/define_function.php

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