/home/bjcompany/domains/benjabhorn.com/public_html/benjabhorn_group/control/categoryAdd.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php 
    $error
=0;             
    if(
$_POST['action']=='DelteCate'){
                
//-----------------Count sub cate  & product-------------------//
                
$query="SELECT SUM(id) SumProduct FROM tbl_home_data WHERE home_type_id = '".$_POST['EditID']."'  ";
                
$result=mysql_query($query);
                
$data=mysql_fetch_assoc($result);
                if(
$data['SumProduct'] > 0) {  $proError='กรุณาลบรายการสินค้าให้หมดก่อนจะลบหมวดสินค้านี้ '$error=1; }
                
                if(
$error==1){ ?>
                        <script language="javascript">
                                alert('<?php echo $cateError?> <?php echo $proError?>');
                                //window.location.href='main.php?work=addCategory1';
                                window.location.href='main.php?work=addCategory1&group=1';
                        </script>
                    <?php }else{
                         
$query="DELETE FROM tbl_product_cate WHERE id='".$_POST['EditID']."' ";
                         
mysql_query($query);
                         
//------------delete background-----------//
                         
if(file_exists($path_product.$_POS['backgroundImg'])){
                                     @
unlink($path_product.$_POS['backgroundImg']);
                             }
                         if(
file_exists($path_product."thb/".$_POS['backgroundImg'])){
                                     @
unlink($path_product."thb/".$_POS['backgroundImg']);
                             }                             
                         
//-----------sort new number
                            
$query="SELECT * FROM  tbl_product_cate WHERE  mainCateId='0'  ORDER BY number  ASC ";
                            
$result=mysql_query($query);
                            
$n=1;
                            while(
$cate=mysql_fetch_assoc($result))
                            {
                                
$query="UPDATE tbl_product_cate SET number='".$n."' WHERE id ='".$cate['id']."'  ";
                                
mysql_query($query);
                                
$n++;
                                }
                        }
                
        }
    
//-------------------------------------------------------------cate_img
       
if($_POST['action']=='AddPack'){
           
                
//-----------------------BACK GROUND IMG----------------------------------//

            
if($_FILES['uploadfile']['name']!=""){
                            
// $tempFileName = $_FILES['uploadfile']['name'];
                               
GetNewFileName($_FILES['uploadfile']['tmp_name'],$_FILES['uploadfile']['name'],$i);
                                
move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path_product.$_FILES['uploadfile']['name']);
             }
             
//-----------------------------------------------------------------------//
           
$query="SELECT MAX(number) AS maxNumber FROM tbl_product_cate ";
           
$result=mysql_query($query);
           
$max=mysql_fetch_assoc($result);
           
$numberAdd$max['maxNumber']+1;
           
$query="INSERT INTO  `tbl_product_cate` (`id` ,`cate_name` ,`mainCateId` ,`number` ,`category_group`, `cate_bg`) VALUES ('' ,  '".$_POST['cate_name']."'  , '0',  '".$numberAdd."' , '".$_GET['group']."'  , '".$_FILES['uploadfile']['name']."' ) ";
           
mysql_query($query);
           

           
        }
   
//-------------------------------------------------------------
         
if($_POST['action']=='EditCate'){
                   if(
$_FILES['newImg']['name'][$_POST['num']]!=""){
                            
// $tempFileName = $_FILES['uploadfile']['name'];
                               
GetNewFileName($_FILES['newImg']['tmp_name'][$_POST['num']],$_FILES['newImg']['name'][$_POST['num']],$i);
                                
move_uploaded_file($_FILES['newImg']['tmp_name'][$_POST['num']], $path_product.$_FILES['newImg']['name'][$_POST['num']]);
                                @
unlink($path_product.$_POST['oldImg'][$_POST['num']]);
             }else{
                                 
$_FILES['newImg']['name'][$_POST['num']]=$_POST['oldImg'][$_POST['num']];
                 }
                 
$query="UPDATE tbl_product_cate SET `cate_name` =  '".$_POST['textfield'][$_POST['num']]."' , `cate_bg`='".$_FILES['newImg']['name'][$_POST['num']]."' WHERE id = '".$_POST['EditID']."' ";
                  
mysql_query($query);
                  
                
         }
         
