Files

237 lines
7.9 KiB
PHP

<?php namespace Phpcmf\Controllers;
class Api extends \Phpcmf\App
{
public function index() {
}
public function category() {
$dirname = dr_safe_filename(\Phpcmf\Service::L('input')->get('mid'));
if (!$dirname) {
$dirname = dr_safe_filename(\Phpcmf\Service::L('input')->get('module'));
}
if (!$dirname) {
$this->_json(0, dr_lang('module参数不存在'));
}
$site = max(1, (int)\Phpcmf\Service::L('input')->get('site'));
if (!$site) {
$site = SITE_ID;
}
// 独立模块填目录 / 共享模块填 share
$category = \Phpcmf\Service::L('category', 'module')->get_category($dirname, $site);
if (!$category) {
$this->_json(0, dr_lang('栏目不存在或未生成缓存'), []);
}
// 模块是否共享(缓存)
$mod_share = [];
$list = [];
foreach ($category as $id => $t) {
if (!$t || !is_array($t)) {
continue;
}
$tid = (int)($t['tid'] ?? 0);
// 外链栏目不输出
if ($tid === 2) {
continue;
}
$cid = (int)($t['id'] ?? $id);
$cmid = dr_safe_filename((string)($t['mid'] ?? ''));
// show_url:基础路径,站点域名与 api_token 由客户端自行组装
if ($dirname !== 'share') {
$t['show_url'] = 'index.php?s='.$dirname.'&c=category&id='.$cid;
} else {
$use_s = false;
if ($cmid !== '') {
if (!isset($mod_share[$cmid])) {
$mod = \Phpcmf\Service::L('cache')->get('module-'.$site.'-'.$cmid);
$mod_share[$cmid] = ($mod && empty($mod['share'])) ? 0 : 1;
}
// share=0 表示独立模块
$use_s = ($mod_share[$cmid] === 0);
}
if ($use_s) {
$t['show_url'] = 'index.php?s='.$cmid.'&c=category&id='.$cid;
} else {
$t['show_url'] = 'index.php?c=category&id='.$cid;
}
}
// list_url:模块栏目才有列表
if ($tid === 1 && $cmid !== '') {
$t['list_url'] = 'index.php?s='.$cmid.'&c=search&catid='.$cid;
} else {
$t['list_url'] = '';
}
$list[$id] = $t;
}
$this->_json(1, 'ok', $list);
}
/**
* 内容关联字段数据读取
*/
public function related() {
// 强制将模板设置为后台
\Phpcmf\Service::V()->admin();
// 登陆判断
/*
if (!$this->uid) {
$this->_json(0, dr_lang('会话超时,请重新登录'));
}*/
// 参数判断
$dirname = dr_safe_filename(\Phpcmf\Service::L('input')->get('module'));
if (!$dirname) {
$this->_json(0, dr_lang('module参数不存在'));
}
// 站点选择
$site = max(1, (int)\Phpcmf\Service::L('input')->get('site'));
$pagesize = (int)\Phpcmf\Service::L('input')->get('pagesize');
if (!$pagesize) {
$pagesize = 10;
}
// 模块缓存判断
$module = $this->get_cache('module-'.$site.'-'.$dirname);
if (!$module) {
$this->_json(0, dr_lang('模块(%s)不存在', $dirname));
}
$module['field']['id'] = [
'name' => 'Id',
'ismain' => 1,
'fieldtype' => 'Text',
'fieldname' => 'id',
];
$param = $data = \Phpcmf\Service::L('input')->get('', true);
$diy = dr_safe_filename($data['diy']);
if (IS_POST) {
$ids = \Phpcmf\Service::L('input')->get_post_ids();
if (!$ids) {
$this->_json(0, dr_lang('没有选择项'));
}
$id = [];
foreach ($ids as $i) {
$id[] = (int)$i;
}
$builder = \Phpcmf\Service::M()->db->table($site.'_'.$dirname);
$builder->whereIn('id', $id);
$mylist = $builder->orderBy('updatetime DESC')->get()->getResultArray();
if (!$mylist) {
$this->_json(0, dr_lang('没有相关数据'));
}
$name = dr_safe_filename(\Phpcmf\Service::L('input')->get('name'));
if (!$name) {
$this->_json(0, dr_lang('name参数不能为空'));
}
$mid = $dirname;
$ids = [];
foreach ($mylist as $t) {
$ids[] = $t['id'];
}
$file = \Phpcmf\Service::V()->code2php(
file_get_contents(is_file(MYPATH.'View/api_related_field_'.$diy.'.html') ? MYPATH.'View/api_related_field_'.$diy.'.html' : COREPATH.'View/api_related_field.html')
);
ob_start();
require $file;
$code = ob_get_clean();
$html = explode('<!--list-->', $code);
$this->_json(1, dr_lang('操作成功'), ['ids' => $ids, 'html' => $html[1]]);
}
$my = intval($data['my']);
$where = [];
if ($my) {
$where[] = 'uid = '.$this->uid;
} elseif ($this->member && $this->member['adminid'] > 0) {
$module['field']['uid'] = [
'name' => dr_lang('账号'),
'ismain' => 1,
'fieldtype' => 'Text',
'fieldname' => 'uid',
];
}
if ($data['search']) {
$catid = (int)$data['catid'];
if ($catid) {
$cat = dr_cat_value($module['mid'], $catid);
if ($cat['catids']) {
$where[] = '`catid` in('.implode(',', $cat['catids']).')';
}
}
$data['keyword'] = dr_safe_replace(urldecode($data['keyword']));
if (isset($data['keyword']) && $data['keyword'] && $data['field'] && isset($module['field'][$data['field']])) {
$data['keyword'] = dr_safe_replace(urldecode($data['keyword']));
if ($data['field'] == 'id') {
// id搜索
$id = [];
$ids = explode(',', $data['keyword']);
foreach ($ids as $i) {
$id[] = (int)$i;
}
$where[] = 'id in('.implode(',', $id).')';
} else if ($data['field'] == 'uid') {
$uid = \Phpcmf\Service::M('member')->uid($data['keyword']);
$where[] = 'uid = '.intval($uid);
} else {
// 其他模糊搜索
$where[] = $data['field'].' LIKE "%'.$data['keyword'].'%"';
}
}
}
sort($module['field']);
$rules = $data;
$rules['page'] = '{page}';
\Phpcmf\Service::V()->assign([
'diy' => $data['diy'],
'mid' => $dirname,
'mmid' => $module['mid'],
'site' => $site,
'param' => $data,
'field' => $module['field'],
'where' => $where ? urlencode(implode(' AND ', $where)) : '',
'search' => dr_form_search_hidden(['search' => 1, 'is_iframe' => 1, 'module' => $dirname, 'site' => $site, 'diy' => $data['diy'], 'my' => $my, 'pagesize' => $pagesize]),
'select' => \Phpcmf\Service::L('category', 'module')->select(
$module['dirname'],
$data['catid'],
'name="catid"',
'--'
),
'urlrule' => '/index.php?s=module&c=api&m=related&'.http_build_query($rules),
'pagesize' => $pagesize,
]);
if (is_file(MYPATH.'View/api_related_'.$diy.'.html')) {
\Phpcmf\Service::V()->display('api_related_'.$diy.'.html');
} else {
\Phpcmf\Service::V()->display('api_related.html');
}
}
}