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/admin/LC_Page_Admin.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_Admin { |
---|
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 | $this->action(); |
---|
66 | $this->sendResponse(); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Page のアクション. |
---|
71 | * |
---|
72 | * @return void |
---|
73 | */ |
---|
74 | function action() { |
---|
75 | $objSess = new SC_Session(); |
---|
76 | $objDb = new SC_Helper_DB_Ex(); |
---|
77 | |
---|
78 | // 認証可否の判定 |
---|
79 | SC_Utils_Ex::sfIsSuccess($objSess); |
---|
80 | |
---|
81 | // パラメータ管理クラス |
---|
82 | $this->objFormParam = new SC_FormParam(); |
---|
83 | // パラメータ情報の初期化 |
---|
84 | $this->lfInitParam(); |
---|
85 | // POST値の取得 |
---|
86 | $this->objFormParam->setParam($_POST); |
---|
87 | |
---|
88 | // 通常時は親カテゴリを0に設定する。 |
---|
89 | $this->arrForm['parent_category_id'] = |
---|
90 | isset($_POST['parent_category_id']) ? $_POST['parent_category_id'] : ""; |
---|
91 | |
---|
92 | if (!isset($_POST['mode'])) $_POST['mode'] = ""; |
---|
93 | |
---|
94 | switch($_POST['mode']) { |
---|
95 | case 'edit': |
---|
96 | $this->objFormParam->convParam(); |
---|
97 | $arrRet = $this->objFormParam->getHashArray(); |
---|
98 | $this->arrErr = $this->lfCheckError($arrRet); |
---|
99 | |
---|
100 | if(count($this->arrErr) == 0) { |
---|
101 | if($_POST['category_id'] == "") { |
---|
102 | $objQuery = new SC_Query(); |
---|
103 | $count = $objQuery->count("dtb_category"); |
---|
104 | if($count < CATEGORY_MAX) { |
---|
105 | $this->lfInsertCat($_POST['parent_category_id']); |
---|
106 | } else { |
---|
107 | print("カテゴリの登録最大数を超えました。"); |
---|
108 | } |
---|
109 | } else { |
---|
110 | $this->lfUpdateCat($_POST['category_id']); |
---|
111 | } |
---|
112 | } else { |
---|
113 | $this->arrForm = array_merge($this->arrForm, $this->objFormParam->getHashArray()); |
---|
114 | $this->arrForm['category_id'] = $_POST['category_id']; |
---|
115 | } |
---|
116 | break; |
---|
117 | case 'pre_edit': |
---|
118 | // 編集項目のカテゴリ名をDBより取得する。 |
---|
119 | $oquery = new SC_Query(); |
---|
120 | $where = "category_id = ?"; |
---|
121 | $cat_name = $oquery->get("dtb_category", "category_name", $where, array($_POST['category_id'])); |
---|
122 | // 入力項目にカテゴリ名を入力する。 |
---|
123 | $this->arrForm['category_name'] = $cat_name; |
---|
124 | // POSTデータを引き継ぐ |
---|
125 | $this->arrForm['category_id'] = $_POST['category_id']; |
---|
126 | break; |
---|
127 | case 'delete': |
---|
128 | $objQuery = new SC_Query(); |
---|
129 | // 子カテゴリのチェック |
---|
130 | $where = "parent_category_id = ? AND del_flg = 0"; |
---|
131 | $count = $objQuery->count("dtb_category", $where, array($_POST['category_id'])); |
---|
132 | if($count != 0) { |
---|
133 | $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br>"; |
---|
134 | } |
---|
135 | // 登録商品のチェック |
---|
136 | $table = "dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id"; |
---|
137 | $where = "T1.category_id = ? AND T2.del_flg = 0"; |
---|
138 | $count = $objQuery->count($table, $where, array($_POST['category_id'])); |
---|
139 | if($count != 0) { |
---|
140 | $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br>"; |
---|
141 | } |
---|
142 | |
---|
143 | if(!isset($this->arrErr['category_name'])) { |
---|
144 | // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。) |
---|
145 | $objDb->sfDeleteRankRecord("dtb_category", "category_id", $_POST['category_id'], "", true); |
---|
146 | } |
---|
147 | break; |
---|
148 | case 'up': |
---|
149 | $objQuery = new SC_Query(); |
---|
150 | $objQuery->begin(); |
---|
151 | $up_id = $this->lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
152 | if($up_id != "") { |
---|
153 | // 上のグループのrankから減算する数 |
---|
154 | $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
155 | // 自分のグループのrankに加算する数 |
---|
156 | $up_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id); |
---|
157 | if($my_count > 0 && $up_count > 0) { |
---|
158 | // 自分のグループに加算 |
---|
159 | $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $up_count); |
---|
160 | // 上のグループから減算 |
---|
161 | $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count); |
---|
162 | } |
---|
163 | } |
---|
164 | $objQuery->commit(); |
---|
165 | break; |
---|
166 | case 'down': |
---|
167 | $objQuery = new SC_Query(); |
---|
168 | $objQuery->begin(); |
---|
169 | $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
170 | if($down_id != "") { |
---|
171 | // 下のグループのrankに加算する数 |
---|
172 | $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']); |
---|
173 | // 自分のグループのrankから減算する数 |
---|
174 | $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id); |
---|
175 | if($my_count > 0 && $down_count > 0) { |
---|
176 | // 自分のグループから減算 |
---|
177 | $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count); |
---|
178 | // 下のグループに加算 |
---|
179 | $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $down_count); |
---|
180 | } |
---|
181 | } |
---|
182 | $objQuery->commit(); |
---|
183 | break; |
---|
184 | case 'moveByDnD': |
---|
185 | // DnDしたカテゴリと移動先のセットを分解する |
---|
186 | $keys = explode("-", $_POST['keySet']); |
---|
187 | if ($keys[0] && $keys[1]) { |
---|
188 | $objQuery = new SC_Query(); |
---|
189 | $objQuery->begin(); |
---|
190 | |
---|
191 | // 移動したデータのrank、level、parent_category_idを取得 |
---|
192 | $rank = $objQuery->get("dtb_category", "rank", "category_id = ?", array($keys[0])); |
---|
193 | $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($keys[0])); |
---|
194 | $parent = $objQuery->get("dtb_category", "parent_category_id", "category_id = ?", array($keys[0])); |
---|
195 | |
---|
196 | // 同一level内のrank配列を作成 |
---|
197 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
198 | if ($level == 1) { |
---|
199 | // 第1階層の時 |
---|
200 | $arrRet = $objQuery->select("rank", "dtb_category", "level = ?", array($level)); |
---|
201 | } else { |
---|
202 | // 第2階層以下の時 |
---|
203 | $arrRet = $objQuery->select("rank", "dtb_category", "level = ? AND parent_category_id = ?", array($level, $parent)); |
---|
204 | } |
---|
205 | for ($i = 0; $i < sizeof($arrRet); $i++) { |
---|
206 | $rankAry[$i + 1] = $arrRet[$i]['rank']; |
---|
207 | } |
---|
208 | |
---|
209 | // 移動したデータのグループ内データ数 |
---|
210 | $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0]); |
---|
211 | if ($rankAry[$keys[1]] > $rank) { |
---|
212 | // データが今の位置より上がった時 |
---|
213 | $up_count = $rankAry[$keys[1]] - $rank; |
---|
214 | $decAry = $objQuery->select("category_id", "dtb_category", "level = ? AND rank > ? AND rank <= ?", array($level, $rank, $rankAry[$keys[1]])); |
---|
215 | foreach($decAry as $value){ |
---|
216 | // 上のグループから減算 |
---|
217 | $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"], $my_count); |
---|
218 | } |
---|
219 | // 自分のグループに加算 |
---|
220 | $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0], $up_count); |
---|
221 | } else if($rankAry[$keys[1]] < $rank) { |
---|
222 | // データが今の位置より下がった時 |
---|
223 | $down_count = 0; |
---|
224 | $incAry = $objQuery->select("category_id", "dtb_category", "level = ? AND rank < ? AND rank >= ?", array($level, $rank, $rankAry[$keys[1]])); |
---|
225 | foreach ($incAry as $value) { |
---|
226 | // 下のグループに加算 |
---|
227 | $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"], $my_count); |
---|
228 | // 合計減算値 |
---|
229 | $down_count += $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"]); |
---|
230 | } |
---|
231 | // 自分のグループから減算 |
---|
232 | $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0], $down_count); |
---|
233 | } |
---|
234 | $objQuery->commit(); |
---|
235 | } |
---|
236 | break; |
---|
237 | case 'tree': |
---|
238 | break; |
---|
239 | case 'csv': |
---|
240 | require_once(CLASS_EX_PATH . "helper_extends/SC_Helper_CSV_Ex.php"); |
---|
241 | |
---|
242 | $objCSV = new SC_Helper_CSV_Ex(); |
---|
243 | |
---|
244 | // CSVを送信する。正常終了の場合、終了。 |
---|
245 | $objCSV->sfDownloadCategoryCsv() && exit; |
---|
246 | |
---|
247 | break; |
---|
248 | default: |
---|
249 | $this->arrForm['parent_category_id'] = 0; |
---|
250 | break; |
---|
251 | } |
---|
252 | |
---|
253 | $this->arrList = $this->lfGetCat($this->arrForm['parent_category_id']); |
---|
254 | $this->arrTree = $objDb->sfGetCatTree($this->arrForm['parent_category_id']); |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * デストラクタ. |
---|
259 | * |
---|
260 | * @return void |
---|
261 | */ |
---|
262 | function destroy() { |
---|
263 | parent::destroy(); |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | // カテゴリの新規追加 |
---|
269 | function lfInsertCat($parent_category_id) { |
---|
270 | |
---|
271 | $objQuery = new SC_Query(); |
---|
272 | $objQuery->begin(); // トランザクションの開始 |
---|
273 | |
---|
274 | |
---|
275 | if($parent_category_id == 0) { |
---|
276 | // ROOT階層で最大のランクを取得する。 |
---|
277 | $where = "parent_category_id = ?"; |
---|
278 | $rank = $objQuery->max("dtb_category", "rank", $where, array($parent_category_id)) + 1; |
---|
279 | } else { |
---|
280 | // 親のランクを自分のランクとする。 |
---|
281 | $where = "category_id = ?"; |
---|
282 | $rank = $objQuery->get("dtb_category", "rank", $where, array($parent_category_id)); |
---|
283 | // 追加レコードのランク以上のレコードを一つあげる。 |
---|
284 | $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?"; |
---|
285 | $objQuery->exec($sqlup, array($rank)); |
---|
286 | } |
---|
287 | |
---|
288 | $where = "category_id = ?"; |
---|
289 | // 自分のレベルを取得する(親のレベル + 1) |
---|
290 | $level = $objQuery->get("dtb_category", "level", $where, array($parent_category_id)) + 1; |
---|
291 | |
---|
292 | // 入力データを渡す。 |
---|
293 | $sqlval = $this->objFormParam->getHashArray(); |
---|
294 | $sqlval['create_date'] = "Now()"; |
---|
295 | $sqlval['update_date'] = "Now()"; |
---|
296 | $sqlval['creator_id'] = $_SESSION['member_id']; |
---|
297 | $sqlval['parent_category_id'] = $parent_category_id; |
---|
298 | $sqlval['rank'] = $rank; |
---|
299 | $sqlval['level'] = $level; |
---|
300 | |
---|
301 | // INSERTの実行 |
---|
302 | $sqlval['category_id'] = $objQuery->nextVal('dtb_category_category_id'); |
---|
303 | $objQuery->insert("dtb_category", $sqlval); |
---|
304 | |
---|
305 | $objQuery->commit(); // トランザクションの終了 |
---|
306 | } |
---|
307 | |
---|
308 | // カテゴリの編集 |
---|
309 | function lfUpdateCat($category_id) { |
---|
310 | $objQuery = new SC_Query(); |
---|
311 | // 入力データを渡す。 |
---|
312 | $sqlval = $this->objFormParam->getHashArray(); |
---|
313 | $sqlval['update_date'] = "Now()"; |
---|
314 | $where = "category_id = ?"; |
---|
315 | $objQuery->update("dtb_category", $sqlval, $where, array($category_id)); |
---|
316 | } |
---|
317 | |
---|
318 | // カテゴリの取得 |
---|
319 | function lfGetCat($parent_category_id) { |
---|
320 | $objQuery = new SC_Query(); |
---|
321 | |
---|
322 | if($parent_category_id == "") { |
---|
323 | $parent_category_id = '0'; |
---|
324 | } |
---|
325 | |
---|
326 | $col = "category_id, category_name, level, rank"; |
---|
327 | $where = "del_flg = 0 AND parent_category_id = ?"; |
---|
328 | $objQuery->setOption("ORDER BY rank DESC"); |
---|
329 | $arrRet = $objQuery->select($col, "dtb_category", $where, array($parent_category_id)); |
---|
330 | return $arrRet; |
---|
331 | } |
---|
332 | |
---|
333 | /* パラメータ情報の初期化 */ |
---|
334 | function lfInitParam() { |
---|
335 | $this->objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, "KVa", array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK")); |
---|
336 | } |
---|
337 | |
---|
338 | /* 入力内容のチェック */ |
---|
339 | function lfCheckError($array) { |
---|
340 | |
---|
341 | $objErr = new SC_CheckError($array); |
---|
342 | $objErr->arrErr = $this->objFormParam->checkError(); |
---|
343 | |
---|
344 | // 階層チェック |
---|
345 | if(!isset($objErr->arrErr['category_name'])) { |
---|
346 | $objQuery = new SC_Query(); |
---|
347 | $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($_POST['parent_category_id'])); |
---|
348 | |
---|
349 | if($level >= LEVEL_MAX) { |
---|
350 | $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>"; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | if (!isset($_POST['category_id'])) $_POST['category_id'] = ""; |
---|
355 | |
---|
356 | // |
---|
357 | |
---|
358 | // 重複チェック |
---|
359 | if(!isset($objErr->arrErr['category_name'])) { |
---|
360 | $objQuery = new SC_Query(); |
---|
361 | $where = "parent_category_id = ? AND category_name = ?"; |
---|
362 | $arrRet = $objQuery->select("category_id, category_name", "dtb_category", $where, array($_POST['parent_category_id'], $array['category_name'])); |
---|
363 | |
---|
364 | if (empty($arrRet)) { |
---|
365 | $arrRet = array(array("category_id" => "", "category_name" => "")); |
---|
366 | } |
---|
367 | |
---|
368 | // 編集中のレコード以外に同じ名称が存在する場合 |
---|
369 | if ($arrRet[0]['category_id'] != $_POST['category_id'] |
---|
370 | && $arrRet[0]['category_name'] == $_POST['category_name']) { |
---|
371 | $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。<br>"; |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | return $objErr->arrErr; |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | // 並びが1つ下のIDを取得する。 |
---|
380 | function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) { |
---|
381 | // 親IDを取得する。 |
---|
382 | $col = "$pid_name"; |
---|
383 | $where = "$id_name = ?"; |
---|
384 | $pid = $objQuery->get($table, $col, $where, $id); |
---|
385 | // すべての子を取得する。 |
---|
386 | $col = "$id_name"; |
---|
387 | $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC"; |
---|
388 | $arrRet = $objQuery->select($col, $table, $where, array($pid)); |
---|
389 | $max = count($arrRet); |
---|
390 | $down_id = ""; |
---|
391 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
392 | if($arrRet[$cnt][$id_name] == $id) { |
---|
393 | $down_id = $arrRet[($cnt + 1)][$id_name]; |
---|
394 | break; |
---|
395 | } |
---|
396 | } |
---|
397 | return $down_id; |
---|
398 | } |
---|
399 | |
---|
400 | // 並びが1つ上のIDを取得する。 |
---|
401 | function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) { |
---|
402 | // 親IDを取得する。 |
---|
403 | $col = "$pid_name"; |
---|
404 | $where = "$id_name = ?"; |
---|
405 | $pid = $objQuery->get($table, $col, $where, $id); |
---|
406 | // すべての子を取得する。 |
---|
407 | $col = "$id_name"; |
---|
408 | $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC"; |
---|
409 | $arrRet = $objQuery->select($col, $table, $where, array($pid)); |
---|
410 | $max = count($arrRet); |
---|
411 | $up_id = ""; |
---|
412 | for($cnt = 0; $cnt < $max; $cnt++) { |
---|
413 | if($arrRet[$cnt][$id_name] == $id) { |
---|
414 | $up_id = $arrRet[($cnt - 1)][$id_name]; |
---|
415 | break; |
---|
416 | } |
---|
417 | } |
---|
418 | return $up_id; |
---|
419 | } |
---|
420 | |
---|
421 | function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) { |
---|
422 | $objDb = new SC_Helper_DB_Ex(); |
---|
423 | // 子ID一覧を取得 |
---|
424 | $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
425 | return count($arrRet); |
---|
426 | } |
---|
427 | |
---|
428 | function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) { |
---|
429 | $objDb = new SC_Helper_DB_Ex(); |
---|
430 | // 子ID一覧を取得 |
---|
431 | $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
432 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
433 | $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) "; |
---|
434 | $sql.= "AND del_flg = 0"; |
---|
435 | $ret = $objQuery->exec($sql); |
---|
436 | return $ret; |
---|
437 | } |
---|
438 | |
---|
439 | function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) { |
---|
440 | $objDb = new SC_Helper_DB_Ex(); |
---|
441 | // 子ID一覧を取得 |
---|
442 | $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id); |
---|
443 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
444 | $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) "; |
---|
445 | $sql.= "AND del_flg = 0"; |
---|
446 | $ret = $objQuery->exec($sql); |
---|
447 | return $ret; |
---|
448 | } |
---|
449 | } |
---|
450 | ?> |
---|