LJSUP-17669: Login.bml form refactoring
[livejournal.git] / htdocs / moodlist.bml
blob2e80cfa2cedb9359d72ece08eb6a9d477ab4924d
1 <?page
2 title=><?_code return BML::ml('.title', {'sitename' => $LJ::SITENAMESHORT}); _code?>
3 head<=
4 <style>
5 .lj_moodlist_link {
6   text-decoration: none;
8 .lj_moodlist_link:link {
9   text-decoration: none;
11 .lj_moodlist_link:visited {
12   text-decoration: none;
14 .lj_moodlist_link:hover {
15   text-decoration: underline;
17 </style>
18 <=head
19 body<=
20 <?_code
22     use strict;
23     use vars qw(%GET);
25     LJ::set_active_crumb('moodlist');
27     my $ret;
28     my $sth;
29     my $dbr = LJ::get_db_reader();
31     my $remote = LJ::get_remote();
32     my $journal = $GET{'journal'} || $remote->{'user'};
33     my $u = LJ::load_user($journal);
35     my $add_header = sub {
36         $ret .= "<?h1 $ML{'.moods.header'} h1?><?p " . BML::ml('.moods.intro', {'sitename' => $LJ::SITENAME}) . " p?><?p " . BML::ml('.moods.howtochange', {'aopts' => "href='$LJ::SITEROOT/customize/style.bml'"}) . " p?>";
37     };
39     # Get a list of all possible moods
40     my $moods = LJ::get_moods();
41     my @mlist = ();
42     my %lists = {};
43     foreach (sort { $moods->{$a}->{'name'} cmp $moods->{$b}->{'name'} } keys %$moods) {
44         my $m = $moods->{$_};
45         push @mlist, $m;
46     }
48     # FIXME: cache this.  it's small.
49     $sth = $dbr->prepare("SELECT moodthemeid, name, is_public FROM moodthemes WHERE is_public='Y'");
50     $sth->execute;
51     my @themes = ();
52     while (my $moodtheme = $sth->fetchrow_hashref) {
53         my $is_active = LJ::run_hook("mood_theme_is_active", $moodtheme->{moodthemeid});
54         next unless !defined $is_active || $is_active;
55         push @themes, $moodtheme;
56     }
57     @themes = sort { lc($a->{name}) cmp lc($b->{name}) } @themes;
58     my @special_themes = LJ::run_hook('modify_mood_theme_list', \@themes, user => $u);
60     my $do_mood_list = sub {
61         # Setup the paging bar
62         my $perpage = 15;
63         my $start;
64         my $self_link;
65         my %items = BML::paging(\@themes, $GET{'page'} || 1, $perpage);
66         my $navbar = LJ::paging_bar($items{'page'}, $items{'pages'}, { 'self_link' => $self_link });
68         if ($items{'page'} == 1) {
69             $start = 0;
70         } else {
71             $start = ($items{'page'} - 1) * $perpage;
72         }
73         my @show_themes = splice (@themes, $start, $perpage);
75         # See if the user changed the shown moods
76         my @show_moods;
77         if($GET{'theme1'} && $GET{'theme2'} &&$ GET{'theme3'} && $GET{'theme4'}) {
78             @show_moods = ($GET{'theme1'}, $GET{'theme2'}, $GET{'theme3'}, $GET{'theme4'});
79         } else {
80             @show_moods = ('happy', 'sad', 'angry', 'tired');
81         }
83         my $mood_theme_table = sub {
84             my $theme = shift;
85             my $special = shift;
87             if ($special) {
88                 my $author = LJ::run_hook("mood_theme_author", $theme->{moodthemeid});
89                 $ret .= "<tr class='standout-border standout-background'><td style='padding-left: 5px;'>$theme->{'name'}";
90                 $ret .= "<br /><span style='font-size: smaller;'>" . BML::ml('.moodtheme.byauthor', {'author' => $author}) . "</span>" if $author;
91                 $ret .= "</td>\n";
92             } else {
93                 $ret .= "<tr><td>$theme->{'name'}</td>\n";
94             }
96             LJ::load_mood_theme($theme->{'moodthemeid'});
98             # Output each of the displayed moods
99             foreach my $mood (@show_moods) {
100                 if (LJ::get_mood_picture($theme->{'moodthemeid'}, LJ::mood_id($mood), \ my %pic)) {
101                     $ret .= "<td align='center'><img align='absmiddle' alt='$theme->{name}' src=\"$pic{'pic'}\" ".LJ::mood_size_attributes(%pic)." hspace='2' vspace='2' />" .
102                         "</td>\n";
103                 } else {
104                     $ret .= "<td></td>\n";
105                 }
106             }
108             $ret .= "<td align='center'>";
109             $ret .= BML::ml('Actionlink', {
110                 'link' => "<a href='$LJ::SITEROOT/moodlist.bml?moodtheme=$theme->{moodthemeid}'>$ML{'.nav.viewall'}</a>"
111             });
112             $ret .= "</td>";
113         };
115         $ret .= "<form action='moodlist.bml' method='get'>\n";
116         $ret .= LJ::html_hidden("page", $items{page});
117         $ret .= "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td></td>";
119         # Create the table columns for each mood
120         my $mname;
121         my $n = 1;
122         foreach $mname (@show_moods) {
123             $ret .= "<td align='center'>" .
124                 LJ::html_select({'name' => "theme$n", 'selected' => "$mname",
125                                  'style' => "text-align: center"},
126                                 map {$_->{name}, $_->{name}} @mlist) . "</td>\n";
127             $n++;
128         }
130         $ret .= "<td align='center'><input type='submit' value=\"$ML{'.btn.switch'}\" /></td></tr>\n";
132         if (@special_themes && $items{page} == 1) {
133             $ret .= "<tr class='standout-border standout-background'>";
134             $ret .= "<td style='padding-left: 5px;' colspan='8'><h2>$ML{'.specialthemes.header'}</h2></td>";
135             $ret .= "</tr>";
136             foreach my $theme (@special_themes) {
137                 $mood_theme_table->($theme, 1);
138             }
139         }
141         my %special_themeids = map { $_->{moodthemeid} => 1 } @special_themes;
142         foreach my $theme (@show_themes) {
143             next if $special_themeids{$theme->{moodthemeid}};
144             $mood_theme_table->($theme, 0);
145         }
147         $ret .= "<tr><td colspan='8' align='center'><br />$navbar</td></tr></table></form>";
148     };
150     my @user_themes;
151     # Determine the action depending on the GET arguments, or lack thereof
152     if (defined $GET{'moodtheme'}) {
153         $ret .= BML::ml('Backlink', {
154                 'link'=>"$LJ::SITEROOT/moodlist.bml",
155                 'text'=>$ML{'.back2'},
156             }). "<br />";
158         $add_header->();
160         # Check if the user is logged in and didn't specify an owner.  If so append their private mood themes
161         my $sth;
162         if ($remote && ! $GET{'ownerid'}) {
163             # FIXME: cache this.  it's small.
164             $sth = $dbr->prepare("SELECT moodthemeid, name, is_public FROM moodthemes WHERE ownerid=?");
165             $sth->execute($remote->{userid});
166             @user_themes = ();
167             push @user_themes, $_ while $_ = $sth->fetchrow_hashref;
168         } elsif ($GET{'ownerid'}) {
169             # FIXME: cache this.  it's small.
170             $sth = $dbr->prepare("SELECT moodthemeid, name, is_public FROM moodthemes WHERE ownerid=? AND moodthemeid=?");
171             $sth->execute($GET{'ownerid'}, $GET{'moodtheme'});
172             @user_themes = ();
173             push @user_themes, $_ while $_ = $sth->fetchrow_hashref;
174         }
176         # Sort the user themes
177         @user_themes = sort { lc($a->{name}) cmp lc($b->{name}) } @user_themes;
179         my $do_theme_detail = sub {
180             my ($themeid) = @_;
182             $ret .= "<br /><table width='100%'><tr><td colspan='5' align='center'>";
184             if ($GET{'ownerid'}) {
185                 $ret .= "<div style='margin-bottom:15px; font-weight:bold'>";
186                 $ret .= LJ::ehtml(@user_themes[0]->{name}) . ' - ';
187                 $ret .= LJ::ljuser(LJ::get_username($GET{'ownerid'}));
188                 $ret .= "</div></td></tr>";
189             } else {
190                 my @opts = ((map {$_->{moodthemeid}, $_->{name}} @themes),
191                         (@user_themes ? (0, "---") : ()),
192                         (map {$_->{moodthemeid}, $_->{name}} @user_themes));
194                 $ret .= "<div style='margin-bottom:15px'><form action='moodlist.bml' method='GET'>";
195                 $ret .= LJ::html_select({'name' => "moodtheme", 'selected' => "$themeid"},
196                                         @opts);
197                 $ret .= "&nbsp;<input type='submit' value=\"$ML{'.btn.view'}\" /></form>";
198                 $ret .= "<a href='/moodlist.bml?moodtheme=$themeid&mode=tree'>$ML{'.view.tree'}</a></div></td></tr>";
199             }
201             # Output all the moods
202             while (@mlist) {
203                 $ret .= "<tr>";
205                 # Show five moods in a row
206                 for (my $i = 0; $i < 5; $i++) {
207                    my $m = shift @mlist;
208                     my $mood = $m->{name};
209                     my %pic;
210                     if (LJ::get_mood_picture($themeid, $m->{id}, \%pic)) {
211                         $ret .= "<td style='text-align:center; vertical-align:bottom'>" .
212                             "<img align='absmiddle' style='margin-top:25px' alt='$m->{name}' src=\"$pic{'pic'}\" ".LJ::mood_size_attributes(%pic)." hspace='2' vspace='2' />" .
213                             "<br /><a class='lj_moodlist_link' href='http://dictionary.reference.com/search?q=$mood'>$mood</a><br /></td>";
214                     } else {
215                         $ret .= "<td style='text-align:center; vertical-align:bottom'>" .
216                             "<a class='lj_moodlist_link' href='http://dictionary.reference.com/search?q=$mood'>$mood</a>" .
217                             "<br /></td>";
218                     }
219                 }
220                 $ret .= "</tr>";
221             }
222             $ret .= "</table>";
223         };
225         my $do_mood_tree = sub {
226             my $self = shift;
227             my $num = shift;
228             return unless $lists{$num};
229             $ret .= "<ul>\n";
230             foreach my $mood (@{$lists{$num}}) {
231                 $ret .= "<li>" . BML::ml('.moodname', {'name' => "<b><a href=\"http://dictionary.reference.com/search?q=$mood->{'name'}\" target='dict'>$mood->{'name'}</a></b>", 'id' => $mood->{'id'}}) . "</li>\n";
232                 my %pic;
233                 if (LJ::get_mood_picture($GET{moodtheme}, $mood->{'id'}, \%pic)) {
234                     unless ($GET{'hidederived'} && $pic{'moodid'} != $mood->{'id'}) {
235                         $ret .= "<img align='absmiddle' src=\"$pic{'pic'}\" ".LJ::mood_size_attributes(%pic)." hspace='2' vspace='2' />\n";
236                     }
237                     if ($GET{'details'} && ($pic{'moodid'} != $mood->{'id'})) {
238                         $ret .= " $ML{'.moodname.fromparent'}";
239                     }
240                 } else {
241                     if ($GET{'details'}) {
242                         $ret .= "<b>" . BML::ml('.moodname.nopicfortheme', {'id' => $GET{moodtheme}}) . "</b>";
243                     }
244                 }
245                 $self->($self, $mood->{'id'});
246             }
247             $ret .= "</ul>\n";
248         };
250         # See if the user can even view this theme
251         my $theme = (grep { $_->{moodthemeid} == $GET{moodtheme} } (@themes, @user_themes))[0];
252         if (! $theme) {
253             # It isn't theirs, or they aren't logged in, and it isn't public and it actually exists
254             return LJ::bad_input($ML{'.error.cantviewtheme'});
255         } elsif ( defined $GET{mode} && $GET{mode} == 'tree') {
257             if ($GET{'ownerid'}) {
258                 $ret .= "<div style='margin-bottom:15px; font-weight:bold' align='center'>";
259                 $ret .= LJ::ehtml(@user_themes[0]->{name}) . ' - ';
260                 $ret .= LJ::ljuser(LJ::get_username($GET{'ownerid'}));
261                 $ret .= "</div></td></tr>";
262             } else {
263                 my @opts = ((map {$_->{moodthemeid}, $_->{name}} @themes),
264                    (@user_themes ? (0, "---") : ()),
265                             (map {$_->{moodthemeid}, $_->{name}} @user_themes));
266                 $ret .= "<div style='margin-bottom:15px; margin-top:30px' align='center'><form action='moodlist.bml' method='GET'>";
267                 $ret .= LJ::html_select({'name' => "moodtheme", 'selected' => "$GET{moodtheme}"},
268                                         @opts);
269                 $ret .= "&nbsp;<input type='submit' value=\"$ML{'.btn.view'}\" />";
270                 $ret .= LJ::html_hidden('mode' => 'tree');
271                 $ret .= "</form>";
272                 $ret .= "<a href='/moodlist.bml?moodtheme=$theme->{moodthemeid}'>$ML{'.view.table'}</a></div>";
273             }
274             $ret .= "<ul>\n";
276             foreach (sort { $moods->{$a}->{'name'} cmp $moods->{$b}->{'name'} } keys %$moods) {
277                 my $m = $moods->{$_};
278                 push @{$lists{$m->{'parent'}}}, $m;
279             }
281             $do_mood_tree->($do_mood_tree, 0);
282         } else {
283             $do_theme_detail->($GET{'moodtheme'});
284         }
286     } else {
287         $add_header->();
288         $do_mood_list->();
289     }
290     return $ret;
292 _code?>
294 <=body
295 page?><?_c <LJDEP>
296 form: htdocs/moodlist.bml
297 link: htdocs/customize/style.bml
298 </LJDEP> _c?>