source: branches/version-2_13-dev/data/class/pages/admin/products/LC_Page_Admin_Products.php @ 22735

Revision 22735, 17.1 KB checked in by h_yoshimoto, 11 years ago (diff)

#2193 ユニットテストチームのコミットをマージ(from camp/camp-2_13-tests)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
26
27/**
28 * 商品管理 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Admin_Products extends LC_Page_Admin_Ex
35{
36
37    // }}}
38    // {{{ functions
39
40    /**
41     * Page を初期化する.
42     *
43     * @return void
44     */
45    function init()
46    {
47        parent::init();
48        $this->tpl_mainpage = 'products/index.tpl';
49        $this->tpl_mainno = 'products';
50        $this->tpl_subno = 'index';
51        $this->tpl_pager = 'pager.tpl';
52        $this->tpl_maintitle = '商品管理';
53        $this->tpl_subtitle = '商品マスター';
54
55        $masterData = new SC_DB_MasterData_Ex();
56        $this->arrPageMax = $masterData->getMasterData('mtb_page_max');
57        $this->arrDISP = $masterData->getMasterData('mtb_disp');
58        $this->arrSTATUS = $masterData->getMasterData('mtb_status');
59        $this->arrPRODUCTSTATUS_COLOR = $masterData->getMasterData('mtb_product_status_color');
60
61        $objDate = new SC_Date_Ex();
62        // 登録・更新検索開始年
63        $objDate->setStartYear(RELEASE_YEAR);
64        $objDate->setEndYear(DATE('Y'));
65        $this->arrStartYear = $objDate->getYear();
66        $this->arrStartMonth = $objDate->getMonth();
67        $this->arrStartDay = $objDate->getDay();
68        // 登録・更新検索終了年
69        $objDate->setStartYear(RELEASE_YEAR);
70        $objDate->setEndYear(DATE('Y'));
71        $this->arrEndYear = $objDate->getYear();
72        $this->arrEndMonth = $objDate->getMonth();
73        $this->arrEndDay = $objDate->getDay();
74
75    }
76
77    /**
78     * Page のプロセス.
79     *
80     * @return void
81     */
82    function process()
83    {
84        $this->action();
85        $this->sendResponse();
86    }
87
88    /**
89     * Page のアクション.
90     *
91     * @return void
92     */
93    function action()
94    {
95
96        $objDb = new SC_Helper_DB_Ex();
97        $objFormParam = new SC_FormParam_Ex();
98        $objProduct = new SC_Product_Ex();
99        $objQuery =& SC_Query_Ex::getSingletonInstance();
100
101        // パラメーター情報の初期化
102        $this->lfInitParam($objFormParam);
103        $objFormParam->setParam($_POST);
104        $this->arrHidden = $objFormParam->getSearchArray();
105        $this->arrForm = $objFormParam->getFormParamList();
106
107        switch ($this->getMode()) {
108            case 'delete':
109                // 商品、子テーブル(商品規格)、会員お気に入り商品の削除
110                $this->doDelete('product_id = ?', array($objFormParam->getValue('product_id')));
111                // 件数カウントバッチ実行
112                $objDb->sfCountCategory($objQuery);
113                $objDb->sfCountMaker($objQuery);
114                // 削除後に検索結果を表示するため breakしない
115
116            // 検索パラメーター生成後に処理実行するため breakしない
117            case 'csv':
118            case 'delete_all':
119
120            case 'search':
121                $objFormParam->convParam();
122                $objFormParam->trimParam();
123                $this->arrErr = $this->lfCheckError($objFormParam);
124                $arrParam = $objFormParam->getHashArray();
125
126                if (count($this->arrErr) == 0) {
127                    $where = 'del_flg = 0';
128                    $arrWhereVal = array();
129                    foreach ($arrParam as $key => $val) {
130                        if ($val == '') {
131                            continue;
132                        }
133                        $this->buildQuery($key, $where, $arrWhereVal, $objFormParam, $objDb);
134                    }
135
136                    $order = 'update_date DESC';
137
138                    /* -----------------------------------------------
139                     * 処理を実行
140                     * ----------------------------------------------- */
141                    switch ($this->getMode()) {
142                        // CSVを送信する。
143                        case 'csv':
144                            $objCSV = new SC_Helper_CSV_Ex();
145                            // CSVを送信する。正常終了の場合、終了。
146                            $objCSV->sfDownloadCsv(1, $where, $arrWhereVal, $order, true);
147                            SC_Response_Ex::actionExit();
148
149                        // 全件削除(ADMIN_MODE)
150                        case 'delete_all':
151                            $this->doDelete($where, $arrWhereVal);
152                            break;
153
154                        // 検索実行
155                        default:
156                            // 行数の取得
157                            $this->tpl_linemax = $this->getNumberOfLines($where, $arrWhereVal);
158                            // ページ送りの処理
159                            $page_max = SC_Utils_Ex::sfGetSearchPageMax($objFormParam->getValue('search_page_max'));
160                            // ページ送りの取得
161                            $objNavi = new SC_PageNavi_Ex($this->arrHidden['search_pageno'],
162                                                          $this->tpl_linemax, $page_max,
163                                                          'fnNaviSearchPage', NAVI_PMAX);
164                            $this->arrPagenavi = $objNavi->arrPagenavi;
165
166                            // 検索結果の取得
167                            $this->arrProducts = $this->findProducts($where, $arrWhereVal, $page_max, $objNavi->start_row,
168                                                                     $order, $objProduct);
169
170                            // 各商品ごとのカテゴリIDを取得
171                            if (count($this->arrProducts) > 0) {
172                                foreach ($this->arrProducts as $key => $val) {
173                                    $this->arrProducts[$key]['categories'] = $objDb->sfGetCategoryId($val['product_id'], 0, true);
174                                    $objDb->g_category_on = false;
175                                }
176                            }
177                    }
178                }
179                break;
180        }
181
182        // カテゴリの読込
183        list($this->arrCatKey, $this->arrCatVal) = $objDb->sfGetLevelCatList(false);
184        $this->arrCatList = $this->lfGetIDName($this->arrCatKey, $this->arrCatVal);
185
186    }
187
188    /**
189     * デストラクタ.
190     *
191     * @return void
192     */
193    function destroy()
194    {
195        parent::destroy();
196    }
197
198    /**
199     * パラメーター情報の初期化を行う.
200     *
201     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
202     * @return void
203     */
204    function lfInitParam(&$objFormParam)
205    {
206
207        // POSTされる値
208        $objFormParam->addParam('商品ID', 'product_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
209        $objFormParam->addParam('カテゴリID', 'category_id', STEXT_LEN, 'n', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
210        $objFormParam->addParam('ページ送り番号','search_pageno', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
211        $objFormParam->addParam('表示件数', 'search_page_max', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
212
213        // 検索条件
214        $objFormParam->addParam('商品ID', 'search_product_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
215        $objFormParam->addParam('商品コード', 'search_product_code', STEXT_LEN, 'KVna', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
216        $objFormParam->addParam('商品名', 'search_name', STEXT_LEN, 'KVa', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
217        $objFormParam->addParam('カテゴリ', 'search_category_id', STEXT_LEN, 'n', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
218        $objFormParam->addParam('種別', 'search_status', INT_LEN, 'n', array('MAX_LENGTH_CHECK'));
219        // 登録・更新日
220        $objFormParam->addParam('開始年', 'search_startyear', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
221        $objFormParam->addParam('開始月', 'search_startmonth', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
222        $objFormParam->addParam('開始日', 'search_startday', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
223        $objFormParam->addParam('終了年', 'search_endyear', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
224        $objFormParam->addParam('終了月', 'search_endmonth', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
225        $objFormParam->addParam('終了日', 'search_endday', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
226
227        $objFormParam->addParam('商品ステータス', 'search_product_statuses', INT_LEN, 'n', array('MAX_LENGTH_CHECK'));
228    }
229
230    /**
231     * 入力内容のチェックを行う.
232     *
233     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
234     * @return void
235     */
236    function lfCheckError(&$objFormParam)
237    {
238        $objErr = new SC_CheckError_Ex($objFormParam->getHashArray());
239        $objErr->arrErr = $objFormParam->checkError();
240
241        $objErr->doFunc(array('開始日', '終了日', 'search_startyear', 'search_startmonth', 'search_startday', 'search_endyear', 'search_endmonth', 'search_endday'), array('CHECK_SET_TERM'));
242
243        return $objErr->arrErr;
244    }
245
246    // カテゴリIDをキー、カテゴリ名を値にする配列を返す。
247    function lfGetIDName($arrCatKey, $arrCatVal)
248    {
249        $max = count($arrCatKey);
250        for ($cnt = 0; $cnt < $max; $cnt++) {
251            $key = isset($arrCatKey[$cnt]) ? $arrCatKey[$cnt] : '';
252            $val = isset($arrCatVal[$cnt]) ? $arrCatVal[$cnt] : '';
253            $arrRet[$key] = $val;
254        }
255        return $arrRet;
256    }
257
258    /**
259     * 商品、子テーブル(商品規格)、お気に入り商品の削除
260     *
261     * @param string $where 削除対象の WHERE 句
262     * @param array $arrParam 削除対象の値
263     * @return void
264     */
265    function doDelete($where, $arrParam = array())
266    {
267        $objQuery =& SC_Query_Ex::getSingletonInstance();
268        $arrRet = $objQuery->getCol('product_id', "dtb_products", $where, $arrParam);
269        $product_ids = array();
270        foreach ($arrRet as $value) {
271            $product_ids[] = $value['product_id'];
272        }
273        $sqlval['del_flg']     = 1;
274        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
275        $objQuery->begin();
276        $objQuery->update('dtb_products_class', $sqlval, "product_id IN (SELECT product_id FROM dtb_products WHERE $where)", $arrParam);
277        $objQuery->delete('dtb_customer_favorite_products', "product_id IN (SELECT product_id FROM dtb_products WHERE $where)", $arrParam);
278
279        $objRecomment = new SC_Helper_BestProducts_Ex();
280        $objRecomment->deleteByProductIDs($product_ids);
281
282        $objQuery->update('dtb_products', $sqlval, $where, $arrParam);
283        $objQuery->commit();
284    }
285
286    /**
287     * クエリを構築する.
288     *
289     * 検索条件のキーに応じた WHERE 句と, クエリパラメーターを構築する.
290     * クエリパラメーターは, SC_FormParam の入力値から取得する.
291     *
292     * 構築内容は, 引数の $where 及び $arrValues にそれぞれ追加される.
293     *
294     * @param string $key 検索条件のキー
295     * @param string $where 構築する WHERE 句
296     * @param array $arrValues 構築するクエリパラメーター
297     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
298     * @param SC_FormParam $objDb SC_Helper_DB_Ex インスタンス
299     * @return void
300     */
301    function buildQuery($key, &$where, &$arrValues, &$objFormParam, &$objDb)
302    {
303        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
304        switch ($key) {
305            // 商品ID
306            case 'search_product_id':
307                $where .= ' AND product_id = ?';
308                $arrValues[] = sprintf('%d', $objFormParam->getValue($key));
309                break;
310            // 商品コード
311            case 'search_product_code':
312                $where .= ' AND product_id IN (SELECT product_id FROM dtb_products_class WHERE product_code ILIKE ?)';
313                $arrValues[] = sprintf('%%%s%%', $objFormParam->getValue($key));
314                break;
315            // 商品名
316            case 'search_name':
317                $where .= ' AND name LIKE ?';
318                $arrValues[] = sprintf('%%%s%%', $objFormParam->getValue($key));
319                break;
320            // カテゴリ
321            case 'search_category_id':
322                list($tmp_where, $tmp_Values) = $objDb->sfGetCatWhere($objFormParam->getValue($key));
323                if ($tmp_where != '') {
324                    $where.= ' AND product_id IN (SELECT product_id FROM dtb_product_categories WHERE ' . $tmp_where . ')';
325                    $arrValues = array_merge((array)$arrValues, (array)$tmp_Values);
326                }
327                break;
328            // 種別
329            case 'search_status':
330                $tmp_where = '';
331                foreach ($objFormParam->getValue($key) as $element) {
332                    if ($element != '') {
333                        if (SC_Utils_Ex::isBlank($tmp_where)) {
334                            $tmp_where .= ' AND (status = ?';
335                        } else {
336                            $tmp_where .= ' OR status = ?';
337                        }
338                        $arrValues[] = $element;
339                    }
340                }
341
342                if (!SC_Utils_Ex::isBlank($tmp_where)) {
343                    $tmp_where .= ')';
344                    $where .= " $tmp_where ";
345                }
346                break;
347            // 登録・更新日(開始)
348            case 'search_startyear':
349                $date = SC_Utils_Ex::sfGetTimestamp($objFormParam->getValue('search_startyear'),
350                                                    $objFormParam->getValue('search_startmonth'),
351                                                    $objFormParam->getValue('search_startday'));
352                $where.= ' AND update_date >= ?';
353                $arrValues[] = $date;
354                break;
355            // 登録・更新日(終了)
356            case 'search_endyear':
357                $date = SC_Utils_Ex::sfGetTimestamp($objFormParam->getValue('search_endyear'),
358                                                    $objFormParam->getValue('search_endmonth'),
359                                                    $objFormParam->getValue('search_endday'), true);
360                $where.= ' AND update_date <= ?';
361                $arrValues[] = $date;
362                break;
363            // 商品ステータス
364            case 'search_product_statuses':
365                $arrPartVal = $objFormParam->getValue($key);
366                $count = count($arrPartVal);
367                if ($count >= 1) {
368                    $where .= ' '
369                        . 'AND product_id IN ('
370                        . '    SELECT product_id FROM dtb_product_status WHERE product_status_id IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', $count) . ')'
371                        . ')';
372                    $arrValues = array_merge($arrValues, $arrPartVal);
373                }
374                break;
375            default:
376                break;
377        }
378    }
379
380    /**
381     * 検索結果の行数を取得する.
382     *
383     * @param string $where 検索条件の WHERE 句
384     * @param array $arrValues 検索条件のパラメーター
385     * @return integer 検索結果の行数
386     */
387    function getNumberOfLines($where, $arrValues)
388    {
389        $objQuery =& SC_Query_Ex::getSingletonInstance();
390        return $objQuery->count('dtb_products', $where, $arrValues);
391    }
392
393    /**
394     * 商品を検索する.
395     *
396     * @param string $where 検索条件の WHERE 句
397     * @param array $arrValues 検索条件のパラメーター
398     * @param integer $limit 表示件数
399     * @param integer $offset 開始件数
400     * @param string $order 検索結果の並び順
401     * @param SC_Product $objProduct SC_Product インスタンス
402     * @return array 商品の検索結果
403     */
404    function findProducts($where, $arrValues, $limit, $offset, $order, &$objProduct)
405    {
406        $objQuery =& SC_Query_Ex::getSingletonInstance();
407
408        // 読み込む列とテーブルの指定
409        $col = 'product_id, name, main_list_image, status, product_code_min, product_code_max, price02_min, price02_max, stock_min, stock_max, stock_unlimited_min, stock_unlimited_max, update_date';
410        $from = $objProduct->alldtlSQL();
411
412        $objQuery->setLimitOffset($limit, $offset);
413        $objQuery->setOrder($order);
414
415        return $objQuery->select($col, $from, $where, $arrValues);
416    }
417}
Note: See TracBrowser for help on using the repository browser.