mourningdove/htdocs/manage/moodthemes.bml
2026-05-24 01:03:05 +00:00

511 lines
21 KiB
Text

<?_c
# This code was forked from the LiveJournal project owned and operated
# by Live Journal, Inc. The code has been modified and expanded by
# Dreamwidth Studios, LLC. These files were originally licensed under
# the terms of the license supplied by Live Journal, Inc, which can
# currently be found at:
#
# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
#
# In accordance with the original license, this code and all its
# modifications are provided under the GNU General Public License.
# A copy of that license can be found in the LICENSE file included as
# part of this distribution.
_c?>
<?page
title=><?_ml .title _ml?>
head<=
<script language="Javascript" type="text/javascript">
<!--
// Since JavaScript is dumb, we must get the page URL and save it here, to compare
// against later when we're seeing if we actually loaded an image or not...
var page_url = document.URL;
// Designed to update the fields for the image just changed as well as
// well as all those that inherit from it.
function update_children(id) {
if (! document.getElementById) return true;
var i = id+'img';
var url = document.getElementById(id);
var img = document.getElementById(i);
// If they actually have us something useful
if (url.value != "") {
var newimage = new Image();
newimage.src=url.value;
var oldurl = img.src;
// Update itself
img.src = url.value;
img.width = newimage.width;
img.height = newimage.height;
var w = id+'w';
var h = id+'h';
var width = document.getElementById(w);
var height = document.getElementById(h);
width.value = newimage.width;
height.value = newimage.height;
var form = document.getElementById('editform');
// Update everything to be its parent
for (var z = 0; z < form.elements.length; z++) {
var inherit = document.getElementById(form[z].id+'inherit');
var parent = document.getElementById(form[z].id+'parent');
if ((parent != undefined) && (inherit != undefined && inherit.checked == true)) {
// our ids and stuff
var pid = parent.value; // our parent's id
var oid = form[z].id; // our id
// at this point we explode!
var par_img = document.getElementById(pid+'img');
if (par_img.src == page_url) {
continue;
}
var our_img = document.getElementById(oid+'img');
our_img.src = par_img.src;
our_img.width = par_img.width;
our_img.height = par_img.height;
// now copy our_img info into text fields
var our_url = document.getElementById(oid);
var our_width = document.getElementById(oid+'w');
var our_height = document.getElementById(oid+'h');
our_url.value = par_img.src;
our_width.value = par_img.width;
our_height.value = par_img.height;
}
}
}
return false;
}
// Logic behind grabbing the parent when checking inherit
function enable(id, parent) {
if (! document.getElementById) return true;
var check = document.getElementById(id+'inherit');
var url = document.getElementById(id);
var w = document.getElementById(id+'w');
var h = document.getElementById(id+'h');
var fill = switchdisable(id, check, url, w, h);
var pi = document.getElementById(parent+'img');
// For some dumb reason an img tag with no source has the src of the pages url
if(fill && parent != id && pi.src != page_url) {
url.value = pi.src;
w.value = pi.width;
h.value = pi.height;
var i = document.getElementById(id+'img');
i.src = pi.src;
i.width = pi.width;
i.height = pi.height;
}
}
// If a set of fields is disabled, it enables them and vice versa
function switchdisable(id, check, url, w, h) {
if (! document.getElementById) return true;
if (check == undefined) {
var check = document.getElementById(id+'inherit');
var url = document.getElementById(id);
var w = document.getElementById(id+'w');
var h = document.getElementById(id+'h');
}
if (check.checked == true) {
url.disabled = true;
w.disabled = true;
h.disabled = true;
return true;
} else {
url.disabled = false;
w.disabled = false;
h.disabled = false;
return false;
}
}
// Since not all browsers support JS, we need to use JS to disable form fields
// instead of doing it in Perl land.
function pageload() {
if (! document.getElementById) return true;
var form = document.getElementById('editform');
if (form == undefined) {
return false;
}
for (var z = 0; z < form.elements.length; z++) {
var inherit = document.getElementById(form[z].id+'inherit');
if (inherit != undefined && inherit.checked == true) {
switchdisable(form[z].id, undefined, undefined, undefined, undefined);
}
}
}
// -->
</script>
<=head
body<=
<?_code
{
use strict;
use vars qw(%POST %GET);
my $remote = LJ::get_remote();
return "<?needlogin?>" unless $remote;
my $authas = $GET{authas} || $remote->user;
my $u = LJ::get_authas_user( $authas );
return LJ::bad_input( $ML{'error.invalidauth'} )
unless $u;
my $self_uri = "/manage/moodthemes";
$self_uri .= "?authas=$authas" if $authas ne $remote->user;
# Populated with all the moods later in editform
my %lists;
# one formauth for the whole page
my $form_auth = LJ::form_auth();
my $ret = '';
my $warn = '';
#### Closure Definitions ####
my $make_tree;
$make_tree = sub {
my ( $num, $theme ) = @_;
return unless $lists{$num};
$ret .= "<ul>\n";
foreach my $mood ( @{ $lists{$num} } ) {
$ret .= "<li><b>$mood->{name}</b>";
my %pic;
$theme->get_picture( $mood->{id}, \%pic ) if $theme;
if ( %pic ) {
$ret .= "<img id='$mood->{id}img' align='absmiddle'";
$ret .= " src=\"$pic{pic}\" width='$pic{w}' height='$pic{h}'";
$ret .= " hspace='2' vspace='2' /></li>\n";
}
# Figure out if this picture is really just inherited from a parent
my $inherited = ( $pic{moodid} != $mood->{id} ) ? 1 : 0;
$ret .= "<table summary=''>";
if ( $mood->{parent} ) {
$ret .= "<tr><td colspan='2'><label for='$mood->{id}inherit'>";
$ret .= BML::ml( '.mood.label.inherit',
{ mood => DW::Mood->mood_name( $mood->{parent} ) } );
$ret .= "</label> ";
$ret .= LJ::html_check( { type => 'check',
id => "$mood->{id}inherit",
name => "$mood->{id}inherit",
onclick => "enable($mood->{id}, $mood->{parent})",
selected => $inherited } );
$ret .= "</td></tr>\n";
}
$ret .= "<tr><td>$ML{'.mood.label.url'} </td><td>";
$ret .= LJ::html_text({ name => $mood->{id}, id => $mood->{id}, value => $pic{pic},
size => 75, onchange => "update_children($mood->{id})" });
$ret .= "</td></tr>\n";
$ret .= "<tr><td>$ML{'.mood.label.width'} </td><td>";
$ret .= LJ::html_text({ name => "$mood->{id}w", id => "$mood->{id}w", value => $pic{w}, size => 4 });
$ret .= " $ML{'.mood.label.height'} " . LJ::html_text({ name => "$mood->{id}h", id => "$mood->{id}h",
value => $pic{h}, size => 4 });
$ret .= "<br /><br /></td></tr>\n";
$ret .= LJ::html_hidden({ name => "$mood->{id}parent", id => "$mood->{id}parent", value => $mood->{parent} });
$ret .= LJ::html_hidden({ name => "$mood->{id}oldinh", id => "$mood->{id}oldinh", value => $inherited });
$ret .= "</table>\n";
$make_tree->( $mood->{id}, $theme );
}
$ret .= "</ul>\n";
};
my $editform = sub {
my ( $id ) = @_;
my $theme = DW::Mood->new( $id );
# Get a list of all possible moods
my $moods = DW::Mood->get_moods;
foreach ( sort { $moods->{$a}->{name} cmp $moods->{$b}->{name} } keys %$moods ) {
my $m = $moods->{$_};
push @{ $lists{ $m->{parent} } }, $m;
}
$make_tree->( 0, $theme );
};
#### End Closure Definitions ####
if ( LJ::did_post() ) { # They actually did something, figure out what
return "<b>$ML{'Error'}</b> $ML{'error.invalidform'}"
unless LJ::check_form_auth();
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{$_};
}
$u->update_self( \%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
$info = DW::Mood->get_themes( { themeid => $themeid,
ownerid => $u->id } );
return LJ::bad_input( $ML{'.error.notyourtheme'} )
unless defined $info;
}
# are we deleting a theme?
foreach my $tid ( @ids ) {
if ( $POST{"delete:$tid"} ) {
# good, delete this one
my $c = $u->delete_moodtheme( $tid );
return "<?h1 $ML{'Error'} h1?><?p $ML{'.error.cantdeletetheme'} p?>" unless $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( $ML{'.error.nonamegiven'} )
unless LJ::trim( $POST{name} );
my $err;
$themeid = $u->create_moodtheme( $POST{name}, '', \$err )
or return "<?h1 $ML{'Error'} h1?><?p $err p?>";
$info->{name} = $POST{name};
}
$ret .= "<?hr?>\n<?h2 ";
$ret .= BML::ml( '.editingtheme.header',
{ name => LJ::ehtml( $info->{name} ) } );
$ret .= " h2?>\n";
# Make the form
$ret .= "<?p <form action='$self_uri' method='post' id='editform' name='editform'>\n";
$ret .= $form_auth;
$ret .= "<b>$ML{'.editingtheme.label.name'}</b> ";
$ret .= LJ::html_text( { name => 'name', value => $info->{name},
size => 50, maxlength => 255 } );
$ret .= "<br /><br />\n";
$ret .= LJ::html_hidden( 'themeid' => $themeid ) . "\n";
# Actually make the editor form
$editform->( $themeid );
$ret .= LJ::html_submit( 'save' => $ML{'.btn.savechanges'} );
$ret .= "</form> p?>\n";
} elsif ( $themeid ) { # Save their changes
my $theme = DW::Mood->new( $themeid ) or return
"<?h1 $ML{'Error'} h1?><?p $ML{'.error.cantupdatetheme'} p?>";
# Update the name or description if needed
if ( $info->{name} ne $POST{name} ) {
$theme->update( 'name' => $POST{name} ) or return
"<?h1 $ML{'Error'} h1?><?p $ML{'.error.cantupdatetheme'} p?>";
}
# The fun part of figuring out what needs to be changed in the db
my @picdata;
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'} || 0;
my $height = $POST{$mid.'h'} || 0;
my $picurl = $POST{$key};
my $mname = DW::Mood->mood_name( $mid );
# don't update if nothing changed
my %picdata;
$theme->get_picture( $mid, \%picdata );
my $same = $picurl eq $picdata{pic} &&
$width == $picdata{w} &&
$height == $picdata{h};
next if $same;
# allow width & height to default to that of parent
if ( my $pid = $POST{$mid . 'parent'} ) {
$width ||= $POST{$pid . 'w'};
$height ||= $POST{$pid . 'h'};
unless ( $width && $height ) {
# check the database
my %parent;
$theme->get_picture( $pid, \%parent );
$width ||= $parent{w};
$height ||= $parent{h};
}
}
# one of these is blank, so delete the mood
unless ( $picurl && $width && $height ) {
push @picdata, [ $mid, {} ];
if ( $picdata{pic} ) {
$ret .= BML::ml( '.mood.reset', { mood => "$mname($mid)"} ) . "<br />\n";
} else {
$warn .= BML::ml( '.mood.notcreated', { mood => "$mname($mid)" } ) . "<br />\n";
}
next;
}
# we have a picurl, width, and height. add it.
if ( $picurl =~ m!^https?://[^\'\"\0\s]+$! ) {
push @picdata, [ $mid, { picurl => $picurl, width => $width, height => $height } ];
$ret .= BML::ml( '.mood.setpic', { mood => "$mname($mid)", url => $picurl } ) . "<br />\n";
}
}
}
# look again for inheritance
# (id key for url was disabled, so no match above)
foreach my $key ( keys %POST ) {
if ( $key =~ /^(\d+)inherit$/ ) {
my $mid = $1;
next if $POST{$mid}; # already processed above
next if $POST{$mid . 'oldinh'}; # no change in status
# inherited, don't represent in db
my $mname = DW::Mood->mood_name( $mid );
if ( $POST{$key} eq 'on' ) {
push @picdata, [ $mid, {} ];
$ret .= BML::ml( '.mood.deleted', { mood => "$mname($mid)"} ) . "<br />\n";
}
}
}
my $err;
$theme->set_picture_multi( \@picdata, \$err ) or
return "<?h1 $ML{'Error'} h1?><?p $err p?>";
if ( $warn ) {
$ret .= "<br />$ML{'.partiallysaved'}<br /><br />$warn<br />\n";
} else {
$ret .= "<br />$ML{'.saved'}<br /><br />\n";
}
}
$ret .= BML::ml('Backlink', {
'link' => $self_uri,
'text' => $ML{'.back2'},
});
} else { # Show the first form to select user, which one to edit, or create a new one
# user switcher
$ret .= "<form action='/manage/moodthemes' method='get'>\n";
$ret .= LJ::make_authas_select($remote, { 'authas' => $GET{'authas'} });
$ret .= "</form>\n\n";
my $moodtheme_upsell = LJ::Hooks::run_hook("moodtheme_upsell");
unless ( $u->can_create_moodthemes ) {
if ($moodtheme_upsell) {
$ret .= $moodtheme_upsell;
} else {
$ret .= $ML{'.error.cantcreatethemes'};
}
} else {
# form to allow users to create new mood themes
$ret .= "<?h1 $ML{'.createtheme.header'} h1?>\n";
$ret .= "<form action='$self_uri' method='post' id='editform' name='editform' style='margin-left: 30px;'>\n";
$ret .= $form_auth;
$ret .= LJ::html_hidden('isnew' => 1) . "\n";
$ret .= "<?p <b>$ML{'.createtheme.label.name'}</b> " . LJ::html_text({ name => 'name', size => 30, maxlength => 50 });
$ret .= LJ::html_submit('create' => $ML{'.btn.create'}) . " p?></form>\n";
# Make up the form to choose to edit a theme or create a new one
$ret .= "<?h1 $ML{'.yourthemes.header'} h1?>\n";
# Get data to figure out if they have any themes already
my @user_themes = DW::Mood->get_themes( { ownerid => $u->id } );
if (@user_themes) { # The have some custom themes already defined
$ret .= "<form action='$self_uri' method='post' id='selectform' name='selectform' style='margin-left: 30px;'>\n";
$ret .= $form_auth;
$ret .= LJ::html_hidden('edit' => 1,
'themeids' => join(",", map { $_->{moodthemeid} } @user_themes));
$ret .= "<table>";
$ret .= "<thead><tr><th></th><th>$ML{'.yourthemes.example.happy'}</th><th>$ML{'.yourthemes.example.sad'}</th><th>$ML{'.yourthemes.example.angry'}</th><th></th></tr></thead>";
foreach my $theme ( @user_themes ) {
my $ename = LJ::ehtml( $theme->{name} );
my $tid = $theme->{moodthemeid};
my $tobj = DW::Mood->new( $tid );
my $use_dis = 0;
if ( $tid == $u->moodtheme ) {
$ret .= "<tr><td><b><a href='/moodlist?moodtheme=$tid&ownerid=$u->{'userid'}'>$ename</a></b></td>";
$use_dis = 1;
} else {
$ret .= "<tr><td><a href='/moodlist?moodtheme=$tid&ownerid=$u->{'userid'}'>$ename</a></td>";
}
my @head_moods = (15, 25, 2); # happy, sad, angry
foreach my $moodid (@head_moods) {
my %pic = ();
$tobj->get_picture( $moodid, \%pic ) if $tobj;
$ret .= "<td>";
if (%pic) {
$ret .= "<img align='absmiddle' src=\"$pic{'pic'}\" width='$pic{'w'}' height='$pic{'h'}' hspace='2' vspace='2' />";
} else {
$ret .= "<i>$ML{'.yourthemes.example.none'}</i>";
}
$ret .= "</td>";
}
my $confirm_delete = LJ::ejs($ML{'.yourthemes.delete.confirm'});
$ret .= "<td>" . LJ::html_submit("edit:$tid" => $ML{'.btn.edit'}) . " " .
LJ::html_submit("use:$tid", $ML{'.btn.use'}, {'disabled' => $use_dis}) . " " .
LJ::html_submit("delete:$tid", $ML{'.btn.delete'},
{ onclick => "return confirm('$confirm_delete');" }) . "</td>";
$ret .= "</tr>";
}
$ret .= "</table>";
$ret .= "</form>\n";
} else {
$ret .= "<?p $ML{'.yourthemes.nothemes'} p?>\n";
}
}
$ret .= "<?p " . BML::ml('.usepublictheme2', {'aopts' => "href='$LJ::SITEROOT/customize/options?group=display'"}) . " p?>"
unless $moodtheme_upsell;
}
return $ret;
}
_code?>
<=body
bodyopts=> onload="pageload();"
page?>