<?php
namespace app\admin\controller\general;
use app\common\controller\Backend;
class Attachment extends Backend
{
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = model('Attachment');
}
public function index()
{
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
$mimetypeQuery = [];
$filter = $this->request->request('filter');
$filterArr = (array)json_decode($filter, TRUE);
if (isset($filterArr['mimetype']) && stripos($filterArr['mimetype'], ',') !== false) {
$this->request->get(['filter' => json_encode(array_merge($filterArr, ['mimetype' => '']))]);
$mimetypeQuery = function ($query) use ($filterArr) {
$mimetypeArr = explode(',', $filterArr['mimetype']);
foreach ($mimetypeArr as $index => $item) {
$query->whereOr('mimetype', 'like', '%' . $item . '%');
}
};
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($mimetypeQuery)
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where($mimetypeQuery)
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
$cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
foreach ($list as $k => &$v) {
$v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
}
unset($v);
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
public function select()
{
if ($this->request->isAjax()) {
return $this->index();
}
return $this->view->fetch();
}
public function add()
{
if ($this->request->isAjax()) {
$this->error();
}
return $this->view->fetch();
}
public function del($ids = "")
{
if ($ids) {
\think\Hook::add('upload_delete', function ($params) {
$attachmentFile = ROOT_PATH . '/public' . $params['url'];
if (is_file($attachmentFile)) {
@unlink($attachmentFile);
}
});
$attachmentlist = $this->model->where('id', 'in', $ids)->select();
foreach ($attachmentlist as $attachment) {
\think\Hook::listen("upload_delete", $attachment);
$attachment->delete();
}
$this->success();
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
}