]> git.decadent.org.uk Git - maypole.git/blob - ex/fancy_example/templates/custom/display_inputs
tested and it seems to work
[maypole.git] / ex / fancy_example / templates / custom / display_inputs
1 [%# 
2
3 =head1 display_inputs
4
5 This *RECURSIVELY* displays inputs for a hash of html elements
6
7 Vars it needs: 
8    classmetadata-- the hash of bunch of data:
9    cgi -- inputs keyed on column names
10    table -- table inputs are for
11    columns -- list  in order want displayed inputs
12    colnames -- hash of what to label inputs
13    
14 errors           -- hash of errors keyed on columns 
15
16
17 TODO -- make it recognize a general submit button for redisplaying
18 values on errors
19
20 =cut
21
22 #
23 %]
24
25 [% # some variables
26    foreign    = []; 
27    names      = [];
28    # get hash of related classes keyed on accessor for Foreign Inputs
29    USE this   = Class(classmetadata.name); 
30    tbl = classmetadata.table;
31    required = { }; 
32    FOR c IN request.config.$tbl.required_cols;
33          required.$c = 1;
34    END;
35
36 %]      
37
38 [% 
39 SET heading_shown = 0; 
40 FOR col = classmetadata.columns;  
41         NEXT IF !classmetadata.cgi.$col;
42         NEXT IF col == "id" OR col == classmetadata.table _ "_id";
43         # Display foreign inputs last 
44         IF (mykeys = classmetadata.cgi.$col.keys); 
45                         foreign.push(col);
46                         names.push(classmetadata.colnames.$col);
47                         NEXT;
48         END;
49     IF ! heading_shown; 
50         heading = classmetadata.moniker | ucfirst; 
51                 "<h4> $heading </h4>";
52         SET heading_shown = 1; 
53     END;
54 %]
55
56 [%  # Base case starts here 
57
58         SET elem = classmetadata.cgi.$col; #.clone; # not sure why clone
59         IF elem.type == 'hidden'; 
60                 elem.as_XML;
61                 NEXT;
62         ELSIF  cgi_params;
63                 param_col = col_prefix _ col;
64                 IF elem.tag == "textarea";
65                         elem = elem.push_content(cgi_params.$param_col);
66                 ELSIF elem.tag == "select";
67                         oldval = set_selected(elem, cgi_params.$col);
68                 ELSE;
69                         oldval = elem.attr("value", cgi_params.$param_col);
70                 END;
71         END;
72 %]
73
74         <label>
75                 [% indicator = '';
76            SET indicator = '*' IF (required.$col); 
77         %]
78                 <span class="field">
79                     [% indicator _ classmetadata.colnames.$col || 
80                         col | replace('_',' ') | ucfirst  %] 
81                 </span>
82                 [% elem.as_XML; %]
83         </label>
84
85         [% IF errors.$col %]
86                 <span class="error">[% errors.$col | html  %]</span>
87         [% END %]
88 [% END; %]
89
90 <!-- Display the differnt component inputs --> 
91
92 [%      USE this = Class(classmetadata.name); 
93         FOR col IN foreign; 
94                 # has_many mapping throws a stick in our spokes because related_class returns the mapped 
95                 # class. Sometimes we just want the has_many class. 
96
97         # In case of Pub Handpumps maps to Beer and we want to add Handpump to Pub, we dont 
98                 # want the mapped data .
99         # In case of "Create New Handpump" -- we want the mapped data probably so we get
100         # Beer inputs and Pub select box.
101
102                 fclass_rel_meta = this.related_meta(request, col);
103         fclass = fclass_rel_meta.foreign_class; # ignor args.mapping
104                 fclass_meta = this.get_classmetadata(fclass);
105                 fclass_meta.cgi = classmetadata.cgi.$col;
106  #       USE Dumper; Dumper.dump(fclass_meta);
107                 INCLUDE display_inputs
108                         col_prefix = col _ "__AF__" _ col_prefix
109                         errors = errors.$col
110                         heading = names.shift
111                         classmetadata = fclass_meta; # localize 
112         END;
113 %]
114