tableAction.php 3.4 KB

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