custom fields in upload.php

This commit is contained in:
xiaomlove
2021-03-03 19:29:29 +08:00
parent 25a69891f4
commit 723a6fadc9
7 changed files with 153 additions and 15 deletions

View File

@@ -220,8 +220,8 @@ function formatCode($text) {
return addTempCode("<br /><div class=\"codetop\">".$lang_functions['text_code']."</div><div class=\"codemain\">$text</div><br />");
}
function formatImg($src, $enableImageResizer, $image_max_width, $image_max_height) {
return addTempCode("<img alt=\"image\" src=\"$src\"" .($enableImageResizer ? " onload=\"Scale(this,$image_max_width,$image_max_height);\" onclick=\"Preview(this);\"" : "") . " />");
function formatImg($src, $enableImageResizer, $image_max_width, $image_max_height, $imgId = "") {
return addTempCode("<img id=\"$imgId\" alt=\"image\" src=\"$src\"" .($enableImageResizer ? " onload=\"Scale(this,$image_max_width,$image_max_height);\" onclick=\"Preview(this);\"" : "") . " />");
}
function formatFlash($src, $width, $height) {

View File

@@ -152,9 +152,6 @@ class DB
public static function update($table, $data, $whereStr)
{
if (empty($table) || empty($data) || !is_array($data) || empty($whereStr)) {
throw new DatabaseException("require table and data(array) and whereStr.");
}
$updateArr = [];
foreach ($data as $field => $value) {
$updateArr[] = "`$field` = " . sqlesc($value);
@@ -164,6 +161,16 @@ class DB
return mysql_affected_rows();
}
public static function delete($table, $whereStr, $limit = null)
{
$sql = "delete from $table where $whereStr";
if (!is_null($limit)) {
$sql .= " limit $limit";
}
sql_query($sql);
return mysql_affected_rows();
}
public static function getOne($table, $whereStr, $fields = '*')
{
if ($fields != '*') {

View File

@@ -11,6 +11,7 @@ class Field
const TYPE_RADIO = 'radio';
const TYPE_CHECKBOX = 'checkbox';
const TYPE_SELECT = 'select';
const TYPE_IMAGE = 'image';
const TYPE_FILE = 'file';
public static $types = [
@@ -19,6 +20,7 @@ class Field
self::TYPE_RADIO => '横向单选(radio)',
self::TYPE_CHECKBOX => '横向多选(checkbox)',
self::TYPE_SELECT => '下拉单选(select)',
self::TYPE_IMAGE => '图片(image)',
self::TYPE_FILE => '文件(file)',
];
@@ -197,14 +199,107 @@ HEAD;
}
public function renderUploadPage()
public function renderUploadPage(array $customValues = [])
{
$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']);
$sql = sprintf('select * from torrents_custom_fields where id in (%s)', $searchBox['custom_fields']);
$res = sql_query($sql);
$html = '';
while ($row = mysql_fetch_assoc($res)) {
$name = "custom_fields[{$row['id']}]";
$currentValue = $customValues[$row['id']] ?? '';
if ($row['type'] == self::TYPE_TEXT) {
$html .= tr($row['label'], sprintf('<input type="text" name="%s" value="%s" style="width: 650px"/>', $name, $currentValue), 1);
} elseif ($row['type'] == self::TYPE_TEXTAREA) {
$html .= tr($row['label'], sprintf('<textarea name="%s" rows="4" style="width: 650px">%s</textarea>', $name, $currentValue), 1);
} elseif ($row['type'] == self::TYPE_RADIO || $row['type'] == self::TYPE_CHECKBOX) {
$part = "";
foreach (preg_split('/[\r\n]+/', trim($row['options'])) as $option) {
if (empty($option) || ($pos = strpos($option, '|')) === false) {
continue;
}
$key = substr($option, 0, $pos);
$value = substr($option, $pos + 1);
$checked = "";
if ($row['type'] == self::TYPE_RADIO && (string)$currentValue === (string)$value) {
$checked = " checked";
}
if ($row['type'] == self::TYPE_CHECKBOX && in_array($value, (array)$currentValue)) {
$checked = " checked";
}
$part .= sprintf(
'<label style="margin-right: 4px"><input type="%s" name="%s" value="%s"%s />%s</label>',
$row['type'], $name, $key, $checked, $value
);
}
$html .= tr($row['label'], $part, 1);
} elseif ($row['type'] == self::TYPE_SELECT) {
$part = '<select name="' . $name . '">';
foreach (preg_split('/[\r\n]+/', trim($row['options'])) as $option) {
if (empty($option) || ($pos = strpos($option, '|')) === false) {
continue;
}
$key = substr($option, 0, $pos);
$value = substr($option, $pos + 1);
$selected = "";
if (in_array($value, (array)$currentValue)) {
$selected = " selected";
}
$part .= sprintf(
'<option value="%s"%s>%s</option>',
$key, $selected, $value
);
}
$part .= '</select>';
$html .= tr($row['label'], $part, 1);
} elseif ($row['type'] == self::TYPE_IMAGE) {
$callbackFunc = "preview_custom_field_image_" . $row['id'];
$iframeId = "iframe_$callbackFunc";
$inputId = "input_$callbackFunc";
$imgId = "attach" . $row['id'];
$previewBoxId = "preview_$callbackFunc";
$y = '<iframe id="' . $iframeId . '" src="' . getSchemeAndHttpHost() . '/attachment.php?callback_func=' . $callbackFunc . '" width="100%" height="24" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>';
$y .= sprintf('<input id="%s" type="text" name="%s" value="%s" style="width: 650px;margin: 10px 0">', $inputId, $name, $currentValue);
$y .= '<div id="' . $previewBoxId . '">';
if (!empty($currentValue)) {
if (substr($currentValue, 0, 4) == 'http') {
$y .= formatImg($currentValue, true, 700, 0, $imgId);
} else {
$y .= format_comment($currentValue);
}
}
$y .= '</div>';
$y .= <<<JS
<script>
function {$callbackFunc}(delkey, url)
{
var previewBox = $('$previewBoxId')
var existsImg = $('$imgId')
var input = $('$inputId')
if (existsImg) {
previewBox.removeChild(existsImg)
input.value = ''
}
var img = document.createElement('img')
img.src=url
img.setAttribute('onload', 'Scale(this, 700, 0);')
img.setAttribute('onclick', 'Preview(this);')
input.value = '[attach]' + delkey + '[/attach]'
img.id='$imgId'
previewBox.appendChild(img)
}
</script>
JS;
$html .= tr($row['label'], $y, 1);
} elseif ($row['type'] == self::TYPE_FILE) {
$html .= tr($row['label'], sprintf('<input type="file" name="%s" />', $name), 1);
}
}
return $html;
}
}

View File

@@ -230,11 +230,19 @@ if ($Attach->enable_attachment())
$dlkey = md5($db_file_location.".".$ext);
sql_query("INSERT INTO attachments (userid, width, added, filename, filetype, filesize, location, dlkey, isimage, thumb) VALUES (".$CURUSER['id'].", ".$width.", ".sqlesc(date("Y-m-d H:i:s")).", ".sqlesc($origfilename).", ".sqlesc($filetype).", ".$filesize.", ".sqlesc($db_file_location.".".$ext).", ".sqlesc($dlkey).", ".($isimage ? 1 : 0).", ".($hasthumb ? 1 : 0).")") or sqlerr(__FILE__, __LINE__);
$count_left--;
echo("<script type=\"text/javascript\">parent.tag_extimage('". "[attach]" . $dlkey . "[/attach]" . "');</script>");
if (!empty($_REQUEST['callback_func'])) {
$url = $httpdirectory_attachment."/".$db_file_location . ".$ext";
if ($hasthumb) {
$url .= ".thumb.jpg";
}
echo sprintf('<script type="text/javascript">parent.%s("%s", "%s")</script>', $_REQUEST['callback_func'], $dlkey, $url);
} else {
echo("<script type=\"text/javascript\">parent.tag_extimage('". "[attach]" . $dlkey . "[/attach]" . "');</script>");
}
}
}
}
print("<form enctype=\"multipart/form-data\" name=\"attachment\" method=\"post\" action=\"attachment.php\">");
print("<form enctype=\"multipart/form-data\" name=\"attachment\" method=\"post\" action=\"attachment.php?callback_func=" . ($_REQUEST['callback_func'] ?? '') . "\">");
print("<tr>");
print("<td class=\"embedded\" colspan=\"2\" align=left>");
print("<input type=\"file\" name=\"file\"".($count_left ? "" : " disabled=\"disabled\"")." />&nbsp;");

