data.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin', 'admin', 0);
  4. class data extends admin {
  5. private $db;
  6. public function __construct() {
  7. $this->db = pc_base::load_model('datacall_model');
  8. parent::__construct();
  9. }
  10. public function init() {
  11. $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=dbsource&c=data&a=add\', title:\''.L('adding_data_source_call').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('adding_data_source_call'));
  12. $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
  13. $list = $this->db->listinfo('','id desc', $page, 20);
  14. $pages = $this->db->pages;
  15. include $this->admin_tpl('data_list');
  16. }
  17. public function add() {
  18. pc_base::load_app_func('global');
  19. if (isset($_POST['dosubmit'])) {
  20. $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage(L('name').L('empty'));
  21. $dis_type = isset($_POST['dis_type']) && intval($_POST['dis_type']) ? intval($_POST['dis_type']) : 1;
  22. $cache = isset($_POST['cache']) && intval($_POST['cache']) ? intval($_POST['cache']) : 0;
  23. $num = isset($_POST['num']) && intval($_POST['num']) ? intval($_POST['num']) : 0;
  24. $type = isset($_POST['type']) && intval($_POST['type']) ? intval($_POST['type']) : 0;
  25. //检查名称是否已经存在
  26. if ($this->db->get_one(array('name'=>$name))) {
  27. showmessage(L('name').L('exists'));
  28. }
  29. $sql = array();
  30. if ($type == '1') { //自定义SQL
  31. $data = isset($_POST['data']) && trim($_POST['data']) ? trim($_POST['data']) : showmessage(L('custom_sql').L('empty'));
  32. $sql = array('data'=>$data);
  33. } else { //模型配置方式
  34. $module = isset($_POST['module']) && trim($_POST['module']) ? trim($_POST['module']) : showmessage(L('please_select_model'));
  35. $action = isset($_POST['action']) && trim($_POST['action']) ? trim($_POST['action']) : showmessage(L('please_select_action'));
  36. $html = pc_tag_class($module);
  37. $data = array();
  38. if (isset($html[$action]) && is_array($html[$action])) {
  39. foreach ($html[$action] as $key=>$val) {
  40. $val['validator']['reg_msg'] = $val['validator']['reg_msg'] ? $val['validator']['reg_msg'] : $val['name'].L('inputerror');
  41. $$key = isset($_POST[$key]) && trim($_POST[$key]) ? trim($_POST[$key]) : '';
  42. if (!empty($val['validator'])) {
  43. if (isset($val['validator']['min']) && strlen($$key) < $val['validator']['min']) {
  44. showmessage($val['name'].L('should').L('is_greater_than').$val['validator']['min'].L('lambda'));
  45. }
  46. if (isset($val['validator']['max']) && strlen($$key) > $val['validator']['max']) {
  47. showmessage($val['name'].L('should').L('less_than').$val['validator']['max'].L('lambda'));
  48. }
  49. if (!preg_match('/'.$val['validator']['reg'].'/'.$val['validator']['reg_param'], $$key)) {
  50. showmessage($val['name'].$val['validator']['reg_msg']);
  51. }
  52. }
  53. $data[$key] = $$key;
  54. }
  55. }
  56. $sql = array('data'=>array2string($data), 'module'=>$module, 'action'=>$action);
  57. }
  58. if ($dis_type == 3) {
  59. $sql['template'] = isset($_POST['template']) && trim($_POST['template']) ? trim($_POST['template']) : '';
  60. }
  61. //初始化数据
  62. $sql['name'] = $name;
  63. $sql['type'] = $type;
  64. $sql['dis_type'] = $dis_type;
  65. $sql['cache'] = $cache;
  66. $sql['num'] = $num;
  67. if ($id = $this->db->insert($sql,true)) {
  68. //当为JS时,输出模板文件
  69. if ($dis_type == 3) {
  70. $tpl = pc_base::load_sys_class('template_cache');
  71. $str = $this->db->get_one(array('id'=>$id), 'template');
  72. $str = $tpl->template_parse($str['template']);
  73. $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.'dbsource'.DIRECTORY_SEPARATOR;
  74. if(!is_dir($filepath)) {
  75. mkdir($filepath, 0777, true);
  76. }
  77. @file_put_contents($filepath.$id.'.php', $str);
  78. }
  79. showmessage('', '', '', 'add');
  80. } else {
  81. showmessage(L('operation_failure'));
  82. }
  83. } else {
  84. pc_base::load_sys_class('form','',0);
  85. $modules = array_merge(array(''=>L('please_select')),pc_base::load_config('modules'));
  86. $show_header = $show_validator = true;
  87. $type = isset($_GET['type']) && intval($_GET['type']) ? intval($_GET['type']) : 0;
  88. $module = isset($_GET['module']) && trim($_GET['module']) ? trim($_GET['module']) : '';
  89. $action = isset($_GET['action']) && trim($_GET['action']) ? trim($_GET['action']) : '';
  90. if ($module) $html = pc_tag_class($module);
  91. pc_base::load_app_func('global','template');
  92. include $this->admin_tpl('data_add');
  93. }
  94. }
  95. public function edit() {
  96. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage(L('illegal_parameters'), HTTP_REFERER);
  97. if (!$edit_data = $this->db->get_one(array('id'=>$id))) {
  98. showmessage(L('notfound'));
  99. }
  100. pc_base::load_app_func('global');
  101. if (isset($_POST['dosubmit'])) {
  102. $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage(L('name').L('empty'));
  103. $dis_type = isset($_POST['dis_type']) && intval($_POST['dis_type']) ? intval($_POST['dis_type']) : 1;
  104. $cache = isset($_POST['cache']) && intval($_POST['cache']) ? intval($_POST['cache']) : 0;
  105. $num = isset($_POST['num']) && intval($_POST['num']) ? intval($_POST['num']) : 0;
  106. $type = isset($_POST['type']) && intval($_POST['type']) ? intval($_POST['type']) : 0;
  107. //检查名称是否已经存在
  108. if ($edit_data['name'] != $name) {
  109. if ($this->db->get_one(array('name'=>$name), 'id')) {
  110. showmessage(L('name').L('exists'));
  111. }
  112. }
  113. $sql = array();
  114. if ($type == '1') { //自定义SQL
  115. $data = isset($_POST['data']) && trim($_POST['data']) ? trim($_POST['data']) : showmessage(L('custom_sql').L('empty'));
  116. $sql = array('data'=>$data);
  117. } else { //模型配置方式
  118. $module = isset($_POST['module']) && trim($_POST['module']) ? trim($_POST['module']) : showmessage(L('please_select_model'));
  119. $action = isset($_POST['action']) && trim($_POST['action']) ? trim($_POST['action']) : showmessage(L('please_select_action'));
  120. $html = pc_tag_class($module);
  121. $data = array();
  122. if (isset($html[$action]) && is_array($html[$action])) {
  123. foreach ($html[$action] as $key=>$val) {
  124. $val['validator']['reg_msg'] = $val['validator']['reg_msg'] ? $val['validator']['reg_msg'] : $val['name'].L('inputerror');
  125. $$key = isset($_POST[$key]) && trim($_POST[$key]) ? trim($_POST[$key]) : '';
  126. if (!empty($val['validator'])) {
  127. if (isset($val['validator']['min']) && strlen($$key) < $val['validator']['min']) {
  128. showmessage($val['name'].L('should').L('is_greater_than').$val['validator']['min'].L('lambda'));
  129. }
  130. if (isset($val['validator']['max']) && strlen($$key) > $val['validator']['max']) {
  131. showmessage($val['name'].L('should').L('less_than').$val['validator']['max'].L('lambda'));
  132. }
  133. if (!preg_match('/'.$val['validator']['reg'].'/'.$val['validator']['reg_param'], $$key)) {
  134. showmessage($val['name'].$val['validator']['reg_msg']);
  135. }
  136. }
  137. $data[$key] = $$key;
  138. }
  139. }
  140. $sql = array('data'=>array2string($data), 'module'=>$module, 'action'=>$action);
  141. }
  142. if ($dis_type == 3) {
  143. $sql['template'] = isset($_POST['template']) && trim($_POST['template']) ? trim($_POST['template']) : '';
  144. }
  145. //初始化数据
  146. $sql['name'] = $name;
  147. $sql['type'] = $type;
  148. $sql['dis_type'] = $dis_type;
  149. $sql['cache'] = $cache;
  150. $sql['num'] = $num;
  151. if ($this->db->update($sql,array('id'=>$id))) {
  152. //当为JS时,输出模板文件
  153. if ($dis_type == 3) {
  154. $tpl = pc_base::load_sys_class('template_cache');
  155. $str = $this->db->get_one(array('id'=>$id), 'template');
  156. $str = $tpl->template_parse($str['template']);
  157. $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.'dbsource'.DIRECTORY_SEPARATOR;
  158. if(!is_dir($filepath)) {
  159. mkdir($filepath, 0777, true);
  160. }
  161. @file_put_contents($filepath.$id.'.php', $str);
  162. }
  163. showmessage('', '', '', 'edit');
  164. } else {
  165. showmessage(L('operation_failure'));
  166. }
  167. } else {
  168. pc_base::load_sys_class('form','',0);
  169. $modules = array_merge(array(''=>L('please_select')),pc_base::load_config('modules'));
  170. $show_header = $show_validator = true;
  171. $type = isset($_GET['type']) ? intval($_GET['type']) : $edit_data['type'];
  172. $module = isset($_GET['module']) && trim($_GET['module']) ? trim($_GET['module']) : $edit_data['module'];
  173. $action = isset($_GET['action']) && trim($_GET['action']) ? trim($_GET['action']) : $edit_data['action'];
  174. if ($edit_data['type'] == 0) $form_data = string2array($edit_data['data']);
  175. if ($module) $html = pc_tag_class($module);
  176. pc_base::load_app_func('global','template');
  177. include $this->admin_tpl('data_edit');
  178. }
  179. }
  180. public function del() {
  181. $id = isset($_GET['id']) ? $_GET['id'] : '';
  182. if (is_array($id)) {
  183. foreach ($id as $key => $v) {
  184. if (intval($v)) {
  185. $id[$key] = intval($v);
  186. } else {
  187. unset($id[$key]);
  188. }
  189. }
  190. $sql = implode('\',\'', $id);
  191. $this->db->delete("id in ('$sql')");
  192. showmessage(L('operation_success'), HTTP_REFERER);
  193. } else {
  194. $id = intval($id);
  195. if(empty($id)) showmessage(L('illegal_parameters'), HTTP_REFERER);
  196. if ($this->db->delete(array('id'=>$id))) {
  197. showmessage(L('operation_success'), HTTP_REFERER);
  198. } else {
  199. showmessage(L('operation_failure'), HTTP_REFERER);
  200. }
  201. }
  202. }
  203. public function public_name() {
  204. $name = isset($_GET['name']) && trim($_GET['name']) ? (pc_base::load_config('system', 'charset') == 'gbk' ? iconv('utf-8', 'gbk', trim($_GET['name'])) : trim($_GET['name'])) : exit('0');
  205. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';
  206. $data = array();
  207. if ($id) {
  208. $data = $this->db->get_one(array('id'=>$id), 'name');
  209. if (!empty($data) && $data['name'] == $name) {
  210. exit('1');
  211. }
  212. }
  213. if ($this->db->get_one(array('name'=>$name), 'id')) {
  214. exit('0');
  215. } else {
  216. exit('1');
  217. }
  218. }
  219. }