You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/models/analytics_pages_model.php

277 lines
7.0 KiB
PHP

<?php
/**
* 页面模型类
*
* @author LJQ
*
* @property CI_DB_active_record $FV
*/
class Analytics_pages_model extends CI_Model
{
// 字段属性
private $p_id = false;
private $p_site_code = false;
private $p_title = false;
private $p_url = false;
private $p_author = false;
private $p_hits = false;
private $p_datetime = false;
//
private $limit = false;
private $order_by = false;
private $my_fields = false;
function __construct()
{
parent::__construct();
$this->FV = $this->load->database('mysql', TRUE);
}
/**
* 重置查询
*/
private function reset()
{
$this->p_id = false;
$this->p_site_code = false;
$this->p_title = false;
$this->p_url = false;
$this->p_author = false;
$this->p_hits = false;
$this->p_datetime = false;
$this->my_fields = false;
$this->order_by = false;
$this->limit = false;
}
/**
* 分页列表
* @param type $site_code
* @param type $author
* @param type $order
* @param type $url
*/
public function get_page_list($page = 0, $site_code = '', $author = '', $order = 'id', $url = '')
{
$this->reset();
if (!empty($site_code))
{
$this->p_site_code = ' AND p_site_code = ' . $this->FV->escape($site_code);
}
if (!empty($author))
{
$this->p_author = ' AND p_author = ' . $this->FV->escape($author);
}
if (!empty($url))
{
$url = $this->FV->escape(urldecode($url));
$this->p_url = " AND p_url LIKE %$url%";
}
// 排序
$this->order_by = ' ORDER BY ';
if ('id' == $order)
{
$this->order_by .= 'p_id DESC';
} else
{
$this->order_by .= 'p_hits DESC';
}
$this->limit = $page . ",30";
return $this->get_list();
}
/**
* 获取列表 - 用于与访客表交互计算的方法
* @param type $page_ids
* @param type $site_code
* @param type $author
* @param type $order
* @param type $url
* @return boolean
*/
public function get_by_ids($page_ids = array(), $site_code = '', $author = '', $order = '', $url = '')
{
$this->reset();
if (empty($page_ids))
{
return FALSE;
}
$page_ids = implode(",", $page_ids);
$this->p_id = "AND p_id IN ($page_ids)";
// 站点代码
if (!empty($site_code))
{
$this->p_site_code = "AND p_site_code =" . $this->FV->escape($site_code);
}
// 作者
if (!empty($author))
{
$this->p_author = "AND p_author =" . $this->FV->escape($author);
}
// URL
if (!empty($url))
{
$url = $this->FV->escape(urldecode($url));
$this->p_url = " AND p_url LIKE %$url%";
}
// 排序
$this->order_by = ' ORDER BY ';
if (!empty($order) && 'hit' === $order)
{
$this->order_by .= 'p_hits DESC';
if ($page_ids)
{
// 排序
$this->order_by .= ", find_in_set(p_id,'$page_ids')";
}
} else
{
if ($page_ids)
{
$this->order_by .= "find_in_set(p_id,'$page_ids'),";
}
$this->order_by .= "p_id DESC";
}
return $this->get_list();
}
/**
* 根据指定条件获取单个page记录
* @param type $url
* @param type $site_code
*/
public function get_one_page($url, $site_code = '')
{
if (empty($url))
{
return FALSE;
}
$this->reset();
$this->p_url = ' AND p_url =' . $this->FV->escape($url);
if (!empty($site_code))
{
$this->p_site_code = ' AND p_site_code =' . $this->FV->escape($site_code);
}
$this->limit = 1;
return $this->get_list();
}
/**
* 添加新的页面并返回
* @param type $site_code
* @param type $page_url
* @param type $page_title
* @return boolean
*/
public function add_add_return($site_code, $page_url, $page_title = '')
{
// 插入新的page
if ($site_code AND $page_url)
{
$sql = "INSERT INTO analytics_pages
(
p_site_code,
p_url,
p_title,
p_datetime
)
VALUES ( ?, ?, ?, ? )";
$this->FV->query($sql, array($site_code, $page_url, $page_title, time()));
return $this->FV->insert_id();
}
return FALSE;
}
/**
* 更新点击
* @param type $page_id
*/
public function inc_view($page_id)
{
if (!empty($page_id))
{
$sql = "UPDATE analytics_pages
SET p_hits = p_hits + 1
WHERE p_id = ?";
return $this->FV->query($sql, array($page_id));
}
return FALSE;
}
/**
* 根据ID获取一条数据
* @param type $id
* @return boolean
*/
public function get_one($id)
{
if (!empty($id) && is_numeric($id))
{
$this->reset();
$this->p_id = ' AND p_id = ' . $this->FV->escape($id);
$this->limit = 1;
return $this->get_list();
}
return FALSE;
}
/**
* 获取数据集
*/
private function get_list()
{
$sql = "SELECT ";
if ($this->my_fields === false)
{
$sql .= "p_id,"
. " p_site_code,"
. " p_title,"
. " p_url,"
. " p_author,"
. " p_hits,"
. " p_datetime";
} else
{
$sql .= $this->my_fields;
}
$sql .= " FROM analytics_pages";
$sql .= " WHERE 1=1 ";
$this->p_id ? $sql .= $this->p_id : false;
$this->p_site_code ? $sql .= $this->p_site_code : false;
$this->p_title ? $sql .= $this->p_title : false;
$this->p_url ? $sql .= $this->p_url : false;
$this->p_author ? $sql .= $this->p_author : false;
$this->p_hits ? $sql .= $this->p_hits : false;
$this->p_datetime ? $sql .= $this->p_datetime : false;
$this->order_by ? $sql.=$this->order_by : false;
$this->limit ? $sql.= ' LIMIT ' . $this->limit : false;
$query = $this->FV->query($sql);
if ($this->limit == 1)
{
if ($query->num_rows() > 0)
{
$row = $query->row();
return $row;
} else
{
return FALSE;
}
} else
{
return $query->result();
}
}
}