power.php 14 KB

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