home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / ib / setups / intrabld / data.z / COUNTER.CC < prev    next >
Text File  |  1996-12-11  |  9KB  |  169 lines

  1. /***********************************************************************\
  2. *                                                                       *
  3. * COUNTER.CC -- IntraBuilder/JavaScript Page Hit-Counter                *
  4. *                                                                       *
  5. * COUNTER.CC contains a custom component that allows the developer to   *
  6. * drop a hit-counter onto any form. This counter is automatically       *
  7. * updated every time the page is loaded.                                *
  8. *                                                                       *
  9. * Syntax:                                                               *
  10. *                                                                       *
  11. *   None                                                                *
  12. *                                                                       *
  13. * Custom Properties:                                                    *
  14. *                                                                       *
  15. *   cTableName = name of counter table                                  *
  16. *                if it does not exist, this custom component will       *
  17. *                create it the table the first time a form is loaded.   *
  18. *   cCountName = name of counter                                        *
  19. *                you can use the exact same table for multiple hit      *
  20. *                counters, and therefore should give a unique name      *
  21. *                for each counter you wish to use.                      *
  22. *   cFontSize  = font size (character string)                           *
  23. *                standard HTML font size: 1 is smallest, 7 is largest,  *
  24. *                3 is standard text (This property is needed, because   *
  25. *                the code below re-writes the text, including the       *
  26. *                HTML tag for font size ... so even if you assign a     *
  27. *                font-size in the inspector, it will be ignored or      *
  28. *                over-written) If left blank, this will default to '3'. *
  29. *   cText      = Optional text to be placed to the left of the counter  *
  30. *                number. Example: "Number of hits: "                    *
  31. *                If left blank, no text will be displayed but the       *
  32. *                value of the counter.                                  *
  33. *                                                                       *
  34. * Methods:                                                              *
  35. *                                                                       *
  36. *   None                                                                *
  37. *                                                                       *
  38. * Example:                                                              *
  39. *                                                                       *
  40. *   Right click on COUNTER.CC in the Explorer of IntraBuilder, and      *
  41. *   select "Load Custom Components". Bring up a form you wish to place  *
  42. *   a counter on in the designer.                                       *
  43. *                                                                       *
  44. *   In the control pallette, this will appear as an HTML object ("A").  *
  45. *   Drag it on to the form where you wish the counter to appear.        *
  46. *                                                                       *
  47. *   Click on the surface of the form once, and using the inspector,     *
  48. *   select the 'Events' page, click the "onServerLoad" event, and then  *
  49. *   click the tool button. In the event code, place the following:      *
  50. *                                                                       *
  51. *          form.Counter1.cTableName = "COUNTERS"                        *
  52. *          form.Counter1.cCountName = "MYFORM1"                         *
  53. *          form.Counter1.cFontSize  = "5"                               *
  54. *          form.Counter1.cText      = "Number of Hits: "                *
  55. *                                                                       *
  56. * Updated 10/18/96 by IntraBuilder Samples Group                        *
  57. * $Revision:   1.0  $                                                   *
  58. *                                                                       *
  59. * Copyright (c) 1996, Borland International, Inc.  All rights reserved. *
  60. *                                                                       *
  61. \***********************************************************************/
  62.  
  63.  
  64. class Counter(FormObj) extends HTML(FormObj) custom 
  65.    {
  66.    with (this) 
  67.       {
  68.          /* ----------------------------------------------------- */
  69.          /* all the work is done in the onServerLoad of the class */
  70.          onServerLoad = class::DoCounter;
  71.          /* default text: */
  72.          text         = "Counter"
  73.       }
  74.  
  75.          /* -----------------------------------------------------
  76.            Custom properties -- these should be changed
  77.            in the instance used on the form, rather than here --
  78.            this is where the defaults are set: */
  79.  
  80.           // name of counter table -- if it doesn't exist, we'll create it
  81.           this.cTableName   = "COUNTER";
  82.           // name of counter -- if it doesn't exist, we'll create it
  83.           this.cCountName   = "MYCOUNTER";
  84.           // fontsize -- max of 7 (largest), minimum of 1 ..., default is 3
  85.           // NOTE: this must be entered as a string
  86.           this.cFontSize = "3"
  87.           // Text displayed to left of counter value (default is blank):
  88.           this.cText     = ""
  89.      
  90.    /* ----------------------------------------------------- 
  91.       Here's where the work begins ...
  92.       ----------------------------------------------------- */
  93.    function DoCounter()
  94.       {
  95.          /* Quick summary of this function:
  96.             a) check for the table (this.cTableName)
  97.                1) if it doesn't exist, create it
  98.             b) check for the counter (this.CountName)
  99.                1) if it doesn't exist, create it (add a record
  100.                   to the table)
  101.             c) increment it
  102.             d) return the character string, with optional text (cText
  103.                property) and fontSize from cFontSize property
  104.          */
  105.  
  106.          /* Use default database object really quickly to see if it exists */
  107.          if (! _sys.databases[0].tableExists(this.cTableName))
  108.             {
  109.                /* if the table 'counter.dbf' does not exist on the server
  110.                   we need to create it, using SQL commands */
  111.                _sys.databases[0].executeSQL( 'CREATE TABLE '+this.cTableName+
  112.                                  '(countname character(15),'+
  113.                                  'counter numeric(5,0))')
  114.             }
  115.  
  116.          /* open the query, and deactivate it when done */
  117.          var qCounter    = new Query()
  118.          qCounter.sql    = "select * from "+this.cTableName
  119.          qCounter.active = true
  120.          /* Look for the counter name */
  121.          if (! qCounter.rowset.endOfSet ) // if not at endOfSet
  122.             {
  123.                // start a locate on the counter
  124.                qCounter.rowset.beginLocate()
  125.                qCounter.rowset.fields["COUNTNAME"].value=this.cCountName
  126.                qCounter.rowset.applyLocate()
  127.              }
  128.          /* if we're at the endOfSet, the name doesn't exist */
  129.          if ( qCounter.rowset.endOfSet )
  130.             {
  131.               /* so we add a new record, replace values
  132.                  and save it */
  133.               qCounter.rowset.beginAppend()
  134.               qCounter.rowset.fields["COUNTNAME"].value = this.cCountName
  135.               qCounter.rowset.fields["COUNTER"].value   = 0
  136.               qCounter.rowset.save()
  137.             }      
  138.          // Lock the row so we can deal with contention 
  139.          qCounter.rowset.lockRow()
  140.          // increment the counter
  141.          qCounter.rowset.fields["COUNTER"].value++
  142.          // save record back ...
  143.          qCounter.rowset.save()
  144.          // unlock the rowset
  145.          qCounter.rowset.unlock()
  146.          // So we can trim the spaces from the left ...:
  147.          var cCount = new StringEx()
  148.          cCount.string =""+qCounter.rowset.fields["COUNTER"].value
  149.          // deactivate the counter query object here 
  150.          qCounter.active=false
  151.          // trim out the spaces on the left:
  152.          cCount.string = cCount.leftTrim()
  153.  
  154.          // Handle font size here ... check for invalid value, and 
  155.          // reset if needed.
  156.          if (this.cFontSize < "1" || this.cFontSize > "7" || this.cFontSize=="")
  157.             {
  158.               // if invalid, set to default size of '3'
  159.               this.cFontSize = "3"
  160.             }
  161.  
  162.          // Assign the string:
  163.          this.text = this.cText+ "<FONT SIZE='"+this.cFontSize+"'>"+
  164.                      parseInt(cCount.string)+"</FONT>"
  165.       } 
  166.    } 
  167.  
  168.  
  169.