16-1
上課範例:admin/index.php
004 | include_once "header_admin.php" ; |
005 | include_once "../up_file.php" ; |
008 | function contact_form( $sn = "" ){ |
009 | global $xoopsDB , $xoopsUser ; |
013 | $DBV =get_contact( $sn ); |
021 | $tel =(!isset( $DBV [ 'tel' ]))? "" : $DBV [ 'tel' ]; |
024 | $email =(!isset( $DBV [ 'email' ]))? "" : $DBV [ 'email' ]; |
027 | $name =(!isset( $DBV [ 'name' ]))? "" : $DBV [ 'name' ]; |
030 | $gsn =(!isset( $DBV [ 'gsn' ]))?null: $DBV [ 'gsn' ]; |
033 | $sn =(!isset( $DBV [ 'sn' ]))? $sn : $DBV [ 'sn' ]; |
036 | $birthday =(!isset( $DBV [ 'birthday' ]))? "" : $DBV [ 'birthday' ]; |
039 | $zip =(!isset( $DBV [ 'zip' ]))? "" : $DBV [ 'zip' ]; |
042 | $county =(!isset( $DBV [ 'county' ]))? "" : $DBV [ 'county' ]; |
045 | $city =(!isset( $DBV [ 'city' ]))? "" : $DBV [ 'city' ]; |
048 | $addr =(!isset( $DBV [ 'addr' ]))? "" : $DBV [ 'addr' ]; |
050 | $op =( empty ( $sn ))? "insert_contact" : "update_contact" ; |
053 | $jquery =get_jquery(); |
055 | include_once XOOPS_ROOT_PATH. "/modules/tadtools/formValidator.php" ; |
056 | $formValidator = new formValidator( "#myForm" ,false); |
057 | $formValidator_code = $formValidator ->render(); |
062 | <script type= 'text/javascript' src= '".TADTOOLS_URL."/My97DatePicker/WdatePicker.js' ></script> |
063 | <script language= 'javascript' src= '../class/twzipcode-1.3.1.js' ></script> |
064 | <script type= 'text/javascript' > |
065 | $(document).ready( function (){ |
066 | $.post( 'ajax.php' , function (data){ |
067 | $( '#menu1' ).html(data); |
068 | $.post( 'ajax.php' , {parent_gsn: $( '#menu1' ).val()} , function (data){ |
069 | $( '#menu2' ).html(data); |
073 | $( '#menu1' ).change( function (){ |
074 | $.post( 'ajax.php' , {parent_gsn: $( '#menu1' ).val()} , function (data){ |
075 | $( '#menu2' ).html(data); |
079 | $( '#AddrForm' ).twzipcode({ |
080 | countyName: 'county' , |
083 | countySel: '$county' , |
090 | <script src= '".TADTOOLS_URL."/multiple-file-upload/jquery.MultiFile.js' ></script> |
091 | <form action= '{$_SERVER[' PHP_SELF ']}' method= 'post' id= 'myForm' enctype= 'multipart/form-data' > |
093 | <table class = 'form_tbl' > |
096 | <tr><td class = 'title' nowrap>群組</td> |
098 | <select id= 'menu1' ></select> |
099 | <select name= 'gsn' id= 'menu2' ></select> |
103 | <tr><td class = 'title' nowrap>姓名</td> |
104 | <td class = 'col' ><input type= 'text' name= 'name' size= '20' value= '{$name}' id= 'name' class = 'validate[required,minSize[2],maxSize[10]]' ></td></tr> |
108 | <tr><td class = 'title' nowrap>電話</td> |
109 | <td class = 'col' ><input type= 'text' name= 'tel' size= '20' value= '{$tel}' id= 'tel' class = 'validate[required,custom[phone]]' ></td></tr> |
112 | <tr><td class = 'title' nowrap>信箱</td> |
113 | <td class = 'col' ><input type= 'text' name= 'email' size= '20' value= '{$email}' id= 'email' class = 'validate[required,custom[email]]' ></td></tr> |
117 | <tr><td class = 'title' nowrap>生日</td> |
118 | <td class = 'col' ><input type= 'text' name= 'birthday' size= '20' value= '{$birthday}' id= 'birthday' onClick=\"WdatePicker({dateFmt: 'yyyy-MM-dd' , isShowWeek:true , skin: 'whyGreen' , maxDate: '%y-%M-%d' , readOnly: true , isShowClear: false , firstDayOfWeek:1})\" class = 'Wdate' ></td></tr> |
121 | <tr><td class = 'title' nowrap>上傳圖片</td> |
123 | <input type= 'file' name= 'upfile[]' class = 'multi' maxlength= '1' accept= 'gif|jpg|png|GIF|JPG|PNG' >". |
124 | list_del_file( 'photo' , $sn )." |
129 | <tr><td class = 'title' nowrap>上傳相關檔案</td> |
131 | <input type= 'file' name= 'docs[]' class = 'multi' >". |
132 | list_del_file( 'files' , $sn )." |
138 | <tr><td class = 'title' nowrap>地址</td> |
140 | <div style= 'position:relative;' > |
141 | <span id= 'AddrForm' ></span> |
142 | <input type= 'text' name= 'addr' size= '30' value= '{$addr}' id= 'addr' > |
147 | <td class = 'bar' colspan= '2' > |
150 | <input type= 'hidden' name= 'sn' value= '{$sn}' > |
152 | <input type= 'hidden' name= 'op' value= '{$op}' > |
153 | <input type= 'submit' value= '儲存' > |
160 | $main =div_3d( "通訊錄管理" , $main , "raised" ); |
168 | function insert_contact(){ |
169 | global $xoopsDB , $xoopsUser ; |
172 | $myts =& MyTextSanitizer::getInstance(); |
173 | $_POST [ 'tel' ]= $myts -> addSlashes ( $_POST [ 'tel' ]); |
174 | $_POST [ 'email' ]= $myts -> addSlashes ( $_POST [ 'email' ]); |
175 | $_POST [ 'name' ]= $myts -> addSlashes ( $_POST [ 'name' ]); |
176 | $_POST [ 'birthday' ]= $myts -> addSlashes ( $_POST [ 'birthday' ]); |
177 | $_POST [ 'zip' ]= $myts -> addSlashes ( $_POST [ 'zip' ]); |
178 | $_POST [ 'county' ]= $myts -> addSlashes ( $_POST [ 'county' ]); |
179 | $_POST [ 'city' ]= $myts -> addSlashes ( $_POST [ 'city' ]); |
180 | $_POST [ 'addr' ]= $myts -> addSlashes ( $_POST [ 'addr' ]); |
183 | $sql = "insert into `" . $xoopsDB ->prefix( "contact" )."` |
184 | (`tel` , `email` , `name` , `gsn` , `birthday` , `zip` , `county` , `city` , `addr`) |
185 | values( '{$_POST[' tel ']}' , '{$_POST[' email ']}' , '{$_POST[' name ']}' , '{$_POST[' gsn ']}' , '{$_POST[' birthday ']}' , '{$_POST[' zip ']}' , '{$_POST[' county ']}' , '{$_POST[' city ']}' , '{$_POST[' addr ']}' )"; |
186 | $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
189 | $sn = $xoopsDB ->getInsertId(); |
191 | upload_file( 'photo' , $sn , '400' ); |
192 | upload_file( 'files' , $sn , '400' , 'docs' ); |
197 | function update_contact( $sn = "" ){ |
198 | global $xoopsDB , $xoopsUser ; |
201 | $myts =& MyTextSanitizer::getInstance(); |
202 | $_POST [ 'tel' ]= $myts -> addSlashes ( $_POST [ 'tel' ]); |
203 | $_POST [ 'email' ]= $myts -> addSlashes ( $_POST [ 'email' ]); |
204 | $_POST [ 'name' ]= $myts -> addSlashes ( $_POST [ 'name' ]); |
205 | $_POST [ 'birthday' ]= $myts -> addSlashes ( $_POST [ 'birthday' ]); |
206 | $_POST [ 'zip' ]= $myts -> addSlashes ( $_POST [ 'zip' ]); |
207 | $_POST [ 'county' ]= $myts -> addSlashes ( $_POST [ 'county' ]); |
208 | $_POST [ 'city' ]= $myts -> addSlashes ( $_POST [ 'city' ]); |
209 | $_POST [ 'addr' ]= $myts -> addSlashes ( $_POST [ 'addr' ]); |
212 | $sql = "update `" . $xoopsDB ->prefix( "contact" )."` set |
213 | `tel` = '{$_POST[' tel ']}' , |
214 | `email` = '{$_POST[' email ']}' , |
215 | `name` = '{$_POST[' name ']}' , |
216 | `gsn` = '{$_POST[' gsn ']}' , |
217 | `birthday` = '{$_POST[' birthday ']}' , |
218 | `zip` = '{$_POST[' zip ']}' , |
219 | `county` = '{$_POST[' county ']}' , |
220 | `city` = '{$_POST[' city ']}' , |
221 | `addr` = '{$_POST[' addr ']}' |
223 | $xoopsDB ->queryF( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
226 | upload_file( 'photo' , $sn , '400' ); |
227 | upload_file( 'files' , $sn , '400' , 'docs' ); |
232 | function list_contact( $show_function =1){ |
233 | global $xoopsDB , $xoopsModule , $isAdmin ; |
236 | $sql = "select gsn,title from `" . $xoopsDB ->prefix( "contact_cate" ). "` where parent_gsn!='0' order by sort" ; |
237 | $result = $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
238 | while (list( $gsn , $title )= $xoopsDB ->fetchRow( $result )){ |
239 | $cate_arr []= "'$gsn':'$title'" ; |
241 | $cate_option =implode( " , " , $cate_arr ); |
244 | include_once XOOPS_ROOT_PATH. "/modules/tadtools/jeditable.php" ; |
246 | $jeditable = new jeditable(); |
253 | $sql = "select * from `" . $xoopsDB ->prefix( "contact" ). "` " ; |
254 | $result = $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
256 | $function_title =( $show_function )? "<th>功能</th>" : "" ; |
260 | while ( $all = $xoopsDB ->fetchArray( $result )){ |
262 | foreach ( $all as $k => $v ){ |
266 | $fun =( $show_function )?" |
268 | <a href= '{$_SERVER[' PHP_SELF ']}?op=contact_form&sn=$sn' class = 'link_button' >編輯</a> |
269 | <a href=\"javascript:delete_contact_func( $sn );\" class = 'link_button' >刪除</a> |
272 | $jeditable ->setSelectCol( "#gsn_{$sn}" , $file , "{{$cate_option}}" , "{'sn' : $sn , 'col' : 'gsn'}" ); |
273 | $jeditable ->setTextCol( "#name_{$sn}" , $file , '100px' , '12px' , "{'sn':$sn , 'col' : 'name'}" ); |
274 | $jeditable ->setTextCol( "#email_{$sn}" , $file , '100px' , '12px' , "{'sn':$sn , 'col' : 'email'}" ); |
276 | $cate =get_contact_cate_all(); |
278 | $file_counter1 =get_file_amount( 'photo' , $sn ); |
279 | $file_counter2 =get_file_amount( 'files' , $sn ); |
283 | <td id= 'gsn_{$sn}' >{ $cate [ $gsn ][ 'title' ]}</td> |
284 | <td id= 'name_{$sn}' >{ $name }</td> |
286 | <td id= 'email_{$sn}' >{ $email }</td> |
292 | <td>{ $file_counter1 }</td> |
293 | <td>{ $file_counter2 }</td> |
299 | $jeditable_set = $jeditable ->render(); |
303 | $add_button =( $show_function )? "<a href='{$_SERVER['PHP_SELF']}?op=contact_form' class='link_button_r'>新增</a>" : "" ; |
310 | function delete_contact_func(sn){ |
311 | var sure = window.confirm( '確定刪除此資料?' ); |
313 | location.href=\"{ $_SERVER [ 'PHP_SELF' ]}?op=delete_contact&sn=\" + sn; |
319 | <form action= 'index.php' method= 'post' enctype= 'multipart/form-data' > |
320 | <INPUT type= 'file' name= 'userfile' > |
321 | <INPUT type= 'hidden' name= 'op' value= 'import' > |
322 | <INPUT type= 'submit' value= '匯入 CSV' > |
323 | <INPUT type= 'button' value= '匯出 CSV' onClick=\"location.href= 'index.php?op=export' \"> |
328 | <form action= 'index.php' method= 'post' enctype= 'multipart/form-data' > |
329 | <INPUT type= 'file' name= 'importfile' > |
330 | <INPUT type= 'hidden' name= 'op' value= 'import_excel' > |
331 | <INPUT type= 'submit' value= '匯入 Excel' > |
332 | <INPUT type= 'button' value= '匯出 Excel' onClick=\"location.href= 'excel.php' \"> |
335 | <INPUT type= 'button' value= '匯出 PDF' onClick=\"location.href= 'pdf.php' \"> |
337 | <table summary= 'list_table' id= 'tbl' style= 'width:100%;' > |
358 | <td colspan=11 class = 'bar' > |
365 | $main =div_3d( "" , $main , "corners" ); |
372 | function get_contact( $sn = "" ){ |
374 | if ( empty ( $sn )) return ; |
375 | $sql = "select * from `" . $xoopsDB ->prefix( "contact" ). "` where `sn` = '{$sn}'" ; |
376 | $result = $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
377 | $data = $xoopsDB ->fetchArray( $result ); |
382 | function delete_contact( $sn = "" ){ |
383 | global $xoopsDB , $isAdmin ; |
384 | $sql = "delete from `" . $xoopsDB ->prefix( "contact" ). "` where `sn` = '{$sn}'" ; |
385 | $xoopsDB ->queryF( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
387 | del_files( '' , 'photo' , $sn ); |
388 | del_files( '' , 'files' , $sn ); |
393 | function get_contact_cate_options( $mode = 'show' , $default_gsn = "0" , $default_parent_gsn = "0" , $unselect_level = "" , $start_search_sn = "0" , $level =0){ |
394 | global $xoopsDB , $xoopsModule ; |
395 | $sql = "select `gsn` , `title` from `" . $xoopsDB ->prefix( "contact_cate" ). "` where `parent_gsn` = '{$start_search_sn}' order by `sort`" ; |
396 | $result = $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ] , 3, mysql_error()); |
398 | $prefix = str_repeat ( " " , $level ); |
401 | $unselect = explode ( "," , $unselect_level ); |
404 | while (list( $gsn , $title )= $xoopsDB ->fetchRow( $result )){ |
406 | $selected =( $gsn == $default_parent_gsn )? "selected=selected" : "" ; |
407 | $selected .=( $gsn == $default_gsn )? "disabled=disabled" : "" ; |
408 | $selected .=(in_array( $level , $unselect ))? "disabled=disabled" : "" ; |
410 | $selected =( $gsn == $default_gsn )? "selected=selected" : "" ; |
411 | $selected .=(in_array( $level , $unselect ))? "disabled=disabled" : "" ; |
413 | $main .= "<option value=$gsn $selected>{$prefix}{$title}</option>" ; |
414 | $main .=get_contact_cate_options( $mode , $default_gsn , $default_parent_gsn , $unselect_level , $gsn , $level ); |
426 | $handle = fopen ( $_FILES [ 'userfile' ][ 'tmp_name' ], "r" ) or die ( "無法開啟" ); |
428 | while (( $data = __fgetcsv( $handle , 1000)) !== FALSE) { |
431 | if (! is_numeric ( $data [0])){ |
435 | $data [1]=mb_convert_encoding( $data [1], "UTF-8" , "Big5" ); |
436 | $data [6]=iconv( "Big5" , "UTF-8" , $data [6]); |
437 | $data [7]=iconv( "Big5" , "UTF-8" , $data [7]); |
438 | $data [8]=iconv( "Big5" , "UTF-8" , $data [8]); |
456 | $sql = "insert into `" . $xoopsDB ->prefix( "contact" ). "` (`gsn` , `name` , `tel` , `email` , `birthday` , `zip` , `county` , `city` , `addr`) values('{$data[0]}' , '{$data[1]}' , '{$data[2]}' , '{$data[3]}' , '{$data[4]}' , '{$data[5]}' , '{$data[6]}' , '{$data[7]}' , '{$data[8]}')" ; |
457 | $xoopsDB ->queryF( $sql ) or die ( $sql ); |
465 | function __fgetcsv(& $handle , $length = null, $d = "," , $e = '"' ) { |
470 | while ( $eof != true) { |
471 | $_line .= ( empty ( $length ) ? fgets ( $handle ) : fgets ( $handle , $length )); |
472 | $itemcnt = preg_match_all( '/' . $e . '/' , $_line , $dummy ); |
473 | if ( $itemcnt % 2 == 0) |
476 | $_csv_line = preg_replace( '/(?: |[ ])?$/' , $d , trim( $_line )); |
478 | $_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/' ; |
479 | preg_match_all( $_csv_pattern , $_csv_line , $_csv_matches ); |
480 | $_csv_data = $_csv_matches [1]; |
482 | for ( $_csv_i = 0; $_csv_i < count ( $_csv_data ); $_csv_i ++) { |
483 | $_csv_data [ $_csv_i ] = preg_replace( "/^" . $e . "(.*)" . $e . "$/s" , "$1" , $_csv_data [ $_csv_i ]); |
484 | $_csv_data [ $_csv_i ] = str_replace ( $e . $e , $e , $_csv_data [ $_csv_i ]); |
486 | return empty ( $_line ) ? false : $_csv_data ; |
493 | $sql = "select * from `" . $xoopsDB ->prefix( "contact" ). "` " ; |
494 | $result = $xoopsDB ->query( $sql ) or redirect_header( $_SERVER [ 'PHP_SELF' ],3, mysql_error()); |
496 | $main = "gsn,name,tel,email,birthday,zip,county,city,addr\n" ; |
497 | while ( $all = $xoopsDB ->fetchArray( $result )){ |
499 | foreach ( $all as $k => $v ){ |
503 | $name =iconv( "UTF-8" , "Big5" , $name ); |
504 | $county =iconv( "UTF-8" , "Big5" , $county ); |
505 | $city =iconv( "UTF-8" , "Big5" , $city ); |
506 | $addr =iconv( "UTF-8" , "Big5" , $addr ); |
508 | $main .= "$gsn,$name,$tel,$email,$birthday,$zip,$county,$city,$addr\n" ; |
511 | header( "Content-type: text/x-csv" ); |
512 | header( "Content-Disposition: attachment; filename=contact.csv" ); |
518 | function import_excel(){ |
521 | include_once '../class/PHPExcel/IOFactory.php' ; |
522 | $reader = PHPExcel_IOFactory::createReader( 'Excel5' ); |
523 | $PHPExcel = $reader ->load( $_FILES [ 'importfile' ][ 'tmp_name' ] ); |
524 | $sheet = $PHPExcel ->getSheet(0); |
525 | $highestRow = $sheet ->getHighestRow(); |
527 | for ( $row = 1; $row <= $highestRow ; $row ++) { |
530 | for ( $col = 0; $col <= 8; $col ++) { |
533 | if ( PHPExcel_Shared_Date::isDateTime( $sheet ->getCellByColumnAndRow( $col , $row ) )){ |
534 | $val = PHPExcel_Shared_Date::ExcelToPHPObject( $sheet ->getCellByColumnAndRow( $col , $row )->getValue())->format( 'Y-m-d' ); |
536 | $val = $sheet ->getCellByColumnAndRow( $col , $row )->getCalculatedValue(); |
540 | if (!get_magic_quotes_runtime()) { |
541 | $v [ $col ]= addSlashes ( $val ); |
546 | if (! is_numeric ( $v [0]) or empty ( $v [0])) continue ; |
548 | $sql = "insert into `" . $xoopsDB ->prefix( "contact" ). "` (`gsn` , `name` , `tel` , `email` , `birthday` , `zip` , `county` , `city` , `addr`) values('{$v[0]}' , '{$v[1]}' , '{$v[2]}' , '{$v[3]}' , '{$v[4]}' , '{$v[5]}' , '{$v[6]}' , '{$v[7]}' , '{$v[8]}')" ; |
549 | $xoopsDB ->queryF( $sql ) or die ( $sql ); |
554 | $op = empty ( $_REQUEST [ 'op' ])? "" : $_REQUEST [ 'op' ]; |
555 | $sn = empty ( $_REQUEST [ 'sn' ])? "" : intval ( $_REQUEST [ 'sn' ]); |
556 | $gsn = empty ( $_REQUEST [ 'gsn' ])? "" : intval ( $_REQUEST [ 'gsn' ]); |
557 | $files_sn = empty ( $_REQUEST [ 'files_sn' ])? "" : intval ( $_REQUEST [ 'files_sn' ]); |
564 | case "insert_contact" : |
565 | $sn =insert_contact(); |
566 | header( "location: {$_SERVER['PHP_SELF']}?sn=$sn" ); |
570 | case "update_contact" : |
572 | header( "location: {$_SERVER['PHP_SELF']}" ); |
577 | $main =contact_form( $sn ); |
581 | case "delete_contact" : |
583 | header( "location: {$_SERVER['PHP_SELF']}" ); |
589 | header( "location: {$_SERVER['PHP_SELF']}" ); |
601 | header( "location: {$_SERVER['PHP_SELF']}" ); |
606 | $main =list_contact(); |
614 | module_admin_footer( $main ,0); |