package DW::Setting::XPostAccounts; use base 'LJ::Setting'; use strict; use warnings; use Digest::MD5 qw(md5_hex); my $footer_length = 1024; # show this setting editor sub should_render { my ( $class, $u ) = @_; return 1; } # link to the help url sub helpurl { my ( $class, $u ) = @_; return "cross_post"; } # label; by default, displayed on the left side of the editor page sub label { my $class = shift; return $class->ml('setting.xpost.label'); } # option. this is where all of the entered info is displayed. in this case, # shows both the existing configured ExternalAccounts, as well as the UI for # adding new accounts. sub option { my ( $class, $u, $errs, $args, %opts ) = @_; return unless LJ::isu($u); # first load up the existing accounts. my @accounts = DW::External::Account->get_external_accounts($u); # label displayed at top of the option (right) section my $ret .= "

" . $class->ml('setting.xpost.option') . "

"; # check to see if we have an update message my $getargs = $opts{getargs}; if ($getargs) { if ( $getargs->{create} ) { my $acct = DW::External::Account->get_external_account( $u, $getargs->{create} ); # FIXME blue is temporary. move to css. $ret .= "
" . $class->ml( 'setting.xpost.message.create', { username => $acct->username, servername => $acct->servername } ) . "
"; } elsif ( $getargs->{update} ) { my $acct = DW::External::Account->get_external_account( $u, $getargs->{update} ); # FIXME blue is temporary. move to css. $ret .= "
" . $class->ml( 'setting.xpost.message.update', { username => $acct->username, servername => $acct->servername } ) . "
"; } } # convenience my $key = $class->pkgkey; # be sure to add your style info in htdocs/stc/settings.css or this won't # look very good. $ret .= "

" . $class->ml('setting.xpost.accounts') . "


"; $ret .= "\n"; if ( scalar @accounts ) { $ret .= ""; $ret .= "\n"; $ret .= "\n"; $ret .= "\n"; $ret .= "\n"; $ret .= "\n"; $ret .= "\n"; $ret .= "\n"; $ret .= "\n"; $ret .= ""; # display each account foreach my $externalacct (@accounts) { my $acctid = $externalacct->acctid; $ret .= "\n"; $ret .= ""; $ret .= ""; $ret .= ""; $ret .= ""; $ret .= "\n"; $ret .= ""; $ret .= "\n"; } } else { $ret .= "\n"; } $ret .= "
" . $class->ml('setting.xpost.option.username') . "" . $class->ml('setting.xpost.option.server') . "" . $class->ml('setting.xpost.option.xpostbydefault') . "" . $class->ml('setting.xpost.option.recordlink') . "" . $class->ml('setting.xpost.option.change') . "" . $class->ml('setting.xpost.option.delete') . "
" . $externalacct->username . "" . $externalacct->servername . "" . LJ::html_check( { name => "${key}xpostbydefault[${acctid}]", value => 1, id => "${key}xpostbydefault[${acctid}]", selected => $externalacct->xpostbydefault } ) . "" . LJ::html_check( { name => "${key}recordlink[${acctid}]", value => 1, id => "${key}recordlink[${acctid}]", selected => $externalacct->recordlink } ) . "" . $class->ml('setting.xpost.option.change') . "" . LJ::html_check( { name => "${key}delete[${acctid}]", value => 1, id => "${key}delete[${acctid}]", selected => 0 } ) . "
" . $class->ml('setting.xpost.noaccounts') . "
\n"; # show account usage. my $max_accounts = $u->count_max_xpost_accounts; $ret .= "

" . $class->ml( 'setting.xpost.message.usage', { current => scalar @accounts, max => $max_accounts } ); # add account if ( scalar @accounts < $max_accounts ) { $ret .= "

" . $class->ml('setting.xpost.btn.add') . "
\n"; } $ret .= "

" . $class->ml('setting.xpost.settings') . "

"; # disable comments on crosspost $ret .= ""; $ret .= ""; # When should the footer be displayed? $ret .= ""; # define custom footer $ret .= "
" . $class->ml('setting.xpost.comments') . ""; $ret .= LJ::html_check( { name => "${key}xpostdisablecomments", value => 1, id => "${key}xpostdisablecomments", selected => $u->prop('opt_xpost_disable_comments') } ) . "

