From cf301fef89c5f590bf12ec0af848d696f9a6487f Mon Sep 17 00:00:00 2001 From: xiaomlove <353856593@qq.com> Date: Sat, 6 Mar 2021 19:16:58 +0800 Subject: [PATCH] custom fields add i18n and getPreparedTorrents() --- include/functions.php | 2 +- lang/chs/lang_fields.php | 2 + lang/chs/lang_functions.php | 2 + lang/cht/lang_fields.php | 31 +++++++++++++ lang/cht/lang_functions.php | 2 + lang/en/lang_fields.php | 31 +++++++++++++ lang/en/lang_functions.php | 2 + nexus/Field/Field.php | 93 ++++++++++++++++++++++++------------- 8 files changed, 131 insertions(+), 34 deletions(-) create mode 100644 lang/cht/lang_fields.php create mode 100644 lang/en/lang_fields.php diff --git a/include/functions.php b/include/functions.php index 57ee5c4a..2984c1da 100644 --- a/include/functions.php +++ b/include/functions.php @@ -4642,7 +4642,7 @@ function displayHotAndClassic() { $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) { $movies_list = ""; diff --git a/lang/chs/lang_fields.php b/lang/chs/lang_fields.php index 4a024064..1f690530 100644 --- a/lang/chs/lang_fields.php +++ b/lang/chs/lang_fields.php @@ -9,11 +9,13 @@ $lang_fields = [ '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' => '你确信要删除此项目吗?', diff --git a/lang/chs/lang_functions.php b/lang/chs/lang_functions.php index 1e44f793..a6f184ed 100644 --- a/lang/chs/lang_functions.php +++ b/lang/chs/lang_functions.php @@ -312,6 +312,8 @@ $lang_functions = array 'text_tag_diy' => '自制', 'text_tag_mother_language' => '国语', 'text_tag_mother_language_subtitle' => '中字', + 'text_required' => '不能为空', + 'text_invalid' => '非法', ); ?> diff --git a/lang/cht/lang_fields.php b/lang/cht/lang_fields.php new file mode 100644 index 00000000..38e70f3a --- /dev/null +++ b/lang/cht/lang_fields.php @@ -0,0 +1,31 @@ + '字段管理', + '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' => '文件', + +]; \ No newline at end of file diff --git a/lang/cht/lang_functions.php b/lang/cht/lang_functions.php index 1e07e16b..514f67d0 100644 --- a/lang/cht/lang_functions.php +++ b/lang/cht/lang_functions.php @@ -313,6 +313,8 @@ $lang_functions = array 'text_tag_diy' => '自制', 'text_tag_mother_language' => '國語', 'text_tag_mother_language_subtitle' => '中字', + 'text_required' => '不能為空', + 'text_invalid' => '非法', ); ?> diff --git a/lang/en/lang_fields.php b/lang/en/lang_fields.php new file mode 100644 index 00000000..a9d3332b --- /dev/null +++ b/lang/en/lang_fields.php @@ -0,0 +1,31 @@ + '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' => '文件', + +]; \ No newline at end of file diff --git a/lang/en/lang_functions.php b/lang/en/lang_functions.php index 2e405ce7..e1b0c398 100644 --- a/lang/en/lang_functions.php +++ b/lang/en/lang_functions.php @@ -314,6 +314,8 @@ $lang_functions = array 'text_tag_diy' => 'DIY', 'text_tag_mother_language' => 'Mother language', 'text_tag_mother_language_subtitle' => 'Mother language subtitle', + 'text_required' => 'Required', + 'text_invalid' => 'Invalid', ); ?> diff --git a/nexus/Field/Field.php b/nexus/Field/Field.php index c1ffa1b9..ccb2a89e 100644 --- a/nexus/Field/Field.php +++ b/nexus/Field/Field.php @@ -52,6 +52,8 @@ class Field ], ]; + private $preparedTorrentCustomFieldValues = []; + public function getTypeHuman($type) { global $lang_fields; @@ -91,14 +93,14 @@ class Field function buildFieldForm(array $row = []) { - global $lang_fields; - $trName = tr($lang_fields['col_name'] . '*', '  仅允许数字、字母、下划线', 1, '', true); + global $lang_fields, $lang_functions; + $trName = tr($lang_fields['col_name'] . '*', '  ' . $lang_fields['col_name_helptext'], 1, '', true); $trLabel = tr($lang_fields['col_label'] . '*', '', 1, '', true); $trType = tr($lang_fields['col_type'] . '*', $this->radio('type', $this->getTypeRadioOptions(), $row['type'] ?? null), 1, '', true); - $trRequired = tr($lang_fields['col_required'] . '*', $this->radio('required', ['0' => '否', '1' => '是'], $row['required'] ?? null), 1, '', true); + $trRequired = tr($lang_fields['col_required'] . '*', $this->radio('required', ['0' => $lang_functions['text_no'], '1' => $lang_functions['text_yes']], $row['required'] ?? null), 1, '', true); $trHelp = tr($lang_fields['col_help'], '', 1, '', true); - $trOptions = tr($lang_fields['col_options'], '
类型为单选、多选、下拉时必填,一行一个,格式:选项值|选项描述文本', 1, '', true); - $trIsSingleRow = tr($lang_fields['col_is_single_row'] . '*', $this->radio('is_single_row', ['0' => '否', '1' => '是'], $row['is_single_row'] ?? null), 1, '', true); + $trOptions = tr($lang_fields['col_options'], '
' . $lang_fields['col_options_helptext'], 1, '', true); + $trIsSingleRow = tr($lang_fields['col_is_single_row'] . '*', $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; $form = << @@ -127,7 +129,7 @@ HTML; function buildFieldTable() { - global $lang_fields; + global $lang_fields, $lang_functions; $perPage = 10; $total = get_row_count('torrents_custom_fields'); list($paginationTop, $paginationBottom, $limit) = pager($perPage, $total, "?"); @@ -144,8 +146,8 @@ HTML; ]; $rows = []; while ($row = mysql_fetch_assoc($res)) { - $row['required_text'] = $row['required'] ? '是' : '否'; - $row['is_single_row_text'] = $row['is_single_row'] ? '是' : '否'; + $row['required_text'] = $row['required'] ? $lang_functions['text_yes'] : $lang_functions['text_no']; + $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['action'] = sprintf( "%s | %s", @@ -175,41 +177,42 @@ HEAD; public function save($data) { + global $lang_functions, $lang_fields; $attributes = []; 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'])) { - throw new \InvalidArgumentException("Name 非法"); + throw new \InvalidArgumentException("{$lang_fields['col_name']} {$lang_functions['text_invalid']}"); } $attributes['name'] = $data['name']; if (empty($data['label'])) { - throw new \InvalidArgumentException("显示标签 必须"); + throw new \InvalidArgumentException("{$lang_fields['col_label']} {$lang_functions['text_required']}"); } $attributes['label'] = $data['label']; if (empty($data['type'])) { - throw new \InvalidArgumentException("类型 必须"); + throw new \InvalidArgumentException("{$lang_fields['col_type']} {$lang_functions['text_required']}"); } if (!isset(self::$types[$data['type']])) { - throw new \InvalidArgumentException("类型 非法"); + throw new \InvalidArgumentException("{$lang_fields['col_type']} {$lang_functions['text_invalid']}"); } $attributes['type'] = $data['type']; 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)) { - throw new \InvalidArgumentException("不能为空 非法"); + throw new \InvalidArgumentException("{$lang_fields['col_name']} {$lang_functions['text_invalid']}"); } $attributes['required'] = $data['required']; 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)) { - throw new \InvalidArgumentException("展示时单独一行 非法"); + throw new \InvalidArgumentException("{$lang_fields['col_is_single_row']} {$lang_functions['text_invalid']}"); } $attributes['is_single_row'] = $data['is_single_row']; @@ -371,16 +374,27 @@ JS; return $html; } - public function listTorrentCustomField($torrentId) + public function listTorrentCustomField($torrentId, $searchBoxId = 0) { 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 = []; $result = []; while ($row = mysql_fetch_assoc($res)) { $typeInfo = self::$types[$row['type']]; if ($typeInfo['has_option']) { - $options = preg_split('/[\r\n]+/', $row['options']); + $options = preg_split('/[\r\n]+/', trim($row['options'])); $optionsArr = []; foreach ($options as $option) { $pos = strpos($option, '|'); @@ -390,17 +404,19 @@ JS; } $row['options'] = $optionsArr; } - $result[$row['id']] = $row; + $result[$row['torrent_id']][$row['id']] = $row; if ($typeInfo['is_value_multiple']) { - $values[$row['id']][] = $row['custom_field_value']; + $values[$row['torrent_id']][$row['id']][] = $row['custom_field_value']; } else { - $values[$row['id']] = $row['custom_field_value']; + $values[$row['torrent_id']][$row['id']] = $row['custom_field_value']; } } - foreach ($result as &$value) { - $value['custom_field_value'] = $values[$value['id']]; + foreach ($result as $tid => &$fields) { + foreach ($fields as &$field) { + $field['custom_field_value'] = $values[$tid][$field['id']]; + } } - return $result; + return $isArray ? $result : $result[$torrentId]; } public function renderOnTorrentDetailsPage($torrentId) @@ -460,20 +476,31 @@ JS; return $result; } - public function getFieldValue($torrentId, $fieldName = null) + public function prepareTorrents(array $torrentIdArr) { - static $result; - if (is_null($result)) { - $customFields = $this->listTorrentCustomField($torrentId); - $result = []; + $customFieldValues = $this->listTorrentCustomField($torrentIdArr); + $result = []; + foreach ($customFieldValues as $tid => &$customFields) { foreach ($customFields as &$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] ?? ''; } + + } \ No newline at end of file