<?page
title=>LJ.Rossia.org Moods
head<=
<style>
.lj_moodlist_link {
  text-decoration: none;
}
.lj_moodlist_link:link {
  text-decoration: none;
}
.lj_moodlist_link:visited {
  text-decoration: none;
}
.lj_moodlist_link:hover {
  text-decoration: underline;
}
</style>
<=head
body<=
<?_code
{
    use strict;
    use vars qw(%GET);

    LJ::set_active_crumb('moodlist');

    my $ret;
    my $sth;
    my $dbr = LJ::get_db_reader();

    my $add_header = sub {
        $ret .= "<?h1 Moods h1?><?p The following are server-supported moods on <?sitename?>.  You can always enter your own, but these are the ones that can have pictures associated with them and can be searched on, etc.  You're encouraged to use these if possible.  p?><?p To change the icons in your journal, go to the <a href=\"/modify.bml\">modify journal page</a> and select your preferred icon set. p?>";
    };

    # Get a list of all possible moods
    my $moods = LJ::get_moods();
    my @mlist = ();
    my %lists = {};
    foreach (sort { $moods->{$a}->{'name'} cmp $moods->{$b}->{'name'} } keys %$moods) {
        my $m = $moods->{$_};
        push @mlist, $m;
    }

    # FIXME: cache this.  it's small.
    $sth = $dbr->prepare("SELECT moodthemeid, name, is_public FROM moodthemes WHERE is_public='Y'");
    $sth->execute;
    my @themes = ();
    push @themes, $_ while $_ = $sth->fetchrow_hashref;
    @themes = sort { lc($a->{name}) cmp lc($b->{name}) } @themes;

    my @user_themes;
    # Determine the action depending on the GET arguments, or lack thereof
    if (defined $GET{'moodtheme'}) {
        $ret .= BML::ml('Backlink', {
                'link'=>'/moodlist.bml',
                'text'=>'Mood Themes',
            }). "<br />";

        $add_header->();

        my $remote = LJ::get_remote();

        # Check if the user is logged in and didn't specify an owner.  If so append their private mood themes
        my $sth;
        if ($remote && ! $GET{'ownerid'}) {
            # FIXME: cache this.  it's small.
            $sth = $dbr->prepare("SELECT moodthemeid, name, is_public FROM moodthemes WHERE ownerid=?");
            $sth->execute($remote->{userid});
            @user_themes = ();
            push @user_themes, $_ while $_ = $sth->fetchrow_hashref;
        } elsif ($GET{'ownerid'}) {
            # FIXME: cache this.  it's small.
            $sth = $dbr->prepare("SELECT moodthemeid, name, is_public FROM moodthemes WHERE ownerid=? AND moodthemeid=?");
            $sth->execute($GET{'ownerid'}, $GET{'moodtheme'});
            @user_themes = ();
            push @user_themes, $_ while $_ = $sth->fetchrow_hashref;
        }

        # Sort the user themes
        @user_themes = sort { lc($a->{name}) cmp lc($b->{name}) } @user_themes;

        # See if the user can even view this theme
        my $theme = (grep { $_->{moodthemeid} == $GET{moodtheme} } (@themes, @user_themes))[0];
        if (! $theme) {
            # It isn't theirs, or they aren't logged in, and it isn't public and it actually exists
            return LJ::bad_input("You only have permission to view public mood themes or user themes with an ownerid parameter.");
        } elsif ( defined $GET{mode} && $GET{mode} == 'tree') {

            if ($GET{'ownerid'}) {
                $ret .= "<div style='margin-bottom:15px; font-weight:bold' align='center'>";
                $ret .= LJ::ehtml(@user_themes[0]->{name}) . ' - ';
                $ret .= LJ::ljuser(LJ::get_username($GET{'ownerid'}));
                $ret .= "</div></td></tr>";
            } else {
                my @opts = ((map {$_->{moodthemeid}, $_->{name}} @themes),
                   (@user_themes ? (0, "---") : ()),
                            (map {$_->{moodthemeid}, $_->{name}} @user_themes));
                $ret .= "<div style='margin-bottom:15px; margin-top:30px' align='center'><form action='moodlist.bml' method='GET'>";
                $ret .= LJ::html_select({'name' => "moodtheme", 'selected' => "$GET{moodtheme}"},
                                        @opts);
                $ret .= "&nbsp;<input type='submit' value='View' />";
                $ret .= LJ::html_hidden('mode' => 'tree');
                $ret .= "</form>";
                $ret .= "<a href='/moodlist.bml?moodtheme=$theme->{moodthemeid}'>View this theme in a table layout.</a></div>";
            }
            $ret .= "<ul>\n";

            foreach (sort { $moods->{$a}->{'name'} cmp $moods->{$b}->{'name'} } keys %$moods) {
                my $m = $moods->{$_};
                push @{$lists{$m->{'parent'}}}, $m;
            }

            do_mood_tree(0);
        } else {
            do_theme_detail($GET{'moodtheme'});
        }

    } else {
        $add_header->();
        do_mood_list();
    }
    return $ret;

    sub do_mood_list
    {
        # Setup the paging bar
        my $perpage = 15;
        my $start;
        my $self_link;
        my %items = BML::paging(\@themes, $GET{'page'} || 1, $perpage);
        my $navbar = LJ::paging_bar($items{'page'}, $items{'pages'}, { 'self_link' => $self_link });

        if ($items{'page'} == 1) {
            $start = 0;
        } else {
            $start = ($items{'page'} - 1) * $perpage;
        }
        my @show_themes = splice (@themes, $start, $perpage);

        # See if the user changed the shown moods
        my @show_moods;
        if($GET{'theme1'} && $GET{'theme2'} &&$ GET{'theme3'} && $GET{'theme4'}) {
            @show_moods = ($GET{'theme1'}, $GET{'theme2'}, $GET{'theme3'}, $GET{'theme4'});
        } else {
            @show_moods = ('happy', 'sad', 'angry', 'tired');
        }

        $ret .= "<form action='moodlist.bml' method='get'>\n";
        $ret .= LJ::html_hidden("page", $items{page});
        $ret .= "<table width='100%' border='0'><tr><td></td>";

        # Create the table columns for each mood
        my $mname;
        my $n = 1;
        foreach $mname (@show_moods) {
            $ret .= "<td align='center'>" .
                LJ::html_select({'name' => "theme$n", 'selected' => "$mname",
                                 'style' => "text-align: center"},
                                map {$_->{name}, $_->{name}} @mlist) . "</td>\n";
            $n++;
        }

        $ret .= "<td align='center'><input type='submit' value='Switch' /></td></tr>\n";

        # Output a table row for each mood theme
        foreach my $theme (@show_themes) {
            $ret .= "<tr><td>$theme->{'name'}</td>\n";

            LJ::load_mood_theme($theme->{'moodthemeid'});

            # Output each of the displayed moods
            foreach my $mood (@show_moods) {
                if (LJ::get_mood_picture($theme->{'moodthemeid'}, LJ::mood_id($mood), \ my %pic)) {
                    $ret .= "<td align='center'><img align='absmiddle' alt='$theme->{name}' src=\"$pic{'pic'}\" width='$pic{'w'}' height='$pic{'h'}' hspace='2' vspace='2' />" .
                        "</td>\n";
                } else {
                    $ret .= "<td></td>\n";
                }
            }

            $ret .= "<td align='center'>";
            $ret .= BML::ml('Actionlink', {
                'link' => "<a href='/moodlist.bml?moodtheme=$theme->{moodthemeid}'>View&nbsp;All</a>"
            });
            $ret .= "</td>";
        }
        $ret .= "<tr><td colspan='8' align='center'><br />$navbar</td></tr></table></form>";
    }

   sub do_theme_detail
   {
       my ($themeid) = @_;

       $ret .= "<br /><table width='100%'><tr><td colspan='5' align='center'>";

       if ($GET{'ownerid'}) {
           $ret .= "<div style='margin-bottom:15px; font-weight:bold'>";
           $ret .= LJ::ehtml(@user_themes[0]->{name}) . ' - ';
           $ret .= LJ::ljuser(LJ::get_username($GET{'ownerid'}));
           $ret .= "</div></td></tr>";
       } else {
           my @opts = ((map {$_->{moodthemeid}, $_->{name}} @themes),
                   (@user_themes ? (0, "---") : ()),
                   (map {$_->{moodthemeid}, $_->{name}} @user_themes));

           $ret .= "<div style='margin-bottom:15px'><form action='moodlist.bml' method='GET'>";
           $ret .= LJ::html_select({'name' => "moodtheme", 'selected' => "$themeid"},
                                   @opts);
           $ret .= "&nbsp;<input type='submit' value='View' /></form>";
           $ret .= "<a href='/moodlist.bml?moodtheme=$themeid&mode=tree'>View this theme in a hierarchal tree layout.</a></div></td></tr>";
       }

       # Output all the moods
       while (@mlist) {
           $ret .= "<tr>";

           # Show five moods in a row
           for (my $i = 0; $i < 5; $i++) {
               my $m = shift @mlist;
               my $mood = $m->{name};
               my %pic;
               if (LJ::get_mood_picture($themeid, $m->{id}, \%pic)) {
                   $ret .= "<td style='text-align:center; vertical-align:bottom'>" .
                       "<img align='absmiddle' style='margin-top:25px' alt='$m->{name}' src=\"$pic{'pic'}\" width='$pic{'w'}' height='$pic{'h'}' hspace='2' vspace='2' />" .
                       "<br /><a class='lj_moodlist_link' href='http://www.dictionary.com/cgi-bin/dict.pl?term=$mood'>$mood</a><br /></td>";
               } else {
                   $ret .= "<td style='text-align:center; vertical-align:bottom'>" .
                       "<a class='lj_moodlist_link' href='http://www.dictionary.com/cgi-bin/dict.pl?term=$mood'>$mood</a>" .
                       "<br /></td>";
               }
           }
           $ret .= "</tr>";
       }
       $ret .= "</table>";
   }

    sub do_mood_tree
    {
        my $num = shift;
        return unless $lists{$num};
        $ret .= "<ul>\n";
        foreach my $mood (@{$lists{$num}}) {
            $ret .= "<li><b><a href=\"http://www.dictionary.com/cgi-bin/dict.pl?term=$mood->{'name'}\" target='dict'>$mood->{'name'}</a></b> (#$mood->{'id'})</li>\n";
            my %pic;
            if (LJ::get_mood_picture($GET{moodtheme}, $mood->{'id'}, \%pic)) {
                unless ($GET{'hidederived'} && $pic{'moodid'} != $mood->{'id'}) {
                    $ret .= "<img align='absmiddle' src=\"$pic{'pic'}\" width='$pic{'w'}' height='$pic{'h'}' hspace='2' vspace='2' />\n";
                }
                if ($GET{'details'} && ($pic{'moodid'} != $mood->{'id'})) {
                    $ret .= " (from parent)";
                }
            } else {
                if ($GET{'details'}) {
                    $ret .= "<b>(no pic for theme \#$GET{moodtheme})</b>";
                }
            }
            do_mood_tree($mood->{'id'});
        }
        $ret .= "</ul>\n";
    }

}
_code?>

<=body
page?><?_c <LJDEP>
form: htdocs/moodlist.bml
link: htdocs/modify.bml
</LJDEP> _c?>