"; $ret .= "
" . $class->ml('setting.xpost.footer') . ""; my $append_when = $u->prop('crosspost_footer_append'); $ret .= LJ::html_select( { name => "${key}crosspost_footer_append", id => "${key}crosspost_footer_append", class => "select", selected => $append_when || 'D' }, 'A' => $class->ml('setting.xpost.option.footer.when.always'), 'D' => $class->ml('setting.xpost.option.footer.when.disabled'), 'N' => $class->ml('setting.xpost.option.footer.when.never') ); $ret .= "
 
"; my $footer_text = $u->prop('crosspost_footer_text'); $ret .= LJ::html_textarea( { name => "${key}crosspost_footer_text", id => "${key}crosspost_footer_text", rows => 3, cols => 80, maxlength => "512", onkeyup => "javascript:updatePreview()", value => $footer_text } ) . "

"; $ret .= "\n"; # define custom footer $ret .= "
 
"; my $footer_nocomments = $u->prop('crosspost_footer_nocomments'); $ret .= LJ::html_textarea( { name => "${key}crosspost_footer_nocomments", id => "${key}crosspost_footer_nocomments", rows => 3, cols => 80, maxlength => "512", onkeyup => "javascript:updatePreviewNocomments()", value => $footer_nocomments } ) . "

"; $ret .= "\n"; my $baseurl = $LJ::SITEROOT; my $alttext = $class->ml('setting.xpost.option.footer.vars.comment_image.alt'); my $default_comment = $class->ml( 'xpost.redirect.comment2', { postlink => "%%url%%", openidlink => "$LJ::SITEROOT/openid/" } ); my $default_nocomments = $class->ml( 'xpost.redirect', { postlink => "%%url%%" } ); # the javascript. we have to do some special magic to get the lengths # to line up for differing newline characters and for unicode characters # outside the basic multilingual plane. $ret .= qq [ ]; $ret .= "

" . $class->ml('setting.xpost.option.footer.vars') . "
"; foreach my $var (qw(url reply_url comment_url comment_image)) { $ret .= "%%$var%%: " . $class->ml("setting.xpost.option.footer.vars.$var") . "
\n"; } $ret .= "
" . $class->ml("setting.xpost.footer.default.label") . "
" . LJ::ehtml($default_comment) . "\n"; $ret .= "

\n"; return $ret; } # each subclass can override if necessary sub error_check { 1 } # this is basically your form handler. takes the submitted form and makes # the appropriate changes. sub save { my ( $class, $u, $args ) = @_; $class->error_check( $u, $args ); # update existing accounts my @accounts = DW::External::Account->get_external_accounts($u); for my $account (@accounts) { my $acctid = $account->{'acctid'}; if ( $class->get_arg( $args, "displayed[$acctid]" ) ) { # delete account if selected if ( $class->get_arg( $args, "delete[$acctid]" ) ) { $account->delete(); } else { # check to see if we need to reset the xpostbydefault if ( $class->get_arg( $args, "xpostbydefault[$acctid]" ) ne $account->{'xpostbydefault'} ) { $account->set_xpostbydefault( $class->get_arg( $args, "xpostbydefault[$acctid]" ) ); } # check to see if we need to reset the recordlink if ( $class->get_arg( $args, "recordlink[$acctid]" ) ne $account->{'recordlink'} ) { $account->set_recordlink( $class->get_arg( $args, "recordlink[$acctid]" ) ); } } } } # reset disable comments $u->set_prop( opt_xpost_disable_comments => $class->get_arg( $args, "xpostdisablecomments" ) ? "1" : "0" ); # change footer text $u->set_prop( crosspost_footer_text => LJ::text_trim( $class->get_arg( $args, "crosspost_footer_text" ), 0, $footer_length ) ); $u->set_prop( crosspost_footer_nocomments => LJ::text_trim( $class->get_arg( $args, "crosspost_footer_nocomments" ), 0, $footer_length ) ); # change footer display $u->set_prop( crosspost_footer_append => $class->get_arg( $args, "crosspost_footer_append" ) ); return 1; } 1;