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

Revision 23682, 17.0 KB checked in by undertree, 9 years ago (diff)

#2651 商品を削除してもおすすめ商品が残るバグを修正

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