#function generate_record project table_id
#query select name as application from `project` where project=?project?
#query select name as tablename, `sql` as tablesql, title as tabletitle, `where`, id_field, width as tablewidth, height as tableheight from `table` where `table`=?table_id?
#print '<?php\n'
#print '$sql->QueryItem(\"SELECT '
#set first=true
#record select field, `Record`, field.sql as fieldsql, `Type`, title, width, SessionVar, height, link_table, link_field, link_display from `field` where field.`table`=?table_id? and SessionVar='' and field.sql<>'' order by `order`
#if not first
#print ', '
#end
#if link_display=''
#print tablesql ^ '.' ^ fieldsql ^ ' as F' ^ field
#else
#if link_table=''
#print queryvars tablesql link_display field 1
#else
#print queryvars ('T' ^ field) link_display field 1
#end
#end
#set first=false
#end
#print ' FROM `' ^ tablesql ^ '`\n'
#record select field, `Add`, field.sql as fieldsql, `Type`, title, width, link_table, link_field, link_display from `field` where field.`table`=?table_id? and link_display<>'' and link_table<>'' and Record<>'none' order by `order`
#print 'LEFT JOIN `' ^ link_table ^ '` AS T' ^ field ^ ' ON ' ^ tablesql ^ '.' ^ fieldsql ^ '=T' ^ field ^ '.' ^ link_field ^ '\n'
#end
#query select field.sql as id_fieldsql, SessionVar from `field` where SessionVar<>'' and field.`table`=?table_id? and field.sql=?id_field? rows nrrec
#if nrrec>0
#print 'WHERE `' ^ tablesql ^ '`.`' ^ id_fieldsql ^ '` = \'$' ^ SessionVar ^ '\'\n'
#end
#print ' LIMIT 1\");\n'
#set expr=''
#set sesvars=''
#set IDintern=''
#record select field, `Record`, name, field.sql as fieldsql, `Type`, title, width, SessionVar, height, link_table, link_field, link_display from `field` where field.`table`=?table_id? order by `order`
#if fieldsql=id_field
#set IDintern=name
#end
#if SessionVar<>''
#set sesvars=sesvars ^ '&' ^ SessionVar ^ '=$' ^ SessionVar
#else
#if link_display=''
#if Type='date'
#print 'preg_match (\'/^(\d+)-0?(\d+)-0?(\d+)$/\', $sql->data[\'F' ^ field ^ '\'], $matches);\n'
#print '$' ^ name ^ ' = \"$matches[3]/$matches[2]/$matches[1]\";\n'
#else
#print '$' ^ name ^ ' = htmlspecialchars($sql->data[\'F' ^ field ^ '\']);\n'
#end
#else
#set getvars=''
#set expr=fieldreplace link_display field
#print getvars
#print '$' ^ name ^ ' = \"' ^ expr ^'\";\n'
#end
#end
#end
#set first=true
#set list=new_thing 'temp' 'root' ('system'['root']) ''
#set fnr=1
#set lnr=1
#set rnr=1
#record select field, `Record` from `field` where field.`table`=?table_id? and `Record`<>'none' order by `order`
#if Record='front'
#set elm=new_thing 'temp' ('00F'^string_of_int fnr) list field
#set fnr=fnr+1
#end
#if Record='left' or Record=''
#set elm=new_thing 'temp' (string_of_dec 2 lnr^'L') list field
#set lnr=lnr+1
#end
#if Record='right'
#set elm=new_thing 'temp' (string_of_dec 2 rnr^'R') list field
#set rnr=rnr+1
#end
#end
#set last=''
<relas thing="list" type="'temp'" rela="elm" sort="name">
#query select `Record`, name, field.sql as fieldsql, `Type`, title, suffix, width, height, link_table, link_field, link_display from `field` where field.`table`=?table_id? and field=?elm.descr?
#if Record='front'
#if last=''
#print 'echo (\'<table class=record>\');\n'
#end
#print 'echo (\"<tr>'
#print '<td colspan=4>$' ^ name ^ (if suffix<>'' then ' ' ^ suffix else '') ^ '</td>\n'
#print '</tr>\");\n'
#set last='front'
#end
#if Record='left' or Record=''
#if last=''
#print 'echo (\'<table class=record>\');\n'
#end
#if last='front'
#print 'echo (\'</table><table class=record>\');\n'
#end
#if last='left'
#print 'echo (\"</tr>\");\n'
#end
#print 'echo (\"<tr>'
#print '<th>' ^ title ^ '</th>\n'
#print '<td>$' ^ name ^ (if suffix<>'' then ' ' ^ suffix else '') ^ '</td>\");\n'
#set last='left'
#end
#if Record='right'
#if last<>'left'
#print 'echo (\"<tr><td></td><td></td>\");\n'
#end
#print 'echo (\"<th>' ^ title ^ '</th>\n'
#print '<td>$' ^ name ^ (if suffix<>'' then ' ' ^ suffix else '') ^ '</td>\n'
#print '</tr>\");\n'
#set last='rigth'
#end
#set first=false
</relas>
#do remove 'temp'['root']
#do execute
#if last='left'
#print 'echo (\"</tr>\");\n'
#end
#print 'echo (\'</table>\');\n'
#print '?>\n'
#if not first
#if IDintern=''
#print '<p><input type=button value=\"Edit ' ^ tabletitle ^ '\" onClick=\"javascript: window.open(\'edit' ^ tablename ^ '.php\',\'\',\'width=' ^ tablewidth ^ ',height=' ^ tableheight ^ ',location=no,menubar=no,scrollbars=yes,status=no,resizable=yes\'); return false;\">\n'
#else
#print '<p><input type=button value=\"Edit ' ^ tabletitle ^ '\" onClick=\"javascript: window.open(\'edit' ^ tablename ^ '.php?' ^ IDintern ^'=<?php echo $' ^ IDintern ^ '; ?>\',\'\',\'width=' ^ tablewidth ^ ',height=' ^ tableheight ^ ',location=no,menubar=no,scrollbars=yes,status=no,resizable=yes\'); return false;\">\n'
#end
#end
#end
#function rights_record project table_id
#query select name as application from `project` where project=?project?
#query select name as tablename, `sql` as tablesql, title as tabletitle, `where`, id_field, width as tablewidth, height as tableheight from `table` where `table`=?table_id?
#set first=true
#set list=new_thing 'temp' 'root' ('system'['root']) ''
#set fnr=1
#set lnr=1
#set rnr=1
#record select field, `Record` from `field` where field.`table`=?table_id? and `Record`<>'none' order by `order`
#if Record='front'
#set elm=new_thing 'temp' ('00F'^string_of_int fnr) list field
#set fnr=fnr+1
#end
#if Record='left' or Record=''
#set elm=new_thing 'temp' (string_of_dec 2 lnr^'L') list field
#set lnr=lnr+1
#end
#if Record='right'
#set elm=new_thing 'temp' (string_of_dec 2 rnr^'R') list field
#set rnr=rnr+1
#end
#end
#set last=''
<relas thing="list" type="'temp'" rela="elm" sort="name">
#query select `field`, `Record`, name, field.sql as fieldsql, `Type`, title, width, height, link_table, link_field, link_display from `field` where field.`table`=?table_id? and field=?elm.descr?
#if Record='front'
#if last=''
#print '<table class=record>\n'
#end
#print '<tr>'
#print '<td>' ^ name ^ ' '
#print field_rights_link field 'hide'
#print '</td>\n'
#print '</tr>\n'
#set last='front'
#end
#if Record='left' or Record=''
#if last=''
#print '<table class=record>'
#end
#if last='front'
#print '</table><table class=record>'
#end
#if last='left'
#print '</tr>\n'
#end
#print '<tr>'
#print '<th>' ^ title ^ '</th>\n'
#print '<td>'
#print field_rights_link field 'hide'
#print '</td>\n'
#set last='left'
#end
#if Record='right'
#if last<>'left'
#print '<tr><td></td><td></td>\n'
#end
#print '<th>' ^ title ^ '</th>\n'
#print '<td>'
#print field_rights_link field 'hide'
#print '</td>\n'
#print '</tr>\n'
#set last='rigth'
#end
#set first=false
</relas>
#do remove 'temp'['root']
#do execute
#if last='left'
#print '</tr>\n'
#end
#if last<>''
#print '</table>\n'
#end
#if not first
#print '<p><input type=button value=\"Edit ' ^ tabletitle ^ '\" onClick=\"javascript: window.open(\'edit' ^ tablename ^ '.php\',\'\',\'width=' ^ tablewidth ^ ',height=' ^ tableheight ^ ',location=no,menubar=no,scrollbars=yes,status=no,resizable=yes\'); return false;\">\n'
#end
#end