source: branches/version-2_13-dev/data/class/helper/SC_Helper_BestProducts.php @ 23569

Revision 23569, 7.5 KB checked in by shutta, 10 years ago (diff)

#2581 (おすすめ商品管理> 最後尾のおすすめ商品の「下へ」ボタンが動作しない)
r23568 に関連して、削除時には上下を詰めないように変更。

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
24/**
25 * おすすめ商品を管理するヘルパークラス.
26 *
27 * @package Helper
28 * @author pineray
29 * @version $Id:$
30 */
31class SC_Helper_BestProducts
32{
33    /**
34     * おすすめ商品の情報を取得.
35     *
36     * @param  integer $best_id     おすすめ商品ID
37     * @param  boolean $has_deleted 削除されたおすすめ商品も含む場合 true; 初期値 false
38     * @return array
39     */
40    public function getBestProducts($best_id, $has_deleted = false)
41    {
42        $objQuery =& SC_Query_Ex::getSingletonInstance();
43        $col = '*';
44        $where = 'best_id = ?';
45        if (!$has_deleted) {
46            $where .= ' AND del_flg = 0';
47        }
48        $arrRet = $objQuery->select($col, 'dtb_best_products', $where, array($best_id));
49
50        return $arrRet[0];
51    }
52
53    /**
54     * おすすめ商品の情報をランクから取得.
55     *
56     * @param  integer $rank        ランク
57     * @param  boolean $has_deleted 削除されたおすすめ商品も含む場合 true; 初期値 false
58     * @return array
59     */
60    public function getByRank($rank, $has_deleted = false)
61    {
62        $objQuery =& SC_Query_Ex::getSingletonInstance();
63        $col = '*';
64        $where = 'rank = ?';
65        if (!$has_deleted) {
66            $where .= ' AND del_flg = 0';
67        }
68        $arrRet = $objQuery->select($col, 'dtb_best_products', $where, array($rank));
69
70        return $arrRet[0];
71    }
72
73    /**
74     * おすすめ商品一覧の取得.
75     *
76     * @param  integer $dispNumber  表示件数
77     * @param  integer $pageNumber  ページ番号
78     * @param  boolean $has_deleted 削除されたおすすめ商品も含む場合 true; 初期値 false
79     * @return array
80     */
81    public function getList($dispNumber = 0, $pageNumber = 0, $has_deleted = false)
82    {
83        $objQuery =& SC_Query_Ex::getSingletonInstance();
84        $col = '*';
85        $where = '';
86        if (!$has_deleted) {
87            $where .= 'del_flg = 0';
88        }
89        $table = 'dtb_best_products';
90        $objQuery->setOrder('rank');
91        if ($dispNumber > 0) {
92            if ($pageNumber > 0) {
93                $objQuery->setLimitOffset($dispNumber, (($pageNumber - 1) * $dispNumber));
94            } else {
95                $objQuery->setLimit($dispNumber);
96            }
97        }
98        $arrRet = $objQuery->select($col, $table, $where);
99
100        return $arrRet;
101    }
102
103    /**
104     * おすすめ商品の登録.
105     *
106     * @param  array    $sqlval
107     * @return multiple 登録成功:おすすめ商品ID, 失敗:FALSE
108     */
109    public function saveBestProducts($sqlval)
110    {
111        $objQuery =& SC_Query_Ex::getSingletonInstance();
112
113        $best_id = $sqlval['best_id'];
114        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
115        // 新規登録
116        if ($best_id == '') {
117            // INSERTの実行
118            if (!$sqlval['rank']) {
119                $sqlval['rank'] = $objQuery->max('rank', 'dtb_best_products') + 1;
120            }
121            $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
122            $sqlval['best_id'] = $objQuery->nextVal('dtb_best_products_best_id');
123            $ret = $objQuery->insert('dtb_best_products', $sqlval);
124        // 既存編集
125        } else {
126            unset($sqlval['creator_id']);
127            unset($sqlval['create_date']);
128            $where = 'best_id = ?';
129            $ret = $objQuery->update('dtb_best_products', $sqlval, $where, array($best_id));
130        }
131
132        return ($ret) ? $sqlval['best_id'] : FALSE;
133    }
134
135    /**
136     * おすすめ商品の削除.
137     *
138     * @param  integer $best_id おすすめ商品ID
139     * @return void
140     */
141    public function deleteBestProducts($best_id)
142    {
143        $objQuery =& SC_Query_Ex::getSingletonInstance();
144
145        $table = 'dtb_best_products';
146        $arrVal = array('del_flg' => 1);
147        $where = 'best_id = ?';
148        $arrWhereVal = array($best_id);
149        $objQuery->update($table, $arrVal, $where, $arrWhereVal);
150    }
151
152    /**
153     * 商品IDの配列からおすすめ商品を削除.
154     *
155     * @param  array $productIDs 商品ID
156     * @return void
157     */
158    public function deleteByProductIDs($productIDs)
159    {
160        $objDb = new SC_Helper_DB_Ex();
161        $arrList = $this->getList();
162        foreach ($arrList as $recommend) {
163            if (in_array($recommend['product_id'], $productIDs)) {
164                $this->deleteBestProducts($recommend['best_id']);
165            }
166        }
167    }
168
169    /**
170     * おすすめ商品の表示順をひとつ上げる.
171     *
172     * @param  integer $best_id おすすめ商品ID
173     * @return void
174     */
175    public function rankUp($best_id)
176    {
177        $arrBestProducts = $this->getBestProducts($best_id);
178        $rank = $arrBestProducts['rank'];
179
180        if ($rank > 1) {
181            // 表示順が一つ上のIDを取得する
182            $arrAboveBestProducts = $this->getByRank($rank - 1);
183            $above_best_id = $arrAboveBestProducts['best_id'];
184
185            if ($above_best_id) {
186                // 一つ上のものを一つ下に下げる
187                $this->changeRank($above_best_id, $rank);
188            } else {
189                // 無ければ何もしない。(歯抜けの場合)
190            }
191
192            // 一つ上に上げる
193            $this->changeRank($best_id, $rank - 1);
194        }
195    }
196
197    /**
198     * おすすめ商品の表示順をひとつ下げる.
199     *
200     * @param  integer $best_id おすすめ商品ID
201     * @return void
202     */
203    public function rankDown($best_id)
204    {
205        $arrBestProducts = $this->getBestProducts($best_id);
206        $rank = $arrBestProducts['rank'];
207
208        if ($rank < RECOMMEND_NUM) {
209            // 表示順が一つ下のIDを取得する
210            $arrBelowBestProducts = $this->getByRank($rank + 1);
211            $below_best_id = $arrBelowBestProducts['best_id'];
212
213            if ($below_best_id) {
214                // 一つ下のものを一つ上に上げる
215                $this->changeRank($below_best_id, $rank);
216            } else {
217                // 無ければ何もしない。(歯抜けの場合)
218            }
219
220            // 一つ下に下げる
221            $this->changeRank($best_id, $rank + 1);
222        }
223    }
224
225    /**
226     * 対象IDのrankを指定値に変更する
227     *
228     * @param integer $best_id 対象ID
229     * @param integer $rank 変更したいrank値
230     * @return void
231     */
232    public function changeRank($best_id, $rank)
233    {
234        $objQuery =& SC_Query_Ex::getSingletonInstance();
235
236        $table = 'dtb_best_products';
237        $sqlval = array('rank' => $rank);
238        $where = 'best_id = ?';
239        $arrWhereVal = array($best_id);
240        $objQuery->update($table, $sqlval, $where, $arrWhereVal);
241    }
242}
Note: See TracBrowser for help on using the repository browser.