tableAction.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. public function tableafter($table, $rows)
  22. {
  23. return array(
  24. 'dbupurl' => m('option')->getval('dbupurl')
  25. );
  26. }
  27. //保存表备注
  28. public function tablesmAjax()
  29. {
  30. $id = $this->post('id');
  31. $value = $this->post('value');
  32. $sql = "ALTER TABLE `$id` COMMENT '$value';";
  33. $this->db->query($sql);
  34. return 'success';
  35. }
  36. public function tablefieldsAjax()
  37. {
  38. $table = $this->post('table');
  39. $rows = $this->db->gettablefields($table);
  40. foreach($rows as $k=>$rs)$rows[$k]['id']=$rs['name'];
  41. $arr['rows'] = $rows;
  42. $this->returnjson($arr);
  43. }
  44. public function savefieldsAjax()
  45. {
  46. $table = $this->post('table');
  47. $allfields = $this->db->getallfields($table);
  48. $name = strtolower($this->post('name'));
  49. if(c('check')->isincn($name))backmsg('字段名不能有中文');
  50. $type = $this->post('type');
  51. $dev = $this->post('dev');
  52. $isnull = $this->post('isnull');
  53. if($table=='' || $name=='' || $type=='')backmsg('hehe');
  54. $lens = $this->post('lens');
  55. $sm = $this->post('explain');
  56. $sql = "ALTER TABLE `$table`";
  57. if(!in_array($name, $allfields)){
  58. $sql.=' ADD';
  59. }else{
  60. $sql.=' MODIFY';
  61. }
  62. $sql.=" `$name`";
  63. $cew = '[varchar][mediumint][int][bigint][smallint][tinyint][decimal]';
  64. if(contain($cew,'['.$type.']')){
  65. if($lens=='0')$lens='10';
  66. $sql.=" $type($lens)";
  67. }else{
  68. $sql.=" $type";
  69. }
  70. if($isnull=='NO')$sql.=' NOT NULL';
  71. if($dev==''){
  72. //$sql.=' DEFAULT NULL';
  73. }else{
  74. $sql.=" DEFAULT '$dev'";
  75. }
  76. $sql.=" COMMENT '$sm'";
  77. $bo = $this->db->query($sql);
  78. $msg = '';
  79. if(!$bo)$msg='错误《'.$sql.'》';
  80. backmsg($msg);
  81. }
  82. public function delfieldsAjax()
  83. {
  84. $table = $this->post('table');
  85. $id = $this->post('id');
  86. $sql = "ALTER table `$table` DROP COLUMN `$id`;";
  87. $msg = '';
  88. $bo = $this->db->query($sql);
  89. if(!$bo)$msg='错误《'.$sql.'》';
  90. backmsg($msg);
  91. }
  92. public function tablerecord_before()
  93. {
  94. $stable = $this->post('stable','', 1);
  95. $key = $this->post('key');
  96. $fid = $this->post('fields','id');
  97. $this->nowtablename = $stable;
  98. $where = '';
  99. if(!isempt($key))$where=" and `$fid`='$key'";
  100. return array(
  101. 'table' => $stable,
  102. 'order' => 'id desc',
  103. 'where' => $where
  104. );
  105. }
  106. public function tablerecord_after($table, $rows)
  107. {
  108. $fieldsar = array();
  109. if($this->loadci==1){
  110. $fieldsarr = $this->db->gettablefields($this->nowtablename);
  111. foreach($fieldsarr as $k1=>$rs1){
  112. $sortable = in_array($rs1['type'], array('int','date','datetime','tinyint','smallint','decimal'));
  113. $text = $rs1['name'];
  114. if(!isempt($rs1['explain']))$text.='('.$rs1['explain'].')';
  115. $fieldsar[] = array(
  116. 'text' => $text,
  117. 'dataIndex' => $rs1['name'],
  118. 'sortable' => $sortable
  119. );
  120. }
  121. }
  122. foreach($rows as $k=>$rs){
  123. foreach($rs as $k1=>$v1){
  124. if($v1===null){
  125. $rows[$k][$k1]='NULL';
  126. $rows[$k][''.$k1.'_color']='#aaaaaa';
  127. }
  128. }
  129. }
  130. return array(
  131. 'fieldsarr' => $fieldsar,
  132. 'rows' => $rows,
  133. );
  134. }
  135. public function savedbupurlAjax()
  136. {
  137. $dz = $this->get('dz');
  138. $dz = $this->jm->base64decode($dz);
  139. m('option')->setval('dbupurl', $dz);
  140. return 'ok';
  141. }
  142. public function dbupdateAjax()
  143. {
  144. $url = m('option')->getval('dbupurl');
  145. if(!$url)return returnerror('未设置更新地址');
  146. if(substr($url,0,4)!='http')return returnerror('更新地址有问题');
  147. $tab = $this->get('tab');
  148. $tab = str_replace(PREFIX,'', $tab);
  149. $url .= 'api.php?m=login&a=dbinfo&tab='.$tab.'&xinhukey='.getconfig('xinhukey').'';
  150. $result = c('curl')->getcurl($url);
  151. if(substr($result, 0, 1)!='{')return returnsuccess($result);
  152. $msg = m('beifen')->updatefabric($result);
  153. if($msg=='ok'){
  154. return returnsuccess('已更新');
  155. }else{
  156. return returnsuccess($msg);
  157. }
  158. }
  159. }
粤ICP备19079148号