LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / Setting / Birthday.pm
blob3e7df9204a0b247ea811f467dd788185d477099b
1 package LJ::Setting::Birthday;
2 use base 'LJ::Setting';
3 use strict;
4 use warnings;
6 use LJ::TimeUtil;
8 sub tags { qw(birthday birthdate dob) }
10 sub as_html {
11 my ($class, $u, $errs, $args) = @_;
12 my $key = $class->pkgkey;
13 my $ret;
15 $ret .= "<label for='${key}month'>" . $class->ml('.setting.birthday.question') . "</label>";
16 my %bdpart;
17 if ($u->{bdate} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/) {
18 ($bdpart{year}, $bdpart{month}, $bdpart{day}) = ($1, $2, $3);
19 if ($bdpart{year} eq "0000") { $bdpart{year} = ""; }
20 if ($bdpart{day} eq "00") { $bdpart{day} = ""; }
22 $ret .= LJ::html_select({ 'name' => "${key}month", 'id' => "${key}month", 'class' => "select", 'selected' => int($bdpart{month}) },
23 '', '', map { $_, LJ::Lang::ml(LJ::Lang::month_long_langcode($_)) } (1..12)) . " ";
25 $ret .= LJ::html_text({ 'name' => "${key}day", 'value' => $bdpart{day}, 'class' => 'text', 'size' => '3', 'maxlength' => '2' }) . " ";
26 $ret .= LJ::html_text({ 'name' => "${key}year", 'value' => $bdpart{year}, 'class' => 'text', 'size' => '5', 'maxlength' => '4' });
28 $ret .= $class->errdiv($errs, "month");
29 $ret .= $class->errdiv($errs, "day");
30 $ret .= $class->errdiv($errs, "year");
32 return $ret;
35 sub error_check {
36 my ($class, $u, $args) = @_;
37 my $month = $class->get_arg($args, "month") || 0;
38 my $day = $class->get_arg($args, "day") || 0;
39 my $year = $class->get_arg($args, "year") || 0;
40 my $this_year = (localtime())[5]+1900;
41 my $err_count = 0;
42 local $BML::ML_SCOPE = "/manage/profile/index.bml";
44 if ($year && $year < 100) {
45 $class->errors("year" => LJ::Lang::ml('.error.year.notenoughdigits'));
46 $err_count++;
49 if ($year && $year >= 100 && ($year < 1890 || $year > $this_year)) {
50 $class->errors("year" => LJ::Lang::ml('.error.year.outofrange'));
51 $err_count++;
54 if ($month && ($month < 1 || $month > 12)) {
55 $class->errors("month" => LJ::Lang::ml('.error.month.outofrange'));
56 $err_count++;
59 if ($day && ($day < 1 || $day > 31)) {
60 $class->errors("day" => LJ::Lang::ml('.error.day.outofrange'));
61 $err_count++;
64 if ($err_count == 0 && $day > LJ::TimeUtil->days_in_month($month, $year)) {
65 $class->errors("day" => LJ::Lang::ml('.error.day.notinmonth'));
68 return 1;
71 sub save {
72 my ($class, $u, $args) = @_;
73 $class->error_check($u, $args);
75 my $month = $class->get_arg($args, "month") || 0;
76 my $day = $class->get_arg($args, "day") || 0;
77 my $year = $class->get_arg($args, "year") || 0;
79 my %update = (
80 'bdate' => sprintf("%04d-%02d-%02d", $year, $month, $day),
82 LJ::update_user($u, \%update);
84 # for the directory
85 my $sidx_bday = sprintf("%02d-%02d", $month, $day);
86 $sidx_bday = "" if !$sidx_bday || $sidx_bday =~ /00/;
87 $u->set_prop('sidx_bday', $sidx_bday);
88 $u->invalidate_directory_record;
89 $u->set_next_birthday;