【phpcms-v9】phpcms-v9中將選中的批量文章推送到推薦位、推送到專題及推送到其他欄目的功能.
1.推送按鈕:
2.推送彈出框:
3.push()函數:
- function push() {
- var str = 0;
- var id = tag = '';
- $("input[name='ids[]']").each(function() {
- if($(this).attr('checked')=='checked') {
- str = 1;
- id += tag+$(this).val();
- tag = '|';
- }
- });
- if(str==0) {
- alert('<?php echo L('you_do_not_check');?>');
- return false;
- }
- window.top.art.dialog({id:'push'}).close();
- window.top.art.dialog({title:'<?php echo L('push');?>:',id:'push',iframe:'?m=content&c=push&action=position_list&catid=<?php echo $catid?>&modelid=<?php echo $modelid?>&id='+id,width:'800',height:'500'}, function(){var d = window.top.art.dialog({id:'push'}).data.iframe;// 使用內置接口獲取iframe對象
- var form = d.document.getElementById('dosubmit');form.click();return false;}, function(){window.top.art.dialog({id:'push'}).close()});
- }
4.modules/content/push.php推送控制器文件:
- <?php
- defined('IN_PHPCMS') or exit('No permission resources.');
- pc_base::load_app_class('admin','admin',0);
- pc_base::load_sys_class('push_factory', '', 0);
- //權限判斷,根據欄目里面的權限設置檢查
- if((isset($_GET['catid']) || isset($_POST['catid'])) && $_SESSION['roleid'] != 1) {
- $catid = isset($_GET['catid']) ? intval($_GET['catid']) : intval($_POST['catid']);
- $this->priv_db = pc_base::load_model('category_priv_model');
- $priv_datas = $this->priv_db->get_one(array('catid'=>$catid,'is_admin'=>1,'action'=>'push'));
- if(!$priv_datas['catid']) showmessage(L('permission_to_operate'),'blank');
- }
- class push extends admin {
- public function __construct() {
- parent::__construct();
- $this->siteid = $this->get_siteid();
- /*
- * $_GET['module']值分析:
- * 1.推送到推薦位:不傳遞moduls參數
- * 2.推送到專題:special
- * 3.推送到其它欄目:content
- */
- $module = (isset($_GET['module']) && !emptyempty($_GET['module'])) ? $_GET['module'] : 'admin';
- if (in_array($module, array('admin', 'special','content'))) {
- /*根據$module值定位到對應的推送接口文件
- * 1.推送至推薦位:調用modules/admin/classes/push_api.class.php接口文件
- * 2.推薦至專題:調用modules/special/classes/push_api.class.php接口文件
- * 3.推薦至其它欄目:調用modules/content/classes/push_api.class.php接口文件
- */
- $this->push = push_factory::get_instance()->get_api($module);
- } else {
- showmessage(L('not_exists_push'), 'blank');
- }
- }
- /**
- * 推送選擇界面
- */
- public function init() {
- if ($_POST['dosubmit']) {
- $c = pc_base::load_model('content_model');//實例化一個content_model對象
- $c->set_model($_POST['modelid']);//模型id
- $info = array();//被推送的文章內容數組
- $ids = explode('|', $_POST['id']);//要推送的文章id,如:39|38|37
- if(is_array($ids)) {
- foreach($ids as $id) {
- $info[$id] = $c->get_content($_POST['catid'], $id);//根據欄目id及文章id獲取文章內容
- }
- }
- //position_list:推薦到推薦位 _push_special:推薦到專題 category_list:推薦到其它欄目
- $_GET['add_action'] = $_GET['add_action'] ? $_GET['add_action'] : $_GET['action'];
- $this->push->$_GET['add_action']($info, $_POST);//將文章推送到其它地方
- showmessage(L('success'), '', '', 'push');
- } else {
- pc_base::load_app_func('global', 'template');
- /*
- * $this->push分析:推送接口文件
- * 1.推送至推薦位:調用modules/admin/classes/push_api.class.php接口文件
- * 2.推薦至專題:調用modules/special/classes/push_api.class.php接口文件
- * 3.推薦至其它欄目:調用modules/content/classes/push_api.class.php接口文件
- */
- if (method_exists($this->push, $_GET['action'])) {
- $html = $this->push->{$_GET['action']}(array('modelid'=>$_GET['modelid'], 'catid'=>$_GET['catid']));
- $tpl = isset($_GET['tpl']) ? 'push_to_category' : 'push_list';
- include $this->admin_tpl($tpl);
- } else { //開源代碼Vevb.com
- showmessage('CLASS METHOD NO EXISTS!', 'blank');
- }
- }
- }
- public function public_ajax_get() {
- if (method_exists($this->push, $_GET['action'])) {
- $html = $this->push->{$_GET['action']}($_GET['html']);
- echo $html;
- } else {
- echo 'CLASS METHOD NO EXISTS!';
- }
- }
- }
- ?>
5.modules/content/classes/push_api.class.php文件分析:
- <?php
- /**
- * position_api.class.php 推薦至欄目接口類
- *
- * @copyright (C) 2005-2010 PHPCMS
- * @license http://www.phpcms.cn/license/
- * @lastmodify 2010-10-14
- */
- defined('IN_PHPCMS') or exit('No permission resources.');
- class push_api {
- private $db, $pos_data; //數據調用屬性
- public function __construct() {
- $this->db = pc_base::load_model('content_model'); //加載數據模型
- }
- /**
- * 接口處理方法,參數1:要推送的文章數組 參數2:舊的模型id及舊的欄目id
- * @param array $param 屬性 請求時,為模型、欄目數組。提交添加為二維信息數據 。例:array(1=>array('title'=>'多發發送方法', ....))
- * @param array $arr 參數 表單數據,只在請求添加時傳遞。 例:array('modelid'=>1, 'catid'=>12);
- */
- public function category_list($param = array(), $arr = array()) {
- //確認推送按鈕
- if ($arr['dosubmit']) {
- $id = $_POST['id'];//要批量推送的文章id,如:39|38|37
- if(emptyempty($id)) return true;
- $id_arr = explode('|',$id);//要批量推送的文章id數組
- if(count($id_arr)==0) return true;
- $old_catid = intval($_POST['catid']);//舊欄目id
- if(!$old_catid) return true;
- $ids = $_POST['ids'];//推送欄目,如:6|68
- if(emptyempty($ids)) return true;
- $ids = explode('|', $ids);//推送欄目id數組
- $siteid = intval($_POST['siteid']);//站點id
- $siteids = getcache('category_content','commons');//所有欄目對應的站點id
- $oldsiteid = $siteids[$old_catid];//舊站點id
- $this->categorys = getcache('category_content_'.$oldsiteid,'commons');//舊站點下所有欄目信息
- $modelid = $this->categorys[$old_catid]['modelid'];//舊欄目所屬的模型id
- $this->db->set_model($modelid);//根據模型id設置對應的模型表
- $tablename = $this->db->table_name;
- $this->hits_db = pc_base::load_model('hits_model');//點擊率統計表
- foreach($id_arr as $id) {//要批量推送的文章id數組
- $this->db->table_name = $tablename;//模型表
- $r = $this->db->get_one(array('id'=>$id));//根據當前文章id獲取該文章詳細信息
- $linkurl = preg_match('/^http://///',$r['url']) ? $r['url'] : siteurl($siteid).$r['url'];
- foreach($ids as $catid) {//推送欄目,如:6|68
- $siteid = $siteids[$catid];//推送到哪個站點id下的欄目
- $this->categorys = getcache('category_content_'.$siteid,'commons');//推送站點下所有的欄目信息
- $modelid = $this->categorys[$catid]['modelid'];//推送欄目所屬模型id
- $this->db->set_model($modelid);//根據模型id設置對應的模型表
- $newid = $this->db->insert(
- array('title'=>$r['title'],//標題
- 'style'=>$r['style'],
- 'thumb'=>$r['thumb'],//縮略圖
- 'keywords'=>$r['keywords'],//關鍵詞
- 'description'=>$r['description'],//描述
- 'status'=>$r['status'],//狀態
- 'catid'=>$catid,//欄目id
- 'url'=>$linkurl,//舊的鏈接地址
- 'sysadd'=>1,//系統添加
- 'username'=>$r['username'],//作者
- 'inputtime'=>$r['inputtime'],//發布時間
- 'updatetime'=>$r['updatetime'],//更新時間
- 'islink'=>1 //外部鏈接
- ),true);//將文章內容發布到推送欄目,并返回新插入的文章id
- $this->db->table_name = $this->db->table_name.'_data';//模型附表
- $this->db->insert(array('id'=>$newid));//新插入的文章id存儲到附表中,注意:文章內容沒有被存儲到附表中
- $hitsid = 'c-'.$modelid.'-'.$newid;
- $this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$catid,'updatetime'=>SYS_TIME));//點擊量統計表
- }
- }
- return true;
- } else {
- $siteid = get_siteid();
- $this->categorys = getcache('category_content_'.$siteid,'commons');
- $tree = pc_base::load_sys_class('tree');
- $tree->icon = array(' │ ',' ├─ ',' └─ ');
- $tree->nbsp = ' ';
- $categorys = array();
- $this->catids_string = array();
- if($_SESSION['roleid'] != 1) {
- $this->priv_db = pc_base::load_model('category_priv_model');
- $priv_result = $this->priv_db->select(array('action'=>'add','roleid'=>$_SESSION['roleid'],'siteid'=>$siteid,'is_admin'=>1));
- $priv_catids = array();
- foreach($priv_result as $_v) {
- $priv_catids[] = $_v['catid'];
- }
- if(emptyempty($priv_catids)) return '';
- }
- foreach($this->categorys as $r) {
- if($r['siteid']!=$siteid || $r['type']!=0) continue;
- if($_SESSION['roleid'] != 1 && !in_array($r['catid'],$priv_catids)) {
- $arrchildid = explode(',',$r['arrchildid']);
- $array_intersect = array_intersect($priv_catids,$arrchildid);
- if(emptyempty($array_intersect)) continue;
- }
- if($r['child']) {
- $r['checkbox'] = '';
- $r['style'] = 'color:#8A8A8A;';
- } else {
- $checked = '';
- if($typeid && $r['usable_type']) {
- $usable_type = explode(',', $r['usable_type']);
- if(in_array($typeid, $usable_type)) {
- $checked = 'checked';
- $this->catids_string[] = $r['catid'];
- }
- }
- $r['checkbox'] = "<input type='checkbox' name='ids[]' value='{$r[catid]}' {$checked}>";
- $r['style'] = '';
- }
- $categorys[$r['catid']] = $r;
- }
- $str = "<tr>
- <td align='center'>/$checkbox</td>
- <td style='/$style'>/$spacer/$catname</td>
- </tr>";
- $tree->init($categorys);
- $categorys = $tree->get_tree(0, $str);
- return $categorys;
- }
- }
- }
- ?>
例如:文章被推送到專題的情況:
新聞熱點
疑難解答