- 題號
- 欄位名稱
- 提示或說明
- 欄位類型
- 選項(用 ; 隔開)
- 必填
- 隨機唯一碼
';
} else {
$main .= '
題號 |
欄位名稱 |
提示或說明 |
欄位類型 |
選項(用 ; 隔開) |
必填 |
隨機唯一碼 |
';
}
foreach ($DcqData as $sort => $data) {
$main .= $this->getCustomSetupCol($sort, $data['data_value'], $data['col_id'], $id, $theme);
$sort++;
}
// 樣板
$main .= $theme == 'vtable' ? "\n\n" : "\n\n\n";
$main .= $this->getCustomSetupCol($sort, 'template', '', $id, $theme);
$sort++;
$main .= $theme == 'vtable' ? "\n\n" : "\n\n";
$main .= "\n
\n
\n";
$main .= '
';
$main .= '
';
$main .= $use_form ? '
' : '';
$main .= $use_form ? '' : '';
$main .= '
';
$SweetAlert = new SweetAlert();
$SweetAlert->render('del_' . $this->col_name . '_dcq_col', XOOPS_URL . "/modules/tadtools/ajax_file.php?dcq_op=del_dcq_col&col_name={$this->col_name}&col_sn={$this->col_sn}&dirname={$this->module_dirname}&col_id=", 'col_id');
return $main;
}
//取得設定界面的單一欄位
private function getCustomSetupCol($sort, $json = '', $col_id = '', $id = '', $theme = '')
{
$val = $json != 'template' ? json_decode($json, true) : [];
$id_content = $json != 'template' ? '' : 'id="form_data_' . $id . '"';
$myts = \MyTextSanitizer::getInstance();
$col_type_arr['input=text'] = _TDC_INPUT;
$col_type_arr['input=radio'] = _TDC_RADIO;
$col_type_arr['input=checkbox'] = _TDC_CHECKBOX;
$col_type_arr['input=date'] = _TDC_DATE;
$col_type_arr['input=file'] = _TDC_FILE;
$col_type_arr['select'] = _TDC_SELECT;
$col_type_arr['textarea'] = _TDC_TEXTAREA;
$col_type_arr['ckeditor'] = _TDC_CKEDITOR;
$col_type_arr['note'] = _TDC_NOTE;
$col_type_arr['user_name'] = _TDC_USER_NAME;
$col_type_arr['user_email'] = _TDC_USER_EMAIL;
$col_type_arr['SchoolCode'] = _TDC_SCHOOL_CODE;
$option = '';
foreach ($col_type_arr as $type => $text) {
$selected = $val['type'] == $type ? 'selected' : '';
$option .= '
';
}
if ($json != 'template') {
$del = ($col_id) ? "
col_name}_dcq_col('{$col_id}')\" style='color:red;'>
" : '';
} else {
$del = '
';
}
$i = $sort + 1;
$class = $theme == 'vtable' ? '1' : 'px-2 nw';
$form_arr[] = [$class, '', $del . "
#{$i}"];
$col_name_sn = 'dcq[' . $this->col_name . '][' . $this->col_sn . ']';
$name_id = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][title]"' : 'id="' . $col_name_sn . '" data-col-name="title"';
$class = $theme == 'vtable' ? '2' : 'c';
$form_arr[] = [$class, '#' . $i, '
'];
$name_id = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][placeholder]"' : 'id="' . $col_name_sn . '" data-col-name="placeholder"';
$class = $theme == 'vtable' ? '1' : 'c';
$form_arr[] = [$class, _TDC_DESCRIPTION, '
'];
$name_id = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][type]"' : 'id="' . $col_name_sn . '" data-col-name="type"';
$class = $theme == 'vtable' ? '1' : 'c';
$form_arr[] = [$class, _TDC_TYPE, '
'];
$name_id = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][opt]"' : 'id="' . $col_name_sn . '" data-col-name="opt"';
$class = $theme == 'vtable' ? '2' : 'c';
$form_arr[] = [$class, _TDC_OPTIONS, '
'];
$name_id = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][require]"' : 'id="' . $col_name_sn . '" data-col-name="require"';
$checked = 1 == $val['require'] ? 'checked' : '';
$class = $theme == 'vtable' ? '1' : 'px-2 nw';
$form_arr[] = [$class, _TDC_REQUIRE . $i, '
'];
$name_id1 = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][old_col_id]"' : 'id="' . $col_name_sn . '" data-col-name="old_col_id"';
$name_id2 = $json != 'template' ? 'name="' . $col_name_sn . '[' . $sort . '][col_id]"' : 'id="' . $col_name_sn . '" data-col-name="col_id"';
$class = $theme == 'vtable' ? '1' : 'c';
$form_arr[] = [$class, _TDC_COL_ID, '
'];
$main = $this->mk_form_group_arr($form_arr, $id_content, $theme);
return $main;
}
//儲存自訂表單設定
public function saveCustomSetupForm($redirict = true, $new_col_sn = '')
{
$dc_op = Request::getString('dc_op');
if ('saveCustomSetupForm' === $dc_op) {
$this->saveDcqData($new_col_sn);
if ($redirict) {
header("location: {$_SERVER['HTTP_REFERER']}");
exit;
}
}
}
//儲存自訂問卷資料資料
private function saveDcqData($new_col_sn = '')
{
global $xoopsDB;
$myts = \MyTextSanitizer::getInstance();
$dcq = Request::getArray('dcq');
$dc_op = Request::getString('dc_op');
foreach ($dcq as $col_name => $dcq_items) {
foreach ($dcq_items as $col_sn => $dcq_item) {
if (!empty($new_col_sn)) {
$col_sn = $new_col_sn;
}
foreach ($dcq_item as $data_sort => $item) {
if ('saveCustomSetupForm' === $dc_op and empty($item['title'])) {
continue;
}
$json_val = json_encode($item, JSON_UNESCAPED_UNICODE);
$json_val = $myts->addSlashes($json_val);
$this->delData('dcq', $data_sort, $col_name, $col_sn, __FILE__, __LINE__);
if (!empty($item['col_id'])) {
$col_id = $item['col_id'];
} else {
if ($item['type'] == "SchoolCode") {
$col_id = 'schoolcode';
} elseif ($item['type'] == "user_name") {
$col_id = 'username';
} elseif ($item['type'] == "user_email") {
$col_id = 'useremail';
} else {
$col_id = $this->rand_str();
}
}
$sql = "replace into `{$this->TadDataCenterTblName}`
(`mid` , `col_name` , `col_sn` , `data_name` , `data_value` , `data_sort`, `col_id` , `sort`, `update_time`)
values('{$this->mid}' , '{$col_name}' , '{$col_sn}' , 'dcq' , '{$json_val}' , '{$data_sort}' , '{$col_id}' , '{$sort}' , now())";
$xoopsDB->queryF($sql) or Utility::web_error($sql, __FILE__, __LINE__);
if ($item['col_id'] != $item['old_col_id']) {
$this->update_col_id($item['old_col_id'], $item['col_id']);
}
}
}
}
}
//更新辨識碼
private function update_col_id($old_col_id, $new_col_id)
{
global $xoopsDB;
$sql = 'UPDATE `' . $this->TadDataCenterTblName . '` SET `data_name`=? WHERE `data_name`=?';
$params = [$this->col_name . '_' . $this->col_sn . '_dcq_' . $new_col_id, $this->col_name . '_' . $this->col_sn . '_dcq_' . $old_col_id];
Utility::query($sql, 'ss', $params) or Utility::web_error($sql, __FILE__, __LINE__);
}
//取得自訂表單題目設定
public function getCustomSetup()
{
$DcqData = $this->getDcqData();
$dcq_arr = [];
foreach ($DcqData as $sort => $data) {
$dcq = json_decode($data['data_value'], true);
list($dcq['form_tag'], $dcq['type']) = explode('=', $dcq['type']);
$dcq['name'] = "{$this->col_name}_{$this->col_sn}_dcq_{$data['col_id']}";
$options = explode(';', $dcq['opt']);
$option_arr = [];
foreach ($options as $opt) {
if (false !== mb_strpos($opt, '=')) {
list($key, $val) = explode('=', $opt);
} else {
$key = $val = $opt;
}
$option_arr[$key] = $val;
}
$dcq['option_arr'] = $option_arr;
$dcq_arr[$sort] = $dcq;
}
return $dcq_arr;
}
//取得自訂表單
public function getCustomForm($use_form = true, $use_submit = false, $action = '', $lw = 3, $rw = 9, $muti_sort = null)
{
global $xoTheme;
$action = empty($action) ? $_SERVER['PHP_SELF'] : $action;
$DcqData = $this->getDcqData();
$form_col = '';
foreach ($DcqData as $data) {
// 該欄位的細項設定
$dcq = json_decode($data['data_value'], true);
list($form_tag, $type) = explode('=', $dcq['type']);
$name = "{$this->col_name}_{$this->col_sn}_dcq_{$data['col_id']}";
$options = explode(';', $dcq['opt']);
$option_arr = [];
foreach ($options as $opt) {
if (false !== mb_strpos($opt, '=')) {
list($key, $val) = explode('=', $opt);
} else {
$key = $val = $opt;
}
$option_arr[$key] = $val;
}
$require = 1 == $dcq['require'] ? ' validate[required]' : '';
if (in_array($type, ['radio', 'checkbox', 'checkbox-radio'])) {
$attr_arr = ['class' => $require];
} elseif (in_array($form_tag, ['user_name', 'user_email', 'SchoolCode'])) {
$attr_arr = ['class' => $require];
} elseif (in_array($type, ['file'])) {
$attr_arr = ['require' => $dcq['require']];
} else {
$attr_arr = ['class' => "my-input my-100 $require"];
}
$muti_sort = $muti_sort ? $muti_sort : null;
$col = $this->getForm('return', $form_tag, $name, $type, null, $option_arr, $attr_arr, $muti_sort, $this->ans_col_name, $this->ans_col_sn);
$form_col .= $this->mk_form_group($lw, $rw, $dcq['title'], $col, false, $dcq['placeholder'], $dcq['require']);
}
if ($form_col) {
if ($xoTheme) {
$form = '';
$xoTheme->addStylesheet('modules/tadtools/css/my-input.css');
} else {
$form = '
';
}
if ($use_form) {
$FormValidator = new FormValidator('#myForm', false);
$FormValidator->render('topLeft');
$form .= '
' : '';
return $form;
}
}
//已填答案列表: $del_col_name= 'uid'(顯示刪除,並以 uid 為參數) ,$display_mode=vertical
public function getCustomAns($call_back_func = '', $del_col_name = false, $display_mode = '')
{
global $xoTheme;
$DcqData = $this->getDcqData();
if ($xoTheme) {
$xoTheme->addStylesheet('modules/tadtools/css/vtable.css');
$main = '';
} else {
$main = '
';
}
$col_type = [];
if ('vertical' === $display_mode) {
$main .= "
";
foreach ($DcqData as $sort => $data) {
$dcq = json_decode($data['data_value'], true);
list($form_tag, $type) = explode('=', $dcq['type']);
if ('note' === $form_tag) {
continue;
}
$dcq_title = $dcq['title'];
$data_name = "{$this->col_name}_{$this->col_sn}_dcq_{$data['col_id']}";
$ans = $this->getDcqDataArr($data_name);
$col_type[$data_name] = $dcq['type'];
$answer = [];
foreach ($ans as $a) {
$answer[] = nl2br(implode('、', $a[$data_name]));
}
$dcq_answer = implode(';', $answer);
$main .= "
";
}
if ($del_col_name) {
$main .= "
";
}
$main .= '
';
} else {
$main .= "
- " . _TDC_FILL_PEOPLE . '
';
foreach ($DcqData as $sort => $data) {
$dcq = json_decode($data['data_value'], true);
list($form_tag, $type) = explode('=', $dcq['type']);
if ('note' === $form_tag) {
continue;
}
$dcq_title = $dcq['title'];
$name[$dcq_title] = $data_name = "{$this->col_name}_{$this->col_sn}_dcq_{$data['col_id']}";
$col_type[$data_name] = $dcq['type'];
$main .= "- {$dcq_title}
";
}
if ($del_col_name) {
$main .= '- ' . _TAD_FUNCTION . '
';
}
$main .= '
';
$data_name_arr = [];
$ans = $this->getDcqDataArr($name);
foreach ($ans as $col_sn => $ans_arr) {
$title = ($call_back_func) ? call_user_func($call_back_func, $col_sn, $this->col_name, $this->col_sn) : $col_sn;
$main .= "
- " . _TDC_FILL_PEOPLE . "
- {$title}
";
foreach ($name as $dcq_title => $data_name) {
$css = $col_type[$data_name] == 'textarea' ? '' : "class='text-center'";
$main .= "- {$dcq_title}
- ";
if ($col_type[$data_name] == 'input=file') {
$TadUpFiles = new TadUpFiles($this->module_dirname, '/' . $data_name);
$TadUpFiles->set_col($this->ans_col_name, $col_sn);
$main .= $TadUpFiles->show_files('', true, 'filename', false, false, null, null, false);
} else {
$main .= nl2br(implode('、', $ans_arr[$data_name]));
}
$main .= '
';
$data_name_arr[$data_name] = $data_name;
}
if ($del_col_name) {
$main .= "-
";
}
$main .= '
';
}
$main .= '
';
}
if ($del_col_name) {
$data_name = implode('|', $data_name_arr);
$SweetAlert = new SweetAlert();
$SweetAlert->render('del_dcq_ans', XOOPS_URL . "/modules/tadtools/ajax_file.php?dcq_op=del_dcq_ans&data_name={$data_name}&dirname={$this->module_dirname}&col_name={$del_col_name}&col_sn=", 'col_ans_sn');
}
return $main;
}
//已填答案陣列
public function getCustomAnsArr()
{
$DcqData = $this->getDcqData();
foreach ($DcqData as $sort => $data) {
$dcq = json_decode($data['data_value'], true);
list($form_tag, $type) = explode('=', $dcq['type']);
if ('note' === $form_tag) {
continue;
}
$name[] = "{$this->col_name}_{$this->col_sn}_dcq_{$data['col_id']}";
}
$ans = $this->getDcqDataArr($name);
return $ans;
}
//取得填答資料陣列
public function getDcqDataArr($data_name = '')
{
global $xoopsDB;
$and_col_name = $this->ans_col_name ? "AND `col_name`=?" : '';
$and_col_sn = $this->ans_col_sn ? "AND `col_sn`=?" : '';
$values = [];
$params = [];
if (is_array($data_name)) {
foreach ($data_name as $name) {
$params[] = $this->mid;
$params[] = $name;
if ($this->ans_col_name) {
$params[] = $this->ans_col_name;
}
if ($this->ans_col_sn) {
$params[] = $this->ans_col_sn;
}
$sql = 'SELECT `col_sn`, `data_sort`, `data_value` FROM `' . $this->TadDataCenterTblName . '`
WHERE `mid`= ? AND `data_name`=? ' . $and_col_name . ' ' . $and_col_sn . ' ORDER BY `col_sn`';
$result = Utility::query($sql, str_repeat('s', count($params)), $params) or Utility::web_error($sql, __FILE__, __LINE__);
while (list($col_sn, $data_sort, $data_value) = $xoopsDB->fetchRow($result)) {
$values[$col_sn][$name][$data_sort] = $data_value;
}
$params = []; // Reset params for the next iteration
}
} else {
$params[] = $this->mid;
$params[] = $data_name;
if ($this->ans_col_name) {
$params[] = $this->ans_col_name;
}
if ($this->ans_col_sn) {
$params[] = $this->ans_col_sn;
}
$sql = 'SELECT `col_sn`, `data_sort`, `data_value` FROM `' . $this->TadDataCenterTblName . '`
WHERE `mid`= ? AND `data_name`=? ' . $and_col_name . ' ' . $and_col_sn . ' ORDER BY `col_sn`';
$result = Utility::query($sql, str_repeat('s', count($params)), $params) or Utility::web_error($sql, __FILE__, __LINE__);
while (list($col_sn, $data_sort, $data_value) = $xoopsDB->fetchRow($result)) {
$values[$col_sn][$data_name][$data_sort] = $data_value;
}
}
return $values;
}
// public function getDcqDataArr($data_name = '')
// {
// global $xoopsDB;
// $and_col_name = $this->ans_col_name ? "and `col_name`='{$this->ans_col_name}'" : '';
// $and_col_sn = $this->ans_col_sn ? "and `col_sn`='{$this->ans_col_sn}'" : '';
// $values = [];
// if (is_array($data_name)) {
// foreach ($data_name as $name) {
// $sql = "select col_sn, data_sort ,data_value from `{$this->TadDataCenterTblName}`
// where `mid`= '{$this->mid}' and `data_name`='{$name}' $and_col_name $and_col_sn order by col_sn";
// $result = $xoopsDB->queryF($sql) or Utility::web_error($sql, __FILE__, __LINE__);
// while (list($col_sn, $data_sort, $data_value) = $xoopsDB->fetchRow($result)) {
// $values[$col_sn][$name][$data_sort] = $data_value;
// }
// }
// } else {
// $sql = "select col_sn, data_sort ,data_value from `{$this->TadDataCenterTblName}`
// where `mid`= '{$this->mid}' and `data_name`='{$data_name}' $and_col_name $and_col_sn order by col_sn";
// $result = $xoopsDB->queryF($sql) or Utility::web_error($sql, __FILE__, __LINE__);
// while (list($col_sn, $data_sort, $data_value) = $xoopsDB->fetchRow($result)) {
// $values[$col_sn][$data_name][$data_sort] = $data_value;
// }
// }
// return $values;
// }
// 文字轉表單
public function strToForm($setup = '')
{
global $xoTheme;
if ($xoTheme) {
$main = '';
$xoTheme->addStylesheet('modules/tadtools/css/my-input.css');
} else {
$main = '
';
}
$cols = $this->getAllCols($setup);
$sort = 0;
foreach ($cols as $col) {
$sort++;
$form = $this->getForm('return', $col['form_tag'], $col['label'], $col['type'], $col['value'], $col['options'], $col['attrs']) . $col['other'];
$main .= $this->mk_form_group(2, 10, $col['label'], $form, false, $col['help'], $col['require']);
}
return $main;
}
// 取得所有的欄位設定
public function getAllCols($setup)
{
$setups = \explode("\n", $setup);
$cols = [];
foreach ($setups as $setup) {
$cols[] = $this->getColSetup($setup);
}
return $cols;
}
// 取得所有欄位的某個項目值
public function getAllColItems($setup, $item = 'label')
{
$setups = \explode("\n", $setup);
$items = [];
foreach ($setups as $setup) {
$col = $this->getColSetup($setup);
$label = $col['label'];
// $items[$label] = $col[$item];
$items[] = $col[$item];
}
return $items;
}
// 取得欄位設定
public function getColSetup($setup)
{
$setup = \trim($setup);
$cols = \explode(",", $setup);
if (!isset($cols[1])) {
$cols[1] = '';
}
$options = $attrs = [];
unset($value);
$type = $help = $other = $form_tag = $value = $col_kind = $require = '';
foreach ($cols as $i => $col) {
if (\strpos($col, '#') !== false) {
$help = \str_replace('#', '', $col);
} elseif ($i == 0) {
$label = $col;
if (\strpos($label, '*') !== false) {
$require = 1;
$attrs['class'][] = 'validate[required]';
$label = \str_replace('*', '', $label);
}
} elseif ($i == 1) {
if (strpos($cols[1], '=') === false) {
$col_kind = $cols[1];
if (!empty($col_kind) && isset($this->col_kind[$col_kind])) {
$form_tag = $this->col_kind[$col_kind]['form_tag'];
$type = $this->col_kind[$col_kind]['type'];
} else {
$form_tag = 'input';
$type = 'text';
}
} else {
list($k, $v) = explode('=', $cols[1]);
$attrs[$k] = $v;
$form_tag = 'input';
$col_kind = $type = 'text';
}
} else {
if (\strpos($col, '+') !== false) {
$col = \str_replace('+', '', $col);
if ($type == 'checkbox') {
$value[] = $col;
} else {
$value = $col;
}
} elseif ($cols[1] == 'const') {
$other = $value = $col;
} elseif ($cols[1] == 'hidden') {
$value = $col;
}
if (\in_array($type, ['select', 'radio', 'checkbox'])) {
$options[$col] = $col;
} elseif (strpos($col, '=') !== false) {
list($k, $v) = explode('=', $col);
$attrs[$k] = $v;
}
}
}
$col_setup['form_tag'] = $form_tag;
$col_setup['label'] = $label;
$col_setup['type'] = $type;
$col_setup['value'] = $value;
$col_setup['options'] = $options;
$col_setup['attrs'] = $attrs;
$col_setup['other'] = $other;
$col_setup['help'] = $help;
$col_setup['require'] = $require;
$col_setup['kind'] = $col_kind;
return $col_setup;
}
private function rand_str($len = 6, $format = 'ALL')
{
switch ($format) {
case 'ALL':
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
case 'CHAR':
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case 'NUMBER':
$chars = '0123456789';
break;
default:
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
mt_srand(intval($seed));
$password = '';
while (mb_strlen($password) < $len) {
$password .= mb_substr($chars, (mt_rand() % mb_strlen($chars)), 1);
}
return $password;
}
}