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