close_xss = 1; // 关闭xss验证
$this->fieldtype = ['TEXT' => ''];
$this->defaulttype = 'TEXT';
}
/**
* 字段相关属性参数
*
* @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']['ext']) || !$post['setting']['option']['ext']) {
return dr_return_data(0, dr_lang('扩展名必须填写'));
} elseif (!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_get_files($value);
}
/**
* 获取附件id
*/
public function get_attach_id($value) {
$data = [];
$value = dr_string2array($value);
if (!$value) {
return $data;
} elseif (!isset($value['file'])) {
return $value;
}
foreach ($value['file'] as $i => $file) {
is_numeric($file) && $data[] = $file;
}
return $data;
}
/**
* 字段入库值
*/
public function insert_value($field) {
$data = [];
$value = \Phpcmf\Service::L('Field')->post[$field['fieldname']];
if ($value && is_array($value) && $value['id']) {
foreach ($value['id'] as $id => $aid) {
$title = (string)$value['title'][$id];
$data['file'][$id] = $aid ? $aid : (string)$value['file'][$id];
$data['title'][$id] = trim($title);
$data['description'][$id] = $value['description'][$id] ? trim($value['description'][$id]) : '';
}
}
// 提取缩略图
if ($data['file'] && is_array($data['file']) && $field['setting']['option']['stslt']) {
$_field = \Phpcmf\Service::L('form')->fields;
if (isset($_field['thumb']) && $_field['thumb']['fieldtype'] == 'File' && !\Phpcmf\Service::L('Field')->data[intval($_field['thumb']['ismain'])]['thumb']) {
$one = array_key_first($data['file']);
if ($data['file'][$one]) {
$info = \Phpcmf\Service::C()->get_attachment($data['file'][$one], 1);
if ($info && in_array($info['fileext'], ['jpg', 'jpeg', 'png', 'gif'])) {
\Phpcmf\Service::L('Field')->data[intval($_field['thumb']['ismain'])]['thumb'] = $data['file'][$one];
}
}
}
}
\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);
!isset($_data['file']) && $_data =['file' => NULL];
!isset($data['file']) && $data = ['file' => NULL];
// 新旧数据都无附件就跳出
if (!$data['file'] && !$_data['file']) {
return NULL;
}
// 新旧数据都一样时表示没做改变就跳出
if (dr_diff($data['file'], $_data['file'])) {
return NULL;
}
// 当无新数据且有旧数据表示删除旧附件
if (!$data['file'] && $_data['file']) {
return [ [], $_data['file'] ];
}
// 当无旧数据且有新数据表示增加新附件
if ($data['file'] && !$_data['file']) {
return [$data['file'], []];
}
// 剩下的情况就是删除旧文件增加新文件
// 新旧附件的交集,表示固定的
$intersect = array_intersect($data['file'], $_data['file']);
return [
array_diff($data['file'], $intersect), // 固有的与新文件中的差集表示新增的附件
array_diff($_data['file'], $intersect), // 固有的与旧文件中的差集表示待删除的附件
];
}
/**
* 验证加载变量设置
*/
public function set_load_js($name, $value) {
\Phpcmf\Service::C()->loadjs['File'] = \Phpcmf\Service::C()->loadjs[$name] = $value;
}
/**
* 字段表单输入
*
* @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']);
// 字段提示信息
$tips = ($name == 'title' && APP_DIR) || $field['setting']['validate']['tips'] ? '' . $field['setting']['validate']['tips'] . '' : '';
$count = intval($field['setting']['option']['count']);
$ts = dr_lang('上传格式要求:%s(%s),最多上传%s个文件', str_replace(',', '、', (string)$field['setting']['option']['ext']), ($field['setting']['option']['size']) . 'MB', $count);
$p = dr_authcode([
'size' => ($field['setting']['option']['size']),
'exts' => $field['setting']['option']['ext'],
'attachment' => $field['setting']['option']['attachment'],
'image_reduce' => $field['setting']['option']['image_reduce'],
'count' => $count,
], 'ENCODE');
// 显示模板
if (!$field['setting']['option']['grid']) {
$tpl = '';
$tpl.= '
{preview}
';
$tpl.= '
';
$tpl.= '
';
$tpl.= '
';
$tpl.= '';
$tpl.= $js_rm = '';
$tpl.= '
';
if ($field['setting']['option']['name']) {
$tpl.= '
';
$tpl .= '';
$tpl.= '
';
}
if ($field['setting']['option']['desc']) {
$tpl.= '
';
$tpl.= '';
$tpl.= '
';
}
$tpl.= '
';
} else {
// 普通table模式
if ($field['setting']['option']['name'] or $field['setting']['option']['desc']) {
// 有名字和描述
$tpl = '';
$tpl.= '| ';
$tpl.= ' {preview} ';
$tpl.= ' | ';
$tpl.= '';
$tpl.= '';
$tpl.= ' ';
if ($field['setting']['option']['name']) {
$tpl .= '';
}
$tpl.= '';
$tpl.= '';
$tpl.= ' ';
if ($field['setting']['option']['desc']) {
$tpl.= ' ';
$tpl.= '';
$tpl.= ' ';
}
$tpl.= ' ';
$tpl.= ' | ';
$tpl.= '';
$tpl.= '';
$tpl.= $js_rm = '';
$tpl.= ' | ';
$tpl.= '
';
} else {
// 没有描述和名字修改
$tpl = '';
$tpl.= '
{preview}
';
$tpl.= '
';
$tpl.= '
';
$tpl.= '
';
$tpl.= '';
$tpl.= $js_rm = '';
$tpl.= '
';
$tpl.= '
';
}
}
// 已保存数据
$val = '';
$value = dr_get_files($value);
if ($value) {
foreach ($value as $i => $t) {
$id = $t['id'] ? $t['id'] : $t['file'];
$file = \Phpcmf\Service::C()->get_attachment($id, 1);
$description = $t['description'] ? htmlspecialchars($t['description']) : '';
if ($file) {
$disabled = 'readonly';
$preview = dr_file_preview_html($file['url'], $file['id']);
$filepath = dr_strcut($file['attachment'], 30);
$upload = '';
} else {
$disabled = '';
$filepath = htmlspecialchars((string)$id);
$preview = dr_file_preview_html($id);
$upload = '';
//$id = '';
}
$val.= str_replace(
['{title}', '{description}', '{id}', '{file}', '{filepath}', '{disabled}', '{preview}', '{upload}'],
[htmlspecialchars((string)$t['title']), $description, $id, $id, $filepath, $disabled, $preview, $upload],
$tpl
);
}
}
if (!$field['setting']['option']['grid']) {
$val = ''.$val.'
';
} else {
if ($field['setting']['option']['name'] or $field['setting']['option']['desc']) {
$val = '';
} else {
$val = ''.$val.'
';
}
}
$json = json_encode([
'name' => $name,
'ext' => !$field['setting']['option']['ext'] || $field['setting']['option']['ext'] == '*' ? 'null' : ' /(\.|\/)('.str_replace(',', '|', $field['setting']['option']['ext']).')$/i',
'size' => floatval($field['setting']['option']['size']) * 1024 * 1024,
'url' => dr_web_prefix(''.(IS_ADMIN ? SELF.'?c=api' : 'index.php?s=api&c=file').'&is_iframe=1&token='.dr_get_csrf_token()).'&siteid=' . SITE_ID . '&m=upload&p=' . $p . '&fid=' . $field['id'],
'unused_url' => dr_web_prefix(''.(IS_ADMIN ? SELF.'?c=api' : 'index.php?s=api&c=file').'&m=input_file_list&is_iframe=1&token='.dr_get_csrf_token()).'&siteid='.SITE_ID.'&p=' . $p . '&fid=' . $field['id'],
'input_url' => dr_web_prefix(''.(IS_ADMIN ? SELF.'?c=api' : 'index.php?s=api&c=file').'&m=input_file_url&is_iframe=1&token='.dr_get_csrf_token()).'&siteid='.SITE_ID.'&p='.$p.'&fid='.$field['id'].'&one='.($field['setting']['option']['name']?0:1),
'tpl' => str_replace($js_rm, '', $tpl),
'area' => \Phpcmf\Service::IS_MOBILE_USER() ? ["95%", "90%"] : ["80%", "80%"],
'url_area' => \Phpcmf\Service::IS_MOBILE_USER() ? ["95%", "90%"] : ["50%", "340px"],
'count' => $count,
'error' => dr_lang('上传文件不能超过%s个', $count).(CI_DEBUG ? '(可在自定义字段中设置本字段的个数值)' : ''),
'chunk' => $field['setting']['option']['chunk'] ? 20 * 1024 * 1024 : 0,
]);
$use = '';
if (!$field['setting']['option']['unused'] && \Phpcmf\Service::C()->uid) {
$use.= '';
}
if ($field['setting']['option']['input']) {
$use.= '';
}
// 表单输出
$str = '
';
if (!$field['setting']['option']['is_ext_tips']) {
$str.= ''.$ts.'
';
}
$str.= '
';
if (!$this->is_load_js('File')) {
$str.= '
';
$this->set_load_js('File', 1);
}
$str.= '
';
// 输出最终表单显示
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_get_files($value);
if ($value) {
// 显示模板
$tpl = '';
$tpl.= '| ';
$tpl.= ' {preview} ';
$tpl.= ' | ';
$tpl.= '';
$tpl.= '{title} {description}';
$tpl.= ' | ';
$tpl.= '
';
$html.= '';
$html.= '';
foreach ($value as $i => $t) {
$id = $t['id'] ? $t['id'] : $t['file'];
$file = \Phpcmf\Service::C()->get_attachment($id, 1);
$description = $t['description'] ? $t['description'] : '';
if ($file) {
$preview = dr_file_preview_html($file['url'], $file['id']);
$filepath = $file['attachment'];
$upload = '';
} else {
$filepath = $id;
$preview = dr_file_preview_html($id);
$upload = '';
}
$html.= str_replace(
['{title}', '{description}', '{id}', '{filepath}', '{preview}', '{upload}'],
[$t['title'], $description, $value, $filepath, $preview, $upload],
$tpl
);
}
$html.= '';
$html.= '
';
}
return $this->input_format($field['fieldname'], $field['name'], $html);
}
}