ONO::Lib::UI::Rate

package ONO::Lib::UI::Rate;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::IO;

sub script {

#: Obsolete, only here for backwards compatibility, returns nothing.

return "";

}

sub init {

my (
$self,
$initial,
$switches,
) = @_;

#: Initialize rating logic.

if (!$initial || $initial > 10) {
$initial = "XX";
}

if (length $initial < 2) {
$initial = "0$initial";
}

my $hide;
if ($initial eq "XX") {
$hide = "hide";
}

my $class;
if ($switches =~ /c/) {
$class .= " auto";
}

return ($initial,$class,$hide);

}

sub stars_view {

my (
$self,
$initial,
$switches,
) = @_;

#: Display rating.
#:
#: -B box
#: -L large
#: -m mini

my ($initial,$class) = &init("",$initial,$switches);

my $STYLE = "width:80px";
my $PAD = "pad5_2";

if ($switches =~ /m/) {
$STYLE = "width:40px;height:8px";
}
if ($switches =~ /L/) {
$STYLE = "width:100px;height:20px";
$PAD = "p5";
}

if ($switches =~ /B/) {

return qq~<div class="box_paper $PAD" style="border:1px solid #ffffff">
<img class="block16$class" src="/ono/osr/images/rating/stars_$initial.gif" style="$STYLE" alt="">
</div>
~;

} else {

return qq~<img class="block16$class" src="/ono/osr/images/rating/stars_$initial.gif" style="$STYLE" alt="">~;

}

}

sub stars_save_file {

#: Store rating to a file

my (
$self,
$FILE,
$id,
$vars_ref,
) = @_;

my %vars = %$vars_ref;
my $count;
if ($vars{"rating_${id}_selected"} && $vars{"rating_${id}_selected"} < 11) {
for (my $i = 0; $i < $vars{"rating_${id}_selected"}; $i++) {
$count .= "x";
}
}
ONO::IO->store_rm($FILE,$count);

}

sub stars {

my (
$self,
$id,
$initial,
$switches,
) = @_;

#: Display stars (editor)
#:
#: -c center
#: -s save icon on change
#: -h half steps/stars
#: -m mini

my ($initial,$class,$hide) = &init("",$initial,$switches);

my $STARS = qq~<table id="rating_${id}_table" class="default_table$class" onmouseout="onojs_ui_rating_leave('$id');"
style="width:80px;height:16px;background-image:url('/ono/osr/images/rating/stars_$initial.gif');background-repeat:no-repeat">
<tr>
~;

my @opts = ('02','04','06','08','10');

my ($STYLE,$SAVE1,$SAVE2);

if ($switches =~ /h/) {
@opts = ('01','02','03','04','05','06','07','08','09','10');
$STYLE = qq~ style="width:8px"~;
}

if ($switches =~ /s/) {
$SAVE1 = qq~<td class="p0"><div id="rating_${id}_save_id" class="hide"><input type="submit" name="rating_${id}_save" value="save" class="button_green button_micro ml5"></div></td>~;
$SAVE2 = qq~onojs_block('rating_${id}_save_id');~;
}

foreach my $opt (@opts) {

$STARS .= qq~ <td class="p0">
<img class="block16" src="/ono/osr/images/spacer/trans.gif" onmouseover="onojs_ui_rating_update('$id','$opt');" onclick="onojs_ui_rating_select('$id','$opt');$SAVE2"$STYLE alt="">
</td>
~;

}

$STARS .= qq~ <td class="p0">
<img class="block16 trans30 $hide" id="rating_${id}_trash" src="/ono/osr/images/icons/crystal/16x16/actions/14_layer_deletelayer.png" alt="reset"
onmouseover = "onojs_ui_rating_update('$id','XX');"
onclick = "onojs_ui_rating_select('$id','XX');onojs_hide('rating_${id}_trash');$SAVE2">
</td>
$SAVE1
</tr>
</table>
<input type="hidden" id="rating_${id}_selected" name="rating_${id}_selected" value="$initial">
~;

return $STARS;

}

sub bar_view {

#: Display rating as bar.

my (
$self,
$initial,
$switches,
$color,
) = @_;

my $STYLE = "width:80px";
if ($switches =~ /m/) {
$STYLE = "width:40px";
}

my $width = 0;
if ($initial) {
$width = $initial*10;
}

if (!$color) {
$color = "#cccccc";
}

return qq~<div class="block16 bo" style="$STYLE;height:8px"><div style="background-color:$color;width:${width}%;height:8px"></div></div>~;

}

###############################################################################
# end of script
###############################################################################

1;

__END__