1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // {{{ requires |
---|
25 | require_once(CLASS_PATH . "pages/LC_Page.php"); |
---|
26 | |
---|
27 | /** |
---|
28 | * カテゴリ管理 のページクラス. |
---|
29 | * |
---|
30 | * @package Page |
---|
31 | * @author LOCKON CO.,LTD. |
---|
32 | * @version $Id$ |
---|
33 | */ |
---|
34 | class LC_Page_Admin_Products_Category extends LC_Page { |
---|
35 | |
---|
36 | // {{{ properties |
---|
37 | |
---|
38 | /** フォームパラメータ */ |
---|
39 | var $objFormParam; |
---|
40 | |
---|
41 | // }}} |
---|
42 | // {{{ functions |
---|
43 | |
---|
44 | /** |
---|
45 | * Page を初期化する. |
---|
46 | * |
---|
47 | * @return void |
---|
48 | */ |
---|
49 | function init() { |
---|
50 | parent::init(); |
---|
51 | $this->tpl_subtitle = 'カテゴリー登録'; |
---|
52 | $this->tpl_mainpage = 'products/category.tpl'; |
---|
53 | $this->tpl_subnavi = 'products/subnavi.tpl'; |
---|
54 | $this->tpl_mainno = 'products'; |
---|
55 | $this->tpl_subno = 'category'; |
---|
56 | $this->tpl_onload = " fnSetFocus('category_name'); "; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Page のプロセス. |
---|
61 | * |
---|
62 | * @return void |
---|
63 | */ |
---|
64 | function process() { |
---|
65 | $conn = new SC_DBConn(); |
---|
66 | $objView = new SC_AdminView(); |
---|
67 | $objSess = new SC_Session(); |
---|
68 | $objDb = new SC_Helper_DB_Ex(); |
---|
69 | |
---|
70 | // 認証可否の判定 |
---|
71 | SC_Utils_Ex::sfIsSuccess($objSess); |
---|
72 | |
---|
73 | // パラメータ管理クラス |
---|
74 | $this->objFormParam = new SC_FormParam(); |
---|
75 | // パラメータ情報の初期化 |
---|
76 | $this->lfInitParam(); |
---|
77 | // POST値の取得 |
---|
78 | $this->objFormParam->setParam($_POST); |
---|
79 | |
---|
80 | // 通常時は親カテゴリを0に設定する。 |
---|
81 | $this->arrForm['parent_category_id'] = |
---|
82 | isset($_POST['parent_category_id']) ? $_POST['parent_category_id'] : ""; |
---|
83 | |
---|
84 | if (!isset($_POST['mode'])) $_POST['mode'] = ""; |
---|
85 | |
---|
86 | switch($_POST['mode']) { |
---|
87 | case 'edit': |
---|
88 | $this->objFormParam->convParam(); |
---|
89 | $arrRet = $this->objFormParam->getHashArray(); |
---|
90 | $this->arrErr = $this->lfCheckError($arrRet); |
---|
91 | |
---|
92 | if(count($this->arrErr) == 0) { |
---|
93 | if($_POST['category_id'] == "") { |
---|
94 | $objQuery = new SC_Query(); |
---|
95 | $count = $objQuery->count("dtb_category"); |
---|
96 | if($count < CATEGORY_MAX) { |
---|
97 | $this->lfInsertCat($_POST['parent_category_id']); |
---|
98 | } else { |
---|
99 | print("カテゴリの登録最大数を超えました。"); |
---|
100 | } |
---|
101 | } else { |
---|
102 | $this->lfUpdateCat($_POST['category_id']); |
---|
103 | } |
---|
104 | } else { |
---|
105 | $this->arrForm = array_merge($this->arrForm, $this->objFormParam->getHashArray()); |
---|
106 | $this->arrForm['category_id'] = $_POST['category_id']; |
---|
107 | } |
---|
108 | break; |
---|
109 | case 'pre_edit': |
---|
110 | // 編集項目のカテゴリ名をDBより取得する。 |
---|
111 | $oquery = new SC_Query(); |
---|
112 | $where = "category_id = ?"; |
---|
113 | $cat_name = $oquery->get("dtb_category", "category_name", $where, array($_POST['category_id'])); |
---|
114 | // 入力項目にカテゴリ名を入力する。 |
---|
115 | $this->arrForm['category_name'] = $cat_name; |
---|
116 | // POSTデータを引き継ぐ |
---|
117 | $this->arrForm['category_id'] = $_POST['category_id']; |
---|
118 | break; |
---|
119 | case 'delete': |
---|
120 | $objQuery = new SC_Query(); |
---|
121 | // 子カテゴリのチェック |
---|
122 | $where = "parent_category_id = ? AND del_flg = 0"; |
---|
123 | $count = $objQuery->count("dtb_category", $where, array($_POST['category_id'])); |
---|
124 | if($count != 0) { |
---|
125 | $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br>"; |
---|
126 | } |
---|
127 | // 登録商品のチェック |
---|
128 | $table = "dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id"; |
---|
129 | $where = "T1.category_id = ? AND T2.del_flg = 0"; |
---|
130 | $count = $objQuery->count($table, $where, array($_POST['category_id'])); |
---|
131 | if($count != 0) { |
---|
132 | $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br>"; |
---|
133 | } |
---|
134 | |
---|
135 | if(!isset($this->arrErr['category_name'])) { |
---|
136 | // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。) |
---|
137 | $objDb->sfDeleteRankRecord("dtb_category", "category_id", $_POST['category_id'], "", true); |
---|
138 | } |
---|
139 | break; |
---|
140 | case 'up': |
---|
141 | $objQuery = new SC_Query(); |
---|
142 | $objQuery->begin(); |
---|
143 | $up_id = $this->lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
144 | if($up_id != "") { |
---|
145 | // 上のグループのrankから減算する数 |
---|
146 | $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
147 | // 自分のグループのrankに加算する数 |
---|
148 | $up_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id); |
---|
149 | if($my_count > 0 && $up_count > 0) { |
---|
150 | // 自分のグループに加算 |
---|
151 | $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $up_count); |
---|
152 | // 上のグループから減算 |
---|
153 | $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count); |
---|
154 | } |
---|
155 | } |
---|
156 | $objQuery->commit(); |
---|
157 | break; |
---|
158 | case 'down': |
---|
159 | $objQuery = new SC_Query(); |
---|
160 | $objQuery->begin(); |
---|
161 | $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
162 | if($down_id != "") { |
---|
163 | // 下のグループのrankに加算する数 |
---|
164 | $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
165 | // 自分のグループのrankから減算する数 |
---|
166 | $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id); |
---|
167 | if($my_count > 0 && $down_count > 0) { |
---|
168 | // 自分のグループから減算 |
---|
169 | $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count); |
---|
170 | // 下のグループに加算 |
---|
171 | $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $down_count); |
---|
172 | } |
---|
173 | } |
---|
174 | $objQuery->commit(); |
---|
175 | break; |
---|
176 | case 'tree': |
---|
177 | break; |
---|
178 | case 'csv': |
---|
179 | require_once(CLASS_EX_PATH . "helper_extends/SC_Helper_CSV_Ex.php"); |
---|
180 | |
---|
181 | $objCSV = new SC_Helper_CSV_Ex(); |
---|
182 | |
---|
183 | // CSVを送信する。正常終了の場合、終了。 |
---|
184 | $objCSV->sfDownloadCategoryCsv() && exit; |
---|
185 | |
---|
186 | break; |
---|
187 | default: |
---|
188 | $this->arrForm['parent_category_id'] = 0; |
---|
189 | break; |
---|
190 | } |
---|
191 | |
---|
192 | $this->arrList = $this->lfGetCat($this->arrForm['parent_category_id']); |
---|
193 | $this->arrTree = $objDb->sfGetCatTree($this->arrForm['parent_category_id']); |
---|
194 | |
---|
195 | $objView->assignobj($this); |
---|
196 | $objView->display(MAIN_FRAME); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * デストラクタ. |
---|
201 | * |
---|
202 | * @return void |
---|
203 | */ |
---|
204 | function destroy() { |
---|
205 | parent::destroy(); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | // カテゴリの新規追加 |
---|
211 | function lfInsertCat($parent_category_id) { |
---|
212 | |
---|
213 | $objQuery = new SC_Query(); |
---|
214 | $objQuery->begin(); // トランザクションの開始 |
---|
215 | |
---|
216 | |
---|
217 | if($parent_category_id == 0) { |
---|
218 | // ROOT階層で最大のランクを取得する。 |
---|
219 | $where = "parent_category_id = ?"; |
---|
220 | $rank = $objQuery->max("dtb_category", "rank", $where, array($parent_category_id)) + 1; |
---|
221 | } else { |
---|
222 | // 親のランクを自分のランクとする。 |
---|
223 | $where = "category_id = ?"; |
---|
224 | $rank = $objQuery->get("dtb_category", "rank", $where, array($parent_category_id)); |
---|
225 | // 追加レコードのランク以上のレコードを一つあげる。 |
---|
226 | $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?"; |
---|
227 | $objQuery->exec($sqlup, array($rank)); |
---|
228 | } |
---|
229 | |
---|
230 | $where = "category_id = ?"; |
---|
231 | // 自分のレベルを取得する(親のレベル + 1) |
---|
232 | $level = $objQuery->get("dtb_category", "level", $where, array($parent_category_id)) + 1; |
---|
233 | |
---|
234 | // 入力データを渡す。 |
---|
235 | $sqlval = $this->objFormParam->getHashArray(); |
---|
236 | $sqlval['create_date'] = "Now()"; |
---|
237 | $sqlval['update_date'] = "Now()"; |
---|
238 | $sqlval['creator_id'] = $_SESSION['member_id']; |
---|
239 | $sqlval['parent_category_id'] = $parent_category_id; |
---|
240 | $sqlval['rank'] = $rank; |
---|
241 | $sqlval['level'] = $level; |
---|
242 | |
---|
243 | // INSERTの実行 |
---|
244 | $objQuery->insert("dtb_category", $sqlval); |
---|
245 | |
---|
246 | $objQuery->commit(); // トランザクションの終了 |
---|
247 | } |
---|
248 | |
---|
249 | // カテゴリの編集 |
---|
250 | function lfUpdateCat($category_id) { |
---|
251 | $objQuery = new SC_Query(); |
---|
252 | // 入力データを渡す。 |
---|
253 | $sqlval = $this->objFormParam->getHashArray(); |
---|
254 | $sqlval['update_date'] = "Now()"; |
---|
255 | $where = "category_id = ?"; |
---|
256 | $objQuery->update("dtb_category", $sqlval, $where, array($category_id)); |
---|
257 | } |
---|
258 | |
---|
259 | // カテゴリの取得 |
---|
260 | function lfGetCat($parent_category_id) { |
---|
261 | $objQuery = new SC_Query(); |
---|
262 | |
---|
263 | if($parent_category_id == "") { |
---|
264 | $parent_category_id = '0'; |
---|
265 | } |
---|
266 | |
---|
267 | $col = "category_id, category_name, level, rank"; |
---|
268 | $where = "del_flg = 0 AND parent_category_id = ?"; |
---|
269 | $objQuery->setoption("ORDER BY rank DESC"); |
---|
270 | $arrRet = $objQuery->select($col, "dtb_category", $where, array($parent_category_id)); |
---|
271 | return $arrRet; |
---|
272 | } |
---|
273 | |
---|
274 | /* パラメータ情報の初期化 */ |
---|
275 | function lfInitParam() { |
---|
276 | $this->objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, "KVa", array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK")); |
---|
277 | } |
---|
278 | |
---|
279 | /* 入力内容のチェック */ |
---|
280 | function lfCheckError($array) { |
---|
281 | |
---|
282 | $objErr = new SC_CheckError($array); |
---|
283 | $objErr->arrErr = $this->objFormParam->checkError(); |
---|
284 | |
---|
285 | // 階層チェック |
---|
286 | if(!isset($objErr->arrErr['category_name'])) { |
---|
287 | $objQuery = new SC_Query(); |
---|
288 | $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($_POST['parent_category_id'])); |
---|
289 | |
---|
290 | if($level >= LEVEL_MAX) { |
---|
291 | $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>"; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | if (!isset($_POST['category_id'])) $_POST['category_id'] = ""; |
---|
296 | |
---|
297 | // |
---|
298 | |
---|
299 | // 重複チェック |
---|
300 | if(!isset($objErr->arrErr['category_name'])) { |
---|
301 | $objQuery = new SC_Query(); |
---|
302 | $where = "parent_category_id = ? AND category_name = ?"; |
---|
303 | $arrRet = $objQuery->select("category_id, category_name", "dtb_category", $where, array($_POST['parent_category_id'], $array['category_name'])); |
---|
304 | |
---|
305 | if (empty($arrRet)) { |
---|
306 | $arrRet = array(array("category_id" => "", "category_name" => "")); |
---|
307 | } |
---|
308 | |
---|
309 | // 編集中のレコード以外に同じ名称が存在する場合 |
---|
310 | if ($arrRet[0]['category_id'] != $_POST['category_id'] |
---|
311 | && $arrRet[0]['category_name'] == $_POST['category_name']) { |
---|
312 | $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。<br>"; |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | return $objErr->arrErr; |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | // 並びが1つ下のIDを取得する。 |
---|
321 | function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) { |
---|
322 | // 親IDを取得する。 |
---|
323 | $col = "$pid_name"; |
---|
324 | $where = "$id_name = ?"; |
---|
325 | $pid = $objQuery->get($table, $col, $where, $id); |
---|
326 | // すべての子を取得する。 |
---|
327 | $col = "$id_name"; |
---|
328 | $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC"; |
---|
329 | $arrRet = $objQuery->select($col, $table, $where, array($pid)); |
---|
330 | $max = count($arrRet); |
---|
331 | $down_id = ""; |
---|
332 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
333 | if($arrRet[$cnt][$id_name] == $id) { |
---|
334 | $down_id = $arrRet[($cnt + 1)][$id_name]; |
---|
335 | break; |
---|
336 | } |
---|
337 | } |
---|
338 | return $down_id; |
---|
339 | } |
---|
340 | |
---|
341 | // 並びが1つ上のIDを取得する。 |
---|
342 | function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) { |
---|
343 | // 親IDを取得する。 |
---|
344 | $col = "$pid_name"; |
---|
345 | $where = "$id_name = ?"; |
---|
346 | $pid = $objQuery->get($table, $col, $where, $id); |
---|
347 | // すべての子を取得する。 |
---|
348 | $col = "$id_name"; |
---|
349 | $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC"; |
---|
350 | $arrRet = $objQuery->select($col, $table, $where, array($pid)); |
---|
351 | $max = count($arrRet); |
---|
352 | $up_id = ""; |
---|
353 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
354 | if($arrRet[$cnt][$id_name] == $id) { |
---|
355 | $up_id = $arrRet[($cnt - 1)][$id_name]; |
---|
356 | break; |
---|
357 | } |
---|
358 | } |
---|
359 | return $up_id; |
---|
360 | } |
---|
361 | |
---|
362 | function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) { |
---|
363 | $objDb = new SC_Helper_DB_Ex(); |
---|
364 | // 子ID一覧を取得 |
---|
365 | $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
366 | return count($arrRet); |
---|
367 | } |
---|
368 | |
---|
369 | function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) { |
---|
370 | $objDb = new SC_Helper_DB_Ex(); |
---|
371 | // 子ID一覧を取得 |
---|
372 | $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
373 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
374 | $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) "; |
---|
375 | $sql.= "AND del_flg = 0"; |
---|
376 | $ret = $objQuery->exec($sql); |
---|
377 | return $ret; |
---|
378 | } |
---|
379 | |
---|
380 | function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) { |
---|
381 | $objDb = new SC_Helper_DB_Ex(); |
---|
382 | // 子ID一覧を取得 |
---|
383 | $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
384 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
385 | $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) "; |
---|
386 | $sql.= "AND del_flg = 0"; |
---|
387 | $ret = $objQuery->exec($sql); |
---|
388 | return $ret; |
---|
389 | } |
---|
390 | } |
---|
391 | ?> |
---|