/** * @param SC_Helper_DB_Ex $objDb */ public function lfGetProducts(&$objDb) { $where = 'del_flg = 0'; $arrWhereVal = array(); /* 入力エラーなし */ foreach ($this->arrForm as $key => $val) { if ($val == '') { continue; } switch ($key) { case 'search_name': $where .= ' AND name ILIKE ?'; $arrWhereVal[] = "%{$val}%"; break; case 'search_category_id': list($tmp_where, $arrTmp) = $objDb->sfGetCatWhere($val); if ($tmp_where != '') { $where .= ' AND product_id IN (SELECT product_id FROM dtb_product_categories WHERE ' . $tmp_where . ')'; $arrWhereVal = array_merge((array) $arrWhereVal, (array) $arrTmp); } break; case 'search_product_code': $where .= ' AND product_id IN (SELECT product_id FROM dtb_products_class WHERE product_code LIKE ?)'; $arrWhereVal[] = "{$val}%"; break; default: break; } } $order = 'update_date DESC, product_id DESC '; $objQuery =& SC_Query_Ex::getSingletonInstance(); // 行数の取得 $linemax = $objQuery->count('dtb_products', $where, $arrWhereVal); $this->tpl_linemax = $linemax; // 何件が該当しました。表示用 // ページ送りの処理 $page_max = SC_Utils_Ex::sfGetSearchPageMax($_POST['search_page_max']); // ページ送りの取得 $objNavi = new SC_PageNavi_Ex($_POST['search_pageno'], $linemax, $page_max, 'eccube.moveSearchPage', NAVI_PMAX); $this->tpl_strnavi = $objNavi->strnavi; // 表示文字列 $startno = $objNavi->start_row; // 取得範囲の指定(開始行番号、行数のセット) $objQuery->setLimitOffset($page_max, $startno); // 表示順序 $objQuery->setOrder($order); // 検索結果の取得 // FIXME 商品コードの表示 $arrProducts = $objQuery->select('*', SC_Product_Ex::alldtlSQL(), $where, $arrWhereVal); return $arrProducts; }
/** * カテゴリ数の登録を行う. * * * @param SC_Query $objQuery SC_Query インスタンス * @param boolean $is_force_all_count 全カテゴリの集計を強制する場合 true * @return void */ public function sfCountCategory($objQuery = NULL, $is_force_all_count = false) { $objProduct = new SC_Product_Ex(); if ($objQuery == NULL) { $objQuery =& SC_Query_Ex::getSingletonInstance(); } $is_out_trans = false; if (!$objQuery->inTransaction()) { $objQuery->begin(); $is_out_trans = true; } //共通のfrom/where文の構築 $sql_where = SC_Product_Ex::getProductDispConditions('alldtl'); // 在庫無し商品の非表示 if (NOSTOCK_HIDDEN) { $where_products_class = '(stock >= 1 OR stock_unlimited = 1)'; $from = $objProduct->alldtlSQL($where_products_class); } else { $from = 'dtb_products as alldtl'; } //dtb_category_countの構成 // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを含まない。 //まずテーブル内容の元を取得 if (!$is_force_all_count) { $arrCategoryCountOld = $objQuery->select('category_id,product_count', 'dtb_category_count'); } else { $arrCategoryCountOld = array(); } //各カテゴリ内の商品数を数えて取得 $sql = <<<__EOS__ SELECT T1.category_id, count(T2.category_id) as product_count FROM dtb_category AS T1 LEFT JOIN dtb_product_categories AS T2 ON T1.category_id = T2.category_id LEFT JOIN {$from} ON T2.product_id = alldtl.product_id WHERE {$sql_where} GROUP BY T1.category_id, T2.category_id __EOS__; $arrCategoryCountNew = $objQuery->getAll($sql); // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。 //差分を取得して、更新対象カテゴリだけを確認する。 //各カテゴリ毎のデータ値において以前との差を見る //古いデータの構造入れ替え $arrOld = array(); foreach ($arrCategoryCountOld as $item) { $arrOld[$item['category_id']] = $item['product_count']; } //新しいデータの構造入れ替え $arrNew = array(); foreach ($arrCategoryCountNew as $item) { $arrNew[$item['category_id']] = $item['product_count']; } unset($arrCategoryCountOld); unset($arrCategoryCountNew); $arrDiffCategory_id = array(); //新しいカテゴリ一覧から見て商品数が異なるデータが無いか確認 foreach ($arrNew as $cid => $count) { if ($arrOld[$cid] != $count) { $arrDiffCategory_id[] = $cid; } } //削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。 foreach ($arrOld as $cid => $count) { if ($arrNew[$cid] != $count && $count > 0) { $arrDiffCategory_id[] = $cid; } } //対象IDが無ければ終了 if (count($arrDiffCategory_id) == 0) { if ($is_out_trans) { $objQuery->commit(); } return; } //差分対象カテゴリIDの重複を除去 $arrDiffCategory_id = array_unique($arrDiffCategory_id); //dtb_category_countの更新 差分のあったカテゴリだけ更新する。 foreach ($arrDiffCategory_id as $cid) { $sqlval = array(); $sqlval['create_date'] = 'CURRENT_TIMESTAMP'; $sqlval['product_count'] = (string) $arrNew[$cid]; if ($sqlval['product_count'] == '') { $sqlval['product_count'] = (string) '0'; } if (isset($arrOld[$cid])) { $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid)); } else { if ($is_force_all_count) { $ret = $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid)); if ($ret > 0) { continue; } } $sqlval['category_id'] = $cid; $objQuery->insert('dtb_category_count', $sqlval); } } unset($arrOld); unset($arrNew); //差分があったIDとその親カテゴリIDのリストを取得する $arrTgtCategory_id = array(); foreach ($arrDiffCategory_id as $parent_category_id) { $arrTgtCategory_id[] = $parent_category_id; $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id); $arrTgtCategory_id = array_unique(array_merge($arrTgtCategory_id, $arrParentID)); } unset($arrDiffCategory_id); //dtb_category_total_count 集計処理開始 //更新対象カテゴリIDだけ集計しなおす。 $arrUpdateData = array(); $where_products_class = ''; if (NOSTOCK_HIDDEN) { $where_products_class .= '(stock >= 1 OR stock_unlimited = 1)'; } $from = $objProduct->alldtlSQL($where_products_class); foreach ($arrTgtCategory_id as $category_id) { $arrWhereVal = array(); list($tmp_where, $arrTmpVal) = $this->sfGetCatWhere($category_id); if ($tmp_where != '') { $sql_where_product_ids = 'product_id IN (SELECT product_id FROM dtb_product_categories WHERE ' . $tmp_where . ')'; $arrWhereVal = $arrTmpVal; } else { $sql_where_product_ids = '0<>0'; // 一致させない } $where = "({$sql_where}) AND ({$sql_where_product_ids})"; $arrUpdateData[$category_id] = $objQuery->count($from, $where, $arrWhereVal); } unset($arrTgtCategory_id); // 更新対象だけを更新。 foreach ($arrUpdateData as $cid => $count) { $sqlval = array(); $sqlval['create_date'] = 'CURRENT_TIMESTAMP'; $sqlval['product_count'] = $count; if ($sqlval['product_count'] == '') { $sqlval['product_count'] = (string) '0'; } $ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', array($cid)); if (!$ret) { $sqlval['category_id'] = $cid; $objQuery->insert('dtb_category_total_count', $sqlval); } } // トランザクション終了処理 if ($is_out_trans) { $objQuery->commit(); } }