#function add_php dbase_id table_id
#query select DatabaseName from `database` where DatabaseID=?dbase_id?
#query select TableID, Subject, Title, Checks, Session, TableName, IDfield from `tables` where TableID=?table_id?
#set orderfield='0'
#query select `FieldID` as orderfield, FieldName as ordername from `fields` where `TableID`=?table_id? and `Type`='ordering'
#print page_header 'add' dbase_id table_id
#print '<?php\n'
#record select Code from actions where TableID=?table_id? and Action='add' and Part='session'
#print Code ^ '\n'
#end
#print '$sql = new MySQL_class;\n'
#print '$sql->Create(\"' ^ DatabaseName ^ '\");\n'
#record select fieldID, `Add`, Name, FieldName, `Type`, DisplayName, DisplayLength, Help, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, SessionVar, LookupDisplayField from `fields` where TableID=?table_id? and LookupTable<>'' and SessionVar<>'' and DisplayName='' order by `order`
#print '$sql->QueryItem(\"SELECT `' ^ LookupDisplayField ^ '` FROM `' ^ LookupTable ^ '` WHERE `' ^ LookupIDField ^ '` = $' ^ SessionVar ^ '\");\n'
#print '$' ^ Name ^ ' = htmlspecialchars($sql->data[0]);\n'
#end
#set firstfield='none'
#query select FieldName as firstfield from `fields` where TableID=?table_id? and Record<>'none' and FieldName<>'' order by `order` limit 1
#print 'if ($_POST[\'' ^ firstfield ^ '\']!=\'\') {\n'
#record select Name, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? and Record<>'none' and FieldName<>'' order by `order`
#if Type='varchar' or Type='text'
#print ' $' ^ Name ^ ' = ($_POST[\'' ^ FieldName ^ '\']);\n'
#else
#if Type='date'
#print '$' ^ Name ^ ' = date(\'j/n/Y\', strtotime($_POST[\'' ^ FieldName ^ '\']));\n'
#else
#print '$' ^ Name ^ ' = $_POST[\'' ^ FieldName ^ '\'];\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>' ^ Title ^ ' add</TITLE>\n'
#print '</HEAD>\n'
#print '<BODY LANG=\"en-US\" DIR=\"LTR\">\n'
#record select Code from actions where TableID=?table_id? and Action='add' and Part='header'
#print Code ^ '\n'
#end
#print '<FORM NAME=\"Insert\" ACTION=\"postadd' ^ Subject ^ '.php\" METHOD=POST>\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>' ^ Title ^ ' 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 `Add`, FieldName, `Type`, DisplayName, `default`, SessionVar, Help, areaheight, listwidth, listwidth*7 as pxwidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? and Record<>'none' 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 ' ' ^ DisplayName ^'\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[\'' ^ FieldName ^ '\']!=\'\');\n'
#if LookupTable<>''
#if pxwidth<>'0'
#print ' echo (\"<SELECT NAME=\\\"' ^ FieldName ^ '\\\" STYLE=\\\"width: ' ^ pxwidth ^ 'px\\\" OnChange=\\\"Insert.action=\'\'; Insert.submit();\\\">\");\n'
#else
#print ' echo (\"<SELECT NAME=\\\"' ^ FieldName ^ '\\\" OnChange=\\\"Insert.action=\'\'; 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 LookupDisplayField=''
#print ' $sql->QueryTry(\"' ^ LookupTable ^ '\");\n'
#else
#print ' $sql->Query(\"SELECT '
#print queryvars 'T' LookupDisplayField '0' 1 ^ ', T.' ^ LookupIDField
#print ' FROM `' ^ LookupTable ^'` as T'
#set wcheck=''
#query select Checks as wcheck from `tables` where databaseID=?dbase_id? and TableName=?LookupTable?
#if wcheck<>''
#print ' \n WHERE ' ^ fieldreplace wcheck '0' ^ '\n'
#end
#print ' ORDER BY ' ^ OrderByField ^ ' ASC\");\n'
#end
#print ' for ($i = 0; $i < $sql->rows; $i++)\n'
#print ' {\n'
#print ' $sql->Fetch($i);\n'
#print ' $opzoekID = $sql->data[' ^ LookupIDField ^ '];\n'
#if LookupDisplayField=''
#print ' $opzoekweergave = $opzoekID;\n'
#else
#set expr=fieldreplace LookupDisplayField '0'
#print getvars
#print ' $opzoekweergave = \"' ^ expr ^ '\";\n'
#end
#print ' echo (\"<OPTION \");\n'
#if default<>''
#print ' if (!$current && $opzoekID == \"' ^ default ^ '\") {echo \"SELECTED \";};\n'
#end
#print ' if ($current && $opzoekID == $_POST[\'' ^ FieldName ^ '\']) {echo \"SELECTED \";};\n'
#print ' echo \"VALUE=\\\"$opzoekID\\\">$opzoekweergave\";\n'
#print ' }\n'
#print ' echo (\"</SELECT>\");\n'
#else
#if Type='enum'
#if pxwidth <>'0'
#print ' echo (\"<SELECT NAME=\\\"' ^ FieldName ^ '\\\" STYLE=\\\"width: ' ^ pxwidth ^ 'px\\\" OnChange=\\\"Insert.action=\'\'; Insert.submit();\\\">\");\n'
#else
#print ' echo (\"<SELECT NAME=\\\"' ^ FieldName ^ '\\\" OnChange=\\\"Insert.action=\'\'; Insert.submit();\\\">\");\n'
#end
#set val=enum data_connection TableName FieldName
#split res=val on ,
#match res with '\(.*\)' into res
#print ' if (\''^res^'\' == $_POST[\'' ^ FieldName ^'\']) {$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'
#if default=''
#print ' $strdefault = $_POST[\'' ^ FieldName ^ '\'];\n'
#print ' echo \"<INPUT TYPE=TEXT NAME=\\\"' ^ FieldName ^ '\\\" SIZE=' ^ listwidth ^ ' VALUE=\\\"$strdefault\\\">\";\n'
#else
#print ' $strdefault = ' ^default^ ';\n'
#print ' if ($current) {$strdefault = $_POST[\'' ^ FieldName ^ '\'];}\n'
#print ' echo \"<INPUT TYPE=TEXT NAME=\\\"' ^ FieldName ^ '\\\" SIZE=' ^ listwidth ^ ' VALUE=\\\"$strdefault\\\">\";\n'
#end
#end
#if Type='text' or Type='mediumtext' or Type='longtext'
#print ' $strdefault = $_POST[\'' ^ FieldName ^ '\'];\n'
#print ' echo \"<TEXTAREA NAME=\\\"' ^ FieldName ^ '\\\" ROWS=' ^ areaheight ^' COLS=' ^ listwidth ^ '>$strdefault</TEXTAREA>\";\n'
#end
#end
#print ' ?>\n'
#if Help<>''
#print ' <INPUT TYPE=BUTTON VALUE=\"Help\" onClick=\"javascript: window.open(\'vraagbaak.php?zoekitem=' ^ Help ^ '\',\'Help\',\'width=300,height=500,location=no,menubar=no,scrollbars=yes,status=no,resizable=yes\'); return false;\">\n\n'
#end
#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 TableID=?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 TableID=?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 dbase_id table_id
#query select DatabaseName from `database` where DatabaseID=?dbase_id?
#query select TableName, TableID, Checks, Session, Subject, IDfield, Include, ListOrderByField from `tables` where TableID=?table_id?
#set orderfield='0'
#query select `FieldID` as orderfield, FieldName as ordername from `fields` where `TableID`=?table_id? and `Type`='ordering'
#print page_header 'postadd' dbase_id table_id
#print '<?php\n'
#print '$sql = new MySQL_class;\n'
#print '$sql->Create(\"' ^ DatabaseName ^ '\");\n'
# remove return form : better implementation needed with no $_POST inside input html code
# print 'echo \"<FORM NAME=\\\"Terug\\\" ACTION=\\\"add' ^ Subject ^ '.php\\\" METHOD=POST>\\n\";\n'
# print 'echo \"<INPUT TYPE=button VALUE=\\\"Back\\\" onClick=\\\"Terug.submit()\\\">\\n\";\n'
# record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, SessionVar, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? order by `order`
# if Add='true'
# print 'echo \"<INPUT TYPE=hidden NAME=\\\"' ^ FieldName ^ '\\\" VALUE=\\\"$_POST[\'' ^ FieldName ^'\']\\\">\\n\";\n'
# end
# end
# print 'echo \"</FORM>\\n\";\n'
#record select `Add`, Name, FieldName, `Type`, DisplayName, `default`, DisplayLength, SessionVar, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? and SessionVar='' and FieldName<>'' and FieldName<>?IDfield? order by `order`
#print '$' ^ FieldName ^ '=quote($_POST[\'' ^ FieldName ^ '\']);\n'
#if LookupTable<>'' and FieldName=''
#if default<>''
#print 'if ($' ^ Name ^ '<>\'\\\'' ^ default ^ '\\\'\') {\n'
#end
#print '$sql->Query(\"SELECT '
#print queryvars 'T' LookupDisplayField '0' 1 ^ ', T.' ^ LookupIDField
#print ' FROM `' ^ LookupTable ^'` as T \n'
#set wcheck=''
#query select Checks as wcheck from `tables` where databaseID=?dbase_id? and TableName=?LookupTable?
#if wcheck<>''
#print 'WHERE ' ^ fieldreplace wcheck '0' ^ '\n'
#end
#print 'and `' ^ LookupIDField ^ '`=$' ^ 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 FieldName, SessionVar from `fields` where TableID=?table_id? and SessionVar<>'' and FieldName<>?IDfield? and FieldName<>'' order by `order`
#if where='1'
#set where=''
#else
#set where=where ^ ' AND '
#end
#set where=where ^'`' ^ TableName ^ '`.`' ^ FieldName ^ '`=\'$' ^ SessionVar ^ '\''
#end
#print 'if ($' ^ ordername ^ '<>\'\') {\n'
#print ' $sql->Query(\"UPDATE `' ^ TableName ^ '` SET `' ^ ordername ^ '`=`' ^ ordername ^ '`+1 WHERE `' ^ ordername ^ '`>=$' ^ordername ^' AND ' ^ where ^'\");\n'
#print '} else {\n'
#print ' $sql->QueryItem(\"SELECT `' ^ ordername ^ '`+1 as next FROM `' ^ TableName ^ '` 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 `' ^ TableName ^ '`\n'
#print ' (\n'
#set first=true
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField, SessionVar from `fields` where TableID=?table_id? and FieldName<>'' and FieldName<>?IDfield? order by `order`
#if first
#set first=false
#else
#print ',\n'
#end
#print ' `' ^ FieldName ^ '`'
#end
#print '\n )\n'
#print ' VALUES (\n'
#set first=true
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField, SessionVar from `fields` where TableID=?table_id? and FieldName<>'' and FieldName<>?IDfield? order by `order`
#if first
#set first=false
#else
#print ',\n'
#end
#if SessionVar<>''
#print ' \'$' ^ SessionVar ^'\''
#else
#print ' $' ^ FieldName
#end
#end
#print '\n )\");\n'
#print '$affected_rows = $sql->a_rows;\n'
#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`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField from `fields` where TableID=?table_id? order by `order`
#if Add='true'
#print 'echo \"$' ^ FieldName ^ '<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'
#print '$_SESSION[\'last_'^TableID^'\']='
#record select `Add`, FieldName, `Type`, DisplayName, DisplayLength, areaheight, areawidth, LookupTable, OrderByField, LookupIDField, LookupDisplayField, SessionVar from `fields` where TableID=?table_id? and FieldName<>'' and FieldName<>?IDfield? and FieldName=?ListOrderByField?
#if SessionVar<>''
#print '\'$' ^ SessionVar ^'\';\n'
#else
#print '$' ^ FieldName ^ ';\n'
#end
#end
#print ';\n'
#print '?>\n'
#print '<INPUT TYPE=BUTTON VALUE=\"Close\" onClick=\"window.opener.location.reload(true);self.close()\">\n'
#end