;;; products.mhtml: -*- Meta-HTML -*- 

;;;  Copyright (c) 1996 Universal Access Inc.
;;;  Author: Henry Minsky (hqm@ua.com) Sat Jul 27 14:09:04 1996.

;;; Define accessors for product database records

;;; The real pathname name of our database file
<set-var dbfile =
  <concat <get-var mhtml::include-prefix mhtml::relative-prefix>
	  /products.db>>

;;; For this demo, if the product database does not exist, we will
;;; just make a predefined one right now. This would not be done in a
;;; production environment, of course.
<when <not <get-file-properties "<get-var dbfile>">>>
  Initializing cake product database...
  <include seed-database.mhtml>
  <if <get-file-properties "<get-var dbfile>">
      "done."
    "error, could not create database file!">
</when>

;;; Looks up a product using a product-id key
<define-tag get-product key>
  <set-var item::product-id=<get-var key>>
  <with-open-database db <get-var dbfile> mode=read>
    <database-load-record db <get-var key> package=item>
  </with-open-database>
</define-tag>

;;; Prints an HTML table of the product database. 
;;; Put in buttons to go to the modify-products page.

<define-tag display-products>
  <with-open-database pdb <get-var dbfile> mode=read>
    <form method=POST action="modify-products.mhtml">
      <table border=1>
	<tr>
	  <th>Edit</th>
	  <th>Item</th>
	  <th>Description</th>
	  <th>Qty</th>
	  <th>Price</th>
	  <th>Next Delivery</th>
	</tr>

	<database-query pdb true 
	  format=
	  <prog
	    <tr>
	    <td><input type=radio name=key value="<get-var key>"></td>
	    <td><get-var key></td>
	    <td><a href="<get-var url>"><get-var title></a></td>
	    <td><get-var inventory></td>
	    <td><get-var price></td>
	    <td><get-var delivery></td>
	    </tr>
	    >
		  >
      </table>
      <input type=submit name=edit-product value="Edit Product">
    </form>
  </with-open-database>
</define-tag>
	  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code to generate forms which can be used to modify products
;;; in the database.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Display a nice HTML <input> field
<define-tag input-field name pretty-name value>
  <tr>
    <td><get-var pretty-name>: </td>
    <td>
      <input type=text maxwidth=80 size=32
	     name=<get-var name> value="<get-var value>"><br>
      </td>
    </tr>
</define-tag>

;;; Display an HTML form to modify a product
<define-tag display-product-fields dbvar id>
  <set-var loaded=<database-load-record <get-var dbvar> <get-var id>>>
  <input-field key       "Product ID"    <get-var id>>
  <input-field url       "URL"           <get-var url>>
  <input-field picture   "Image File"    <get-var picture>>
  <input-field title     "Product Title" <get-var title>>
  <input-field inventory "Qty in stock"  <get-var inventory>>
  <input-field color     "Color"         <get-var color>>
  <input-field price     "Price"         <get-var price>>
  <input-field delivery  "Delivery Date" <get-var delivery>>
</define-tag>

<define-tag display-product-form id>
  <with-open-database pdb <get-var dbfile>>
    <form method=POST action=<get-var mhtml::location>>
      <table border=1>
	<display-product-fields pdb <get-var id>>
	<tr>
	  <td>
	    <input type=submit name=Modify value="Modify">
	    <input type=submit name=Delete value="Delete">
	    <input type=submit name=Cancel value="Cancel">
	    
	  </td>
	</tr>
      </table>
    </form>
  </with-open-database>
</define-tag>
