root/trunk/htdocs/friends/add.bml @ 1083

Revision 1083, 6.2 KB (checked in by bradfitz, 11 years ago)

lets people remove friends from the friend add page. :)

okay, sounds counter-intuitive, but try it out... it's nice.

maybe need to change userinfo.bml toolbar link later.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1(=_CODE
2
3 my $dbs = LJ::get_dbs();
4 my $dbh = $dbs->{'dbh'};
5 my $dbr = $dbs->{'reader'};
6
7 my $remote = LJ::get_remote($dbs);
8 my $user = $FORM{'user'};
9 my $userid = LJ::get_userid($dbs, $user);
10 $body = "";
11
12 unless ($remote)
13 {
14     $title = "Add Friend";
15     $body = "(=H1 Login First H1=)(=P To add a user to your friends list you must first go and <A HREF=\"/login.bml?ret=1\">log in</A>.  If you don't already have an account you can <a href=\"/create.bml\">create one</a> to track your friend's journals. P=)";
16     return;
17 }
18
19 unless ($user && $userid)
20 {
21     $title = "Error";
22     $body = "Invalid or missing username given.  To add a friend, go to the <A HREF=\"/friends/edit.bml\">edit friends</A> page.";
23     return;
24 }
25 
26 if ($FORM{'mode'} eq "add")
27 {
28     unless (LJ::did_post()) {
29         $title = "Error";
30         $body = "(=H1 Error H1=)(=P (=REQUIREPOST=) P=)";
31         return;
32     }
33
34     my $gmask = 1;
35     foreach my $bit (1..30) {
36         next unless $FORM{"bit_$bit"};
37         $gmask |= (1 << $bit);
38     }
39
40     my $req = {
41         "user" => $remote->{'user'},
42         "mode" => "editfriends",
43         "ver"  => $LJ::PROTOCOL_VER,
44     };
45
46
47     if ($FORM{'action:delete'}) {
48         $req->{"editfriend_delete_$user"} = 1;
49     } else {
50         $req->{"editfriend_add_1_user"} = $user;
51         $req->{"editfriend_add_1_fg"} = $FORM{'editfriend_add_1_fg'};
52         $req->{"editfriend_add_1_bg"} = $FORM{'editfriend_add_1_bg'};
53         $req->{"editfriend_add_1_groupmask"} = $gmask;
54     }
55     
56     my %res = ();
57     LJ::do_request($dbh, $req, \%res,
58                    { "noauth" => 1, "userid" => $remote->{'userid'} } );
59     
60     if ($res{'success'} eq "OK")
61     {
62         if ($FORM{'action:delete'}) {
63             $title = "Friend Removed";
64             $body = "(=H1 Success H1=)(=P User (=LJUSER $user LJUSER=) was removed from your friends list.  You can view your friends page <A HREF=\"$LJ::SITEROOT/users/$remote->{'user'}/friends\">here</A>. P=)";
65         } else {
66             $title = "Friend Added!";
67             $body = "(=H1 Success H1=)(=P User (=LJUSER $user LJUSER=) was added to your friend list.  You can view your friends page <A HREF=\"$LJ::SITEROOT/users/$remote->{'user'}/friends\">here</A>. P=)";
68         }
69     } else {
70         $title = "Error";
71         $body = "(=H1 Error H1=)(=P $res{'errmsg'} P=)";
72     }
73     return;
74 }
75
76 # check to see if user is already a friend.
77 $sth = $dbr->prepare("SELECT * FROM friends WHERE userid=$remote->{'userid'} AND friendid=$userid");
78 $sth->execute;
79 my $fr = $sth->fetchrow_hashref;
80 
81 if ($fr) {
82     $title .= "Modify Friend";
83     $body .= "(=H1 Modify Friend H1=)(=P You already have (=LJUSER $user LJUSER=) listed as a friend.  Here you can either modify the settings (colors & groups) or remove $user from your friends list. P=)";
84 } else {
85     $title .= "Add Friend";
86     $body .= "(=H1 Add $user as a friend? H1=)(=P To add <B>$user</B> to your friends list, click the button below.  P=)";
87 }
88
89 $body .= "<form method='post'>";
90 $body .= "<input type='hidden' name='mode' value='add'>";
91 $body .= "<input type='hidden' name='user' value=\"$user\">";
92
93 if ($fr) {
94     $body .= "<center><input type='submit' value=\"Modify\">";
95     $body .= " - <input type='submit' name='action:delete' value=\"Remove\"></center>";
96 } else {
97     $body .= "<center><input type='submit' value=\"Add $user!\"></center>";
98 }
99
100 ## let them pick friend groups
101 $body .= "(=H1 Friend Groups H1=)(=P What friend groups do you want to put this user in?  Friend groups are used for filtering your friends list view and also for group-based viewing security of items. P=)<blockquote>";
102 my $err;
103 my $greq = LJ::Protocol::do_request($dbs, "getfriendgroups", {
104     'username' => $remote->{'user'},
105     'ver'      => $LJ::PROTOCOL_VER,
106 }, \$err, { 'noauth' => 1 });
107
108 if (@{$greq->{'friendgroups'}}) {
109     foreach my $g (@{$greq->{'friendgroups'}}) {
110         my $ck = ($fr && ($fr->{'groupmask'} & (1 << $g->{'id'}))) ?
111             "checked='1'" : "";
112
113         # by default, newly added friends are in default view unless unchecked
114         $ck = "checked='1'" if (! $fr && $g->{'name'} eq "Default View");
115
116         $body .= "<input type='checkbox' value='1' name='bit_$g->{'id'}' $ck> ";
117         $body .= LJ::ehtml($g->{'name'}) . "<br />\n";
118     }
119 } else {
120     $body .= "<i>No friend groups setup.</i>";
121 }
122 $body .= "</blockquote>";
123
124 ## let them pick the colors
125 $body .= "(=H1 Colors H1=)(=P You may also optionally select the colors that will represent $user in your friends list.  P=)";
126
127 $ret .= "<P><CENTER><TABLE CELLPADDING=4><TR><TD><B>Foreground</B></TD><TD><B>Background</B></TD></TR>\n";
128
129 my @color = ();
130 LJ::load_codes($dbs, { "color" => \@color });
131
132 my $sel = $fr || { 'fgcolor' => '#000000',
133                    'bgcolor' => '#FFFFFF', };
134
135 $ret .= "<TR>";
136 $ret .= "<TD><SELECT NAME=\"editfriend_add_1_fg\">";
137 foreach (@color) {
138     my $selected = $_->{'code'} eq $sel->{'fgcolor'} ? " SELECTED" : "";
139     $ret .= "<OPTION VALUE=\"$_->{'code'}\"$selected>$_->{'item'}\n";
140 }
141 $ret .= "</SELECT></TD>\n";
142 $ret .= "<TD><SELECT NAME=\"editfriend_add_1_bg\">";
143 foreach (@color) {
144     my $selected = $_->{'code'} eq $sel->{'bgcolor'} ? " SELECTED" : "";
145     $ret .= "<OPTION VALUE=\"$_->{'code'}\"$selected>$_->{'item'}\n";
146 }
147 $ret .= "</SELECT></TD>\n";
148 $ret .= "</TR>\n";
149 $ret .= "</TABLE>\n";
150 
151 ### color swatch
152 my $col = 0;
153 $ret .= "<P><TABLE border=0 cellspacing=0 cellpadding=0>";
154 foreach (@color) {
155     if ($col==0) { $ret .= "<TR>\n"; }
156     $col++;
157     my $ecolor = LJ::ehtml($_->{'item'});
158     $ret .= "<TD bgcolor=$_->{'code'}><IMG SRC=\"/img/dot.gif\" WIDTH=14 HEIGHT=14 TITLE=\"$ecolor\" ALT=\"$ecolor\"></TD>\n";
159     if ($col==23) { $ret .= "</TR>\n"; $col==0; }
160 }
161 if ($col) { $ret .= "</TR>\n"; $col==0; }
162 $ret .= "</TABLE>\n";
163 $ret .= "<FONT SIZE=-2 FACE=\"Arial,Helvetica\">(Hover your mouse over a color to see its name)</FONT>";
164
165 $ret .= "</CENTER>\n";
166 $body .= $ret;
167
168 $body .= "</form>";
169
170 return;
171     
172_CODE=)
173(=PAGE
174TITLE=>(=_CODE return $title; _CODE=)
175BODY=>(=_CODE return $body; _CODE=)
176PAGE=)(=_C <LJDEP>
177link: htdocs/login.bml, htdocs/create.bml, htdocs/friends/edit.bml, htdocs/users
178img: htdocs/img/dot.gif
179post: htdocs/friends/add.bml
180</LJDEP> _C=)
Note: See TracBrowser for help on using the browser.