power.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. include "conn.php";
  3. checkLogin("权限管理");
  4. // Move all header operations to the top
  5. $act = $_GET['act'] ?? '';
  6. $page = $_GET['Page'] ?? '';
  7. $keys = urlencode($_GET['Keys'] ?? '');
  8. $ord = urlencode($_GET['Ord'] ?? '');
  9. // Handle save action
  10. if ($act == "save") {
  11. $isedit = false;
  12. $id = $_POST['id'] ?? '';
  13. if ($id != "" && is_numeric($id)) {
  14. $isedit = true;
  15. }
  16. $powername = textEncode($_POST['powername'] ?? '');
  17. $powercontent = isset($_POST['powercontent']) ? implode(',', $_POST['powercontent']) : '';
  18. if ($isedit) {
  19. // Check if power name exists
  20. $id = (int)$id;
  21. $sql = "SELECT COUNT(id) as count FROM power WHERE powername = '" . $conn->real_escape_string($powername) . "' AND id != " . $id;
  22. $result = $conn->query($sql);
  23. if ($result->fetch_assoc()['count'] > 0) {
  24. echo "<script>alert('Sorry,该权限名称已经存在,请检查更换');history.back()</script>";
  25. exit;
  26. }
  27. $sql = "UPDATE power SET powername = '" . $conn->real_escape_string($powername) . "',
  28. powercontent = '" . $conn->real_escape_string($powercontent) . "'
  29. WHERE id = " . $id;
  30. $conn->query($sql);
  31. } else {
  32. // Check if power name exists
  33. $sql = "SELECT COUNT(id) as count FROM power WHERE powername = '" . $conn->real_escape_string($powername) . "'";
  34. $result = $conn->query($sql);
  35. if ($result->fetch_assoc()['count'] > 0) {
  36. echo "<script>alert('Sorry,该权限名称已经存在,请检查更换');history.back()</script>";
  37. exit;
  38. }
  39. $sql = "INSERT INTO power (powername, powercontent, powerstate)
  40. VALUES ('" . $conn->real_escape_string($powername) . "',
  41. '" . $conn->real_escape_string($powercontent) . "', 1)";
  42. $conn->query($sql);
  43. }
  44. header("Location: ?keys=$keys&Ord=$ord&Page=$page");
  45. exit;
  46. }
  47. // Handle postchk action
  48. if ($act == "postchk") {
  49. if (isset($_POST['chkbox']) && is_array($_POST['chkbox'])) {
  50. $ids = array_map('intval', $_POST['chkbox']);
  51. $idList = implode(',', $ids);
  52. $chkact = $_POST['chkact'] ?? '';
  53. if ($chkact == "0" || $chkact == "1") {
  54. $sql = "UPDATE power SET powerstate = " . (int)$chkact . " WHERE id IN (" . $idList . ")";
  55. } else {
  56. $sql = "DELETE FROM power WHERE id IN (" . $idList . ")";
  57. }
  58. $conn->query($sql);
  59. }
  60. header("Location: ?Keys=$keys&Ord=$ord&Page=$page");
  61. exit;
  62. }
  63. // Now start HTML output
  64. ?>
  65. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  66. <html xmlns="http://www.w3.org/1999/xhtml">
  67. <head>
  68. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  69. <title>管理区域</title>
  70. <link rel="stylesheet" href="css/common.css" type="text/css" />
  71. <script language="javascript" src="js/jquery-1.7.2.min.js"></script>
  72. <script type="text/javascript" src="js/js.js"></script>
  73. </head>
  74. <body>
  75. <div id="man_zone">
  76. <?php
  77. if ($act == "add" || $act == "edit") {
  78. $id = $_GET['id'] ?? '';
  79. $isedit = false;
  80. $powername = '';
  81. $powercontent = '';
  82. if ($id != "" && is_numeric($id)) {
  83. $isedit = true;
  84. $id = (int)$id;
  85. $sql = "SELECT powername, powercontent FROM power WHERE id = " . $id;
  86. $result = $conn->query($sql);
  87. if ($row = $result->fetch_assoc()) {
  88. $powername = textUncode($row['powername']);
  89. $powercontent = $row['powercontent'];
  90. } else {
  91. $isedit = false;
  92. }
  93. }
  94. $hrefstr = "?keys=$keys&Ord=$ord&Page=$page";
  95. ?>
  96. <form name="form1" method="post" action="<?php echo $hrefstr; ?>&act=save">
  97. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  98. <tbody>
  99. <tr>
  100. <th width="8%">权限名称</th>
  101. <td><input type="text" id="powername" name="powername" value="<?php echo $powername; ?>" class="txt1" /><input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
  102. </tr>
  103. <tr>
  104. <th width="8%">可用权限</th>
  105. <td>
  106. <fieldset>
  107. <legend>信息管理</legend>
  108. <input type="checkbox" id="powerchk1" name="powercontent[]" value="单页管理" <?php if (strpos($powercontent, "单页管理") !== false) echo 'checked="checked"'; ?> /><label for="powerchk1">单页管理</label>
  109. <input type="checkbox" id="powerchk2" name="powercontent[]" value="分类管理" <?php if (strpos($powercontent, "分类管理") !== false) echo 'checked="checked"'; ?> /><label for="powerchk2">分类管理</label>
  110. <input type="checkbox" id="powerchk3" name="powercontent[]" value="专题管理" <?php if (strpos($powercontent, "专题管理") !== false) echo 'checked="checked"'; ?> /><label for="powerchk3">专题管理</label>
  111. <input type="checkbox" id="powerchk4" name="powercontent[]" value="信息管理" <?php if (strpos($powercontent, "信息管理") !== false) echo 'checked="checked"'; ?> /><label for="powerchk4">信息管理</label><br />
  112. </fieldset>
  113. <fieldset>
  114. <legend>售后管理</legend>
  115. <input type="checkbox" id="powerchk21" name="powercontent[]" value="售后_单页" <?php if (strpos($powercontent, "售后_单页") !== false) echo 'checked="checked"'; ?> /><label for="powerchk21">单页管理</label>
  116. <input type="checkbox" id="powerchk22" name="powercontent[]" value="售后_信息" <?php if (strpos($powercontent, "售后_信息") !== false) echo 'checked="checked"'; ?> /><label for="powerchk22">信息管理</label><br />
  117. </fieldset>
  118. </td>
  119. </tr>
  120. <tr>
  121. <th></th>
  122. <td><input type="submit" name="save" id="save" value="确定" class="btn1" /> <input type="reset" name="save" id="save" value="重置" class="btn1" /> <input type="button" value="返回" class="btn1" onClick="location.href='<?php echo $hrefstr; ?>'" /></td>
  123. </tr>
  124. </tbody>
  125. </table>
  126. </form>
  127. <?php
  128. exit;
  129. }
  130. $keyscode = textEncode($keys);
  131. $orderby = "id DESC";
  132. if (in_array($ord, ['powername', 'powername Desc', 'powerstate', 'powerstate Desc', 'powercontent', 'powercontent Desc'])) {
  133. $orderby = "$ord, id DESC";
  134. }
  135. $sql = "SELECT id, powername, powercontent, powerstate FROM power
  136. WHERE id <> 1 AND (powername LIKE '%" . $conn->real_escape_string($keyscode) . "%'
  137. OR powercontent LIKE '%" . $conn->real_escape_string($keyscode) . "%')
  138. ORDER BY $orderby";
  139. $result = $conn->query($sql);
  140. $total_records = $result->num_rows;
  141. $records_per_page = 10;
  142. $total_pages = ceil($total_records / $records_per_page);
  143. if ($page == "") $page = 1;
  144. if ($page == "end") $page = $total_pages;
  145. if (!is_numeric($page) || $page < 1) $page = 1;
  146. $page = (int)$page;
  147. if ($page > $total_pages) $page = $total_pages;
  148. $start = ($page - 1) * $records_per_page;
  149. $sql .= " LIMIT $start, $records_per_page";
  150. $result = $conn->query($sql);
  151. $hrefstr = "?keys=$keys";
  152. ?>
  153. <form id="form1" method="post" action="?act=postchk&Keys=<?php echo $keys; ?>&Ord=<?php echo $ord; ?>&Page=<?php echo $page; ?>" onSubmit="return false">
  154. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  155. <thead>
  156. <tr>
  157. <th width="4%"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></th>
  158. <th width="6%">序号</th>
  159. <th width="20%"><a href="<?php echo $hrefstr; ?><?php echo ($ord=="powername" ? "&ord=powername+Desc" : "&ord=powername"); ?>">权限名称<?php echo ($ord=="powername" ? "↓" : ($ord=="powername+Desc" ? "↑" : "")); ?></a></th>
  160. <th width="54%"><a href="<?php echo $hrefstr; ?><?php echo ($ord=="powercontent" ? "&ord=powercontent+Desc" : "&ord=powercontent"); ?>">可用权限<?php echo ($ord=="powercontent" ? "↓" : ($ord=="powercontent+Desc" ? "↑" : "")); ?></a></th>
  161. <th width="8%"><a href="<?php echo $hrefstr; ?><?php echo ($ord=="powerstate" ? "&ord=powerstate+Desc" : "&ord=powerstate"); ?>">状态<?php echo ($ord=="powerstate" ? "↓" : ($ord=="powerstate+Desc" ? "↑" : "")); ?></a></th>
  162. <th width="8%">操作</th>
  163. </tr>
  164. </thead>
  165. <tbody>
  166. <?php
  167. if ($result->num_rows > 0) {
  168. $tempNum = ($page - 1) * $records_per_page;
  169. while ($row = $result->fetch_assoc()) {
  170. $tempNum++;
  171. ?>
  172. <tr onMouseOver="this.style.background='#F7FCFF'" onMouseOut="this.style.background='#FFFFFF'">
  173. <td align="center"><input type="checkbox" name="chkbox[]" value="<?php echo $row['id']; ?>" /></td>
  174. <td align="center"><?php echo $tempNum; ?></td>
  175. <td align="center"><?php echo $row['powername']; ?></td>
  176. <td align="center"><?php echo $row['powercontent']; ?></td>
  177. <td align="center"><?php echo ($row['powerstate'] == 1 ? "正常" : "停用"); ?></td>
  178. <td align="center"><a href="?Keys=<?php echo $keys; ?>&Ord=<?php echo $ord; ?>&Page=<?php echo $page; ?>&act=edit&id=<?php echo $row['id']; ?>" class="ico_edit">修改</a></td>
  179. </tr>
  180. <?php
  181. }
  182. } else {
  183. if ($keys == "") {
  184. ?>
  185. <tr>
  186. <td align="center" colspan="7">Sorry,当前暂无权限</td>
  187. </tr>
  188. <?php
  189. } else {
  190. ?>
  191. <tr>
  192. <td align="center" colspan="7"><a href="?">Sorry,没有找到"<?php echo $keyscode; ?>"相关的权限,点击返回</a></td>
  193. </tr>
  194. <?php
  195. }
  196. }
  197. ?>
  198. </tbody>
  199. <tfoot>
  200. <tr>
  201. <td colspan="7">
  202. <div class="showpagebox">
  203. <?php
  204. if ($total_pages > 1) {
  205. $pageName = "?Keys=$keys&Ord=$ord&";
  206. $pagelen = 3;
  207. if ($page > 1) {
  208. echo "<a href=\"{$pageName}Page=1\">首页</a>";
  209. echo "<a href=\"{$pageName}Page=" . ($page-1) . "\">上一页</a>";
  210. }
  211. if ($pagelen * 2 + 1 >= $total_pages) {
  212. $startPage = 1;
  213. $endPage = $total_pages;
  214. } else {
  215. if ($page <= $pagelen + 1) {
  216. $startPage = 1;
  217. $endPage = $pagelen * 2 + 1;
  218. } else {
  219. $startPage = $page - $pagelen;
  220. $endPage = $page + $pagelen;
  221. }
  222. if ($page + $pagelen > $total_pages) {
  223. $startPage = $total_pages - $pagelen * 2;
  224. $endPage = $total_pages;
  225. }
  226. }
  227. for ($i = $startPage; $i <= $endPage; $i++) {
  228. if ($i == $page) {
  229. echo "<a class=\"current\">$i</a>";
  230. } else {
  231. echo "<a href=\"{$pageName}Page=$i\">$i</a>";
  232. }
  233. }
  234. if ($page < $total_pages) {
  235. if ($total_pages - $page > $pagelen) {
  236. echo "<a href=\"{$pageName}Page=$total_pages\">...$total_pages</a>";
  237. }
  238. echo "<a href=\"{$pageName}Page=" . ($page+1) . "\">下一页</a>";
  239. echo "<a href=\"{$pageName}Page=$total_pages\">尾页</a>";
  240. }
  241. echo "<input type=\"text\" id=\"Pagego\" value=\"$page\" onFocus=\"if(this.value == '$page'){this.value='';};\" onBlur=\"if(this.value == ''){this.value='$page';}\" onKeyUp=\"this.value=this.value.replace(/\D/g,'')\" onKeyDown=\"if(event.keyCode==13){location.href='{$pageName}Page='+document.getElementById('Pagego').value}\" />";
  242. }
  243. ?>
  244. </div>
  245. <div class="searchbox">
  246. <input type="text" id="keys" value="<?php echo ($keyscode == "") ? "请输入搜索关键词" : $keyscode; ?>"
  247. onFocus="if(this.value == '<?php echo ($keyscode == "") ? "请输入搜索关键词" : $keyscode; ?>'){this.value='';}"
  248. onBlur="if(this.value == ''){this.value='<?php echo ($keyscode == "") ? "请输入搜索关键词" : $keyscode; ?>';}"
  249. onKeyDown="if(event.keyCode==13){location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)}" />
  250. <input type="button" id="searchgo" value="go" onClick="location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)" />
  251. </div>
  252. <div class="postchkbox">
  253. <select id="chkact" name="chkact">
  254. <option value="1">正常</option>
  255. <option value="0">停用</option>
  256. <option value="-1">删除</option>
  257. </select>
  258. <input type="button" value="执行" onClick="postchk(2)" class="btn1" />
  259. </div>
  260. </td>
  261. </tr>
  262. </tfoot>
  263. </table>
  264. </form>
  265. </div>
  266. </body>
  267. </html>