database.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * 提供data_controller类使用的接口
  4. */
  5. namespace controller\api;
  6. use \controller\base;
  7. class database extends base
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->load->model('table')->checkRuleTable();
  13. }
  14. /**
  15. * 保存creator
  16. */
  17. public function creatorAction()
  18. {
  19. $data = array();
  20. $database = $this->request->post('database');
  21. $tableName = $this->request->post('tableName');
  22. $rules = $this->request->post($tableName);
  23. if (!$rules) {
  24. $data['code'] = 1;
  25. echo $this->jsonEncode($data);
  26. return;
  27. }
  28. $result = $this->load->model('table')->saveRules($database, $tableName, $rules);
  29. if ($result) {
  30. $data['code'] = 0;
  31. } else {
  32. $data['code'] = 1;
  33. $data['error'] = $this->load->model('table')->tablesError;
  34. }
  35. $this->echoJson($data);
  36. }
  37. /**
  38. * 编辑详细的规则
  39. */
  40. public function rulesAction()
  41. {
  42. $data = array();
  43. $database = $this->request->post('database');
  44. $tableName = $this->request->post('tableName');
  45. $rules = $this->request->post('rules');
  46. if (!$rules) {
  47. $data['code'] = 1;
  48. echo $this->jsonEncode($data);
  49. return;
  50. }
  51. $result = $this->load->model('table')->updateRules($database, $tableName, $rules);
  52. if ($result) {
  53. $data['code'] = 0;
  54. } else {
  55. $data['code'] = 1;
  56. $data['error'] = $this->load->model('table')->tablesError;
  57. }
  58. $this->echoJson($data);
  59. }
  60. /**
  61. * 获取制定数据库的数据包
  62. */
  63. public function tableListsAction()
  64. {
  65. $tableName = $this->request->get('tableName', '');
  66. $this->echoJson($this->load->model('table')->getTableLists($this->load->model('table')->getUseDB(), $tableName));
  67. }
  68. /**
  69. * 获取自定表的字段
  70. */
  71. public function fieldsListsAction()
  72. {
  73. $tableName = $this->request->get('tableName');
  74. $this->echoJson($this->load->model('table')->getFieldsLists($this->load->model('table')->getUseDB(), $tableName));
  75. }
  76. /**
  77. * 删除数据表中的数据
  78. */
  79. public function tableAction()
  80. {
  81. $data = array();
  82. $database = $this->request->get('__database', $this->load->model('table')->getUseDB());
  83. $tableName = $this->request->get('__tableName', '');
  84. if (!$database || !$tableName) {
  85. $data['code'] = 1;
  86. $data['msg'] = '参数不全';
  87. echo $this->jsonEncode($data);
  88. return;
  89. }
  90. $pri = explode(',', $this->request->get('__pri'));
  91. $priVal = array();
  92. foreach ($pri as $val) {
  93. if (!$this->request->keyExists($_GET, $val)) {
  94. $data['code'] = 1;
  95. break;
  96. }
  97. $priVal[$val] = $this->request->get($val);
  98. }
  99. $result = $this->load->model('table')->removeTableData($database, $tableName, $priVal);
  100. if ($result) {
  101. $data['code'] = 0;
  102. } else {
  103. $data['code'] = 1;
  104. $data['error'] = $this->load->model('table')->getError() === false ? '删除数据失败' : $this->load->model('table')->getError();
  105. }
  106. $this->echoJson($data);
  107. }
  108. /**
  109. * 更新数据表中的数据
  110. */
  111. public function updateAction()
  112. {
  113. try {
  114. $data = array();
  115. $data['code'] = 0;
  116. $database = $this->request->post('__database');
  117. $tableName = $this->request->post('__tableName');
  118. $pri = explode(',', $this->request->post('__pri'));
  119. $fields = $this->request->post('fields');
  120. $priVal = array();
  121. foreach ($pri as $val) {
  122. if (!$this->request->keyExists($_POST, $val)) {
  123. $data['code'] = 1;
  124. break;
  125. }
  126. $priVal[$val] = $this->request->post($val);
  127. }
  128. if ($data['code'] != 1) {
  129. $result = $this->load->model('table')->updateTableData($database, $tableName, $priVal, $fields);
  130. if (!$result->isError()) {
  131. $data['code'] = 0;
  132. } else {
  133. $data['code'] = 1;
  134. $data['error'] = $result->getResult();
  135. }
  136. }
  137. } catch (Exception $e) {
  138. $data['code'] = 1;
  139. $data['msg'] = $e->getMessage();
  140. }
  141. $this->echoJson($data);
  142. }
  143. /**
  144. * 添加数据表中的数据
  145. */
  146. public function addAction()
  147. {
  148. try {
  149. $data = array();
  150. $data['code'] = 0;
  151. $database = $this->request->post('database');
  152. $tableName = $this->request->post('tableName');
  153. $fields = $this->request->post('fields');
  154. if ($data['code'] != 1) {
  155. $result = $this->load->model('table')->addTableData($database, $tableName, $fields);
  156. if ($result->getCode() == 0) {
  157. $data['code'] = 0;
  158. } else {
  159. $data['code'] = 1;
  160. $data['error'] = $result->getMessage();
  161. }
  162. }
  163. } catch (Exception $e) {
  164. $data['code'] = 1;
  165. $data['msg'] = $e->getMessage();
  166. }
  167. $this->echoJson($data);
  168. }
  169. /**
  170. * 下载数据表的配置文件
  171. * @author Jinhui Zhu<jinhui.zhu@live.cn> 2015-09-28 11:16
  172. */
  173. public function downloadConfigAction()
  174. {
  175. $database = $this->request->get('database');
  176. $tableName = $this->request->get('tableName');
  177. try {
  178. if (!$database || !$tableName) {
  179. $data['code'] = 1;
  180. $data['msg'] = '参数不全';
  181. throw new Exception($data['msg'], $data['code']);
  182. }
  183. $result = $this->load->model('table')->autoRules($database, $tableName);
  184. if ($result) {
  185. $tableStruct = $this->load->model('table')->getTableSQL($database, $tableName);
  186. $result['sql'] = '';
  187. if ($tableStruct['sql']) $result['sql'] = str_replace("'", "\'", $tableStruct['sql']);
  188. $downloadStr = "<?php\n\t return " . var_export($result, true) . ';';
  189. _loadClass('\Qii\Library\Download')->downloadByString($database . '.' . $tableName . '.config.php', $downloadStr);
  190. }
  191. } catch (Exception $e) {
  192. $this->showErrorPage($e->getMessage());
  193. }
  194. }
  195. /**
  196. * 获取数据表建表URL
  197. * @author Jinhui Zhu <jinhui.zhu@live.cn> 2015-09-28 12:03
  198. *
  199. */
  200. public function tableSQLAction()
  201. {
  202. $database = $this->request->get('database');
  203. $tableName = $this->request->get('tableName');
  204. $data = array();
  205. try {
  206. $data = $this->load->model('table')->getTableSQL($database, $tableName);
  207. $data['code'] = 0;
  208. } catch (Exception $e) {
  209. $data['code'] = 1;
  210. $data['msg'] = $e->getMessage();
  211. }
  212. $this->echoJson($data);
  213. }
  214. /**
  215. * 备份数据表及数据
  216. */
  217. public function backupAction()
  218. {
  219. $database = $this->request->get('database');
  220. $tableName = $this->request->get('tableName');
  221. try {
  222. $downloadStr = $this->load->model('table')->backupTable($database, $tableName);
  223. _loadClass('\Qii\Library\Download')->downloadByString($database . '.' . $tableName . '.sql', $downloadStr);
  224. } catch (Exception $e) {
  225. $data['code'] = 1;
  226. $data['msg'] = $e->getMessage();
  227. $this->echoJson($data);
  228. }
  229. }
  230. public function restoreAction()
  231. {
  232. $database = $this->request->post('database');
  233. $tableName = $this->request->post('tableName');
  234. if (!$database || !$tableName || !isset($_FILES['restoreSQL']) ||
  235. !isset($_FILES['restoreSQL']['tmp_name']) ||
  236. $_FILES['restoreSQL']['tmp_name'] == '' ||
  237. $_FILES['restoreSQL']['error'] != UPLOAD_ERR_OK
  238. ) {
  239. $data['code'] = 1;
  240. $data['msg'] = '参数或文件错误';
  241. $this->echoJson($data);
  242. return;
  243. }
  244. $fileName = $_FILES['restoreSQL']['tmp_name'];
  245. $data = $this->load->model('table')->restore($database, $tableName, $fileName);
  246. echo $this->jsonEncode($data);
  247. }
  248. public function creatBasicCodeAction()
  249. {
  250. $database = $this->request->get('database');
  251. $tableName = $this->request->get('tableName');
  252. try {
  253. if (!$database || !$tableName) {
  254. $data['code'] = 1;
  255. $data['msg'] = '参数或文件错误';
  256. throw new \Exception($data['msg'], $data['code']);
  257. }
  258. $rules = $this->load->model('table')->getRules($database, $tableName);
  259. if(!isset($rules['rules']))
  260. {
  261. $data['code'] = 1;
  262. $data['msg'] = '请先设置规则';
  263. throw new \Exception($data['msg'], $data['code']);
  264. }
  265. $privateKeys = '';
  266. if(isset($rules['rules']['pri']) && $rules['rules']['pri']) {
  267. $privateKeys = 'array(\'' . join('\', \'', array_keys($rules['rules']['pri'])) . '\')';
  268. }
  269. $this->view->assign('privateKeys', $privateKeys);
  270. $code = $this->load->model('code');
  271. $code->setDatabase($database);
  272. $code->setClass($tableName);
  273. $this->view->assign('code', $code->output());
  274. $sampleCode = $this->view->fetch('manage/data/code.html');
  275. _loadClass('\Qii\Library\Download')->downloadByString($tableName . '.php', $sampleCode);
  276. } catch (\Exception $e) {
  277. echo $e->getMessage();
  278. }
  279. }
  280. }