//----------------------------------------
         
if($_POST['action']=='Sort')
    {
        switch(
$_POST['toSort']) {
                case 
"up"  
                        
$numberUpDate=$_POST['number']-1;
                        
$query="UPDATE tbl_product_cate SET number = number+1 WHERE number='".$numberUpDate."'  AND mainCateId='0'   ";
                        
mysql_query($query);
                        
$query="UPDATE tbl_product_cate SET number = number-1 WHERE id='".$_POST['EditID']."'     ";
                        
mysql_query($query);
                        break;
                case 
"down"  
                        
$numberUpDate=$_POST['number']+1;
                        
$query="UPDATE tbl_product_cate SET number =number-1 WHERE number='".$numberUpDate."'  AND mainCateId='0'   ";
                        
mysql_query($query);
                        
$query="UPDATE tbl_product_cate SET number =number+1 WHERE id='".$_POST['EditID']."'   ";
                        
mysql_query($query);    
                        break;    
         }
        
    }
//end Sort
   //-------------------------------------------------------------    
   
$query="SELECT * FROM  tbl_product_cate WHERE  mainCateId='0' AND category_group='".$_GET['group']."' ORDER BY number  ASC ";
    
$result=mysql_query($query);
    
$queryMax "SELECT MAX(number) MaxNumber FROM  tbl_product_cate WHERE  mainCateId='0' ";
    
$resultMax=mysql_query($queryMax);
    
$dataMax=mysql_fetch_assoc($resultMax);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
                //--------------------------------        
            function  FormValid(){
                     if(document.form1.cate_name.value==''){
                                 alert("กรุณาใส่ชื่อ หมวดสินค้า ");
                                return false;
                    }else {     
                            document.form1.action.value = 'AddPack';
                        }
                }
            //---------------------------------------------------
          function EditThis(ids,nums){
                      document.form1.EditID.value=ids;                      
                      document.form1.num.value=nums;
                      document.form1.action.value = 'EditCate';
                    document.form1.submit();
              }        
            //---------------------------------------------------
         function DeleteThis(ids,bgimg){
                 if(confirm('ต้องการลบรายการนี้')){
                      document.form1.EditID.value=ids;                      
                      document.form1.action.value = 'DelteCate';
                      document.form1.backgroundImg.value = bgimg;                    
                    document.form1.submit();                        
                 }else{
                         return false;
                     }
             
             }
            //---------------------------------------------------            
                function sortThis(number, toSort , ids ){
                          document.form1.number.value=number;
                        document.form1.toSort.value=toSort;
                        document.form1.EditID.value=ids;
                        document.form1.action.value='Sort';
                        document.form1.submit();
          }
