home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Javascript / InteractiveWebDesignJavascript / Scripting / cgi1 / perl / cgi-bin / order.pl < prev    next >
Encoding:
Perl Script  |  2001-07-18  |  10.6 KB  |  379 lines

  1. #!C:/perl/bin/perl
  2.  
  3. # order.pl
  4. ######################################
  5. # Order Management System
  6. #
  7. # A simple on-line database to demonstrate
  8. # the capabilities of Perl, Win32 and the Web.
  9. # Database is a text file; limited insert, 
  10. # update and delete features are implemented.
  11. #
  12. # Created: 1/2/98
  13. # Author: Michael L. Curry 
  14. # http://www.web-data.org
  15. # email: mlc@web-data.org
  16. ######################################
  17. # Copyright 1998, This script may be 
  18. # distributed and modified as needed. 
  19. # Please credit the author as appropriate.
  20. ######################################
  21.  
  22. require("cgi-lib.pl");
  23. &ReadParse(*form);
  24. $|=1;     # turn on error messages
  25. print "Content-type: text/html\n\n";
  26. $error1 = 'You must select an order before you can view details';
  27. $error2 = 'You must select an order before you can fill it';
  28.  
  29. ######################################
  30. # File references
  31. #
  32. $applet = "order.pl";
  33. $base_server_path = 'http://mcurry/iit99/perl/cgi-bin/';
  34. $base_file_path = 'c:/webshare/wwwroot/iit99/perl/cgi-bin/';
  35. $db = $base_file_path . "order.db";
  36.  
  37. #############################
  38. # Test all valid button selections
  39. #
  40. if ($form{'new.x'}) { &new; } # main page, new record button
  41.  
  42. elsif ($form{'view.x'}) # main page, view an order button
  43.     if ($form{'D1'}) {&view;} # view the order if they selected one
  44.     else {&main($error1);}  # complain if they forgot to select one
  45. }
  46. elsif ($form{'modify.x'}) { &modify; } # detail page, modify order 
  47. elsif ($form{'save.x'}) { &save; } # detail page, save new order button
  48. elsif ($form{'fill.x'}) # main page, fill the order button
  49.     if ($form{'D1'}) {&fill;} # delete the order
  50.     else {&main($error2);}  # complain if they forgot to select one
  51. else {&main();} # otherwise, give them the summary
  52.  
  53. ##################
  54. # Main: display main menu
  55. #
  56. sub main {
  57.     local ($msg) = @_;
  58.     &header('Main Menu', $msg);
  59.     &summary;
  60. }
  61. ##################
  62. # New 
  63. #
  64. sub new {
  65.     &header('Create a New Order');
  66.     &addFields('Place Order','save');
  67. }
  68. ##################
  69. # View 
  70. #
  71. sub view {
  72.     &header('View & Modify an Order');
  73.     &addFields('Update Order','modify');
  74. }
  75. ##################
  76. # Save
  77. #
  78. sub save {
  79.     &insert;
  80.     &header('Main Menu', 'Previous order inserted');
  81.     &summary;
  82. }
  83. ##################
  84. # Modify
  85. #
  86. sub modify {
  87.     &update_modify;
  88.     &header('Main Menu', 'Previous order updated');
  89.     &summary;
  90. }
  91. ##################
  92. # Fill
  93. #
  94. sub fill {
  95.     &update_delete;
  96.     &header('Main Menu','Previous order filled');
  97.     &summary;
  98. }
  99. ##################
  100. # Order Summary
  101. #
  102. sub summary {
  103.  
  104.     local $action = $base_server_path . $applet;
  105.     open (DAT, "< $db") || die "Can't open $db:$!<br>";
  106.  
  107. print <<SUMMARY1;
  108. <form action="$action" method="GET">
  109.     <input type="hidden" name="page" value="main order"><div
  110.     align="center"><center><table border="0">
  111.         <tr>
  112.             <td><input type="image" name="new"
  113.             src="../dkBluBtn.gif" align="bottom" border="0"
  114.             width="32" height="25"></td>
  115.             <td><font size="5">New Order </font></td>
  116.             <td> </td>
  117.             <td><input type="image" name="view"
  118.             src="../dkBluBtn.gif" align="bottom" border="0"
  119.             width="32" height="25"></td>
  120.             <td><font size="5">View Order</font></td>
  121.             <td> </td>
  122.             <td><input type="image" name="fill"
  123.             src="../dkBluBtn.gif" align="bottom" border="0"
  124.             width="32" height="25"></td>
  125.             <td><font size="5">Fill Order</font></td>
  126.         </tr>
  127.     </table>
  128.     </center></div><p align="center"><select name="D1" size="5">
  129. SUMMARY1
  130.  
  131.     while (<DAT>)
  132.     {
  133.     print "<option>$_</option>";
  134.     }
  135.  
  136. print <<SUMMARY2;
  137.     </select></p>
  138. </form>
  139. <div align="center"><center>
  140.  
  141. <table border="0">
  142.     <tr>
  143.         <td valign="top"><font size="5"><img src="../Fork_LiftF1D1.gif"
  144.         width="27" height="21"></font></td>
  145.         <td><font size="4">The Order Management System provides
  146.         current order status reflected in the local order
  147.         administration database. For configuration information, </font><a
  148.         href="../configur.htm"><font size="4">consult this document</font></a><font
  149.         size="4">.</font><p><font size="5"><strong>Instructions: </strong></font></p>
  150.         </td>
  151.     </tr>
  152.     <tr>
  153.         <td> </td>
  154.         <td><ol>
  155.             <li>Select New Order to create an order. </li>
  156.             <li>Select View Order and make changes to the order
  157.                 and view details.</li>
  158.             <li>Select Fill Order to delete the order.</li>
  159.         </ol>
  160.         </td>
  161.     </tr>
  162. </table>
  163. </center></div>
  164. <p><em>Developed by:</em> <a
  165. href="http://www.cs.uoregon.edu/~currym">Michael L. Curry</a> <br>
  166. ⌐ Copyright 1998, <a href="../configur.htm#About the OMS">See
  167. distribution notice</a></p>
  168. </body>
  169. </html>
  170.  
  171. SUMMARY2
  172.     close (DAT);
  173. }
  174. ##################
  175. # Add Fields
  176. #
  177. sub addFields {
  178. local ($label,$button) = @_;
  179.  
  180. if ($form{'D1'} && $form{'view.x'}) 
  181. {
  182.     ($id, $date, $part, $desc, $price, $qty, $status) = split(/::/, $form{'D1'});
  183.  
  184. }
  185. else {$id = &getindex;}
  186.  
  187. local $action = $base_server_path . $applet;
  188.  
  189. print <<FIELDS;
  190. <form action="$action" method="GET">
  191.     <div align="center"><center><table border="0">
  192.         <tr>
  193.             <td><font size="4">Order Id</font></td>
  194.             <td><font size="4"><input type="text" size="10"
  195.             name="T1" value="$id"></font></td>
  196.             <td><font size="4">Date Created</font></td>
  197.             <td><font size="4"><input type="text" size="10"
  198.             name="T2" value="$date"></font></td>
  199.             <td> </td>
  200.             <td> </td>
  201.         </tr>
  202.         <tr>
  203.             <td><font size="4">Part Number</font></td>
  204.             <td><font size="4"><input type="text" size="10"
  205.             name="T3" value="$part"></font></td>
  206.             <td><font size="4">Description</font></td>
  207.             <td colspan="3"><font size="4"><input type="text"
  208.             size="20" name="T4" value="$desc"></font></td>
  209.             <td> </td>
  210.             <td> </td>
  211.         </tr>
  212.         <tr>
  213.             <td><font size="4">Unit Price</font></td>
  214.             <td><font size="4"><input type="text" size="10"
  215.             name="T5" value="$price"></font></td>
  216.             <td><font size="4">Quantity</font></td>
  217.             <td><font size="4"><input type="text" size="5"
  218.             name="T6" value="$qty"></font></td>
  219.             <td> </td>
  220.             <td> </td>
  221.         </tr>
  222.         <tr>
  223.             <td><font size="4">Status</font></td>
  224.             <td colspan="3"><font size="4"><input type="text"
  225.             size="30" name="T7" value="$status"></font></td>
  226.             <td> </td>
  227.             <td> </td>
  228.             <td> </td>
  229.             <td> </td>
  230.         </tr>
  231.         <tr>
  232.             <td><p align="right"><input type="image" name="$button"
  233.             value="save new" src="../dkBluBtn.gif" align="bottom" border="0"
  234.             width="32" height="25"></p>
  235.             </td>
  236.             <td valign="top"><font size="4"><strong>$label</strong></font></td>
  237.             <td> </td>
  238.             <td> </td>
  239.             <td> </td>
  240.             <td> </td>
  241.             <td> </td>
  242.             <td> </td>
  243.         </tr>
  244.     </table>
  245.     </center></div><p><font size="4"> </font></p>
  246. </form>
  247. </body>
  248. </html>
  249.  
  250. FIELDS
  251. }
  252. ##################
  253. # Header
  254. #
  255. sub header {
  256.     local ($msg1, $msg2) = @_;
  257.  
  258. print <<HEADER;
  259. <html>
  260.  
  261. <head>
  262. <title>New Order</title>
  263. </head>
  264.  
  265. <body background="../Lava.gif" bgcolor="#FFFFFF" text="#000000"
  266. bgproperties="fixed">
  267.  
  268. <table border="0" width="100%">
  269.     <tr>
  270.         <td rowspan="2"><img src="../roller.gif" width="158"
  271.         height="117"></td>
  272.         <td rowspan="2"><p align="center"><font color="#008080"
  273.         size="6"><strong>Order Management System</strong></font></p>
  274.         </td>
  275.     </tr>
  276. </table>
  277.  
  278. <center><p><font color="#800040" size="4"><strong>$msg2</strong></font></p>
  279. HEADER
  280. }
  281. ##################
  282. # Insert record into Database 
  283. #
  284. sub insert {
  285.  
  286.     local ($line);
  287.     $line = $form{'T1'}.'::'.$form{'T2'}.'::'.$form{'T3'}.'::'.$form{'T4'}.'::'.$form{'T5'}.'::'.$form{'T6'}.'::'.$form{'T7'}."\n";
  288.     open (DAT, ">> $db") || die "Can't open $db:$!<br>";
  289.     print DAT $line;
  290.     close(DAT);
  291. }
  292.  
  293. ##################
  294. # Update Database by modifying record
  295. #
  296. sub update_modify {
  297.  
  298.     local ($tempDB, $line, $pat); # vars
  299.     $tempDB = $db; 
  300.     $tempDB =~ s/\.db/\.tmp/;
  301.     #
  302.     # build the new line
  303.     $pat = $form{'T1'};
  304.     $line = $form{'T1'}.'::'.$form{'T2'}.'::'.$form{'T3'}.'::'.$form{'T4'}.'::'.$form{'T5'}.'::'.$form{'T6'}.'::'.$form{'T7'}."\n";
  305.     
  306.     #print "Pat: $pat <br>";
  307.     #
  308.     # write new line & other lines to temp db
  309.     #
  310.     open (DAT, "< $db") || die "Can't open $db:$!<br>"; #read from
  311.     open (TMP, "> $tempDB") || die "Can't open $tempDB:$!<br>"; #write to
  312.     while (<DAT>)
  313.     {
  314.     if ($_ =~ /^\s*$pat\:\:/)     
  315.     { print TMP $line; }
  316.     else { print TMP $_; } 
  317.     }
  318.     close (DAT); close (TMP);
  319.     #
  320.     # Now over write the db with the temp db
  321.     #
  322.     open (DAT, "> $db") || die "Can't open $db:$!<br>"; #write to
  323.     open (TMP, "< $tempDB") || die "Can't open $tempDB:$!<br>"; #read from
  324.     while (<TMP>) {print DAT $_;}
  325.     close (DAT); close (TMP);
  326. }
  327. ##################
  328. # Update Database by deleting record
  329. #
  330. sub update_delete {
  331.  
  332.     local ($tempDB, $line, $pat); 
  333.     $tempDB = $db;
  334.     $tempDB =~ s/\.db/\.tmp/;
  335.     $pat = $form{'D1'};
  336.  
  337.     # print "Pat: $pat <br>";
  338.     #
  339.     # eliminate line from temp db
  340.     #
  341.     open (DAT, "< $db") || die "Can't open $db:$!<br>"; #read from
  342.     open (TMP, "> $tempDB") || die "Can't open $tempDB:$!<br>"; #write to
  343.     while (<DAT>)
  344.     {
  345.     if ($_ =~ /$pat/) {
  346.         #print $_, "matched", $pat, "<br>";
  347.         next; # delete line
  348.     }
  349.     else {print TMP $_;}
  350.     }
  351.     close (DAT); close (TMP);
  352.     #
  353.     # Now overwrite the db with the temp db
  354.     #
  355.     open (DAT, "> $db") || die "Can't open $db:$!<br>"; #write to
  356.     open (TMP, "< $tempDB") || die "Can't open $tempDB:$!<br>"; #read from
  357.     while (<TMP>) {print DAT $_;}
  358.     close (DAT); close (TMP);
  359. }
  360. ##################
  361. # get index: returns new index number
  362. #
  363. sub getindex {
  364.     local (%hash, @tmp, $key, $lastID);
  365.     open (DAT, "< $db") || die "Can't open $db:$!<br>";
  366.     while (<DAT>) 
  367.     {
  368.     @tmp = split (/::/,$_);
  369.     $hash{$tmp[0]}=$tmp[3];
  370.     }
  371.     close (DAT);
  372.     foreach $key (sort(keys(%hash))) {$lastID = $key}
  373.     $lastID= $lastID + 1;
  374.     return ($lastID);
  375. }
  376.