# Now Playing with Sort & Folders module for TiVoWeb.
#
# Based very heavily on other parts of TiVoWeb!
#
# LJ @ http://www.tivocommunity.com/
#
# WARNING: This code comes with absolutely no warranty. Use it at your own risk.
# If it turns your TiVo into a pumpkin don't say you weren't warned. I'd be interested to know if it does though... ;)
#
# 27Dec02 15:23 v1.0
# 27Dec02 18:32 v1.1 Force TiVo Suggestions to 'expire' before user selected programs.
# Quicker process for generating sort data.
# Add 'Genre' as a sort option.
# 04Jan03 18:40 v1.2 Add delete checkboxes back in.
# 18Jan03 10:38 v2.0 Add folders. :)
# 18Jan03 19:21 v2.1 Fix incorrect call ;)
# 19Jan03 09:50 v2.2 Add a new sort view: By Folder. Put 'Mins' column back into 'Assign' view.
# 19Jan03 21:59 v2.3 Adds auto-assignment by Series and folder sorting.
# 20Jan03 22:08 v2.4 Rearrange menu lines. Add sorting to assignment screen.
# 24Jan03 20:36 v2.5 Add a new 'Unassigned' option. Default to 'by folder' sort next if sort line isn't displayed.
# Renamed to folders.itcl.
# 09Feb03 08:09 v2.6 Fix error in auto-assigns if series no longer exists.
# Don't allow null seriesids to be added to auto-assigns.
# 09Jul03 08:52 v2.7 Skip programmes with no series when auto-assigning.
# 08Nov03 15:50 v2.8 Take into account any start and end padding in a programme's duration.
proc action_folders {chan path env} {
global db
global genrenums
global npffile
global fldrdlabels
global fldrvals
set npffile "/var/hack/etc/npf.txt"
set npfafile "/var/hack/etc/npfauto.txt"
set fldrdlabels ""
set fldrvals ""
set shows ""
set action ""
set sub ""
if {$::version3} {
RetryTransaction {
set state [db $db open "/State/MyWorld"]
set sort [dbobj $state get NowShowingSortOrder]
}
set nowshowingdir "/Recording/NowShowingByClassic"
} else {
set nowshowingdir "/Recording/NowShowing"
set sort 6
}
if { [file exists $npffile] } {
set npfhnd [open $npffile r]
gets $npfhnd fldrinfo
close $npfhnd
} else {
set fldrinfo [list]
}
if { [file exists $npfafile] } {
set npfhnd [open $npfafile r]
gets $npfhnd fldrauto
close $npfhnd
} else {
set fldrauto [list]
}
eval $env
if { $action == "folders" && $sub == "dodelete" } {
set dellist [lsort -dictionary [info vars "fldr_*"]]
if { [llength $dellist] > 0 } {
foreach fldr $dellist {
lappend fldn [lindex [split $fldr "_"] 1]
}
set delstring ""
for {set i [expr [llength $fldn] - 1]} {$i > -1} {incr i -1} {
set delstring "
Folder '[lindex [lindex $fldrinfo [lindex $fldn $i]] 0]' Deleted$delstring"
set fldrinfo [lreplace $fldrinfo [lindex $fldn $i] [lindex $fldn $i]]
}
set npfhnd [open $npffile w]
puts -nonewline $npfhnd $fldrinfo
close $npfhnd
for {set i [expr [llength $fldn] - 1]} {$i >= 0} {incr i -1} {
for {set j [expr [llength $fldrauto] - 1]} {$j >= 0 } {incr j -1} {
if { [lindex [lindex $fldrauto $j] 1] == [expr [lindex $fldn $i] + 1]} {
set fldrauto [lreplace $fldrauto $j $j]
} elseif { [lindex [lindex $fldrauto $j] 1] > [expr [lindex $fldn $i] + 1] } {
set fldrauto [lreplace $fldrauto $j $j "[lindex [lindex $fldrauto $j] 0] [expr [lindex [lindex $fldrauto $j] 1] - 1]"]
}
}
}
set npfhnd [open $npfafile w]
puts -nonewline $npfhnd $fldrauto
close $npfhnd
} else {
set delstring "
No Folders Selected For Deletion"
}
}
if { $action == "folders" && $sub == "docreate" } {
if { [string first "{" $name] > -1 || [string first "}" $name] > -1 || [string first "\"" $name] > -1} {
set addstring "
Sorry, '{', '}' or '\"' are not allowed in folder names."
} elseif { [string length [string trim $name]] == 0 } {
set addstring "
No Folder Name Entered!"
} else {
set fexists 0
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
if { [lindex [lindex $fldrinfo $i] 0] == $name } {
set fexists 1
break
}
}
if { $fexists == 0 } {
lappend fldrinfo "{$name}"
set npfhnd [open $npffile w]
puts -nonewline $npfhnd $fldrinfo
close $npfhnd
set addstring "
Folder '$name' Added"
} else {
set addstring "
Folder '$name' Already Exists"
}
}
}
if { $action == "folders" && $sub == "dosort" } {
set fldrorg $fldrinfo
set fldrinfo [lsort -dictionary -index 0 $fldrinfo]
set npfhnd [open $npffile w]
puts -nonewline $npfhnd $fldrinfo
close $npfhnd
if { [llength $fldrauto] > 0 } {
set fldrtrans "0"
for {set i 0} {$i < [llength $fldrorg]} {incr i 1} {
for {set j 0} {$j < [llength $fldrorg]} {incr j 1} {
if { [lindex [lindex $fldrinfo $j] 0] == [lindex [lindex $fldrorg $i] 0] } {
break
}
}
lappend fldrtrans [expr $j + 1]
}
for {set i 0} {$i < [llength $fldrauto]} {incr i 1} {
set fldrauto [lreplace $fldrauto $i $i "[lindex [lindex $fldrauto $i] 0] [lindex $fldrtrans [lindex [lindex $fldrauto $i] 1]]"]
}
set npfhnd [open $npfafile w]
puts -nonewline $npfhnd $fldrauto
close $npfhnd
}
}
if { $sub != "create" && $sub != "docreate" && $sub != "dodelete" && $sub != "doauto" && $sub != "auto" && $sub != "doassign" && $sub != "dosort" } {
if { [llength $fldrauto] > 0 } {
ForeachMfsFileTrans prgfsid name type $nowshowingdir "" 15 {
set found 0
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
if {[lsearch [lindex $fldrinfo $i] $prgfsid ] > 0 } {
set found 1
break
}
}
if { $found == 0 } {
set rec [db $db openid $prgfsid]
set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set series [dbobj $program get Series]
if { $series != "" } {
set serfsid [dbobj $series fsid]
for {set i 0} {$i < [llength $fldrauto]} {incr i 1} {
if { $serfsid == [lindex [lindex $fldrauto $i] 0] } {
break
}
}
if { $i < [llength $fldrauto] } {
set lelem [expr [lindex [lindex $fldrauto $i] 1] - 1]
set lrepl "[lindex $fldrinfo $lelem] \{$prgfsid\}"
set fldrinfo [lreplace $fldrinfo $lelem $lelem $lrepl]
}
}
}
}
set npfhnd [open $npffile w]
puts -nonewline $npfhnd $fldrinfo
close $npfhnd
}
}
if { $action == "folders" && $sub == "doauto" } {
set dellist [lsort [info vars "fldr_*"]]
if { [llength $dellist] > 0 } {
foreach fldr $dellist {
lappend fldn [lindex [split $fldr "_"] 1]
}
set delstring "
Auto Assignments Updated"
for {set i [expr [llength $fldn] - 1]} {$i > -1} {incr i -1} {
set fldrauto [lreplace $fldrauto [lindex $fldn $i] [lindex $fldn $i]]
}
set npfhnd [open $npfafile w]
puts -nonewline $npfhnd $fldrauto
close $npfhnd
} else {
set delstring "
No Auto Assignments Selected For Deletion"
}
}
set fldridx 1
lappend fldrvals 0
if {$::lang == "en"} {
lappend fldrlabels "-- All Programmes --"
} else {
lappend fldrlabels "-- All Programs --"
}
lappend fldrdlabels "-- Unassigned --"
foreach fldr $fldrinfo {
lappend fldrvals $fldridx
incr fldridx 1
lappend fldrlabels [lindex $fldr 0]
lappend fldrdlabels [lindex $fldr 0]
}
set fldridx 0
set sort 6
eval $env
set title "Now Playing with Sort and Folders"
if { $fldridx > 0 } {
set title "$title - [lindex $fldrlabels $fldridx]"
}
puts $chan [html_start $title]
puts $chan [html_form_start "GET" "/folders" "name=\"fldrform\""]
puts $chan [html_table_start "" "View Folder" "COLSPAN=3"]
if { $action == "folders" } {
set nextsort 6
} elseif { $sort > 6 } {
set nextsort 0
set sort 0
} else {
set nextsort $sort
}
puts $chan [tr "" [td "View Folder:"] [td [html_form_select "fldridx\" WIDTH=\"20" $fldrvals $fldrlabels $fldridx]] [td [html_form_hidden "sort" $nextsort][html_form_input "submit" "submit" "Go"]]]
if { $fldridx > 0 && $sort >= 6 } {
set sort 0
}
puts $chan [html_table_end]
puts $chan [html_form_end]
puts $chan "Manage Folders: "
puts $chan [html_link "/folders?action=folders&sub=create" "Create"]
puts $chan [html_link "/folders?action=folders&sub=delete" "Delete"]
puts $chan [html_link "/folders?action=folders&sub=sort" "Sort"]
puts $chan [html_link "/folders?action=folders&sub=auto" "Auto Assigns"]
puts $chan [html_link "/folders?action=folders&sub=assign&sort=0" "Assign"]
puts $chan [html_link "/folders?action=folders&sub=assign&sort=7" "Unassigned"]
if { $env == "" || $action != "folders" } {
puts $chan "
Sort by: "
puts $chan "[html_link "/folders?fldridx=$fldridx&sort=0" [expr {$sort == 0 ? "Classic" : "Classic"}]] "
puts $chan "[html_link "/folders?fldridx=$fldridx&sort=1" [expr {$sort == 1 ? "Expiry Date" : "Expiry Date"}]] "
puts $chan "[html_link "/folders?fldridx=$fldridx&sort=2" [expr {$sort == 2 ? "Title" : "Title"}]] "
puts $chan "[html_link "/folders?fldridx=$fldridx&sort=3" [expr {$sort == 3 ? "Title + Episode Title" : "Title + Episode Title"}]] "
puts $chan "[html_link "/folders?fldridx=$fldridx&sort=4" [expr {$sort == 4 ? "Duration" : "Duration"}]] "
puts $chan "[html_link "/folders?fldridx=$fldridx&sort=5" [expr {$sort == 5 ? "Genre" : "Genre"}]] "
puts $chan "[html_link "/folders?fldridx=0&sort=6" [expr {$sort == 6 ? "Folder" : "Folder"}]]
"
} elseif { $sub == "assign" && $sort != 7 } {
puts $chan "Sort by: "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=0" [expr {$sort == 0 ? "Classic" : "Classic"}]] "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=1" [expr {$sort == 1 ? "Expiry Date" : "Expiry Date"}]] "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=2" [expr {$sort == 2 ? "Title" : "Title"}]] "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=3" [expr {$sort == 3 ? "Title + Episode Title" : "Title + Episode Title"}]] "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=4" [expr {$sort == 4 ? "Duration" : "Duration"}]] "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=5" [expr {$sort == 5 ? "Genre" : "Genre"}]] "
puts $chan "[html_link "/folders?action=folders&sub=assign&sort=6" [expr {$sort == 6 ? "Folder" : "Folder"}]]
"
} else {
puts $chan "
"
}
puts $chan "
"
if { $action == "folders" } {
switch -exact -- $sub {
delete {
if { [llength $fldrinfo] > 0 } {
puts $chan "
"
puts $chan [html_form_start "POST" "/folders?action=folders&sub=dodelete&" "name=\"form\""]
set delete_th [th ""]
puts $chan [html_table_start "" "" ""]
if { $::lang == "en" } {
set prgttl "Programmes"
} else {
set prgttl "Programs"
}
puts $chan [tr "ALIGN=CENTER" [th "align=left" "Folder"] [th $prgttl] [th "Auto Assigns"] $delete_th]
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
set fldrai [lappend fldrai 0]
}
for {set i 0} {$i < [llength $fldrauto]} {incr i 1} {
set lelem [expr [lindex [lindex $fldrauto $i] 1] - 1]
set fldrai [lreplace $fldrai $lelem $lelem [expr [lindex $fldrai $lelem] + 1]]
}
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
set delete_td [td "ALIGN=CENTER" [html_form_checkbox "fldr_$i" ""]]
puts $chan [tr "" [td [lindex [lindex $fldrinfo $i] 0]] [td "align=center" [expr [llength [lindex $fldrinfo $i]] - 1]] [td "align=center" [lindex $fldrai $i]] $delete_td]
}
puts -nonewline $chan [html_table_end]
puts $chan [html_form_input "submit" "submit" "Delete Folders"]
puts $chan [html_form_end]
} else {
puts $chan "No Folders Exist!"
}
}
create {puts $chan [html_form_start "POST" "/folders?action=folders&sub=docreate&" "name=\"form\""]
puts $chan [html_table_start "" "" ""]
set name ""
puts $chan [tr "ALIGN=CENTER" [th "COLSPAN=2" "Create A New Folder"]]
puts $chan [tr "" [td "Folder Name:"] [td [html_form_text 1 35 "name" $name]]]
puts -nonewline $chan [html_table_end]
puts $chan [html_form_input "submit" "submit" "Create Folder"]
puts $chan "
"
puts $chan [html_form_end]
}
docreate {
puts $chan "$addstring"
}
dodelete {
puts $chan "$delstring"
}
doauto {
puts $chan "$delstring"
}
assign {
if { [llength $fldrinfo] == 0 } {
puts $chan "
No Folders Exist!"
} else {
puts $chan [html_form_start "POST" "/folders?action=folders&sub=doassign&" "name=\"form\""]
puts $chan [html_table_start "" "" ""]
if {$::lang == "en"} {
puts $chan [tr "ALIGN=CENTER" [th ""] [th "Programme"] [th "Episode"] [th "Mins"] [th "COLSPAN=2" "Date"] [th "Folder"] [th "Auto"]]
} else {
puts $chan [tr "ALIGN=CENTER" [th ""] [th "Program"] [th "Episode"] [th "Mins"] [th "COLSPAN=2" "Date"] [th "Folder"] [th "Auto"]]
}
set fsid2fldr [list]
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
for {set j 1} {$j < [llength [lindex $fldrinfo $i]]} {incr j 1} {
lappend fsid2fldr "[lindex [lindex $fldrinfo $i] $j] [expr $i + 1]"
}
}
set nstype 0
set rcount 0
ForeachMfsFileTrans fsid name type $nowshowingdir "" 15 {
set rec [db $db openid $fsid]
# 7: Unassigned only 6: Folder 5: Genre 4: Duration 3: Title+EpTitle 2: Title, 1: Expiration, 0: Classic
switch -exact -- $sort {
7 { set prgfldridx 0
for {set i 0} {$i < [llength $fsid2fldr]} {incr i 1} {
if { $fsid == [lindex [lindex $fsid2fldr $i] 0] } {
set prgfldridx [lindex [lindex $fsid2fldr $i] 1]
break
}
}
if { $prgfldridx == 0 } {
lappend shows [list $nstype $fsid $prgfldridx]
}
}
6 { set prgfldridx 0
for {set i 0} {$i < [llength $fsid2fldr]} {incr i 1} {
if { $fsid == [lindex [lindex $fsid2fldr $i] 0] } {
set prgfldridx [lindex [lindex $fsid2fldr $i] 1]
break
}
}
lappend shows [list $nstype $fsid $prgfldridx]
}
5 { set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
set sortkey ""
set genr [dbobj $program get Genre]
if {$genr != ""} {
for {set i 0} {$i < [llength $genrenums]} {incr i 1} {
if {[lsearch -exact $genr [lindex $genrenums $i]] > -1} {
append sortkey "y"
} else {
append sortkey "n"
}
}
} else {
set sortkey "z"
}
set sortkey "$sortkey $title"
lappend shows [list $nstype $fsid $sortkey]
}
4 { set showing [dbobj $rec get Showing]
set startpad [defaultval 0 [dbobj $rec get StartPadding]]
set endpad [defaultval 0 [dbobj $rec get EndPadding]]
set sortkey [expr [dbobj $showing get Duration] + $startpad + $endpad]
lappend shows [list $nstype $fsid $sortkey]
}
3 { set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
set sortkey "$title [strim [dbobj $program get EpisodeTitle]]"
lappend shows [list $nstype $fsid $sortkey]
}
2 { set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set sortkey [strim [dbobj $program get Title]]
lappend shows [list $nstype $fsid $sortkey]
}
1 { set showing [dbobj $rec get Showing]
set expdate [dbobj $rec get ExpirationDate]
set expsecs [expr $expdate * 86400 + [dbobj $rec get ExpirationTime]]
set sortkey [expr {[string index $name 0] == "A" ? "B[expr abs($expsecs)]" : "A[expr abs($expsecs)]" }]
lappend shows [list $nstype $fsid $sortkey]
}
0 {
set rec [db $db openid $fsid]
set prgfldridx 0
for {set i 0} {$i < [llength $fsid2fldr]} {incr i 1} {
if { $fsid == [lindex [lindex $fsid2fldr $i] 0] } {
set prgfldridx [lindex [lindex $fsid2fldr $i] 1]
break
}
}
print_nowplayingfrow $chan $rec 0 "" $prgfldridx
}
}
}
if { $sort > 0 } {
# 6: Folder 5: Genre 4: Duration 3: Title+EpTitle 2: Title, 1: Expiration, 0: Classic
switch -exact -- $sort {
6 { set shows [lsort -integer -index 2 $shows] }
5 { set shows [lsort -dictionary -index 2 $shows] }
4 { set shows [lsort -integer -index 2 $shows] }
3 { set shows [lsort -ascii -index 2 $shows] }
2 { set shows [lsort -ascii -index 2 $shows] }
1 { set shows [lsort -dictionary -index 2 $shows] }
}
set rcount 0
set lastfldr -1
foreach show $shows {
if {$sort == 6 && $lastfldr != [lindex $show 2]} {
puts $chan [tr "ALIGN=CENTER" [th "COLSPAN=8" "[lindex $fldrdlabels [lindex $show 2]]"]]
set lastfldr [lindex $show 2]
}
RetryTransaction {
set rec [db $db openid [lindex $show 1]]
set prgfldridx 0
for {set i 0} {$i < [llength $fsid2fldr]} {incr i 1} {
if { [lindex $show 1] == [lindex [lindex $fsid2fldr $i] 0] } {
set prgfldridx [lindex [lindex $fsid2fldr $i] 1]
break
}
}
print_nowplayingfrow $chan $rec 0 "" $prgfldridx
}
}
}
puts -nonewline $chan [html_table_end]
if { [llength $shows] > 0 || $sort == 0 } {
puts $chan [html_form_input "submit" "submit" "Assign Folders"]
} else {
puts $chan "None Found"
}
puts $chan [html_form_end]
}
}
doassign {
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
if { [llength [lindex $fldrinfo $i]] > 1 } {
set fldrinfo [lreplace $fldrinfo $i $i "{[lindex [lindex $fldrinfo $i] 0]}"]
}
}
set envvals [split $env ";"]
foreach prog $envvals {
if { [string range [lindex $prog 1] 0 7] == "fldridx_" } {
set fldrid [lindex $prog 2]
if { $fldrid > 0 } {
set prgfsid [lindex [split [lindex $prog 1] "_"] 1]
set lelem [expr $fldrid - 1]
set lrepl "[lindex $fldrinfo $lelem] \{$prgfsid\}"
set fldrinfo [lreplace $fldrinfo $lelem $lelem $lrepl]
}
continue
}
if { [string range [lindex $prog 1] 0 8] == "fldrauto_" } {
if { $fldrid > 0 } {
set serfsid [lindex [split [lindex $prog 1] "_"] 1]
if { $serfsid != "" } {
for {set i 0} {$i < [llength $fldrauto]} {incr i 1} {
if { $serfsid == [lindex [lindex $fldrauto $i] 0] } {
break
}
}
if { $i == [llength $fldrauto] } {
set fldrauto [lappend fldrauto "$serfsid $fldrid"]
} else {
set fldrauto [lreplace $fldrauto $i $i "$serfsid $fldrid"]
}
}
}
}
}
set npfhnd [open $npffile w]
puts -nonewline $npfhnd $fldrinfo
close $npfhnd
set npfhnd [open $npfafile w]
puts -nonewline $npfhnd $fldrauto
close $npfhnd
puts $chan "
Folder Assignments Updated"
}
auto {
if { [llength $fldrauto] > 0 } {
puts $chan "
"
puts $chan [html_form_start "POST" "/folders?action=folders&sub=doauto&" "name=\"form\""]
set delete_th [th ""]
puts $chan [html_table_start "" "" ""]
puts $chan [tr "ALIGN=CENTER" [th [html_link "/folders?action=folders&sub=auto&sub2=series" "Series"]] [th [html_link "/folders?action=folders&sub=auto&sub2=folder" "Folder"]] $delete_th]
for {set i 0} {$i < [llength $fldrauto]} {incr i 1} {
set title "-- Series No Longer Exists -- \[[lindex [lindex $fldrauto $i] 0]\]"
set delete_td [td "ALIGN=CENTER" [html_form_checkbox "fldr_$i" ""]]
RetryTransaction {
if { ![catch {set series [db $db openid [lindex [lindex $fldrauto $i] 0]] } errid ] } {
set title "[strim [dbobj $series get Title]] \[[lindex [lindex $fldrauto $i] 0]\]"
}
}
lappend aalist [list $title [lindex [lindex $fldrdlabels [lindex [lindex $fldrauto $i] 1]] 0] $delete_td]
}
if { [info exists "sub2"] } {
switch -exact $sub2 {
series { set aalist [lsort -ascii -index 0 $aalist] }
folder { set aalist [lsort -ascii -index 1 $aalist] }
}
}
for {set i 0} {$i < [llength $aalist]} {incr i 1} {
puts $chan [tr "" [td [lindex [lindex $aalist $i] 0]] [td [lindex [lindex $aalist $i] 1]] [lindex [lindex $aalist $i] 2]]
}
puts -nonewline $chan [html_table_end]
puts $chan [html_form_input "submit" "submit" "Delete Auto Assignments"]
puts $chan [html_form_end]
} else {
puts $chan "
No Auto Assignments Exist!"
}
}
sort {
if { [llength $fldrinfo] > 0 } {
puts $chan [html_form_start "POST" "/folders?action=folders&sub=dosort&" "name=\"form\""]
puts $chan [html_form_input "submit" "submit" "Click to Sort Folders"]
puts $chan [html_form_end]
} else {
puts $chan "
No Folders to Sort!"
}
}
dosort {
puts $chan "
Folders Sorted"
}
# default { puts $chan "sub *$sub*"
# puts $chan "env *$env*
"
# }
}
} else {
if {$::multi_delete} {
set nstype 6
} else {
set nstype 0
}
set delete_th ""
if {$nstype == 6} {
puts $chan "
"
puts $chan [html_form_start "POST" "/confirmdelete/6/" "name=\"form\""]
set delete_th [th ""]
}
puts $chan [html_table_start "" "" ""]
if { $fldridx > 0 } {
puts $chan [tr "ALIGN=CENTER" [th "COLSPAN=7" "[lindex $fldrlabels $fldridx]"]]
}
if {$::lang == "en"} {
puts $chan [tr "ALIGN=CENTER" [th ""] [th "Programme"] [th "Episode"] [th "Mins"] [th "COLSPAN=2" "Date"] $delete_th]
} else {
puts $chan [tr "ALIGN=CENTER" [th ""] [th "Program"] [th "Episode"] [th "Mins"] [th "COLSPAN=2" "Date"] $delete_th]
}
if { $sort == 6 } {
set fsid2fldr [list]
for {set i 0} {$i < [llength $fldrinfo]} {incr i 1} {
for {set j 1} {$j < [llength [lindex $fldrinfo $i]]} {incr j 1} {
lappend fsid2fldr "[lindex [lindex $fldrinfo $i] $j] [expr $i + 1]"
}
}
}
set rcount 0
ForeachMfsFileTrans fsid name type $nowshowingdir "" 15 {
if { $fldridx == 0 || [lsearch [lindex $fldrinfo [expr $fldridx - 1]] $fsid ] > 0 } {
set rec [db $db openid $fsid]
# 6: Folder 5: Genre 4: Duration 3: Title+EpTitle 2: Title, 1: Expiration, 0: Classic
switch -exact -- $sort {
6 {
set prgfldridx 0
for {set i 0} {$i < [llength $fsid2fldr]} {incr i 1} {
if { $fsid == [lindex [lindex $fsid2fldr $i] 0] } {
set prgfldridx [lindex [lindex $fsid2fldr $i] 1]
break
}
}
lappend shows [list $nstype $fsid $prgfldridx]
}
5 {
set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
set sortkey ""
set genr [dbobj $program get Genre]
if {$genr != ""} {
for {set i 0} {$i < [llength $genrenums]} {incr i 1} {
if {[lsearch -exact $genr [lindex $genrenums $i]] > -1} {
append sortkey "y"
} else {
append sortkey "n"
}
}
} else {
set sortkey "z"
}
set sortkey "$sortkey $title"
lappend shows [list $nstype $fsid $sortkey]
}
4 {
set showing [dbobj $rec get Showing]
set startpad [defaultval 0 [dbobj $rec get StartPadding]]
set endpad [defaultval 0 [dbobj $rec get EndPadding]]
set sortkey [expr [dbobj $showing get Duration] + $startpad + $endpad]
lappend shows [list $nstype $fsid $sortkey]
}
3 {
set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
set sortkey "$title [strim [dbobj $program get EpisodeTitle]]"
lappend shows [list $nstype $fsid $sortkey]
}
2 {
set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set sortkey [strim [dbobj $program get Title]]
lappend shows [list $nstype $fsid $sortkey]
}
1 {
set showing [dbobj $rec get Showing]
set expdate [dbobj $rec get ExpirationDate]
set expsecs [expr $expdate * 86400 + [dbobj $rec get ExpirationTime]]
set sortkey [expr {[string index $name 0] == "A" ? "B[expr abs($expsecs)]" : "A[expr abs($expsecs)]" }]
lappend shows [list $nstype $fsid $sortkey]
}
0 {
if {$nstype == 6} {
print_nowplayingfrow $chan $rec $nstype $rcount
incr rcount
} else {
print_nowplayingfrow $chan $rec $nstype
}
}
}
}
}
if { $sort > 0 } {
# 6: Folder 5: Genre 4: Duration 3: Title+EpTitle 2: Title, 1: Expiration, 0: Classic
switch -exact -- $sort {
6 { set shows [lsort -integer -index 2 $shows] }
5 { set shows [lsort -dictionary -index 2 $shows] }
4 { set shows [lsort -integer -index 2 $shows] }
3 { set shows [lsort -ascii -index 2 $shows] }
2 { set shows [lsort -ascii -index 2 $shows] }
1 { set shows [lsort -dictionary -index 2 $shows] }
}
set rcount 0
set lastfldr -1
foreach show $shows {
if {$sort == 6 && $lastfldr != [lindex $show 2]} {
puts $chan [tr "ALIGN=CENTER" [th "COLSPAN=7" "[lindex $fldrdlabels [lindex $show 2]] "]]
set lastfldr [lindex $show 2]
}
if {$nstype == 6} {
RetryTransaction {
set rec2 [db $db openid [lindex $show 1]]
print_nowplayingfrow $chan $rec2 [lindex $show 0] $rcount
incr rcount
}
} else {
RetryTransaction {
set rec2 [db $db openid [lindex $show 1]]
print_nowplayingfrow $chan $rec2 [lindex $show 0]
}
}
}
}
puts -nonewline $chan [html_table_end]
if {$nstype == 6} {
if {$::lang == "en"} {
puts $chan [html_form_input "submit" "submit" "Delete Programmes"]
} else {
puts $chan [html_form_input "submit" "submit" "Delete Shows"]
}
puts $chan [html_form_end]
}
}
puts $chan [html_end]
}
proc print_nowplayingfrow {chan rec type args} {
global db
global tzoffset
global images
global cache_ns_rec
global cache_ns_series
global fldrdlabels
global fldrvals
set assign 0
set argcc [llength $args]
switch -exact -- $argcc {
1 { set rcount [lindex $args 0] }
2 { set assign 1
set prgfldridx [lindex $args 1] }
}
set showing [dbobj $rec get Showing]
set showingfsid [dbobj $rec gettarget Showing]
set station [dbobj $showing get Station]
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
set description [strim [dbobj $program get Description]]
set manual 0
set suggestion 0
set vod 0
set watchonly 0
if {$::version3} {
set recbeh [dbobj $rec get RecordingBehavior]
set presbeh [dbobj $recbeh get PresentationBehavior]
set progbeh [dbobj $recbeh get ProgramGuideBehavior]
set diskbeh [dbobj $recbeh get DiskBehavior]
if { $presbeh == 10 || $presbeh == 9 } {
set manual 1
}
if { $progbeh == 3 || $progbeh == 8 } {
set manual 1
}
if { $presbeh == 6 } {
set suggestion 1
}
if { $diskbeh == 7 } {
# for multipart
set watchonly 1
}
} else {
set seltype [dbobj $rec get SelectionType]
if { $seltype == 10 || $seltype == 5 } {
set manual 1
}
if { $seltype == 6 } {
set createby [dbobj $rec get CreatedBy]
if { $createby == 2 } {
set vod 1
} else {
set suggestion 1
}
}
}
if { $manual } {
if { $title == "" } {
set title "Manual Recording"
} else {
set title "Manual: $title"
}
}
set state [dbobj $rec get State]
set fsid [dbobj $rec fsid]
set index [lsearch $cache_ns_rec $fsid]
set seriesfsid ""
set series [dbobj $program get Series]
if { $index == -1 } {
if {$series != ""} {
set seriesfsid [dbobj $series fsid]
lappend cache_ns_rec $fsid
lappend cache_ns_series $seriesfsid
}
} else {
set seriesfsid [lindex $cache_ns_series $index]
}
if {$series != ""} {
set episodic [defaultval 1 [dbobj $series get Episodic]]
} else {
set episodic 1
}
if { $episodic == 1 } {
set eptitle [strim [dbobj $program get EpisodeTitle]]
if { $eptitle == "" } {
set eptitle "No Episode Title"
}
} else {
set eptitle "Not an Episode"
}
if { $::description_hover && $description != "" } {
set description [htmlEncode $description]
regsub -all {\"} $description {\"} description
set attrs "title=\"$description\""
} else {
set attrs ""
}
set seconds [expr [dbobj $showing get Date] * 86400 + [dbobj $showing get Time] + $tzoffset]
if {$::lang == "en"} {
set date ""
set daystr [nth [clock format $seconds -format "%e"]]
set day [clock format $seconds -format "%a $daystr %b"]
} else {
set day [clock format $seconds -format "%a"]
set date [clock format $seconds -format "%1m/%1d"]
}
set bookmark [dbobj $rec get Bookmark]
set duration [expr [dbobj $showing get Duration] / 60]
set totpad [expr [defaultval 0 [dbobj $rec get StartPadding]] + [defaultval 0 [dbobj $rec get EndPadding]]]
if {$totpad > 0} {
set duration "$duration +[expr $totpad / 60]"
}
if { $bookmark != "" } {
set durn "$duration ([expr [expr [dbobj $bookmark get TimeMs] / 1000] / (60)])"
} else {
set durn $duration
}
set expdate [dbobj $rec get ExpirationDate]
set expsecs [expr $expdate * 86400 + [dbobj $rec get ExpirationTime]]
set nowsecs [clock seconds]
if {$state == 5} {
set imagenum 20
} elseif {$state == 3} {
set imagenum 8
} elseif {$suggestion} {
set imagenum 10
} elseif {$vod} {
set imagenum 21
} elseif {$expdate == 24855} {
set imagenum 0
} elseif {$expsecs < $nowsecs} {
set imagenum 5
} elseif {$expsecs < [expr $nowsecs + 24*60*60]} {
set imagenum 3
} else {
set imagenum -1
}
if { $imagenum >= 0} {
set imagef [lindex $images $imagenum]
set imageh [img "alt=\"\"" $imagef]
if { $imagenum == 20 } { ;# recycled
set imageh [html_link "/confirmdelete/3/$fsid" [img "alt=\"undelete\"" $imagef]]
}
} else {
set imageh ""
}
set form_td ""
if {$type == 6} {
set form_td [td "ALIGN=CENTER" [html_form_checkbox "fsid_${rcount}_$fsid" ""]]
}
if { $assign == 1 } {
set form_td [td "" [html_form_select "fldridx_$fsid" $fldrvals $fldrdlabels $prgfldridx]]
set form_td "$form_td [td "align=center" [html_form_checkbox "fldrauto_$seriesfsid" ""]]"
}
if {$manual || $watchonly} {
puts $chan [tr "" [td $imageh] [td [html_link "/showing/$showingfsid" $title]] [td [html_link "/showing/$showingfsid" $eptitle $attrs]] [td $durn] [td $day] [td $date] $form_td]
} else {
puts $chan [tr "" [td $imageh] [td [html_link "/series/$seriesfsid" $title]] [td [html_link "/showing/$showingfsid" $eptitle $attrs]] [td $durn] [td $day] [td $date] $form_td]
}
}
# Ensure /var/hack/etc exists
file mkdir /var/hack/etc
register_module "folders" "Folders" "Now Playing with Sort and Folders"