本文章为原创内容,转载请注明出处。
上周班上的小范同学问我广告模块怎么设计,我说你去参考phpcms啊,他上面的广告模块设计的很不错呢。
那么,就让我们开始吧。
PHPCMS广告模块详细分析——广告的生成
一、功能。
我们首先从功能开始,这里用的是最新下载的 phpcms_v9.5.2_UTF8 ,有兴趣的同学可以下载下来。
跳过安装步骤,我们进入后台,直接看广告模块。
广告位列表
广告列表
广告统计
那么,我们就很清楚phpcms广告模块的功能了。
每个广告位最多显示一个广告,但是可以设置多个广告进行时间排序的播放,每一个广告都会有自己的统计信息,统计点击量和显示量。
二、数据库分析。
让我们打开phpcms的数据库,分析下数据是怎么存储的。
打开数据库,我们会发现三个名字中带有poser的表,没错!这(至少)三个表就是负责存储广告相关数据的。
广告位 poster_space
广告 poster
广告浏览IP统计 poster_201312
这样的话,数据统计也是很明确的啦!
poster_space表中存储着广告位,poster中存储每条广告的信息,包含统计信息的点击量,poster_201312存放着2013年12月的广告IP统计,因为每个用户的IP都不一样,数据量会非常大,所以要分月存放。
三,代码分析。
上面的内容都是铺垫,对于程序员们来说,源代码才是真刀实枪!
上码!
广告模块存放于phpcmsmodulesposter ,是作为一个phpcms的模块的存在。
我们按流程分析,按照 广告位->广告->前台调用 这个顺序,把源代码撸一遍!
1.space.php
先贴个几个图
广告模版
<?php
/**
* 这里是小雨的注释
*
* 广告模块的代码量其实不大,也就不到300行,除去官方注释和空行之外也就没有多少了。
*
*/
defined("IN_PHPCMS") or exit("No permission resources.");
pc_base::load_app_class("admin", "admin", 0);
pc_base::load_sys_class("form", "", 0);
/**
* 这里是小雨的注释
*
* 这里继承了admin类,为的是不允许前台调用
*
* 写这个注释是为了和后面的index.pphp进行区分
*/
class space extends admin {
private $M, $db;
/**
* 这里是小雨的注释
*
* 构造函数,因为phpcms的多站点管理,所以在构造函数中获取了当前站点的id并放入M中,方便下面的方法调用。
*/
function __construct() {
parent::__construct();
$setting = new_html_special_chars(getcache("poster", "commons"));
$this->M = $setting[$this->get_siteid()];
$this->db = pc_base::load_model("poster_space_model");
}
public function init() {
$TYPES = $this->template_type();
$page = max(intval($_GET["page"]), 1);
$infos = $this->db->listinfo(array("siteid" => $this->get_siteid()), "`spaceid`", $page);
$pages = $this->db->pages;
$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"));
include $this->admin_tpl("space_list");
}
/**
* 添加广告版块
*/
public function add() {
if (isset($_POST["dosubmit"])) {
$space = $this->check($_POST["space"]);
$space["setting"] = array2string($_POST["setting"]);
$space["siteid"] = $this->get_siteid();
$spaceid = $this->db->insert($space, true);
if ($spaceid) {
if ($space["type"] == "code") {
$path = "{show_ad(" . $space["siteid"] . ", " . $spaceid . ")}";
} else {
$path = "poster_js/" . $spaceid . ".js";
}
$this->db->update(array("path" => $path), array("siteid" => $this->get_siteid(), "spaceid" => $spaceid));
showmessage(L("added_successful"), "?m=poster&c=space", "", "add");
}
} else {
$TYPES = $this->template_type();
/**
* 这里是小雨的注释
*
* 没错!这里是添加广告版位的控制器,那么我们在上面的图中看到的广告位的下拉菜单来自哪里呢
* 让我们继续分析下面的函数 getcache
*/
$poster_template = getcache("poster_template_" . $this->get_siteid(), "commons");
$show_header = $show_validator = true;
include $this->admin_tpl("space_add");
}
}
/**
* 编辑广告版位
*/
public function edit() {
$_GET["spaceid"] = intval($_GET["spaceid"]);
if (!$_GET["spaceid"])
showmessage(L("illegal_operation"), HTTP_REFERER);
if (isset($_POST["dosubmit"])) {
$space = $this->check($_POST["space"]);
$space["setting"] = array2string($_POST["setting"]);
/**
* 这里是小雨的注释
*
* 修改提交的时候,通过判断表单中的广告位类型,写入了不同的值到了路径这个参数
*/
if ($space["type"] == "code") {
$space["path"] = "{show_ad(" . $this->get_siteid() . ", " . $_GET["spaceid"] . ")}";
} else {
$space["path"] = "poster_js/" . $_GET["spaceid"] . ".js";
}
if (isset($_POST["old_type"]) && $_POST["old_type"] != $space["type"]) {
$poster_db = pc_base::load_model("poster_model");
$poster_db->delete(array("spaceid" => $_GET["spaceid"]));
$space["items"] = 0;
}
if ($this->db->update($space, array("spaceid" => $_GET["spaceid"])))
showmessage(L("edited_successful"), "?m=poster&c=space", "", "testIframe" . $_GET["spaceid"]);
} else {
$info = $this->db->get_one(array("spaceid" => $_GET["spaceid"]));
/**
* 这里是小雨的注释
*
* 修改的时候将存入数据库的 setting字段转化为数组
*/
$setting = string2array($info["setting"]);
$TYPES = $this->template_type();
/**
* 这里是小雨的注释
*
* 拿到了广告模版的缓存,注意,是缓存,也就是存在原始文件,因为在上面的数据库中并没有存储模版的信息
*/
$poster_template = getcache("poster_template_" . $this->get_siteid(), "commons");
$show_header = $show_validator = true;
include $this->admin_tpl("space_edit");
}
}
/**
* 广告版位调用代码
*/
public function public_call() {
$_GET["sid"] = intval($_GET["sid"]);
if (!$_GET["sid"])
showmessage(L("illegal_action"), HTTP_REFERER, "", "call");
$r = $this->db->get_one(array("spaceid" => $_GET["sid"], "siteid" => $this->get_siteid()));
include $this->admin_tpl("space_call");
}
/**
* 广告预览
*/
public function public_preview() {
if (is_numeric($_GET["spaceid"])) {
$_GET["spaceid"] = intval($_GET["spaceid"]);
$r = $this->db->get_one(array("spaceid" => $_GET["spaceid"], "siteid" => $this->get_siteid()));
$scheme = $_SERVER["SERVER_PORT"] == "443" ? "https://" : "http://";
/**
* 这里是小雨的注释
*
* 在这里,同样的对 广告位 类型是 代码 的进行了特殊待遇
*
*/
if ($r["type"] == "code") {
$db = pc_base::load_model("poster_model");
$rs = $db->get_one(array("spaceid" => $r["spaceid"], "siteid" => $this->get_siteid()), "setting", "`id` ASC");
if ($rs["setting"]) {
$d = string2array($rs["setting"]);
$data = $d["code"];
}
} else {
$path = APP_PATH . "caches/" . $r["path"];
}
include $this->admin_tpl("space_preview");
}
}
private function template_type() {
pc_base::load_app_func("global", "poster");
return get_types();
}
/**
* 删除广告版位
* @param intval $sid 广告版位的ID,当批量删除时系统会递归删除
*/
public function delete() {
if ((!isset($_GET["spaceid"]) || empty($_GET["spaceid"])) && (!isset($_POST["spaceid"]) || empty($_POST["spaceid"]))) {
showmessage(L("illegal_parameters"), HTTP_REFERER);
} else {
if (is_array($_POST["spaceid"])) {
array_map(array($this, _del), $_POST["spaceid"]); //如果是批量操作,则递归数组
} elseif ($_GET["spaceid"]) {
$_GET["spaceid"] = intval($_GET["spaceid"]);
$db = pc_base::load_model("poster_model");
$db->delete(array("siteid" => $this->get_siteid(), "spaceid" => $_GET["spaceid"]));
$this->db->delete(array("siteid" => $this->get_siteid(), "spaceid" => $_GET["spaceid"]));
}
showmessage(L("operation_success"), HTTP_REFERER);
}
}
/**
* 广告位删除
* @param intval $spaceid 专题ID
*/
private function _del($spaceid = 0) {
$spaceid = intval($spaceid);
if (!$spaceid)
return false;
$db = pc_base::load_model("poster_model");
$db->delete(array("siteid" => $this->get_siteid(), "spaceid" => $spaceid));
$this->db->delete(array("siteid" => $this->get_siteid(), "spaceid" => $spaceid));
return true;
}
/**
* 广告模块配置
*/
public function setting() {
if (isset($_POST["dosubmit"])) {
//读取了缓存
$setting = getcache("poster", "commons");
$setting[$this->get_siteid()] = $_POST["setting"];
setcache("poster", $setting, "commons"); //设置缓存
$m_db = pc_base::load_model("module_model"); //调用模块数据模型
$setting = array2string($_POST["setting"]);
$m_db->update(array("setting" => $setting), array("module" => ROUTE_M)); //将配置信息存入数据表中
showmessage(L("setting_updates_successful"), HTTP_REFERER, "", "setting");
} else {
/**
* 这里是小雨的注释
*
* 注意这个函数
* extract() 函数从数组中把变量导入到当前的符号表中
* 也就是将构造函数中的,从缓存中取出的设置的数组直接打散作为变量
* @ 的符号是防止报错的
*/
@extract($this->M);
include $this->admin_tpl("setting");
}
}
/**
* 配置模板
*/
public function poster_template() {
/**
* 这里是小雨的注释
*
* 这里配置了模版,也就是从文件中读取
*
*/
$tpl_root = pc_base::load_config("system", "tpl_root");
$templatedir = PC_PATH . $tpl_root . pc_base::load_config("system", "tpl_name") . DIRECTORY_SEPARATOR . "poster" . DIRECTORY_SEPARATOR;
/**
*
* 这里的$templatedir 为 phpcms emplates/defaultposter
*
*/
$poster_template = getcache("poster_template_" . get_siteid(), "commons");
/**
* 找到所有的html的文件,循环得到不包含扩展名的文件名,作为配置项
*/
$templates = glob($templatedir . "*.html");
if (is_array($templates) && !empty($templates)) {
foreach ($templates as $k => $tem) {
$templates[$k] = basename($tem, ".html");
}
}
$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"));
include $this->admin_tpl("poster_template");
}
/**
* 删除模板配置
*/
public function public_tempate_del() {
if (!isset($_GET["id"]))
showmessage(L("illegal_parameters"), HTTP_REFERER);
$siteid = $this->get_siteid();
$poster_template = getcache("poster_template_" . $siteid, "commons");
/**
*
* 这里在删除的时候只是修改了缓存文件中的,并没有修改原来的哦
*
*/
if ($poster_template[$_GET["id"]]) {
unset($poster_template[$_GET["id"]]);
}
setcache("poster_template_" . $siteid, $poster_template, "commons");
showmessage(L("operation_success"), HTTP_REFERER);
}
/**
* 配置模板
*
* 注:这里只能对 "iscore" => 0, 的模版进行设置哦
*/
public function public_tempate_setting() {
$siteid = $this->get_siteid();
$poster_template = getcache("poster_template_" . $siteid, "commons");
if (isset($_POST["dosubmit"])) {
if (is_array($_POST["info"]["type"]) && !empty($_POST["info"]["type"])) {
$type2name = array("images" => L("photo"), "flash" => L("flash"), "text" => L("title"));
$type = array();
foreach ($_POST["info"]["type"] as $t) {
if (in_array($t, array("images", "flash", "text"))) {
$type[$t] = $type2name[$t];
} else {
continue;
}
}
}
unset($_POST["info"]["type"]);
$_POST["info"]["type"] = $type;
$poster_template[$_POST["template"]] = $_POST["info"];
/**
*
* 这里设置了模版的缓存,放在了
* caches/caches_commons/caches_data/poster_template_1.cache
* 中,特佩服phpcms的缓存
*/
setcache("poster_template_" . $siteid, $poster_template, "commons");
showmessage(L("setting_success"), "", "", "testIframe");
} else {
if (!isset($_GET["template"])) {
showmessage(L("illegal_parameters"));
} else {
$template = $_GET["template"];
}
if ($poster_template[$template]) {
$info = $poster_template[$template];
if (is_array($info["type"]) && !empty($info["type"])) {
$type = array();
$type = array_keys($info["type"]);
unset($info["type"]);
$info["type"] = $type;
}
}
include $this->admin_tpl("template_setting");
}
}
/**
* 更新js
*/
public function create_js($page = 0) {
$page = max(intval($_GET["page"]), 1);
if ($page == 1) {
/**
* 这里是小雨的注释
*
* 获取了当前站点下能用的广告做了数量的分页
*
*
*/
$result = $this->db->get_one(array("disabled" => 0, "siteid" => get_siteid()), "COUNT(*) AS num");
if ($result["num"]) {
$total = $result["num"];
$pages = ceil($total / 20);
}
} else {
$pages = $_GET["pages"] ? intval($_GET["pages"]) : 0;
}
$offset = ($page - 1) * 20;
$data = $this->db->listinfo(array("disabled" => 0, "siteid" => get_siteid()), "spaceid ASC", $page);
/**
*
* 其实这个方法只是个套子,真正的更新js在下面
*
* 读取了html的类
* 使用了html中的create_js,来更新了js
*/
$html = pc_base::load_app_class("html");
foreach ($data as $d) {
if ($d["type"] != "code") {
$html->create_js($d["spaceid"]);
} else {
continue;
}
}
$page++;
if ($page > $pages) {
showmessage(L("update_js_success"), "?m=poster&c=space&a=init");
} else {
showmessage(L("update_js") . "<font style="color:red">" . ($page - 1) . "/" . $pages . "</font>", "?m=poster&c=space&a=create_js&page=" . $page . "&pages=" . $pages);
}
}
/**
* 检测版位名称是否存在
*/
public function public_check_space() {
if (!$_GET["name"])
exit(0);
if (pc_base::load_config("system", "charset") == "gbk") {
$_GET["name"] = iconv("UTF-8", "GBK", $_GET["name"]);
}
$name = $_GET["name"];
if ($_GET["spaceid"]) {
$spaceid = intval($_GET["spaceid"]);
$r = $this->db->get_one(array("spaceid" => $spaceid, "siteid" => $this->get_siteid()));
if ($r["name"] == $name) {
exit("1");
}
}
$r = $this->db->get_one(array("siteid" => $this->get_siteid(), "name" => $name), "spaceid");
if ($r["spaceid"]) {
exit("0");
} else {
exit("1");
}
}
/**
* 检查表单数据
* @param Array $data 表单传递过来的数组
* @return Array 检查后的数组
*/
private function check($data = array()) {
if ($data["name"] == "")
showmessage(L("name_plates_not_empty"));
$info = $this->db->get_one(array("name" => $data["name"], "siteid" => $this->get_siteid()), "spaceid");
if (($info["spaceid"] && $info["spaceid"] != $_GET["spaceid"]) || ($info["spaceid"] && !isset($_GET["spaceid"]))) {
showmessage(L("space_exist"), HTTP_REFERER);
}
if ((!isset($data["width"]) || $data["width"] == 0) && in_array($data["type"], array("banner", "fixure", "float", "couplet", "imagechange", "imagelist"))) {
showmessage(L("plate_width_not_empty"), HTTP_REFERER);
} else {
$data["width"] = intval($data["width"]);
}
if ((!isset($data["height"]) || $data["height"] == 0) && in_array($data["type"], array("banner", "fixure", "float", "couplet", "imagechange", "imagelist"))) {
showmessage(L("plate_height_not_empty"), HTTP_REFERER);
} else {
$data["height"] = intval($data["height"]);
}
$TYPES = $this->template_type();
return $data;
}
}
?>
这里的总结。
广告位模版配置文件在缓存中,广告的模版存在于phpcms emplates/defaultposter 中,它和更新js功能有及其密切的关系。
下面开始分析广告的js是怎么生成的。
首先看位于 cache/poster_js下的一个已经生成的js文件
名为1.js的文件时对应的数据,位于poster_space表中id为1的记录,因为这条记录中path的值为 poster_js/1.js
function PCMSAD(PID) {
this.ID = PID;
this.PosID = 0;
this.ADID = 0;
this.ADType = "";
this.ADName = "";
this.ADContent = "";
this.PaddingLeft = 0;
this.PaddingTop = 0;
this.Wspaceidth = 0;
this.Height = 0;
this.IsHitCount = "N";
this.UploadFilePath = "";
this.URL = "";
this.SiteID = 0;
this.ShowAD = showADContent;
this.Stat = statAD;
}
function statAD() {
var new_element = document.createElement("script");
new_element.type = "text/javascript";
new_element.src="http://localhost/phpcms952/index.php?m=poster&c=index&a=show&siteid="+this.SiteID+"&spaceid="+this.ADID+"&id="+this.PosID;
document.body.appendChild(new_element);
}
function showADContent() {
var content = this.ADContent;
var str = "";
var AD = eval("("+content+")");
if (this.ADType == "images") {
if (AD.Images[0].imgADLinkUrl) str += "<a href=""+this.URL+"&a=poster_click&sitespaceid="+this.SiteID+"&id="+this.ADID+"&url="+AD.Images[0].imgADLinkUrl+"" target="_blank">";
str += "<img title=""+AD.Images[0].imgADAlt+"" src=""+this.UploadFilePath+AD.Images[0].ImgPath+"" width=""+this.Width+"" height=""+this.Height+"" style="border:0px;">";
if (AD.Images[0].imgADLinkUrl) str += "</a>";
}else if(this.ADType == "flash"){
str += "<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width=""+this.Width+"" height=""+this.Height+"" id="FlashAD_"+this.ADID+"" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">";
str += "<param name="movie" value=""+this.UploadFilePath+AD.Images[0].ImgPath+"" />";
str += "<param name="quality" value="autohigh" />";
str += "<param name="wmode" value="opaque"/>";
str += "<embed src=""+this.UploadFilePath+AD.Images[0].ImgPath+"" quality="autohigh" wmode="opaque" name="flashad" swliveconnect="TRUE" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width=""+this.Width+"" height=""+this.Height+""></embed>";
str += "</object>";
}
str += "";
document.write(str);
}
var cmsAD_1 = new PCMSAD("cmsAD_1");
cmsAD_1.PosID = 1;
cmsAD_1.ADID = 1;
cmsAD_1.ADType = "images";
cmsAD_1.ADName = "banner";
cmsAD_1.ADContent = "{"Images":[{"imgADLinkUrl":"http%3A%2F%2Fwww.phpcms.cn","imgADAlt":"","ImgPath":"http://localhost/phpcms952/uploadfile/poster/2.png"}],"imgADLinkTarget":"New","Count":"1","showAlt":"Y"}";
cmsAD_1.URL = "http://localhost/phpcms952/index.php?m=poster&c=index";
cmsAD_1.SiteID = 1;
cmsAD_1.Width = 430;
cmsAD_1.Height = 63;
cmsAD_1.UploadFilePath = "";
cmsAD_1.ShowAD();
var isIE=!!window.ActiveXObject;
if (isIE){
if (document.readyState=="complete"){
cmsAD_1.Stat();
} else {
document.onreadystatechange=function(){
if(document.readyState=="complete") cmsAD_1.Stat();
}
}
} else {
cmsAD_1.Stat();
}
这是一个类型为banner的广告模型,我们拿出它的模版文件
function PCMSAD(PID) {
this.ID = PID;
this.PosID = 0;
this.ADID = 0;
this.ADType = "";
this.ADName = "";
this.ADContent = "";
this.PaddingLeft = 0;
this.PaddingTop = 0;
this.Wspaceidth = 0;
this.Height = 0;
this.IsHitCount = "N";
this.UploadFilePath = "";
this.URL = "";
this.SiteID = 0;
this.ShowAD = showADContent;
this.Stat = statAD;
}
function statAD() {
var new_element = document.createElement("script");
new_element.type = "text/javascript";
new_element.src="{APP_PATH}index.php?m=poster&c=index&a=show&siteid="+this.SiteID+"&spaceid="+this.ADID+"&id="+this.PosID;
document.body.appendChild(new_element);
}
function showADContent() {
var content = this.ADContent;
var str = "";
var AD = eval("("+content+")");
if (this.ADType == "images") {
if (AD.Images[0].imgADLinkUrl) str += "<a href=""+this.URL+"&a=poster_click&sitespaceid="+this.SiteID+"&id="+this.ADID+"&url="+AD.Images[0].imgADLinkUrl+"" target="_blank">";
str += "<img title=""+AD.Images[0].imgADAlt+"" src=""+this.UploadFilePath+AD.Images[0].ImgPath+"" width=""+this.Width+"" height=""+this.Height+"" style="border:0px;">";
if (AD.Images[0].imgADLinkUrl) str += "</a>";
}else if(this.ADType == "flash"){
str += "<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width=""+this.Width+"" height=""+this.Height+"" id="FlashAD_"+this.ADID+"" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">";
str += "<param name="movie" value=""+this.UploadFilePath+AD.Images[0].ImgPath+"" />";
str += "<param name="quality" value="autohigh" />";
str += "<param name="wmode" value="opaque"/>";
str += "<embed src=""+this.UploadFilePath+AD.Images[0].ImgPath+"" quality="autohigh" wmode="opaque" name="flashad" swliveconnect="TRUE" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width=""+this.Width+"" height=""+this.Height+""></embed>";
str += "</object>";
}
str += "";
document.write(str);
}
var cmsAD_{$spaceid} = new PCMSAD("cmsAD_{$spaceid}");
cmsAD_{$spaceid}.PosID = {$spaceid};
cmsAD_{$spaceid}.ADID = {$p_id};
cmsAD_{$spaceid}.ADType = "{$p_type}";
cmsAD_{$spaceid}.ADName = "{$p_name}";
cmsAD_{$spaceid}.ADContent = "{"Images":[{"imgADLinkUrl":"{urlencode($p_setting[1]["linkurl"])}","imgADAlt":"{$p_setting[1]["alt"]}","ImgPath":"<?php echo $p_type=="images" ? $p_setting[1]["imageurl"] : $p_setting[1]["flashurl"];?>"}],"imgADLinkTarget":"New","Count":"1","showAlt":"Y"}";
cmsAD_{$spaceid}.URL = "{APP_PATH}index.php?m=poster&c=index";
cmsAD_{$spaceid}.SiteID = {$siteid};
cmsAD_{$spaceid}.Width = {$width};
cmsAD_{$spaceid}.Height = {$height};
cmsAD_{$spaceid}.UploadFilePath = "";
cmsAD_{$spaceid}.ShowAD();
var isIE=!!window.ActiveXObject;
if (isIE){
if (document.readyState=="complete"){
cmsAD_{$spaceid}.Stat();
} else {
document.onreadystatechange=function(){
if(document.readyState=="complete") cmsAD_{$spaceid}.Stat();
}
}
} else {
cmsAD_{$spaceid}.Stat();
}
这样,给我们的认识就是,模版上的变量被替换了,并重新生成了js文件,放到了相应的文件夹。
<?php
/**
*
* @param 广告生成js类
*/
defined("IN_PHPCMS") or exit("No permission resources.");
class html {
private $db, $s_db, $queue;
/**
*
* 类的构造函数读取了三个模型作为了类的内部变量
* 广告位,广告,队列
*
* 这里提一下队列模型
*
* phpcms的队列模型,就是在任务太多的情况下
* 将其存入名为queue的表中
* 处理完一个删除一个
* 可以理解为排队机制
*
*/
public function __construct() {
$this->s_db = pc_base::load_model("poster_space_model");
$this->db = pc_base::load_model("poster_model");
$this->queue = pc_base::load_model("queue_model");
}
/**
* 生成广告js文件
* @param intval $id 广告版位ID
* @return boolen 成功返回true
*/
public function create_js($id = 0) {
$id = intval($id);
if (!$id) {
$this->msg = L("no_create_js");
return false;
}
$siteid = get_siteid();
/**
*
* 这里的生成js文件就是单一的
* 在上文提到的调用中循环采用了这个
* 所以下面拿到的是当前站点id下的广告位的数据
*
*/
$r = $this->s_db->get_one(array("siteid" => $siteid, "spaceid" => $id));
$now = SYS_TIME; //将系统时间写入变量
if ($r["setting"])
$space_setting = string2array($r["setting"]); //如果存在广告位的设置,就转化成数组,其实肯定存在的
if ($r["type"] == "code")
return true; //代码类型的就直接返回了,因为不用生产js文件
$poster_template = getcache("poster_template_" . $siteid, "commons"); //读取了广告模版设置的缓存
/**
*
* 下面进行一个判断
*
* 判断广告位的类型配置是不是存在option一项
*
*
*/
if ($poster_template[$r["type"]]["option"]) {
/**
* 如果存在
* 获取
* 当前站点 当前广告位 能使用 设置时间在当前时间内 , 按照 人为排序正序 id倒序
* 的所有广告
*
* 并且打散数组
*/
$where = "`spaceid`="" . $id . "" AND `siteid`="" . $siteid . "" AND `disabled`=0 AND `startdate`<="" . $now . "" AND (`enddate`>="" . $now . "" OR `enddate`=0) ";
$pinfo = $this->db->select($where, "*", "", "`listorder` ASC, `id` DESC");
if (is_array($pinfo) && !empty($pinfo)) {
foreach ($pinfo as $k => $rs) {
if ($rs["setting"]) {
$rs["setting"] = string2array($rs["setting"]);
$pinfo[$k] = $rs;
} else {
unset($pinfo[$k]);
}
}
extract($r);
} else {
return true;
}
} else {
/**
* 如果不存在
*
* 取出数据的方式和上面一致
*
* 但是在打散的数组的每个变量前面都加上了 P
*
* 所以对于该广告模块来说,充分使用了extract这个函数
*
* 有兴趣的同学可以研究下
*
*/
$where = " `spaceid`="" . $id . "" AND `siteid`="" . $siteid . "" AND `disabled`=0 AND `startdate`<="" . $now . "" AND (`enddate`>="" . $now . "" OR `enddate`=0)";
$pinfo = $this->db->get_one($where, "*", "`listorder` ASC, `id` DESC");
if (is_array($pinfo) && $pinfo["setting"]) {
$pinfo["setting"] = string2array($pinfo["setting"]);
}
extract($r);
if (!is_array($pinfo) || empty($pinfo))
return true;
extract($pinfo, EXTR_PREFIX_SAME, "p");
}
$file = CACHE_PATH . $path;
/**
*
* 这里用的ob函数进行输出
*
* 优点:不会因为php文件提前输出header而出错,又可以及时刷新缓存区而不浪费资源
*/
ob_start();
/**
*
* 这里在文件输出流中引入了广告模版文件
* 而在原始的模版文件中存在着大量的php变量
* 这些变量和上面被打散的数组中的变量是一致的
* 也就是说在这一步
* 变量被巧妙的替换了
* 很自然的转化成了js文件
*/
include template("poster", $type);
$data = ob_get_contents();
/**
*
* 将合并后的文件内容存进变量后清空了ob文件流
*/
ob_end_clean();
/**
*
* 最后根据路径写入文件
*/
$strlen = pc_base::load_config("system", "lock_ex") ? file_put_contents($file, $data, LOCK_EX) : file_put_contents($file, $data);
@chmod($file, 0777);
return true;
}
}
?>
四、总结
根据上面的图文注释,小伙伴们都可以清楚的知道phpcms的广告是怎么产生的了吧。
配置文件+模版文件+数据库中具体数据=js广告文件
五、后记
用了一晚上的时间,又重新分析了一下phpcms,这个功能原来在大连实训的时候写过,也是参考phpcms的,这回也算是复习吧。
温故知新,回头再看phpcms,还是有很多值得学习的。
看来有空还要研究下它的队列机制了。