Custom Mood Theme Editor head<= <=head body<= " unless $remote; my $authas = $GET{'authas'} || $remote->{'user'}; my $u = LJ::get_authas_user($authas); return LJ::bad_input("You could not be authenticated as the specified user.") unless $u; my $self_uri = "/manage/moodthemes.bml"; $self_uri .= "?authas=$authas" if $authas ne $remote->{'user'}; # Populated with all the moods later in editform my %lists; my $ret; #### Closure Definitions #### my $make_tree; $make_tree = sub { my ($num, $tid) = @_; return unless $lists{$num}; $ret .= "\n"; }; my $editform = sub { my $id = shift; # Get a list of all possible moods my $moods = LJ::get_moods(); foreach (sort { $moods->{$a}->{'name'} cmp $moods->{$b}->{'name'} } keys %$moods) { my $m = $moods->{$_}; push @{$lists{$m->{'parent'}}}, $m; } $make_tree->(0, $id); }; #### End Closure Definitions #### if (LJ::did_post()) { # They actually did something, figure out what my $themeid = $POST{'themeid'}; my $info; # Figure out if they are editing a theme and which one they are my @ids = split ',', $POST{'themeids'}; foreach (@ids) { $themeid = $_ if $POST{"edit:$_"}; } if (!$themeid) { # They decided to actually use a custom theme my $use_theme; foreach (@ids) { $use_theme = $_ if $POST{"use:$_"}; } if ($use_theme) { my %update; $update{'moodthemeid'} = $use_theme; foreach (keys %update) { delete $update{$_} if $u->{$_} eq $update{$_}; } LJ::update_user($u, \%update); return BML::redirect($self_uri); } } elsif ($POST{'isnew'} != 1) { # Make sure they can even edit this theme and fill in the $info variable for later use my $dbr = LJ::get_db_reader(); my $sth = $dbr->prepare("SELECT name FROM moodthemes WHERE moodthemeid=? AND ownerid=?"); $sth->execute($themeid, $u->{'userid'}); $info = $sth->fetchrow_hashref; return LJ::bad_input("You do not have permission to edit this theme.") unless defined $info; } # are we deleting a theme? foreach my $tid (@ids) { if ($POST{"delete:$tid"}) { # good, delete this one my $dbh = LJ::get_db_writer(); my $c = $dbh->do("DELETE FROM moodthemes WHERE moodthemeid = ? AND ownerid = ?", undef, $tid, $u->{userid})+0; return "" unless $c; $dbh->do("DELETE FROM moodthemedata WHERE moodthemeid = ?", undef, $tid); # safe since we verified $c return BML::redirect($self_uri); } } # We are either making changes to an existing theme or showing the edit form for a new theme if ($themeid && $POST{'edit'} == 1 || $POST{'isnew'} == 1) { # Insert the new theme name and description into the db and grab its new themeid if ($POST{'isnew'} == 1) { return LJ::bad_input("You must specify a name for this theme.") unless LJ::trim($POST{'name'}); my $dbh = LJ::get_db_writer(); my $i = $dbh->do("INSERT INTO moodthemes (ownerid, name, is_public) VALUES (?, ?, 'N')", undef, $u->{'userid'}, $POST{'name'}); return "" unless $i; $themeid = $dbh->{'mysql_insertid'}; $info->{'name'} = $POST{'name'}; } $ret .= "\n"; $ret .= "{name} h2?>\n"; # Make the form $ret .= "\n"; $ret .= "Theme Name: "; $ret .= LJ::html_text({ name => 'name', value => $info->{name}, size => 50, maxlength => 255 }); $ret .= "

