#function add_php project table_id
#query select name as application from `project` where project=?project?
#query select `table`, name as tablename, title as tabletitle, `where`, `sql` as tablesql, id_field from `table` where `table`=?table_id?
#set orderfield='0'
#query select `field` as orderfield, field.sql as ordername from `field` where `table`=?table_id? and `Type`='ordering'
#print page_header 'add' project table_id
#print '<?php\n'
#record select Code from actions where `table`=?table_id? and Action='add' and Part='session'
#print Code ^ '\n'
#end
#record select field, `Add`, name, field.sql as fieldsql, `Type`, title, width, height, link_table, link_field, SessionVar, link_display from `field` where `table`=?table_id? and link_table<>'' and title='' order by `order`
#print '$sql->QueryItem(\"SELECT ' ^ queryvars ('T' ^ field) link_display field 1 ^ ' FROM `' ^ link_table ^ '` as T' ^ field ^ ' WHERE `' ^ link_field ^ '` = $' ^ SessionVar ^ '\");\n'
#set getvars=''
#set expr=fieldreplace link_display field
#print getvars
#print ' $' ^ name ^ '_lup = \"' ^ expr ^ '\";\n'
#end
#set firstfield='none'
#query select name as firstfield from `field` where `table`=?table_id? and Record<>'none' and field.sql<>'' order by `order` limit 1
#print 'if ($_POST[\'' ^ firstfield ^ '\']!=\'\') {\n'
#record select name, field.sql as fieldsql, `Type`, title, width, height, link_table, link_field, link_display from `field` where `table`=?table_id? and Record<>'none' and field.sql<>'' order by `order`
#if Type='varchar' or Type='text' or Type='password'
#print ' $' ^ name ^ ' = ($_POST[\'' ^ name ^ '\']);\n'
#else
#if Type='date'
#print '$' ^ name ^ ' = date(\'j/n/Y\', strtotime($_POST[\'' ^ name ^ '\']));\n'
#else
#print '$' ^ name ^ ' = $_POST[\'' ^ name ^ '\'];\n'
#end
#end
#end
#print '}\n'
#print '?>\n'
#print '<HTML>\n'
#print '<HEAD>\n'
#print ' <META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=windows-1252\">\n'
#print ' <TITLE>' ^ tabletitle ^ ' add</TITLE>\n'
#print '</HEAD>\n'
#print '<BODY LANG=\"en-US\" DIR=\"LTR\" ONLOAD=\"Insert.<?php\n'
#set fid='0'
#set ff=''
#record select field, `Record`, `Edit`, field.sql as fieldsql, name, `Type`, title, SessionVar, height, link_table, link_field, link_display, `default` from `field` where `table`=?table_id? and field.sql<>'' and Record<>'none' and `Edit`<>'false' order by `order`
#if ff=''
#set ff=fieldsql
#end
#if fid>'0'
#print 'if ($_POST[FOCUS]==' ^ fid ^ ') {echo \'' ^ name ^ '\';}\nelse '
#set fid='0'
#end
#if link_table<>'' or Type='enum'
#set fid=field
#end
#end
#print '{echo \'' ^ ff ^ '\';}\n'
#print '?>.focus();\">\n'
#record select Code from actions where `table`=?table_id? and Action='add' and Part='header'
#print Code ^ '\n'
#end
#print '<FORM NAME=\"Insert\" ACTION=\"postadd' ^ tablename ^ '.php\" METHOD=POST>\n'
#print '<INPUT TYPE=\"HIDDEN\" NAME=\"FOCUS\" VALUE=\"\">\n'
#print '<TABLE WIDTH=100% BORDER=0 CELLPADDING=4 CELLSPACING=1>\n'
#print ' <THEAD>\n'
#print ' <TR>\n'
#print ' <TD COLSPAN=2 WIDTH=100% VALIGN=TOP BGCOLOR=\"#000080\">\n'
#print ' <P><FONT COLOR=\"#ffffff\"><FONT FACE=\"Verdana, sans-serif\"><FONT SIZE=1 STYLE=\"font-size: 8pt\">'
#print '<B>' ^ tabletitle ^ ' add\n'
#print ' </B></FONT></FONT></FONT></P>\n'
#print ' </TD>\n'
#print ' </TR>\n'
#print ' </THEAD>\n'
#print ' <TBODY>\n'
#if orderfield<>'0'
#print ' <INPUT TYPE=HIDDEN NAME=\"' ^ ordername ^ '\" VALUE=\"<?php echo $_GET[' ^ ordername ^']; ?>\">\n'
#end
#record select field, `Add`, name, field.sql as fieldsql, `Type`, title, `default`, SessionVar, width as ListWidth, width * 7 as width, height, link_table, link_where, link_field, link_display from `field` where `table`=?table_id? and Record<>'none' and `Add`<>'false' order by `order`
#if SessionVar<>''
#print ' <INPUT TYPE=HIDDEN NAME=\"' ^ SessionVar ^ '\" VALUE=\"<?php echo $' ^ SessionVar ^ '; ?>\">\n'
#end
#print ' <TR VALIGN=TOP>\n'
#print ' <TD WIDTH=20% BGCOLOR=\"#e6e6ff\">\n'
#print ' <P><FONT COLOR=\"#000080\"><FONT FACE=\"Verdana, sans-serif\"><FONT SIZE=1 STYLE=\"font-size: 8pt\">\n'
#print ' ' ^ title ^'\n'
#print ' </FONT></FONT></FONT></P>\n'
#print ' </TD>\n'
#print ' <TD WIDTH=80%>\n'
#print ' <P><FONT COLOR=\"#000080\"><FONT FACE=\"Verdana, sans-serif\"><FONT SIZE=1 STYLE=\"font-size: 8pt\">\n'
#print ' <?php\n'
#print ' $current=($_POST[\'' ^ name ^ '\']!=\'\');\n'
#if link_table<>''
#if width<>'0'
#print ' echo (\"<SELECT NAME=\\\"' ^ name ^ '\\\" STYLE=\\\"width: ' ^ width ^ 'px\\\" OnChange=\\\"Insert.action=\'\'; Insert.FOCUS.value=\'' ^ field ^ '\'; Insert.submit();\\\">\");\n'
#else
#print ' echo (\"<SELECT NAME=\\\"' ^ name ^ '\\\" OnChange=\\\"Insert.action=\'\'; Insert.FOCUS.value=\'' ^ field ^ '\'; Insert.submit();\\\">\");\n'
#end
#if default='<empty>'
#print ' echo (\"<OPTION VALUE=\\\"\\\"></OPTION>\");\n'
#set default=''
#end
#if default<>'' and string_find default '$'<0
#print ' echo (\"<OPTION VALUE=\\\"' ^default^ '\\\">' ^ default ^ '</OPTION>\");\n'
#end
#if link_display=''
#print ' $sql->QueryTry(\"' ^ link_table ^ '\");\n'
#else
#print '$sql->Query(\"SELECT '
#print queryvars '' link_display '0' 1 ^ ', `' ^ link_table ^ '`.' ^ link_field
#print ' FROM `' ^ link_table ^'`' ^ queryjoin ^ ' '
#set wcheck=''
#query select `where` as wcheck from `table` where project=?project? and name=?link_table?
#if wcheck<>''
#print '\n WHERE ' ^ fieldreplace wcheck '0'
#if link_where<>''
#print ' and ' ^ link_where
#end
#print '\n'
#else
#if link_where<>''
#print ' where ' ^ link_where ^ '\n'
#end
#end
#print ' ' ^ queryorder ^ '\");\n'
#end
#print ' for ($i = 0; $i < $sql->rows; $i++)\n'
#print ' {\n'
#print ' $sql->Fetch($i);\n'
#if link_display=''
#set lfield=''
#match link_field with \([^,]+\),\(.*\) into lfield,dfield
#print ' $searchID = $sql->data[' ^ lfield ^ '];\n'
#print ' $searchshow = $sql->data[' ^ dfield ^ '];\n'
#end
#if lfield=''
#print ' $searchID = $sql->data[' ^ link_field ^ '];\n'
#print ' $searchshow = $searchID;\n'
#end
#else
#print ' $searchID = $sql->data[' ^ link_field ^ '];\n'
#set expr=fieldreplace link_display '0'
#print getvars
#print ' $searchshow = \"' ^ expr ^ '\";\n'
#end
#print ' echo (\"<OPTION \");\n'
#if default<>''
#print ' if (!$current && $searchID == \"' ^ default ^ '\") {echo \"SELECTED \";};\n'
#end
#print ' if ($current && $searchID == $_POST[\'' ^ name ^ '\']) {echo \"SELECTED \";};\n'
#print ' echo \"VALUE=\\\"$searchID\\\">$searchshow\";\n'
#print ' }\n'
#print ' echo (\"</SELECT>\");\n'
#else
#if Type='enum'
#if width <>'0'
#print ' echo (\"<SELECT NAME=\\\"' ^ name ^ '\\\" STYLE=\\\"width: ' ^ width ^ 'px\\\" OnChange=\\\"Insert.action=\'\'; Insert.FOCUS.value=\'' ^ field ^ '\'; Insert.submit();\\\">\");\n'
#else
#print ' echo (\"<SELECT NAME=\\\"' ^ name ^ '\\\" OnChange=\\\"Insert.action=\'\'; Insert.FOCUS.value=\'' ^ field ^ '\'; Insert.submit();\\\">\");\n'
#end
#set val=enum data_connection tablesql fieldsql
#split res=val on ,
#match res with '\(.*\)' into res
#print ' if (\''^res^'\' == $_POST[\'' ^ name ^'\']) {$sel=\'SELECTED \';} else {$sel=\'\';};\n'
#print ' echo \"<OPTION VALUE=\\\"' ^ res ^ '\\\"$sel>' ^ res ^ '</OPTION>\\n\";\n'
#end
#end
#print ' echo (\"</SELECT></FONT></FONT>\");\n'
#end
#if Type='int' or Type='data' or Type='varchar' or Type='password'
#if default=''
#print ' $strdefault = $_POST[\'' ^ name ^ '\'];\n'
#print ' echo \"<INPUT TYPE=TEXT NAME=\\\"' ^ name ^ '\\\" SIZE=' ^ ListWidth ^ ' VALUE=\\\"$strdefault\\\">\";\n'
#else
#print ' $strdefault = ' ^default^ ';\n'
#print ' if ($current) {$strdefault = $_POST[\'' ^ name ^ '\'];}\n'
#print ' echo \"<INPUT TYPE=TEXT NAME=\\\"' ^ name ^ '\\\" SIZE=' ^ ListWidth ^ ' VALUE=\\\"$strdefault\\\">\";\n'
#end
#end
#if Type='text' or Type='mediumtext' or Type='longtext'
#print ' $strdefault = $_POST[\'' ^ name ^ '\'];\n'
#print ' echo \"<TEXTAREA NAME=\\\"' ^ name ^ '\\\" ROWS=' ^ height ^' COLS=' ^ ListWidth ^ '>$strdefault</TEXTAREA>\";\n'
#end
#end
#print ' ?>\n'
#print ' </FONT></FONT></FONT></P>\n'
#print ' </TD>\n'
#print ' </TR>\n'
#end
#print ' </TBODY>\n'
#print '</TABLE>\n'
#print '<P ALIGN=RIGHT>\n'
#set Code=''
#query select Code from actions where `table`=?table_id? and Action='add' and Part='complete'
#if Code<>''
#print '<?php\n'
#print 'if ('^ Code ^') { echo \"<INPUT TYPE=submit VALUE=Add>\\n\"; }\n'
#print '?>\n'
#else
#print '<INPUT TYPE=submit VALUE=Add>\n'
#end
#print '<INPUT TYPE=BUTTON VALUE=\"Cancel\" onClick=\"window.close()\">\n'
#print '</P>\n'
#print '</FORM>\n'
#record select Code from actions where `table`=?table_id? and Action='add' and Part='footer'
#print '<?php\n' ^ Code ^ '\n?>\n'
#end
#print '</BODY>\n'
#print '</HTML>\n'
#end
#function postadd_php project table_id
#query select name as application from `project` where project=?project?
#query select name as tablesql, `table`, `where`, name as tablename, id_field from `table` where `table`=?table_id?
#set orderfield='0'
#query select `field` as orderfield, field.sql as ordername from `field` where `table`=?table_id? and `Type`='ordering'
#print page_header 'postadd' project table_id
#print '<?php\n'
# remove return form : better implementation needed with no $_POST inside input html code
# print 'echo \"<FORM NAME=\\\"Terug\\\" ACTION=\\\"add' ^ tablename ^ '.php\\\" METHOD=POST>\\n\";\n'
# print 'echo \"<INPUT TYPE=button VALUE=\\\"Back\\\" onClick=\\\"Terug.submit()\\\">\\n\";\n'
# record select `Add`, name, field.sql as fieldsql, `Type`, title, width, SessionVar, height, link_table, OrderByField, link_field, link_display from `field` where `table`=?table_id? order by `order`
# if Add='true'
# print 'echo \"<INPUT TYPE=hidden NAME=\\\"' ^ name ^ '\\\" VALUE=\\\"$_POST[\'' ^ name ^'\']\\\">\\n\";\n'
# end
# end
# print 'echo \"</FORM>\\n\";\n'
#record select `Add`, name, field.sql as fieldsql, `Type`, title, `default`, width, SessionVar, height, link_table, link_field, link_display from `field` where `table`=?table_id? and SessionVar='' and field.sql<>'' and field.sql<>?id_field? order by `order`
#print '$' ^ name ^ '=quote($_POST[\'' ^ name ^ '\']);\n'
#if link_table<>'' and fieldsql=''
#if default<>''
#print 'if ($' ^ name ^ '<>\'\\\'' ^ default ^ '\\\'\') {\n'
#end
#print '$sql->Query(\"SELECT '
#print queryvars 'T' link_display '0' 1 ^ ', T.' ^ link_field
#print ' FROM `' ^ link_table ^'` as T \n'
#set wcheck=''
#query select `where` as wcheck from `table` where project=?project? and name=?link_table?
#if wcheck<>''
#print 'WHERE ' ^ fieldreplace wcheck '0' ^ '\n'
#end
#print 'and `' ^ link_field ^ '`=$' ^ name
#print '\");\n'
#print 'if ($sql->rows==0) { die (\"No related record field: ' ^ name ^ ' value: $' ^ name ^ '\"); }\n'
#if default<>''
#print '}\n'
#end
#end
#end
#if orderfield<>'0'
#set where='1'
#record select field.sql as fieldsql, SessionVar from `field` where `table`=?table_id? and SessionVar<>'' and field.sql<>?id_field? and field.sql<>'' order by `order`
#if where='1'
#set where=''
#else
#set where=where ^ ' AND '
#end
#set where=where ^'`' ^ tablesql ^ '`.`' ^ fieldsql ^ '`=\'$' ^ SessionVar ^ '\''
#end
#print 'if ($' ^ ordername ^ '<>\'\' && $' ^ ordername ^ '<>\'\\\'\\\'\') {\n'
#print ' $sql->Query(\"UPDATE `' ^ tablesql ^ '` SET `' ^ ordername ^ '`=`' ^ ordername ^ '`+1 WHERE `' ^ ordername ^ '`>=$' ^ordername ^' AND ' ^ where ^'\");\n'
#print '} else {\n'
#print ' $sql->QueryItem(\"SELECT `' ^ ordername ^ '`+1 as next FROM `' ^ tablesql ^ '` WHERE ' ^ where ^ ' ORDER BY `' ^ ordername ^ '` DESC LIMIT 1\");\n'
#print ' if (!$sql->data[next]) {\n'
#print ' $' ^ ordername ^'=1;\n'
#print ' } else {\n'
#print ' $' ^ ordername ^ '=$sql->data[next];\n'
#print ' }\n'
#print '}\n'
#end
#print '$sql->Insert(\"INSERT INTO `' ^ tablesql ^ '`\n'
#print ' (\n'
#set first=true
#record select `Add`, name, field.sql as fieldsql, `Type`, title, width, height, link_table, link_field, link_display, SessionVar from `field` where field.`table`=?table_id? and field.sql<>'' and field.sql<>?id_field? order by `order`
#if first
#set first=false
#else
#print ',\n'
#end
#print ' `' ^ fieldsql ^ '`'
#end
#print '\n )\n'
#print ' VALUES (\n'
#set first=true
#record select `Add`, name, field.sql as fieldsql, `Type`, title, width, height, link_table, link_field, link_display, SessionVar from `field` where field.`table`=?table_id? and field.sql<>'' and field.sql<>?id_field? order by `order`
#if first
#set first=false
#else
#print ',\n'
#end
#if SessionVar<>''
#print ' \'$' ^ SessionVar ^'\''
#else
#print ' $' ^ name
#end
#end
#print '\n )\");\n'
#print '$affected_rows = $sql->a_rows;\n'
#record select Code from actions where actions.`table`=?table_id? and Action='add' and Part='onchange'
#print 'if ($affected_rows>0) {\n' ^ Code ^ '\n}\n'
#end
#print 'echo \"<P><FONT FACE=\\\"Verdana, sans-serif\\\"><FONT SIZE=1 STYLE=\\\"font-size: 8pt\\\">\";\n'
#print 'echo \"Er is/zijn $affected_rows records toegevoegd<BR>\";\n'
#record select `Add`, field.sql as fieldsql, name, `Type`, title, width, height, link_table, link_field, link_display from `field` where field.`table`=?table_id? order by `order`
#if Add='true'
#print 'echo \"$' ^ name ^ '<BR>\";\n'
#end
#end
#print 'echo \"</P>\";\n'
#print 'if ($affected_rows>0) {\n'
#print ' echo \"<script>window.opener.location.reload(true)</script>\";\n'
#print ' echo \"<script>window.close(self)</script>\";\n'
#print '}\n'
## create last_table field from current table_order
##record select `Add`, name, field.sql as fieldsql, `Type`, title, width, height, link_table, link_field, link_display, SessionVar from `field` where field.`table`=?table_id? and field.sql<>'' and field.sql<>?id_field? and `sql`=?ListOrderByField?
##print '$_SESSION[\'last_'^table^'\']=$' ^ name ^ ';\n'
##end
#print ';\n'
#print '?>\n'
#print '<INPUT TYPE=BUTTON VALUE=\"Close\" onClick=\"window.opener.location.reload(true);self.close()\">\n'
#end