custom fields add i18n and getPreparedTorrents()

This commit is contained in:
xiaomlove
2021-03-06 19:16:58 +08:00
parent cb4e5e5eb5
commit cf301fef89
8 changed files with 131 additions and 34 deletions
+1 -1
View File
@@ -4642,7 +4642,7 @@ function displayHotAndClassic()
{ {
$Cache->add_whole_row(); $Cache->add_whole_row();
$res = sql_query("SELECT * FROM torrents WHERE picktype = " . sqlesc($type_each) . " AND seeders > 0 AND url != '' ORDER BY id DESC LIMIT 30") or sqlerr(__FILE__, __LINE__); $res = sql_query("SELECT sp_state, url, id, name, small_descr FROM torrents WHERE picktype = " . sqlesc($type_each) . " AND seeders > 0 AND url != '' ORDER BY id DESC LIMIT 30") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0) if (mysql_num_rows($res) > 0)
{ {
$movies_list = ""; $movies_list = "";
+2
View File
@@ -9,11 +9,13 @@ $lang_fields = [
'text_edit' => '编辑', 'text_edit' => '编辑',
'col_id' => 'ID', 'col_id' => 'ID',
'col_name' => 'Name', 'col_name' => 'Name',
'col_name_helptext' => '仅允许数字、字母、下划线',
'col_label' => '显示标签', 'col_label' => '显示标签',
'col_type' => '类型', 'col_type' => '类型',
'col_required' => '不能为空', 'col_required' => '不能为空',
'col_help' => '辅助说明', 'col_help' => '辅助说明',
'col_options' => '选项', 'col_options' => '选项',
'col_options_helptext' => '类型为单选、多选、下拉时必填,一行一个,格式:选项值|选项描述文本',
'col_action' => '操作', 'col_action' => '操作',
'col_is_single_row' => '展示时单独一行', 'col_is_single_row' => '展示时单独一行',
'js_sure_to_delete_this' => '你确信要删除此项目吗?', 'js_sure_to_delete_this' => '你确信要删除此项目吗?',
+2
View File
@@ -312,6 +312,8 @@ $lang_functions = array
'text_tag_diy' => '自制', 'text_tag_diy' => '自制',
'text_tag_mother_language' => '国语', 'text_tag_mother_language' => '国语',
'text_tag_mother_language_subtitle' => '中字', 'text_tag_mother_language_subtitle' => '中字',
'text_required' => '不能为空',
'text_invalid' => '非法',
); );
?> ?>
+31
View File
@@ -0,0 +1,31 @@
<?php
$lang_fields = [
'field_management' => '字段管理',
'text_manage' => '管理',
'text_add' => '添加',
'text_field' => '字段',
'text_delete' => '刪除',
'text_edit' => '編輯',
'col_id' => 'ID',
'col_name' => 'Name',
'col_name_helptext' => '僅允許數字、字母、下劃線',
'col_label' => '顯示標簽',
'col_type' => '類型',
'col_required' => '不能為空',
'col_help' => '輔助說明',
'col_options' => '選項',
'col_options_helptext' => '類型為單選、多選、下拉時必填,一行一個,格式:選項值|選項描述文本',
'col_action' => '操作',
'col_is_single_row' => '展示時單獨一行',
'js_sure_to_delete_this' => '你確信要刪除此項目嗎?',
'submit_submit' => '提交',
'field_type_text' => '短文本',
'field_type_textarea' => '長文本',
'field_type_radio' => '橫向單選',
'field_type_checkbox' => '橫向多選',
'field_type_select' => '下拉單選',
'field_type_image' => '圖片',
'field_type_file' => '文件',
];
+2
View File
@@ -313,6 +313,8 @@ $lang_functions = array
'text_tag_diy' => '自制', 'text_tag_diy' => '自制',
'text_tag_mother_language' => '國語', 'text_tag_mother_language' => '國語',
'text_tag_mother_language_subtitle' => '中字', 'text_tag_mother_language_subtitle' => '中字',
'text_required' => '不能為空',
'text_invalid' => '非法',
); );
?> ?>
+31
View File
@@ -0,0 +1,31 @@
<?php
$lang_fields = [
'field_management' => 'Custom field managent',
'text_manage' => 'Manage',
'text_add' => 'Add',
'text_field' => 'Field',
'text_delete' => 'Delete',
'text_edit' => 'Edit',
'col_id' => 'ID',
'col_name' => 'Name',
'col_name_helptext' => 'Only allow digit, alphabet, underline',
'col_label' => 'Display label',
'col_type' => 'Type',
'col_required' => 'Required',
'col_help' => 'Helptext',
'col_options' => 'Options',
'col_options_helptext' => '类型为单选、多选、下拉时必填,一行一个,格式:选项值|选项描述文本',
'col_action' => '操作',
'col_is_single_row' => '展示时单独一行',
'js_sure_to_delete_this' => '你确信要删除此项目吗?',
'submit_submit' => '提交',
'field_type_text' => '短文本',
'field_type_textarea' => '长文本',
'field_type_radio' => '横向单选',
'field_type_checkbox' => '横向多选',
'field_type_select' => '下拉单选',
'field_type_image' => '图片',
'field_type_file' => '文件',
];
+2
View File
@@ -314,6 +314,8 @@ $lang_functions = array
'text_tag_diy' => 'DIY', 'text_tag_diy' => 'DIY',
'text_tag_mother_language' => 'Mother language', 'text_tag_mother_language' => 'Mother language',
'text_tag_mother_language_subtitle' => 'Mother language subtitle', 'text_tag_mother_language_subtitle' => 'Mother language subtitle',
'text_required' => 'Required',
'text_invalid' => 'Invalid',
); );
?> ?>
+60 -33
View File
@@ -52,6 +52,8 @@ class Field
], ],
]; ];
private $preparedTorrentCustomFieldValues = [];
public function getTypeHuman($type) public function getTypeHuman($type)
{ {
global $lang_fields; global $lang_fields;
@@ -91,14 +93,14 @@ class Field
function buildFieldForm(array $row = []) function buildFieldForm(array $row = [])
{ {
global $lang_fields; global $lang_fields, $lang_functions;
$trName = tr($lang_fields['col_name'] . '<font color="red">*</font>', '<input type="text" name="name" value="' . ($row['name'] ?? '') . '" style="width: 300px" />&nbsp;&nbsp;仅允许数字、字母、下划线', 1, '', true); $trName = tr($lang_fields['col_name'] . '<font color="red">*</font>', '<input type="text" name="name" value="' . ($row['name'] ?? '') . '" style="width: 300px" />&nbsp;&nbsp;' . $lang_fields['col_name_helptext'], 1, '', true);
$trLabel = tr($lang_fields['col_label'] . '<font color="red">*</font>', '<input type="text" name="label" value="' . ($row['label'] ?? '') . '" style="width: 300px" />', 1, '', true); $trLabel = tr($lang_fields['col_label'] . '<font color="red">*</font>', '<input type="text" name="label" value="' . ($row['label'] ?? '') . '" style="width: 300px" />', 1, '', true);
$trType = tr($lang_fields['col_type'] . '<font color="red">*</font>', $this->radio('type', $this->getTypeRadioOptions(), $row['type'] ?? null), 1, '', true); $trType = tr($lang_fields['col_type'] . '<font color="red">*</font>', $this->radio('type', $this->getTypeRadioOptions(), $row['type'] ?? null), 1, '', true);
$trRequired = tr($lang_fields['col_required'] . '<font color="red">*</font>', $this->radio('required', ['0' => '否', '1' => '是'], $row['required'] ?? null), 1, '', true); $trRequired = tr($lang_fields['col_required'] . '<font color="red">*</font>', $this->radio('required', ['0' => $lang_functions['text_no'], '1' => $lang_functions['text_yes']], $row['required'] ?? null), 1, '', true);
$trHelp = tr($lang_fields['col_help'], '<textarea name="help" rows="4" cols="80">' . ($row['help'] ?? '') . '</textarea>', 1, '', true); $trHelp = tr($lang_fields['col_help'], '<textarea name="help" rows="4" cols="80">' . ($row['help'] ?? '') . '</textarea>', 1, '', true);
$trOptions = tr($lang_fields['col_options'], '<textarea name="options" rows="6" cols="80">' . ($row['options'] ?? '') . '</textarea><br/>类型为单选、多选、下拉时必填,一行一个,格式:选项值|选项描述文本', 1, '', true); $trOptions = tr($lang_fields['col_options'], '<textarea name="options" rows="6" cols="80">' . ($row['options'] ?? '') . '</textarea><br/>' . $lang_fields['col_options_helptext'], 1, '', true);
$trIsSingleRow = tr($lang_fields['col_is_single_row'] . '<font color="red">*</font>', $this->radio('is_single_row', ['0' => '否', '1' => '是'], $row['is_single_row'] ?? null), 1, '', true); $trIsSingleRow = tr($lang_fields['col_is_single_row'] . '<font color="red">*</font>', $this->radio('is_single_row', ['0' => $lang_functions['text_no'], '1' => $lang_functions['text_yes']], $row['is_single_row'] ?? null), 1, '', true);
$id = $row['id'] ?? 0; $id = $row['id'] ?? 0;
$form = <<<HTML $form = <<<HTML
<div> <div>
@@ -127,7 +129,7 @@ HTML;
function buildFieldTable() function buildFieldTable()
{ {
global $lang_fields; global $lang_fields, $lang_functions;
$perPage = 10; $perPage = 10;
$total = get_row_count('torrents_custom_fields'); $total = get_row_count('torrents_custom_fields');
list($paginationTop, $paginationBottom, $limit) = pager($perPage, $total, "?"); list($paginationTop, $paginationBottom, $limit) = pager($perPage, $total, "?");
@@ -144,8 +146,8 @@ HTML;
]; ];
$rows = []; $rows = [];
while ($row = mysql_fetch_assoc($res)) { while ($row = mysql_fetch_assoc($res)) {
$row['required_text'] = $row['required'] ? '是' : '否'; $row['required_text'] = $row['required'] ? $lang_functions['text_yes'] : $lang_functions['text_no'];
$row['is_single_row_text'] = $row['is_single_row'] ? '是' : '否'; $row['is_single_row_text'] = $row['is_single_row'] ? $lang_functions['text_yes'] : $lang_functions['text_no'];
$row['type_text'] = sprintf('%s(%s)', $this->getTypeHuman($row['type']), $row['type']); $row['type_text'] = sprintf('%s(%s)', $this->getTypeHuman($row['type']), $row['type']);
$row['action'] = sprintf( $row['action'] = sprintf(
"<a href=\"javascript:confirm_delete('%s', '%s', '');\">%s</a> | <a href=\"?action=edit&type=&id=%s\">%s</a>", "<a href=\"javascript:confirm_delete('%s', '%s', '');\">%s</a> | <a href=\"?action=edit&type=&id=%s\">%s</a>",
@@ -175,41 +177,42 @@ HEAD;
public function save($data) public function save($data)
{ {
global $lang_functions, $lang_fields;
$attributes = []; $attributes = [];
if (empty($data['name'])) { if (empty($data['name'])) {
throw new \InvalidArgumentException("Name 必须"); throw new \InvalidArgumentException("{$lang_fields['col_name']} {$lang_functions['text_required']}");
} }
if (!preg_match('/^\w+$/', $data['name'])) { if (!preg_match('/^\w+$/', $data['name'])) {
throw new \InvalidArgumentException("Name 非法"); throw new \InvalidArgumentException("{$lang_fields['col_name']} {$lang_functions['text_invalid']}");
} }
$attributes['name'] = $data['name']; $attributes['name'] = $data['name'];
if (empty($data['label'])) { if (empty($data['label'])) {
throw new \InvalidArgumentException("显示标签 必须"); throw new \InvalidArgumentException("{$lang_fields['col_label']} {$lang_functions['text_required']}");
} }
$attributes['label'] = $data['label']; $attributes['label'] = $data['label'];
if (empty($data['type'])) { if (empty($data['type'])) {
throw new \InvalidArgumentException("类型 必须"); throw new \InvalidArgumentException("{$lang_fields['col_type']} {$lang_functions['text_required']}");
} }
if (!isset(self::$types[$data['type']])) { if (!isset(self::$types[$data['type']])) {
throw new \InvalidArgumentException("类型 非法"); throw new \InvalidArgumentException("{$lang_fields['col_type']} {$lang_functions['text_invalid']}");
} }
$attributes['type'] = $data['type']; $attributes['type'] = $data['type'];
if (!isset($data['required'])) { if (!isset($data['required'])) {
throw new \InvalidArgumentException("不能为空 必须"); throw new \InvalidArgumentException("{$lang_fields['col_required']} {$lang_functions['text_required']}");
} }
if (!in_array($data['required'], ["0", "1"], true)) { if (!in_array($data['required'], ["0", "1"], true)) {
throw new \InvalidArgumentException("不能为空 非法"); throw new \InvalidArgumentException("{$lang_fields['col_name']} {$lang_functions['text_invalid']}");
} }
$attributes['required'] = $data['required']; $attributes['required'] = $data['required'];
if (!isset($data['is_single_row'])) { if (!isset($data['is_single_row'])) {
throw new \InvalidArgumentException("展示时单独一行 必须"); throw new \InvalidArgumentException("{$lang_fields['col_is_single_row']} {$lang_functions['text_required']}");
} }
if (!in_array($data['is_single_row'], ["0", "1"], true)) { if (!in_array($data['is_single_row'], ["0", "1"], true)) {
throw new \InvalidArgumentException("展示时单独一行 非法"); throw new \InvalidArgumentException("{$lang_fields['col_is_single_row']} {$lang_functions['text_invalid']}");
} }
$attributes['is_single_row'] = $data['is_single_row']; $attributes['is_single_row'] = $data['is_single_row'];
@@ -371,16 +374,27 @@ JS;
return $html; return $html;
} }
public function listTorrentCustomField($torrentId) public function listTorrentCustomField($torrentId, $searchBoxId = 0)
{ {
global $browsecatmode; global $browsecatmode;
$res = sql_query("select f.*, v.custom_field_value from torrents_custom_field_values v inner join torrents_custom_fields f on v.custom_field_id = f.id inner join searchbox box on box.id = $browsecatmode and find_in_set(f.id, box.custom_fields) where torrent_id = $torrentId"); if ($searchBoxId <= 0) {
$searchBoxId = $browsecatmode;
}
//suppose torrentId is array
$isArray = true;
$torrentIdArr = $torrentId;
if (!is_array($torrentId)) {
$isArray = false;
$torrentIdArr = [$torrentId];
}
$torrentIdStr = implode(',', $torrentIdArr);
$res = sql_query("select f.*, v.custom_field_value, v.torrent_id from torrents_custom_field_values v inner join torrents_custom_fields f on v.custom_field_id = f.id inner join searchbox box on box.id = $searchBoxId and find_in_set(f.id, box.custom_fields) where torrent_id in ($torrentIdStr)");
$values = []; $values = [];
$result = []; $result = [];
while ($row = mysql_fetch_assoc($res)) { while ($row = mysql_fetch_assoc($res)) {
$typeInfo = self::$types[$row['type']]; $typeInfo = self::$types[$row['type']];
if ($typeInfo['has_option']) { if ($typeInfo['has_option']) {
$options = preg_split('/[\r\n]+/', $row['options']); $options = preg_split('/[\r\n]+/', trim($row['options']));
$optionsArr = []; $optionsArr = [];
foreach ($options as $option) { foreach ($options as $option) {
$pos = strpos($option, '|'); $pos = strpos($option, '|');
@@ -390,17 +404,19 @@ JS;
} }
$row['options'] = $optionsArr; $row['options'] = $optionsArr;
} }
$result[$row['id']] = $row; $result[$row['torrent_id']][$row['id']] = $row;
if ($typeInfo['is_value_multiple']) { if ($typeInfo['is_value_multiple']) {
$values[$row['id']][] = $row['custom_field_value']; $values[$row['torrent_id']][$row['id']][] = $row['custom_field_value'];
} else { } else {
$values[$row['id']] = $row['custom_field_value']; $values[$row['torrent_id']][$row['id']] = $row['custom_field_value'];
} }
} }
foreach ($result as &$value) { foreach ($result as $tid => &$fields) {
$value['custom_field_value'] = $values[$value['id']]; foreach ($fields as &$field) {
$field['custom_field_value'] = $values[$tid][$field['id']];
}
} }
return $result; return $isArray ? $result : $result[$torrentId];
} }
public function renderOnTorrentDetailsPage($torrentId) public function renderOnTorrentDetailsPage($torrentId)
@@ -460,20 +476,31 @@ JS;
return $result; return $result;
} }
public function getFieldValue($torrentId, $fieldName = null) public function prepareTorrents(array $torrentIdArr)
{ {
static $result; $customFieldValues = $this->listTorrentCustomField($torrentIdArr);
if (is_null($result)) { $result = [];
$customFields = $this->listTorrentCustomField($torrentId); foreach ($customFieldValues as $tid => &$customFields) {
$result = [];
foreach ($customFields as &$field) { foreach ($customFields as &$field) {
$field['custom_field_value_formatted'] = $this->formatCustomFieldValue($field); $field['custom_field_value_formatted'] = $this->formatCustomFieldValue($field);
$result[$field['name']] = $field; $result[$tid][$field['name']] = $field;
} }
} }
return is_null($fieldName) ? $result : ($result[$fieldName] ?? ''); $this->preparedTorrentCustomFieldValues = $result;
}
public function getPreparedTorrent($torrentId = null, $fieldName = null)
{
if (is_null($torrentId)) {
return $this->preparedTorrentCustomFieldValues;
}
if (is_null($fieldName)) {
return $this->preparedTorrentCustomFieldValues[$torrentId] ?? [];
}
return $this->preparedTorrentCustomFieldValues[$torrentId][$fieldName] ?? '';
} }
} }