source: branches/version-2_5-dev/data/class/SC_Product.php @ 18829

Revision 18829, 21.5 KB checked in by nanasess, 16 years ago (diff)

#781(規格のデータベースを木構造に)

  • カートの中身を product_class_id で特定するように修正
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/*  [名称] SC_Product
25 *  [概要] 商品クラス
26 */
27class SC_Product {
28
29    /** 規格名一覧 */
30    var $arrClassName;
31    /** 規格分類名一覧 */
32    var $arrClassCatName;
33    var $classCategories = array();
34    var $stock_find;
35    /** 規格1クラス名 */
36    var $className1 = '';
37    /** 規格2クラス名 */
38    var $className2 = '';
39    /** 規格1が設定されている */
40    var $classCat1_find;
41    /** 規格2が設定されている */
42    var $classCat2_find;
43    var $classCats1;
44
45    /**
46     * SC_Queryインスタンスに設定された検索条件をもとに商品IDの配列を取得する.
47     *
48     * 検索条件は, SC_Query::getWhere() 関数で設定しておく必要があります.
49     *
50     * @param SC_Query $objQuery SC_Query インスタンス
51     * @param array $arrVal 検索パラメータの配列
52     * @return array 商品IDの配列
53     */
54    function findProductIds(&$objQuery, $arrVal = array()) {
55        $table = <<< __EOS__
56                 dtb_products AS alldtl
57            JOIN dtb_product_categories AS T2
58              ON alldtl.product_id = T2.product_id
59            JOIN dtb_category
60              ON T2.category_id = dtb_category.category_id
61__EOS__;
62        // SC_Query::getCol() ではパフォーマンスが出ない
63        $results = $objQuery->select('alldtl.product_id', $table, "", $arrVal,
64                                     MDB2_FETCHMODE_ORDERED);
65        foreach ($results as $val) {
66            $resultValues[] = $val[0];
67        }
68        return array_unique($resultValues);
69    }
70
71    /**
72     * SC_Queryインスタンスに設定された検索条件をもとに商品一覧の配列を取得する.
73     *
74     * 主に SC_Product::findProductIds() で取得した商品IDを検索条件にし,
75     * SC_Query::setOrder() や SC_Query::setLimitOffset() を設定して, 商品一覧
76     * の配列を取得する.
77     *
78     * @param SC_Query $objQuery SC_Query インスタンス
79     * @param array $arrVal 検索パラメータ(ソート条件)の配列
80     * @return array 商品一覧の配列
81     */
82    function lists(&$objQuery, $arrVal = array()) {
83        $col = <<< __EOS__
84             product_id
85            ,product_code_min
86            ,product_code_max
87            ,name
88            ,comment1
89            ,comment2
90            ,comment3
91            ,main_list_comment
92            ,main_image
93            ,main_list_image
94            ,price01_min
95            ,price01_max
96            ,price02_min
97            ,price02_max
98            ,stock_min
99            ,stock_max
100            ,stock_unlimited_min
101            ,stock_unlimited_max
102            ,deliv_date_id
103            ,status
104            ,del_flg
105            ,update_date
106__EOS__;
107        return $objQuery->select($col, $this->alldtlSQL($objQuery->where),
108                                 "", $arrVal);
109    }
110
111    /**
112     * 商品詳細を取得する.
113     *
114     * @param integer $productId 商品ID
115     * @return array 商品詳細情報の配列
116     */
117    function getDetail($productId) {
118        $objQuery =& SC_Query::getSingletonInstance();
119        $result = $objQuery->select("*", $this->alldtlSQL("product_id = ?"),
120                                    "product_id = ?",
121                                    array($productId, $productId));
122        return $result[0];
123    }
124
125    /**
126     * 商品詳細情報と商品規格を取得する.
127     *
128     * @param integer $productClassId 商品規格ID
129     * @return array 商品詳細情報と商品規格の配列
130     */
131    function getDetailAndProductsClass($productClassId) {
132        $result = $this->getProductsClass($productClassId);
133        $result = array_merge($result, $this->getDetail($result['product_id']));
134        return $result;
135    }
136
137    /**
138     * 商品IDに紐づく商品規格を自分自身に設定する.
139     *
140     * 引数の商品IDの配列に紐づく商品規格を取得し, 自分自身のフィールドに
141     * 設定する.
142     *
143     * @param array $arrProductId 商品ID の配列
144     * @return void
145     */
146    function setProductsClassByProductIds($arrProductId) {
147
148        foreach ($arrProductId as $productId) {
149            $rows[$productId] = $this->getProductsClassFullByProductId($productId);
150        }
151
152        $arrProductsClass = array();
153        foreach ($rows as $productId => $arrProductClass) {
154            $classCats1 = array();
155            $classCats1[''] = '選択してください';
156
157            // 規格1クラス名
158            $this->className1[$productId] =
159                isset($arrProductClass[0]['class_name1'])
160                ? $arrProductClass[0]['class_name1']
161                : '';
162
163            // 規格2クラス名
164            $this->className2[$productId] =
165                isset($arrProductClass[0]['class_name2'])
166                ? $arrProductClass[0]['class_name2']
167                : '';
168
169            // 規格1が設定されている
170            $this->classCat1_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id1']));
171            // 規格2が設定されている
172            $this->classCat2_find[$productId] = (!SC_Utils_Ex::isBlank($arrProductClass[0]['classcategory_id2']));
173
174            $this->stock_find[$productId] = false;
175            $classCategories = array();
176            $classCategories['']['']['name'] = '選択してください';
177            $classCategories['']['']['product_class_id'] = $arrProductClass[0]['product_class_id'];
178            $this->product_class_id[$productId] = $arrProductClass[0]['product_class_id'];
179            foreach ($arrProductClass as $productsClass) {
180                $productsClass1 = $productsClass['classcategory_id1'];
181                $productsClass2 = $productsClass['classcategory_id2'];
182                $classCategories[$productsClass1]['']['name'] = '選択してください';
183                // 在庫
184                $stock_find_class = ($productsClass['stock_unlimited'] || $productsClass['stock'] > 0);
185
186                $classCategories[$productsClass1][$productsClass2]['name'] = $productsClass['name2'] . ($stock_find_class ? '' : ' (品切れ中)');
187
188                $classCategories[$productsClass1][$productsClass2]['stock_find'] = $stock_find_class;
189
190                if ($stock_find_class) {
191                    $this->stock_find[$productId] = true;
192                }
193
194                if (!in_array($classcat_id1, $classCats1)) {
195                    $classCats1[$productsClass1] = $productsClass['name1']
196                        . ($productsClass2 == 0 && !$stock_find_class ? ' (品切れ中)' : '');
197                }
198
199                // 価格
200                $classCategories[$productsClass1][$productsClass2]['price01']
201                    = strlen($productsClass['price01'])
202                    ? number_format(SC_Helper_DB_Ex::sfPreTax($productsClass['price01']))
203                    : '';
204
205                $classCategories[$productsClass1][$productsClass2]['price02']
206                    = strlen($productsClass['price02'])
207                    ? number_format(SC_Helper_DB_Ex::sfPreTax($productsClass['price02']))
208                    : '';
209
210                // ポイント
211                // XXX sfPrePoint() の第4パラメータは、処理にバグがあるため現状省略している。(http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=3540&forum=1&post_id=13853#forumpost13853)
212                $classCategories[$productsClass1][$productsClass2]['point']
213                    = SC_Utils_Ex::sfPrePoint($productsClass['price02'], $productsClass['point_rate']);
214
215                // 商品コード
216                $classCategories[$productsClass1][$productsClass2]['product_code'] = $productsClass['product_code'];
217                // 商品規格ID
218                $classCategories[$productsClass1][$productsClass2]['product_class_id'] = $productsClass['product_class_id'];
219            }
220
221            $this->classCategories[$productId] = $classCategories;
222
223            // 規格1
224            $this->classCats1[$productId] = $classCats1;
225        }
226    }
227
228    /**
229     * SC_Query インスタンスに設定された検索条件を使用して商品規格を取得する.
230     *
231     * @param SC_Query $objQuery SC_Queryインスタンス
232     * @param array $params 検索パラメータの配列
233     * @return array 商品規格の配列
234     */
235    function getProductsClassByQuery(&$objQuery, $params) {
236        // 末端の規格を取得
237        $col = <<< __EOS__
238            T1.product_id,
239            T1.stock,
240            T1.stock_unlimited,
241            T1.price01,
242            T1.price02,
243            T1.point_rate,
244            T1.product_code,
245            T1.product_class_id,
246            T1.del_flg,
247            T1.down,
248            T1.down_filename,
249            T1.down_realfilename,
250            T2.class_combination_id,
251            T2.parent_class_combination_id,
252            T2.classcategory_id,
253            T2.level,
254            T3.name,
255            T4.name AS class_name,
256            T4.class_id
257__EOS__;
258        $table = <<< __EOS__
259                      dtb_products_class T1
260            LEFT JOIN dtb_class_combination T2
261                   ON T1.class_combination_id = T2.class_combination_id
262            LEFT JOIN dtb_classcategory T3
263                   ON T2.classcategory_id = T3.classcategory_id
264            LEFT JOIN dtb_class T4
265                   ON T3.class_id = T4.class_id
266__EOS__;
267        $arrRet = $objQuery->select($col, $table, "", $params);
268        $levels = array();
269        $parents = array();
270        foreach ($arrRet as $rows) {
271            $levels[] = $rows['level'];
272            $parents[] = $rows['parent_class_combination_id'];
273        }
274        $level = max($levels);
275        $parentsClass = array();
276        // 階層分の親を取得
277        for ($i = 0; $i < $level -1; $i++) {
278            $objQuery =& SC_Query::getSingletonInstance();
279            $objQuery->setWhere('T1.class_combination_id IN (' . implode(', ', array_pad(array(), count($parents), '?')) . ')');
280
281            $col = <<< __EOS__
282                T1.class_combination_id,
283                T1.classcategory_id,
284                T1.parent_class_combination_id,
285                T1.level,
286                T2.name,
287                T3.name AS class_name,
288                T3.class_id
289__EOS__;
290            $table = <<< __EOS__
291                          dtb_class_combination T1
292                LEFT JOIN dtb_classcategory T2
293                       ON T1.classcategory_id = T2.classcategory_id
294                LEFT JOIN dtb_class T3
295                       ON T2.class_id = T3.class_id
296__EOS__;
297
298            $arrParents = $objQuery->select($col, $table, "", $parents);
299
300            unset($parents);
301            foreach ($arrParents as $rows) {
302                $parents[] = $rows['parent_class_combination_id'];
303
304                foreach ($arrRet as $child) {
305                    if ($child['parent_class_combination_id']
306                        == $rows['class_combination_id']) {
307                        $rows['product_id'] = $child['product_id'];
308                    }
309                }
310                $tmpParents[] = $rows;
311            }
312            $parentsClass = array_merge($parentsClass, $tmpParents);
313        }
314
315        // 末端から枝を作成
316        $tmpClass = array_merge($arrRet, $parentsClass);
317
318        foreach ($tmpClass as $val) {
319            $val['class_id' . $val['level']] = $val['class_id'];
320            $val['class_name' . $val['level']] = $val['class_name'];
321            $val['name' . $val['level']] = $val['name'];
322            $val['classcategory_id' . $val['level']] = $val['classcategory_id'];
323            $arrProductsClass[] = $val;
324        }
325
326        return $arrProductsClass;
327    }
328
329    /**
330     * 商品規格IDから商品規格を取得する.
331     */
332    function getProductsClass($productClassId) {
333        $objQuery =& SC_Query::getSingletonInstance();
334        $objQuery->setWhere('product_class_id = ?');
335        $objQuery->setOrder("T2.level DESC");
336        $results = $this->getProductsClassByQuery($objQuery, $productClassId);
337        $productsClass = $this->getProductsClassFull($results);
338        return $productsClass[0];
339    }
340
341    /**
342     * 複数の商品IDに紐づいた, 商品規格を取得する.
343     *
344     * @param array $productIds 商品IDの配列
345     * @return array 商品規格の配列
346     */
347    function getProductsClassByProductIds($productIds = array()) {
348        if (empty($productIds)) {
349            return array();
350        }
351        $objQuery =& SC_Query::getSingletonInstance();
352        $objQuery->setWhere('product_id IN (' . implode(', ', array_pad(array(), count($productIds), '?')) . ')');
353        $objQuery->setOrder("T2.level DESC");
354        return $this->getProductsClassByQuery($objQuery, $productIds);
355    }
356
357    /**
358     * 商品IDに紐づいた, 商品規格を階層ごとに取得する.
359     *
360     * @param array $productId 商品ID
361     * @return array 階層ごとの商品規格の配列
362     */
363    function getProductsClassLevelByProductId($productId) {
364        $results = $this->getProductsClassByProductIds(array($productId));
365        return $this->getProductsClassLevel($results);
366    }
367
368    /**
369     * 商品IDに紐づいた, 商品規格をすべての組み合わせごとに取得する.
370     *
371     * @param array $productId 商品ID
372     * @return array すべての組み合わせの商品規格の配列
373     */
374    function getProductsClassFullByProductId($productId) {
375        $results = $this->getProductsClassByProductIds(array($productId));
376        return $this->getProductsClassFull($results);
377    }
378
379    /**
380     * 商品規格の配列から, 商品規格を階層ごとに取得する.
381     *
382     * @access private
383     * @param array $productsClassResults 商品規格の結果の配列
384     * @return array 階層ごとの商品規格の配列
385     */
386    function getProductsClassLevel($productsClassResults) {
387        foreach ($productsClassResults as $row) {
388            $productsClassLevel["level" . $row['level']][] = $row;
389        }
390        return $productsClassLevel;
391    }
392
393    /**
394     * 商品規格の配列から, 商品規格のすべての組み合わせを取得する.
395     *
396     * @access private
397     * @param array $productsClassResults 商品規格の結果の配列
398     * @ array 階層ごとの商品規格の配列
399     */
400    function getProductsClassFull($productsClassResults) {
401        $results = $this->getProductsClassLevel($productsClassResults);
402        $productsClass = array();
403        if (SC_Utils_Ex::isBlank($results["level1"])
404            && SC_Utils_Ex::isBlank($results["level2"])) {
405            return $results["level"];
406        }
407
408        foreach ($results["level1"] as $level1) {
409            foreach ($results["level2"] as $level2) {
410                if ($level2['parent_class_combination_id'] == $level1['class_combination_id']) {
411                    $level1 = array_merge($level1, $level2);
412                }
413            }
414            $productsClass[] = $level1;
415        }
416        return $productsClass;
417    }
418
419    /**
420     * 商品詳細の SQL を取得する.
421     *
422     * @param string $where 商品詳細の WHERE 句
423     * @return string 商品詳細の SQL
424     */
425    function alldtlSQL($where = "") {
426        $whereCause = "";
427        if (!SC_Utils_Ex::isBlank($where)) {
428            $whereCause = " WHERE " . $where;
429        }
430        /*
431         * point_rate は商品規格(dtb_products_class)ごとに保持しているが,
432         * 商品(dtb_products)ごとの設定なので MAX のみを取得する.
433         */
434        $sql = <<< __EOS__
435            (
436             SELECT dtb_products.product_id,
437                    dtb_products.name,
438                    dtb_products.maker_id,
439                    dtb_products.rank,
440                    dtb_products.status,
441                    dtb_products.comment1,
442                    dtb_products.comment2,
443                    dtb_products.comment3,
444                    dtb_products.comment4,
445                    dtb_products.comment5,
446                    dtb_products.comment6,
447                    dtb_products.note,
448                    dtb_products.file1,
449                    dtb_products.file2,
450                    dtb_products.file3,
451                    dtb_products.file4,
452                    dtb_products.file5,
453                    dtb_products.file6,
454                    dtb_products.main_list_comment,
455                    dtb_products.main_list_image,
456                    dtb_products.main_comment,
457                    dtb_products.main_image,
458                    dtb_products.main_large_image,
459                    dtb_products.sub_title1,
460                    dtb_products.sub_comment1,
461                    dtb_products.sub_image1,
462                    dtb_products.sub_large_image1,
463                    dtb_products.sub_title2,
464                    dtb_products.sub_comment2,
465                    dtb_products.sub_image2,
466                    dtb_products.sub_large_image2,
467                    dtb_products.sub_title3,
468                    dtb_products.sub_comment3,
469                    dtb_products.sub_image3,
470                    dtb_products.sub_large_image3,
471                    dtb_products.sub_title4,
472                    dtb_products.sub_comment4,
473                    dtb_products.sub_image4,
474                    dtb_products.sub_large_image4,
475                    dtb_products.sub_title5,
476                    dtb_products.sub_comment5,
477                    dtb_products.sub_image5,
478                    dtb_products.sub_large_image5,
479                    dtb_products.sub_title6,
480                    dtb_products.sub_comment6,
481                    dtb_products.sub_image6,
482                    dtb_products.sub_large_image6,
483                    dtb_products.del_flg,
484                    dtb_products.creator_id,
485                    dtb_products.create_date,
486                    dtb_products.update_date,
487                    dtb_products.deliv_date_id,
488                    T4.product_code_min,
489                    T4.product_code_max,
490                    T4.price01_min,
491                    T4.price01_max,
492                    T4.price02_min,
493                    T4.price02_max,
494                    T4.stock_min,
495                    T4.stock_max,
496                    T4.stock_unlimited_min,
497                    T4.stock_unlimited_max,
498                    T4.point_rate,
499                    T4.class_count
500               FROM dtb_products
501               JOIN (
502                       SELECT product_id,
503                              MIN(product_code) AS product_code_min,
504                              MAX(product_code) AS product_code_max,
505                              MIN(price01) AS price01_min,
506                              MAX(price01) AS price01_max,
507                              MIN(price02) AS price02_min,
508                              MAX(price02) AS price02_max,
509                              MIN(stock) AS stock_min,
510                              MAX(stock) AS stock_max,
511                              MIN(stock_unlimited) AS stock_unlimited_min,
512                              MAX(stock_unlimited) AS stock_unlimited_max,
513                              MAX(point_rate) AS point_rate,
514                              COUNT(*) as class_count
515                         FROM dtb_products_class
516                       $whereCause
517                     GROUP BY product_id
518                     ) AS T4
519                 ON dtb_products.product_id = T4.product_id
520        ) AS alldtl
521__EOS__;
522        return $sql;
523    }
524
525    /**
526     * 商品規格ID1、2に紐づいた,product_class_idを取得する.
527     *
528     * @param int $productId 商品ID
529     * @param int $classcategory_id1 商品規格ID1
530     * @param int $classcategory_id2 商品規格ID2
531     * @return string product_class_id
532     */
533    function getClasscategoryIdsByProductClassId($productId, $classcategory_id1, $classcategory_id2) {
534        $objQuery = new SC_Query();
535        $col = "T1.product_id AS product_id,T1.product_class_id AS product_class_id,T1.classcategory_id1 AS classcategory_id1,T1.classcategory_id2 AS classcategory_id2";
536        $table = <<< __EOS__
537            (SELECT
538                pc.product_code AS product_code,
539                pc.product_id AS product_id,
540                pc.product_class_id AS product_class_id,
541                pc.class_combination_id AS class_combination_id,
542                COALESCE(cc2.classcategory_id,0) AS classcategory_id1,
543                COALESCE(cc1.classcategory_id,0) AS classcategory_id2
544            FROM
545                dtb_products_class pc LEFT JOIN dtb_class_combination cc1 ON pc.class_combination_id = cc1.class_combination_id
546                LEFT JOIN dtb_class_combination cc2 ON cc1.parent_class_combination_id = cc2.class_combination_id) T1
547__EOS__;
548        $where = "T1.product_id = ? AND T1.classcategory_id1 = ? AND T1.classcategory_id2 = ?";
549        $arrRet = $objQuery->select($col, $table, $where,
550                                    array($productId, $classcategory_id1, $classcategory_id2));
551        return $arrRet[0]['product_class_id'];
552    }
553
554}
555?>
Note: See TracBrowser for help on using the repository browser.