-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathSC_Helper_DB.php
More file actions
1785 lines (1579 loc) · 60 KB
/
SC_Helper_DB.php
File metadata and controls
1785 lines (1579 loc) · 60 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* DB関連のヘルパークラス.
*
* @package Helper
* @author EC-CUBE CO.,LTD.
* @version $Id$
*/
// NOTE: PHP5 対応が不要となったらクラス定数に変更する。
define('SC_HELPER_DB_BASIS_DATA_CACHE_REALFILE', MASTER_DATA_REALDIR.'dtb_baseinfo.serial');
class SC_Helper_DB
{
/** ルートカテゴリ取得フラグ */
public $g_root_on;
/** ルートカテゴリID */
public $g_root_id;
/** 選択中カテゴリ取得フラグ */
public $g_category_on;
/** 選択中カテゴリID */
public $g_category_id;
/** @var bool */
public $g_maker_on;
/** @var array */
public $g_maker_id;
/**
* カラムの存在チェックと作成を行う.
*
* チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
* 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
* カラムの生成も行う場合は, $col_type も必須となる.
*
* @param string $tableName テーブル名
* @param string $colType カラムのデータ型
* @param string $dsn データソース名
* @param bool $add カラムの作成も行う場合 true
*
* @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
* テーブルが存在しない場合 false,
* 引数 $add == false でカラムが存在しない場合 false
*/
public static function sfColumnExists($tableName, $colName, $colType = '', $dsn = '', $add = false)
{
$dbFactory = SC_DB_DBFactory_Ex::getInstance();
$dsn = $dbFactory->getDSN($dsn);
$objQuery = SC_Query_Ex::getSingletonInstance($dsn);
// テーブルが無ければエラー
if (!in_array($tableName, $objQuery->listTables())) {
return false;
}
// 正常に接続されている場合
if (!$objQuery->isError()) {
// カラムリストを取得
$columns = $objQuery->listTableFields($tableName);
if (in_array($colName, $columns)) {
return true;
}
}
// カラムを追加する
if ($add) {
return static::sfColumnAdd($tableName, $colName, $colType);
}
return false;
}
/**
* @param string $colType
* @param string $tableName
*/
public static function sfColumnAdd($tableName, $colName, $colType)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
return $objQuery->query("ALTER TABLE $tableName ADD $colName $colType ");
}
/**
* データの存在チェックを行う.
*
* @param string $tableName テーブル名
* @param string $where データを検索する WHERE 句
* @param array $arrWhereVal WHERE句のプレースホルダ値
*
* @return bool データが存在する場合 true, データの追加に成功した場合 true,
* $add == false で, データが存在しない場合 false
*/
public static function sfDataExists($tableName, $where, $arrWhereVal)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$exists = $objQuery->exists($tableName, $where, $arrWhereVal);
return $exists;
}
/**
* 店舗基本情報を取得する.
*
* 引数 $force が false の場合は, キャッシュされた結果を使用する.
*
* @param bool $force キャッシュファイルを生成し、ローカルキャッシュを削除するか
*
* @return array 店舗基本情報の配列
*/
public static function sfGetBasisData($force = false)
{
static $arrData = null;
// キャッシュファイルが存在しない場合、キャッシュファイルを生成する
if (!$force && !file_exists(SC_HELPER_DB_BASIS_DATA_CACHE_REALFILE)) {
$force = true;
}
if ($force) {
// キャッシュファイルを生成
$success = SC_Helper_DB_Ex::sfCreateBasisDataCache();
// ローカルキャッシュを削除
$arrData = null;
}
// ローカルキャッシュが無い場合、キャッシュファイルを読み込む
if (is_null($arrData)) {
// キャッシュデータファイルを読み込む
$arrData = SC_Helper_DB_Ex::getBasisDataFromCacheFile();
}
return $arrData;
}
/**
* 基本情報のキャッシュデータを取得する
*
* エラー画面表示で直接呼ばれる。キャッシュファイルが存在しなくとも空の配列を応答することで、(幾らかの情報欠落などはあるかもしれないが) エラー画面の表示できるよう考慮している。
*
* @param bool $generate キャッシュファイルが無い時、DBのデータを基にキャッシュを生成するか
*
* @return array 店舗基本情報の配列
*
* @deprecated 2.17.1 本体で使用されていないため非推奨
*/
public static function sfGetBasisDataCache($generate = false)
{
$cacheData = [];
// ファイル存在確認
if (!file_exists(SC_HELPER_DB_BASIS_DATA_CACHE_REALFILE) && $generate) {
// 存在していなければキャッシュ生成
static::sfCreateBasisDataCache();
}
$cacheData = SC_Helper_DB_Ex::getBasisDataFromCacheFile(true);
return $cacheData;
}
/**
* 基本情報のキャッシュデータを取得する
*
* エラー画面表示で直接呼ばれる。キャッシュファイルが存在しなくとも空の配列を応答することで、(幾らかの情報欠落などはあるかもしれないが) エラー画面の表示できるよう考慮している。
*
* @param bool $ignore_error エラーを無視するか
*
* @return array 店舗基本情報の配列
*/
public static function getBasisDataFromCacheFile($ignore_error = false)
{
$arrReturn = [];
// ファイル存在確認
if (file_exists(SC_HELPER_DB_BASIS_DATA_CACHE_REALFILE)) {
// キャッシュデータファイルを読み込みアンシリアライズした配列を取得
$arrReturn = unserialize(file_get_contents(SC_HELPER_DB_BASIS_DATA_CACHE_REALFILE));
} elseif (!$ignore_error) {
throw new Exception('基本情報のキャッシュデータファイルが存在しません。');
}
return $arrReturn;
}
/**
* 店舗基本情報をDBから取得する.
*
* @return array 店舗基本情報の配列
*/
public static function getBasisDataFromDB()
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$arrReturn = $objQuery->getRow('*', 'dtb_baseinfo');
return $arrReturn;
}
/**
* 基本情報のキャッシュデータファイルを生成する
* データはsfGetBasisDataより取得。
*
* このメソッドが直接呼ばれるのは、
*「基本情報管理>SHOPマスター」の更新完了後。
* sfGetBasisDataCacheでは、
* キャッシュデータファイルが無い場合に呼ばれます。
*
* @return bool キャッシュデータファイル生成結果
*/
public static function sfCreateBasisDataCache()
{
// データ取得
$arrData = static::getBasisDataFromDB();
// シリアライズ
$data = serialize($arrData);
// ファイルを書き出しモードで開く
$handle = fopen(SC_HELPER_DB_BASIS_DATA_CACHE_REALFILE, 'w');
if (!$handle) {
// ファイル生成失敗
return false;
}
// ファイルの内容を書き出す.
$res = fwrite($handle, $data);
// ファイルを閉じる
fclose($handle);
if ($res === false) {
// ファイル生成失敗
return false;
}
// ファイル生成成功
return true;
}
/**
* 基本情報の登録数を取得する
*
* @return int
*
* @deprecated
*/
public function sfGetBasisCount()
{
$objQuery = SC_Query_Ex::getSingletonInstance();
return $objQuery->count('dtb_baseinfo');
}
/**
* 基本情報の登録有無を取得する
*
* @return bool 有無
*/
public function sfGetBasisExists()
{
$objQuery = SC_Query_Ex::getSingletonInstance();
return $objQuery->exists('dtb_baseinfo');
}
/**
* 選択中のアイテムのルートカテゴリIDを取得する
*
* @deprecated 本体で使用されていないため非推奨
*/
public function sfGetRootId()
{
if (!$this->g_root_on) {
$this->g_root_on = true;
if (!isset($_GET['product_id'])) {
$_GET['product_id'] = '';
}
if (!isset($_GET['category_id'])) {
$_GET['category_id'] = '';
}
if (!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
// 選択中のカテゴリIDを判定する
$category_id = SC_Helper_DB_Ex::sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
// ROOTカテゴリIDの取得
if (count($category_id) > 0) {
$arrRet = static::sfGetParentsArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
$root_id = $arrRet[0] ?? '';
} else {
$root_id = '';
}
} else {
// ROOTカテゴリIDをなしに設定する
$root_id = '';
}
$this->g_root_id = $root_id;
}
return $this->g_root_id;
}
/**
* 受注番号、最終ポイント、加算ポイント、利用ポイントから「オーダー前ポイント」を取得する
*
* @param int $order_id 受注番号
* @param int $use_point 利用ポイント
* @param int $add_point 加算ポイント
* @param int $order_status 対応状況
*
* @return array オーダー前ポイントの配列
*/
public static function sfGetRollbackPoint($order_id, $use_point, $add_point, $order_status)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$arrRet = $objQuery->getRow('customer_id', 'dtb_order', 'order_id = ?', [$order_id]);
if (isset($arrRet['customer_id']) && $arrRet['customer_id'] >= 1) {
$arrRet = $objQuery->getRow('point', 'dtb_customer', 'customer_id = ?', [$arrRet['customer_id']]);
$point = $arrRet['point'];
$rollback_point = $arrRet['point'];
// 対応状況がポイント利用対象の場合、使用ポイント分を戻す
if ((new SC_Helper_Purchase_Ex())->isUsePoint($order_status)) {
$rollback_point += $use_point;
}
// 対応状況がポイント加算対象の場合、加算ポイント分を戻す
if (SC_Helper_Purchase_Ex::isAddPoint($order_status)) {
$rollback_point -= $add_point;
}
} else {
$rollback_point = '';
$point = '';
}
return [$point, $rollback_point];
}
/**
* カテゴリツリーの取得を行う.
*
* @param int $parent_category_id 親カテゴリID
* @param bool $count_check 登録商品数のチェックを行う場合 true
*
* @return array カテゴリツリーの配列
*/
public static function sfGetCatTree($parent_category_id, $count_check = false)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$col = '';
$col .= ' cat.category_id,';
$col .= ' cat.category_name,';
$col .= ' cat.parent_category_id,';
$col .= ' cat.level,';
$col .= ' cat.rank,';
$col .= ' cat.creator_id,';
$col .= ' cat.create_date,';
$col .= ' cat.update_date,';
$col .= ' cat.del_flg, ';
$col .= ' ttl.product_count';
$from = 'dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id';
// 登録商品数のチェック
if ($count_check) {
$where = 'del_flg = 0 AND product_count > 0';
} else {
$where = 'del_flg = 0';
}
$objQuery->setOption('ORDER BY rank DESC');
$arrRet = $objQuery->select($col, $from, $where);
$arrParentID = SC_Utils_Ex::getTreeTrail($parent_category_id, 'category_id', 'parent_category_id', $arrRet);
foreach ($arrRet as $key => $array) {
foreach ($arrParentID as $val) {
if ($array['category_id'] == $val) {
$arrRet[$key]['display'] = 1;
break;
}
}
}
return $arrRet;
}
/**
* カテゴリツリーを走査し, パンくずリスト用の配列を生成する.
*
* @param array カテゴリの配列
* @param int $parent 上位カテゴリID
* @param array パンくずリスト用の配列
*
* @result void
*
* @see sfGetCatTree()
* @deprecated 本体で使用されていないため非推奨
*/
public function findTree(&$arrTree, $parent, &$result)
{
if ($result[count($result) - 1]['parent_category_id'] === 0) {
return;
} else {
foreach ($arrTree as $val) {
if ($val['category_id'] == $parent) {
$result[] = [
'category_id' => $val['category_id'],
'parent_category_id' => (int) $val['parent_category_id'],
'category_name' => $val['category_name'],
];
$this->findTree($arrTree, $val['parent_category_id'], $result);
}
}
}
}
/**
* カテゴリツリーの取得を複数カテゴリで行う.
*
* @param int $product_id 商品ID
* @param bool $count_check 登録商品数のチェックを行う場合 true
*
* @return array カテゴリツリーの配列
*/
public static function sfGetMultiCatTree($product_id, $count_check = false)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$col = '';
$col .= ' cat.category_id,';
$col .= ' cat.category_name,';
$col .= ' cat.parent_category_id,';
$col .= ' cat.level,';
$col .= ' cat.rank,';
$col .= ' cat.creator_id,';
$col .= ' cat.create_date,';
$col .= ' cat.update_date,';
$col .= ' cat.del_flg, ';
$col .= ' ttl.product_count';
$from = 'dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id';
// 登録商品数のチェック
if ($count_check) {
$where = 'del_flg = 0 AND product_count > 0';
} else {
$where = 'del_flg = 0';
}
$objQuery->setOption('ORDER BY rank DESC');
$arrRet = $objQuery->select($col, $from, $where);
$arrCategory_id = SC_Helper_DB_Ex::sfGetCategoryId($product_id);
$arrCatTree = [];
foreach ($arrCategory_id as $pkey => $parent_category_id) {
$arrParentID = SC_Helper_DB_Ex::sfGetParentsArray('dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
foreach ($arrParentID as $pid) {
foreach ($arrRet as $key => $array) {
if ($array['category_id'] == $pid) {
$arrCatTree[$pkey][] = $arrRet[$key];
break;
}
}
}
}
return $arrCatTree;
}
/**
* 親カテゴリを連結した文字列を取得する.
*
* @param int $category_id カテゴリID
*
* @return string 親カテゴリを連結した文字列
*
* @deprecated 本体で使用されていないため非推奨
*/
public function sfGetCatCombName($category_id)
{
// 商品が属するカテゴリIDを縦に取得
$objQuery = SC_Query_Ex::getSingletonInstance();
$arrCatID = static::sfGetParentsArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
$ConbName = '';
// カテゴリ名称を取得する
foreach ($arrCatID as $val) {
$sql = 'SELECT category_name FROM dtb_category WHERE category_id = ?';
$arrVal = [$val];
$CatName = $objQuery->getOne($sql, $arrVal);
$ConbName .= $CatName.' | ';
}
// 最後の | をカットする
$ConbName = substr_replace($ConbName, '', strlen($ConbName) - 2, 2);
return $ConbName;
}
/**
* 指定したカテゴリIDの大カテゴリを取得する.
*
* @param int $category_id カテゴリID
*
* @return array 指定したカテゴリIDの大カテゴリ
*
* @deprecated 本体で使用されていないため非推奨
*/
public function sfGetFirstCat($category_id)
{
// 商品が属するカテゴリIDを縦に取得
$objQuery = SC_Query_Ex::getSingletonInstance();
$arrRet = [];
$arrCatID = static::sfGetParentsArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
$arrRet['id'] = $arrCatID[0];
// カテゴリ名称を取得する
$sql = 'SELECT category_name FROM dtb_category WHERE category_id = ?';
$arrVal = [$arrRet['id']];
$arrRet['name'] = $objQuery->getOne($sql, $arrVal);
return $arrRet;
}
/**
* カテゴリツリーの取得を行う.
*
* $products_check:true商品登録済みのものだけ取得する
*
* @param string $addwhere 追加する WHERE 句
* @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
* @param string $head カテゴリ名のプレフィックス文字列
*
* @return array カテゴリツリーの配列
*/
public static function sfGetCategoryList($addwhere = '', $products_check = false, $head = CATEGORY_HEAD)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$where = 'del_flg = 0';
if ($addwhere != '') {
$where .= " AND $addwhere";
}
$objQuery->setOption('ORDER BY rank DESC');
if ($products_check) {
$col = 'T1.category_id, category_name, level';
$from = 'dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id';
$where .= ' AND product_count > 0';
} else {
$col = 'category_id, category_name, level';
$from = 'dtb_category';
}
$arrRet = $objQuery->select($col, $from, $where);
$max = count($arrRet);
$arrList = [];
for ($cnt = 0; $cnt < $max; $cnt++) {
$id = $arrRet[$cnt]['category_id'];
$name = $arrRet[$cnt]['category_name'];
$arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']).$name;
}
return $arrList;
}
/**
* カテゴリツリーの取得を行う.
*
* 親カテゴリの Value=0 を対象とする
*
* @param bool $parent_zero 親カテゴリの Value=0 の場合 true
*
* @return array カテゴリツリーの配列
*/
public static function sfGetLevelCatList($parent_zero = true)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
// カテゴリ名リストを取得
$col = 'category_id, parent_category_id, category_name';
$where = 'del_flg = 0';
$objQuery->setOption('ORDER BY level');
$arrRet = $objQuery->select($col, 'dtb_category', $where);
$arrCatName = [];
foreach ($arrRet as $arrTmp) {
$arrCatName[$arrTmp['category_id']] =
(($arrTmp['parent_category_id'] > 0) ?
$arrCatName[$arrTmp['parent_category_id']] : '')
.CATEGORY_HEAD.$arrTmp['category_name'];
}
$col = 'category_id, parent_category_id, category_name, level';
$where = 'del_flg = 0';
$objQuery->setOption('ORDER BY rank DESC');
$arrRet = $objQuery->select($col, 'dtb_category', $where);
$max = count($arrRet);
$arrValue = [];
$arrOutput = [];
for ($cnt = 0; $cnt < $max; $cnt++) {
if ($parent_zero) {
if ($arrRet[$cnt]['level'] == LEVEL_MAX) {
$arrValue[$cnt] = $arrRet[$cnt]['category_id'];
} else {
$arrValue[$cnt] = '';
}
} else {
$arrValue[$cnt] = $arrRet[$cnt]['category_id'];
}
$arrOutput[$cnt] = $arrCatName[$arrRet[$cnt]['category_id']];
}
return [$arrValue, $arrOutput];
}
/**
* 選択中の商品のカテゴリを取得する.
*
* 引数のカテゴリIDが有効な場合は, カテゴリIDを含んだ配列を返す
* 引数のカテゴリIDが無効な場合, dtb_product_categories にレコードが存在する場合は, カテゴリIDを含んだ配列を返す
*
* @param int $product_id プロダクトID
* @param int $category_id カテゴリID
* @param bool $closed 引数のカテゴリIDが無効な場合で, 非表示の商品を含む場合はtrue
*
* @return array 選択中の商品のカテゴリIDの配列
*/
public static function sfGetCategoryId($product_id, $category_id = 0, $closed = false)
{
if ($closed) {
$status = '';
} else {
$status = 'status = 1';
}
$category_id = (int) $category_id;
$product_id = (int) $product_id;
$objCategory = new SC_Helper_Category_Ex();
// XXX SC_Helper_Category::isValidCategoryId() で使用している SC_Helper_DB::sfIsRecord() が内部で del_flg = 0 を追加するため, $closed は機能していない
if ($objCategory->isValidCategoryId($category_id, $closed)) {
$category_id = [$category_id];
} elseif (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $product_id, $status)) {
$objQuery = SC_Query_Ex::getSingletonInstance();
$category_id = $objQuery->getCol('category_id', 'dtb_product_categories', 'product_id = ?', [$product_id]);
} else {
// 不正な場合は、空の配列を返す。
$category_id = [];
}
return $category_id;
}
/**
* 商品をカテゴリの先頭に追加する.
*
* @param int $category_id カテゴリID
* @param int $product_id プロダクトID
*
* @return void
*/
public function addProductBeforCategories($category_id, $product_id)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$sqlval = [
'category_id' => $category_id,
'product_id' => $product_id,
];
$arrSql = [];
$arrSql['rank'] = '(SELECT COALESCE(MAX(rank), 0) FROM dtb_product_categories sub WHERE category_id = ?) + 1';
$from_and_where = $objQuery->dbFactory->getDummyFromClauseSql();
$from_and_where .= ' WHERE NOT EXISTS(SELECT * FROM dtb_product_categories WHERE category_id = ? AND product_id = ?)';
$objQuery->insert('dtb_product_categories', $sqlval, $arrSql, [$category_id], $from_and_where, [$category_id, $product_id]);
}
/**
* 商品をカテゴリの末尾に追加する.
*
* @param int $category_id カテゴリID
* @param int $product_id プロダクトID
*
* @return void
*
* @deprecated 本体で使用されていないため非推奨
*/
public function addProductAfterCategories($category_id, $product_id)
{
$sqlval = [
'category_id' => $category_id,
'product_id' => $product_id,
];
$objQuery = SC_Query_Ex::getSingletonInstance();
// 現在の商品カテゴリを取得
$arrCat = $objQuery->select(
'product_id, category_id, rank',
'dtb_product_categories',
'category_id = ?',
[$category_id]
);
$min = 0;
foreach ($arrCat as $val) {
// 同一商品が存在する場合は登録しない
if ($val['product_id'] == $product_id) {
return;
}
// 最下位ランクを取得
$min = ($min < $val['rank']) ? $val['rank'] : $min;
}
$sqlval['rank'] = $min;
$objQuery->insert('dtb_product_categories', $sqlval);
}
/**
* 商品をカテゴリから削除する.
*
* @param int $category_id カテゴリID
* @param int $product_id プロダクトID
*
* @return void
*/
public function removeProductByCategories($category_id, $product_id)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
$objQuery->delete(
'dtb_product_categories',
'category_id = ? AND product_id = ?',
[$category_id, $product_id]
);
}
/**
* 商品カテゴリを更新する.
*
* @param array $arrCategory_id 登録するカテゴリIDの配列
* @param int $product_id プロダクトID
*
* @return void
*/
public function updateProductCategories($arrCategory_id, $product_id)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
// 現在のカテゴリ情報を取得
$arrCurrentCat = $objQuery->getCol(
'category_id',
'dtb_product_categories',
'product_id = ?',
[$product_id]
);
// 登録するカテゴリ情報と比較
foreach ($arrCurrentCat as $category_id) {
// 登録しないカテゴリを削除
if (!in_array($category_id, $arrCategory_id)) {
$this->removeProductByCategories($category_id, $product_id);
}
}
// カテゴリを登録
foreach ($arrCategory_id as $category_id) {
$this->addProductBeforCategories($category_id, $product_id);
SC_Utils_Ex::extendTimeOut();
}
}
/**
* カテゴリ数の登録を行う.
*
* @param SC_Query $objQuery SC_Query インスタンス
* @param bool $is_force_all_count 全カテゴリの集計を強制する場合 true
* @param bool $is_nostock_hidden 在庫切れの商品は非表示にする場合 true
*
* @return void
*/
public function sfCountCategory($objQuery = null, $is_force_all_count = false, $is_nostock_hidden = NOSTOCK_HIDDEN)
{
$objProduct = new SC_Product_Ex();
if ($objQuery == null) {
$objQuery = SC_Query_Ex::getSingletonInstance();
}
$is_out_trans = false;
if (!$objQuery->inTransaction()) {
$objQuery->begin();
$is_out_trans = true;
}
// 共通のfrom/where文の構築
$where_alldtl = SC_Product_Ex::getProductDispConditions('alldtl');
// 在庫無し商品の非表示
if ($is_nostock_hidden) {
$from_alldtl = $objProduct->alldtlSQL('(stock >= 1 OR stock_unlimited = 1)');
} else {
$from_alldtl = 'dtb_products as alldtl';
}
// dtb_category_countの構成
// 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを含まない。
if ($is_force_all_count) {
$objQuery->delete('dtb_category_count');
$arrCategoryCountOld = [];
} else {
// テーブル内容の元を取得
$arrCategoryCountOld = $objQuery->select('category_id, product_count', 'dtb_category_count');
}
// 各カテゴリ内の商品数を数えて取得
$sql = <<< __EOS__
SELECT T1.category_id, count(*) as product_count
FROM dtb_category AS T1
INNER JOIN dtb_product_categories AS T2
ON T1.category_id = T2.category_id
INNER JOIN $from_alldtl
ON T2.product_id = alldtl.product_id
AND $where_alldtl
WHERE T1.del_flg = 0
GROUP BY T1.category_id
HAVING count(*) <> 0
__EOS__;
$arrCategoryCountNew = $objQuery->getAll($sql);
// 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。
// 差分を取得して、更新対象カテゴリだけを確認する。
// 各カテゴリ毎のデータ値において以前との差を見る
// 古いデータの構造入れ替え
$arrOld = [];
foreach ($arrCategoryCountOld as $item) {
$arrOld[$item['category_id']] = $item['product_count'];
}
// 新しいデータの構造入れ替え
$arrNew = [];
foreach ($arrCategoryCountNew as $item) {
$arrNew[$item['category_id']] = $item['product_count'];
}
unset($arrCategoryCountOld);
unset($arrCategoryCountNew);
$arrNotExistsProductCategoryId = [];
// 削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。
foreach ($arrOld as $category_id => $count) {
// 商品が存在しない
if (!isset($arrNew[$category_id])) {
$arrNotExistsProductCategoryId[] = $category_id;
}
// 変更なし
elseif ($arrNew[$category_id] == $count) {
unset($arrNew[$category_id]);
}
}
// 差分があったIDとその親カテゴリID
$arrTgtCategoryId = $arrNotExistsProductCategoryId;
foreach ($arrNotExistsProductCategoryId as $category_id) {
$objQuery->delete('dtb_category_count', 'category_id = ?', [$category_id]);
$arrParentID = self::sfGetParentsArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
$arrTgtCategoryId = array_merge($arrTgtCategoryId, $arrParentID);
}
// dtb_category_countの更新 差分のあったカテゴリだけ更新する。
foreach ($arrNew as $category_id => $count) {
$sqlval = [];
$sqlval['create_date'] = 'CURRENT_TIMESTAMP';
$sqlval['product_count'] = $count;
if (isset($arrOld[$category_id])) {
$objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', [$category_id]);
} else {
$sqlval['category_id'] = $category_id;
$objQuery->insert('dtb_category_count', $sqlval);
}
$arrParentID = self::sfGetParentsArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
$arrTgtCategoryId = array_merge($arrTgtCategoryId, $arrParentID);
}
$arrTgtCategoryId = array_unique($arrTgtCategoryId);
unset($arrOld);
unset($arrNew);
// dtb_category_total_count 集計処理開始
// 更新対象カテゴリIDだけ集計しなおす。
$arrUpdateData = [];
foreach ($arrTgtCategoryId as $category_id) {
$arrWhereVal = [];
[$tmp_where, $arrTmpVal] = static::sfGetCatWhere($category_id);
if ($tmp_where != '') {
$where_product_ids = 'product_id IN (SELECT product_id FROM dtb_product_categories WHERE '.$tmp_where.')';
$arrWhereVal = $arrTmpVal;
} else {
$where_product_ids = '0<>0'; // 一致させない
}
$where = "($where_alldtl) AND ($where_product_ids)";
$arrUpdateData[$category_id] = $objQuery->count($from_alldtl, $where, $arrWhereVal);
}
unset($arrTgtCategoryId);
// 更新対象だけを更新。
foreach ($arrUpdateData as $category_id => $count) {
if ($count == 0) {
$objQuery->delete('dtb_category_total_count', 'category_id = ?', [$category_id]);
continue;
}
$sqlval = [
'product_count' => $count,
'create_date' => 'CURRENT_TIMESTAMP',
];
$ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', [$category_id]);
if (!$ret) {
$sqlval['category_id'] = $category_id;
$objQuery->insert('dtb_category_total_count', $sqlval);
}
}
// トランザクション終了処理
if ($is_out_trans) {
$objQuery->commit();
}
}
/**
* 子IDの配列を返す.
*
* @param string $table テーブル名
* @param string $pid_name 親ID名
* @param string $id_name ID名
* @param int $id ID
* @param array 子ID の配列
*
* @deprecated 本体で使用されていないため非推奨
*/
public static function sfGetChildsID($table, $pid_name, $id_name, $id)
{
$arrRet = static::sfGetChildrenArray($table, $pid_name, $id_name, $id);
return $arrRet;
}
/**
* 階層構造のテーブルから子ID配列を取得する.
*
* @param string $table テーブル名
* @param string $pid_name 親ID名
* @param string $id_name ID名
* @param int $id ID番号
*
* @return array 子IDの配列
*/
public static function sfGetChildrenArray($table, $pid_name, $id_name, $id)
{
$arrChildren = [];
$arrRet = [$id];
while (count($arrRet) > 0) {
$arrChildren = array_merge($arrChildren, $arrRet);
$arrRet = SC_Helper_DB_Ex::sfGetChildrenArraySub($table, $pid_name, $id_name, $arrRet);
}
return $arrChildren;
}
/**
* 親ID直下の子IDを全て取得する.
*
* @param string $pid_name 親ID名
* @param string $id_name ID名
* @param array $arrPID 親IDの配列
* @param string $table