2021-03-02 21:03:02 +08:00
< ? php
namespace Nexus\Field ;
2022-09-19 16:27:04 +08:00
use App\Models\SearchBox ;
2022-10-02 21:14:11 +08:00
use App\Models\Tag ;
2022-09-19 16:27:04 +08:00
use App\Models\TorrentCustomField ;
use App\Models\TorrentCustomFieldValue ;
use Elasticsearch\Endpoints\Search ;
2021-06-08 10:42:39 +08:00
use Nexus\Database\NexusDB ;
2021-03-02 21:03:02 +08:00
class Field
{
const TYPE_TEXT = 'text' ;
const TYPE_TEXTAREA = 'textarea' ;
const TYPE_RADIO = 'radio' ;
const TYPE_CHECKBOX = 'checkbox' ;
const TYPE_SELECT = 'select' ;
2021-03-03 19:29:29 +08:00
const TYPE_IMAGE = 'image' ;
2021-03-02 21:03:02 +08:00
public static $types = [
2021-03-04 20:43:55 +08:00
self :: TYPE_TEXT => [
'text' => 'text' ,
'has_option' => false ,
'is_value_multiple' => false ,
],
self :: TYPE_TEXTAREA => [
'text' => 'textarea' ,
'has_option' => false ,
'is_value_multiple' => false ,
],
self :: TYPE_RADIO => [
'text' => 'radio' ,
'has_option' => true ,
'is_value_multiple' => false ,
],
self :: TYPE_CHECKBOX => [
'text' => 'checkbox' ,
'has_option' => true ,
'is_value_multiple' => true ,
],
self :: TYPE_SELECT => [
'text' => 'select' ,
'has_option' => true ,
'is_value_multiple' => false ,
],
self :: TYPE_IMAGE => [
'text' => 'image' ,
'has_option' => false ,
'is_value_multiple' => false ,
],
2021-03-02 21:03:02 +08:00
];
2021-03-06 19:16:58 +08:00
private $preparedTorrentCustomFieldValues = [];
2021-03-04 20:43:55 +08:00
public function getTypeHuman ( $type )
{
$map = [
2025-12-28 22:36:52 +08:00
self :: TYPE_TEXT => nexus_trans ( 'field.type.text' ),
self :: TYPE_TEXTAREA => nexus_trans ( 'field.type.textarea' ),
self :: TYPE_RADIO => nexus_trans ( 'field.type.radio' ),
self :: TYPE_CHECKBOX => nexus_trans ( 'field.type.checkbox' ),
self :: TYPE_SELECT => nexus_trans ( 'field.type.select' ),
self :: TYPE_IMAGE => nexus_trans ( 'field.type.image' ),
2021-03-04 20:43:55 +08:00
];
return $map [ $type ] ? ? '' ;
}
public function getTypeRadioOptions ()
{
$out = [];
foreach ( self :: $types as $key => $value ) {
2021-03-18 02:01:12 +08:00
$out [ $key ] = sprintf ( '%s(%s)' , $value [ 'text' ], $this -> getTypeHuman ( $key ));
2021-03-04 20:43:55 +08:00
}
return $out ;
}
2021-03-03 00:23:08 +08:00
2021-03-02 21:03:02 +08:00
public function radio ( $name , $options , $current = null )
{
$arr = [];
foreach ( $options as $value => $label ) {
$arr [] = sprintf (
2021-03-03 00:23:08 +08:00
'<label style="margin-right: 4px;"><input type="radio" name="%s" value="%s"%s />%s</label>' ,
2021-03-02 21:03:02 +08:00
$name , $value , ( string ) $current === ( string ) $value ? ' checked' : '' , $label
);
}
return implode ( '' , $arr );
}
function buildFieldForm ( array $row = [])
{
2022-09-19 16:27:04 +08:00
global $lang_fields , $lang_functions , $lang_catmanage ;
2021-03-18 02:01:12 +08:00
$trName = tr ( $lang_fields [ 'col_name' ] . '<font color="red">*</font>' , '<input type="text" name="name" value="' . ( $row [ 'name' ] ? ? '' ) . '" style="width: 300px" /> ' . $lang_fields [ 'col_name_help' ], 1 , '' , true );
2021-03-02 21:03:02 +08:00
$trLabel = tr ( $lang_fields [ 'col_label' ] . '<font color="red">*</font>' , '<input type="text" name="label" value="' . ( $row [ 'label' ] ? ? '' ) . '" style="width: 300px" />' , 1 , '' , true );
2021-03-04 20:43:55 +08:00
$trType = tr ( $lang_fields [ 'col_type' ] . '<font color="red">*</font>' , $this -> radio ( 'type' , $this -> getTypeRadioOptions (), $row [ 'type' ] ? ? null ), 1 , '' , true );
2021-03-06 19:16:58 +08:00
$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 );
2021-03-02 21:03:02 +08:00
$trHelp = tr ( $lang_fields [ 'col_help' ], '<textarea name="help" rows="4" cols="80">' . ( $row [ 'help' ] ? ? '' ) . '</textarea>' , 1 , '' , true );
2021-03-18 02:01:12 +08:00
$trOptions = tr ( $lang_fields [ 'col_options' ], '<textarea name="options" rows="6" cols="80">' . ( $row [ 'options' ] ? ? '' ) . '</textarea><br/>' . $lang_fields [ 'col_options_help' ], 1 , '' , true );
2021-03-06 19:16:58 +08:00
$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 );
2022-09-19 16:27:04 +08:00
$trPriority = tr ( nexus_trans ( 'label.priority' ) . '<font color="red">*</font>' , '<input type="number" name="priority" value="' . ( $row [ 'priority' ] ? ? '0' ) . '" style="width: 300px" />' , 1 , '' , true );
$trDisplay = tr ( $lang_fields [ 'col_display' ], '<textarea name="display" rows="4" cols="80">' . ( $row [ 'display' ] ? ? '' ) . '</textarea><br/>' . $lang_catmanage [ 'row_custom_field_display_help' ], 1 , '' , true );
2021-03-02 21:03:02 +08:00
$id = $row [ 'id' ] ? ? 0 ;
$form = <<< HTML
2021-03-03 00:23:08 +08:00
< div >
2021-03-18 02:01:12 +08:00
< h1 align = " center " >< a class = " faqlink " href = " ?action=view " > { $lang_fields [ 'text_field' ]} </ a ></ h1 >
< form method = " post " action = " fields.php?action=submit " >
2021-03-02 21:03:02 +08:00
< div >
< table border = " 1 " cellspacing = " 0 " cellpadding = " 10 " width = " 100% " >
< input type = " hidden " name = " id " value = " { $id } " />
{ $trName }
{ $trLabel }
{ $trType }
{ $trRequired }
{ $trHelp }
{ $trOptions }
2021-03-04 20:43:55 +08:00
{ $trIsSingleRow }
2022-09-19 16:27:04 +08:00
{ $trPriority }
{ $trDisplay }
2021-03-02 21:03:02 +08:00
</ table >
</ div >
< div style = " text-align: center; margin-top: 10px; " >
< input type = " submit " value = " { $lang_fields [ 'submit_submit' ] } " />
</ div >
</ form >
</ div >
HTML ;
return $form ;
}
function buildFieldTable ()
{
2021-03-06 19:16:58 +08:00
global $lang_fields , $lang_functions ;
2021-03-03 00:23:08 +08:00
$perPage = 10 ;
$total = get_row_count ( 'torrents_custom_fields' );
list ( $paginationTop , $paginationBottom , $limit ) = pager ( $perPage , $total , " ? " );
2022-09-19 16:27:04 +08:00
$sql = " select * from torrents_custom_fields order by priority desc $limit " ;
2021-03-02 21:03:02 +08:00
$res = sql_query ( $sql );
$header = [
'id' => $lang_fields [ 'col_id' ],
'name' => $lang_fields [ 'col_name' ],
'label' => $lang_fields [ 'col_label' ],
2021-03-03 00:23:08 +08:00
'type_text' => $lang_fields [ 'col_type' ],
2021-03-02 21:03:02 +08:00
'required_text' => $lang_fields [ 'col_required' ],
2021-03-04 20:43:55 +08:00
'is_single_row_text' => $lang_fields [ 'col_is_single_row' ],
2022-09-19 16:27:04 +08:00
'priority' => nexus_trans ( 'label.priority' ),
2021-03-02 21:03:02 +08:00
'action' => $lang_fields [ 'col_action' ],
];
$rows = [];
while ( $row = mysql_fetch_assoc ( $res )) {
2021-03-06 19:16:58 +08:00
$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' ];
2021-03-04 20:43:55 +08:00
$row [ 'type_text' ] = sprintf ( '%s(%s)' , $this -> getTypeHuman ( $row [ 'type' ]), $row [ 'type' ]);
2021-03-02 21:03:02 +08:00
$row [ 'action' ] = sprintf (
2021-03-18 02:01:12 +08:00
" <a href= \" javascript:confirm_delete('%s', '%s', ''); \" >%s</a> | <a href= \" ?action=edit&id=%s \" >%s</a> " ,
2021-03-02 21:03:02 +08:00
$row [ 'id' ], $lang_fields [ 'js_sure_to_delete_this' ], $lang_fields [ 'text_delete' ], $row [ 'id' ], $lang_fields [ 'text_edit' ]
);
$rows [] = $row ;
}
2021-03-03 00:23:08 +08:00
$head = <<< HEAD
2021-03-18 02:01:12 +08:00
< h1 align = " center " > { $lang_fields [ 'field_management' ]} </ h1 >
2021-03-03 00:23:08 +08:00
< div style = " margin-bottom: 8px; " >
< span id = " add " >
2021-03-18 02:01:12 +08:00
< a href = " ?action=add " class = " big " >< b > { $lang_fields [ 'text_add' ]} </ b ></ a >
2021-03-03 00:23:08 +08:00
</ span >
</ div >
HEAD ;
2021-03-02 21:03:02 +08:00
$table = $this -> buildTable ( $header , $rows );
2021-03-03 00:23:08 +08:00
return $head . $table . $paginationBottom ;
2021-03-02 21:03:02 +08:00
}
public function save ( $data )
{
2021-03-06 19:16:58 +08:00
global $lang_functions , $lang_fields ;
2021-03-02 21:03:02 +08:00
$attributes = [];
if ( empty ( $data [ 'name' ])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_name' ] } { $lang_functions [ 'text_required' ] } " );
2021-03-02 21:03:02 +08:00
}
if ( ! preg_match ( '/^\w+$/' , $data [ 'name' ])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_name' ] } { $lang_functions [ 'text_invalid' ] } " );
2021-03-02 21:03:02 +08:00
}
$attributes [ 'name' ] = $data [ 'name' ];
if ( empty ( $data [ 'label' ])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_label' ] } { $lang_functions [ 'text_required' ] } " );
2021-03-02 21:03:02 +08:00
}
$attributes [ 'label' ] = $data [ 'label' ];
if ( empty ( $data [ 'type' ])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_type' ] } { $lang_functions [ 'text_required' ] } " );
2021-03-02 21:03:02 +08:00
}
if ( ! isset ( self :: $types [ $data [ 'type' ]])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_type' ] } { $lang_functions [ 'text_invalid' ] } " );
2021-03-02 21:03:02 +08:00
}
$attributes [ 'type' ] = $data [ 'type' ];
if ( ! isset ( $data [ 'required' ])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_required' ] } { $lang_functions [ 'text_required' ] } " );
2021-03-02 21:03:02 +08:00
}
if ( ! in_array ( $data [ 'required' ], [ " 0 " , " 1 " ], true )) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_name' ] } { $lang_functions [ 'text_invalid' ] } " );
2021-03-02 21:03:02 +08:00
}
$attributes [ 'required' ] = $data [ 'required' ];
2021-03-04 20:43:55 +08:00
if ( ! isset ( $data [ 'is_single_row' ])) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_is_single_row' ] } { $lang_functions [ 'text_required' ] } " );
2021-03-04 20:43:55 +08:00
}
if ( ! in_array ( $data [ 'is_single_row' ], [ " 0 " , " 1 " ], true )) {
2021-03-06 19:16:58 +08:00
throw new \InvalidArgumentException ( " { $lang_fields [ 'col_is_single_row' ] } { $lang_functions [ 'text_invalid' ] } " );
2021-03-04 20:43:55 +08:00
}
$attributes [ 'is_single_row' ] = $data [ 'is_single_row' ];
2021-03-02 21:03:02 +08:00
$attributes [ 'help' ] = $data [ 'help' ] ? ? '' ;
$attributes [ 'options' ] = trim ( $data [ 'options' ] ? ? '' );
2022-09-19 16:27:04 +08:00
$attributes [ 'display' ] = trim ( $data [ 'display' ] ? ? '' );
$attributes [ 'priority' ] = trim ( $data [ 'priority' ] ? ? '0' );
2021-03-02 21:03:02 +08:00
$now = date ( 'Y-m-d H:i:s' );
$attributes [ 'updated_at' ] = $now ;
$table = 'torrents_custom_fields' ;
if ( ! empty ( $data [ 'id' ])) {
2021-06-08 10:42:39 +08:00
$result = NexusDB :: update ( $table , $attributes , " id = " . sqlesc ( $data [ 'id' ]));
2021-03-02 21:03:02 +08:00
} else {
$attributes [ 'created_at' ] = $now ;
2021-06-08 10:42:39 +08:00
$result = NexusDB :: insert ( $table , $attributes );
2021-03-02 21:03:02 +08:00
}
return $result ;
}
2021-03-05 02:05:27 +08:00
protected function buildTable ( array $header , array $rows )
2021-03-02 21:03:02 +08:00
{
$table = '<table border="1" cellspacing="0" cellpadding="5" width="100%"><thead><tr>' ;
foreach ( $header as $key => $value ) {
$table .= sprintf ( '<td class="colhead">%s</td>' , $value );
}
$table .= '</tr></thead><tbody>' ;
foreach ( $rows as $row ) {
$table .= '<tr>' ;
foreach ( $header as $headerKey => $headerValue ) {
$table .= sprintf ( '<td class="colfollow">%s</td>' , $row [ $headerKey ] ? ? '' );
}
$table .= '</tr>' ;
}
2021-03-03 00:23:08 +08:00
$table .= '</tbody></table>' ;
2021-03-02 21:03:02 +08:00
return $table ;
}
2021-03-03 01:56:07 +08:00
public function buildFieldCheckbox ( $name , $current = [])
{
$sql = 'select * from torrents_custom_fields' ;
$res = sql_query ( $sql );
if ( ! is_array ( $current )) {
$current = explode ( ',' , $current );
}
2021-03-04 20:43:55 +08:00
$checkbox = '' ;
2021-03-03 01:56:07 +08:00
while ( $row = mysql_fetch_assoc ( $res )) {
2021-03-04 20:43:55 +08:00
$checkbox .= sprintf (
2021-03-03 01:56:07 +08:00
'<label style="margin-right: 4px;"><input type="checkbox" name="%s" value="%s"%s>%s</label>' ,
2021-03-18 02:01:12 +08:00
$name , $row [ 'id' ], in_array ( $row [ 'id' ], $current ) ? ' checked' : '' , " { $row [ 'name' ] } [ { $row [ 'label' ] } ] "
2021-03-03 01:56:07 +08:00
);
}
2021-03-04 20:43:55 +08:00
$checkbox .= '' ;
return $checkbox ;
2021-03-03 01:56:07 +08:00
}
2025-05-12 16:46:01 +07:00
public function renderOnUploadPage ( $torrentId , $searchBoxId )
2021-03-03 01:56:07 +08:00
{
2022-09-19 16:27:04 +08:00
$searchBox = NexusDB :: getOne ( 'searchbox' , " id = $searchBoxId " );
2021-03-03 01:56:07 +08:00
if ( empty ( $searchBox )) {
2022-09-19 16:27:04 +08:00
throw new \RuntimeException ( " Invalid search box: $searchBoxId " );
2021-03-03 01:56:07 +08:00
}
2022-09-19 16:27:04 +08:00
$customValues = $this -> listTorrentCustomField ( $torrentId , $searchBoxId );
$sql = sprintf ( 'select * from torrents_custom_fields where id in (%s) order by priority desc' , $searchBox [ 'custom_fields' ] ? : 0 );
2021-03-03 19:29:29 +08:00
$res = sql_query ( $sql );
$html = '' ;
while ( $row = mysql_fetch_assoc ( $res )) {
2022-09-19 16:27:04 +08:00
$name = " custom_fields[ $searchBoxId ][ { $row [ 'id' ] } ] " ;
2021-03-05 02:05:27 +08:00
$currentValue = $customValues [ $row [ 'id' ]][ 'custom_field_value' ] ? ? '' ;
2022-09-19 16:27:04 +08:00
$requireText = '' ;
if ( $row [ 'required' ]) {
$requireText = " <font color= \" red \" >*</font> " ;
}
$trLabel = $row [ 'label' ] . $requireText ;
$trRelation = " mode_ $searchBoxId " ;
2021-03-03 19:29:29 +08:00
if ( $row [ 'type' ] == self :: TYPE_TEXT ) {
2022-09-19 16:27:04 +08:00
$html .= tr ( $trLabel , sprintf ( '<input type="text" name="%s" value="%s" style="width: %s"/>' , $name , $currentValue , '99%' ), 1 , $trRelation );
2021-03-03 19:29:29 +08:00
} elseif ( $row [ 'type' ] == self :: TYPE_TEXTAREA ) {
2022-09-19 16:27:04 +08:00
$html .= tr ( $trLabel , sprintf ( '<textarea name="%s" rows="4" style="width: %s">%s</textarea>' , $name , '99%' , $currentValue ), 1 , $trRelation );
2021-03-03 19:29:29 +08:00
} elseif ( $row [ 'type' ] == self :: TYPE_RADIO || $row [ 'type' ] == self :: TYPE_CHECKBOX ) {
2021-03-04 02:27:39 +08:00
if ( $row [ 'type' ] == self :: TYPE_CHECKBOX ) {
$name .= '[]' ;
}
2021-03-03 19:29:29 +08:00
$part = " " ;
foreach ( preg_split ( '/[\r\n]+/' , trim ( $row [ 'options' ])) as $option ) {
if ( empty ( $option ) || ( $pos = strpos ( $option , '|' )) === false ) {
continue ;
}
2021-03-04 02:27:39 +08:00
$value = substr ( $option , 0 , $pos );
$label = substr ( $option , $pos + 1 );
2021-03-03 19:29:29 +08:00
$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>' ,
2021-03-04 02:27:39 +08:00
$row [ 'type' ], $name , $value , $checked , $label
2021-03-03 19:29:29 +08:00
);
}
2022-09-19 16:27:04 +08:00
$html .= tr ( $trLabel , $part , 1 , $trRelation );
2021-03-03 19:29:29 +08:00
} 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 ;
}
2021-03-04 02:27:39 +08:00
$value = substr ( $option , 0 , $pos );
$label = substr ( $option , $pos + 1 );
2021-03-03 19:29:29 +08:00
$selected = " " ;
if ( in_array ( $value , ( array ) $currentValue )) {
$selected = " selected " ;
}
$part .= sprintf (
'<option value="%s"%s>%s</option>' ,
2021-03-04 02:27:39 +08:00
$value , $selected , $label
2021-03-03 19:29:29 +08:00
);
}
$part .= '</select>' ;
2022-09-19 16:27:04 +08:00
$html .= tr ( $trLabel , $part , 1 , $trRelation );
2021-03-03 19:29:29 +08:00
} 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>' ;
2022-08-22 01:17:04 +08:00
$y .= sprintf ( '<input id="%s" type="text" name="%s" value="%s" style="width: %s;margin: 10px 0">' , $inputId , $name , $currentValue , '99%' );
2021-03-03 19:29:29 +08:00
$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 )
2021-03-03 01:56:07 +08:00
}
2021-03-03 19:29:29 +08:00
</ script >
JS ;
2022-09-19 16:27:04 +08:00
$html .= tr ( $trLabel , $y , 1 , $trRelation , true );
2021-03-03 19:29:29 +08:00
}
}
return $html ;
}
2022-09-19 16:27:04 +08:00
public function listTorrentCustomField ( $torrentId , $searchBoxId )
2021-03-04 02:27:39 +08:00
{
2021-03-06 19:16:58 +08:00
//suppose torrentId is array
$isArray = true ;
$torrentIdArr = $torrentId ;
if ( ! is_array ( $torrentId )) {
$isArray = false ;
$torrentIdArr = [ $torrentId ];
}
$torrentIdStr = implode ( ',' , $torrentIdArr );
2022-09-19 16:27:04 +08:00
$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 ) order by f.priority desc " );
2021-03-04 20:43:55 +08:00
$values = [];
2021-03-04 02:27:39 +08:00
$result = [];
while ( $row = mysql_fetch_assoc ( $res )) {
2021-03-04 20:43:55 +08:00
$typeInfo = self :: $types [ $row [ 'type' ]];
if ( $typeInfo [ 'has_option' ]) {
2021-03-06 19:16:58 +08:00
$options = preg_split ( '/[\r\n]+/' , trim ( $row [ 'options' ]));
2021-03-04 20:43:55 +08:00
$optionsArr = [];
foreach ( $options as $option ) {
$pos = strpos ( $option , '|' );
$value = substr ( $option , 0 , $pos );
$label = substr ( $option , $pos + 1 );
$optionsArr [ $value ] = $label ;
}
$row [ 'options' ] = $optionsArr ;
}
2021-03-06 19:16:58 +08:00
$result [ $row [ 'torrent_id' ]][ $row [ 'id' ]] = $row ;
2021-03-04 20:43:55 +08:00
if ( $typeInfo [ 'is_value_multiple' ]) {
2022-11-16 17:37:42 +08:00
$values [ $row [ 'torrent_id' ]][ $row [ 'id' ]] = json_decode ( $row [ 'custom_field_value' ], true );
2021-03-04 02:27:39 +08:00
} else {
2021-03-06 19:16:58 +08:00
$values [ $row [ 'torrent_id' ]][ $row [ 'id' ]] = $row [ 'custom_field_value' ];
2021-03-04 20:43:55 +08:00
}
}
2021-03-06 19:16:58 +08:00
foreach ( $result as $tid => & $fields ) {
foreach ( $fields as & $field ) {
$field [ 'custom_field_value' ] = $values [ $tid ][ $field [ 'id' ]];
}
2021-03-04 20:43:55 +08:00
}
2021-03-18 20:32:35 +08:00
return $isArray ? $result : ( $result [ $torrentId ] ? ? []);
2021-03-04 20:43:55 +08:00
}
2022-09-19 16:27:04 +08:00
public function renderOnTorrentDetailsPage ( $torrentId , $searchBoxId )
2021-03-04 20:43:55 +08:00
{
2022-09-19 16:27:04 +08:00
$displayName = get_searchbox_value ( $searchBoxId , 'custom_fields_display_name' );
$customFields = $this -> listTorrentCustomField ( $torrentId , $searchBoxId );
$mixedRowContent = get_searchbox_value ( $searchBoxId , 'custom_fields_display' );
2021-03-04 20:43:55 +08:00
$rowByRowHtml = '' ;
2022-08-22 01:47:05 +08:00
$shouldRenderMixRow = false ;
2021-03-04 20:43:55 +08:00
foreach ( $customFields as $field ) {
2022-08-22 01:47:05 +08:00
if ( empty ( $field [ 'custom_field_value' ])) {
//No value, remove special tags
$mixedRowContent = str_replace ( " <% { $field [ 'name' ] } .label%> " , '' , $mixedRowContent );
$mixedRowContent = str_replace ( " <% { $field [ 'name' ] } .value%> " , '' , $mixedRowContent );
continue ;
}
$shouldRenderMixRow = true ;
2022-08-22 01:17:04 +08:00
$contentNotFormatted = $this -> formatCustomFieldValue ( $field , false );
2021-03-04 20:43:55 +08:00
$mixedRowContent = str_replace ( " <% { $field [ 'name' ] } .label%> " , $field [ 'label' ], $mixedRowContent );
2022-08-22 01:17:04 +08:00
$mixedRowContent = str_replace ( " <% { $field [ 'name' ] } .value%> " , $contentNotFormatted , $mixedRowContent );
2021-03-04 20:43:55 +08:00
if ( $field [ 'is_single_row' ]) {
2022-09-19 16:27:04 +08:00
if ( ! empty ( $field [ 'display' ])) {
$customFieldDisplay = $field [ 'display' ];
$customFieldDisplay = str_replace ( " <% { $field [ 'name' ] } .label%> " , $field [ 'label' ], $customFieldDisplay );
$customFieldDisplay = str_replace ( " <% { $field [ 'name' ] } .value%> " , $contentNotFormatted , $customFieldDisplay );
2025-12-28 22:36:52 +08:00
$rowByRowHtml .= tr ( $field [ 'label' ], format_comment ( $customFieldDisplay ), 1 );
2022-09-19 16:27:04 +08:00
} else {
$contentFormatted = $this -> formatCustomFieldValue ( $field , true );
$rowByRowHtml .= tr ( $field [ 'label' ], $contentFormatted , 1 );
}
2021-03-04 20:43:55 +08:00
}
}
2022-09-15 01:13:14 +08:00
2021-03-04 20:43:55 +08:00
$result = $rowByRowHtml ;
2022-09-15 01:13:14 +08:00
if ( $shouldRenderMixRow && $mixedRowContent ) {
2022-08-22 00:07:36 +08:00
$result .= tr ( $displayName , format_comment ( $mixedRowContent ), 1 );
2021-03-04 02:27:39 +08:00
}
return $result ;
}
2021-03-04 20:43:55 +08:00
2021-03-05 02:05:27 +08:00
2022-08-22 01:17:04 +08:00
protected function formatCustomFieldValue ( array $customFieldWithValue , $doFormatComment = false ) : string
2021-03-05 02:05:27 +08:00
{
$result = '' ;
$fieldValue = $customFieldWithValue [ 'custom_field_value' ];
switch ( $customFieldWithValue [ 'type' ]) {
case self :: TYPE_TEXT :
case self :: TYPE_TEXTAREA :
2025-12-28 22:36:52 +08:00
$result .= $doFormatComment ? format_comment ( $fieldValue ) : $fieldValue ;
2021-03-05 02:05:27 +08:00
break ;
case self :: TYPE_IMAGE :
if ( substr ( $fieldValue , 0 , 4 ) == 'http' ) {
2022-08-22 01:17:04 +08:00
$result .= $doFormatComment ? formatImg ( $fieldValue , true , 700 , 0 , " attach { $customFieldWithValue [ 'id' ] } " ) : $fieldValue ;
2021-03-05 02:05:27 +08:00
} else {
2025-12-28 22:36:52 +08:00
$result .= $doFormatComment ? format_comment ( $fieldValue ) : $fieldValue ;
2021-03-05 02:05:27 +08:00
}
break ;
case self :: TYPE_RADIO :
case self :: TYPE_CHECKBOX :
case self :: TYPE_SELECT ;
$fieldContent = [];
foreach (( array ) $fieldValue as $item ) {
$fieldContent [] = $customFieldWithValue [ 'options' ][ $item ] ? ? '' ;
}
$result .= implode ( ' ' , $fieldContent );
break ;
default :
break ;
}
return $result ;
}
2021-03-06 19:16:58 +08:00
public function prepareTorrents ( array $torrentIdArr )
2021-03-05 02:05:27 +08:00
{
2021-03-06 19:16:58 +08:00
$customFieldValues = $this -> listTorrentCustomField ( $torrentIdArr );
$result = [];
foreach ( $customFieldValues as $tid => & $customFields ) {
2021-03-05 02:05:27 +08:00
foreach ( $customFields as & $field ) {
$field [ 'custom_field_value_formatted' ] = $this -> formatCustomFieldValue ( $field );
2021-03-06 19:16:58 +08:00
$result [ $tid ][ $field [ 'name' ]] = $field ;
2021-03-05 02:05:27 +08:00
}
}
2021-03-06 19:16:58 +08:00
$this -> preparedTorrentCustomFieldValues = $result ;
}
2021-03-05 02:05:27 +08:00
2021-03-06 19:16:58 +08:00
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 ] ? ? '' ;
2021-03-05 02:05:27 +08:00
}
2022-09-19 16:27:04 +08:00
public function saveFieldValues ( $searchBoxId , $torrentId , array $data )
{
$searchBox = SearchBox :: query () -> findOrFail ( $searchBoxId );
$enabledFields = TorrentCustomField :: query () -> find ( $searchBox -> custom_fields );
$insert = [];
$now = now ();
foreach ( $enabledFields as $field ) {
if ( empty ( $data [ $field -> id ])) {
if ( $field -> required ) {
// throw new \InvalidArgumentException(nexus_trans("nexus.require_argument", ['argument' => $field->label]));
do_log ( " Field: { $field -> label } required, but empty " );
}
continue ;
}
$insert [] = [
'torrent_id' => $torrentId ,
'custom_field_id' => $field -> id ,
2022-11-16 17:37:42 +08:00
'custom_field_value' => is_array ( $data [ $field -> id ]) ? json_encode ( $data [ $field -> id ]) : $data [ $field -> id ],
2022-09-19 16:27:04 +08:00
'created_at' => $now ,
'updated_at' => $now ,
];
}
TorrentCustomFieldValue :: query () -> where ( 'torrent_id' , $torrentId ) -> delete ();
TorrentCustomFieldValue :: query () -> insert ( $insert );
}
2021-03-06 19:16:58 +08:00
2021-04-02 19:48:41 +08:00
}