42cd712001-09-03Martin Nilsson // This is a roxen module. Copyright © 1997 - 2001, Roxen IS.
0e834c2000-02-24Martin Nilsson // Released under GPL // made by Per Hedbor
84af181997-11-14Per Hedbor 
42cd712001-09-03Martin Nilsson constant cvs_version = "$Id: wizard_tag.pike,v 1.30 2001/09/03 18:52:22 nilsson Exp $";
84af181997-11-14Per Hedbor constant thread_safe=1; #include <module.h> inherit "module"; inherit "wizard";
b3281f2000-09-10Martin Nilsson constant module_type = MODULE_TAG;
bc0fa02001-03-08Per Hedbor constant module_name = "Tags: Wizard generator";
69ab4c2000-04-06Mattias Wingstedt constant module_doc = #"Provides the <tt>&lt;wizard&gt;</tt> tag that is used to create wizard like user interface. Each wizard can contain several pages with form. The user moves through each page in order and the module keeps track of all the user's choices.";
84af181997-11-14Per Hedbor 
cf33bb2000-02-04Marcus Wellhardh string internal_verify(string t, mapping args, string contents, int l, int ol, mapping m) {
e97f1e2000-02-08Marcus Wellhardh  if(sizeof(args)) contents = make_container("if", args, contents); m->verify += ({ ({ contents, ol + l, m->id }) }); return "<__wizard_error__ id=\"id_"+(m->id++)+"\">";
cf33bb2000-02-04Marcus Wellhardh }
f1be801998-02-22Per Hedbor string internal_page(string t, mapping args, string contents, int l, int ol,
e97f1e2000-02-08Marcus Wellhardh  mapping f, RequestID id)
84af181997-11-14Per Hedbor {
cf33bb2000-02-04Marcus Wellhardh  mapping m = ([ "verify":({ }) ]);
0e834c2000-02-24Martin Nilsson 
cf33bb2000-02-04Marcus Wellhardh  f->pages += ({ ({ parse_html_lines(contents, ([]), ([ "verify":internal_verify ]), l, m), ol + l, m->verify }) });
84af181997-11-14Per Hedbor }
6d03cf1998-07-22Johan Schön string internal_done(string t, mapping args, string contents, int l, int ol,
e97f1e2000-02-08Marcus Wellhardh  mapping f, RequestID id)
6d03cf1998-07-22Johan Schön { f->done=contents; }
f1be801998-02-22Per Hedbor string fix_relative(string file, object id) { if(file != "" && file[0] == '/') return file; file = combine_path(dirname(id->not_query) + "/", file); return file; } string old_pike = ""; object old_wizard = 0;
959a491998-07-03Marcus Comstedt string tag_wizard(string t, mapping args, string contents, object id, object file, mapping defines)
84af181997-11-14Per Hedbor {
558cba1998-08-17Peter Bortas  if(!id->misc->line) id->misc->line=-1;
84af181997-11-14Per Hedbor  mapping f = ([ "pages":({}) ]);
62fbb11998-07-19Henrik Grubbström (Grubba)  string pike = ("inherit \"wizard\";\n" +
558cba1998-08-17Peter Bortas  sprintf("# "+id->misc->line+" %O\n"
62fbb11998-07-19Henrik Grubbström (Grubba)  "string name = %O;\n",
b796b51998-11-18Per Hedbor  id->not_query, (args->name||"unnamed")));
21b2fc2000-03-20Martin Stjernholm  if (args->method || args->enctype) { string method = ""; if (args->method) method += " method=" + Roxen.html_encode_tag_value (args->method); if (args->enctype) method += " enctype=" + Roxen.html_encode_tag_value (args->enctype); pike += sprintf ("constant wizard_method = %O;\n", method); }
84af181997-11-14Per Hedbor  int p;
41d0f91998-02-20Per Hedbor  foreach(glob("*-label", indices(args)), string a) {
558cba1998-08-17Peter Bortas  pike += sprintf("# "+id->misc->line+" %O\n",
62fbb11998-07-19Henrik Grubbström (Grubba)  id->not_query); pike += sprintf(" string "+replace(replace(a,"-","_"),({"(",")","+",">"}),
0e834c2000-02-24Martin Nilsson  ({"","","",""}))+
62fbb11998-07-19Henrik Grubbström (Grubba)  " = %O;\n", args[a]);
41d0f91998-02-20Per Hedbor  }
f1be801998-02-22Per Hedbor  if(args->ok) {
558cba1998-08-17Peter Bortas  pike += sprintf("# "+id->misc->line+" %O\n", id->not_query);
62fbb11998-07-19Henrik Grubbström (Grubba)  pike += sprintf("mixed wizard_done(object id)\n" "{\n" " id->not_query = %O;\n\""+
b796b51998-11-18Per Hedbor  " return id->conf->get_file( id );\n"
62fbb11998-07-19Henrik Grubbström (Grubba)  "}\n\n", fix_relative(args->ok, id));
f1be801998-02-22Per Hedbor  }
6d03cf1998-07-22Johan Schön  parse_html_lines(contents, ([]), ([ "page":internal_page,
0e834c2000-02-24Martin Nilsson  "done":internal_done ]),
e97f1e2000-02-08Marcus Wellhardh  (int)id->misc->line, f, id);
0487401998-08-05Henrik Grubbström (Grubba)  if (f->done && !args->ok) {
cb9dc41998-08-19Henrik Grubbström (Grubba)  pike += sprintf("mixed wizard_done(object id)\n" "{\n"
03873f1998-11-02Per Hedbor  " return parse_rxml(%O,id);\n"
cb9dc41998-08-19Henrik Grubbström (Grubba)  "}\n", f->done);
0487401998-08-05Henrik Grubbström (Grubba)  }
f1be801998-02-22Per Hedbor  foreach(f->pages, array q)
84af181997-11-14Per Hedbor  {
62fbb11998-07-19Henrik Grubbström (Grubba)  pike += sprintf("# "+q[1]+" %O\n", id->not_query); pike += sprintf("string page_"+p+"(object id) {" +
03873f1998-11-02Per Hedbor  " return parse_rxml(%O,id);\n"
62fbb11998-07-19Henrik Grubbström (Grubba)  "}\n", q[0]);
e97f1e2000-02-08Marcus Wellhardh  // Code to enable verification of wizard pages.
cf33bb2000-02-04Marcus Wellhardh  if(q[2] && sizeof(q[2])) {
e97f1e2000-02-08Marcus Wellhardh  // FIXME line numbers for verify sections. pike += "int verify_"+p+"(object id) {\n" " int c;\n" " string s = \"\";\n" " id->misc->__wizard_error__ = ([ ]);\n";
0e834c2000-02-24Martin Nilsson  foreach(q[2], array v)
e97f1e2000-02-08Marcus Wellhardh  pike += sprintf(" s = parse_rxml(%O, id);\n" " if(id->misc->defines[\" _ok\"]) {\n" " id->misc->__wizard_error__->id_%d = s;\n" " c++;\n" " }\n", v[0], v[2]);
0e834c2000-02-24Martin Nilsson 
e97f1e2000-02-08Marcus Wellhardh  pike += " if(c)\n" " return 1;\n" "}\n";
cf33bb2000-02-04Marcus Wellhardh  }
0e834c2000-02-24Martin Nilsson 
84af181997-11-14Per Hedbor  p++; }
e97f1e2000-02-08Marcus Wellhardh  //werror("Wiz: %s\n", pike);
f1be801998-02-22Per Hedbor  object w; if(pike == old_pike) w = old_wizard; else { old_wizard = w = compile_string(pike)(); old_pike = pike; }
6d03cf1998-07-22Johan Schön  mixed res = w->wizard_for(id,fix_relative(args->cancel||args->done||"",id));
f1be801998-02-22Per Hedbor 
84af181997-11-14Per Hedbor  if(mappingp(res)) {
f629132000-02-14Marcus Wellhardh  id->misc->defines[" _error"] = res->error; id->misc->defines[" _extra_heads"] = res->extra_heads;
f1be801998-02-22Per Hedbor  return res->data||(res->file&&res->file->read())||"";
84af181997-11-14Per Hedbor  } return res; }
cf33bb2000-02-04Marcus Wellhardh string tag_wizard_error(string t, mapping args, object id, object file, mapping defines) {
e97f1e2000-02-08Marcus Wellhardh  if(id->misc->__wizard_error__ && args->id && id->misc->__wizard_error__[args->id]) return id->misc->__wizard_error__[args->id];
cf33bb2000-02-04Marcus Wellhardh  return ""; }
84af181997-11-14Per Hedbor 
cf33bb2000-02-04Marcus Wellhardh mapping query_tag_callers() { return ([ "__wizard_error__" : tag_wizard_error ]); }
cd34021999-05-19David Hedbor 
84af181997-11-14Per Hedbor mapping query_container_callers() { return ([ "wizard" : tag_wizard ]); }