</script>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post" name="form1" onSubmit="return FormValid();" enctype="multipart/form-data">
    <table width="100%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td class="txt10-black">
      <table width="100%" border="0" cellspacing="1" cellpadding="3">
    
        <tr>
          <td width="26%" align="right" class="txt10-black">ชื่อหมวด</td>
          <td width="74%"><input name="cate_name" type="text" id="cate_name" size="40" /></td>
        </tr>
        <tr>
          <td width="26%" align="right" class="txt10-black">รูปหมวด</td>
          <td class="txt-8-red"><input name="uploadfile" type="file" id="uploadfile" size="28" />
            <br />
            **ขนาดไฟล์ไม่เกิน 1 MB (.jpg ,.gif, .png) ขนาด 235 x สูง 180 pixel</td>
        </tr>
        <tr>
          <td align="right">
           <input type="hidden" name="num"  />
            <input type="hidden" name="EditID"  />        
            <input type="hidden" name="action" id="action" />
            <input type="hidden" name="toSort" id="toSort" /> 
          <input type="hidden" name="number"  />     
            <input type="hidden" name="backgroundImg"  />          
            <input name="mainCateId" type="hidden" id="mainCateId" value="0" />
            </td>
          <td><input type="submit" name="button" id="button" value="เพิมหมวดสินค้า" /></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr class="txt10-black">
          <td height="30" colspan="2" align="left" bgcolor="#FFFFFF" class="txt10-black" ><img src="images/black_icon/16x16/notepad_2.png" width="16" height="16" hspace="5" align="absmiddle" />category list</td>
          </tr>
        <tr>
          <td colspan="2" align="left"><table width="100%" border="0" cellspacing="1" cellpadding="1">
             <tr>
              <td align="center" bgcolor="#E1E1E1">&nbsp;</td>
              <td bgcolor="#E1E1E1">&nbsp;</td>
              <td align="center" bgcolor="#E1E1E1" class="txt10-black">รูปหมวด</td>
              <td bgcolor="#E1E1E1">&nbsp;</td>
              <td bgcolor="#E1E1E1">&nbsp;</td>
              <td align="center" bgcolor="#E1E1E1" class="txt10-black">&nbsp;</td>
              <td align="center" bgcolor="#E1E1E1" class="txt10-black">แก้ไข</td>
              <td align="center" bgcolor="#E1E1E1" class="txt10-black">ลบ</td>
            </tr>
            <?php $n=1;while($data=mysql_fetch_assoc($result)){ ?>
            <tr>
              <td width="3%" align="center"><img src="images/black_icon/16x16/rnd_br_next.png" width="16" height="16" /></td>
              <td width="48%"><input name="textfield[<?php echo $n?>]" type="text" id="textfield" size="50"  value="<?php echo $data['cate_name']?>"/></td>
              <td width="19%" align="center" class="txt10-black">
              <!--<a href="#" title="<?php echo $data['cate_bg']?>">
              <img src="images/black_icon/16x16/picture.png" width="16" height="16" border="0" onClick="windowOpener('645', '750', 'backgroundCategory', 'category_background_view.php?CID=<?php echo $data['id']?>&background=<?php echo $data['cate_bg']?>')" /> -->
              <img src="<?php echo $path_product.$data['cate_bg']?>"  />
              </a>
              
              <input type="hidden" name="oldImg[<?php echo $n?>]"  value="<?php echo $data['cate_bg']?>" />
              <br />
              เปลียนรูป <span class="txt-8-red">
              <input name="newImg[<?php echo $n?>]" type="file" size="10" />
              </span></td>
              <td width="10%" align="center" bgcolor="#DBDBB9"> <?php if($data['number']>1){?>
               <a href="#" onClick="sortThis('<?php echo $data['number'];?>', 'up' , '<?php echo $data['id'];?>' )" title="up">
              <img src="images/black_icon/16x16/sq_br_up.png" width="16" height="16" hspace="5" vspace="3" border="0" /></a>
              <?php ?></td>
              <td width="8%" align="center" bgcolor="#EBEBD8"><?php if($dataMax['MaxNumber'] > $data['number']){?>
                <a href="#" onClick="sortThis('<?php echo $data['number'];?>', 'down' , '<?php echo $data['id'];?>' )" title="down"> <img src="images/black_icon/16x16/sq_br_down.png" width="16" height="16" hspace="5" vspace="3" border="0" /></a>
                <?php ?></td>
              <td width="1%" align="center">&nbsp;</td>
              <td width="6%" align="center">
              <a href="javascript:void(0)"  onclick="EditThis('<?php echo $data['id']?>','<?php echo $n?>')" title="แก้ไข <?php echo $data['cate_name']?>">
              <img src="images/black_icon/16x16/doc_edit.png" width="16" height="16" border="0" /></a></td>
              <td width="5%" align="center">
              <a href="javascript:void(0)" onClick="DeleteThis('<?php echo $data['id']?>', '<?php echo $data['cate_bg']?>')" title="ลบ <?php echo $data['cate_name']?>">
              <img src="images/black_icon/16x16/doc_delete.png" width="16" height="16" border="0" />
             </a>
              </td>
            </tr>
         
            <tr>
              <td height="1" colspan="8" align="center" bgcolor="#E1E1E1"></td>
              </tr>
              <?php $n++; }?>
          </table></td>
          </tr>
      </table></td>
  </tr>
  </table>
</form>
</body>
</html>