get_cache('form-' . SITE_ID);
// 将变量传入模板
\Phpcmf\Service::V()->assign([
'forms' => $forms,
'list' => \Phpcmf\Service::M('my', 'mokexw')->getConfig(),
'upload_url' => dr_url('mokexw/upload/add'),
]);
// 选择输出模板 后台位于 ./Views/test.html 此文件已经创建好了
\Phpcmf\Service::V()->display('index.html');
}
public function getHtml()
{
$formid = \Phpcmf\Service::L('input')->get('formid');
$formid = dr_safe_replace($formid);
$form = \Phpcmf\Service::C()->get_cache('form-' . SITE_ID, $formid);
$html = '';
if ($form) {
$n = 0;
$html .= '
';
}
$this->_json(1, 'ok', $html);
}
//导出列表
public function export_list()
{
# code...
}
//导出
public function export_con()
{
$formid = \Phpcmf\Service::L('input')->get('form');
$cid = \Phpcmf\Service::L('input')->get('cid');
$xingming = \Phpcmf\Service::L('input')->get('xingming');
$form = \Phpcmf\Service::C()->get_cache('form-' . SITE_ID, $formid);
if (!$formid || !$cid) {
return false;
}
$content = \Phpcmf\Service::M('my', 'mokexw')->get_form_content($formid, $cid);
//文件名称
$filename = '导出表单(' . $formid . ')内容_' . $cid;
$filename .= '.docx';
//配置信息
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '
');
$config = \Phpcmf\Service::M('my', 'mokexw')->getConfig($formid);
$tpl = WEBPATH . $config['path'];
if (!$config || !is_file($tpl)) {
return false;
}
//类库
$document = new \PhpOffice\PhpWord\TemplateProcessor($tpl);
//填充之
$fills = [];
foreach ($form['field'] as $fieldname => $field) {
if (!isset($content[$fieldname]) || !$content[$fieldname]) {
continue;
}
$value = $content[$fieldname] ?? '';
if (isset($config['set'][$fieldname])) {
if (!$config['set'][$fieldname] || $config['set'][$fieldname] == 'value') {
if (is_array($value)) {
$value = implode(',', $value);
}
} elseif ($config['set'][$fieldname] == 'name') {
$onefield = dr_field_options($field['id']);
if (!$onefield) {
continue;
}
if (is_array($value)) {
$values = [];
foreach ($onefield as $value5 => $name5) {
if (in_array($value5, $value)) {
$values[] = $name5;
}
}
$value = implode(',', $values);
} else {
foreach ($onefield as $value5 => $name5) {
if ($value == $value5) {
$value = $name5;
}
}
}
}
}
if (is_array($value)) {
$value = implode(',', $value);
}
$fills[$fieldname] = $value;
}
//系统字段
$sys = ['inputtime', 'inputip', 'uid'];
foreach ($sys as $v22) {
if ($v22 == 'inputtime' || $v22 == 'inputip') {
if (isset($content['inputtime'])) {
$fills['inputtime'] = $content['inputtime'] ? dr_date($content['inputtime']) : '';
}
if (isset($content['uid']) && !$fills['author']) {
$fills['author'] = $content['uid'] ? dr_member_info($content['uid'], 'username') : '';
}
} else {
$fills[$v22] = $content[$v22] ?? '';
}
}
$document->setValues($fills);
ob_end_clean();
ob_start();
if (!is_dir(WEBPATH . 'uploadfile/word/')) {
dr_mkdirs(WEBPATH . 'uploadfile/word/');
}
//生成文件
// $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($document, 'Word2007');
$document->saveAs(WEBPATH . 'uploadfile/word/' . $filename);
// $document->save('php://output');
//直接下载
// header("Content-Description: File Transfer");
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="' . $filename);
// header('Content-Transfer-Encoding: binary');
// header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
// header('Expires: 0');
readfile(WEBPATH . 'uploadfile/word/' . $filename);
}
}