dzadsod/web/select.class.php

322 lines
10 KiB
PHP

<?php
defined('IN_MET') or exit('No permission');
load::own_class('appweb');
load::own_func('appcmp');
/*
* 配置
* 包含其他功能初始化
*/
class select extends appweb
{
/*
* @$querys string 搜索内容
* @$tname string 指定的查询表
* @$fiend string 作为返回ID的值
* @$source string 查询来源页面,用于区分用哪个页面来查询的好做一些特殊处理
* @$allnone string 是否显示默认值全部,或者当没有搜索结果时将搜索内容当作值返回
* @$condition string 页面查询限制条件,采用$$$作为多条件分割,|||作为字段名和值的分割
* @$default string 默认值
* @$division string 默认值分隔符
*
* @$where string sql判断语句
*/
# 过滤方法名
private $ado = ['doselect'];
//搜索内容
private $querys = '';
//指定字段为值
private $fiend;
//来源页面标记
private $source;
//是否显示默认值全部
private $allnone;
//判断条件
private $condition;
//默认值
private $default;
//默认值分隔符
private $division;
//sql判断条件
private $where = '';
//返回的数组
private $data = [];
public function __construct()
{
global $_M, $_YW;
parent::__construct();
// 过滤
if (!in_array(M_ACTION, $this->ado, true)) {
//报错
exit(0);
}
//指定表
$this->tname = $_M['form']['tname'];
//搜索内容
$this->querys = $_M['form']['querys'];
//以指定字段为值
$this->fiend = $_M['form']['fiend'] ?: 'id';
//来源
$this->source = $_M['form']['source'];
//是否显示全部
$this->allnone = $_M['form']['allnone'];
//判断条件
$this->condition = $_M['form']['condition'];
//默认值
$this->default = $_M['form']['default'];
//默认值分隔符
$this->division = $_M['form']['division'];
}
# select2 选项字段处理
private function select_fiend($val)
{
global $_M, $_YW;
$strarr = stringto_array($this->fiend, '|');
foreach ($strarr as $value) {
$arrstr[] = $val[ $value ];
}
return arrayto_string($arrstr, '|');
}
# allnone 默认选项全部处理
private function allnone()
{
global $_M, $_YW;
// 若不需要显示‘全部’值,则直接返回空
// 0 为不显示‘全部’,同时也在未搜索到结果的情况下 不 设置为可选值
// 1 为显示 显示‘全部’,同时也在未搜索结果的情况下 设置可选值
// 2 只显示‘全部’
// 3 只将未搜索到结果的情况下设置为可选值
if (empty($this->allnone) || $this->allnone == '3') return false;
//然后可根据来源标记进行处理
switch ($this->source) {
// case 'githooks':
// $initial[] = ['id' => 'newver','text'=>'自动部署至最新版本'];
// break;
default:
$initial[] = ['id' => '', 'text' => '全部'];
break;
}
$this->data[] = ["text" => '', "children" => $initial];
}
# 当未搜索到值是否将搜索结果设置为可选值
private function children()
{
global $_M, $_YW;
// 判断是否要将搜索内容设置为可选值
if (empty($this->allnone) || $this->allnone == '2' || strlen($this->querys) == 0) return false;
// 要注意id的是否为可写入数据库 误区,若有些字段为数字 则会导致无法录入成功
// 后期可增加一个判断方式来处理这个误区
$this->data[] = [
"text" => '',
"children" => [
['id' => $this->querys, 'text' => $this->querys]
]
];
}
# querys 处理
private function querys()
{
global $_M, $_YW;
$curdate = date('Y-m-d', time());
switch ($this->source) {
case 'weblaunchsearch':
$this->where = " l_starttime <= '{$curdate}' ";
// $this->where = " NOT ( l_endtime < '{$curdate}' OR l_starttime > '{$curdate}' ) ";
break;
case 'webcontractsearch':
$this->where = " h.h_starttime <= '{$curdate}' ";
break;
default:
$this->where = false;
break;
}
if (strlen($this->querys) == 0) return false;
switch ($this->tname) {
case 'village':
$this->where = " v_name LIKE '%{$this->querys}%' ";
break;
case 'customer':
$this->where = " c_allname LIKE '%{$this->querys}%' ";
break;
case 'workers':
$this->where = " w_name LIKE '%{$this->querys}%' OR w_tel LIKE '%{$this->querys}%' ";
break;
case 'contract':
$this->where .= " AND ( h.h_number LIKE '%{$this->querys}%' OR c.c_allname LIKE '%{$this->querys}%' OR c.c_name LIKE '%{$this->querys}%' ) ";
break;
case 'launch':
$this->where .= " AND l_title LIKE '%{$this->querys}%' ";
break;
case 'weuser':
$nickname = urlencode($this->querys);
$this->where = " nickname LIKE '%{$nickname}%' ";
break;
default:
break;
}
}
# condition 处理
private function condition()
{
global $_M, $_YW;
if (empty($this->condition)) return false;
// 进行处理
$condition = stringto_array($this->condition, '|||', '$$$');
foreach ($condition as $value) {
list($key, $val) = $value;
$valstr[] = " {$key} = '{$val}' ";
}
$valstr = arrayto_string($valstr, ' AND ');
if (strlen($valstr) > 0) {
if (!empty($this->where)) $this->where = " ({$this->where}) AND ";
$this->where .= $valstr;
}
}
# 选项值的组成样式
private function valuet($val)
{
global $_M, $_YW;
switch ($this->tname) {
case 'village':
$fiend = [$val['v_name'], $val['v_province'] . $val['v_city'] . $val['v_district'] . $val['v_address']];
break;
case 'customer':
$fiend = [$val['c_name'], $val['c_allname'], $val['c_province'] . $val['c_city'] . $val['c_district'] . $val['c_address']];
break;
case 'workers':
$fiend = [$val['w_name'], $val['w_tel']];
break;
case 'contract':
$fiend = [$val['h_number'], $val['c_allname']];
break;
case 'launch':
$fiend = [$val['l_title'], $val['l_hnumber']];
break;
case 'weuser':
$sex = ['未知', '男', '女'];
$fiend = [$val['nickname'], $sex[ $val['sex'] ]];
break;
default:
break;
}
$text = arrayto_string($fiend, ' / ');
return $text;
}
//编号字段,根据此字段进行编号处理
private function charter()
{
global $_M, $_YW;
switch ($this->tname) {
case 'village':
$fiend = 'v_name';
break;
case 'customer':
$fiend = 'c_name';
break;
case 'workers':
$fiend = 'w_name';
break;
case 'contract':
$fiend = 'h_number';
break;
case 'launch':
$fiend = 'l_title';
break;
case 'weuser':
$fiend = 'nickname';
break;
default:
break;
}
return $fiend;
}
// 查看场所名称列表
public function doselect()
{
global $_M, $_YW;
//判断条件
self::querys();
//带入的判断条件
self::condition();
//编号的字段
$fiend = self::charter();
//查询
if ($this->tname != 'contract') {
$array = $this->tsql->table($this->tname)->where($this->where)->order('id DESC')->all('id');
} else {
$array = $this->tsql->table_unset()
->tables([$this->tname, 'h'], 'LEFT JOIN')
->tables(['customer', 'c'], 'ON h.h_cid = c.id')
->where($this->where)
->qfield(" h.*,c.c_allname ")
->order('h.id DESC')->all('id');
}
// dump($this->tsql->query());
foreach ($array as $val) {
if ($this->tname == 'weuser') {
$val['nickname'] = urldecode($val['nickname']);
}
$letter = getstrcharter($val[ $fiend ]);
$selectlist[ $letter ][] = $val;
}
//是否显示全部
self::allnone();
//是否将搜索内容作为结果
self::children();
//处理结果
foreach ($selectlist as $key => $val) {
$arr = [];
foreach ($val as $v) {
$valid = self::select_fiend($v);
//判断是否只输出默认值
if (strlen($this->default) > 0) {
$default = stringto_array($this->default, $this->division);
if (!in_array($valid, $default, true)) continue;
}
//组成选项
$arr[] = ['id' => $valid, 'text' => self::valuet($v)];
}
$this->data[] = ["text" => $key, "children" => $arr];
}
//当值不存在时
if (!count($this->data)) {
//输出没有搜索到结果
$this->data = [["text" => $this->querys == '' ? '检索不到相关信息' : "检索不到 {$this->querys} 相关信息", "children" => []]];
} else {
//对数组排序A-ZZ
$text = array_column($this->data, 'text');
array_multisort($text, SORT_ASC, $this->data);
}
echo jsoncallback(['results' => $this->data]);
}
}
?>