PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/kernel/category.class.php

http://xklog.googlecode.com/
PHP | 343 lines | 310 code | 16 blank | 17 comment | 34 complexity | 07b27e69c8ae9f0538909ded033c9ac3 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. !defined('IN_NOVA') && exit('Access Denied!');
  3. class Category {
  4. public function start() {
  5. global $request;
  6. $id = $request->id;
  7. $action = $request->get( 'p' );
  8. switch( $action ) {
  9. case 'addsort':
  10. $this->sort_add_data();
  11. break;
  12. case 'editsort':
  13. $this->sort_edit_data();
  14. break;
  15. case 'delsort':
  16. $this->sort_del_data();
  17. break;
  18. case "datacheckexe":
  19. $this->data_check_exe();
  20. break;
  21. default:
  22. $this->show( $action );
  23. }
  24. }
  25. function sort_add_data() {
  26. global $db,$request,$cache;
  27. $name = trim( $request->get( 'name','P' ) );
  28. $alias = trim( $request->get( 'alias','P' ) );
  29. $parent = $request->get( 'parent','P','int' );
  30. $description = trim( $request->get( 'description','P' ) );
  31. $num = $request->get( 'num','P','int' );
  32. if( $name == '' || $alias == '' || $description == '' || $parent === '' ) {
  33. echo '<p class="error">???????????</p>';
  34. return;
  35. }
  36. // ? $alias ????
  37. if( $db->result( "SELECT COUNT(cid) FROM `" . DB_PREFIX . "category` WHERE alias='$alias'" ) ) {
  38. echo '<p class="error">????????????</p>';
  39. return;
  40. }
  41. /*$sort_folder = APP_ROOT . 'article/' . $alias . '/';
  42. if( !$this->writeable( $sort_folder ) ) {
  43. echo '<p class="error">???????????</p>';
  44. return;
  45. }*/
  46. $db->query( "INSERT INTO `" . DB_PREFIX . "category` (`pid`,`num`,`name`,`alias`,`description`,`articlenum`) VALUES ($parent,$num,'$name','$alias','$description',0)" );
  47. $cache->refresh( 'category', TRUE );
  48. echo '<p class="warning">??????</p>';
  49. }
  50. function writeable($var) {
  51. $result = false;
  52. if ( !is_dir($var) ) {
  53. @mkdir($var, 0777);
  54. }
  55. if ( is_dir($var) ) {
  56. $var .= 'temp.txt';
  57. if ( ($fp=@fopen($var, 'wb')) && (@fwrite($fp, 'XKLog')) ) {
  58. @fclose($fp);
  59. @unlink($var);
  60. $result = true;
  61. }
  62. }
  63. return $result;
  64. }
  65. function sort_edit_data() {
  66. global $db,$request,$cache;
  67. $id = $request->get( 'id','P','int' );
  68. $name = $request->get( 'name','P' );
  69. $alias = $request->get( 'alias','P' );
  70. $parent = $request->get( 'parent','P','int' );
  71. $description = $request->get( 'description','P' );
  72. $num = $request->get( 'num','P','int' );
  73. if( $name == '' || $alias == '' || $description == '' || $parent === '' ) {
  74. echo '<p class="error">???????????</p>';
  75. return;
  76. }
  77. if( $db->result( "SELECT COUNT(cid) FROM `" . DB_PREFIX . "category` WHERE alias='$alias' AND cid<>$id" ) ) {
  78. echo '<p class="error">??????????</p>';
  79. return;
  80. }
  81. /*$sort_folder = APP_ROOT . 'article/' . $alias . '/';
  82. $old_alias = $db->result( "SELECT alias FROM `" . DB_PREFIX . "category` WHERE cid=$id" );
  83. $path = APP_ROOT . 'article/';
  84. if ( !is_dir( $path . $old_alias ) ) {
  85. @mkdir( $path . $old_alias, 0777 );
  86. }
  87. @rename( $path . $old_alias, $path . $alias );
  88. if( !$this->writeable( $path . $alias ) ) {
  89. echo '<p class="error">???????????</p>';
  90. return;
  91. }*/
  92. $db->query( "UPDATE `" . DB_PREFIX . "category` SET pid='$parent',name='$name',alias='$alias',description='$description',num='$num' WHERE cid=$id" );
  93. $cache->refresh( 'category', TRUE );
  94. echo '<p class="warning">??????</p>';
  95. }
  96. function sort_del_data() {
  97. global $db,$request,$cache;
  98. $id = $request->get( 'id', 'G', 'int');
  99. $article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 And category=$id" );
  100. if( $article_num != 0 ) {
  101. echo '<p class="error">?????????????</p>';
  102. }else{
  103. $db->query( "DELETE FROM `" . DB_PREFIX . "category` WHERE cid=$id" );
  104. $cache->refresh( 'category', TRUE );
  105. echo '<p class="warning">??????</p>';
  106. }
  107. }
  108. function data_check_exe() {
  109. global $cache,$db;
  110. If( count( $cache->category ) == 0 ) {
  111. $this->show( 'datacheck', '??????', 'error' );
  112. return;
  113. }else{
  114. foreach( $cache->category as $category ) {
  115. $cid = $category['cid'];
  116. $article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 And category=$cid" );
  117. if( $category['articlenum'] != $article_num ) {
  118. $db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum=$article_num WHERE cid=$cid" );
  119. }
  120. }
  121. }
  122. $cache->refresh( 'category', TRUE );
  123. echo '<p class="warning">??????????</p>';
  124. }
  125. private function show( $action, $message = '', $type = '' ) {
  126. global $cache;
  127. if( $message != '' ) {
  128. $message = '<p class="' . $type . '">' . $message . '</p>';
  129. }
  130. @header("content-type: text/html; charset=utf-8");
  131. ?>
  132. <div class="admin_panel">
  133. <?php
  134. echo $message;
  135. switch( $action ) {
  136. case "sort":
  137. $this->sort_list();
  138. break;
  139. case "add":
  140. $this->sort_add();
  141. break;
  142. case "edit":
  143. $this->sort_edit();
  144. break;
  145. case "datacheck":
  146. $this->data_check();
  147. break;
  148. default:
  149. $this->main();
  150. }
  151. ?>
  152. <script>
  153. $(document).ready(function(){
  154. $('#ajax_form').ajaxForm(function(data){
  155. showMessage(data);
  156. $("#admin_loading").css('visibility','hidden');
  157. });
  158. });
  159. </script>
  160. </div>
  161. <?php
  162. }
  163. private function sort_list() {
  164. global $db;
  165. $sql = 'SELECT * FROM `' . DB_PREFIX . 'category` ORDER BY num ASC,cid DESC';
  166. $sort_array = $db->fetch_all( $sql );
  167. ?>
  168. <div class="admin_title"><?php echo L('_CATEGORY_MANAGEMENT_'); ?></div>
  169. <div class="admin_content">
  170. <div style="padding:5px;margin:2px;">
  171. <table style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#dddddd">
  172. <tr align="center" height="22" bgcolor="#ffffff">
  173. <td width="3%"></td>
  174. <td width="4%"><?php echo L('_NUM_'); ?></td>
  175. <td width="17%"><?php echo L('_NAME_'); ?></td>
  176. <td width="6%">??</td>
  177. <td width="14%"><?php echo L('_ALIAS_'); ?></td>
  178. <td width="6%"><?php echo L('_SORT_'); ?></td>
  179. <td width="33%"><?php echo L('_DESCRIPTION_'); ?></td>
  180. <td width="6%"><?php echo L('_NUMBER_'); ?></td>
  181. <td width="11%"><?php echo L('_OPERATION_'); ?></td>
  182. </tr>
  183. <?php
  184. foreach( $sort_array as $value ) {
  185. ?>
  186. <tr align="center" height="20" bgcolor="#ffffff">
  187. <td ></td>
  188. <td><?php echo $value['cid'] ?></td>
  189. <td><?php echo $value['name'] ?></td>
  190. <td><?php echo $value['pid'] ?></td>
  191. <td><?php echo $value['alias'] ?></td>
  192. <td><?php echo $value['num'] ?></td>
  193. <td><?php echo $value['description'] ?></td>
  194. <td><?php echo $value['articlenum'] ?></td>
  195. <td>
  196. <span class="Control">
  197. <a href="#" onclick="ajax_load('<?php echo ADMIN_PATH ?>','category','edit',null,'<?php echo $value['cid'] ?>');return false;"><?php echo L('_EDIT_'); ?></a>&nbsp; &nbsp;
  198. <a href="#" onclick="if(confirm('<?php echo L('_DELETE_CONFIRM_1_'); ?>'))ajax_get('<?php echo ADMIN_PATH ?>','category','delsort',null,'<?php echo $value['cid'] ?>');return false;"><?php echo L('_DELETE_'); ?></a>
  199. </span>
  200. </td>
  201. </tr>
  202. <?php
  203. }
  204. ?>
  205. </table>
  206. </div>
  207. <div class="main_button"></div>
  208. </div>
  209. <?php
  210. }
  211. private function sort_add() {
  212. ?>
  213. <div class="admin_title"><?php echo L('_CATEGORY_ADD_'); ?></div>
  214. <div class="admin_content">
  215. <form action="index.php?m=category&p=addsort" method="post" id="ajax_form">
  216. <table border="0" cellpadding="2" cellspacing="1">
  217. <tr>
  218. <td width="180"><div align="right"><?php echo L('_NAME_'); ?>&nbsp;&nbsp;</div></td>
  219. <td align="left"><input name="name" type="text" size="30" maxlength="50" class="main_text" /></td>
  220. </tr>
  221. <tr>
  222. <td width="180"><div align="right"><?php echo L('_ALIAS_'); ?>&nbsp;&nbsp;</div></td>
  223. <td align="left"><input name="alias" type="text" size="30" maxlength="50" class="main_text" /></td>
  224. </tr>
  225. <tr>
  226. <td width="180"><div align="right"><?php echo L('_DESCRIPTION_'); ?>&nbsp;&nbsp;</div></td>
  227. <td align="left"><input name="description" type="text" size="30" maxlength="100" class="main_text" /></td>
  228. </tr>
  229. <tr>
  230. <td width="180"><div align="right">??&nbsp;&nbsp;</div></td>
  231. <td align="left"><select name="parent"><option value="0">|</option><?php echo $this->get_category(); ?></select></td>
  232. </tr>
  233. <tr>
  234. <td width="180"><div align="right"><?php echo L('_SORT_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_SORT_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
  235. <td align="left"><input name="num" type="text" size="30" maxlength="10" class="main_text" /></td>
  236. </tr>
  237. <tr>
  238. <td width="180"><div align="right"></div></td>
  239. <td align="left"><input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value=" <?php echo L('_ADD_'); ?> " /></td>
  240. </tr>
  241. </table>
  242. </form>
  243. <div class="main_button"></div>
  244. </div>
  245. <?php
  246. }
  247. private function get_category( $current = 0, $pid = 0, $deep = 0 ) {
  248. global $cache;
  249. $output = '';
  250. foreach ( $cache->category as $row ) {
  251. if( $row['pid'] != $pid ) continue;
  252. if( $current == $row['cid'] ) {
  253. $output .= '<option value="' . $row['cid'] . '" selected="selected">';
  254. } else {
  255. $output .= '<option value="' . $row['cid'] . '">';
  256. }
  257. for( $i = 0 ; $i < $deep ; $i ++ ) {
  258. $output .= '&nbsp;&nbsp;';
  259. }
  260. $output .= '|- ?' . $row['name'] . '</option>';
  261. $output .= $this->get_category( $current, $row['cid'], $deep + 1 );
  262. }
  263. return $output;
  264. }
  265. private function sort_edit() {
  266. global $db,$request;
  267. $id = $request->get( 'id','G','int' );
  268. $sql = "SELECT * FROM `" . DB_PREFIX . "category` WHERE cid=$id";
  269. $sort_array = $db->fetch_one_array( $sql );
  270. ?>
  271. <div class="admin_title">????</div>
  272. <div class="admin_content">
  273. <form action="index.php?m=category&p=editsort&id=<?php echo $id ?>" method="post" id="ajax_form">
  274. <input name="id" type="hidden" value="<?php echo $id ?>" />
  275. <table border="0" cellpadding="2" cellspacing="1">
  276. <tr>
  277. <td width="180"><div align="right"><?php echo L('_NAME_'); ?>&nbsp;&nbsp;</div></td>
  278. <td align="left"><input name="name" type="text" size="30" maxlength="50" class="main_text" value="<?php echo $sort_array['name'] ?>" /></td>
  279. </tr>
  280. <tr>
  281. <td width="180"><div align="right"><?php echo L('_ALIAS_'); ?>&nbsp;&nbsp;</div></td>
  282. <td align="left"><input name="alias" type="text" size="30" maxlength="50" class="main_text" value="<?php echo $sort_array['alias'] ?>" /></td>
  283. </tr>
  284. <tr>
  285. <td width="180"><div align="right"><?php echo L('_DESCRIPTION_'); ?>&nbsp;&nbsp;</div></td>
  286. <td align="left"><input name="description" type="text" size="30" maxlength="100" class="main_text" value="<?php echo $sort_array['description'] ?>" /></td>
  287. </tr>
  288. <tr>
  289. <td width="180"><div align="right">??&nbsp;&nbsp;</div></td>
  290. <td align="left"><select name="parent"><option value="0">|</option><?php echo $this->get_category( $sort_array['pid'] ); ?></select></td>
  291. </tr>
  292. <tr>
  293. <td width="180"><div align="right"><?php echo L('_SORT_'); ?>&nbsp;&nbsp;<div class="main_tips">?????0???&nbsp;&nbsp;</div></div></td>
  294. <td align="left"><input name="num" type="text" size="30" maxlength="10" class="main_text" value="<?php echo $sort_array['num'] ?>" /></td>
  295. </tr>
  296. <tr>
  297. <td width="180"><div align="right"></div></td>
  298. <td align="left"><input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value=" <?php echo L('_EDIT_'); ?> " /></td>
  299. </tr>
  300. </table>
  301. </form>
  302. <div class="main_button"></div>
  303. </div>
  304. <?php
  305. }
  306. private function data_check() {
  307. ?>
  308. <div class="admin_title"><?php echo L('_CATEGORY_MANAGEMENT_'); ?></div>
  309. <div class="admin_content">
  310. <div style="padding:10px;">
  311. <a href="#" onclick="ajax_get('<?php echo ADMIN_PATH ?>','category','datacheckexe');return false;"><?php echo L('_CATEGORY_DATA_CHECK_'); ?></a>
  312. </div>
  313. </div>
  314. <?php
  315. }
  316. private function main() {
  317. ?>
  318. <div class="admin_title"><?php echo L('_CATEGORY_MANAGEMENT_'); ?></div>
  319. <div class="admin_content">
  320. <div style="padding:10px;">
  321. <?php echo L('_CATEGORY_MESSAGE_'); ?>
  322. </div>
  323. </div>
  324. <?php
  325. }
  326. }
  327. ?>