'短文本(text)',
self::TYPE_TEXTAREA => '长文本(textarea)',
self::TYPE_RADIO => '横向单选(radio)',
self::TYPE_CHECKBOX => '横向多选(checkbox)',
self::TYPE_SELECT => '下拉单选(select)',
self::TYPE_FILE => '文件(file)',
];
public function radio($name, $options, $current = null)
{
$arr = [];
foreach ($options as $value => $label) {
$arr[] = sprintf(
'',
$name, $value, (string)$current === (string)$value ? ' checked' : '', $label
);
}
return implode('', $arr);
}
function buildFieldForm(array $row = [])
{
global $lang_fields;
$trName = tr($lang_fields['col_name'] . '*', ' 仅允许数字、字母、下划线', 1, '', true);
$trLabel = tr($lang_fields['col_label'] . '*', '', 1, '', true);
$trType = tr($lang_fields['col_type'] . '*', $this->radio('type', self::$types, $row['type'] ?? null), 1, '', true);
$trRequired = tr($lang_fields['col_required'] . '*', $this->radio('required', ['0' => '否', '1' => '是'], $row['required'] ?? null), 1, '', true);
$trHelp = tr($lang_fields['col_help'], '', 1, '', true);
$trOptions = tr($lang_fields['col_options'], '
类型为单选、多选、下拉时必填,一行一个,格式:选项值|选项描述文本', 1, '', true);
$id = $row['id'] ?? 0;
$form = <<
HTML;
return $form;
}
function buildFieldTable()
{
global $lang_fields;
$perPage = 10;
$total = get_row_count('torrents_custom_fields');
list($paginationTop, $paginationBottom, $limit) = pager($perPage, $total, "?");
$sql = "select * from torrents_custom_fields order by id asc $limit";
$res = sql_query($sql);
$header = [
'id' => $lang_fields['col_id'],
'name' => $lang_fields['col_name'],
'label' => $lang_fields['col_label'],
'type_text' => $lang_fields['col_type'],
'required_text' => $lang_fields['col_required'],
'action' => $lang_fields['col_action'],
];
$rows = [];
while ($row = mysql_fetch_assoc($res)) {
$row['required_text'] = $row['required'] ? '是' : '否';
$row['type_text'] = self::$types[$row['type']] ?? '';
$row['action'] = sprintf(
"%s | %s",
$row['id'], $lang_fields['js_sure_to_delete_this'], $lang_fields['text_delete'], $row['id'], $lang_fields['text_edit']
);
$rows[] = $row;
}
$head = <<{$lang_fields['field_management']} -
HEAD;
$table = $this->buildTable($header, $rows);
return $head . $table . $paginationBottom;
}
public function save($data)
{
$attributes = [];
if (empty($data['name'])) {
throw new \InvalidArgumentException("Name 必须");
}
if (!preg_match('/^\w+$/', $data['name'])) {
throw new \InvalidArgumentException("Name 非法");
}
$attributes['name'] = $data['name'];
if (empty($data['label'])) {
throw new \InvalidArgumentException("显示标签 必须");
}
$attributes['label'] = $data['label'];
if (empty($data['type'])) {
throw new \InvalidArgumentException("类型 必须");
}
if (!isset(self::$types[$data['type']])) {
throw new \InvalidArgumentException("类型 非法");
}
$attributes['type'] = $data['type'];
if (!isset($data['required'])) {
throw new \InvalidArgumentException("不能为空 必须");
}
if (!in_array($data['required'], ["0", "1"], true)) {
throw new \InvalidArgumentException("不能为空 非法");
}
$attributes['required'] = $data['required'];
$attributes['help'] = $data['help'] ?? '';
$attributes['options'] = trim($data['options'] ?? '');
$now = date('Y-m-d H:i:s');
$attributes['updated_at'] = $now;
$table = 'torrents_custom_fields';
if (!empty($data['id'])) {
$result = DB::update($table, $attributes, "id = " . sqlesc($data['id']));
} else {
$attributes['created_at'] = $now;
$result = DB::insert($table, $attributes);
}
return $result;
}
public function buildTable(array $header, array $rows)
{
$table = '';
foreach ($header as $key => $value) {
$table .= sprintf('| %s | ', $value);
}
$table .= '
';
foreach ($rows as $row) {
$table .= '';
foreach ($header as $headerKey => $headerValue) {
$table .= sprintf('| %s | ', $row[$headerKey] ?? '');
}
$table .= '
';
}
$table .= '
';
return $table;
}
public function buildFieldCheckbox($name, $current = [])
{
$sql = 'select * from torrents_custom_fields';
$res = sql_query($sql);
$checkbox = [];
if (!is_array($current)) {
$current = explode(',', $current);
}
while ($row = mysql_fetch_assoc($res)) {
$checkbox[] = sprintf(
'',
$name, $row['id'], in_array($row['id'], $current) ? ' checked' : '', "{$row['label']}({$row['id']})"
);
}
return implode('', $checkbox);
}
public function renderUploadPage()
{
$searchBoxId = get_setting('main.browsecat');
$searchBox = DB::getOne('searchbox', "id = $searchBoxId");
if (empty($searchBox)) {
throw new \RuntimeException("Invalid search box: $searchBoxId");
}
$customFieldIdArr = explode(',', $searchBox['custom_fields']);
}
}