space.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin','admin',0);
  4. pc_base::load_sys_class('form', '', 0);
  5. class space extends admin {
  6. private $M, $db;
  7. function __construct() {
  8. parent::__construct();
  9. $setting = new_html_special_chars(getcache('poster', 'commons'));
  10. $this->M = $setting[$this->get_siteid()];
  11. $this->db = pc_base::load_model('poster_space_model');
  12. }
  13. public function init() {
  14. $TYPES = $this->template_type();
  15. $page = max(intval($_GET['page']), 1);
  16. $infos = $this->db->listinfo(array('siteid'=>$this->get_siteid()), '`spaceid`', $page);
  17. $pages = $this->db->pages;
  18. $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=poster&c=space&a=add\', title:\''.L('add_space').'\', width:\'540\', height:\'320\'}, 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('add_space'));
  19. include $this->admin_tpl('space_list');
  20. }
  21. /**
  22. * 添加广告版块
  23. */
  24. public function add() {
  25. if (isset($_POST['dosubmit'])) {
  26. $space = $this->check($_POST['space']);
  27. $space['setting'] = array2string($_POST['setting']);
  28. $space['siteid'] = $this->get_siteid();
  29. $spaceid = $this->db->insert($space, true);
  30. if ($spaceid) {
  31. if ($space['type']=='code') {
  32. $path = '{show_ad('.$space['siteid'].', '.$spaceid.')}';
  33. } else {
  34. $path = 'poster_js/'.$spaceid.'.js';
  35. }
  36. $this->db->update(array('path'=>$path), array('siteid'=>$this->get_siteid(), 'spaceid'=>$spaceid));
  37. showmessage(L('added_successful'), '?m=poster&c=space', '', 'add');
  38. }
  39. } else {
  40. $TYPES = $this->template_type();
  41. $poster_template = getcache('poster_template_'.$this->get_siteid(), 'commons');
  42. $show_header = $show_validator = true;
  43. include $this->admin_tpl('space_add');
  44. }
  45. }
  46. /**
  47. * 编辑广告版位
  48. */
  49. public function edit() {
  50. $_GET['spaceid'] = intval($_GET['spaceid']);
  51. if (!$_GET['spaceid']) showmessage(L('illegal_operation'), HTTP_REFERER);
  52. if (isset($_POST['dosubmit'])) {
  53. $space = $this->check($_POST['space']);
  54. $space['setting'] = array2string($_POST['setting']);
  55. if ($space['type']=='code') {
  56. $space['path'] = '{show_ad('.$this->get_siteid().', '.$_GET['spaceid'].')}';
  57. } else {
  58. $space['path'] = 'poster_js/'.$_GET['spaceid'].'.js';
  59. }
  60. if (isset($_POST['old_type']) && $_POST['old_type']!=$space['type']) {
  61. $poster_db = pc_base::load_model('poster_model');
  62. $poster_db->delete(array('spaceid'=>$_GET['spaceid']));
  63. $space['items'] = 0;
  64. }
  65. if ($this->db->update($space, array('spaceid'=>$_GET['spaceid']))) showmessage(L('edited_successful'), '?m=poster&c=space', '', 'testIframe'.$_GET['spaceid']);
  66. } else {
  67. $info = $this->db->get_one(array('spaceid' => $_GET['spaceid']));
  68. $setting = string2array($info['setting']);
  69. $TYPES = $this->template_type();
  70. $poster_template = getcache('poster_template_'.$this->get_siteid(), 'commons');
  71. $show_header = $show_validator = true;
  72. include $this->admin_tpl('space_edit');
  73. }
  74. }
  75. /**
  76. * 广告版位调用代码
  77. */
  78. public function public_call() {
  79. $_GET['sid'] = intval($_GET['sid']);
  80. if (!$_GET['sid']) showmessage(L('illegal_action'), HTTP_REFERER, '', 'call');
  81. $r = $this->db->get_one(array('spaceid'=>$_GET['sid'], 'siteid'=>$this->get_siteid()));
  82. include $this->admin_tpl('space_call');
  83. }
  84. /**
  85. * 广告预览
  86. */
  87. public function public_preview() {
  88. if (is_numeric($_GET['spaceid'])) {
  89. $_GET['spaceid'] = intval($_GET['spaceid']);
  90. $r = $this->db->get_one(array('spaceid'=>$_GET['spaceid'], 'siteid'=>$this->get_siteid()));
  91. $scheme = $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  92. if ($r['type']=='code') {
  93. $db = pc_base::load_model('poster_model');
  94. $rs = $db->get_one(array('spaceid'=>$r['spaceid'], 'siteid'=>$this->get_siteid()), 'setting', '`id` ASC');
  95. if ($rs['setting']) {
  96. $d = string2array($rs['setting']);
  97. $data = $d['code'];
  98. }
  99. } else {
  100. $path = APP_PATH.'caches/'.$r['path'];
  101. }
  102. include $this->admin_tpl('space_preview');
  103. }
  104. }
  105. private function template_type() {
  106. pc_base::load_app_func('global','poster');
  107. return get_types();
  108. }
  109. /**
  110. * 删除广告版位
  111. * @param intval $sid 广告版位的ID,当批量删除时系统会递归删除
  112. */
  113. public function delete() {
  114. if ((!isset($_GET['spaceid']) || empty($_GET['spaceid'])) && (!isset($_POST['spaceid']) || empty($_POST['spaceid']))) {
  115. showmessage(L('illegal_parameters'), HTTP_REFERER);
  116. } else {
  117. if (is_array($_POST['spaceid'])) {
  118. array_map(array($this, _del), $_POST['spaceid']); //如果是批量操作,则递归数组
  119. } elseif($_GET['spaceid']) {
  120. $_GET['spaceid'] = intval($_GET['spaceid']);
  121. $db = pc_base::load_model('poster_model');
  122. $db->delete(array('siteid'=>$this->get_siteid(), 'spaceid'=>$_GET['spaceid']));
  123. $this->db->delete(array('siteid'=>$this->get_siteid(), 'spaceid' => $_GET['spaceid']));
  124. }
  125. showmessage(L('operation_success'), HTTP_REFERER);
  126. }
  127. }
  128. /**
  129. * 广告位删除
  130. * @param intval $spaceid 专题ID
  131. */
  132. private function _del($spaceid = 0) {
  133. $spaceid = intval($spaceid);
  134. if (!$spaceid) return false;
  135. $db = pc_base::load_model('poster_model');
  136. $db->delete(array('siteid'=>$this->get_siteid(), 'spaceid'=>$spaceid));
  137. $this->db->delete(array('siteid'=>$this->get_siteid(), 'spaceid' => $spaceid));
  138. return true;
  139. }
  140. /**
  141. * 广告模块配置
  142. */
  143. public function setting() {
  144. if (isset($_POST['dosubmit'])) {
  145. $setting = getcache('poster', 'commons');
  146. $setting[$this->get_siteid()] = $_POST['setting'];
  147. setcache('poster', $setting, 'commons'); //设置缓存
  148. $m_db = pc_base::load_model('module_model'); //调用模块数据模型
  149. $setting = array2string($_POST['setting']);
  150. $m_db->update(array('setting'=>$setting), array('module'=>ROUTE_M)); //将配置信息存入数据表中
  151. showmessage(L('setting_updates_successful'), HTTP_REFERER, '', 'setting');
  152. } else {
  153. @extract($this->M);
  154. include $this->admin_tpl('setting');
  155. }
  156. }
  157. /**
  158. * 配置模板
  159. */
  160. public function poster_template() {
  161. $tpl_root = pc_base::load_config('system', 'tpl_root');
  162. $templatedir = PC_PATH.$tpl_root.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR.'poster'.DIRECTORY_SEPARATOR;
  163. $poster_template = getcache('poster_template_'.get_siteid(), 'commons');
  164. $templates = glob($templatedir.'*.html');
  165. if (is_array($templates) && !empty($templates)) {
  166. foreach ($templates as $k => $tem) {
  167. $templates[$k] = basename($tem, ".html");
  168. }
  169. }
  170. $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=poster&c=space&a=add\', title:\''.L('add_space').'\', width:\'540\', height:\'320\'}, 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('add_space'));
  171. include $this->admin_tpl('poster_template');
  172. }
  173. /**
  174. * 删除模板配置
  175. */
  176. public function public_tempate_del() {
  177. if (!isset($_GET['id'])) showmessage(L('illegal_parameters'), HTTP_REFERER);
  178. $siteid = $this->get_siteid();
  179. $poster_template = getcache('poster_template_'.$siteid, 'commons');
  180. if ($poster_template[$_GET['id']]) {
  181. unset($poster_template[$_GET['id']]);
  182. }
  183. setcache('poster_template_'.$siteid, $poster_template, 'commons');
  184. showmessage(L('operation_success'), HTTP_REFERER);
  185. }
  186. /**
  187. * 配置模板
  188. */
  189. public function public_tempate_setting() {
  190. $siteid = $this->get_siteid();
  191. $poster_template = getcache('poster_template_'.$siteid, 'commons');
  192. if (isset($_POST['dosubmit'])) {
  193. if (is_array($_POST['info']['type']) && !empty($_POST['info']['type'])) {
  194. $type2name = array('images'=>L('photo'), 'flash'=>L('flash'), 'text'=>L('title'));
  195. $type = array();
  196. foreach ($_POST['info']['type'] as $t) {
  197. if (in_array($t, array('images', 'flash', 'text'))) {
  198. $type[$t] = $type2name[$t];
  199. } else {
  200. continue;
  201. }
  202. }
  203. }
  204. unset($_POST['info']['type']);
  205. $_POST['info']['type'] = $type;
  206. $poster_template[$_POST['template']] = $_POST['info'];
  207. setcache('poster_template_'.$siteid, $poster_template, 'commons');
  208. showmessage(L('setting_success'), '', '', 'testIframe');
  209. } else {
  210. if (!isset($_GET['template'])) {
  211. showmessage(L('illegal_parameters'));
  212. } else {
  213. $template = $_GET['template'];
  214. }
  215. if ($poster_template[$template]) {
  216. $info = $poster_template[$template];
  217. if (is_array($info['type']) && !empty($info['type'])) {
  218. $type = array();
  219. $type = array_keys($info['type']);
  220. unset($info['type']);
  221. $info['type'] = $type;
  222. }
  223. }
  224. include $this->admin_tpl('template_setting');
  225. }
  226. }
  227. /**
  228. * 更新js
  229. */
  230. public function create_js($page = 0) {
  231. $page = max(intval($_GET['page']), 1);
  232. if ($page==1) {
  233. $result = $this->db->get_one(array('disabled'=>0, 'siteid'=>get_siteid()), 'COUNT(*) AS num');
  234. if ($result['num']) {
  235. $total = $result['num'];
  236. $pages = ceil($total/20);
  237. }
  238. } else {
  239. $pages = $_GET['pages'] ? intval($_GET['pages']) : 0;
  240. }
  241. $offset = ($page-1)*20;
  242. $data = $this->db->listinfo(array('disabled'=>0, 'siteid'=>get_siteid()), 'spaceid ASC', $page);
  243. $html = pc_base::load_app_class('html');
  244. foreach ($data as $d) {
  245. if ($d['type']!='code') {
  246. $html->create_js($d['spaceid']);
  247. } else {
  248. continue;
  249. }
  250. }
  251. $page++;
  252. if ($page>$pages) {
  253. showmessage(L('update_js_success'), '?m=poster&c=space&a=init');
  254. } else {
  255. showmessage(L('update_js').'<font style="color:red">'.($page-1).'/'.$pages.'</font>', '?m=poster&c=space&a=create_js&page='.$page.'&pages='.$pages);
  256. }
  257. }
  258. /**
  259. * 检测版位名称是否存在
  260. */
  261. public function public_check_space() {
  262. if (!$_GET['name']) exit(0);
  263. if (pc_base::load_config('system', 'charset')=='gbk') {
  264. $_GET['name'] = iconv('UTF-8', 'GBK', $_GET['name']);
  265. }
  266. $name = $_GET['name'];
  267. if ($_GET['spaceid']) {
  268. $spaceid = intval($_GET['spaceid']);
  269. $r = $this->db->get_one(array('spaceid' => $spaceid, 'siteid'=>$this->get_siteid()));
  270. if ($r['name'] == $name) {
  271. exit('1');
  272. }
  273. }
  274. $r = $this->db->get_one(array('siteid' => $this->get_siteid(), 'name' => $name), 'spaceid');
  275. if ($r['spaceid']) {
  276. exit('0');
  277. } else {
  278. exit('1');
  279. }
  280. }
  281. /**
  282. * 检查表单数据
  283. * @param Array $data 表单传递过来的数组
  284. * @return Array 检查后的数组
  285. */
  286. private function check($data = array()) {
  287. if ($data['name'] == '') showmessage(L('name_plates_not_empty'));
  288. $info = $this->db->get_one(array('name'=>$data['name'], 'siteid'=>$this->get_siteid()), 'spaceid');
  289. if (($info['spaceid'] && $info['spaceid']!=$_GET['spaceid']) || ($info['spaceid'] && !isset($_GET['spaceid']))) {
  290. showmessage(L('space_exist'), HTTP_REFERER);
  291. }
  292. if ((!isset($data['width']) || $data['width']==0) && in_array($data['type'], array('banner', 'fixure', 'float', 'couplet', 'imagechange', 'imagelist'))) {
  293. showmessage(L('plate_width_not_empty'), HTTP_REFERER);
  294. } else {
  295. $data['width'] = intval($data['width']);
  296. }
  297. if ((!isset($data['height']) || $data['height']==0) && in_array($data['type'], array('banner', 'fixure', 'float', 'couplet', 'imagechange', 'imagelist'))) {
  298. showmessage(L('plate_height_not_empty'), HTTP_REFERER);
  299. } else {
  300. $data['height'] = intval($data['height']);
  301. }
  302. $TYPES = $this->template_type();
  303. return $data;
  304. }
  305. }
  306. ?>