rec_position.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * 推荐位
  4. *
  5. *
  6. *
  7. ***/
  8. defined('InShopNC') or exit('Access Invalid!');
  9. class rec_positionControl extends SystemControl{
  10. public function __construct(){
  11. parent::__construct();
  12. Language::read('rec_position');
  13. }
  14. /**
  15. * 推荐位列表
  16. *
  17. */
  18. public function rec_listOp(){
  19. $model = model('rec_position');
  20. //删除推荐位
  21. if (chksubmit()){
  22. $condition = array('rec_id'=>array('in',$_POST['rec_id']));
  23. $list = $model->where($condition)->select();
  24. if (!$list) showMessage(Language::get('param_error'));
  25. $result = $model->where($condition)->delete();
  26. if ($result){
  27. foreach ($list as $info){
  28. $info['content'] = unserialize($info['content']);
  29. if ($info['pic_type'] == 1 && is_array($info['content']['body'])){
  30. foreach ($info['content']['body'] as $v){
  31. $file = BASE_UPLOAD_PATH.'/'.$v['title'];
  32. if (is_file($file)) @unlink($file);
  33. }
  34. }
  35. dkcache("rec_position/{$info['rec_id']}");
  36. }
  37. $this->log(L('nc_del,rec_position').'['.implode(',',$_POST['rec_id']).']',1);
  38. }else{
  39. showMessage(Language::get('nc_common_del_fail'));
  40. }
  41. }
  42. $condition = array();
  43. if ($_GET['pic_type'] == '0'){
  44. $condition['pic_type'] = 0;
  45. }elseif($_GET['pic_type'] == 1){
  46. $condition['pic_type'] = array('in','1,2');
  47. }
  48. if (!empty($_GET['keywords'])){
  49. $condition['title'] = array('like','%'.$_GET['keywords'].'%');
  50. }
  51. $list = $model->where($condition)->order('rec_id desc')->page(10)->select();
  52. foreach ((array)$list as $k=>$v){
  53. $list[$k]['content'] = unserialize($v['content']);
  54. if ($v['pic_type'] == 1){
  55. $list[$k]['content']['body'][0]['title'] = UPLOAD_SITE_URL.'/'.$list[$k]['content']['body'][0]['title'];
  56. }
  57. }
  58. Tpl::output('list',$list);
  59. Tpl::output('page',$model->showpage());
  60. Tpl::showpage('rec_position.index');
  61. }
  62. /**
  63. * 新增推荐位
  64. *
  65. */
  66. public function rec_addOp(){
  67. Tpl::showpage('rec_position.add');
  68. }
  69. /**
  70. * 编辑推荐位
  71. *
  72. */
  73. public function rec_editOp(){
  74. $model = Model('rec_position');
  75. $info = $model->where(array('rec_id'=>intval($_GET['rec_id'])))->find();
  76. if (!$info) showMessage(Language::get('no_record'));
  77. $info['content'] = unserialize($info['content']);
  78. foreach((array)$info['content']['body'] as $k=>$v){
  79. if ($info['pic_type'] == 1){
  80. $info['content']['body'][$k]['title'] = UPLOAD_SITE_URL.'/'.$v['title'];
  81. }
  82. }
  83. Tpl::output('info',$info);
  84. Tpl::showpage('rec_position.edit');
  85. }
  86. /**
  87. * 删除
  88. *
  89. */
  90. public function rec_delOp(){
  91. $model = Model('rec_position');
  92. $_GET['rec_id'] = intval($_GET['rec_id']);
  93. $info = $model->where(array('rec_id'=>$_GET['rec_id']))->find();
  94. if (!$info) showMessage(Language::get('no_record'));
  95. $info['content'] = unserialize($info['content']);
  96. $result = $model->where(array('rec_id'=>$_GET['rec_id']))->delete();
  97. if ($result){
  98. if ($info['pic_type'] == 1 && is_array($info['content']['body'])){
  99. foreach ($info['content']['body'] as $v){
  100. @unlink(BASE_UPLOAD_PATH.'/'.$v['title']);
  101. }
  102. }
  103. dkcache("rec_position/{$info['rec_id']}");
  104. $this->log(L('nc_del,rec_position').'[ID:'.$_GET['rec_id'].']',1);
  105. showMessage(Language::get('nc_common_save_succ'));
  106. }else{
  107. showMessage(Language::get('nc_common_save_fail'));
  108. }
  109. }
  110. /**
  111. * 添加保存推荐位
  112. *
  113. */
  114. public function rec_saveOp(){
  115. $array = array();
  116. $data = array();
  117. $pattern = "/^http:\/\/[A-Za-z0-9]+[A-Za-z0-9.]+\.[A-Za-z0-9]+/i";
  118. //文字类型
  119. if ($_POST['rec_type'] == 1){
  120. if (is_array($_POST['txt']) && is_array($_POST['urltxt'])){
  121. foreach ($_POST['txt'] as $k=>$v){
  122. if (trim($v) == '') continue;
  123. $c = count($array['body']);
  124. $array['body'][$c]['title'] = $v;
  125. $array['body'][$c]['url'] = preg_match($pattern,$_POST['urltxt'][$k]) ? $_POST['urltxt'][$k] : '';
  126. $data['pic_type'] = 0;
  127. }
  128. }else{
  129. showMessage(Language::get('param_error'));
  130. }
  131. }elseif ($_POST['rec_type'] == 2 && $_POST['pic_type'] == 1){
  132. //本地图片上传
  133. if (is_array($_FILES['pic']['tmp_name'])){
  134. foreach($_FILES['pic']['tmp_name'] as $k=>$v){
  135. if (empty($v)) continue;
  136. $ext = strtolower(pathinfo($_FILES['pic']['name'][$k], PATHINFO_EXTENSION));
  137. if (in_array($ext,array('jpg','jpeg','gif','png'))){
  138. $filename = substr(md5(microtime(true)),0,16).rand(100,999).$k.'.'.$ext;
  139. if ($_FILES['pic']['size'][$k]<1024*1024){
  140. move_uploaded_file($v,BASE_UPLOAD_PATH.'/'.ATTACH_REC_POSITION.'/'.$filename);
  141. }
  142. if ($_FILES['pic']['error'][$k] != 0) showMessage(Language::get('nc_common_op_fail'));
  143. $c = count($array['body']);
  144. $array['body'][$c]['title'] = ATTACH_REC_POSITION.'/'.$filename;
  145. $array['body'][$c]['url'] = preg_match($pattern,$_POST['urlup'][$k]) ? $_POST['urlup'][$k] : '';
  146. $array['width'] = is_numeric($_POST['rwidth']) ? $_POST['rwidth'] : '';
  147. $array['height'] = is_numeric($_POST['rheight']) ? $_POST['rheight'] : '';
  148. $data['pic_type'] = 1;
  149. }
  150. if (empty($array)) showMessage(Language::get('param_error'));
  151. }
  152. }
  153. }elseif ($_POST['rec_type'] == 2 && $_POST['pic_type'] == 2){
  154. //远程图片
  155. if (is_array($_POST['pic'])){
  156. foreach ($_POST['pic'] as $k=>$v){
  157. if (!preg_match("/^(http\:\/\/)/i",$v)) continue;
  158. $ext = strtolower(pathinfo($v, PATHINFO_EXTENSION));
  159. if (in_array($ext,array('jpg','jpeg','gif','png','bmp'))){
  160. $c = count($array['body']);
  161. $array['body'][$c]['title'] = $v;
  162. $array['body'][$c]['url'] = preg_match($pattern,$_POST['urlremote'][$k]) ? $_POST['urlremote'][$k] : '';
  163. $array['width'] = is_numeric($_POST['rwidth']) ? $_POST['rwidth'] : '';
  164. $array['height'] = is_numeric($_POST['rheight']) ? $_POST['rheight'] : '';
  165. $data['pic_type'] = 2;
  166. }
  167. if (empty($array)) showMessage(Language::get('param_error'));
  168. }
  169. }
  170. }else{
  171. showMessage(Language::get('param_error'));
  172. }
  173. $array['target'] = intval($_POST['rtarget']);
  174. $data['title'] = $_POST['rtitle'];
  175. $data['content'] = serialize($array);
  176. $model = Model('rec_position');
  177. $model->insert($data);
  178. $this->log(L('nc_add,rec_position').'['.$_POST['rtitle'].']',1);
  179. showMessage(Language::get('nc_common_save_succ'),'index.php?act=rec_position&op=rec_list');
  180. }
  181. /**
  182. * 编辑保存推荐位
  183. *
  184. */
  185. public function rec_edit_saveOp(){
  186. if (!is_numeric($_POST['rec_id'])) showMessage(Language::get('param_error'));
  187. $array = array();
  188. $data = array();
  189. $pattern = "/^http:\/\/[A-Za-z0-9]+[A-Za-z0-9.]+\.[A-Za-z0-9]+/i";
  190. //文字类型
  191. if ($_POST['rec_type'] == 1){
  192. if (is_array($_POST['txt']) && is_array($_POST['urltxt'])){
  193. foreach ($_POST['txt'] as $k=>$v){
  194. if (trim($v) == '') continue;
  195. $c = count($array['body']);
  196. $array['body'][$c]['title'] = $v;
  197. $array['body'][$c]['url'] = preg_match($pattern,$_POST['urltxt'][$k]) ? $_POST['urltxt'][$k] : '';
  198. $data['pic_type'] = 0;
  199. }
  200. }else{
  201. showMessage(Language::get('param_error'));
  202. }
  203. }elseif ($_POST['rec_type'] == 2 && $_POST['pic_type'] == 1){
  204. //本地图片上传
  205. if (is_array($_FILES['pic']['tmp_name'])){
  206. foreach($_FILES['pic']['tmp_name'] as $k=>$v){
  207. //未上传新图的,还用老图
  208. if (empty($v) && !empty($_POST['opic'][$k])){
  209. $array['body'][$k]['title'] = str_ireplace(UPLOAD_SITE_URL.'/','',$_POST['opic'][$k]);
  210. $array['body'][$k]['url'] = preg_match($pattern,$_POST['urlup'][$k]) ? $_POST['urlup'][$k] : '';
  211. }
  212. $ext = strtolower(pathinfo($_FILES['pic']['name'][$k], PATHINFO_EXTENSION));
  213. if (in_array($ext,array('jpg','jpeg','gif','png','bmp'))){
  214. $filename = substr(md5(microtime(true)),0,16).rand(100,999).$k.'.'.$ext;
  215. if ($_FILES['pic']['size'][$k]<1024*1024){
  216. move_uploaded_file($v,BASE_UPLOAD_PATH.'/'.ATTACH_REC_POSITION.'/'.$filename);
  217. }
  218. if ($_FILES['pic']['error'][$k] != 0) showMessage(Language::get('nc_common_save_fail'));
  219. //删除老图
  220. $old_file = str_ireplace(array(UPLOAD_SITE_URL,'..'),array(BASE_UPLOAD_PATH,''),$_POST['opic'][$k]);
  221. if (is_file($old_file)) @unlink($old_file);
  222. $array['body'][$k]['title'] = ATTACH_REC_POSITION.'/'.$filename;
  223. $array['body'][$k]['url'] = preg_match($pattern,$_POST['urlup'][$k]) ? $_POST['urlup'][$k] : '';
  224. $data['pic_type'] = 1;
  225. }
  226. }
  227. //最后删除数据库里有但没有POST过来的图片
  228. $model = Model('rec_position');
  229. $oinfo = $model->where(array('rec_id'=>$_POST['rec_id']))->find();
  230. $oinfo = unserialize($oinfo['content']);
  231. foreach ($oinfo['body'] as $k=>$v) {
  232. if (!in_array(UPLOAD_SITE_URL.'/'.$v['title'],(array)$_POST['opic'])){
  233. if (is_file(BASE_UPLOAD_PATH.'/'.$v['title'])){
  234. @unlink(BASE_UPLOAD_PATH.'/'.$v['title']);
  235. }
  236. }
  237. }
  238. unset($oinfo);
  239. }
  240. //如果是上传图片,则取原图片地址
  241. if (empty($array)){
  242. if (is_array($_POST['opic'])){
  243. foreach ($_POST['opic'] as $k=>$v){
  244. $array['body'][$k]['title'] = $v;
  245. $array['body'][$k]['url'] = preg_match($pattern,$_POST['urlup'][$k]) ? $_POST['urlup'][$k] : '';
  246. }
  247. }
  248. }
  249. }elseif ($_POST['rec_type'] == 2 && $_POST['pic_type'] == 2){
  250. //远程图片
  251. if (is_array($_POST['pic'])){
  252. foreach ($_POST['pic'] as $k=>$v){
  253. if (!preg_match("/^(http\:\/\/)/i",$v)) continue;
  254. $ext = strtolower(pathinfo($v, PATHINFO_EXTENSION));
  255. if (in_array($ext,array('jpg','jpeg','gif','png','bmp'))){
  256. $c = count($array['body']);
  257. $array['body'][$c]['title'] = $v;
  258. $array['body'][$c]['url'] = preg_match($pattern,$_POST['urlremote'][$k]) ? $_POST['urlremote'][$k] : '';
  259. $data['pic_type'] = 2;
  260. }
  261. }
  262. }
  263. }else{
  264. showMessage(Language::get('param_error'));
  265. }
  266. if ($_POST['rec_type'] != 1){
  267. $array['width'] = is_numeric($_POST['rwidth']) ? $_POST['rwidth'] : '';
  268. $array['height'] = is_numeric($_POST['rheight']) ? $_POST['rheight'] : '';
  269. }
  270. $array['target'] = intval($_POST['rtarget']);
  271. $data['title'] = $_POST['rtitle'];
  272. $data['content'] = serialize($array);
  273. $model = Model('rec_position');
  274. //如果是把本地上传类型改为文字或远程,则先取出原来上传的图片路径,待update成功后,再删除这些图片
  275. if ($_POST['opic_type'] == 1 && ($_POST['pic_type'] == 2 || $_POST['rec_type'] == 1)){
  276. $oinfo = $model->where(array('rec_id'=>$_POST['rec_id']))->find();
  277. $oinfo = unserialize($oinfo['content']);
  278. }
  279. $result = $model->where(array('rec_id'=>$_POST['rec_id']))->update($data);
  280. if ($result){
  281. if ($oinfo){
  282. foreach ($oinfo['body'] as $v){
  283. if (is_file(BASE_UPLOAD_PATH.'/'.$v['title'])){
  284. @unlink(BASE_UPLOAD_PATH.'/'.$v['title']);
  285. }
  286. }
  287. }
  288. dkcache("rec_position/{$_POST['rec_id']}");
  289. showMessage(Language::get('nc_common_save_succ'),'index.php?act=rec_position&op=rec_list');
  290. }else{
  291. showMessage(Language::get('nc_common_save_fail'),'index.php?act=rec_position&op=rec_list');
  292. }
  293. }
  294. public function rec_codeOp(){
  295. Tpl::showpage('rec_position.code','null_layout');
  296. }
  297. public function rec_viewOp(){
  298. @header("Content-type: text/html; charset=".CHARSET);
  299. echo rec(intval($_GET['rec_id']));
  300. }
  301. }