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

Last change on this file since 1365 was 1348, checked in by nanardon, 9 years ago

Add endcircuit attribute

File size: 9.3 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    my @val = grep { $_ } 
95        ($self->{c}->req->param($self->htmlname))
96        || $self->getValues;
97
98    if (@val) {
99        return @val;
100    } else {
101        return (grep { $_ } ($self->default_value))
102    }
103}
104
105sub attr_field_name {
106    my ($self) = @_;
107
108    $self->htmlname;
109}
110
111sub attr_field {
112    my ($self, $type) = @_;
113
114    # Force Array context:
115    my @attr_raw_value = $self->getValues;
116    my @htmls = ();
117
118    foreach my $attr_raw_value (@attr_raw_value) {
119        $self->htmlnewid;
120        push(@htmls, $self->attr_item($attr_raw_value));
121    }
122
123    if (my $js = $self->attr_js) {
124        push(@htmls,
125            qq[ <script>
126            \$( function() {
127            $js
128            });
129            </script>]
130        );
131    }
132
133    if ($self->{multiple} && !$self->readonly) {
134
135        my $old = $self->{valcount} + 1;
136        $self->{valcount} = 999;
137        my $code = $self->attr_item('');
138        $code =~ s/'/\\'/g;
139
140        my $addempty = sprintf('<div id="%s_div"></div>', $self->htmlid);
141        $addempty .= "\n";
142        $addempty .= sprintf(q|
143            <button id="%s_add" type="button" style="display:block">+</button>
144            <script type="text/javascript">
145            var cn_%s = %d;
146            $( '#%s_add' ).click(function(even, ui) {
147                $('#%s_div').append('%s'.replace(/%d/g, cn_%s)).append('<br>');
148
149                cn_%s++;
150                %s
151            });
152            </script>
153            |,
154            $self->htmlid,
155            $self->htmlid,
156            $old,
157            $self->htmlid,
158            $self->htmlid,
159            $code,
160            $self->{valcount},
161            $self->htmlid,
162            $self->htmlid,
163            $self->attr_js,
164        );
165
166        $addempty .= "\n";
167        push( @htmls, $addempty);
168
169    }
170
171    return join("<br>\n", @htmls);
172}
173
174sub attr_item {
175    my ($self, $value) = @_;
176
177    my $html = '';
178
179    my $type = 'attr_' . uc($self->form_type);
180
181    if ($self->can($type)) {
182        $html .= $self->$type($value);
183    } else {
184        $html .= $self->attr_TEXT($value);
185    }
186
187    $html .= q|<span class="inputvalidate" id="|. $self->htmlid . q|_check"></span>|;
188
189    if (my $ref = $self->reference) {
190        my $uri_part = $self->_uri_part($ref);
191        if ($self->base->attribute($ref, 'displayName')) {
192            my $text = '';
193            if ($value &&
194                (my $obj = $self->base->get_object($ref, $value))) {
195                $text = $obj->get_attributes('displayName');
196            }
197            #$html .= sprintf(
198                #qq|<span id="${htmlid}_ref" style="margin-left: 1em">$text</span>|,
199                #   $self->escape($text),
200                #);
201        } elsif($value && $uri_part) {
202            $html .= sprintf(
203                '<a href="%s"><img class="attr" src="%s" title="%s"></a>',
204                $self->{c}->uri_for('/', $uri_part, $value),
205                $self->{c}->uri_for('/static', 'images', 'arrow-right.png'),
206                $value,
207            );
208        }
209    }
210
211    $html
212}
213
214sub attr_js {
215    my ($self, $value) = @_;
216
217    my $type = 'attr_' . uc($self->form_type) . '_js';
218
219    if ($self->can($type)) {
220        return $self->$type($value);
221    } else {
222        return '';
223    }
224}
225
226
227sub attr_TEXT {
228    my ($self, $value, $name, $id) = @_;
229
230    return sprintf(
231        '<input type="text" id="%s" name="%s" value="%s" size="%d">',
232        $self->escape($id   || $self->htmlid),
233        $self->escape($name || $self->htmlname),
234        $self->escape($value),
235        $self->form_option('length') || 30,
236    );
237
238}
239
240sub attr_TEXT_js {
241    my ($self) = @_;
242
243    my $html = '';
244
245    if ($self->uniq) {
246        $html .= sprintf(q|
247                $( '[name="%s"]' ).on('input', function() {
248                    var id = $(this).attr('id');
249                    $.ajax({
250                        method: "POST",
251                        url: '%s',
252                        data: { val: $(this).val() },
253                    }). done(function( msg ) {
254                        $( '#' + id + '_check' ).html (msg);
255                    });
256                    return false;
257                });
258        |,
259        $self->htmlname,
260        $self->{c}->uri_for('/ajax', 'objattrexist', $self->otype, $self->name),
261        );
262    }
263
264    $html;
265}
266
267sub attr_LABEL {
268    my ($self, $value) = @_;
269    return sprintf('%s <input id="%s" type=hidden name="%s" value="%s">',
270        $self->escape($value),
271        $self->escape($self->htmlid),
272        $self->escape($self->htmlname),
273        $self->escape($value),
274    );
275}
276
277sub attr_TEXTAREA {
278    my ($self, $value) = @_;
279    return sprintf(
280        '<textarea id="%s" name="%s" cols="40">%s</textarea>',
281        $self->escape($self->htmlid),
282        $self->escape($self->htmlname),
283        $self->escape($value || ''),
284    );
285}
286
287sub attr_DATE_js {
288    my ($self, $value) = @_;
289    return sprintf(q|
290        $('[name="%s"]').datepicker( {
291            changeMonth: true,
292            changeYear: true,
293            dateFormat: 'dd/mm/yy'
294        } );
295    |, $self->escape($self->htmlname));
296}
297
298sub attr_CHECKBOX {
299    my ($self, $value) = @_;
300    $value ||= '';
301
302    return sprintf('<input type="checkbox" id="%s" name="%s"%s>%s',
303        $self->escape($self->htmlid),
304        $self->escape($self->htmlname),
305        ($value ? '  checked="yes"' : ''),
306        ($self->form_option('rawvalue') ? " $value" : ''),
307    );
308}
309
310sub attr_LIST {
311    my ($self, $value) = @_;
312
313    my @values = $self->can_values;
314
315    if (@values < 10) {
316        return sprintf(q|<select id="%s" name="%s">%s%s</select>|,
317            $self->htmlid,
318            $self->htmlname,
319            ($self->mandatory ? '' : '<option value="">--</option>'),
320            join(",\n", map { $_ = sprintf('<option value="%s"%s>%s</option>', $_, ($_ eq $value ? ' selected="selected"' : ''), $self->display($_)) } sort $self->can_values),
321        );
322    } else {
323        my $html = $self->attr_TEXT($self->display($value));
324    }
325}
326
327sub attr_LIST_js {
328    my ($self) = @_;
329    return sprintf(q|
330            $('[name="%s"]').autocomplete({
331                delay: 200,
332                minLength: 0,
333                source: '%s',
334            })
335            .autocomplete( "instance" )._renderItem = function( ul, item ) {
336                return $( "<li>" )
337                .append( "<a>" + item.label + " (" + item.value +  ")</a>" )
338                .appendTo( ul );
339            };
340        |,
341        $self->htmlname,
342        $self->{c}->uri_for('/json/refval', $self->otype, $self->name),
343    );
344}
345
346sub _uri_part {
347    my ($self, $ref) = @_;
348    my $uri_part = {
349        user => 'users',
350        group => 'groups',
351        nethost => 'nethosts',
352        netzone => 'netzones',
353        site => 'sites',
354        aliasess => 'aliases',
355    }->{$ref};
356}
357
358sub _attr_js_item {
359    my ($self, $value, $htmlid) = @_;
360
361    $htmlid = $self->htmlid;
362    my (@js);
363
364    if (my $ref = $self->reference) {
365        my $uri_part = $self->_uri_part($ref);
366        if ($self->base->attribute($ref, 'displayName')) {
367            push(@js, 
368                $self->{c}->prototype->observe_field( $htmlid,
369                    {
370                        update => "${htmlid}_ref",
371                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
372                        frequency => 1,
373                        with   => "'attr=displayName&id=' + element.options[element.selectedIndex].text",
374                    }
375                )
376            ) if ($self->form_type =~ /list/i);
377        }
378    }
379
380    return(join("\n", @js));
381}
382
383
384=head1 AUTHOR
385
386Thauvin Olivier
387
388=head1 LICENSE
389
390This library is free software, you can redistribute it and/or modify
391it under the same terms as Perl itself.
392
393=cut
394
3951;
Note: See TracBrowser for help on using the repository browser.