member_model.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * 管理员后台会员模型操作类
  4. */
  5. defined('IN_PHPCMS') or exit('No permission resources.');
  6. pc_base::load_app_class('admin', 'admin', 0);
  7. pc_base::load_sys_class('form', '', 0);
  8. //模型原型存储路径
  9. define('MODEL_PATH',PC_PATH.'modules'.DIRECTORY_SEPARATOR.'member'.DIRECTORY_SEPARATOR.'fields'.DIRECTORY_SEPARATOR);
  10. //模型缓存路径
  11. define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
  12. class member_model extends admin {
  13. private $db;
  14. function __construct() {
  15. parent::__construct();
  16. $this->db = pc_base::load_model('sitemodel_model');
  17. }
  18. /**
  19. * 会员模型列表
  20. */
  21. function manage() {
  22. $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
  23. $member_model_list = $this->db->listinfo(array('type'=>2, 'siteid'=>$this->get_siteid()), 'sort', $page, 10);
  24. $pages = $this->db->pages;
  25. $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=member&c=member_model&a=add\', title:\''.L('add_model').'\', 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('member_model_add'));
  26. include $this->admin_tpl('member_model_list');
  27. }
  28. /**
  29. * 添加会员模型
  30. */
  31. function add() {
  32. if(isset($_POST['dosubmit'])) {
  33. $info = array();
  34. $info['name'] = $_POST['info']['modelname'];
  35. $info['tablename'] = 'member_'.$_POST['info']['tablename'];
  36. $info['description'] = $_POST['info']['description'];
  37. $info['type'] = 2;
  38. $info['siteid'] = $this->get_siteid();
  39. if(!empty($_FILES['model_import']['tmp_name'])) {
  40. $model_import = @file_get_contents($_FILES['model_import']['tmp_name']);
  41. if(!empty($model_import)) {
  42. $model_import_data = string2array($model_import);
  43. }
  44. }
  45. $is_exists = $this->db->table_exists($info['tablename']);
  46. if($is_exists) showmessage(L('operation_failure'),'?m=member&c=member_model&a=manage', '', 'add');
  47. $modelid = $this->db->insert($info, 1);
  48. if($modelid) {
  49. $model_sql = file_get_contents(MODEL_PATH.'model.sql');
  50. $tablepre = $this->db->db_tablepre;
  51. $tablename = $info['tablename'];
  52. $model_sql = str_replace('$tablename', $tablepre.$tablename, $model_sql);
  53. $this->db->sql_execute($model_sql);
  54. if(!empty($model_import_data)) {
  55. $this->sitemodel_field_db = pc_base::load_model('sitemodel_field_model');
  56. $tablename = $tablepre.$tablename;
  57. foreach($model_import_data as $v) {
  58. //修改模型表字段
  59. $field = $v['field'];
  60. $minlength = $v['minlength'] ? $v['minlength'] : 0;
  61. $maxlength = $v['maxlength'] ? $v['maxlength'] : 0;
  62. $field_type = $v['formtype'];
  63. require MODEL_PATH.$field_type.DIRECTORY_SEPARATOR.'config.inc.php';
  64. if(isset($v['setting']['fieldtype'])) {
  65. $field_type = $v['setting']['fieldtype'];
  66. }
  67. require MODEL_PATH.'add.sql.php';
  68. $v['setting'] = array2string($v['setting']);
  69. $v['modelid'] = $modelid;
  70. unset($v['fieldid']);
  71. $fieldid = $this->sitemodel_field_db->insert($v, 1);
  72. }
  73. }
  74. //更新模型缓存
  75. pc_base::load_app_class('member_cache','','');
  76. member_cache::update_cache_model();
  77. showmessage(L('operation_success'),'?m=member&c=member_model&a=manage', '', 'add');
  78. } else {
  79. showmessage(L('operation_failue'),'?m=member&c=member_model&a=manage', '', 'add');
  80. }
  81. } else {
  82. $show_header = $show_scroll = true;
  83. include $this->admin_tpl('member_model_add');
  84. }
  85. }
  86. /**
  87. * 修改会员模型
  88. */
  89. function edit() {
  90. if(isset($_POST['dosubmit'])) {
  91. $modelid = isset($_POST['info']['modelid']) ? $_POST['info']['modelid'] :showmessage(L('operation_success'),'?m=member&c=member_model&a=manage', '', 'edit');
  92. $info['name'] = $_POST['info']['modelname'];
  93. $info['disabled'] = $_POST['info']['disabled'] ? 1 : 0;
  94. $info['description'] = $_POST['info']['description'];
  95. $this->db->update($info, array('modelid'=>$modelid));
  96. //更新模型缓存
  97. pc_base::load_app_class('member_cache','','');
  98. member_cache::update_cache_model();
  99. showmessage(L('operation_success'),'?m=member&c=member_model&a=manage', '', 'edit');
  100. } else {
  101. $show_header = $show_scroll = true;
  102. $modelinfo = $this->db->get_one(array('modelid'=>$_GET['modelid']));
  103. include $this->admin_tpl('member_model_edit');
  104. }
  105. }
  106. /**
  107. * 删除会员模型
  108. */
  109. function delete() {
  110. $modelidarr = isset($_POST['modelid']) ? $_POST['modelid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
  111. $newmodelarr = '';
  112. foreach ($modelidarr as $modelarr) {
  113. if($modelarr!=10) {
  114. $newmodelarr[] = intval($modelarr);
  115. }
  116. }
  117. $where = to_sqls($newmodelarr, '', 'modelid');
  118. $modelinfo = $this->db->select($where);
  119. foreach($modelinfo as $v) {
  120. $this->db->drop_table($v['tablename']);
  121. }
  122. if ($this->db->delete($where)) {
  123. //删除模型字段
  124. $this->sitemodel_field_db = pc_base::load_model('sitemodel_field_model');
  125. $this->sitemodel_field_db->delete($where);
  126. //修改用户模型组为普通会员
  127. $this->member_db = pc_base::load_model('members_model');
  128. $this->member_db->update(array('modelid'=>10), $where);
  129. //更新模型缓存
  130. pc_base::load_app_class('member_cache','','');
  131. member_cache::update_cache_model();
  132. showmessage(L('operation_success'), HTTP_REFERER);
  133. } else {
  134. showmessage(L('operation_failure'), HTTP_REFERER);
  135. }
  136. }
  137. /**
  138. * 导入会员模型
  139. */
  140. function import(){
  141. include $this->admin_tpl('member_model_import');
  142. }
  143. /**
  144. * 导出会员模型
  145. */
  146. function export() {
  147. $modelid = isset($_GET['modelid']) ? $_GET['modelid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
  148. $modelarr = getcache('member_model', 'commons');
  149. $this->sitemodel_field_db = pc_base::load_model('sitemodel_field_model');
  150. $modelinfo = $this->sitemodel_field_db->select(array('modelid'=>$modelid));
  151. foreach($modelinfo as $k=>$v) {
  152. $modelinfoarr[$k] = $v;
  153. $modelinfoarr[$k]['setting'] = string2array($v['setting']);
  154. }
  155. $res = var_export($modelinfoarr, TRUE);
  156. header('Content-Disposition: attachment; filename="'.$modelarr[$modelid]['tablename'].'.model"');
  157. echo $res;exit;
  158. }
  159. /**
  160. * 修改会员模型
  161. */
  162. function move() {
  163. if(isset($_POST['dosubmit'])) {
  164. $from_modelid = isset($_POST['from_modelid']) ? $_POST['from_modelid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
  165. $to_modelid = !empty($_POST['to_modelid']) && $_POST['to_modelid'] != $from_modelid ? $_POST['to_modelid'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
  166. //更新会员表modelid
  167. $this->db->change_member_modelid($from_modelid, $to_modelid);
  168. showmessage(L('member_move').L('operation_success'), HTTP_REFERER, '', 'move');
  169. } else {
  170. $show_header = $show_scroll = true;
  171. $modelarr = $this->db->select(array('type'=>2));
  172. foreach($modelarr as $v) {
  173. $modellist[$v['modelid']] = $v['name'];
  174. }
  175. include $this->admin_tpl('member_model_move');
  176. }
  177. }
  178. /**
  179. * 排序会员模型
  180. */
  181. function sort() {
  182. if(isset($_POST['sort'])) {
  183. foreach($_POST['sort'] as $k=>$v) {
  184. $this->db->update(array('sort'=>$v), array('modelid'=>$k));
  185. }
  186. //更新模型缓存
  187. pc_base::load_app_class('member_cache','','');
  188. member_cache::update_cache_model();
  189. showmessage(L('operation_success'), HTTP_REFERER);
  190. } else {
  191. showmessage(L('operation_failure'), HTTP_REFERER);
  192. }
  193. }
  194. /**
  195. * 检查模型名称
  196. * @param string $username 模型名
  197. * @return $status {0:模型名已经存在 ;1:成功}
  198. */
  199. public function public_checkmodelname_ajax() {
  200. $modelname = isset($_GET['modelname']) ? trim($_GET['modelname']) : exit('0');
  201. if(CHARSET != 'utf-8') {
  202. $modelname = iconv('utf-8', CHARSET, $modelname);
  203. $modelname = addslashes($modelname);
  204. }
  205. $oldmodelname = $_GET['oldmodelname'];
  206. if($modelname==$oldmodelname) exit('1');
  207. $status = $this->db->get_one(array('name'=>$modelname));
  208. if($status) {
  209. exit('0');
  210. } else {
  211. exit('1');
  212. }
  213. }
  214. /**
  215. * 检查模型表是否存在
  216. * @param string $username 模型名
  217. * @return $status {0:模型表名已经存在 ;1:成功}
  218. */
  219. public function public_checktablename_ajax() {
  220. $tablename = isset($_GET['tablename']) ? trim($_GET['tablename']) : exit('0');
  221. $status = $this->db->table_exists('member_'.$tablename);
  222. if($status) {
  223. exit('0');
  224. } else {
  225. exit('1');
  226. }
  227. }
  228. }
  229. ?>