use strict; use warnings;

package NIML;

use base 'Markup';

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Vars;
use Database;

# config
our $root ||= "niml";
our $command_prefix ||= "C_";

# the template can specify:

# - src attributes - for server-side-include-like behaviour
# - name attributes - bind content and inputs to a data hash
# - datatype - table name / perl class, or field type
# - uitype - perl class - a subclass of NIML - to implement (part of) the user
#   interface
#   - for starters, let's just have one uitype attribute, on the <html> tag
#   - or else, could select the class based on the pathname of the template
#     i.e. edit/getaway.html -> NIML::edit::getaway
#     - but, may want different pages to use same class
# - I want to be able to specify other "parameters" to the perl class in the
#   template, too, e.g. a url for the "next" page

# - what about "sessions", "session data", do we want to pass around session
#   data in the URL?  in hidden form fields?  or keep it on server for ~24hours?
#   - Chris wants to be able to bookmark pages, we will need to put session
#     data in the URLs, and put it in <form action=""> attributes, and add it
#     to some links.
#     - will have to NOT add variables to the action which has been put in an
#       <input> tag, else it will be submitted twice! but do add it to links.
#       phew!
#     - use abbreviations for variable names, like fastmail does?
#   - in many ways, it would be better to keep session data on the server.
#     - especially if there were lots of it.
#   - if you want to be able to bookmark, e.g. a search-results page, you would
#     have to use the "GET" method, can't bookmark the response to a POST.
#     only way to get around that would be to redirect to another URL after a POST.
#     That may be necessary anyway.  HTTP needs a "you have been automatically
#     redirected to ... please update your URL" response header.

sub cgi {
	my ($class) = @_;
	my $path_info = $ENV{PATH_INFO}; $path_info =~ s,^/+,,;
	my $file = "$ENV{DOCUMENT_ROOT}/$root/$path_info";
	if ($file =~ m,(/|^)\.\.(/|$),
		|| $file eq "" || ! -f $file || ! -r $file) {
		die "file not found";
	}

	my $self = $class->parse_file($file);

	# Bless $self into a class based on the template name, if there is one...
	# XXX or should we use a class-name specified in the file?
	my $class = "NIML::$path_info"; for ($class) { s,/,::,g; s,\.[^.]*$,,; }

	if (eval qq{ use $class; }) { bless $self, $class; }

	my $cgi_query = CGI->new;
	my $data = cgi_vars($cgi_query);

	if ($data->{command}) {
		# We use the "value" of the submit to call a "command" method.
		# We add "C_" before the method name to mask CGI running arbitrary methods.

		my $command = $command_prefix . $data->{submit};
		my $method = $self->can($command);
	}

	print "Content-Type: text/html\n\n";

	print "$class, $self";
	#print $self->html;
}

# sub class {
# 	my ($self) = @_;
# 	# scan $self->{elements} for class to use
# }

#TODO make sure every <input type="submit"> is given the name "command"

#TODO form processing, 

1
