| 1 | package Tags::HTML::ChangePassword; | ||
| 2 | |||
| 3 | 3 | ▶ | use base qw(Tags::HTML); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use Class::Utils qw(set_params split_params); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Error::Pure qw(err); | ||
| 9 | use List::Util qw(none); | ||
| 10 | use Mo::utils::CSS 0.07 qw(check_css_unit); | ||
| 11 | use Mo::utils::Language 0.05 qw(check_language_639_2); | ||
| 12 | use Readonly; | ||
| 13 | use Tags::HTML::Messages; | ||
| 14 | |||
| 15 | Readonly::Array our @FORM_METHODS => qw(post get); | ||
| 16 | Readonly::Array our @TEXT_KEYS => qw(change_password old_password_label password1_label password2_label submit); | ||
| 17 | |||
| 18 | our $VERSION = 0.08; | ||
| 19 | |||
| 20 | # Constructor. | ||
| 21 | sub new { | ||
| 22 | my ($class, @params) = @_; | ||
| 23 | |||
| 24 | # Create object. | ||
| 25 | my ($object_params_ar, $other_params_ar) = split_params( | ||
| 26 | ['css_change_password', 'form_method', 'lang', 'link', 'text', 'width'], @params); | ||
| 27 | my $self = $class->SUPER::new(@{$other_params_ar}); | ||
| 28 | |||
| 29 | # CSS style for change password box. | ||
| 30 | $self->{'css_change_password'} = 'form-change-password'; | ||
| 31 | |||
| 32 | # Form method. | ||
| 33 | $self->{'form_method'} = 'post'; | ||
| 34 | |||
| 35 | # Language. | ||
| 36 | $self->{'lang'} = 'eng'; | ||
| 37 | |||
| 38 | # Language texts. | ||
| 39 | $self->{'text'} = { | ||
| 40 | 'eng' => { | ||
| 41 | 'change_password' => 'Change password', | ||
| 42 | 'old_password_label' => 'Old password', | ||
| 43 | 'password1_label' => 'New password', | ||
| 44 | 'password2_label' => 'Confirm new password', | ||
| 45 | 'submit' => 'Save Changes', | ||
| 46 | }, | ||
| 47 | }; | ||
| 48 | |||
| 49 | # Login box width. | ||
| 50 | $self->{'width'} = '300px'; | ||
| 51 | |||
| 52 | # Process params. | ||
| 53 | set_params($self, @{$object_params_ar}); | ||
| 54 | |||
| 55 | # Check form method. | ||
| 56 | if (none { $self->{'form_method'} eq $_ } @FORM_METHODS) { | ||
| 57 | err "Parameter 'form_method' has bad value."; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Check lang. | ||
| 61 | check_language_639_2($self, 'lang'); | ||
| 62 | |||
| 63 | # Check text. | ||
| 64 | if (! defined $self->{'text'}) { | ||
| 65 | err "Parameter 'text' is required."; | ||
| 66 | } | ||
| 67 | if (ref $self->{'text'} ne 'HASH') { | ||
| 68 | err "Parameter 'text' must be a hash with language texts."; | ||
| 69 | } | ||
| 70 | if (! exists $self->{'text'}->{$self->{'lang'}}) { | ||
| 71 | err "Texts for language '$self->{'lang'}' doesn't exist."; | ||
| 72 | } | ||
| 73 | if (@TEXT_KEYS != keys %{$self->{'text'}->{$self->{'lang'}}}) { | ||
| 74 | err "Number of texts isn't same as expected."; | ||
| 75 | } | ||
| 76 | foreach my $req_text_key (@TEXT_KEYS) { | ||
| 77 | if (! exists $self->{'text'}->{$self->{'lang'}}->{$req_text_key}) { | ||
| 78 | err "Text for lang '$self->{'lang'}' and key '$req_text_key' doesn't exist."; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | check_css_unit($self, 'width'); | ||
| 83 | |||
| 84 | $self->{'_tags_messages'} = Tags::HTML::Messages->new( | ||
| 85 | 'css' => $self->{'css'}, | ||
| 86 | 'flag_no_messages' => 0, | ||
| 87 | 'tags' => $self->{'tags'}, | ||
| 88 | ); | ||
| 89 | |||
| 90 | # Object. | ||
| 91 | return $self; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _cleanup { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | delete $self->{'_messages'}; | ||
| 98 | delete $self->{'_message_types'}; | ||
| 99 | |||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _prepare { | ||
| 104 | my ($self, $message_types_hr) = @_; | ||
| 105 | |||
| 106 | if (! defined $message_types_hr) { | ||
| 107 | err 'No message types to prepare.'; | ||
| 108 | } | ||
| 109 | |||
| 110 | $self->{'_message_types'} = $message_types_hr; | ||
| 111 | |||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _init { | ||
| 116 | my ($self, $messages_ar) = @_; | ||
| 117 | |||
| 118 | if (! defined $messages_ar) { | ||
| 119 | err 'No messages to init.'; | ||
| 120 | } | ||
| 121 | |||
| 122 | $self->{'_messages'} = $messages_ar; | ||
| 123 | |||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | # Process 'Tags'. | ||
| 128 | sub _process { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | my $old_password_id = 'old_password'; | ||
| 132 | my $password1_id = 'password1'; | ||
| 133 | my $password2_id = 'password2'; | ||
| 134 | |||
| 135 | # Main content. | ||
| 136 | $self->{'tags'}->put( | ||
| 137 | ['b', 'form'], | ||
| 138 | ['a', 'class', $self->{'css_change_password'}], | ||
| 139 | ['a', 'method', $self->{'form_method'}], | ||
| 140 | |||
| 141 | ['b', 'fieldset'], | ||
| 142 | ['b', 'legend'], | ||
| 143 | ['d', $self->_text('change_password')], | ||
| 144 | ['e', 'legend'], | ||
| 145 | |||
| 146 | ['b', 'p'], | ||
| 147 | ['b', 'label'], | ||
| 148 | ['a', 'for', $old_password_id], | ||
| 149 | ['d', $self->_text('old_password_label')], | ||
| 150 | ['e', 'label'], | ||
| 151 | ['b', 'input'], | ||
| 152 | ['a', 'type', 'password'], | ||
| 153 | ['a', 'name', $old_password_id], | ||
| 154 | ['a', 'id', $old_password_id], | ||
| 155 | ['a', 'autofocus', 'autofocus'], | ||
| 156 | ['e', 'input'], | ||
| 157 | ['e', 'p'], | ||
| 158 | |||
| 159 | ['b', 'p'], | ||
| 160 | ['b', 'label'], | ||
| 161 | ['a', 'for', $password1_id], | ||
| 162 | ['d', $self->_text('password1_label')], | ||
| 163 | ['e', 'label'], | ||
| 164 | ['b', 'input'], | ||
| 165 | ['a', 'type', 'password'], | ||
| 166 | ['a', 'name', $password1_id], | ||
| 167 | ['a', 'id', $password1_id], | ||
| 168 | ['e', 'input'], | ||
| 169 | ['e', 'p'], | ||
| 170 | |||
| 171 | ['b', 'p'], | ||
| 172 | ['b', 'label'], | ||
| 173 | ['a', 'for', $password2_id], | ||
| 174 | ['d', $self->_text('password2_label')], | ||
| 175 | ['e', 'label'], | ||
| 176 | ['b', 'input'], | ||
| 177 | ['a', 'type', 'password'], | ||
| 178 | ['a', 'name', $password2_id], | ||
| 179 | ['a', 'id', $password2_id], | ||
| 180 | ['e', 'input'], | ||
| 181 | ['e', 'p'], | ||
| 182 | |||
| 183 | ['b', 'p'], | ||
| 184 | ['b', 'button'], | ||
| 185 | ['a', 'type', 'submit'], | ||
| 186 | ['a', 'name', 'change_password'], | ||
| 187 | ['a', 'value', 'change_password'], | ||
| 188 | ['d', $self->_text('submit')], | ||
| 189 | ['e', 'button'], | ||
| 190 | ['e', 'p'], | ||
| 191 | |||
| 192 | ['e', 'fieldset'], | ||
| 193 | ); | ||
| 194 | |||
| 195 | $self->{'_tags_messages'}->process($self->{'_messages'}); | ||
| 196 | |||
| 197 | $self->{'tags'}->put( | ||
| 198 | ['e', 'form'], | ||
| 199 | ); | ||
| 200 | |||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | # Process 'CSS::Struct'. | ||
| 205 | sub _process_css { | ||
| 206 | my $self = shift; | ||
| 207 | |||
| 208 | $self->{'css'}->put( | ||
| 209 | ['s', '.'.$self->{'css_change_password'}], | ||
| 210 | ['d', 'width', $self->{'width'}], | ||
| 211 | ['d', 'background-color', '#f2f2f2'], | ||
| 212 | ['d', 'padding', '20px'], | ||
| 213 | ['d', 'border-radius', '5px'], | ||
| 214 | ['d', 'box-shadow', '0 0 10px rgba(0, 0, 0, 0.2)'], | ||
| 215 | ['e'], | ||
| 216 | |||
| 217 | ['s', '.'.$self->{'css_change_password'}.' fieldset'], | ||
| 218 | ['d', 'border', 'none'], | ||
| 219 | ['d', 'padding', 0], | ||
| 220 | ['d', 'margin-bottom', '20px'], | ||
| 221 | ['e'], | ||
| 222 | |||
| 223 | ['s', '.'.$self->{'css_change_password'}.' legend'], | ||
| 224 | ['d', 'font-weight', 'bold'], | ||
| 225 | ['d', 'margin-bottom', '10px'], | ||
| 226 | ['e'], | ||
| 227 | |||
| 228 | ['s', '.'.$self->{'css_change_password'}.' p'], | ||
| 229 | ['d', 'margin', 0], | ||
| 230 | ['d', 'padding', '10px 0'], | ||
| 231 | ['e'], | ||
| 232 | |||
| 233 | ['s', '.'.$self->{'css_change_password'}.' label'], | ||
| 234 | ['d', 'display', 'block'], | ||
| 235 | ['d', 'font-weight', 'bold'], | ||
| 236 | ['d', 'margin-bottom', '5px'], | ||
| 237 | ['e'], | ||
| 238 | |||
| 239 | ['s', '.'.$self->{'css_change_password'}.' input[type="text"]'], | ||
| 240 | ['s', '.'.$self->{'css_change_password'}.' input[type="password"]'], | ||
| 241 | ['d', 'width', '100%'], | ||
| 242 | ['d', 'padding', '8px'], | ||
| 243 | ['d', 'border', '1px solid #ccc'], | ||
| 244 | ['d', 'border-radius', '3px'], | ||
| 245 | ['e'], | ||
| 246 | |||
| 247 | ['s', '.'.$self->{'css_change_password'}.' button[type="submit"]'], | ||
| 248 | ['d', 'width', '100%'], | ||
| 249 | ['d', 'padding', '10px'], | ||
| 250 | ['d', 'background-color', '#4CAF50'], | ||
| 251 | ['d', 'color', '#fff'], | ||
| 252 | ['d', 'border', 'none'], | ||
| 253 | ['d', 'border-radius', '3px'], | ||
| 254 | ['d', 'cursor', 'pointer'], | ||
| 255 | ['e'], | ||
| 256 | |||
| 257 | ['s', '.'.$self->{'css_change_password'}.' button[type="submit"]:hover'], | ||
| 258 | ['d', 'background-color', '#45a049'], | ||
| 259 | ['e'], | ||
| 260 | |||
| 261 | ['s', '.'.$self->{'css_change_password'}.' .messages'], | ||
| 262 | ['d', 'text-align', 'center'], | ||
| 263 | ['e'], | ||
| 264 | ); | ||
| 265 | |||
| 266 | $self->{'_tags_messages'}->process_css($self->{'_message_types'}); | ||
| 267 | |||
| 268 | return; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub _text { | ||
| 272 | my ($self, $key) = @_; | ||
| 273 | |||
| 274 | return $self->{'text'}->{$self->{'lang'}}->{$key}; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | 1; | ||
| 279 | |||
| 280 | __END__ | ||