JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform codegen: use tem_auto_unset()
[wfpl.git] / metaform / template.php
index b591fbc..7b278af 100644 (file)
 
 
 # SETUP
-<!--~opt_email_1 start~-->
+<!--~opt_email {~-->
 # To send results by e-mail, all you have to do is set your e-mail address here:
-$GLOBALS['~form_name~_form_recipient'] = "fixme@example.com";
-<!--~end~--><!--~opt_db_1 start~-->
-# To save results to a database, you'll need to create the ~form_name~ table
-# (the file ~form_name~.sql should help with this), and create the file
+$GLOBALS['~file_name~_form_recipient'] = 'fixme@example.com';
+<!--~}~--><!--~opt_db {~-->
+# To save results to a database, you'll need to create the ~table_name~ table
+# (the file ~file_name~.sql should help with this), and create the file
 # 'code/db_connect.php' which calls db_connect() see:
 # code/wfpl/examples/db_connect.php
 #
 # if you rename any of the database fields, you'll need to update this:
 
-define('~form_name.upper~_DB_FIELDS', '~db_fields~');
-<!--~end~--><!--~upload_settings start~-->
+define('~file_name upper~_DB_FIELDS', '~db_fields~');
+<!--~}~--><!--~upload_settings {~-->
 # Set this to the path to your uploads directory. It can be relative to the
 # location of this script. IT MUST END WITH A SLASH
 $GLOBALS['upload_directory'] = 'uploads/';
-<!--~end~--><!--~opt_http_pass_1 start~-->
-# Define the username and password required to view this form:
-define('AUTH_REALM', '~form_name~ administration area');
-define('AUTH_USER', 'fixme');
-define('AUTH_PASS', 'fixme');
-<!--~end~-->
+<!--~}~--><!--~file_settings {~-->
+$GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.txt'; # comment this out to use uploader's filename
+<!--~}~--><!--~image_settings {~-->
+$GLOBALS['~name~_max_width'] = '400';
+$GLOBALS['~name~_max_height'] = '400';<!--~thumb_settings {~-->
+$GLOBALS['~name~_thumb_max_width'] = '70';
+$GLOBALS['~name~_thumb_max_height'] = '70';
+$GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
+<!--~}~--><!--~}~-->
 
 require_once('code/wfpl/template.php');
 require_once('code/wfpl/format.php');
 require_once('code/wfpl/messages.php');
-require_once('code/wfpl/email.php');<!--~opt_db_2 start~-->
-require_once('code/db_connect.php');<!--~end~--><!--~image_include start~-->
-require_once('code/wfpl/upload.php');<!--~end~-->
-
-function ~form_name~_get_fields() {<!--~formats start~-->
-       $~name~ = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra start~-->, '~name~'<!--~end~-->);<!--~end~--><!--~image_upload start~-->
+require_once('code/wfpl/email.php');<!--~opt_db {~-->
+require_once('code/db_connect.php');<!--~}~--><!--~uploads_include {~-->
+require_once('code/wfpl/upload.php');<!--~}~-->
+
+function ~file_name~_get_fields() {
+       $data = array();
+<!--~formats {~-->
+       $data['~name~'] = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra {~-->, '~name~'<!--~}~-->);<!--~}~--><!--~image_upload {~-->
        if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
-               $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
+               $data['~name~'] = convert_uploaded_image('~name~', $GLOBALS['upload_directory'] . $GLOBALS['~name~_file_name'], $GLOBALS['~name~_max_width'], $GLOBALS['~name~_max_height']<!--~thumb_upload_params {~-->, $GLOBALS['~name~_thumb_max_width'], $GLOBALS['~name~_thumb_max_height']<!--~}~-->);
        } else {
                if($_REQUEST['delete_~name~'] == 'Yes') {
-                       $~name~ = '';
+                       $data['~name~'] = '';
                } else {
-                       $~name~ = format_filename($_REQUEST['old_~name~']);
+                       $data['~name~'] = format_image_w_h<!--~thumb_w_h {~-->_thumb_w_h<!--~}~-->($_REQUEST['old_~name~']);
                }
-       }<!--~end~-->
-
-       ~form_name~_tem_sets(~php_fields~);
+       }<!--~}~--><!--~file_upload {~-->
+       $~name~_filename_tmp = $GLOBALS['~name~_file_name'];
+       if(!$~name~_filename_tmp) {
+               $~name~_filename_tmp = format_filename($_REQUEST['~name~']);
+       }
+       if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
+               $data['~name~'] = save_uploaded_file('~name~', $GLOBALS['upload_directory'] . $~name~_filename_tmp);
+       } else {
+               if($_REQUEST['delete_~name~'] == 'Yes') {
+                       $data['~name~'] = '';
+               } else {
+                       $data['~name~'] = format_path($_REQUEST['old_~name~']);
+               }
+       }<!--~}~-->
 
-       return array(~php_fields~);
+       return $data;
 }
 
