source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrFormsA.pm @ 1490

Last change on this file since 1490 was 1408, checked in by nanardon, 9 years ago

Avoid undef value

File size: 9.5 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrFormsA;
2
3use strict;
4use warnings;
5use base 'LATMOS::Accounts::Bases::Attributes';
6use base 'Catalyst::Model';
7use LATMOS::Accounts::Log;
8
9=head1 NAME
10
11LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
12
13=head1 DESCRIPTION
14
15Catalyst Model.
16
17=cut
18
19sub escape {
20    my ($self, $text) = @_;
21    $text ||= '';
22    for ($text) {
23        s/&/&/g;
24        s/</&lt;/g;
25        s/>/&gt;/g;
26        s/"/&quot;/g;
27    }
28    $text;
29}
30
31sub new {
32    my ($class) = @_;
33    bless({}, $class);
34}
35
36# call either OBJ or type + base
37
38sub ACCEPT_CONTEXT {
39    my ($self, $c, $attributes, $base_or_object, $maybe_otype) = @_;
40
41    my $new = LATMOS::Accounts::Bases::Attributes->new($attributes, $base_or_object, $maybe_otype);
42    $new->{c} = $c;
43    $new->{htmlname} = $attributes;
44    $new->{htmlname} =~ s/[\.-]/_/g;
45    $new->{htmlid} = $attributes;
46    $new->{htmlid} =~ s/[\.-]/_/g;
47    $new->{valcount} = 0;
48    $new->{hconfig} = LATMOS::Accounts::Web->config->{attrs}{$attributes} || {};
49    bless($new, __PACKAGE__);
50}
51
52sub htmlname {
53    $_[0]->{htmlname};
54}
55
56sub htmlid {
57    my ($self) = @_;
58
59    if ($self->{valcount}) {
60        return sprintf('%s_%03d', $self->{htmlid}, $self->{valcount});
61    } else {
62        return $_[0]->{htmlid};
63    }
64}
65
66sub htmlnewid {
67    my ($self) = @_;
68
69    $self->{valcount}++;
70
71    return $self->htmlid;
72}
73
74sub hconfig {
75    my ($self, $val) = @_;
76    $self->{hconfig}{$val}
77}
78
79sub attr_label {
80    my ($self, $label, $hint) = @_;
81    $label ||= '';
82   
83    $hint = $self->hconfig('hint');
84
85    return sprintf('<label %sfor="%s">%s</label>',
86        ($hint ? sprintf('title="%s"', $self->escape($hint)) : ''),
87        $self->escape($self->htmlname),
88        $self->escape($self->hconfig('label') || $self->htmlname),
89    );
90}
91
92sub attr_raw_value {
93    my ($self) = @_;
94
95    my @val = grep { $_ } (exists($self->{c}->req->params->{$self->htmlname}))
96        ? ($self->{c}->req->param($self->htmlname))
97        : $self->getValues;
98
99    if (@val) {
100        return @val;
101    } else {
102        return (grep { $_ } ($self->default_value))
103    }
104}
105
106sub attr_field_name {
107    my ($self) = @_;
108
109    $self->htmlname;
110}
111
112sub attr_field {
113    my ($self, $type) = @_;
114
115    # Force Array context:
116    my @attr_raw_value = $self->attr_raw_value;
117    if (!$self->multiple) {
118        push(@attr_raw_value, undef) unless(@attr_raw_value);
119    }
120    my @htmls = ();
121
122    foreach my $attr_raw_value (@attr_raw_value) {
123        $self->htmlnewid;
124        push(@htmls, $self->attr_item($attr_raw_value));
125    }
126
127    if (my $js = $self->attr_js) {
128        push(@htmls,
129            qq[ <script>
130            \$( function() {
131            $js
132            });
133            </script>]
134        );
135    }
136
137    my $addempty = '';
138    if ($self->{multiple} && !$self->readonly) {
139
140        my $old = $self->{valcount} + 1;
141        $self->{valcount} = 999;
142        my $code = $self->attr_item('');
143        $code =~ s/'/\\'/g;
144
145        $addempty = sprintf('<div id="%s_div"></div>', $self->htmlid);
146        $addempty .= "\n";
147        $addempty .= sprintf(q|
148            <button id="%s_add" type="button" style="display:block">+</button>
149            <script type="text/javascript">
150            var cn_%s = %d;
151            $( '#%s_add' ).click(function(even, ui) {
152                $('#%s_div').append('%s'.replace(/%d/g, cn_%s)).append('<br>');
153
154                cn_%s++;
155                %s
156            });
157            </script>
158            |,
159            $self->htmlid,
160            $self->htmlid,
161            $old,
162            $self->htmlid,
163            $self->htmlid,
164            $code,
165            $self->{valcount},
166            $self->htmlid,
167            $self->htmlid,
168            $self->attr_js,
169        );
170
171        $addempty .= "\n";
172
173    }
174
175    return join("<br>\n", @htmls) . $addempty;
176}
177
178sub attr_item {
179    my ($self, $value) = @_;
180
181    my $html = '';
182
183    my $type = 'attr_' . uc($self->form_type);
184
185    if ($self->can($type)) {
186        $html .= $self->$type($value);
187    } else {
188        $html .= $self->attr_TEXT($value);
189    }
190
191    $html .= q|<span class="inputvalidate" id="|. $self->htmlid . q|_check"></span>|;
192
193    if (my $ref = $self->reference) {
194        my $uri_part = $self->_uri_part($ref);
195        if ($self->base->attribute($ref, 'displayName')) {
196            my $text = '';
197            if ($value &&
198                (my $obj = $self->base->get_object($ref, $value))) {
199                $text = $obj->get_attributes('displayName');
200            }
201            #$html .= sprintf(
202                #qq|<span id="${htmlid}_ref" style="margin-left: 1em">$text</span>|,
203                #   $self->escape($text),
204                #);
205        } elsif($value && $uri_part) {
206            $html .= sprintf(
207                '<a href="%s"><img class="attr" src="%s" title="%s"></a>',
208                $self->{c}->uri_for('/', $uri_part, $value),
209                $self->{c}->uri_for('/static', 'images', 'arrow-right.png'),
210                $value,
211            );
212        }
213    }
214
215    $html
216}
217
218sub attr_js {
219    my ($self, $value) = @_;
220
221    my $type = 'attr_' . uc($self->form_type) . '_js';
222
223    if ($self->can($type)) {
224        return $self->$type($value);
225    } else {
226        return '';
227    }
228}
229
230
231sub attr_TEXT {
232    my ($self, $value, $name, $id) = @_;
233
234    return sprintf(
235        '<input type="text" id="%s" name="%s" value="%s" size="%d">',
236        $self->escape($id   || $self->htmlid),
237        $self->escape($name || $self->htmlname),
238        $self->escape($value),
239        $self->form_option('length') || 30,
240    );
241
242}
243
244sub attr_TEXT_js {
245    my ($self) = @_;
246
247    my $html = '';
248
249    if ($self->uniq) {
250        $html .= sprintf(q|
251                $( '[name="%s"]' ).on('input', function() {
252                    var id = $(this).attr('id');
253                    $.ajax({
254                        method: "POST",
255                        url: '%s',
256                        data: { val: $(this).val() },
257                    }). done(function( msg ) {
258                        $( '#' + id + '_check' ).html (msg);
259                    });
260                    return false;
261                });
262        |,
263        $self->htmlname,
264        $self->{c}->uri_for('/ajax', 'objattrexist', $self->otype, $self->name),
265        );
266    }
267
268    $html;
269}
270
271sub attr_LABEL {
272    my ($self, $value) = @_;
273    return sprintf('%s <input id="%s" type=hidden name="%s" value="%s">',
274        $self->escape($value),
275        $self->escape($self->htmlid),
276        $self->escape($self->htmlname),
277        $self->escape($value),
278    );
279}
280
281sub attr_TEXTAREA {
282    my ($self, $value) = @_;
283    return sprintf(
284        '<textarea id="%s" name="%s" cols="40">%s</textarea>',
285        $self->escape($self->htmlid),
286        $self->escape($self->htmlname),
287        $self->escape($value || ''),
288    );
289}
290
291sub attr_DATE_js {
292    my ($self, $value) = @_;
293    return sprintf(q|
294        $('[name="%s"]').datepicker( {
295            changeMonth: true,
296            changeYear: true,
297            dateFormat: 'dd/mm/yy'
298        } );
299    |, $self->escape($self->htmlname));
300}
301
302sub attr_CHECKBOX {
303    my ($self, $value) = @_;
304    $value ||= '';
305
306    return sprintf('<input type="checkbox" id="%s" name="%s"%s>%s',
307        $self->escape($self->htmlid),
308        $self->escape($self->htmlname),
309        ($value ? '  checked="yes"' : ''),
310        ($self->form_option('rawvalue') ? " $value" : ''),
311    );
312}
313
314sub attr_LIST {
315    my ($self, $value) = @_;
316
317    my @values = $self->can_values;
318    $value ||= '';
319
320    if (@values < 10) {
321        return sprintf(q|<select id="%s" name="%s">%s%s</select>|,
322            $self->htmlid,
323            $self->htmlname,
324            ($self->mandatory ? '' : '<option value="">--</option>'),
325            join(",\n", map { $_ = sprintf('<option value="%s"%s>%s</option>', $_, ($_ eq $value ? ' selected="selected"' : ''), $self->display($_)) } sort $self->can_values),
326        );
327    } else {
328        my $html = $self->attr_TEXT($self->display($value));
329    }
330}
331
332sub attr_LIST_js {
333    my ($self) = @_;
334    return sprintf(q|
335            $('[name="%s"]').autocomplete({
336                delay: 200,
337                minLength: 0,
338                source: '%s',
339            })
340            .autocomplete( "instance" )._renderItem = function( ul, item ) {
341                return $( "<li>" )
342                .append( "<a>" + item.label + " (" + item.value +  ")</a>" )
343                .appendTo( ul );
344            };
345        |,
346        $self->htmlname,
347        $self->{c}->uri_for('/json/refval', $self->otype, $self->name),
348    );
349}
350
351sub _uri_part {
352    my ($self, $ref) = @_;
353    my $uri_part = {
354        user => 'users',
355        group => 'groups',
356        nethost => 'nethosts',
357        netzone => 'netzones',
358        site => 'sites',
359        aliasess => 'aliases',
360    }->{$ref};
361}
362
363sub _attr_js_item {
364    my ($self, $value, $htmlid) = @_;
365
366    $htmlid = $self->htmlid;
367    my (@js);
368
369    if (my $ref = $self->reference) {
370        my $uri_part = $self->_uri_part($ref);
371        if ($self->base->attribute($ref, 'displayName')) {
372            push(@js, 
373                $self->{c}->prototype->observe_field( $htmlid,
374                    {
375                        update => "${htmlid}_ref",
376                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
377                        frequency => 1,
378                        with   => "'attr=displayName&id=' + element.options[element.selectedIndex].text",
379                    }
380                )
381            ) if ($self->form_type =~ /list/i);
382        }
383    }
384
385    return(join("\n", @js));
386}
387
388
389=head1 AUTHOR
390
391Thauvin Olivier
392
393=head1 LICENSE
394
395This library is free software, you can redistribute it and/or modify
396it under the same terms as Perl itself.
397
398=cut
399
4001;
Note: See TracBrowser for help on using the repository browser.