Skip to content

Commit

Permalink
模型功能
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhiliang committed Jan 15, 2016
1 parent 57abcd0 commit 75f5762
Show file tree
Hide file tree
Showing 19 changed files with 1,226 additions and 300 deletions.
17 changes: 10 additions & 7 deletions App/Admin/Common/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,28 @@ function file_icon($file,$type = 'png') {
* 读取upload配置类型
* @param array $args 上传配置信息
*/
function getswfinit($args) {
function getUploadParams($args) {
$siteid = get_siteid();
$site_setting = get_site_setting($siteid);
$site_allowext = $site_setting['upload_allowext'];
$args = explode(',',$args);
$allowupload = empty($args[2]) ? $site_setting['upload_maxsize'] : $args[2];
$watermark_enable = empty($args[5]) ? $site_setting['watermark_enable'] : $args['5'];

$arr['file_upload_limit'] = intval($args[0]) ? intval($args[0]) : '8';
$args['1'] = ($args[1]!='') ? $args[1] : $site_allowext;
$args['1'] = empty($args[1]) ? $site_allowext : $args[1];
$arr_allowext = explode('|', $args[1]);
$allowexts = array();
foreach($arr_allowext as $k=>$v) {
$v = '*.'.$v;
$array[$k] = $v;
$allowexts[] = '*.'.$v;
}
$upload_allowext = implode(';', $array);
$upload_allowext = implode(';', $allowexts);
$arr['file_types'] = $upload_allowext;
$arr['file_types_post'] = $args[1];
$arr['allowupload'] = intval($args[2]);
$arr['allowupload'] = sizecount($allowupload * 1024);
$arr['thumb_width'] = intval($args[3]);
$arr['thumb_height'] = intval($args[4]);
$arr['watermark_enable'] = ($args[5]=='') ? 1 : intval($args[5]);
$arr['watermark_enable'] = $watermark_enable;
return $arr;
}

Expand Down
100 changes: 14 additions & 86 deletions App/Admin/Controller/FileController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@ public function upload() {
$result['message'] = $upload->getError();
} else {
$attach_info = current($info);
$attach_info['thumbs'] = AttachmentLogic::createThumb($attach_info);
// 将附件插入附件表
$attach_info = array(
'actor_id' => $this->user['id'],
'type' => 1,
'url' => $attach_info["savepath"] . $attach_info["savename"],
'path' => $attach_info["savepath"] . $attach_info["savename"],
'name' => $attach_info["savename"],
'size' => $attach_info['size'],
'ext' => $attach_info['ext'],
'thumbs' => json_decode($attachment_info['thumbs']),
'ip' => get_client_ip(),
'title' => $attach_info['name'],
'url' => $attach_info["savepath"] . $attach_info["savename"],
'path' => $attach_info["savepath"] . $attach_info["savename"],
'name' => $attach_info["savename"],
'size' => $attach_info['size'],
'ext' => $attach_info['ext'],
'ip' => get_client_ip(),
'uploaded_at' => date('Y-m-d H:i:s'),
);

$thumbs = AttachmentLogic::createThumb($attach_info);
$attach_info['thumbs'] = json_encode($thumbs);
// 将附件插入附件表
if($attachment_id = model("Attachment")->add($attach_info)) {
$attach_info['id'] = $attachment_id;
$attach_info['thumbs'] = json_decode($attach_info['thumbs']);
$result['data'] = $attach_info;
} else {
$result['code'] = 10014;
Expand All @@ -62,10 +61,7 @@ public function upload() {
$this->ajaxReturn($result);
} else {
$args = $_GET['args'];
$args = getswfinit($args);
$site_setting = get_site_setting();
$file_size_limit = sizecount($site_setting['upload_maxsize']*1024);
$this->assign('file_size_limit',$file_size_limit);
$args = getUploadParams($args);
$this->assign('args',$args);
$this->display();
// abort(404);
Expand All @@ -91,78 +87,10 @@ public function crop() {
if ($result === false) {
$this->ajaxReturn(array('code' => $attachment_logic->getErrorCode(), 'message' => $attachment_logic->getErrorMessage()));
}
$attachment_origin = $result['origin'];
$attachment = $result['crop'];
$thumb = $result['thumb'];

// 等待重构 to-do
if ($type == 'avatar') {
model('attachment')->startTrans();
if (model('attachment')->where(array('id' => $attachment['id']))->save($attachment) === false) {
model('attachment')->rollback();
// 删除裁剪后的缩略图
unlink(UPLOAD_PATH . $thumb['path']);
// 删除裁剪后的图
unlink(UPLOAD_PATH . $attachment['path']);
// 删除原图
unlink(UPLOAD_PATH . $attachment_origin['path']);
$this->ajaxReturn(array('code' => 10011, 'message' => '附件路径更新失败'));
}

if (logic('actor')->updateAvatar($this->user['id'], $thumb['url']) === false) {
// 删除裁剪后的缩略图
unlink(UPLOAD_PATH . $thumb['path']);
// 删除裁剪后的图
unlink(UPLOAD_PATH . $attachment['path']);
// 删除原图
unlink(UPLOAD_PATH . $attachment_origin['path']);
model('attachment')->rollback();
$this->ajaxReturn(array('code' => 10014, 'message' => '艺人头像更新失败'));
};
model('attachment')->commit();
$attachment['photo_id'] = $photo_id;
$attachment['thumb'] = $thumb['url'];
} else {
unset($attachment['id']);
model('attachment')->startTrans();
$attachment['id'] = model('attachment')->add($attachment);
if ($attachment['id'] === false) {
// 删除裁剪后的缩略图
unlink(UPLOAD_PATH . $thumb['path']);
// 删除裁剪后的图
unlink(UPLOAD_PATH . $attachment['path']);
// 删除原图
unlink(UPLOAD_PATH . $attachment_origin['path']);
model('attachment')->rollback();
$this->ajaxReturn(array('code' => 10013, 'message' => '保存裁剪后的图片失败'));
}
$photo_id = D('photo')->add(array('actor_id' => $this->user['id'], 'url' => $attachment['url'], 'thumb' => $thumb['url'], 'created_at' => date("Y-m-d H:i:s"), 'updated_at' => date("Y-m-d H:i:s") ));
if ($photo_id === false) {
// 删除裁剪后的缩略图
unlink(UPLOAD_PATH . $thumb['path']);
// 删除裁剪后的图
unlink(UPLOAD_PATH . $attachment['path']);
// 删除原图
unlink(UPLOAD_PATH . $attachment_origin['path']);
model('attachment')->rollback();
$this->ajaxReturn(array('code' => 60009, 'message' => '才艺发布失败'));
}
$attachment = $result['crop'];
$attachment['thumb'] = $result['thumb']['url'];

// 更新艺人写真数目
if (D('actor')->where(array('id' => $this->user['id']))->save(array('photos' => array('exp', '`photos` + 1'))) === false) {
// 删除裁剪后的缩略图
unlink(UPLOAD_PATH . $thumb['path']);
// 删除裁剪后的图
unlink(UPLOAD_PATH . $attachment['path']);
// 删除原图
unlink(UPLOAD_PATH . $attachment_origin['path']);
model('attachment')->rollback();
$this->ajaxReturn(array('code' => 60009, 'message' => '写真数目更新失败'));
};
model('attachment')->commit();
$attachment['photo_id'] = $photo_id;
$attachment['thumb'] = $thumb['url'];
}
$this->ajaxReturn(array('code' => 0, 'message' => '', 'data' => $attachment));
} else {
abort(404);
Expand Down
116 changes: 116 additions & 0 deletions App/Admin/Controller/ModelController.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
// +----------------------------------------------------------------------
// | TP-Admin [ 多功能后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.hhailuo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: XiaoYao <[email protected]>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\CommonController;

/**
* 系统模型控制
*/
class ModelController extends CommonController {
protected $db, $fieldDb, $modelTypes;

function __construct() {
parent::__construct();
$this->db = model("Model");
$this->fieldDb = model("ModelField");
$this->modelTypes = $this->db->getModelTypes();
}

public function index() {
$models = $this->db->where("siteid = %d", $this->siteid)->select();
// $types = $this->type_db->where("siteid = %d", $this->siteid)->select();
$this->assign("types",$this->modelTypes);
$this->assign("models",$models);
$this->display();
}

public function add() {
if (IS_POST) {
$this->checkToken();
$data = $_POST['model'];
$data['siteid'] = (isset($this->siteid) ? $this->siteid : 1);

$this->db->startTrans();
if ($modelid = $this->db->add($data)) {
if (logic('Model')->execModelCreateSql($data['tablename'], $modelid)) {
$this->db->commit();
$this->success('添加成功!');
} else {
$this->db->rollback();
$this->error("添加失败! ");
}
} else {
$this->db->rollback();
$this->error("添加失败! ");
}
} else {
$this->display();
}
}

public function edit() {
if (IS_POST) {
$this->checkToken();
if ($this->db->where(array('siteid' => $this->siteid, 'id' => $_POST['modelid']))->save($_POST['model']) !==false) {
$this->success("更新成功!", $_POST['forward']);
} else {
// $this->error("更新失败! 最后执行SQL:".$this->db->getLastSql());
$this->error("更新失败! ");
}
} else {
$model = $this->db->where(array('siteid' => $this->siteid))->find($_GET['modelid']);
if (empty($model)) {
$this->error('模型不存在!');
}
$this->assign('model',$model);
$this->display();
}
}

public function delete() {
$modelid = intval($_GET['modelid']);
$model = $this->db->find($modelid);
if (empty($model)) {
$this->error('模型不存在!');
}
$this->db->startTrans();
if ($this->db->where(array('siteid' => $this->siteid,'id' => $modelid))->delete() !== false) {
if ($this->fieldDb->where(array('modelid'=>$modelid,'siteid'=>$this->siteid))->delete() === false) {
$this->db->rollback();
$this->error('删除失败');
}
if ($result = $this->db->drop_table($model['tablename']) === false) {
$this->db->rollback();
$this->error('删除失败');
}
$this->db->commit();
$this->success('删除成功');
} else {
$this->db->rollback();
$this->error('删除失败');
}
}

/**
* 模型名称重复异步检测
*/
public function public_check_name() {
if (!isset($_GET['field']) || !($_GET[$_GET['clientid']])) exit("0");
$where = array($_GET['field'] => $_GET[$_GET['clientid']]);
if (isset($_GET['modelid'])) {
$where['id'] = array('NEQ',$_GET['modelid']);
}
if($this->db->where($where)->find()) {
exit("0");
} else {
exit("1");
}
}
}
Loading

0 comments on commit 75f5762

Please sign in to comment.