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 'radio' => array('radio', 'oneline', 'varchar(200)'),
46 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
47 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
48 'delete' => array('checkbox', 'yesno', 'n/a'),
49 'image' => array('image', 'oneline', 'varchar(200)'),
50 'submit' => array('submit', 'oneline', 'n/a')
53 if(isset($_REQUEST['form_name'])) {
54 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
56 $GLOBALS['form_name'] = 'some_form';
59 if(isset($_REQUEST['fields'])) {
60 if(isset($_REQUEST['view_sql'])) {
63 } elseif(isset($_REQUEST['view_php'])) {
66 } elseif(isset($_REQUEST['view_template'])) {
69 } elseif(isset($_REQUEST['view_email'])) {
72 } elseif(isset($_REQUEST['download_tar'])) {
75 } elseif(isset($_REQUEST['preview'])) {
78 } elseif(isset($_REQUEST['edit'])) {
79 tem_set('fields', $_REQUEST['fields']);
80 tem_set('form_name', $GLOBALS['form_name']);
83 die("Sorry... couldn't tell which button you pressed");
88 tem_output('code/wfpl/metaform/main.html');
92 function field_input($type) { return $GLOBALS['types'][$type][0]; }
93 function field_format($type) { return $GLOBALS['types'][$type][1]; }
94 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
96 function get_fields() {
97 $fields_str = unix_newlines($_REQUEST['fields']);
99 $fields_str = rtrim($fields_str);
100 $fields = split("\n", $fields_str);
101 foreach($fields as $field) {
102 list($name, $type, $options) = split(' *', $field);
103 if($options) $options = split(',', $options);
104 if(!$type) $type = $name;
105 $input = field_input($type);
106 $format = field_format($type);
107 $sql = field_sql($type);
108 $ret[] = array($name, $type, $input, $format, $sql, $options);
113 # this one, that you're using to create forms
114 function set_form_action() {
115 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
116 if($action == '') $action = './';
117 tem_set('form_action', $action);
120 # perfect HTTP headers for viewing created files
121 function view_headers() {
122 header('Content-type: text/plain');
128 function make_sql() {
130 $tem->load('code/wfpl/metaform/template.sql');
131 $tem->set('form_name', $GLOBALS['form_name']);
132 $fields = get_fields();
133 foreach($fields as $field) {
134 list($name, $type, $input, $format, $sql) = $field;
136 $tem->set('name', $name);
137 $tem->set('type', $sql);
139 $tem->set('default', '0');
141 $tem->set('default', '""');
150 function view_sql() {
156 # pass false if you want to exclude the <head> and <body> tag etc.
157 function make_template($whole_file = true) {
158 $uploads_output_already = false;
160 $tem->load('code/wfpl/metaform/template.html');
161 $tem->set('form_name', $GLOBALS['form_name']);
162 $fields = get_fields();
163 foreach($fields as $field) {
164 list($name, $type, $input, $format, $sql) = $field;
165 $tem->set('name', $name);
166 $tem->set('caption', $name); # fixme
168 if($input != 'hidden') {
171 if($input == 'image' && !$uploads_output_already) {
172 $tem->sub('uploads');
173 $tem->set('enctype_attr', '" enctype="multipart/form-data');
174 $uploads_output_already = true;
177 $tem->set('name', 'save');
178 $tem->set('caption', 'Save');
185 return $tem->get('form');
189 function view_template() {
191 echo make_template();
195 function make_php() {
197 $tem->load('code/wfpl/metaform/template.php');
198 $tem->set('form_name', $GLOBALS['form_name']);
199 $fields = get_fields();
202 $always_field = false;
203 $image_included_yet = false;
204 foreach($fields as $field) {
205 list($name, $type, $input, $format, $sql) = $field;
206 if($input != 'submit') {
207 $tem->set('format', $format);
208 $tem->set('name', $name);
209 $tem->set('db_field', ''); # we don't want to use the value from last time
211 if($db_fields != '') $db_fields .= ',';
213 if($php_fields != '') $php_fields .= ', ';
214 $php_fields .= '$' . $name;
216 if($input == 'image') {
217 $tem->sub('image_upload');
218 $tem->sub('image_db');
219 if(!$image_included_yet) {
220 $tem->sub('image_include');
221 $tem->sub('upload_max');
222 $tem->sub('upload_settings');
223 $image_included_yet = true;
226 $tem->sub('formats');
227 $tem->sub('tem_sets');
229 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
230 $always_field = $name;
234 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
235 $tem->set('always_field', $always_field);
236 $tem->set('db_fields', $db_fields);
237 $tem->set('php_fields', $php_fields);
238 $tem->set('metaform_url', edit_url());
242 # make a URL for the edit page with all the fields filled in
243 function edit_url() {
245 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
246 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
247 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
251 function view_php() {
257 function make_email() {
259 $tem->load('code/wfpl/metaform/template.email.txt');
260 $tem->set('form_name', $GLOBALS['form_name']);
261 $fields = get_fields();
262 foreach($fields as $field) {
263 list($name, $type, $input, $format, $sql) = $field;
264 $tem->set('name', $name);
265 $tem->set('caption', $name); # fixme
266 if($type == 'textarea') {
267 $tem->sub('multi_line');
275 function view_email() {
283 $tem->load('code/wfpl/metaform/preview.html');
284 $tem->set('form_name', $GLOBALS['form_name']);
285 $tem->set('fields', $_REQUEST['fields']);
286 $preview_tem = new tem();
287 $preview = $preview_tem->run(make_template(false));
289 $tem->set('preview', $preview);
294 function download_tar() {
295 $name = $GLOBALS['form_name'];
297 "$name.html" => make_template(),
298 "$name.sql" => make_sql(),
299 "$name.email.txt" => make_email(),
300 "$name.php" => make_php());
301 make_wfpl_tar($name, $data);