Board.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary Board/Blank.pm »
1 package Tags::HTML::Message::Board;
2  
3 4 ▶use base qw(Tags::HTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶use Class::Utils 0.06 qw(set_params split_params);
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Data::HTML::Element::Button;
9 use Data::HTML::Element::Textarea;
10 use Error::Pure qw(err);
11 use Mo::utils 0.06 qw(check_bool check_required);
12 use Mo::utils::CSS 0.02 qw(check_css_class);
13 use Mo::utils::Language 0.05 qw(check_language_639_2);
14 use Readonly;
15 use Scalar::Util qw(blessed);
16 use Tags::HTML::Element::Button;
17 use Tags::HTML::Element::Textarea;
18  
19 Readonly::Array our @TEXT_KEYS => qw(add_comment author date save);
20 Readonly::Scalar our $CSS_CLASS_ADD_COMMENT => 'add-comment';
21  
22 our $VERSION = 0.05;
23  
24 # Constructor.
25 sub new {
26 my ($class, @params) = @_;
27  
28 # Create object.
29 my ($object_params_ar, $other_params_ar) = split_params(
30 ['css_class', 'lang', 'mode_comment_form', 'text'], @params);
31 my $self = $class->SUPER::new(@{$other_params_ar});
32  
33 # CSS class.
34 $self->{'css_class'} = 'message-board';
35  
36 # Language.
37 $self->{'lang'} = 'eng';
38  
39 # Mode for comment form.
40 $self->{'mode_comment_form'} = 1;
41  
42 # Language texts.
43 $self->{'text'} = {
44 'eng' => {
45 'add_comment' => 'Add comment',
46 'author' => 'Author',
47 'date' => 'Date',
48 'save' => 'Save',
49 },
50 };
51  
52 # Process params.
53 set_params($self, @{$object_params_ar});
54  
55 # Check 'css_class'.
56 check_required($self, 'css_class');
57 check_css_class($self, 'css_class');
58  
59 # Check lang.
60 check_language_639_2($self, 'lang');
61  
62 # Check 'mode_comment_form'.
63 check_required($self, 'mode_comment_form');
64 check_bool($self, 'mode_comment_form');
65  
66 # Check text.
67 if (! defined $self->{'text'}) {
68 err "Parameter 'text' is required.";
69 }
70 if (ref $self->{'text'} ne 'HASH') {
71 err "Parameter 'text' must be a hash with language texts.";
72 }
73 if (! exists $self->{'text'}->{$self->{'lang'}}) {
74 err "Texts for language '$self->{'lang'}' doesn't exist.";
75 }
76 if (@TEXT_KEYS != keys %{$self->{'text'}->{$self->{'lang'}}}) {
77 err "Number of texts isn't same as expected.";
78 }
79 foreach my $req_text_key (@TEXT_KEYS) {
80 if (! exists $self->{'text'}->{$self->{'lang'}}->{$req_text_key}) {
81 err "Text for lang '$self->{'lang'}' and key '$req_text_key' doesn't exist.";
82 }
83 }
84  
85 my %c = (
86 'css' => $self->{'css'},
87 'tags' => $self->{'tags'},
88 );
89 $self->{'_tags_textarea'} = Tags::HTML::Element::Textarea->new(%c);
90 my $data_textarea = Data::HTML::Element::Textarea->new(
91 'autofocus' => 1,
92 'id' => 'message_board_comment_message',
93 'name' => 'message_board_comment_message',
94 'rows' => 6,
95 );
96 $self->{'_tags_textarea'}->init($data_textarea);
97  
98 $self->{'_tags_button'} = Tags::HTML::Element::Button->new(%c);
99 my $data_button = Data::HTML::Element::Button->new(
100 'data' => [
101 $self->_text('save'),
102 ],
103 'name' => 'action',
104 'type' => 'submit',
105 'value' => 'add_message_board_comment',
106 );
107 $self->{'_tags_button'}->init($data_button);
108  
109 # Object.
110 return $self;
111 }
112  
113 sub _cleanup {
114 my $self = shift;
115  
116 delete $self->{'_board'};
117  
118 return;
119 }
120  
121 sub _init {
122 my ($self, $board) = @_;
123  
124 if (! defined $board
125 || ! blessed($board)
126 || ! $board->isa('Data::Message::Board')) {
127  
128 err "Data object must be a 'Data::Message::Board' instance.";
129 }
130  
131 $self->{'_board'} = $board;
132  
133 return;
134 }
135  
136 sub _process {
137 my $self = shift;
138  
139 if (! exists $self->{'_board'}) {
140 return;
141 }
142  
143 $self->{'tags'}->put(
144 ['b', 'div'],
145 ['a', 'class', $self->{'css_class'}],
146 );
147 $self->_tags_message($self->{'_board'}, 'main-message');
148  
149 # Comments.
150 $self->{'tags'}->put(
151 ['b', 'div'],
152 ['a', 'class', 'comments'],
153 );
154 foreach my $comment (@{$self->{'_board'}->comments}) {
155 $self->_tags_message($comment, 'comment');
156 }
157 $self->{'tags'}->put(
158 ['e', 'div'],
159 );
160  
161 if ($self->{'mode_comment_form'}) {
162 $self->{'tags'}->put(
163 ['b', 'div'],
164 ['a', 'class', $CSS_CLASS_ADD_COMMENT],
165 ['b', 'div'],
166 ['a', 'class', 'title'],
167 ['d', $self->_text('add_comment')],
168 ['e', 'div'],
169 ['b', 'form'],
170 ['a', 'method', 'post'],
171 );
172 $self->{'_tags_textarea'}->process;
173 $self->{'_tags_button'}->process;
174 $self->{'tags'}->put(
175 ['e', 'form'],
176 ['e', 'div'],
177 );
178 }
179  
180 $self->{'tags'}->put(
181 ['e', 'div'],
182 );
183  
184 return;
185 }
186  
187 sub _process_css {
188 my $self = shift;
189  
190 $self->{'css'}->put(
191 ['s', '.'.$self->{'css_class'}],
192 ['d', 'font-family', 'Arial, Helvetica, sans-serif'],
193 ['d', 'margin', '1em'],
194 ['e'],
195  
196 ['s', '.'.$self->{'css_class'}.' .main-message'],
197 ['d', 'border', '1px solid #ccc'],
198 ['d', 'padding', '20px'],
199 ['d', 'border-radius', '5px'],
200 ['d', 'background-color', '#f9f9f9'],
201 ['d', 'max-width', '600px'],
202 ['d', 'margin', 'auto'],
203 ['e'],
204  
205 ['s', '.'.$self->{'css_class'}.' .comments'],
206 ['d', 'max-width', '600px'],
207 ['d', 'margin', 'auto'],
208 ['e'],
209  
210 ['s', '.'.$self->{'css_class'}.' .comment'],
211 ['d', 'border-left', '2px solid #ccc'],
212 ['d', 'padding-left', '10px'],
213 ['d', 'margin-top', '20px'],
214 ['d', 'margin-left', '10px'],
215 ['e'],
216  
217 ['s', '.'.$self->{'css_class'}.' .author'],
218 ['d', 'font-weight', 'bold'],
219 ['d', 'font-size', '1.2em'],
220 ['e'],
221  
222 ['s', '.'.$self->{'css_class'}.' .comment .author'],
223 ['d', 'font-size', '1em'],
224 ['e'],
225  
226 ['s', '.'.$self->{'css_class'}.' .date'],
227 ['d', 'color', '#555'],
228 ['d', 'font-size', '0.9em'],
229 ['d', 'margin-bottom', '10px'],
230 ['e'],
231  
232 ['s', '.'.$self->{'css_class'}.' .comment .date'],
233 ['d', 'font-size', '0.8em'],
234 ['e'],
235  
236 ['s', '.'.$self->{'css_class'}.' .text'],
237 ['d', 'margin-top', '10px'],
238 ['d', 'white-space', 'pre-wrap'],
239 ['e'],
240 );
241 if ($self->{'mode_comment_form'}) {
242 $self->{'_tags_textarea'}->process_css;
243 $self->{'_tags_button'}->process_css;
244 $self->{'css'}->put(
245 ['s', '.'.$self->{'css_class'}.' .'.$CSS_CLASS_ADD_COMMENT],
246 ['d', 'max-width', '600px'],
247 ['d', 'margin', 'auto'],
248 ['e'],
249  
250 ['s', '.'.$self->{'css_class'}.' .'.$CSS_CLASS_ADD_COMMENT.' .title'],
251 ['d', 'margin-top', '20px'],
252 ['d', 'font-weight', 'bold'],
253 ['d', 'font-size', '1.2em'],
254 ['e'],
255  
256 # Rewrite default Tags::HTML::Element::Button CSS.
257 ['s', 'button'],
258 ['d', 'margin', 0],
259 ['e'],
260 );
261 }
262  
263 return;
264 }
265  
266 sub _tags_message {
267 my ($self, $obj, $class) = @_;
268  
269 $self->{'tags'}->put(
270 ['b', 'div'],
271 ['a', 'class', $class],
272  
273 ['b', 'div'],
274 ['a', 'class', 'author'],
275 ['d', $self->_text('author').': '.$obj->author->name],
276 ['e', 'div'],
277  
278 ['b', 'div'],
279 ['a', 'class', 'date'],
280 ['d', $self->_text('date').': '.$obj->date->dmy('.').' '.$obj->date->hms],
281 ['e', 'div'],
282  
283 ['b', 'pre'],
284 ['a', 'class', 'text'],
285 ['d', $obj->message],
286 ['e', 'pre'],
287  
288 ['e', 'div'],
289 );
290  
291 return;
292 }
293  
294 sub _text {
295 my ($self, $key) = @_;
296  
297 return $self->{'text'}->{$self->{'lang'}}->{$key};
298 }
299  
300 1;
301  
302 __END__
↑ Summary Board/Blank.pm »