tableAction.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class tableClassAction extends Action
  3. {
  4. public function initAction()
  5. {
  6. if($this->getsession('isadmin')!='1')backmsg('别乱来');
  7. if(getconfig('systype')=='demo')backmsg('演示的不要改');
  8. }
  9. public function tablebefore($table)
  10. {
  11. $key = $this->post('key');
  12. $where = 'and `TABLE_SCHEMA`=\''.DB_BASE.'\'';
  13. if($key!='')$where.=" and (`TABLE_NAME` like '%$key%' or `TABLE_COMMENT` like '%$key%')";
  14. return array(
  15. 'table' => 'information_schema.`TABLES`',
  16. 'fields'=> '`TABLE_NAME` as id,`ENGINE` as `engine`,`TABLE_ROWS` as `rows`,`TABLE_COMMENT` as `explain`,`CREATE_TIME` as `cjsj`,`UPDATE_TIME` as `gxsj`,`TABLE_COLLATION`',
  17. 'where' => $where
  18. );
  19. }
  20. //保存表备注
  21. public function tablesmAjax()
  22. {
  23. $id = $this->post('id');
  24. $value = $this->post('value');
  25. $sql = "ALTER TABLE `$id` COMMENT '$value';";
  26. $this->db->query($sql);
  27. return 'success';
  28. }
  29. public function tablefieldsAjax()
  30. {
  31. $table = $this->post('table');
  32. $rows = $this->db->gettablefields($table);
  33. foreach($rows as $k=>$rs)$rows[$k]['id']=$rs['name'];
  34. $arr['rows'] = $rows;
  35. $this->returnjson($arr);
  36. }
  37. public function savefieldsAjax()
  38. {
  39. $table = $this->post('table');
  40. $allfields = $this->db->getallfields($table);
  41. $name = strtolower($this->post('name'));
  42. if(c('check')->isincn($name))backmsg('字段名不能有中文');
  43. $type = $this->post('type');
  44. $dev = $this->post('dev');
  45. $isnull = $this->post('isnull');
  46. if($table=='' || $name=='' || $type=='')backmsg('hehe');
  47. $lens = $this->post('lens');
  48. $sm = $this->post('explain');
  49. $sql = "ALTER TABLE `$table`";
  50. if(!in_array($name, $allfields)){
  51. $sql.=' ADD';
  52. }else{
  53. $sql.=' MODIFY';
  54. }
  55. $sql.=" `$name`";
  56. $cew = '[varchar][mediumint][int][bigint][smallint][tinyint][decimal]';
  57. if(contain($cew,'['.$type.']')){
  58. if($lens=='0')$lens='10';
  59. $sql.=" $type($lens)";
  60. }else{
  61. $sql.=" $type";
  62. }
  63. if($isnull=='NO')$sql.=' NOT NULL';
  64. if($dev==''){
  65. //$sql.=' DEFAULT NULL';
  66. }else{
  67. $sql.=" DEFAULT '$dev'";
  68. }
  69. $sql.=" COMMENT '$sm'";
  70. $bo = $this->db->query($sql);
  71. $msg = '';
  72. if(!$bo)$msg='错误《'.$sql.'》';
  73. backmsg($msg);
  74. }
  75. public function delfieldsAjax()
  76. {
  77. $table = $this->post('table');
  78. $id = $this->post('id');
  79. $sql = "ALTER table `$table` DROP COLUMN `$id`;";
  80. $msg = '';
  81. $bo = $this->db->query($sql);
  82. if(!$bo)$msg='错误《'.$sql.'》';
  83. backmsg($msg);
  84. }
  85. public function tablerecord_before()
  86. {
  87. $stable = $this->post('stable','', 1);
  88. $key = $this->post('key');
  89. $fid = $this->post('fields','id');
  90. $this->nowtablename = $stable;
  91. $where = '';
  92. if(!isempt($key))$where=" and `$fid`='$key'";
  93. return array(
  94. 'table' => $stable,
  95. 'order' => 'id desc',
  96. 'where' => $where
  97. );
  98. }
  99. public function tablerecord_after($table, $rows)
  100. {
  101. $fieldsar = array();
  102. if($this->loadci==1){
  103. $fieldsarr = $this->db->gettablefields($this->nowtablename);
  104. foreach($fieldsarr as $k1=>$rs1){
  105. $sortable = in_array($rs1['type'], array('int','date','datetime','tinyint','smallint','decimal'));
  106. $text = $rs1['name'];
  107. if(!isempt($rs1['explain']))$text.='('.$rs1['explain'].')';
  108. $fieldsar[] = array(
  109. 'text' => $text,
  110. 'dataIndex' => $rs1['name'],
  111. 'sortable' => $sortable
  112. );
  113. }
  114. }
  115. return array(
  116. 'fieldsarr' => $fieldsar
  117. );
  118. }
  119. }
粤ICP备19079148号