-function ~form_name~_tem_sets(~php_fields~) {<!--~tem_sets start~-->
-       tem_set('~name~', $~name~);<!--~end~-->
-}
-<!--~opt_listing_2 start~-->
+<!--~opt_listing {~-->
 # You may pass a "where clause" for the db query.
-function ~form_name~_display_listing($where = 'order by ~always_field~ limit 100') {
-       $rows = db_get_rows('~form_name~', 'id,~always_field~', $where);
+function ~file_name~_display_listing($where = 'order by ~always_field~ limit 100') {
+       $rows = db_get_assocs('~table_name~', 'id<!--~listing_fields_1 {~-->,~name~<!--~}~-->', $where);
        if($rows == false || count($rows) == 0) {
-               tem_show('empty_listing');
-               tem_show('listings');
-               return false;
+               tem_set('listings', array('empty_listing' => true));
+               return;
        }
 
-       foreach($rows as $row) {
-               list($id, $~always_field~) = $row;
-               tem_set('id', $id);
-               if($~always_field~ == '') {
-                       $~always_field~ = '--';
+       # make sure there's something clickable
+       foreach($rows as &$row) {
+               if($row['~always_field~'] == '') {
+                       $row['~always_field~'] = '--';
                }
-               tem_set('~always_field~', $~always_field~);
-               tem_show('listing_row');
        }
-       tem_show('populated_listing');
-       tem_show('listings');
+       tem_set('listings', array(
+               'populated_listing' => true,
+               'rows' => $rows));
        return true;
 }
-<!--~end~-->
-function ~form_name~_main() {<!--~opt_display_1 start~-->
-       if(isset($_REQUEST['~form_name~_id'])) {
-               $ret = ~form_name~_display_main();
-               if($ret) {
-                       return $ret;
-               }
-               tem_show('display_body');
-       } else {
-               $ret = ~form_name~_edit_main();
-               if($ret) {
-                       return $ret;
-               }
-               tem_show('edit_body');
+<!--~}~-->
+function ~file_name~_main() {<!--~opt_pass {~-->
+       if(!logged_in_as_admin()) {
+               $GLOBALS['url'] = this_url();
+               return 'admin_login';
        }
-<!--~end~--><!--~opt_display_1_else start~-->
-       $ret = _~form_name~_main();
-       if($ret) {
-               return $ret;
+<!--~}~--><!--~opt_display {~-->
+       if(isset($_REQUEST['~file_name~_id'])) {
+               return ~file_name~_display_main();
+       } else {
+               return ~file_name~_edit_main();
        }
-<!--~end~-->
-       # sections displayed with tem_show() will be coppied to the main template if you have one.
-       tem_show('main_body');
-}<!--~opt_display_2 start~-->
-
-function ~form_name~_display_main() {
-       $id = format_int($_REQUEST['~form_name~_id']);
-       unset($_REQUEST['~form_name~_id']);
+}
+
+function ~file_name~_display_main() {
+       $id = format_int($_REQUEST['~file_name~_id']);
+       unset($_REQUEST['~file_name~_id']);
        if(!$id) {
                message('Error: Broken link');
-               return './~form_name~';
+               return './~file_name~';
        }
-       $row = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $id);
+       $row = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id);
        if(!$row) {
-               message('Error: Not found');
-               return './~form_name~';
+               message('Error: ~singular cap~ not found');
+               return './~file_name~';
        }
-       list(~php_fields~) = $row;
-       ~form_name~_tem_sets(~php_fields~);
-       tem_set('id', $id);
+       tem_set('display', $row);
 }
 
-function ~form_name~_edit_main() {<!--~end~--><!--~opt_display_2_else start~-->
-
-
-function _~form_name~_main() {<!--~end~--><!--~opt_http_pass_2 start~-->
-       # To remove password protection, just delete this block:
-       if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != AUTH_USER || $_SERVER['PHP_AUTH_PW'] != AUTH_PASS) {
-               header('WWW-Authenticate: Basic realm="' . AUTH_REALM . '"');
-               header('HTTP/1.0 401 Unauthorized');
-               echo '401 Unauthorized';
-               exit;
-       }
-       <!--~end~--><!--~pulldowns start~-->
+function ~file_name~_edit_main() {<!--~}~--><!--~pulldowns {~-->
        pulldown('~name~', array('option 1', 'option 2', 'option 3'));
-       <!--~end~--><!--~opt_db_3 start~-->
-       $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
-       unset($_REQUEST['~form_name~_edit_id']);
+<!--~}~--><!--~opt_db {~-->
+       $edit_id = format_int($_REQUEST['~file_name~_edit_id']);
+       unset($_REQUEST['~file_name~_edit_id']);
        if($edit_id) {
                # add hidden field for database id of row we're editing
-               tem_set('~form_name~_edit_id', $edit_id);
-               tem_show('editing');
+               tem_set('~file_name~_edit_id', $edit_id);
+               tem_set('editing', 'show');
+               tem_set('edit_msg', 'show');
        }
 
-       $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
-       unset($_REQUEST['~form_name~_delete_id']);
+       $delete_id = format_int($_REQUEST['~file_name~_delete_id']);
+       unset($_REQUEST['~file_name~_delete_id']);
        if($delete_id) {
-               db_delete('~form_name~', 'where id=%i', $delete_id);
-               message('Entry deleted.');
+               db_delete('~table_name~', 'where id=%i', $delete_id);
+               message('~singular cap~ deleted.');
 
-               return './~form_name~';
+               return './~file_name~';
        }
 
-       if(!$edit_id) {<!--~opt_listing_1 start~-->
-               if(!isset($_REQUEST['~form_name~_new']) && !isset($_REQUEST['~always_field~'])) {
-                       ~form_name~_display_listing();
+       if(!$edit_id) {<!--~opt_listing {~-->
+               if(!isset($_REQUEST['~file_name~_new']) && !isset($_REQUEST['~always_field~'])) {
+                       ~file_name~_display_listing();
                        return;
                }
-               <!--~end~-->
-               tem_show('new_msg');
-       }<!--~end~-->
-
-       if(isset($_REQUEST['~always_field~'])) {
-               list(~php_fields~) = ~form_name~_get_fields();
+<!--~}~-->
+               tem_set('new_msg', 'show');
+       }
+<!--~}~-->
+       if(isset($_POST['~always_field~'])) {
+               $data = ~file_name~_get_fields();
 
-               if("you're happy with the POSTed values") {<!--~opt_db_4 start~-->
+               if("you're happy with the POSTed values") {<!--~opt_db {~-->
                        if($edit_id) {
-                               db_update('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id);
-                               message('Updated.');
+                               db_update_assoc('~table_name~', $data, 'where id=%i', $edit_id);
+                               message('~singular cap~ updated.');
                        } else {
-                               db_insert('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~);
-                               message('Saved.');
-                       }<!--~end~--><!--~opt_email_2 start~-->
-                       if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
-                               $to = $GLOBALS['~form_name~_form_recipient'];
-                               $from = $to;
-                               $reply_to = '';
+                               db_insert_assoc('~table_name~', $data);
+                               message('~singular cap~ saved.');
+                       }<!--~}~--><!--~opt_email {~-->
+                       if($GLOBALS['~file_name~_form_recipient'] != 'fixme@example.com') {
+                               $to = $GLOBALS['~file_name~_form_recipient'];
+                               $from = 'noreply@~this_domain~';
+                               $reply_to = $to;
                                if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
                                        $reply_to = $_REQUEST['email'];
                                        if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
                                                $reply_to = "$_REQUEST[name] <$reply_to>";
                                        }
                                }
-                               $subject = '~form_name~ form submitted';
-                               $message = tem_run('~form_name~.email.txt');
+                               $subject = '~plural cap~ form submitted';
+                               $email_template = new tem();
+                               $email_template->load('~file_name~.email.txt');
+                               $email_template->sets($data);
+                               $message = $email_template->run();
                                $cc = '';
                                $bcc = '';
                                if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
@@ -195,32 +185,28 @@ function _~form_name~_main() {<!--~end~--><!--~opt_http_pass_2 start~-->
                                } else {
                                        message('Message sent');
                                }
-                       }<!--~end~-->
+                       }<!--~}~-->
                        if($error !== true) {
-                               return './~form_name~'; # FIXME is this the page you want to go to after successful form submission?
+                               return './~file_name~'; # FIXME is this the page you want to go to after successful form submission?
                        }
                }
-               # otherwise, we display the form again. ~form_name~_get_fields() has
+               # otherwise, we display the form again. ~file_name~_get_fields() has
                # already put the posted values back into the template engine, so they will
                # show up in the form fields. You should add some message asking people to
-               # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
+               # fix their entry in whatever way you require.<!--~opt_db {~-->
        } elseif($edit_id) {
                # we've recieved an edit id, but no data. So we grab the values to be edited from the database
-               list(~php_fields~) = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $edit_id);
-               ~form_name~_tem_sets(~php_fields~);<!--~end~-->
+               $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $edit_id);<!--~}~-->
        } else {
                # form not submitted, you can set default values like so:
-               #tem_set('~always_field~', 'Yes');
-       }<!--~upload_max start~-->
-
-       tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
+               #$data = array('~always_field~' => 'Yes');
+               $data = array();
+       }<!--~upload_max {~-->
 
-       # this has to be later in the file because it requres that ~always_field~ be set already
-       if($edit_id) {
-               tem_show('edit_msg');
-       }
+       tem_set('upload_max_filesize', upload_max_filesize());<!--~}~-->
 
-       tem_show('form');
+       tem_set('form', $data);<!--~show_extra_headers {~-->
+       tem_set('extra_headers', 'show');<!--~}~-->
 }
 
 ?>