3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
7 # wfpl is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU Lesser General Public License as published by the Free
9 # Software Foundation; either version 2.1 of the License, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # This file writes the code for you (sql, php, html, email) to handle a form.
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.php');
26 require_once('code/wfpl/tar.php');
28 # see code/wfpl/metaform/template.html for the html templates for these elements
29 $GLOBALS['types'] = array(
30 # type input format sql
31 'name' => array('textbox', 'oneline', 'varchar(200)'),
32 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
33 'int' => array('textbox', 'int', 'int'),
34 'bigint' => array('textbox', 'int', 'varchar(100)'), # up to 100 digits, stored as a string
35 'email' => array('textbox', 'email', 'varchar(100)'),
36 'phone' => array('textbox', 'phone', 'varchar(32)'),
37 'money' => array('textbox', 'money', 'varchar(32)'),
38 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
39 'url' => array('textbox', 'url', 'varchar(200)'),
40 'hidden' => array('hidden', 'unix', 'varchar(200)'),
41 'password' => array('password', 'oneline', 'varchar(200)'),
42 'textarea' => array('textarea', 'unix', 'text'),
43 'pulldown' => array('pulldown', 'options', 'int'),
44 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
45 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
46 'delete' => array('checkbox', 'yesno', 'n/a'),
47 'image' => array('image', 'oneline', 'varchar(200)'),
48 'submit' => array('submit', 'oneline', 'n/a')
51 if(isset($_REQUEST['form_name'])) {
52 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
54 $GLOBALS['form_name'] = 'some_form';
57 if(isset($_REQUEST['fields'])) {
58 if(isset($_REQUEST['view_sql'])) {
61 } elseif(isset($_REQUEST['view_php'])) {
64 } elseif(isset($_REQUEST['view_template'])) {
67 } elseif(isset($_REQUEST['view_email'])) {
70 } elseif(isset($_REQUEST['download_tar'])) {
73 } elseif(isset($_REQUEST['preview'])) {
76 } elseif(isset($_REQUEST['edit'])) {
77 tem_set('fields', $_REQUEST['fields']);
78 tem_set('form_name', $GLOBALS['form_name']);
81 die("Sorry... couldn't tell which button you pressed");
86 tem_output('code/wfpl/metaform/main.html');
90 function field_input($type) { return $GLOBALS['types'][$type][0]; }
91 function field_format($type) { return $GLOBALS['types'][$type][1]; }
92 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
94 function get_fields() {
95 $fields_str = unix_newlines($_REQUEST['fields']);
97 $fields_str = rtrim($fields_str);
98 $fields = split("\n", $fields_str);
99 foreach($fields as $field) {
100 list($name, $type, $options) = split(' *', $field);
101 if($options) $options = split(',', $options);
102 if(!$type) $type = $name;
103 $input = field_input($type);
104 $format = field_format($type);
105 $sql = field_sql($type);
106 $ret[] = array($name, $type, $input, $format, $sql, $options);
111 # this one, that you're using to create forms
112 function set_form_action() {
113 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
114 if($action == '') $action = './';
115 tem_set('form_action', $action);
118 # perfect HTTP headers for viewing created files
119 function view_headers() {
120 header('Content-type: text/plain');
126 function make_sql() {
128 $tem->load('code/wfpl/metaform/template.sql');
129 $tem->set('form_name', $GLOBALS['form_name']);
130 $fields = get_fields();
131 foreach($fields as $field) {
132 list($name, $type, $input, $format, $sql) = $field;
134 $tem->set('name', $name);
135 $tem->set('type', $sql);
137 $tem->set('default', '0');
139 $tem->set('default', '""');
148 function view_sql() {
154 # pass false if you want to exclude the <head> and <body> tag etc.
155 function make_template($whole_file = true) {
156 $uploads_output_already = false;
158 $tem->load('code/wfpl/metaform/template.html');
159 $tem->set('form_name', $GLOBALS['form_name']);
160 $fields = get_fields();
161 foreach($fields as $field) {
162 list($name, $type, $input, $format, $sql) = $field;
163 $tem->set('name', $name);
164 $tem->set('caption', $name); # fixme
166 if($input != 'hidden') {
169 if($input == 'image' && !$uploads_output_already) {
170 $tem->sub('uploads');
171 $tem->set('enctype_attr', '" enctype="multipart/form-data');
172 $uploads_output_already = true;
175 $tem->set('name', 'save');
176 $tem->set('caption', 'Save');
183 return $tem->get('form');
187 function view_template() {
189 echo make_template();
193 function make_php() {
195 $tem->load('code/wfpl/metaform/template.php');
196 $tem->set('form_name', $GLOBALS['form_name']);
197 $fields = get_fields();
200 $always_field = false;
201 $image_included_yet = false;
202 foreach($fields as $field) {
203 list($name, $type, $input, $format, $sql) = $field;
204 if($input != 'submit') {
205 $tem->set('format', $format);
206 $tem->set('name', $name);
207 $tem->set('db_field', ''); # we don't want to use the value from last time
209 if($db_fields != '') $db_fields .= ',';
211 if($php_fields != '') $php_fields .= ', ';
212 $php_fields .= '$' . $name;
214 if($input == 'image') {
215 $tem->sub('image_upload');
216 $tem->sub('image_db');
217 if(!$image_included_yet) {
218 $tem->sub('image_include');
219 $tem->sub('upload_max');
220 $tem->sub('upload_settings');
221 $image_included_yet = true;
224 $tem->sub('formats');
225 $tem->sub('tem_sets');
227 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
228 $always_field = $name;
232 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
233 $tem->set('always_field', $always_field);
234 $tem->set('db_fields', $db_fields);
235 $tem->set('php_fields', $php_fields);
236 $tem->set('metaform_url', edit_url());
240 # make a URL for the edit page with all the fields filled in
241 function edit_url() {
243 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
244 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
245 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
249 function view_php() {
255 function make_email() {
257 $tem->load('code/wfpl/metaform/template.email.txt');
258 $tem->set('form_name', $GLOBALS['form_name']);
259 $fields = get_fields();
260 foreach($fields as $field) {
261 list($name, $type, $input, $format, $sql) = $field;
262 $tem->set('name', $name);
263 $tem->set('caption', $name); # fixme
269 function view_email() {
277 $tem->load('code/wfpl/metaform/preview.html');
278 $tem->set('form_name', $GLOBALS['form_name']);
279 $tem->set('fields', $_REQUEST['fields']);
280 $preview_tem = new tem();
281 $preview = $preview_tem->run(make_template(false));
283 $tem->set('preview', $preview);
288 function download_tar() {
289 $name = $GLOBALS['form_name'];
291 "$name.html" => make_template(),
292 "$name.sql" => make_sql(),
293 "$name.email.txt" => make_email(),
294 "$name.php" => make_php());
295 make_wfpl_tar($name, $data);