source: branches/rel/html/admin/products/category.php @ 12157

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