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.

139 lines
4.4 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Mobile\Model;
use Think\Model;
/**
* 文档基础模型
*/
class LinksModel extends Model{
/* 自动验证规则 */
protected $_validate = array(
array('type', 'require', '友链类型不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
array('title', 'require', '链接名称不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
array('title', '1,50', '链接名称不能超过50个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH),
array('link_url', 'require', '链接地址不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
array('link_url', 'url', '链接地址非法', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
);
/* 自动完成规则 */
protected $_auto = array(
array('create_time', 'getCreateTime', self::MODEL_BOTH,'callback'),
array('status',1, self::MODEL_BOTH),
);
/**
* 构造函数
* @param string $name 模型名称
* @param string $tablePrefix 表前缀
* @param mixed $connection 数据库连接信息
*/
public function __construct($name = '', $tablePrefix = '', $connection = '') {
/* 设置默认的表前缀 */
$this->tablePrefix ='tab_';
/* 执行构造方法 */
parent::__construct($name, $tablePrefix, $connection);
}
/**
* 获取详情页数据
* @param integer $id 文档ID
* @return array 详细数据
*/
public function detail($id){
/* 获取基础数据 */
$info = $this->field(true)->find($id);
if(!(is_array($info) || 1 !== $info['status'])){
$this->error = '文档被禁用或已删除!';
return false;
}
/* 获取模型数据 */
$logic = $this->logic($info['model_id']);
$detail = $logic->detail($id); //获取指定ID的数据
if(!$detail){
$this->error = $logic->getError();
return false;
}
$info = array_merge($info, $detail);
return $info;
}
/**
* 创建时间不写则取当前时间
* @return int 时间戳
* @author huajie <banhuajie@163.com>
*/
protected function getCreateTime(){
$create_time = I('post.create_time');
return $create_time?strtotime($create_time):NOW_TIME;
}
/**
* 获取扩展模型对象
* @param integer $model 模型编号
* @return object 模型对象
*/
private function logic($model){
$name = $model;//parse_name(get_document_model($model, 'name'), 1);
$class = is_file(MODULE_PATH . 'Logic/' . $name . 'Logic' . EXT) ? $name : 'Base';
$class = MODULE_NAME . '\\Logic\\' . $class . 'Logic';
return new $class($name);
}
/**
* 检查标识是否已存在(只需在同一根节点下不重复)
* @param string $name
* @return true无重复false已存在
* @author huajie <banhuajie@163.com>
*/
protected function checkName(){
$name = I('post.name');
$category_id = I('post.category_id', 0);
$id = I('post.id', 0);
$map = array('name' => $name, 'id' => array('neq', $id), 'status' => array('neq', -1));
$category = get_category($category_id);
if ($category['pid'] == 0) {
$map['category_id'] = $category_id;
} else {
$parent = get_parent_category($category['id']);
$root = array_shift($parent);
$map['category_id'] = array('in', D("Category")->getChildrenId($root['id']));
}
$res = $this->where($map)->getField('id');
if ($res) {
return false;
}
return true;
}
/**
* 生成不重复的name标识
* @author huajie <banhuajie@163.com>
*/
private function generateName(){
$str = 'abcdefghijklmnopqrstuvwxyz0123456789'; //源字符串
$min = 10;
$max = 39;
$name = false;
while (true){
$length = rand($min, $max); //生成的标识长度
$name = substr(str_shuffle(substr($str,0,26)), 0, 1); //第一个字母
$name .= substr(str_shuffle($str), 0, $length);
//检查是否已存在
$res = $this->getFieldByName($name, 'id');
if(!$res){
break;
}
}
return $name;
}
}