home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / trashcan.prg < prev    next >
Text File  |  1993-10-14  |  4KB  |  137 lines

  1. /*
  2.  * File......: TRASHCAN.PRG
  3.  * Author....: Martin Colloby
  4.  * BBS.......: The Dark Knight Returns
  5.  * Net/Node..: 050/069
  6.  * User Name.: Martin Colloby
  7.  * Date......: 18/4/93
  8.  * Revision..: 1.0
  9.  *
  10.  * This is an original work by Martin Colloby and is placed in the public
  11.  * domain.
  12.  *
  13.  * Modification history:
  14.  * ---------------------
  15.  *
  16.  * $Log$
  17.  *
  18.  */
  19.  
  20.  
  21. /*  $DOC$
  22.  *  $FUNCNAME$
  23.  *      GT_TRASHCAN()
  24.  *  $CATEGORY$
  25.  *      General
  26.  *  $ONELINER$
  27.  *      Copy a record to a trashcan and then delete it
  28.  *  $SYNTAX$
  29.  *      GT_TrashCan( cTable , lSecond , cReason , cDesc )
  30.  *  $ARGUMENTS$
  31.  *      cTable    - Table to be audited
  32.  *      lSecond   - If .T., use two indexes
  33.  *      cReason   - Reason for deletion ( code - C2 )
  34.  *      cDesc     - Reason if code is other - C40
  35.  *  $RETURNS$
  36.  *      NIL
  37.  *  $DESCRIPTION$
  38.  *      The current record in the table cTable is appended to
  39.  *      TABLES\TRASHCAN\cTable
  40.  *  $EXAMPLES$
  41.  *
  42.  *  $SEEALSO$
  43.  *
  44.  *  $INCLUDE$
  45.  *      GT_LIB.CH
  46.  *  $END$
  47.  */
  48. *
  49. #include "GT_LIB.ch"
  50.  
  51. FUNCTION GT_TrashCan( cTable , lSecond , cReason , cDesc )
  52.  
  53. /*****************************************************************************
  54.  Purpose - Remove the current record from cTable and place it in a trash can
  55.  Returns - None
  56.  Author  - Log
  57.  Created - 22/10/92
  58.  Edited  - 4/11/92 - Log - Now adds a record to the audit trail marking the
  59.                            record as trashed.
  60. ******************************************************************************
  61.  Parameters - cTable    - Table to be audited
  62.               lSecond   - If .T., use two indexes
  63.               cReason   - Reason for deletion ( code - C2 )
  64.               cDesc     - Reason if code is other - C40
  65.  Locals     - aFields   - Array to hold fields in cTable
  66.               nCount    - Counting variable
  67.  PUBLICS    - cUserName - Name of user performing operation
  68. ******************************************************************************
  69.  N.B. Each application must maintain a table of cReason codes in the format :
  70.  
  71.             REASON C 2
  72.             DESC   C 40
  73. *****************************************************************************/
  74.  
  75. LOCAL aFields  := {}
  76. LOCAL aIndexes := {}
  77. LOCAL nCount   := 0
  78.  
  79. // Select the table
  80. SELECT ( cTable )
  81.  
  82. // Try to lock the record
  83. IF GT_RecLock( 0 )
  84.     // Copy the contents of the data table into the array
  85.     FOR nCount := 1 TO FCOUNT()
  86.         AADD( aFields , FIELDGET( nCount ) )
  87.     NEXT
  88.  
  89.     aIndexes := { "trashcan\" + cTable }
  90.  
  91.     IF lSecond
  92.         AADD( aIndexes , "trashcan\" + ALLTRIM( LEFT( cTable , 7 ) ) + "1" )
  93.     ENDIF
  94.  
  95.     // Open the Trashcan table
  96.     GT_UseDbf( "trashcan\" + cTable , aIndexes , "trashcan" , .F. , .F. , .F. , 5 )
  97.  
  98.     // Add a blank record
  99.     GT_AddRec( 5 )
  100.  
  101.     // Update the key fields
  102.     REPLACE trashcan->trashdate WITH DATE()
  103.     REPLACE trashcan->trashtime WITH TIME()
  104.     REPLACE trashcan->trashuser WITH cUserName
  105.     REPLACE trashcan->trashreas WITH cReason
  106.     REPLACE trashcan->trashdesc WITH cDesc
  107.  
  108.     // This code block stuffs the contents of the array a_fields into the current
  109.     // table, but places them five fields to the right of their ordinal position
  110.     // in the array - this gets them past the first five key fields
  111.     AEVAL( aFields , { |value, count| FIELDPUT( count + 5 , value ) } )
  112.  
  113.     // Unlock the trashcan table record
  114.     UNLOCK
  115.  
  116.     // Release the trashcan table
  117.     SELECT "trashcan"
  118.     USE
  119.  
  120.     // Go back to the source table
  121.     SELECT ( cTable )
  122.  
  123.     // Audit this operation
  124.     GT_Audit( cTable , A_TRASHCAN , .F. )
  125.  
  126.     // Delete the record, and then unlock it
  127.     DELETE
  128.     SKIP
  129.     UNLOCK
  130.     DBCOMMITALL()
  131. ELSE
  132.     GT_Warning( { "Couldn't lock record to trash" } )
  133. ENDIF
  134.  
  135. RETURN NIL
  136. *
  137.