flea_region.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class flea_regionControl extends SystemControl{
  7. public function __construct(){
  8. parent::__construct();
  9. Language::read('region,flea_index');
  10. if ($GLOBALS['setting_config']['flea_isuse'] != 1 ){
  11. showMessage(Language::get('flea_index_unable'),'index.php?act=dashboard&op=welcome');
  12. // showMessage(Language::get('admin_ztc_unavailable'),'index.php?act=dashboard&op=welcome');
  13. }
  14. }
  15. /**
  16. * 地区列表
  17. *
  18. * @param
  19. * @return
  20. */
  21. public function flea_regionOp(){
  22. require_once(BASE_DATA_PATH.DS.'cache'.DS.'flea_cache.php');
  23. $lang = Language::getLangContent();
  24. /**
  25. * 实例化模型
  26. */
  27. $model_area = Model('flea_area');
  28. /**
  29. * 增加 修改 地区信息
  30. */
  31. if ($_POST['form_submit'] == 'ok'){
  32. /**
  33. * 是否生成缓存的标识
  34. */
  35. $new_cache = true;
  36. /**
  37. * 新增地区
  38. */
  39. if (is_array($_POST['new_area_name'])){
  40. foreach ($_POST['new_area_name'] as $k => $v){
  41. if (!empty($v)){
  42. $insert_array = array();
  43. $insert_array['flea_area_name'] = $v;
  44. $insert_array['flea_area_parent_id'] = $_POST['flea_area_parent_id'];
  45. $insert_array['flea_area_sort'] = intval($_POST['new_area_sort'][$k]);
  46. $insert_array['flea_area_deep'] = $_POST['child_area_deep'];
  47. $model_area->add($insert_array);
  48. $new_cache = true;
  49. }
  50. }
  51. }
  52. /**
  53. * 修改地区
  54. */
  55. if (is_array($_POST['area_name'])){
  56. foreach ($_POST['area_name'] as $k => $v){
  57. if (!empty($v)){
  58. $insert_array = array();
  59. $insert_array['flea_area_id'] = $k;
  60. $insert_array['flea_area_name'] = $v;
  61. $insert_array['flea_area_sort'] = intval($_POST['area_sort'][$k]);
  62. $model_area->update($insert_array);
  63. $new_cache = true;
  64. }
  65. }
  66. }
  67. /**
  68. * 删除地区
  69. */
  70. if (!empty($_POST['hidden_del_id'])){
  71. $_POST['hidden_del_id'] = trim($_POST['hidden_del_id'],'|');
  72. $del_id = explode('|',$_POST['hidden_del_id']);
  73. $model_area->del($del_id,$_POST['child_area_deep']);
  74. $new_cache = true;
  75. }
  76. /**
  77. * 更新缓存
  78. */
  79. if ($new_cache === true){
  80. flea_Cache::getCache('flea_area',array('deep'=>$_POST['child_area_deep'],'new'=>'1'));
  81. }
  82. showMessage($lang['region_index_modify_succ']);
  83. }
  84. /**
  85. * 导航地区内容
  86. */
  87. /**
  88. * 一级
  89. */
  90. $province_list = flea_Cache::getCache('flea_area',array('deep'=>'1'));
  91. $child_area_deep = 1;
  92. /**
  93. * 二级
  94. */
  95. if(!empty($_GET['province'])){
  96. $cache_data = flea_Cache::getCache('flea_area',array('deep'=>'2'));
  97. if (is_array($cache_data)){
  98. $city_list = array();
  99. foreach ($cache_data as $k => $v){
  100. if ($v['flea_area_parent_id'] == intval($_GET['province'])){
  101. $city_list[] = $v;
  102. }
  103. }
  104. }
  105. unset($cache_data);
  106. $child_area_deep = 2;
  107. /**
  108. * 三级
  109. */
  110. if(!empty($_GET['city'])){
  111. $cache_data = flea_Cache::getCache('flea_area',array('deep'=>'3'));
  112. if (is_array($cache_data)){
  113. $district_list = array();
  114. foreach ($cache_data as $k => $v){
  115. if ($v['flea_area_parent_id'] == intval($_GET['city'])){
  116. $district_list[] = $v;
  117. }
  118. }
  119. }
  120. unset($cache_data);
  121. $child_area_deep = 3;
  122. /**
  123. * 四级
  124. */
  125. if(!empty($_GET['district'])){
  126. $child_area_deep = 4;
  127. }
  128. }
  129. }
  130. /**
  131. * 地区列表
  132. */
  133. $condition['flea_area_parent_id'] = $_GET['flea_area_parent_id']?$_GET['flea_area_parent_id']:'0';
  134. $area_list = $model_area->getListArea($condition,'flea_area_sort asc');
  135. Tpl::output('province',$_GET['province']?$_GET['province']:'');
  136. Tpl::output('city',$_GET['city']);
  137. Tpl::output('district',$_GET['district']);
  138. Tpl::output('province_list',$province_list);
  139. Tpl::output('city_list',$city_list);
  140. Tpl::output('district_list',$district_list);
  141. Tpl::output('flea_area_parent_id',$_GET['flea_area_parent_id']?$_GET['flea_area_parent_id']:'0');
  142. Tpl::output('area_list',$area_list);
  143. Tpl::output('child_area_deep',$child_area_deep);
  144. Tpl::showpage('flea_region.index');
  145. }
  146. /**
  147. * 导入地区
  148. *
  149. * @param
  150. * @return
  151. */
  152. public function flea_region_importOp(){
  153. require_once(BASE_DATA_PATH.DS.'cache'.DS.'flea_cache.php');
  154. $lang = Language::getLangContent();
  155. /**
  156. * 实例化模型
  157. */
  158. $model_area = Model('flea_area');
  159. /**
  160. * 导入
  161. */
  162. if ($_POST['form_submit'] == 'ok'){
  163. if (!empty($_FILES['csv'])){
  164. $fp = @fopen($_FILES['csv']['tmp_name'],'rb');
  165. /**
  166. * 父ID
  167. */
  168. $area_parent_id_1 = 0;
  169. while (!feof($fp)) {
  170. $data = fgets($fp, 4096);
  171. switch (strtoupper($_POST['charset'])){
  172. case 'UTF-8':
  173. if (strtoupper(CHARSET) !== 'UTF-8'){
  174. $data = iconv('UTF-8',strtoupper(CHARSET),$data);
  175. }
  176. break;
  177. case 'GBK':
  178. if (strtoupper(CHARSET) !== 'GBK'){
  179. $data = iconv('GBK',strtoupper(CHARSET),$data);
  180. }
  181. break;
  182. }
  183. if (!empty($data)){
  184. $data = str_replace('"','',$data);
  185. /**
  186. * 逗号去除
  187. */
  188. $tmp_array = array();
  189. $tmp_array = explode(',',$data);
  190. /**
  191. * 第一位是序号,后面的是内容,最后一位名称
  192. */
  193. $tmp_deep = 'flea_area_parent_id_'.(count($tmp_array)-1);
  194. $insert_array = array();
  195. $insert_array['flea_area_sort'] = $tmp_array[0];
  196. $insert_array['flea_area_parent_id'] = $$tmp_deep;
  197. $insert_array['flea_area_name'] = $tmp_array[count($tmp_array)-1];
  198. $insert_array['flea_area_deep'] = count($tmp_array)-1;
  199. $area_id = $model_area->add($insert_array);
  200. /**
  201. * 赋值这个深度父ID
  202. */
  203. $tmp = 'flea_area_parent_id_'.count($tmp_array);
  204. $$tmp = $area_id;
  205. }
  206. }
  207. /**
  208. * 重新生成缓存
  209. */
  210. for ($i=1;$i<=4;$i++){
  211. $tmp = 'flea_area_parent_id_'.$i;
  212. if (intval($$tmp) >= 0){
  213. flea_Cache::getCache('flea_area',array('deep'=>intval($i),'new'=>'1'));
  214. }
  215. }
  216. showMessage($lang['region_import_succ'],'index.php?act=flea_region&op=flea_region');
  217. }else {
  218. showMessage($lang['region_import_csv_null']);
  219. }
  220. }
  221. Tpl::showpage('flea_region.import');
  222. }
  223. /**
  224. * 导入默认地区
  225. *
  226. * @param
  227. * @return
  228. */
  229. public function flea_import_default_areaOp() {
  230. $lang = Language::getLangContent();
  231. $file = BASE_DATA_PATH.'/resource/examples/flea_area.sql';
  232. if (!is_file($file)){
  233. showMessage($lang['region_import_csv_null']);
  234. }
  235. $handle = @fopen($file, "r");
  236. $tmp_sql = '';
  237. if ($handle) {
  238. Db::query("TRUNCATE TABLE `".DBPRE."flea_area`");
  239. while (!feof($handle)) {
  240. $buffer = fgets($handle);
  241. if (trim($buffer) != ''){
  242. $tmp_sql .= $buffer;
  243. if (substr(rtrim($buffer),-1) == ';'){
  244. if (preg_match('/^(INSERT)\s+(INTO)\s+/i', ltrim($tmp_sql)) && substr(rtrim($buffer),-2) == ');'){
  245. //标准的SQL语句,将被执行
  246. }else{
  247. //不能组成标准的SQL语句,继续向下一行取内容,直到组成合法的SQL为止
  248. continue;
  249. }
  250. if (!empty($tmp_sql)){
  251. if (strtoupper(CHARSET) == 'GBK'){
  252. $tmp_sql = iconv('UTF-8',strtoupper(CHARSET),$tmp_sql);
  253. }
  254. $tmp_sql = str_replace("`33hao_flea_area`","`".DBPRE."flea_area`",$tmp_sql);
  255. Db::query($tmp_sql);
  256. unset($tmp_sql);
  257. }
  258. }
  259. }
  260. }
  261. @fclose($handle);
  262. /**
  263. * 重新生成缓存
  264. */
  265. require_once(BASE_DATA_PATH.DS.'cache'.DS.'flea_cache.php');
  266. for ($i=1;$i<=4;$i++){
  267. $tmp = 'flea_area_parent_id_'.$i;
  268. if (intval($$tmp) >= 0){
  269. flea_Cache::getCache('flea_area',array('deep'=>intval($i),'new'=>'1'));
  270. }
  271. }
  272. showMessage($lang['region_import_succ'],'index.php?act=flea_region&op=flea_region');
  273. }else {
  274. showMessage($lang['region_import_csv_null']);
  275. }
  276. }
  277. }