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 | /** |
---|
25 | * DB関連のヘルパークラス. |
---|
26 | * |
---|
27 | * @package Helper |
---|
28 | * @author LOCKON CO.,LTD. |
---|
29 | * @version $Id:SC_Helper_DB.php 15532 2007-08-31 14:39:46Z nanasess $ |
---|
30 | */ |
---|
31 | class SC_Helper_DB { |
---|
32 | |
---|
33 | // {{{ properties |
---|
34 | |
---|
35 | /** ルートカテゴリ取得フラグ */ |
---|
36 | var $g_root_on; |
---|
37 | |
---|
38 | /** ルートカテゴリID */ |
---|
39 | var $g_root_id; |
---|
40 | |
---|
41 | /** 選択中カテゴリ取得フラグ */ |
---|
42 | var $g_category_on; |
---|
43 | |
---|
44 | /** 選択中カテゴリID */ |
---|
45 | var $g_category_id; |
---|
46 | |
---|
47 | // }}} |
---|
48 | // {{{ functions |
---|
49 | |
---|
50 | /** |
---|
51 | * データベースのバージョンを所得する. |
---|
52 | * |
---|
53 | * @param string $dsn データソース名 |
---|
54 | * @return string データベースのバージョン |
---|
55 | */ |
---|
56 | function sfGetDBVersion($dsn = "") { |
---|
57 | $dbFactory = SC_DB_DBFactory_Ex::getInstance(); |
---|
58 | return $dbFactory->sfGetDBVersion($dsn); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * カラムの存在チェックと作成を行う. |
---|
63 | * |
---|
64 | * チェック対象のテーブルに, 該当のカラムが存在するかチェックする. |
---|
65 | * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う. |
---|
66 | * カラムの生成も行う場合は, $col_type も必須となる. |
---|
67 | * |
---|
68 | * @param string $table_name テーブル名 |
---|
69 | * @param string $column_name カラム名 |
---|
70 | * @param string $col_type カラムのデータ型 |
---|
71 | * @param string $dsn データソース名 |
---|
72 | * @param bool $add カラムの作成も行う場合 true |
---|
73 | * @return bool カラムが存在する場合とカラムの生成に成功した場合 true, |
---|
74 | * テーブルが存在しない場合 false, |
---|
75 | * 引数 $add == false でカラムが存在しない場合 false |
---|
76 | */ |
---|
77 | function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) { |
---|
78 | $dbFactory = SC_DB_DBFactory_Ex::getInstance(); |
---|
79 | $dsn = $dbFactory->getDSN($dsn); |
---|
80 | |
---|
81 | $objQuery =& SC_Query::getSingletonInstance($dsn); |
---|
82 | |
---|
83 | // テーブルが無ければエラー |
---|
84 | if(!in_array($table_name, $objQuery->listTables())) return false; |
---|
85 | |
---|
86 | // 正常に接続されている場合 |
---|
87 | if(!$objQuery->isError()) { |
---|
88 | list($db_type) = split(":", $dsn); |
---|
89 | |
---|
90 | // カラムリストを取得 |
---|
91 | $columns = $objQuery->listTableFields($table_name); |
---|
92 | |
---|
93 | if(in_array($col_name, $columns)){ |
---|
94 | return true; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | // カラムを追加する |
---|
99 | if($add){ |
---|
100 | $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type "); |
---|
101 | return true; |
---|
102 | } |
---|
103 | return false; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * データの存在チェックを行う. |
---|
108 | * |
---|
109 | * @param string $table_name テーブル名 |
---|
110 | * @param string $where データを検索する WHERE 句 |
---|
111 | * @param string $dsn データソース名 |
---|
112 | * @param string $sql データの追加を行う場合の SQL文 |
---|
113 | * @param bool $add データの追加も行う場合 true |
---|
114 | * @return bool データが存在する場合 true, データの追加に成功した場合 true, |
---|
115 | * $add == false で, データが存在しない場合 false |
---|
116 | */ |
---|
117 | function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) { |
---|
118 | $dbFactory = SC_DB_DBFactory_Ex::getInstance(); |
---|
119 | $dsn = $dbFactory->getDSN($dsn); |
---|
120 | |
---|
121 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
122 | $count = $objQuery->count($table_name, $where, $arrval); |
---|
123 | |
---|
124 | if($count > 0) { |
---|
125 | $ret = true; |
---|
126 | } else { |
---|
127 | $ret = false; |
---|
128 | } |
---|
129 | // データを追加する |
---|
130 | if(!$ret && $add) { |
---|
131 | $objQuery->exec($sql); |
---|
132 | } |
---|
133 | return $ret; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * 店舗基本情報を取得する. |
---|
138 | * |
---|
139 | * @param boolean $force 強制的にDB取得するか |
---|
140 | * @param string $col 取得カラムを指定する |
---|
141 | * @return array 店舗基本情報の配列 |
---|
142 | */ |
---|
143 | function sfGetBasisData($force = false, $col = "") { |
---|
144 | static $data; |
---|
145 | |
---|
146 | if ($force || !isset($data)) { |
---|
147 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
148 | |
---|
149 | if ($col === "") { |
---|
150 | $arrRet = $objQuery->select('*', 'dtb_baseinfo'); |
---|
151 | } else { |
---|
152 | $arrRet = $objQuery->select($col, "dtb_baseinfo"); |
---|
153 | } |
---|
154 | |
---|
155 | if (isset($arrRet[0])) { |
---|
156 | $data = $arrRet[0]; |
---|
157 | } else { |
---|
158 | $data = array(); |
---|
159 | } |
---|
160 | } |
---|
161 | return $data; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * 基本情報の登録数を取得する |
---|
166 | * |
---|
167 | * @return int |
---|
168 | */ |
---|
169 | function sfGetBasisCount() { |
---|
170 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
171 | |
---|
172 | return $objQuery->count("dtb_baseinfo"); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | /* 選択中のアイテムのルートカテゴリIDを取得する */ |
---|
177 | function sfGetRootId() { |
---|
178 | |
---|
179 | if(!$this->g_root_on) { |
---|
180 | $this->g_root_on = true; |
---|
181 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
182 | |
---|
183 | if (!isset($_GET['product_id'])) $_GET['product_id'] = ""; |
---|
184 | if (!isset($_GET['category_id'])) $_GET['category_id'] = ""; |
---|
185 | |
---|
186 | if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) { |
---|
187 | // 選択中のカテゴリIDを判定する |
---|
188 | $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']); |
---|
189 | // ROOTカテゴリIDの取得 |
---|
190 | $arrRet = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $category_id); |
---|
191 | $root_id = isset($arrRet[0]) ? $arrRet[0] : ""; |
---|
192 | } else { |
---|
193 | // ROOTカテゴリIDをなしに設定する |
---|
194 | $root_id = ""; |
---|
195 | } |
---|
196 | $this->g_root_id = $root_id; |
---|
197 | } |
---|
198 | return $this->g_root_id; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * 受注番号、最終ポイント、加算ポイント、利用ポイントから「オーダー前ポイント」を取得する |
---|
203 | * |
---|
204 | * @param integer $order_id 受注番号 |
---|
205 | * @param integer $use_point 利用ポイント |
---|
206 | * @param integer $add_point 加算ポイント |
---|
207 | * @return array オーダー前ポイントの配列 |
---|
208 | */ |
---|
209 | function sfGetRollbackPoint($order_id, $use_point, $add_point) { |
---|
210 | $objQuery = new SC_Query(); |
---|
211 | $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id)); |
---|
212 | $customer_id = $arrRet[0]['customer_id']; |
---|
213 | if($customer_id != "" && $customer_id >= 1) { |
---|
214 | $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id)); |
---|
215 | $point = $arrRet[0]['point']; |
---|
216 | $rollback_point = $arrRet[0]['point'] + $use_point - $add_point; |
---|
217 | } else { |
---|
218 | $rollback_point = ""; |
---|
219 | $point = ""; |
---|
220 | } |
---|
221 | return array($point, $rollback_point); |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | |
---|
226 | /** |
---|
227 | * カテゴリツリーの取得を行う. |
---|
228 | * |
---|
229 | * @param integer $parent_category_id 親カテゴリID |
---|
230 | * @param bool $count_check 登録商品数のチェックを行う場合 true |
---|
231 | * @return array カテゴリツリーの配列 |
---|
232 | */ |
---|
233 | function sfGetCatTree($parent_category_id, $count_check = false) { |
---|
234 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
235 | $col = ""; |
---|
236 | $col .= " cat.category_id,"; |
---|
237 | $col .= " cat.category_name,"; |
---|
238 | $col .= " cat.parent_category_id,"; |
---|
239 | $col .= " cat.level,"; |
---|
240 | $col .= " cat.rank,"; |
---|
241 | $col .= " cat.creator_id,"; |
---|
242 | $col .= " cat.create_date,"; |
---|
243 | $col .= " cat.update_date,"; |
---|
244 | $col .= " cat.del_flg, "; |
---|
245 | $col .= " ttl.product_count"; |
---|
246 | $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id"; |
---|
247 | // 登録商品数のチェック |
---|
248 | if($count_check) { |
---|
249 | $where = "del_flg = 0 AND product_count > 0"; |
---|
250 | } else { |
---|
251 | $where = "del_flg = 0"; |
---|
252 | } |
---|
253 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
254 | $arrRet = $objQuery->select($col, $from, $where); |
---|
255 | |
---|
256 | $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id); |
---|
257 | |
---|
258 | foreach($arrRet as $key => $array) { |
---|
259 | foreach($arrParentID as $val) { |
---|
260 | if($array['category_id'] == $val) { |
---|
261 | $arrRet[$key]['display'] = 1; |
---|
262 | break; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | return $arrRet; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * カテゴリツリーを走査し, パンくずリスト用の配列を生成する. |
---|
272 | * |
---|
273 | * @param array カテゴリの配列 |
---|
274 | * @param integer $parent 上位カテゴリID |
---|
275 | * @param array パンくずリスト用の配列 |
---|
276 | * @result void |
---|
277 | * @see sfGetCatTree() |
---|
278 | */ |
---|
279 | function findTree(&$arrTree, $parent, &$result) { |
---|
280 | if ($result[count($result) - 1]['parent_category_id'] === 0) { |
---|
281 | return; |
---|
282 | } else { |
---|
283 | foreach ($arrTree as $key => $val) { |
---|
284 | if ($val['category_id'] == $parent) { |
---|
285 | $result[] = array('category_id' => $val['category_id'], |
---|
286 | 'parent_category_id' => (int) $val['parent_category_id'], |
---|
287 | 'category_name' => $val['category_name']); |
---|
288 | $this->findTree($arrTree, $val['parent_category_id'], $result); |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * カテゴリツリーの取得を複数カテゴリーで行う. |
---|
296 | * |
---|
297 | * @param integer $product_id 商品ID |
---|
298 | * @param bool $count_check 登録商品数のチェックを行う場合 true |
---|
299 | * @return array カテゴリツリーの配列 |
---|
300 | */ |
---|
301 | function sfGetMultiCatTree($product_id, $count_check = false) { |
---|
302 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
303 | $col = ""; |
---|
304 | $col .= " cat.category_id,"; |
---|
305 | $col .= " cat.category_name,"; |
---|
306 | $col .= " cat.parent_category_id,"; |
---|
307 | $col .= " cat.level,"; |
---|
308 | $col .= " cat.rank,"; |
---|
309 | $col .= " cat.creator_id,"; |
---|
310 | $col .= " cat.create_date,"; |
---|
311 | $col .= " cat.update_date,"; |
---|
312 | $col .= " cat.del_flg, "; |
---|
313 | $col .= " ttl.product_count"; |
---|
314 | $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id"; |
---|
315 | // 登録商品数のチェック |
---|
316 | if($count_check) { |
---|
317 | $where = "del_flg = 0 AND product_count > 0"; |
---|
318 | } else { |
---|
319 | $where = "del_flg = 0"; |
---|
320 | } |
---|
321 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
322 | $arrRet = $objQuery->select($col, $from, $where); |
---|
323 | |
---|
324 | $arrCategory_id = $this->sfGetCategoryId($product_id); |
---|
325 | |
---|
326 | $arrCatTree = array(); |
---|
327 | foreach ($arrCategory_id as $pkey => $parent_category_id) { |
---|
328 | $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id); |
---|
329 | |
---|
330 | foreach($arrParentID as $pid) { |
---|
331 | foreach($arrRet as $key => $array) { |
---|
332 | if($array['category_id'] == $pid) { |
---|
333 | $arrCatTree[$pkey][] = $arrRet[$key]; |
---|
334 | break; |
---|
335 | } |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | return $arrCatTree; |
---|
341 | } |
---|
342 | |
---|
343 | /** |
---|
344 | * 親カテゴリーを連結した文字列を取得する. |
---|
345 | * |
---|
346 | * @param integer $category_id カテゴリID |
---|
347 | * @return string 親カテゴリーを連結した文字列 |
---|
348 | */ |
---|
349 | function sfGetCatCombName($category_id){ |
---|
350 | // 商品が属するカテゴリIDを縦に取得 |
---|
351 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
352 | $arrCatID = $this->sfGetParents("dtb_category", "parent_category_id", "category_id", $category_id); |
---|
353 | $ConbName = ""; |
---|
354 | |
---|
355 | // カテゴリー名称を取得する |
---|
356 | foreach($arrCatID as $key => $val){ |
---|
357 | $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?"; |
---|
358 | $arrVal = array($val); |
---|
359 | $CatName = $objQuery->getOne($sql,$arrVal); |
---|
360 | $ConbName .= $CatName . ' | '; |
---|
361 | } |
---|
362 | // 最後の | をカットする |
---|
363 | $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2); |
---|
364 | |
---|
365 | return $ConbName; |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * 指定したカテゴリーIDのカテゴリーを取得する. |
---|
370 | * |
---|
371 | * @param integer $category_id カテゴリID |
---|
372 | * @return array 指定したカテゴリーIDのカテゴリー |
---|
373 | */ |
---|
374 | function sfGetCat($category_id){ |
---|
375 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
376 | |
---|
377 | // カテゴリーを取得する |
---|
378 | $arrVal = array($category_id); |
---|
379 | $res = $objQuery->select('category_id AS id, category_name AS name', 'dtb_category', 'category_id = ?', $arrVal); |
---|
380 | |
---|
381 | return $res[0]; |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * 指定したカテゴリーIDの大カテゴリーを取得する. |
---|
386 | * |
---|
387 | * @param integer $category_id カテゴリID |
---|
388 | * @return array 指定したカテゴリーIDの大カテゴリー |
---|
389 | */ |
---|
390 | function sfGetFirstCat($category_id){ |
---|
391 | // 商品が属するカテゴリIDを縦に取得 |
---|
392 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
393 | $arrRet = array(); |
---|
394 | $arrCatID = $this->sfGetParents("dtb_category", "parent_category_id", "category_id", $category_id); |
---|
395 | $arrRet['id'] = $arrCatID[0]; |
---|
396 | |
---|
397 | // カテゴリー名称を取得する |
---|
398 | $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?"; |
---|
399 | $arrVal = array($arrRet['id']); |
---|
400 | $arrRet['name'] = $objQuery->getOne($sql,$arrVal); |
---|
401 | |
---|
402 | return $arrRet; |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | * カテゴリツリーの取得を行う. |
---|
407 | * |
---|
408 | * $products_check:true商品登録済みのものだけ取得する |
---|
409 | * |
---|
410 | * @param string $addwhere 追加する WHERE 句 |
---|
411 | * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true |
---|
412 | * @param string $head カテゴリ名のプレフィックス文字列 |
---|
413 | * @return array カテゴリツリーの配列 |
---|
414 | */ |
---|
415 | function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) { |
---|
416 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
417 | $where = "del_flg = 0"; |
---|
418 | |
---|
419 | if($addwhere != "") { |
---|
420 | $where.= " AND $addwhere"; |
---|
421 | } |
---|
422 | |
---|
423 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
424 | |
---|
425 | if($products_check) { |
---|
426 | $col = "T1.category_id, category_name, level"; |
---|
427 | $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id"; |
---|
428 | $where .= " AND product_count > 0"; |
---|
429 | } else { |
---|
430 | $col = "category_id, category_name, level"; |
---|
431 | $from = "dtb_category"; |
---|
432 | } |
---|
433 | |
---|
434 | $arrRet = $objQuery->select($col, $from, $where); |
---|
435 | |
---|
436 | $max = count($arrRet); |
---|
437 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
438 | $id = $arrRet[$cnt]['category_id']; |
---|
439 | $name = $arrRet[$cnt]['category_name']; |
---|
440 | $arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']) . $name; |
---|
441 | } |
---|
442 | return $arrList; |
---|
443 | } |
---|
444 | |
---|
445 | /** |
---|
446 | * カテゴリーツリーの取得を行う. |
---|
447 | * |
---|
448 | * 親カテゴリの Value=0 を対象とする |
---|
449 | * |
---|
450 | * @param bool $parent_zero 親カテゴリの Value=0 の場合 true |
---|
451 | * @return array カテゴリツリーの配列 |
---|
452 | */ |
---|
453 | function sfGetLevelCatList($parent_zero = true) { |
---|
454 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
455 | |
---|
456 | // カテゴリ名リストを取得 |
---|
457 | $col = "category_id, parent_category_id, category_name"; |
---|
458 | $where = "del_flg = 0"; |
---|
459 | $objQuery->setOption("ORDER BY level"); |
---|
460 | $arrRet = $objQuery->select($col, "dtb_category", $where); |
---|
461 | $arrCatName = array(); |
---|
462 | foreach ($arrRet as $arrTmp) { |
---|
463 | $arrCatName[$arrTmp['category_id']] = |
---|
464 | (($arrTmp['parent_category_id'] > 0)? |
---|
465 | $arrCatName[$arrTmp['parent_category_id']] : "") |
---|
466 | . CATEGORY_HEAD . $arrTmp['category_name']; |
---|
467 | } |
---|
468 | |
---|
469 | $col = "category_id, parent_category_id, category_name, level"; |
---|
470 | $where = "del_flg = 0"; |
---|
471 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
472 | $arrRet = $objQuery->select($col, "dtb_category", $where); |
---|
473 | $max = count($arrRet); |
---|
474 | |
---|
475 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
476 | if($parent_zero) { |
---|
477 | if($arrRet[$cnt]['level'] == LEVEL_MAX) { |
---|
478 | $arrValue[$cnt] = $arrRet[$cnt]['category_id']; |
---|
479 | } else { |
---|
480 | $arrValue[$cnt] = ""; |
---|
481 | } |
---|
482 | } else { |
---|
483 | $arrValue[$cnt] = $arrRet[$cnt]['category_id']; |
---|
484 | } |
---|
485 | |
---|
486 | $arrOutput[$cnt] = $arrCatName[$arrRet[$cnt]['category_id']]; |
---|
487 | } |
---|
488 | |
---|
489 | return array($arrValue, $arrOutput); |
---|
490 | } |
---|
491 | |
---|
492 | /** |
---|
493 | * 選択中の商品のカテゴリを取得する. |
---|
494 | * |
---|
495 | * @param integer $product_id プロダクトID |
---|
496 | * @param integer $category_id カテゴリID |
---|
497 | * @return array 選択中の商品のカテゴリIDの配列 |
---|
498 | * |
---|
499 | */ |
---|
500 | function sfGetCategoryId($product_id, $category_id = 0, $closed = false) { |
---|
501 | if ($closed) { |
---|
502 | $status = ""; |
---|
503 | } else { |
---|
504 | $status = "status = 1"; |
---|
505 | } |
---|
506 | |
---|
507 | if(!$this->g_category_on) { |
---|
508 | $this->g_category_on = true; |
---|
509 | $category_id = (int) $category_id; |
---|
510 | $product_id = (int) $product_id; |
---|
511 | if (SC_Utils_Ex::sfIsInt($category_id) && $category_id != 0 && $this->sfIsRecord("dtb_category","category_id", $category_id)) { |
---|
512 | $this->g_category_id = array($category_id); |
---|
513 | } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) { |
---|
514 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
515 | $where = "product_id = ?"; |
---|
516 | $category_id = $objQuery->getCol("category_id", "dtb_product_categories", "product_id = ?", array($product_id)); |
---|
517 | $this->g_category_id = $category_id; |
---|
518 | } else { |
---|
519 | // 不正な場合は、空の配列を返す。 |
---|
520 | $this->g_category_id = array(); |
---|
521 | } |
---|
522 | } |
---|
523 | return $this->g_category_id; |
---|
524 | } |
---|
525 | |
---|
526 | /** |
---|
527 | * 商品をカテゴリの先頭に追加する. |
---|
528 | * |
---|
529 | * @param integer $category_id カテゴリID |
---|
530 | * @param integer $product_id プロダクトID |
---|
531 | * @return void |
---|
532 | */ |
---|
533 | function addProductBeforCategories($category_id, $product_id) { |
---|
534 | |
---|
535 | $sqlval = array("category_id" => $category_id, |
---|
536 | "product_id" => $product_id); |
---|
537 | |
---|
538 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
539 | |
---|
540 | // 現在の商品カテゴリを取得 |
---|
541 | $arrCat = $objQuery->select("product_id, category_id, rank", |
---|
542 | "dtb_product_categories", |
---|
543 | "category_id = ?", |
---|
544 | array($category_id)); |
---|
545 | |
---|
546 | $max = "0"; |
---|
547 | foreach ($arrCat as $val) { |
---|
548 | // 同一商品が存在する場合は登録しない |
---|
549 | if ($val["product_id"] == $product_id) { |
---|
550 | return; |
---|
551 | } |
---|
552 | // 最上位ランクを取得 |
---|
553 | $max = ($max < $val["rank"]) ? $val["rank"] : $max; |
---|
554 | } |
---|
555 | $sqlval["rank"] = $max + 1; |
---|
556 | $objQuery->insert("dtb_product_categories", $sqlval); |
---|
557 | } |
---|
558 | |
---|
559 | /** |
---|
560 | * 商品をカテゴリの末尾に追加する. |
---|
561 | * |
---|
562 | * @param integer $category_id カテゴリID |
---|
563 | * @param integer $product_id プロダクトID |
---|
564 | * @return void |
---|
565 | */ |
---|
566 | function addProductAfterCategories($category_id, $product_id) { |
---|
567 | $sqlval = array("category_id" => $category_id, |
---|
568 | "product_id" => $product_id); |
---|
569 | |
---|
570 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
571 | |
---|
572 | // 現在の商品カテゴリを取得 |
---|
573 | $arrCat = $objQuery->select("product_id, category_id, rank", |
---|
574 | "dtb_product_categories", |
---|
575 | "category_id = ?", |
---|
576 | array($category_id)); |
---|
577 | |
---|
578 | $min = 0; |
---|
579 | foreach ($arrCat as $val) { |
---|
580 | // 同一商品が存在する場合は登録しない |
---|
581 | if ($val["product_id"] == $product_id) { |
---|
582 | return; |
---|
583 | } |
---|
584 | // 最下位ランクを取得 |
---|
585 | $min = ($min < $val["rank"]) ? $val["rank"] : $min; |
---|
586 | } |
---|
587 | $sqlval["rank"] = $min; |
---|
588 | $objQuery->insert("dtb_product_categories", $sqlval); |
---|
589 | } |
---|
590 | |
---|
591 | /** |
---|
592 | * 商品をカテゴリから削除する. |
---|
593 | * |
---|
594 | * @param integer $category_id カテゴリID |
---|
595 | * @param integer $product_id プロダクトID |
---|
596 | * @return void |
---|
597 | */ |
---|
598 | function removeProductByCategories($category_id, $product_id) { |
---|
599 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
600 | $objQuery->delete("dtb_product_categories", |
---|
601 | "category_id = ? AND product_id = ?", array($category_id, $product_id)); |
---|
602 | } |
---|
603 | |
---|
604 | /** |
---|
605 | * 商品カテゴリを更新する. |
---|
606 | * |
---|
607 | * @param array $arrCategory_id 登録するカテゴリIDの配列 |
---|
608 | * @param integer $product_id プロダクトID |
---|
609 | * @return void |
---|
610 | */ |
---|
611 | function updateProductCategories($arrCategory_id, $product_id) { |
---|
612 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
613 | |
---|
614 | // 現在のカテゴリ情報を取得 |
---|
615 | $arrCurrentCat = $objQuery->select("product_id, category_id, rank", |
---|
616 | "dtb_product_categories", |
---|
617 | "product_id = ?", |
---|
618 | array($product_id)); |
---|
619 | |
---|
620 | // 登録するカテゴリ情報と比較 |
---|
621 | foreach ($arrCurrentCat as $val) { |
---|
622 | |
---|
623 | // 登録しないカテゴリを削除 |
---|
624 | if (!in_array($val["category_id"], $arrCategory_id)) { |
---|
625 | $this->removeProductByCategories($val["category_id"], $product_id); |
---|
626 | } |
---|
627 | } |
---|
628 | |
---|
629 | // カテゴリを登録 |
---|
630 | foreach ($arrCategory_id as $category_id) { |
---|
631 | $this->addProductBeforCategories($category_id, $product_id); |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | /** |
---|
636 | * カテゴリ数の登録を行う. |
---|
637 | * |
---|
638 | * |
---|
639 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
640 | * @param boolean $is_force_all_count 全カテゴリの集計を強制する場合 true |
---|
641 | * @return void |
---|
642 | */ |
---|
643 | function sfCountCategory($objQuery = NULL, $is_force_all_count = false){ |
---|
644 | $objProduct = new SC_Product(); |
---|
645 | |
---|
646 | if($objQuery == NULL) { |
---|
647 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
648 | } |
---|
649 | |
---|
650 | $is_out_trans = false; |
---|
651 | if(!$objQuery->inTransaction()){ |
---|
652 | $objQuery->begin(); |
---|
653 | $is_out_trans = true; |
---|
654 | } |
---|
655 | |
---|
656 | //共通のfrom/where文の構築 |
---|
657 | $sql_where = 'alldtl.del_flg = 0 AND alldtl.status = 1'; |
---|
658 | // 在庫無し商品の非表示 |
---|
659 | if (NOSTOCK_HIDDEN === true) { |
---|
660 | $sql_where_dtl = 'stock_max >= 1 OR stock_unlimited_max = 1'; |
---|
661 | $from = $objProduct->alldtlSQL($sql_where_dtl); |
---|
662 | }else{ |
---|
663 | $from = " dtb_products as alldtl "; |
---|
664 | } |
---|
665 | |
---|
666 | //dtb_category_countの構成 |
---|
667 | // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを含まない。 |
---|
668 | // 2.5で消える予定だったが復活させます。DELETE処理は無くしました。 |
---|
669 | |
---|
670 | //まずテーブル内容の元を取得 |
---|
671 | if(!$is_force_all_count) { |
---|
672 | $arrCategoryCountOld = $objQuery->select('category_id,product_count','dtb_category_count'); |
---|
673 | }else{ |
---|
674 | $arrCategoryCountOld = array(); |
---|
675 | } |
---|
676 | |
---|
677 | //各カテゴリ内の商品数を数えて取得 |
---|
678 | $sql = <<< __EOS__ |
---|
679 | SELECT T1.category_id, count(T2.category_id) as product_count |
---|
680 | FROM dtb_category AS T1 |
---|
681 | LEFT JOIN dtb_product_categories AS T2 |
---|
682 | ON T1.category_id = T2.category_id |
---|
683 | LEFT JOIN $from |
---|
684 | ON T2.product_id = alldtl.product_id |
---|
685 | WHERE $sql_where |
---|
686 | GROUP BY T1.category_id, T2.category_id |
---|
687 | __EOS__; |
---|
688 | |
---|
689 | $arrCategoryCountNew = $objQuery->getAll($sql); |
---|
690 | // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。 |
---|
691 | //差分を取得して、更新対象カテゴリだけを確認する。 |
---|
692 | |
---|
693 | //各カテゴリ毎のデータ値において以前との差を見る |
---|
694 | //古いデータの構造入れ替え |
---|
695 | $arrOld = array(); |
---|
696 | foreach($arrCategoryCountOld as $item){ |
---|
697 | $arrOld[$item['category_id']] = $item['product_count']; |
---|
698 | } |
---|
699 | //新しいデータの構造入れ替え |
---|
700 | $arrNew = array(); |
---|
701 | foreach($arrCategoryCountNew as $item){ |
---|
702 | $arrNew[$item['category_id']] = $item['product_count']; |
---|
703 | } |
---|
704 | |
---|
705 | $arrDiffCategory_id = array(); |
---|
706 | //新しいカテゴリ一覧から見て商品数が異なるデータが無いか確認 |
---|
707 | foreach($arrNew as $cid => $count){ |
---|
708 | if($arrOld[$cid] != $count){ |
---|
709 | $arrDiffCategory_id[] = $cid; |
---|
710 | } |
---|
711 | } |
---|
712 | //削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。 |
---|
713 | foreach($arrOld as $cid => $count){ |
---|
714 | if($arrNew[$cid] != $count){ |
---|
715 | $arrDiffCategory_id[] = $cid; |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | //対象IDが無ければ終了 |
---|
720 | if(count($arrDiffCategory_id) == 0){ |
---|
721 | if($is_out_trans) { |
---|
722 | $objQuery->commit(); |
---|
723 | } |
---|
724 | return; |
---|
725 | } |
---|
726 | |
---|
727 | //差分対象カテゴリIDの重複を除去 |
---|
728 | $arrDiffCategory_id = array_unique($arrDiffCategory_id); |
---|
729 | |
---|
730 | //dtb_category_countの更新 差分のあったカテゴリだけ更新する。 |
---|
731 | foreach($arrDiffCategory_id as $cid) { |
---|
732 | $sqlval = array(); |
---|
733 | $sqlval['create_date'] = 'Now()'; |
---|
734 | $sqlval['product_count'] = (string)$arrNew[$cid]; |
---|
735 | if($sqlval['product_count'] =="") { |
---|
736 | $sqlval['product_count'] = (string)'0'; |
---|
737 | } |
---|
738 | if(isset($arrOld[$cid])) { |
---|
739 | $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid)); |
---|
740 | }else{ |
---|
741 | $sqlval['category_id'] = $cid; |
---|
742 | $objQuery->insert('dtb_category_count', $sqlval); |
---|
743 | } |
---|
744 | } |
---|
745 | |
---|
746 | //差分があったIDとその親カテゴリIDのリストを取得する |
---|
747 | $arrTgtCategory_id = array(); |
---|
748 | foreach ($arrDiffCategory_id as $parent_category_id) { |
---|
749 | $arrTgtCategory_id[] = $parent_category_id; |
---|
750 | $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id); |
---|
751 | foreach($arrParentID as $pid) { |
---|
752 | $arrTgtCategory_id[] = $pid; |
---|
753 | } |
---|
754 | } |
---|
755 | |
---|
756 | //重複を取り除く |
---|
757 | $arrTgtCategory_id = array_unique($arrTgtCategory_id); |
---|
758 | |
---|
759 | //dtb_category_total_count 集計処理開始 |
---|
760 | //更新対象カテゴリIDだけ集計しなおす。 |
---|
761 | $arrUpdateData = array(); |
---|
762 | foreach ($arrTgtCategory_id as $category_id) { |
---|
763 | $arrval = array(); |
---|
764 | $arrval[] = $category_id; |
---|
765 | list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id); |
---|
766 | if ($tmp_where != "") { |
---|
767 | $sql_where_product_ids = "product_id IN (SELECT product_id FROM dtb_product_categories WHERE " . $tmp_where . ")"; |
---|
768 | $arrval = array_merge((array)$arrval, (array)$tmp_arrval, (array)$tmp_arrval); |
---|
769 | } else { |
---|
770 | $sql_where_product_ids = '0<>0'; // 一致させない |
---|
771 | } |
---|
772 | $where = "($sql_where) AND ($sql_where_product_ids)"; |
---|
773 | |
---|
774 | $from = $objProduct->alldtlSQL($sql_where_product_ids); |
---|
775 | $sql = "SELECT count(*) FROM $from WHERE $where "; |
---|
776 | $arrUpdateData[ $category_id ] = $objQuery->getOne($sql, $arrval); |
---|
777 | } |
---|
778 | // 更新対象だけを更新。 |
---|
779 | foreach($arrUpdateData as $cid => $count) { |
---|
780 | $sqlval = array(); |
---|
781 | $sqlval['create_date'] = 'Now()'; |
---|
782 | $sqlval['product_count'] = $count; |
---|
783 | if($sqlval['product_count'] =="") { |
---|
784 | $sqlval['product_count'] = (string)'0'; |
---|
785 | } |
---|
786 | $ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', array($cid)); |
---|
787 | if(!$ret) { |
---|
788 | $sqlval['category_id'] = $cid; |
---|
789 | $ret = $objQuery->insert('dtb_category_total_count', $sqlval); |
---|
790 | } |
---|
791 | } |
---|
792 | // トランザクション音終了処理 |
---|
793 | if($is_out_trans) { |
---|
794 | $objQuery->commit(); |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | /** |
---|
799 | * 子IDの配列を返す. |
---|
800 | * |
---|
801 | * @param string $table テーブル名 |
---|
802 | * @param string $pid_name 親ID名 |
---|
803 | * @param string $id_name ID名 |
---|
804 | * @param integer $id ID |
---|
805 | * @param array 子ID の配列 |
---|
806 | */ |
---|
807 | function sfGetChildsID($table, $pid_name, $id_name, $id) { |
---|
808 | $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
809 | return $arrRet; |
---|
810 | } |
---|
811 | |
---|
812 | /** |
---|
813 | * 階層構造のテーブルから子ID配列を取得する. |
---|
814 | * |
---|
815 | * @param string $table テーブル名 |
---|
816 | * @param string $pid_name 親ID名 |
---|
817 | * @param string $id_name ID名 |
---|
818 | * @param integer $id ID番号 |
---|
819 | * @return array 子IDの配列 |
---|
820 | */ |
---|
821 | function sfGetChildrenArray($table, $pid_name, $id_name, $id) { |
---|
822 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
823 | $col = $pid_name . "," . $id_name; |
---|
824 | $arrData = $objQuery->select($col, $table); |
---|
825 | |
---|
826 | $arrPID = array(); |
---|
827 | $arrPID[] = $id; |
---|
828 | $arrChildren = array(); |
---|
829 | $arrChildren[] = $id; |
---|
830 | |
---|
831 | $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID); |
---|
832 | |
---|
833 | while(count($arrRet) > 0) { |
---|
834 | $arrChildren = array_merge($arrChildren, $arrRet); |
---|
835 | $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet); |
---|
836 | } |
---|
837 | |
---|
838 | return $arrChildren; |
---|
839 | } |
---|
840 | |
---|
841 | /** |
---|
842 | * 親ID直下の子IDをすべて取得する. |
---|
843 | * |
---|
844 | * @param array $arrData 親カテゴリの配列 |
---|
845 | * @param string $pid_name 親ID名 |
---|
846 | * @param string $id_name ID名 |
---|
847 | * @param array $arrPID 親IDの配列 |
---|
848 | * @return array 子IDの配列 |
---|
849 | */ |
---|
850 | function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) { |
---|
851 | $arrChildren = array(); |
---|
852 | $max = count($arrData); |
---|
853 | |
---|
854 | for($i = 0; $i < $max; $i++) { |
---|
855 | foreach($arrPID as $val) { |
---|
856 | if($arrData[$i][$pid_name] == $val) { |
---|
857 | $arrChildren[] = $arrData[$i][$id_name]; |
---|
858 | } |
---|
859 | } |
---|
860 | } |
---|
861 | return $arrChildren; |
---|
862 | } |
---|
863 | |
---|
864 | /** |
---|
865 | * 所属するすべての階層の親IDを配列で返す. |
---|
866 | * |
---|
867 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
868 | * @param string $table テーブル名 |
---|
869 | * @param string $pid_name 親ID名 |
---|
870 | * @param string $id_name ID名 |
---|
871 | * @param integer $id ID |
---|
872 | * @return array 親IDの配列 |
---|
873 | */ |
---|
874 | function sfGetParents($table, $pid_name, $id_name, $id) { |
---|
875 | $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id); |
---|
876 | // 配列の先頭1つを削除する。 |
---|
877 | array_shift($arrRet); |
---|
878 | return $arrRet; |
---|
879 | } |
---|
880 | |
---|
881 | /** |
---|
882 | * 階層構造のテーブルから親ID配列を取得する. |
---|
883 | * |
---|
884 | * @param string $table テーブル名 |
---|
885 | * @param string $pid_name 親ID名 |
---|
886 | * @param string $id_name ID名 |
---|
887 | * @param integer $id ID |
---|
888 | * @return array 親IDの配列 |
---|
889 | */ |
---|
890 | function sfGetParentsArray($table, $pid_name, $id_name, $id) { |
---|
891 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
892 | $col = $pid_name . "," . $id_name; |
---|
893 | $arrData = $objQuery->select($col, $table); |
---|
894 | |
---|
895 | $arrParents = array(); |
---|
896 | $arrParents[] = $id; |
---|
897 | $child = $id; |
---|
898 | |
---|
899 | $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child); |
---|
900 | |
---|
901 | while($ret != "") { |
---|
902 | $arrParents[] = $ret; |
---|
903 | $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret); |
---|
904 | } |
---|
905 | |
---|
906 | $arrParents = array_reverse($arrParents); |
---|
907 | |
---|
908 | return $arrParents; |
---|
909 | } |
---|
910 | |
---|
911 | /** |
---|
912 | * カテゴリから商品を検索する場合のWHERE文と値を返す. |
---|
913 | * |
---|
914 | * @param integer $category_id カテゴリID |
---|
915 | * @return array 商品を検索する場合の配列 |
---|
916 | */ |
---|
917 | function sfGetCatWhere($category_id) { |
---|
918 | // 子カテゴリIDの取得 |
---|
919 | $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id); |
---|
920 | $tmp_where = ""; |
---|
921 | foreach ($arrRet as $val) { |
---|
922 | if($tmp_where == "") { |
---|
923 | $tmp_where.= "category_id IN ( ?"; |
---|
924 | } else { |
---|
925 | $tmp_where.= ",? "; |
---|
926 | } |
---|
927 | $arrval[] = $val; |
---|
928 | } |
---|
929 | $tmp_where.= " ) "; |
---|
930 | return array($tmp_where, $arrval); |
---|
931 | } |
---|
932 | |
---|
933 | /** |
---|
934 | * SELECTボックス用リストを作成する. |
---|
935 | * |
---|
936 | * @param string $table テーブル名 |
---|
937 | * @param string $keyname プライマリーキーのカラム名 |
---|
938 | * @param string $valname データ内容のカラム名 |
---|
939 | * @param string $where WHERE句 |
---|
940 | * @param array $arrval プレースホルダ |
---|
941 | * @return array SELECT ボックス用リストの配列 |
---|
942 | */ |
---|
943 | function sfGetIDValueList($table, $keyname, $valname, $where = '', $arrVal = array()) { |
---|
944 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
945 | $col = "$keyname, $valname"; |
---|
946 | $objQuery->setWhere("del_flg = 0"); |
---|
947 | $objQuery->setOrder("rank DESC"); |
---|
948 | $arrList = $objQuery->select($col, $table, $where, $arrVal); |
---|
949 | $count = count($arrList); |
---|
950 | for($cnt = 0; $cnt < $count; $cnt++) { |
---|
951 | $key = $arrList[$cnt][$keyname]; |
---|
952 | $val = $arrList[$cnt][$valname]; |
---|
953 | $arrRet[$key] = $val; |
---|
954 | } |
---|
955 | return $arrRet; |
---|
956 | } |
---|
957 | |
---|
958 | /** |
---|
959 | * ランキングを上げる. |
---|
960 | * |
---|
961 | * @param string $table テーブル名 |
---|
962 | * @param string $colname カラム名 |
---|
963 | * @param string|integer $id テーブルのキー |
---|
964 | * @param string $andwhere SQL の AND 条件である WHERE 句 |
---|
965 | * @return void |
---|
966 | */ |
---|
967 | function sfRankUp($table, $colname, $id, $andwhere = "") { |
---|
968 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
969 | $objQuery->begin(); |
---|
970 | $where = "$colname = ?"; |
---|
971 | if($andwhere != "") { |
---|
972 | $where.= " AND $andwhere"; |
---|
973 | } |
---|
974 | // 対象項目のランクを取得 |
---|
975 | $rank = $objQuery->get("rank", $table, $where, array($id)); |
---|
976 | // ランクの最大値を取得 |
---|
977 | $maxrank = $objQuery->max("rank", $table, $andwhere); |
---|
978 | // ランクが最大値よりも小さい場合に実行する。 |
---|
979 | if($rank < $maxrank) { |
---|
980 | // ランクが一つ上のIDを取得する。 |
---|
981 | $where = "rank = ?"; |
---|
982 | if($andwhere != "") { |
---|
983 | $where.= " AND $andwhere"; |
---|
984 | } |
---|
985 | $uprank = $rank + 1; |
---|
986 | $up_id = $objQuery->get($colname, $table, $where, array($uprank)); |
---|
987 | // ランク入れ替えの実行 |
---|
988 | $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?"; |
---|
989 | if($andwhere != "") { |
---|
990 | $sqlup.= " AND $andwhere"; |
---|
991 | } |
---|
992 | $objQuery->exec($sqlup, array($rank + 1, $id)); |
---|
993 | $objQuery->exec($sqlup, array($rank, $up_id)); |
---|
994 | } |
---|
995 | $objQuery->commit(); |
---|
996 | } |
---|
997 | |
---|
998 | /** |
---|
999 | * ランキングを下げる. |
---|
1000 | * |
---|
1001 | * @param string $table テーブル名 |
---|
1002 | * @param string $colname カラム名 |
---|
1003 | * @param string|integer $id テーブルのキー |
---|
1004 | * @param string $andwhere SQL の AND 条件である WHERE 句 |
---|
1005 | * @return void |
---|
1006 | */ |
---|
1007 | function sfRankDown($table, $colname, $id, $andwhere = "") { |
---|
1008 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1009 | $objQuery->begin(); |
---|
1010 | $where = "$colname = ?"; |
---|
1011 | if($andwhere != "") { |
---|
1012 | $where.= " AND $andwhere"; |
---|
1013 | } |
---|
1014 | // 対象項目のランクを取得 |
---|
1015 | $rank = $objQuery->get("rank", $table, $where, array($id)); |
---|
1016 | |
---|
1017 | // ランクが1(最小値)よりも大きい場合に実行する。 |
---|
1018 | if($rank > 1) { |
---|
1019 | // ランクが一つ下のIDを取得する。 |
---|
1020 | $where = "rank = ?"; |
---|
1021 | if($andwhere != "") { |
---|
1022 | $where.= " AND $andwhere"; |
---|
1023 | } |
---|
1024 | $downrank = $rank - 1; |
---|
1025 | $down_id = $objQuery->get($colname, $table, $where, array($downrank)); |
---|
1026 | // ランク入れ替えの実行 |
---|
1027 | $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?"; |
---|
1028 | if($andwhere != "") { |
---|
1029 | $sqlup.= " AND $andwhere"; |
---|
1030 | } |
---|
1031 | $objQuery->exec($sqlup, array($rank - 1, $id)); |
---|
1032 | $objQuery->exec($sqlup, array($rank, $down_id)); |
---|
1033 | } |
---|
1034 | $objQuery->commit(); |
---|
1035 | } |
---|
1036 | |
---|
1037 | /** |
---|
1038 | * 指定順位へ移動する. |
---|
1039 | * |
---|
1040 | * @param string $tableName テーブル名 |
---|
1041 | * @param string $keyIdColumn キーを保持するカラム名 |
---|
1042 | * @param string|integer $keyId キーの値 |
---|
1043 | * @param integer $pos 指定順位 |
---|
1044 | * @param string $where SQL の AND 条件である WHERE 句 |
---|
1045 | * @return void |
---|
1046 | */ |
---|
1047 | function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") { |
---|
1048 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1049 | $objQuery->begin(); |
---|
1050 | |
---|
1051 | // 自身のランクを取得する |
---|
1052 | if($where != "") { |
---|
1053 | $getWhere = "$keyIdColumn = ? AND " . $where; |
---|
1054 | } else { |
---|
1055 | $getWhere = "$keyIdColumn = ?"; |
---|
1056 | } |
---|
1057 | $rank = $objQuery->get("rank", $tableName, $getWhere, array($keyId)); |
---|
1058 | |
---|
1059 | $max = $objQuery->max("rank", $tableName, $where); |
---|
1060 | |
---|
1061 | // 値の調整(逆順) |
---|
1062 | if($pos > $max) { |
---|
1063 | $position = 1; |
---|
1064 | } else if($pos < 1) { |
---|
1065 | $position = $max; |
---|
1066 | } else { |
---|
1067 | $position = $max - $pos + 1; |
---|
1068 | } |
---|
1069 | |
---|
1070 | //入れ替え先の順位が入れ換え元の順位より大きい場合 |
---|
1071 | if( $position > $rank ) $term = "rank - 1"; |
---|
1072 | |
---|
1073 | //入れ替え先の順位が入れ換え元の順位より小さい場合 |
---|
1074 | if( $position < $rank ) $term = "rank + 1"; |
---|
1075 | |
---|
1076 | // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合 |
---|
1077 | if (!isset($term)) $term = "rank"; |
---|
1078 | |
---|
1079 | // 指定した順位の商品から移動させる商品までのrankを1つずらす |
---|
1080 | $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?"; |
---|
1081 | if($where != "") { |
---|
1082 | $sql.= " AND $where"; |
---|
1083 | } |
---|
1084 | |
---|
1085 | if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position )); |
---|
1086 | if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 )); |
---|
1087 | |
---|
1088 | // 指定した順位へrankを書き換える。 |
---|
1089 | $sql = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? "; |
---|
1090 | if($where != "") { |
---|
1091 | $sql.= " AND $where"; |
---|
1092 | } |
---|
1093 | |
---|
1094 | $objQuery->exec( $sql, array( $position, $keyId ) ); |
---|
1095 | $objQuery->commit(); |
---|
1096 | } |
---|
1097 | |
---|
1098 | /** |
---|
1099 | * ランクを含むレコードを削除する. |
---|
1100 | * |
---|
1101 | * レコードごと削除する場合は、$deleteをtrueにする |
---|
1102 | * |
---|
1103 | * @param string $table テーブル名 |
---|
1104 | * @param string $colname カラム名 |
---|
1105 | * @param string|integer $id テーブルのキー |
---|
1106 | * @param string $andwhere SQL の AND 条件である WHERE 句 |
---|
1107 | * @param bool $delete レコードごと削除する場合 true, |
---|
1108 | * レコードごと削除しない場合 false |
---|
1109 | * @return void |
---|
1110 | */ |
---|
1111 | function sfDeleteRankRecord($table, $colname, $id, $andwhere = "", |
---|
1112 | $delete = false) { |
---|
1113 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1114 | $objQuery->begin(); |
---|
1115 | // 削除レコードのランクを取得する。 |
---|
1116 | $where = "$colname = ?"; |
---|
1117 | if($andwhere != "") { |
---|
1118 | $where.= " AND $andwhere"; |
---|
1119 | } |
---|
1120 | $rank = $objQuery->get("rank", $table, $where, array($id)); |
---|
1121 | |
---|
1122 | if(!$delete) { |
---|
1123 | // ランクを最下位にする、DELフラグON |
---|
1124 | $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 "; |
---|
1125 | $sqlup.= "WHERE $colname = ?"; |
---|
1126 | // UPDATEの実行 |
---|
1127 | $objQuery->exec($sqlup, array($id)); |
---|
1128 | } else { |
---|
1129 | $objQuery->delete($table, "$colname = ?", array($id)); |
---|
1130 | } |
---|
1131 | |
---|
1132 | // 追加レコードのランクより上のレコードを一つずらす。 |
---|
1133 | $where = "rank > ?"; |
---|
1134 | if($andwhere != "") { |
---|
1135 | $where.= " AND $andwhere"; |
---|
1136 | } |
---|
1137 | $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where"; |
---|
1138 | $objQuery->exec($sqlup, array($rank)); |
---|
1139 | $objQuery->commit(); |
---|
1140 | } |
---|
1141 | |
---|
1142 | /** |
---|
1143 | * 親IDの配列を元に特定のカラムを取得する. |
---|
1144 | * |
---|
1145 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
1146 | * @param string $table テーブル名 |
---|
1147 | * @param string $id_name ID名 |
---|
1148 | * @param string $col_name カラム名 |
---|
1149 | * @param array $arrId IDの配列 |
---|
1150 | * @return array 特定のカラムの配列 |
---|
1151 | */ |
---|
1152 | function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) { |
---|
1153 | $col = $col_name; |
---|
1154 | $len = count($arrId); |
---|
1155 | $where = ""; |
---|
1156 | |
---|
1157 | for($cnt = 0; $cnt < $len; $cnt++) { |
---|
1158 | if($where == "") { |
---|
1159 | $where = "$id_name = ?"; |
---|
1160 | } else { |
---|
1161 | $where.= " OR $id_name = ?"; |
---|
1162 | } |
---|
1163 | } |
---|
1164 | |
---|
1165 | $objQuery->setOrder("level"); |
---|
1166 | $arrRet = $objQuery->select($col, $table, $where, $arrId); |
---|
1167 | return $arrRet; |
---|
1168 | } |
---|
1169 | |
---|
1170 | /** |
---|
1171 | * カテゴリ変更時の移動処理を行う. |
---|
1172 | * |
---|
1173 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
1174 | * @param string $table テーブル名 |
---|
1175 | * @param string $id_name ID名 |
---|
1176 | * @param string $cat_name カテゴリ名 |
---|
1177 | * @param integer $old_catid 旧カテゴリID |
---|
1178 | * @param integer $new_catid 新カテゴリID |
---|
1179 | * @param integer $id ID |
---|
1180 | * @return void |
---|
1181 | */ |
---|
1182 | function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) { |
---|
1183 | if ($old_catid == $new_catid) { |
---|
1184 | return; |
---|
1185 | } |
---|
1186 | // 旧カテゴリでのランク削除処理 |
---|
1187 | // 移動レコードのランクを取得する。 |
---|
1188 | $where = "$id_name = ?"; |
---|
1189 | $rank = $objQuery->get("rank", $table, $where, array($id)); |
---|
1190 | // 削除レコードのランクより上のレコードを一つ下にずらす。 |
---|
1191 | $where = "rank > ? AND $cat_name = ?"; |
---|
1192 | $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where"; |
---|
1193 | $objQuery->exec($sqlup, array($rank, $old_catid)); |
---|
1194 | // 新カテゴリでの登録処理 |
---|
1195 | // 新カテゴリの最大ランクを取得する。 |
---|
1196 | $max_rank = $objQuery->max("rank", $table, "$cat_name = ?", array($new_catid)) + 1; |
---|
1197 | $where = "$id_name = ?"; |
---|
1198 | $sqlup = "UPDATE $table SET rank = ? WHERE $where"; |
---|
1199 | $objQuery->exec($sqlup, array($max_rank, $id)); |
---|
1200 | } |
---|
1201 | |
---|
1202 | /** |
---|
1203 | * 都道府県から配送料金を取得する. |
---|
1204 | * |
---|
1205 | * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列 |
---|
1206 | * @return string 指定の都道府県, 商品種別の配送料金 |
---|
1207 | */ |
---|
1208 | function sfGetDelivFee($pref_id, $product_type_id) { |
---|
1209 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1210 | |
---|
1211 | /* |
---|
1212 | * FIXME 都道府県が指定されていない場合は、東京都の番号を指定しておく |
---|
1213 | * http://svn.ec-cube.net/open_trac/ticket/410 |
---|
1214 | */ |
---|
1215 | if($pref_id == "") { |
---|
1216 | $pref_id = 13; |
---|
1217 | } |
---|
1218 | if (!is_array($pref_id)) { |
---|
1219 | $pref_id = array($pref_id); |
---|
1220 | } |
---|
1221 | $sql = <<< __EOS__ |
---|
1222 | SELECT SUM(T1.fee) AS fee |
---|
1223 | FROM dtb_delivfee T1 |
---|
1224 | JOIN dtb_deliv T2 |
---|
1225 | ON T1.deliv_id = T2.deliv_id |
---|
1226 | WHERE T1.pref = ? AND T2.product_type_id = ? |
---|
1227 | __EOS__; |
---|
1228 | |
---|
1229 | $result = 0; |
---|
1230 | foreach ($pref_id as $pref) { |
---|
1231 | $result += $objQuery->getOne($sql, array($pref, $product_type_id)); |
---|
1232 | } |
---|
1233 | return $result; |
---|
1234 | } |
---|
1235 | |
---|
1236 | /** |
---|
1237 | * レコードの存在チェックを行う. |
---|
1238 | * |
---|
1239 | * TODO SC_Query に移行するべきか? |
---|
1240 | * |
---|
1241 | * @param string $table テーブル名 |
---|
1242 | * @param string $col カラム名 |
---|
1243 | * @param array $arrval 要素の配列 |
---|
1244 | * @param array $addwhere SQL の AND 条件である WHERE 句 |
---|
1245 | * @return bool レコードが存在する場合 true |
---|
1246 | */ |
---|
1247 | function sfIsRecord($table, $col, $arrval, $addwhere = "") { |
---|
1248 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1249 | $arrCol = split("[, ]", $col); |
---|
1250 | |
---|
1251 | $where = "del_flg = 0"; |
---|
1252 | |
---|
1253 | if($addwhere != "") { |
---|
1254 | $where.= " AND $addwhere"; |
---|
1255 | } |
---|
1256 | |
---|
1257 | foreach($arrCol as $val) { |
---|
1258 | if($val != "") { |
---|
1259 | if($where == "") { |
---|
1260 | $where = "$val = ?"; |
---|
1261 | } else { |
---|
1262 | $where.= " AND $val = ?"; |
---|
1263 | } |
---|
1264 | } |
---|
1265 | } |
---|
1266 | $ret = $objQuery->get($col, $table, $where, $arrval); |
---|
1267 | |
---|
1268 | if($ret != "") { |
---|
1269 | return true; |
---|
1270 | } |
---|
1271 | return false; |
---|
1272 | } |
---|
1273 | |
---|
1274 | /** |
---|
1275 | * メーカー商品数数の登録を行う. |
---|
1276 | * |
---|
1277 | * @param SC_Query $objQuery SC_Query インスタンス |
---|
1278 | * @return void |
---|
1279 | */ |
---|
1280 | function sfCountMaker($objQuery){ |
---|
1281 | $sql = ""; |
---|
1282 | |
---|
1283 | //テーブル内容の削除 |
---|
1284 | $objQuery->query("DELETE FROM dtb_maker_count"); |
---|
1285 | |
---|
1286 | //各メーカーの商品数を数えて格納 |
---|
1287 | $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) "; |
---|
1288 | $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() "; |
---|
1289 | $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2"; |
---|
1290 | $sql .= " ON T1.maker_id = T2.maker_id "; |
---|
1291 | $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 "; |
---|
1292 | $sql .= " GROUP BY T1.maker_id, T2.maker_id "; |
---|
1293 | $objQuery->query($sql); |
---|
1294 | } |
---|
1295 | |
---|
1296 | /** |
---|
1297 | * 選択中の商品のメーカーを取得する. |
---|
1298 | * |
---|
1299 | * @param integer $product_id プロダクトID |
---|
1300 | * @param integer $maker_id メーカーID |
---|
1301 | * @return array 選択中の商品のメーカーIDの配列 |
---|
1302 | * |
---|
1303 | */ |
---|
1304 | function sfGetMakerId($product_id, $maker_id = 0, $closed = false) { |
---|
1305 | if ($closed) { |
---|
1306 | $status = ""; |
---|
1307 | } else { |
---|
1308 | $status = "status = 1"; |
---|
1309 | } |
---|
1310 | |
---|
1311 | if (!$this->g_maker_on) { |
---|
1312 | $this->g_maker_on = true; |
---|
1313 | $maker_id = (int) $maker_id; |
---|
1314 | $product_id = (int) $product_id; |
---|
1315 | if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) { |
---|
1316 | $this->g_maker_id = array($maker_id); |
---|
1317 | } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) { |
---|
1318 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1319 | $where = "product_id = ?"; |
---|
1320 | $maker_id = $objQuery->getCol("maker_id", "dtb_products", "product_id = ?", array($product_id)); |
---|
1321 | $this->g_maker_id = $maker_id; |
---|
1322 | } else { |
---|
1323 | // 不正な場合は、空の配列を返す。 |
---|
1324 | $this->g_maker_id = array(); |
---|
1325 | } |
---|
1326 | } |
---|
1327 | return $this->g_maker_id; |
---|
1328 | } |
---|
1329 | |
---|
1330 | /** |
---|
1331 | * メーカーの取得を行う. |
---|
1332 | * |
---|
1333 | * $products_check:true商品登録済みのものだけ取得する |
---|
1334 | * |
---|
1335 | * @param string $addwhere 追加する WHERE 句 |
---|
1336 | * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true |
---|
1337 | * @return array カテゴリツリーの配列 |
---|
1338 | */ |
---|
1339 | function sfGetMakerList($addwhere = "", $products_check = false) { |
---|
1340 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1341 | $where = "del_flg = 0"; |
---|
1342 | |
---|
1343 | if($addwhere != "") { |
---|
1344 | $where.= " AND $addwhere"; |
---|
1345 | } |
---|
1346 | |
---|
1347 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
1348 | |
---|
1349 | if($products_check) { |
---|
1350 | $col = "T1.maker_id, name"; |
---|
1351 | $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id"; |
---|
1352 | $where .= " AND product_count > 0"; |
---|
1353 | } else { |
---|
1354 | $col = "maker_id, name"; |
---|
1355 | $from = "dtb_maker"; |
---|
1356 | } |
---|
1357 | |
---|
1358 | $arrRet = $objQuery->select($col, $from, $where); |
---|
1359 | |
---|
1360 | $max = count($arrRet); |
---|
1361 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
1362 | $id = $arrRet[$cnt]['maker_id']; |
---|
1363 | $name = $arrRet[$cnt]['name']; |
---|
1364 | $arrList[$id] = $name; |
---|
1365 | } |
---|
1366 | return $arrList; |
---|
1367 | } |
---|
1368 | |
---|
1369 | /** |
---|
1370 | * 店舗基本情報に基づいて税金額を返す |
---|
1371 | * |
---|
1372 | * @param integer $price 計算対象の金額 |
---|
1373 | * @return integer 税金額 |
---|
1374 | */ |
---|
1375 | function sfTax($price) { |
---|
1376 | // 店舗基本情報を取得 |
---|
1377 | $CONF = SC_Helper_DB_Ex::sfGetBasisData(); |
---|
1378 | |
---|
1379 | return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']); |
---|
1380 | } |
---|
1381 | |
---|
1382 | /** |
---|
1383 | * 店舗基本情報に基づいて税金付与した金額を返す |
---|
1384 | * |
---|
1385 | * @param integer $price 計算対象の金額 |
---|
1386 | * @return integer 税金付与した金額 |
---|
1387 | */ |
---|
1388 | function sfCalcIncTax($price, $tax = null, $tax_rule = null) { |
---|
1389 | // 店舗基本情報を取得 |
---|
1390 | $CONF = SC_Helper_DB_Ex::sfGetBasisData(); |
---|
1391 | |
---|
1392 | return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']); |
---|
1393 | } |
---|
1394 | |
---|
1395 | /** |
---|
1396 | * 店舗基本情報に基づいて加算ポイントを返す |
---|
1397 | * |
---|
1398 | * @param integer $totalpoint |
---|
1399 | * @param integer $use_point |
---|
1400 | * @return integer 加算ポイント |
---|
1401 | */ |
---|
1402 | function sfGetAddPoint($totalpoint, $use_point) { |
---|
1403 | // 店舗基本情報を取得 |
---|
1404 | $CONF = SC_Helper_DB_Ex::sfGetBasisData(); |
---|
1405 | |
---|
1406 | return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']); |
---|
1407 | } |
---|
1408 | |
---|
1409 | /** |
---|
1410 | * 指定ファイルが存在する場合 SQL として実行 |
---|
1411 | * |
---|
1412 | * XXX プラグイン用に追加。将来消すかも。 |
---|
1413 | * |
---|
1414 | * @param string $sqlFilePath SQL ファイルのパス |
---|
1415 | * @return void |
---|
1416 | */ |
---|
1417 | function sfExecSqlByFile($sqlFilePath) { |
---|
1418 | if (file_exists($sqlFilePath)) { |
---|
1419 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1420 | |
---|
1421 | $sqls = file_get_contents($sqlFilePath); |
---|
1422 | if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない'); |
---|
1423 | |
---|
1424 | foreach (explode(';', $sqls) as $sql) { |
---|
1425 | $sql = trim($sql); |
---|
1426 | if (strlen($sql) == 0) continue; |
---|
1427 | $objQuery->query($sql); |
---|
1428 | } |
---|
1429 | } |
---|
1430 | } |
---|
1431 | |
---|
1432 | /** |
---|
1433 | * 商品規格を設定しているか |
---|
1434 | * |
---|
1435 | * @param integer $product_id 商品ID |
---|
1436 | * @return bool 商品規格が存在する場合:true, それ以外:false |
---|
1437 | */ |
---|
1438 | function sfHasProductClass($product_id) { |
---|
1439 | if (!SC_Utils_Ex::sfIsInt($product_id)) return false; |
---|
1440 | |
---|
1441 | $objQuery =& SC_Query::getSingletonInstance(); |
---|
1442 | $where = 'product_id = ? AND class_combination_id IS NOT NULL'; |
---|
1443 | $count = $objQuery->count('dtb_products_class', $where, array($product_id)); |
---|
1444 | |
---|
1445 | return $count >= 1; |
---|
1446 | } |
---|
1447 | } |
---|
1448 | ?> |
---|