View File

@@ -12,6 +12,16 @@ $res = sql_query("SELECT torrents.*, categories.mode as cat_mode FROM torrents L
$row = mysql_fetch_array($res);
if (!$row) die();
/**
* custom fields
* @since v1.6
*/
$customFieldValueRes = sql_query("select * from torrents_custom_field_values where torrent_id = $id");
$customFieldValues = [];
while ($row = mysql_fetch_assoc($res)) {
$customFieldValues[$row['custom_field_id']] = unserialize($row['custom_field_value']);
}
if ($enablespecial == 'yes' && get_user_class() >= $movetorrent_class)
$allowmove = true; //enable moving torrent to other section
else $allowmove = false;

View File

@@ -352,6 +352,24 @@ if (!$ret) {
}
$id = mysql_insert_id();
/**
* add custom fields
* @since v1.6
*/
if (!empty($_POST['custom_fields'])) {
$now = date('Y-m-d H:i:s');
foreach ($_POST['custom_fields'] as $customField => $customValue) {
$customData = [
'torrent_id' => $id,
'custom_field_id' => $customField,
'custom_field_value' => serialize($customValue),
'created_at' => $now,
'updated_at' => $now,
];
\Nexus\Database\DB::insert('torrents_custom_field_values', $customData);
}
}
@sql_query("DELETE FROM files WHERE torrent = $id");
foreach ($filelist as $file) {
@sql_query("INSERT INTO files (torrent, filename, size) VALUES ($id, ".sqlesc($file[0]).",".$file[1].")");

View File

@@ -64,11 +64,11 @@ stdhead($lang_upload['head_upload']);
$ptGen = new \Nexus\PTGen\PTGen();
echo $ptGen->renderUploadPageFormInput("");
}
if ($enablenfo_main=='yes')
tr($lang_upload['row_nfo_file'], "<input type=\"file\" class=\"file\" name=\"nfo\" /><br /><font class=\"medium\">".$lang_upload['text_only_viewed_by'].get_user_class_name($viewnfo_class,false,true,true).$lang_upload['text_or_above']."</font>", 1);
$field = new \Nexus\Field\Field();
$field = new \Nexus\Field\Field();
$field->renderUploadPage();
if ($enablenfo_main=='yes') {
tr($lang_upload['row_nfo_file'], "<input type=\"file\" class=\"file\" name=\"nfo\" /><br /><font class=\"medium\">".$lang_upload['text_only_viewed_by'].get_user_class_name($viewnfo_class,false,true,true).$lang_upload['text_or_above']."</font>", 1);
}
print("<tr><td class=\"rowhead\" style='padding: 3px' valign=\"top\">".$lang_upload['row_description']."<font color=\"red\">*</font></td><td class=\"rowfollow\">");
textbbcode("upload","descr","",false);
print("</td></tr>\n");