index.php
加入對應流程
//批次匯入 CSV
case 'tad_signup_data_import_csv':
Tad_signup_data::import_csv($id);
redirect_header("{$_SERVER['PHP_SELF']}?id=$id", 3, "成功匯入報名資料!");
break;
class\Tad_signup_data.php
的 preview_csv()
,把不需要的標頭拿掉
// 預覽 CSV
public static function preview_csv($action_id)
{
/*--略--*/
// 製作標題
$head_row = explode("\n", $action['setup']);
$head = $type = [];
foreach ($head_row as $head_data) {
$cols = explode(',', $head_data);
if (strpos($cols[0], '#') === false) {
$head[] = str_replace('*', '', trim($cols[0]));
$type[] = trim($cols[1]);
}
}
$xoopsTpl->assign('head', $head);
$xoopsTpl->assign('type', $type);
/*--略--*/
}
templates\op_tad_signup_data_preview_csv.tpl
樣板部份,也改成若沒有標頭就不顯示欄位
<table class="table table-bordered table-sm">
<!--略-->
<{foreach from=$preview_data key=i item=data name=preview_data}>
<{if $smarty.foreach.preview_data.iteration > 1}>
<tr>
<{foreach from=$data key=j item=val}>
<{assign var=title value=$head.$j}>
<{assign var=input_type value=$type.$j}>
<{if $title!=''}>
<td>
<!--略-->
</td>
<{/if}>
<{/foreach}>
</tr>
<{/if}>
<{/foreach}>
</tbody>
</table>
class\Tad_signup_data.php
加入對應方法,和 store()
差不多流程,差別在於多筆資料需要用迴圈一筆一筆抽出寫入:
影片中資料庫大小寫不正確,一律改為小寫 prefix("tad_signup_data")
//批次匯入 CSV
public static function import_csv($action_id)
{
global $xoopsDB, $xoopsUser;
//XOOPS表單安全檢查
Utility::xoops_security_check();
if (!$_SESSION['can_add']) {
redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
}
$action_id = (int) $action_id;
$uid = $xoopsUser->uid();
$action = Tad_signup_actions::get($action_id);
$TadDataCenter = new TadDataCenter('tad_signup');
foreach ($_POST['tdc'] as $tdc) {
$sql = "insert into `" . $xoopsDB->prefix("tad_signup_data") . "` (
`action_id`,
`uid`,
`signup_date`,
`accept`
) values(
'{$action_id}',
'{$uid}',
now(),
'1'
)";
$xoopsDB->queryF($sql) or Utility::web_error($sql, __FILE__, __LINE__);
$id = $xoopsDB->getInsertId();
$TadDataCenter->set_col('id', $id);
$TadDataCenter->saveCustomData($tdc);
$action['signup'] = self::get_all($action_id);
if (count($action['signup']) > $action['number']) {
$TadDataCenter->set_col('data_id', $id);
$TadDataCenter->saveCustomData(['tag' => ['候補']]);
}
}
}
$head_row = explode("\n", $action['setup']);
$head = $type = [];
foreach ($head_row as $head_data) {
$cols = explode(',', $head_data);
if (strpos($cols[0], '#') === false) {
$head[] = str_replace('*', '', trim($cols[0]));
$type[] = trim($cols[1]);
}
}
可以改用下列方式更簡單喔:
$TadDataCenter = new TadDataCenter('tad_signup');
$head = $TadDataCenter->getAllColItems($action['setup']);
$type = $TadDataCenter->getAllColItems($action['setup'], 'type');
tadtools/class/TadDataCenter.php 必須是 2021/10/29 日以後的版本,可至此下載覆蓋
link to https://github.com/tadlearn/tad_signup/commit/e265ab10a9232c9d19529b7eaa74a0ca748d8f01 \