home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************\
- * *
- * COUNTER.CC -- IntraBuilder/JavaScript Page Hit-Counter *
- * *
- * COUNTER.CC contains a custom component that allows the developer to *
- * drop a hit-counter onto any form. This counter is automatically *
- * updated every time the page is loaded. *
- * *
- * Syntax: *
- * *
- * None *
- * *
- * Custom Properties: *
- * *
- * cTableName = name of counter table *
- * if it does not exist, this custom component will *
- * create it the table the first time a form is loaded. *
- * cCountName = name of counter *
- * you can use the exact same table for multiple hit *
- * counters, and therefore should give a unique name *
- * for each counter you wish to use. *
- * cFontSize = font size (character string) *
- * standard HTML font size: 1 is smallest, 7 is largest, *
- * 3 is standard text (This property is needed, because *
- * the code below re-writes the text, including the *
- * HTML tag for font size ... so even if you assign a *
- * font-size in the inspector, it will be ignored or *
- * over-written) If left blank, this will default to '3'. *
- * cText = Optional text to be placed to the left of the counter *
- * number. Example: "Number of hits: " *
- * If left blank, no text will be displayed but the *
- * value of the counter. *
- * *
- * Methods: *
- * *
- * None *
- * *
- * Example: *
- * *
- * Right click on COUNTER.CC in the Explorer of IntraBuilder, and *
- * select "Load Custom Components". Bring up a form you wish to place *
- * a counter on in the designer. *
- * *
- * In the control pallette, this will appear as an HTML object ("A"). *
- * Drag it on to the form where you wish the counter to appear. *
- * *
- * Click on the surface of the form once, and using the inspector, *
- * select the 'Events' page, click the "onServerLoad" event, and then *
- * click the tool button. In the event code, place the following: *
- * *
- * form.Counter1.cTableName = "COUNTERS" *
- * form.Counter1.cCountName = "MYFORM1" *
- * form.Counter1.cFontSize = "5" *
- * form.Counter1.cText = "Number of Hits: " *
- * *
- * Updated 10/18/96 by IntraBuilder Samples Group *
- * $Revision: 1.0 $ *
- * *
- * Copyright (c) 1996, Borland International, Inc. All rights reserved. *
- * *
- \***********************************************************************/
-
-
- class Counter(FormObj) extends HTML(FormObj) custom
- {
- with (this)
- {
- /* ----------------------------------------------------- */
- /* all the work is done in the onServerLoad of the class */
- onServerLoad = class::DoCounter;
- /* default text: */
- text = "Counter"
- }
-
- /* -----------------------------------------------------
- Custom properties -- these should be changed
- in the instance used on the form, rather than here --
- this is where the defaults are set: */
-
- // name of counter table -- if it doesn't exist, we'll create it
- this.cTableName = "COUNTER";
- // name of counter -- if it doesn't exist, we'll create it
- this.cCountName = "MYCOUNTER";
- // fontsize -- max of 7 (largest), minimum of 1 ..., default is 3
- // NOTE: this must be entered as a string
- this.cFontSize = "3"
- // Text displayed to left of counter value (default is blank):
- this.cText = ""
-
- /* -----------------------------------------------------
- Here's where the work begins ...
- ----------------------------------------------------- */
- function DoCounter()
- {
- /* Quick summary of this function:
- a) check for the table (this.cTableName)
- 1) if it doesn't exist, create it
- b) check for the counter (this.CountName)
- 1) if it doesn't exist, create it (add a record
- to the table)
- c) increment it
- d) return the character string, with optional text (cText
- property) and fontSize from cFontSize property
- */
-
- /* Use default database object really quickly to see if it exists */
- if (! _sys.databases[0].tableExists(this.cTableName))
- {
- /* if the table 'counter.dbf' does not exist on the server
- we need to create it, using SQL commands */
- _sys.databases[0].executeSQL( 'CREATE TABLE '+this.cTableName+
- '(countname character(15),'+
- 'counter numeric(5,0))')
- }
-
- /* open the query, and deactivate it when done */
- var qCounter = new Query()
- qCounter.sql = "select * from "+this.cTableName
- qCounter.active = true
- /* Look for the counter name */
- if (! qCounter.rowset.endOfSet ) // if not at endOfSet
- {
- // start a locate on the counter
- qCounter.rowset.beginLocate()
- qCounter.rowset.fields["COUNTNAME"].value=this.cCountName
- qCounter.rowset.applyLocate()
- }
- /* if we're at the endOfSet, the name doesn't exist */
- if ( qCounter.rowset.endOfSet )
- {
- /* so we add a new record, replace values
- and save it */
- qCounter.rowset.beginAppend()
- qCounter.rowset.fields["COUNTNAME"].value = this.cCountName
- qCounter.rowset.fields["COUNTER"].value = 0
- qCounter.rowset.save()
- }
- // Lock the row so we can deal with contention
- qCounter.rowset.lockRow()
- // increment the counter
- qCounter.rowset.fields["COUNTER"].value++
- // save record back ...
- qCounter.rowset.save()
- // unlock the rowset
- qCounter.rowset.unlock()
- // So we can trim the spaces from the left ...:
- var cCount = new StringEx()
- cCount.string =""+qCounter.rowset.fields["COUNTER"].value
- // deactivate the counter query object here
- qCounter.active=false
- // trim out the spaces on the left:
- cCount.string = cCount.leftTrim()
-
- // Handle font size here ... check for invalid value, and
- // reset if needed.
- if (this.cFontSize < "1" || this.cFontSize > "7" || this.cFontSize=="")
- {
- // if invalid, set to default size of '3'
- this.cFontSize = "3"
- }
-
- // Assign the string:
- this.text = this.cText+ "<FONT SIZE='"+this.cFontSize+"'>"+
- parseInt(cCount.string)+"</FONT>"
- }
- }
-
-
-