Helper
[ class tree: Helper ] [ index: Helper ] [ all elements ]

Source for file SC_Helper_DB.php

Documentation is available at SC_Helper_DB.php

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

Documentation generated on Fri, 24 Feb 2012 14:02:42 +0900 by Seasoft