\n"; $ret .= LJ::html_hidden('themeid' => $themeid) . "\n"; # Actually make the editor form $editform->($themeid); $ret .= LJ::html_submit('save' => "Save Changes") . " p?>\n"; } elsif ($themeid) { # Save their changes my $dbh = LJ::get_db_writer(); # Update the name or description if needed if ($info->{'name'} ne $POST{'name'}) { my $u = $dbh->do("UPDATE moodthemes SET name=? WHERE moodthemeid=?", undef, $POST{'name'}, $POST{'themeid'}); return "" unless $u; } # The fun part of figuring out what needs to be changed in the db my (@repl_values, @repl_bind, @del_values, @del_bind); foreach my $key (keys %POST) { # A key that is fully numeric signifies an actual mood theme. # We then build off this id number to get other information needed # about what the user specified. if ($key =~ /(^\d+$)/) { my $mid = $1; my $width = $POST{$mid.'w'}; my $height = $POST{$mid.'h'}; my $picurl = $POST{$key}; my $mname = LJ::mood_name($mid); if (($POST{$mid.'check'} ne 'checked') && (!$picurl || $width == 0 || $height == 0)) { # Delete this picture push @del_values, $mid; push @del_bind, ("?"); next; } if ($POST{$mid.'check'} eq 'checked') { # Inherited, thus don't represent it in the db push @del_values, ($themeid, $mid); push @del_bind, ("(moodthemeid=? AND moodid=?)"); $ret .= "$mname($mid) was deleted and will now be represented by its parent.
\n"; } else { # Not inherited, it is different than its parent or it is actually a parent if ($picurl) { push @repl_values, ($themeid, $mid, $picurl, $width, $height); push @repl_bind, ("(?, ?, ?, ?, ?)"); $ret .= "$mname($mid) is set to $picurl.
\n"; } } } } if (@del_values) { my $bind = join(",", @del_bind); my $p = $dbh->do("DELETE FROM moodthemedata WHERE moodthemeid=? AND moodid IN($bind)", undef, $themeid, @del_values); return "" unless $p; } my $bind = join(",", @repl_bind); my $i = $dbh->do("REPLACE INTO moodthemedata (moodthemeid, moodid, " . "picurl, width, height) VALUES $bind", undef, @repl_values); return "" unless $i; # Kill any memcache data about this moodtheme LJ::MemCache::delete([$themeid, "moodthemedata:$themeid"]); delete $LJ::CACHE_MOOD_THEME{$themeid}; $ret .= "
Changes have been successfully saved.

\n"; return BML::redirect($self_uri); } $ret .= BML::ml('Backlink', { 'link' => $self_uri, 'text' => 'Return To Editor', }); } else { # Show the first form to select user, which one to edit, or create a new one # user switcher $ret .= "
\n"; $ret .= LJ::make_authas_select($remote, { 'authas' => $GET{'authas'} }); $ret .= "
\n\n"; unless (LJ::get_cap($u, "moodthemecreate")) { $ret .= "Sorry, your account type does not allow creation of custom mood themes." } else { # form to allow users to create new mood themes $ret .= "\n"; $ret .= "
\n"; $ret .= LJ::html_hidden('isnew' => 1) . "\n"; $ret .= "Name: " . LJ::html_text({ name => 'name', size => 30, maxlength => 50 }); $ret .= LJ::html_submit('create' => "Create") . " p?>
\n"; # Make up the form to choose to edit a theme or create a new one $ret .= "\n"; # Get data to figure out if they have any themes already my $dbr = LJ::get_db_reader(); my $sth = $dbr->prepare("SELECT moodthemeid, name FROM moodthemes WHERE ownerid=?"); $sth->execute($u->{userid}); my @user_themes = (); push @user_themes, $_ while $_ = $sth->fetchrow_hashref; if (@user_themes) { # The have some custom themes already defined $ret .= "
\n"; $ret .= LJ::html_hidden('edit' => 1, 'themeids' => join(",", map { $_->{moodthemeid} } @user_themes)); $ret .= ""; $ret .= ""; foreach my $theme (@user_themes) { my $name = $theme->{'name'}; my $tid = $theme->{'moodthemeid'}; my $use_dis = 0; if ($tid == $u->{'moodthemeid'}) { $ret .= ""; $use_dis = 1; } else { $ret .= ""; } my @head_moods = (15, 25, 2); # happy, sad, angry foreach my $moodid (@head_moods) { my %pic = (); LJ::get_mood_picture($tid, $moodid, \%pic); $ret .= ""; } $ret .= ""; $ret .= ""; } $ret .= "
HappySadAngry
$name
$name"; if (%pic) { $ret .= ""; } else { $ret .= "[no image]"; } $ret .= "" . LJ::html_submit("edit:$tid" => "Edit") . " " . LJ::html_submit("use:$tid", "Use", {'disabled' => $use_dis}) . " " . LJ::html_submit("delete:$tid", "Delete", { onclick => 'return confirm("Are you sure you want to delete this mood theme?");' }) . "
"; $ret .= "
\n"; } else { $ret .= "\n"; } } $ret .= "Modify Journal page. p?>"; } return $ret; } _code?> <=body bodyopts=> onload="pageload();" page?>