close_xss = 1; // 关闭xss验证
$this->fieldtype = ['TEXT' => ''];
$this->defaulttype = 'TEXT';
$this->img_ext = 'jpg,gif,png,jpeg,svg,webp';
}
/**
* 字段相关属性参数
*
* @param array $value 值
* @return string
*/
public function option($option) {
$mthumb = '';
if (\Phpcmf\Service::M('field')->relatedname == 'module') {
$mthumb = '
';
}
return [$this->_search_field().$mthumb.'
'.$this->attachment($option).'',
''];
}
/**
* 验证字段属性
*/
public function edit_config($post) {
if (!isset($post['setting']['option']['size']) || !$post['setting']['option']['size']) {
return dr_return_data(0, dr_lang('文件大小必须填写'));
} elseif (!isset($post['setting']['option']['count']) || !$post['setting']['option']['count']) {
return dr_return_data(0, dr_lang('上传数量必须填写'));
}
return dr_return_data(1, 'ok');
}
/**
* 字段输出
*/
public function output($value) {
return dr_string2array($value);
}
/**
* 获取附件id
*/
public function get_attach_id($value) {
return dr_string2array($value);
}
/**
* 字段入库值
*/
public function insert_value($field) {
$data = \Phpcmf\Service::L('Field')->post[$field['fieldname']];
if ($data) {
if ($field['setting']['option']['stslt']) {
$_field = \Phpcmf\Service::L('form')->fields;
if (isset($_field['thumb']) && $_field['thumb']['fieldtype'] == 'File' && !\Phpcmf\Service::L('Field')->data[$_field['thumb']['ismain']]['thumb']) {
$one = array_key_first($data);
if ($data[$one]) {
\Phpcmf\Service::L('Field')->data[1]['thumb'] = $data[$one];
}
}
}
if (dr_count($data) > $field['setting']['option']['count']) {
$data = array_slice($data, 0, $field['setting']['option']['count']-2);
}
}
\Phpcmf\Service::L('Field')->data[$field['ismain']][$field['fieldname']] = dr_array2string($data);
}
/**
* 附件处理
*/
public function attach($data, $_data) {
$data = dr_string2array($data);
$_data = dr_string2array($_data);
if (!isset($_data)) {
$_data = [];
}
if (!isset($data)) {
$data = [];
}
// 新旧数据都无附件就跳出
if (!$data && !$_data) {
return NULL;
}
// 新旧数据都一样时表示没做改变就跳出
if (dr_diff($data, $_data)) {
return NULL;
}
// 当无新数据且有旧数据表示删除旧附件
if (!$data && $_data) {
return [
[],
$_data
];
}
// 当无旧数据且有新数据表示增加新附件
if ($data && !$_data) {
return [
$data,
[]
];
}
// 剩下的情况就是删除旧文件增加新文件
// 新旧附件的交集,表示固定的
$intersect = array_intersect($data, $_data);
return [
array_diff($data, $intersect), // 固有的与新文件中的差集表示新增的附件
array_diff($_data, $intersect), // 固有的与旧文件中的差集表示待删除的附件
];
}
protected function _format_file_size($fileSize, $round = 2) {
if (!$fileSize) {
return 0;
}
$i = 0;
$inv = 1 / 1024;
$unit = [' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];
while ($fileSize >= 1024 && $i < 8) {
$fileSize *= $inv;
++$i;
}
$temp = sprintf("%.2f", $fileSize);
$value = $temp - (int) $temp ? $temp : $fileSize;
return ''.round($value, $round).'' . $unit[$i];
}
/**
* 字段表单输入
*
* @return string
*/
public function input($field, $value = '')
{
// 字段禁止修改时就返回显示字符串
if ($this->_not_edit($field, $value)) {
return $this->show($field, $value);
}
// 字段存储名称
$name = $field['fieldname'];
// 字段显示名称
$text = ($field['setting']['validate']['required'] ? ' * ' : '') . dr_lang($field['name']);
// 表单宽度设置
$width = \Phpcmf\Service::IS_MOBILE_USER() ? '100%' : ($field['setting']['option']['width'] ? $field['setting']['option']['width'] : '100%');
// 字段提示信息
$tips = ($name == 'title' && APP_DIR) || $field['setting']['validate']['tips'] ? '' . $field['setting']['validate']['tips'] . '' : '';
$size = ($field['setting']['option']['size']);
!$size && $size = 0;
$count = intval($field['setting']['option']['count']);
$p = dr_authcode([
'size' => $size,
'exts' => $this->img_ext,
'attachment' => $field['setting']['option']['attachment'],
'image_reduce' => $field['setting']['option']['image_reduce'],
], 'ENCODE');
$url = WEB_DIR.''.(IS_ADMIN ? SELF.'?c=api' : 'index.php?s=api&c=file').'&token='.dr_get_csrf_token().'&siteid=' . SITE_ID . '&m=upload&p=' . $p . '&fid=' . $field['id'];
// 显示模板
$i = 0;
$tpl = '';
if ($value) {
$value = dr_string2array($value);
if ($value) {
foreach ($value as $id) {
$file = \Phpcmf\Service::C()->get_attachment($id, 1);
if ($file) {
$editname = '';
if ($file['uid'] == \Phpcmf\Service::C()->uid || IS_ADMIN) {
$editname = ' onclick="dr_iframe(\''.dr_lang('修改名称').'\', \''.dr_web_prefix((IS_ADMIN ? SELF.'?c=api' : 'index.php?s=api&c=file').'&m=name_edit&id='.$file['id']).'\', \'350px\', \'220px\');" title="'.dr_lang('修改名称').'"';
}
$tpl.= '';
$tpl.= '
';
$tpl.= '
.')
';
$tpl.= '
';
$tpl.= '
'.$this->_format_file_size($file['filesize']).'
'.$file['filename'].'
';
$tpl.= '
';
$tpl.= ' ';
$tpl.= ' ';
$tpl.= '
';
$tpl.= '
';
$i++;
}
}
}
}
$tpl.= '';
$ts = dr_lang('每张图片最大%s,最多上传%s张图片', $size . 'MB', intval($field['setting']['option']['count']));
// 表单输出
$str = '
';
if (!$field['setting']['option']['is_ext_tips']) {
$str.= ''.$ts.'
';
}
if (!$this->is_load_js($field['fieldtype'])) {
$str.= '
';
$this->set_load_js($field['fieldtype'], 1);
}
$js = \Phpcmf\Service::L('js_packer');
$str.= $js->pack('
', 0);
// 输出最终表单显示
return $this->input_format($name, $text, $str.$tips);
}
/**
* 字段表单显示
*
* @param string $field 字段数组
* @param array $value 值
* @return string
*/
public function show($field, $value = null) {
$html = '';
$value = dr_string2array($value);
if ($value) {
$html.= '';
foreach ($value as $id) {
$html.= '
';
}
$html.= '
';
}
return $this->input_format($field['fieldname'], $field['name'], $html);
}
}