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 'state' => array('states', 'oneline', 'varchar(2)'),
38 'money' => array('textbox', 'money', 'varchar(32)'),
39 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
40 'url' => array('textbox', 'url', 'varchar(200)'),
41 'hidden' => array('hidden', 'unix', 'varchar(200)'),
42 'password' => array('password', 'oneline', 'varchar(200)'),
43 'textarea' => array('textarea', 'unix', 'text'),
44 'pulldown' => array('pulldown', 'options', 'int'),
45 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
46 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
47 'delete' => array('checkbox', 'yesno', 'n/a'),
48 'image' => array('image', 'oneline', 'varchar(200)'),
49 'submit' => array('submit', 'oneline', 'n/a')
52 if(isset($_REQUEST['form_name'])) {
53 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
55 $GLOBALS['form_name'] = 'some_form';
58 if(isset($_REQUEST['fields'])) {
59 if(isset($_REQUEST['view_sql'])) {
62 } elseif(isset($_REQUEST['view_php'])) {
65 } elseif(isset($_REQUEST['view_template'])) {
68 } elseif(isset($_REQUEST['view_email'])) {
71 } elseif(isset($_REQUEST['download_tar'])) {
74 } elseif(isset($_REQUEST['preview'])) {
77 } elseif(isset($_REQUEST['edit'])) {
78 tem_set('fields', $_REQUEST['fields']);
79 tem_set('form_name', $GLOBALS['form_name']);
82 die("Sorry... couldn't tell which button you pressed");
87 tem_output('code/wfpl/metaform/main.html');
91 function field_input($type) { return $GLOBALS['types'][$type][0]; }
92 function field_format($type) { return $GLOBALS['types'][$type][1]; }
93 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
95 function get_fields() {
96 $fields_str = unix_newlines($_REQUEST['fields']);
98 $fields_str = rtrim($fields_str);
99 $fields = split("\n", $fields_str);
100 foreach($fields as $field) {
101 list($name, $type, $options) = split(' *', $field);
102 if($options) $options = split(',', $options);
103 if(!$type) $type = $name;
104 $input = field_input($type);
105 $format = field_format($type);
106 $sql = field_sql($type);
107 $ret[] = array($name, $type, $input, $format, $sql, $options);
112 # this one, that you're using to create forms
113 function set_form_action() {
114 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
115 if($action == '') $action = './';
116 tem_set('form_action', $action);
119 # perfect HTTP headers for viewing created files
120 function view_headers() {
121 header('Content-type: text/plain');
127 function make_sql() {
129 $tem->load('code/wfpl/metaform/template.sql');
130 $tem->set('form_name', $GLOBALS['form_name']);
131 $fields = get_fields();
132 foreach($fields as $field) {
133 list($name, $type, $input, $format, $sql) = $field;
135 $tem->set('name', $name);
136 $tem->set('type', $sql);
138 $tem->set('default', '0');
140 $tem->set('default', '""');
149 function view_sql() {
155 # pass false if you want to exclude the <head> and <body> tag etc.
156 function make_template($whole_file = true) {
157 $uploads_output_already = false;
159 $tem->load('code/wfpl/metaform/template.html');
160 $tem->set('form_name', $GLOBALS['form_name']);
161 $fields = get_fields();
162 foreach($fields as $field) {
163 list($name, $type, $input, $format, $sql) = $field;
164 $tem->set('name', $name);
165 $tem->set('caption', $name); # fixme
167 if($input != 'hidden') {
170 if($input == 'image' && !$uploads_output_already) {
171 $tem->sub('uploads');
172 $tem->set('enctype_attr', '" enctype="multipart/form-data');
173 $uploads_output_already = true;
176 $tem->set('name', 'save');
177 $tem->set('caption', 'Save');
184 return $tem->get('form');
188 function view_template() {
190 echo make_template();
194 function make_php() {
196 $tem->load('code/wfpl/metaform/template.php');
197 $tem->set('form_name', $GLOBALS['form_name']);
198 $fields = get_fields();
201 $always_field = false;
202 $image_included_yet = false;
203 foreach($fields as $field) {
204 list($name, $type, $input, $format, $sql) = $field;
205 if($input != 'submit') {
206 $tem->set('format', $format);
207 $tem->set('name', $name);
208 $tem->set('db_field', ''); # we don't want to use the value from last time
210 if($db_fields != '') $db_fields .= ',';
212 if($php_fields != '') $php_fields .= ', ';
213 $php_fields .= '$' . $name;
215 if($input == 'image') {
216 $tem->sub('image_upload');
217 $tem->sub('image_db');
218 if(!$image_included_yet) {
219 $tem->sub('image_include');
220 $tem->sub('upload_max');
221 $tem->sub('upload_settings');
222 $image_included_yet = true;
225 $tem->sub('formats');
226 $tem->sub('tem_sets');
228 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
229 $always_field = $name;
233 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
234 $tem->set('always_field', $always_field);
235 $tem->set('db_fields', $db_fields);
236 $tem->set('php_fields', $php_fields);
237 $tem->set('metaform_url', edit_url());
241 # make a URL for the edit page with all the fields filled in
242 function edit_url() {
244 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
245 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
246 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
250 function view_php() {
256 function make_email() {
258 $tem->load('code/wfpl/metaform/template.email.txt');
259 $tem->set('form_name', $GLOBALS['form_name']);
260 $fields = get_fields();
261 foreach($fields as $field) {
262 list($name, $type, $input, $format, $sql) = $field;
263 $tem->set('name', $name);
264 $tem->set('caption', $name); # fixme
265 if($type == 'textarea') {
266 $tem->sub('multi_line');
274 function view_email() {
282 $tem->load('code/wfpl/metaform/preview.html');
283 $tem->set('form_name', $GLOBALS['form_name']);
284 $tem->set('fields', $_REQUEST['fields']);
285 $preview_tem = new tem();
286 $preview = $preview_tem->run(make_template(false));
288 $tem->set('preview', $preview);
293 function download_tar() {
294 $name = $GLOBALS['form_name'];
296 "$name.html" => make_template(),
297 "$name.sql" => make_sql(),
298 "$name.email.txt" => make_email(),
299 "$name.php" => make_php());
300 make_wfpl_tar($name, $data);