source: branches/dev/html/admin/products/category.php @ 15256

Revision 15256, 10.0 KB checked in by kishida, 17 years ago (diff)
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7require_once("../require.php");
8
9class LC_Page {
10    function LC_Page() {
11        $this->tpl_mainpage = 'products/category.tpl';
12        $this->tpl_subnavi = 'products/subnavi.tpl';
13        $this->tpl_mainno = 'products';     
14        $this->tpl_subno = 'category';
15        $this->tpl_onload = " fnSetFocus('category_name'); ";
16        $this->tpl_subtitle = '¥«¥Æ¥´¥ê¡¼ÅÐÏ¿';
17    }
18}
19
20$conn = new SC_DBConn();
21$objPage = new LC_Page();
22$objView = new SC_AdminView();
23$objSess = new SC_Session();
24
25// ǧ¾Ú²ÄÈݤÎȽÄê
26sfIsSuccess($objSess);
27
28// ¥Ñ¥é¥á¡¼¥¿´ÉÍý¥¯¥é¥¹
29$objFormParam = new SC_FormParam();
30// ¥Ñ¥é¥á¡¼¥¿¾ðÊó¤Î½é´ü²½
31lfInitParam();
32// POSTÃͤμèÆÀ
33$objFormParam->setParam($_POST);
34
35// Ä̾ï»þ¤Ï¿Æ¥«¥Æ¥´¥ê¤ò0¤ËÀßÄꤹ¤ë¡£
36$objPage->arrForm['parent_category_id'] = $_POST['parent_category_id'];
37
38sfprintr($_POST);
39
40switch($_POST['mode']) {
41case 'edit':
42    $objFormParam->convParam();
43    $arrRet =  $objFormParam->getHashArray();
44    $objPage->arrErr = lfCheckError($arrRet);
45   
46    if(count($objPage->arrErr) == 0) {
47        if($_POST['category_id'] == "") {
48            $objQuery = new SC_Query();
49            $count = $objQuery->count("dtb_category");
50            if($count < CATEGORY_MAX) {         
51                lfInsertCat($_POST['parent_category_id']);
52            } else {
53                print("¥«¥Æ¥´¥ê¤ÎÅÐÏ¿ºÇÂç¿ô¤òĶ¤¨¤Þ¤·¤¿¡£");
54            }
55        } else {
56            lfUpdateCat($_POST['category_id']);
57        }
58    } else {
59        $objPage->arrForm = array_merge($objPage->arrForm, $objFormParam->getHashArray());
60        $objPage->arrForm['category_id'] = $_POST['category_id'];
61    }
62    break;
63case 'pre_edit':
64    // ÊÔ½¸¹àÌܤΥ«¥Æ¥´¥ê̾¤òDB¤è¤ê¼èÆÀ¤¹¤ë¡£
65    $oquery = new SC_Query();
66    $where = "category_id = ?";
67    $cat_name = $oquery->get("dtb_category", "category_name", $where, array($_POST['category_id']));
68    // ÆþÎϹàÌܤ˥«¥Æ¥´¥ê̾¤òÆþÎϤ¹¤ë¡£
69    $objPage->arrForm['category_name'] = $cat_name;
70    // POST¥Ç¡¼¥¿¤ò°ú¤­·Ñ¤°
71    $objPage->arrForm['category_id'] = $_POST['category_id'];
72    break;
73case 'delete':
74    $objQuery = new SC_Query();
75    // »Ò¥«¥Æ¥´¥ê¤Î¥Á¥§¥Ã¥¯
76    $where = "parent_category_id = ? AND del_flg = 0";
77    $count = $objQuery->count("dtb_category", $where, array($_POST['category_id']));
78    if($count != 0) {
79        $objPage->arrErr['category_name'] = "¢¨ »Ò¥«¥Æ¥´¥ê¤¬Â¸ºß¤¹¤ë¤¿¤áºï½ü¤Ç¤­¤Þ¤»¤ó¡£<br>";
80    }
81    // ÅÐÏ¿¾¦ÉʤΥÁ¥§¥Ã¥¯
82    $where = "category_id = ? AND del_flg = 0";
83    $count = $objQuery->count("dtb_products", $where, array($_POST['category_id']));
84    if($count != 0) {
85        $objPage->arrErr['category_name'] = "¢¨ ¥«¥Æ¥´¥êÆâ¤Ë¾¦Éʤ¬Â¸ºß¤¹¤ë¤¿¤áºï½ü¤Ç¤­¤Þ¤»¤ó¡£<br>";
86    }   
87   
88    if(!isset($objPage->arrErr['category_name'])) {
89        // ¥é¥ó¥¯ÉÕ¤­¥ì¥³¡¼¥É¤Îºï½ü(¢¨½èÍýÉé²Ù¤ò¹Íθ¤·¤Æ¥ì¥³¡¼¥É¤´¤Èºï½ü¤¹¤ë¡£)
90        sfDeleteRankRecord("dtb_category", "category_id", $_POST['category_id'], "", true);
91    }
92    break;
93case 'up':
94    $objQuery = new SC_Query();
95    $objQuery->begin();
96    $up_id = lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
97    if($up_id != "") {
98        // ¾å¤Î¥°¥ë¡¼¥×¤Îrank¤«¤é¸º»»¤¹¤ë¿ô
99        $my_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
100        // ¼«Ê¬¤Î¥°¥ë¡¼¥×¤Îrank¤Ë²Ã»»¤¹¤ë¿ô
101        $up_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id);
102        if($my_count > 0 && $up_count > 0) {
103            // ¼«Ê¬¤Î¥°¥ë¡¼¥×¤Ë²Ã»»
104            lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $up_count);
105            // ¾å¤Î¥°¥ë¡¼¥×¤«¤é¸º»»
106            lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count);
107        }
108    }
109    $objQuery->commit();
110    break;
111case 'down':
112    $objQuery = new SC_Query();
113    $objQuery->begin();
114    $down_id = lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
115    if($down_id != "") {
116        // ²¼¤Î¥°¥ë¡¼¥×¤Îrank¤Ë²Ã»»¤¹¤ë¿ô
117        $my_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
118        // ¼«Ê¬¤Î¥°¥ë¡¼¥×¤Îrank¤«¤é¸º»»¤¹¤ë¿ô
119        $down_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
120        if($my_count > 0 && $down_count > 0) {
121            // ¼«Ê¬¤Î¥°¥ë¡¼¥×¤«¤é¸º»»
122            lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
123            // ²¼¤Î¥°¥ë¡¼¥×¤Ë²Ã»»
124            lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $down_count);
125        }
126    }
127    $objQuery->commit();
128    break;
129case 'tree':
130    break;
131default:
132    $objPage->arrForm['parent_category_id'] = 0;
133    break;
134}
135
136$objPage->arrList = lfGetCat($objPage->arrForm['parent_category_id']);
137$objPage->arrTree = sfGetCatTree($objPage->arrForm['parent_category_id']);
138
139$objView->assignobj($objPage);
140$objView->display(MAIN_FRAME);
141//-----------------------------------------------------------------------------------------------------------------------------------
142
143// ¥«¥Æ¥´¥ê¤Î¿·µ¬ÄɲÃ
144function lfInsertCat($parent_category_id) {
145    global $objFormParam;
146   
147    $objQuery = new SC_Query();
148    $objQuery->begin(); // ¥È¥é¥ó¥¶¥¯¥·¥ç¥ó¤Î³«»Ï
149   
150   
151    if($parent_category_id == 0) {
152        // ROOT³¬ÁؤǺÇÂç¤Î¥é¥ó¥¯¤ò¼èÆÀ¤¹¤ë¡£       
153        $where = "parent_category_id = ?";
154        $rank = $objQuery->max("dtb_category", "rank", $where, array($parent_category_id)) + 1;
155    } else {
156        // ¿Æ¤Î¥é¥ó¥¯¤ò¼«Ê¬¤Î¥é¥ó¥¯¤È¤¹¤ë¡£
157        $where = "category_id = ?";
158        $rank = $objQuery->get("dtb_category", "rank", $where, array($parent_category_id));
159        // Äɲå쥳¡¼¥É¤Î¥é¥ó¥¯°Ê¾å¤Î¥ì¥³¡¼¥É¤ò°ì¤Ä¤¢¤²¤ë¡£
160        $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
161        $objQuery->exec($sqlup, array($rank));
162    }
163   
164    $where = "category_id = ?";
165    // ¼«Ê¬¤Î¥ì¥Ù¥ë¤ò¼èÆÀ¤¹¤ë(¿Æ¤Î¥ì¥Ù¥ë + 1)   
166    $level = $objQuery->get("dtb_category", "level", $where, array($parent_category_id)) + 1;
167   
168    // ÆþÎϥǡ¼¥¿¤òÅϤ¹¡£
169    $sqlval = $objFormParam->getHashArray();
170    $sqlval['create_date'] = "Now()";
171    $sqlval['update_date'] = "Now()";
172    $sqlval['creator_id'] = $_SESSION['member_id'];
173    $sqlval['parent_category_id'] = $parent_category_id;
174    $sqlval['rank'] = $rank;
175    $sqlval['level'] = $level;
176   
177    // INSERT¤Î¼Â¹Ô
178    $objQuery->insert("dtb_category", $sqlval);
179   
180    $objQuery->commit();    // ¥È¥é¥ó¥¶¥¯¥·¥ç¥ó¤Î½ªÎ»
181}
182
183// ¥«¥Æ¥´¥ê¤ÎÊÔ½¸
184function lfUpdateCat($category_id) {
185    global $objFormParam;
186    $objQuery = new SC_Query();
187    // ÆþÎϥǡ¼¥¿¤òÅϤ¹¡£
188    $sqlval = $objFormParam->getHashArray();
189    $sqlval['update_date'] = "Now()";
190    $where = "category_id = ?";
191    $objQuery->update("dtb_category", $sqlval, $where, array($category_id));
192}
193
194// ¥«¥Æ¥´¥ê¤Î¼èÆÀ
195function lfGetCat($parent_category_id) {
196    $objQuery = new SC_Query();
197   
198    if($parent_category_id == "") {
199        $parent_category_id = '0';
200    }
201   
202    $col = "category_id, category_name, level, rank";
203    $where = "del_flg = 0 AND parent_category_id = ?";
204    $objQuery->setoption("ORDER BY rank DESC");
205    $arrRet = $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
206    return $arrRet;
207}
208
209/* ¥Ñ¥é¥á¡¼¥¿¾ðÊó¤Î½é´ü²½ */
210function lfInitParam() {
211    global $objFormParam;
212    $objFormParam->addParam("¥«¥Æ¥´¥ê̾", "category_name", STEXT_LEN, "KVa", array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
213}
214
215/* ÆþÎÏÆâÍƤΥÁ¥§¥Ã¥¯ */
216function lfCheckError($array) {
217    global $objFormParam;
218    $objErr = new SC_CheckError($array);
219    $objErr->arrErr = $objFormParam->checkError();
220   
221    // ³¬ÁØ¥Á¥§¥Ã¥¯
222    if(!isset($objErr->arrErr['category_name'])) {
223        $objQuery = new SC_Query();
224        $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($_POST['parent_category_id']));
225       
226        if($level >= LEVEL_MAX) {
227            $objErr->arrErr['category_name'] = "¢¨ ".LEVEL_MAX."³¬Áذʾå¤ÎÅÐÏ¿¤Ï¤Ç¤­¤Þ¤»¤ó¡£<br>";
228        }
229    }
230       
231    // ½ÅÊ£¥Á¥§¥Ã¥¯
232    if(!isset($objErr->arrErr['category_name'])) {
233        $objQuery = new SC_Query();
234        $where = "parent_category_id = ? AND category_name = ?";
235        $arrRet = $objQuery->select("category_id, category_name", "dtb_category", $where, array($_POST['parent_category_id'], $array['category_name']));
236        // ÊÔ½¸Ãæ¤Î¥ì¥³¡¼¥É°Ê³°¤ËƱ¤¸Ì¾¾Î¤¬Â¸ºß¤¹¤ë¾ì¹ç
237        if ($arrRet[0]['category_id'] != $_POST['category_id'] && $arrRet[0]['category_name'] == $_POST['category_name']) {
238            $objErr->arrErr['category_name'] = "¢¨ ´û¤ËƱ¤¸ÆâÍƤÎÅÐÏ¿¤¬Â¸ºß¤·¤Þ¤¹¡£<br>";
239        }
240    }
241
242    return $objErr->arrErr;
243}
244
245
246// ʤӤ¬1¤Ä²¼¤ÎID¤ò¼èÆÀ¤¹¤ë¡£
247function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
248    // ¿ÆID¤ò¼èÆÀ¤¹¤ë¡£
249    $col = "$pid_name";
250    $where = "$id_name = ?";
251    $pid = $objQuery->get($table, $col, $where, $id);
252    // ¤¹¤Ù¤Æ¤Î»Ò¤ò¼èÆÀ¤¹¤ë¡£
253    $col = "$id_name";
254    $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
255    $arrRet = $objQuery->select($col, $table, $where, array($pid));
256    $max = count($arrRet);
257    $down_id = "";
258    for($cnt = 0; $cnt < $max; $cnt++) {
259        if($arrRet[$cnt][$id_name] == $id) {
260            $down_id = $arrRet[($cnt + 1)][$id_name];
261            break;
262        }
263    }
264    return $down_id;
265}
266
267// ʤӤ¬1¤Ä¾å¤ÎID¤ò¼èÆÀ¤¹¤ë¡£
268function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
269    // ¿ÆID¤ò¼èÆÀ¤¹¤ë¡£
270    $col = "$pid_name";
271    $where = "$id_name = ?";
272    $pid = $objQuery->get($table, $col, $where, $id);
273    // ¤¹¤Ù¤Æ¤Î»Ò¤ò¼èÆÀ¤¹¤ë¡£
274    $col = "$id_name";
275    $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
276    $arrRet = $objQuery->select($col, $table, $where, array($pid));
277    $max = count($arrRet);
278    $up_id = "";
279    for($cnt = 0; $cnt < $max; $cnt++) {
280        if($arrRet[$cnt][$id_name] == $id) {
281            $up_id = $arrRet[($cnt - 1)][$id_name];
282            break;
283        }
284    }
285    return $up_id;
286}
287
288function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
289    // »ÒID°ìÍ÷¤ò¼èÆÀ
290    $arrRet = sfGetChildrenArray($table, $pid_name, $id_name, $id);
291    return count($arrRet);
292}
293
294function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
295    // »ÒID°ìÍ÷¤ò¼èÆÀ
296    $arrRet = sfGetChildrenArray($table, $pid_name, $id_name, $id);
297    $line = sfGetCommaList($arrRet);
298    $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
299    $sql.= "AND del_flg = 0";
300    $ret = $objQuery->exec($sql);
301    return $ret;
302}
303
304function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
305    // »ÒID°ìÍ÷¤ò¼èÆÀ
306    $arrRet = sfGetChildrenArray($table, $pid_name, $id_name, $id);
307    $line = sfGetCommaList($arrRet);
308    $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
309    $sql.= "AND del_flg = 0";
310    $ret = $objQuery->exec($sql);
311    return $ret;
312}
313?>
Note: See TracBrowser for help on using the repository browser.