1# Copyright 2002-2007 Interchange Development Group and others
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.  See the LICENSE file for details.
7#
8# $Id: row_edit.coretag,v 1.12 2007-03-30 23:40:54 pajamian Exp $
9
10UserTag row-edit Order       key table size columns
11UserTag row-edit HasEndTag
12UserTag row-edit addAttr
13UserTag row-edit Interpolate 1
14UserTag row-edit Version     $Revision: 1.12 $
15UserTag row-edit Routine     <<EOR
16sub {
17	my ($key,$table,$size,$columns,$opt) = @_;
18	use vars qw/$CGI $Values $Variable/;
19#::logDebug("row_edit options=" . ::uneval($opt));
20	$table = $table || $CGI::values{mv_data_table} || return "BLANK DB";
21	my $db = ::database_exists_ref($table);
22	my $mtab = $::Variable->{UI_META_TABLE} || 'mv_metadata';
23	my $mdb = ::database_exists_ref($mtab);
24	$opt->{view} ||= $CGI->{ui_meta_view};
25
26	my $view = Vend::Table::Editor::meta_record($table, $opt->{view}) || {};
27
28	my $tm_extra = '';
29	my $ta_extra = '';
30	my $tf_extra = '';
31	if($opt->{extra}) {
32		$tf_extra = " $opt->{extra}";
33	}
34	if($opt->{meta_extra}) {
35		$tm_extra .= " $opt->{meta_extra}";
36	}
37	if($opt->{textarea_extra}) {
38		$tm_extra .= " $opt->{textarea_extra}";
39	}
40
41	$ta_extra ||= $tf_extra;
42	$tm_extra ||= $tf_extra;
43
44	my $prependor = '';
45	if($opt->{pointer}) {
46		$prependor = $opt->{pointer};
47		$prependor =~ s/\D+//;
48		$prependor = $prependor ? $prependor . '_' : '';
49#::logDebug("setting prependor to $prependor");
50	}
51
52	my $appendor = '';
53	if($opt->{stacker}) {
54		$appendor = "__$opt->{stacker}";
55#::logDebug("setting appendor to $appendor");
56	}
57	return errmsg("non-existent table '%s' for row-edit", $table)
58		unless $db;
59	$db = $db->ref();
60
61	my $acl = UI::Primitive::get_ui_table_acl();
62
63	my $record;
64	my $bad;
65	if ($key) {
66		eval {
67			$bad = ! $db->record_exists($key);
68			$bad = errmsg('DELETED') if $bad;
69		};
70		$bad = errmsg('ERROR') if $@;
71		if($bad) {
72			# Do nothing, we are already bad
73		}
74		elsif($acl) {
75			$bad = errmsg('Not available')
76				if ! UI::Primitive::ui_acl_atom($acl, 'keys', $key);
77		}
78		else {
79			$record = $db->row_hash($key);
80		}
81	}
82
83	$record ||= {};
84
85	my @cols;
86
87	if($columns ||= $view->{spread_cols} || $view->{attribute}) {
88		@cols = split /[\s,\0]+/, $columns;
89		my %col;
90		for($db->columns()) {
91			$col{$_} = 1;
92		}
93		@cols = grep defined $col{$_}, @cols;
94	}
95	else {
96		@cols = $db->columns();
97	}
98
99	if($acl) {
100		@cols = UI::Primitive::ui_acl_grep( $acl, 'fields', @cols);
101	}
102
103	# See if we have a textarea reference
104	my %ta;
105	if($opt->{textarea}) {
106		my @tmp = split /[\s,\0]+/, $opt->{textarea};
107		for(@tmp) {
108			$ta{$_} = 1;
109		}
110	}
111
112	my $out = '';
113
114	my $meta   = $CGI->{ui_no_meta_display} ? '' : $view->{spread_meta};
115	my %do_ta;
116	my %do_meta;
117	if($meta) {
118		my @metas = grep /\S/, split /[\0,\s]+/, $meta;
119		@do_meta{@metas} = @metas;
120	}
121
122	if($view->{spread_textarea}) {
123		my @tas = grep /\S/, split /[\0,\s]+/, $view->{spread_textarea};
124		@do_ta{@tas} = @tas;
125	}
126	my $tmp;
127
128	$size = $size || $view->{spread_width} || $view->{width} || 12;
129	if($bad) {
130		for(@cols) {
131			$out .= "<TD$tf_extra>$bad</TD>";
132		}
133	}
134	elsif($key or $opt->{blank}) {
135		for(@cols) {
136			my $text = $opt->{blank} ? '' : $record->{$_} || '';
137			my $msg = '';
138			if($do_meta{$_}) {
139				my $tmp = Vend::Tags->display( {
140											table => $table,
141											column => $_,
142											name => "$prependor$_$appendor",
143											value => $text,
144											template => ' $WIDGET$ ',
145											specific => $opt->{ui_meta_specific},
146											key => $key,
147										});
148				$out .= "<TD$tm_extra>$tmp</TD>";
149				next;
150			}
151			elsif($do_ta{$_}) {
152				my $rows = $opt->{height} || 4;
153				HTML::Entities::encode($text, $ESCAPE_CHARS::std);
154				$out .= <<EOF;
155<TD$ta_extra><TEXTAREA NAME="$prependor$_$appendor" COLS="$size" ROWS="$rows">$text</TEXTAREA>$msg</TD>
156EOF
157			}
158			else {
159				$text =~ s/"/&quot;/g;
160				$out .= <<EOF;
161<TD$tf_extra><INPUT NAME="$prependor$_$appendor" SIZE=$size VALUE="$text">$msg</TD>
162EOF
163			}
164		}
165	}
166	else {
167		for(@cols) {
168				$out .= <<EOF;
169<TH ALIGN=left>$_</TH>
170EOF
171		}
172	}
173	return $out;
174
175}
176EOR
177