poster.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin','admin',0);
  4. pc_base::load_app_func('global', 'poster');
  5. class poster extends admin {
  6. private $db, $s_db;
  7. function __construct() {
  8. parent::__construct();
  9. $this->s_db = pc_base::load_model('poster_space_model');
  10. $this->db = pc_base::load_model('poster_model');
  11. $setting = new_html_special_chars(getcache('poster', 'commons'));
  12. $this->M = $setting[$this->get_siteid()];
  13. }
  14. /**
  15. * 广告列表
  16. */
  17. public function init() {
  18. $spaceid = $_GET['spaceid'] ? intval($_GET['spaceid']) : 0;
  19. if (!isset($spaceid) || empty($spaceid)) {
  20. showmessage(L('illegal_action'), HTTP_REFERER);
  21. }
  22. $page = max($_GET['page'], 1);
  23. $infos = $this->db->listinfo(array('spaceid'=>$spaceid, 'siteid'=>$this->get_siteid()), '`listorder` ASC, `id` DESC', $page);
  24. pc_base::load_sys_class('format', '', 0);
  25. $types = array('images'=>L('photo'), 'flash'=>L('flash'), 'text'=>L('title'));
  26. $show_dialog = $show_header = true;
  27. $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'));
  28. include $this->admin_tpl('poster_list');
  29. }
  30. /**
  31. * 添加广告
  32. */
  33. public function add() {
  34. if (isset($_POST['dosubmit'])) {
  35. $poster = $this->check($_POST['poster']);
  36. $setting = $this->check_setting($_POST['setting'], $poster['type']);
  37. $poster['siteid'] = $this->get_siteid();
  38. $poster['setting'] = array2string($setting);
  39. $poster['addtime'] = SYS_TIME;
  40. $id = $this->db->insert($poster, true);
  41. if ($id) {
  42. $this->s_db->update(array('items'=>'+=1'), array('spaceid'=>$poster['spaceid'], 'siteid'=>$this->get_siteid()));
  43. $this->create_js($poster['spaceid']);
  44. if(is_array($setting['images'])) {
  45. foreach ($setting['images'] as $im) {
  46. $imgs[] = $im['imageurl'];
  47. }
  48. }
  49. if (pc_base::load_config('system','attachment_stat')) {
  50. $this->attachment_db = pc_base::load_model('attachment_model');
  51. $this->attachment_db->api_update($imgs, 'poster-'.$id, 1);
  52. }
  53. showmessage(L('add_ads_success'), 'index.php?m=poster&c=space&a=init');
  54. } else {
  55. showmessage(L('operation_failure'), 'index.php?m=poster&c=space&a=init');
  56. }
  57. } else {
  58. $spaceid = intval($_GET['spaceid']);
  59. $sinfo = $this->s_db->get_one(array('spaceid' => $spaceid, 'siteid'=>$this->get_siteid()), 'name, type');
  60. $setting = $this->get_setting($sinfo['type']);
  61. $TYPES = get_types();
  62. $default = count($setting)>0 ? L('please_select').'&nbsp;&nbsp;&nbsp;&nbsp;' : '';
  63. }
  64. $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'));
  65. pc_base::load_sys_class('form', '', 0);
  66. include $this->admin_tpl('poster_add');
  67. }
  68. /**
  69. * 广告修改
  70. */
  71. public function edit() {
  72. $_GET['id'] = intval($_GET['id']);
  73. if (!$_GET['id']) showmessage(L('illegal_action'), HTTP_REFERER);
  74. if (isset($_POST['dosubmit'])) {
  75. $poster = $this->check($_POST['poster']);
  76. $setting = $this->check_setting($_POST['setting'], $poster['type']);
  77. $poster['setting'] = array2string($setting);
  78. $this->db->update($poster, array('id'=>$_GET['id'], 'siteid'=>$this->get_siteid()));
  79. $this->create_js(intval($_GET['spaceid']));
  80. foreach ($setting as $im) {
  81. $imgs[] = $im['imageurl'];
  82. }
  83. if(pc_base::load_config('system','attachment_stat')) {
  84. $this->attachment_db = pc_base::load_model('attachment_model');
  85. $this->attachment_db->api_update($imgs, 'poster-'.$_GET['id'], 1);
  86. }
  87. showmessage(L('edit_ads_success'), 'index.php?m=poster&c=poster&a=init&spaceid='.$_GET['spaceid']);
  88. } else {
  89. $info = $this->db->get_one(array('id'=>$_GET['id'], 'siteid'=>$this->get_siteid()));
  90. $sinfo = $this->s_db->get_one(array('spaceid' => $info['spaceid'], 'siteid'=>$this->get_siteid()), 'name, type');
  91. $setting = $this->get_setting($sinfo['type']);
  92. $TYPES = get_types();
  93. $info['setting'] = string2array($info['setting']);
  94. $default = count($setting)>0 ? L('please_select').'&nbsp;&nbsp;&nbsp;&nbsp;' : '';
  95. $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'));
  96. pc_base::load_sys_class('form', '', 0);
  97. include $this->admin_tpl('poster_edit');
  98. }
  99. }
  100. /**
  101. * 广告排序
  102. */
  103. public function listorder() {
  104. if (isset($_POST['listorder']) && is_array($_POST['listorder'])) {
  105. $listorder = $_POST['listorder'];
  106. foreach ($listorder as $k => $v) {
  107. $this->db->update(array('listorder'=>$v), array('id'=>$k));
  108. }
  109. }
  110. showmessage(L('operation_success'), HTTP_REFERER);
  111. }
  112. /**
  113. * 生成广告js文件
  114. * @param intval $id 广告版位ID
  115. * @return boolen 成功返回true
  116. */
  117. private function create_js($id = 0) {
  118. $html = pc_base::load_app_class('html');
  119. if (!$html->create_js($id)) showmessge($html->msg, HTTP_REFERER);
  120. return true;
  121. }
  122. /**
  123. * 启用、停用广告。此方法不真正执行操作,调用真正的操作方法
  124. * @param intval $id 广告ID
  125. */
  126. public function public_approval() {
  127. if (!isset($_POST['id']) || !is_array($_POST['id'])) {
  128. showmessage(L('illegal_parameters'), HTTP_REFERER);
  129. } else {
  130. array_map(array($this, _approval), $_POST['id']);
  131. }
  132. showmessage(L('operation_success'), HTTP_REFERER);
  133. }
  134. private function _approval($id = 0) {
  135. $id = intval($id);
  136. if (!$id) return false;
  137. $_GET['passed'] = intval($_GET['passed']);
  138. $this->db->update(array('disabled'=>$_GET['passed'] ), array('id'=>$id, 'siteid'=>$this->get_siteid()));
  139. return true;
  140. }
  141. /**
  142. * 删除广告 此方法不真正执行删除操作,调用真正的删除操作方法
  143. * @param invtal $id 广告ID
  144. */
  145. public function delete() {
  146. if (!isset($_POST['id']) || !is_array($_POST['id'])) {
  147. showmessage(L('illegal_parameters'), HTTP_REFERER);
  148. } else {
  149. array_map(array($this, _del), $_POST['id']);
  150. }
  151. showmessage(L('operation_success'), HTTP_REFERER);
  152. }
  153. /***
  154. * 广告删除
  155. */
  156. private function _del($id = 0) {
  157. $id = intval($id);
  158. if (!$id) return false;
  159. $r = $this->db->get_one(array('id'=>$id, 'siteid'=>$this->get_siteid()), 'spaceid');
  160. $this->s_db->update(array('items'=>'-=1'), array('spaceid'=>$r['spaceid'], 'siteid'=>$this->get_siteid()));
  161. $this->db->delete(array('id'=>$id, 'siteid'=>$this->get_siteid()));
  162. if (pc_base::load_config('system','attachment_stat')) {
  163. $this->attachment_db = pc_base::load_model('attachment_model');
  164. $keyid = 'poster-'.$id;
  165. $this->attachment_db->api_delete($keyid);
  166. }
  167. return true;
  168. }
  169. /**
  170. * 广告统计
  171. */
  172. public function stat() {
  173. $_GET['id'] = intval($_GET['id']);
  174. $info = $this->db->get_one(array('id'=>$_GET['id']), 'spaceid');
  175. if (!$_GET['id']) showmessage(L('illegal_operation'));
  176. /**
  177. *如果设置了日期查询,设置查询的开始时间和结束时间
  178. */
  179. $sdb = pc_base::load_model('poster_stat_model'); //调用广告统计的数据模型
  180. $year = date('Y', SYS_TIME);
  181. $month = date('m', SYS_TIME);
  182. $day = date('d', SYS_TIME);
  183. $where = $group = $order = '';
  184. $fields = '*';
  185. $where = "`pid`='".$_GET['id']."' AND `siteid`='".$this->get_siteid()."'";
  186. if ($_GET['range'] == 2) { //昨天的统计
  187. $fromtime = mktime(0, 0, 0, $month, $day-2, $year);
  188. $totime = mktime(0, 0, 0, $month, $day-1, $year);
  189. $where .= " AND `clicktime`>='".$fromtime."'";
  190. $where .= " AND `clicktime`<='".$totime."'";
  191. } elseif(is_numeric($_GET['range'])) { //如果设置了查询的天数
  192. $fromtime = mktime(0, 0, 0, $month, $day-$_GET['range'], $year);
  193. $where .= " AND `clicktime`>='".$fromtime."'";
  194. }
  195. $order = '`clicktime` DESC';
  196. //如果设置了按点击、展示统计
  197. $_GET['click'] = isset($_GET['click']) ? intval($_GET['click']) : 0;
  198. if (is_numeric($_GET['click'])) {
  199. $_GET['click'] = intval($_GET['click']);
  200. $where .= " AND `type`='".$_GET['click']."'";
  201. //如果设置了按地区或者按ip分类
  202. if ($_GET['group']) {
  203. $_GET['group'] = preg_replace('#`#', '', $_GET['group']);
  204. $group = " `".$_GET['group']."`";
  205. $fields = "*, COUNT(".$_GET['group'].") AS num";
  206. $order = " `num` DESC";
  207. }
  208. $r = $sdb->get_one($where, 'COUNT(*) AS num', '', $group); //取得总数
  209. } else {
  210. $r = $sdb->get_one($where, 'COUNT(*) AS num');
  211. }
  212. $page = max(intval($_GET['page']), 1);
  213. $curr_page = 20;
  214. $limit = ($page-1)*$curr_page.','.$curr_page;
  215. $pages = pages($r['num'], $page, 20); //生成分页
  216. $data = $sdb->select($where, $fields, $limit, $order, $group);
  217. $selectstr = $sdb->get_list($_GET['year']); //取得历史查询下拉框,有历史数据查询时,会自动换表
  218. pc_base::load_sys_class('format', '', 0);
  219. $show_header = true;
  220. unset($r);
  221. include $this->admin_tpl('poster_stat');
  222. }
  223. /**
  224. * 根据版位的类型,得到版位的配置信息。如广告类型等
  225. * @param string $type 版位的类型,默认情况下是一张图片或者动画
  226. * return boolean
  227. */
  228. private function get_setting($type) {
  229. $data = $poster_template = array();
  230. $poster_template = getcache('poster_template_'.$this->get_siteid(), 'commons');
  231. if (is_array($poster_template) && !empty($poster_template)) {
  232. $data = $poster_template[$type];
  233. } else {
  234. switch($type) {
  235. case 'banner':
  236. $data['type'] = array('images' => L('photo'), 'flash' => L('flash'));
  237. $data['num'] = 1;
  238. break;
  239. case 'fixure':
  240. $data['type'] = array('images' => L('photo'), 'flash' => L('flash'));
  241. $data['num'] = 1;
  242. break;
  243. case 'float':
  244. $data['type'] = array('images' => L('photo'), 'flash' => L('flash'));
  245. $data['num'] = 1;
  246. break;
  247. case 'couplet':
  248. $data['type'] = array('images' => L('photo'), 'flash' => L('flash'));
  249. $data['num'] = 2;
  250. break;
  251. case 'imagechange':
  252. $data['type'] = array('images' => L('photo'));
  253. $data['num'] = 1;
  254. break;
  255. case 'imagelist':
  256. $data['type'] = array('images' => L('photo'));
  257. $data['num'] = 1;
  258. break;
  259. case 'text':
  260. $data['type'] = array('text' => L('title'));
  261. break;
  262. case 'code':
  263. $data['type'] = array('text' => L('title'));
  264. break;
  265. default :
  266. $data['type'] = array('images' => L('photo'), 'flash' => L('flash'));
  267. $data['num'] = 1;
  268. }
  269. }
  270. return $data;
  271. }
  272. /**
  273. * 检查广告属性信息
  274. * @param array $data
  275. * return array
  276. */
  277. private function check($data) {
  278. if (!isset($data['name']) || empty($data['name'])) showmessage(L('adsname_no_empty'), HTTP_REFERER);
  279. if (!isset($data['type']) || empty($data['type'])) showmessage(L('no_ads_type'), HTTP_REFERER);
  280. $data['startdate'] = $data['startdate'] ? strtotime($data['startdate']) : SYS_TIME;
  281. $data['enddate'] = $data['enddate'] ? strtotime($data['enddate']) : strtotime('next month', $data['startdate']);
  282. if($data['startdate']>=$data['enddate']) $data['enddate'] = strtotime('next month', $data['startdate']);
  283. return $data;
  284. }
  285. /**
  286. * 检查广告的内容信息,如图片、flash、文字
  287. * @param array $setting
  288. * @param string $type 广告的类型
  289. * @return array
  290. */
  291. private function check_setting($setting = array(), $type = 'images') {
  292. switch ($type) {
  293. case 'images':
  294. unset($setting['flash'], $setting['text']);
  295. if(is_array($setting['images'])) {
  296. $tag = 0;
  297. foreach ($setting['images'] as $k => $s) {
  298. if($s['linkurl']=='http://') {
  299. $setting['images'][$k]['linkurl'] = '';
  300. }
  301. if (!$s['imageurl']) unset($setting['images'][$k]);
  302. else $tag = 1;
  303. }
  304. if (!$tag) showmessage(L('no_setting_photo'), HTTP_REFERER);
  305. }
  306. break;
  307. case 'flash':
  308. unset($setting['images'], $setting['text']);
  309. if (is_array($setting['flash'])) {
  310. $tag = 0;
  311. foreach ($setting['flash'] as $k => $s) {
  312. if (!$s['flashurl']) unset($setting['flash'][$k]);
  313. else $tag = 1;
  314. }
  315. if (!$tag) showmessage(L('no_flash_path'), HTTP_REFERER);
  316. }
  317. break;
  318. case 'text':
  319. unset($setting['images'], $setting['flash']);
  320. if ((!isset($setting['text'][1]['title']) || empty($setting['text'][1]['title'])) && (!isset($setting['text']['code']) || empty($setting['text']['code']))) {
  321. showmessage(L('no_title_info'), HTTP_REFERER);
  322. }
  323. break;
  324. }
  325. return $setting[$type];
  326. }
  327. /**
  328. * ajax检查广告名的合法性
  329. */
  330. public function public_check_poster() {
  331. if (!$_GET['name']) exit(0);
  332. if (CHARSET=='gbk') {
  333. $_GET['name'] = safe_replace(iconv('UTF-8', 'GBK', $_GET['name']));
  334. }
  335. if ($_GET['id']) {
  336. $spaceid = intval($_GET['spaceid']);
  337. $r = $this->db->get_one(array('id' => $id));
  338. if($r['name'] == $_GET['name']) {
  339. exit('1');
  340. }
  341. }
  342. $r = $this->db->get_one(array('siteid' => $this->get_siteid(), 'name' => $_GET['name']), 'id');
  343. if ($r['id']) {
  344. exit('0');
  345. } else {
  346. exit('1');
  347. }
  348. }
  349. }
  350. ?>