function cloneUTable($attr)
 {
     //---------------------------------------------------------------------
     // destination table must exist prior to call
     //---------------------------------------------------------------------
     $csrcTable = new muser_fields_create();
     $cdestTable = new muser_fields_create();
     // load source table record
     $csrcTable->lTableID = $attr->lSrcTableID;
     $csrcTable->loadTableViaTableID(false);
     $srcTable =& $csrcTable->userTables[0];
     // load source table fields
     $csrcTable->loadTableFields(true);
     $srcFields =& $csrcTable->fields;
     if ($csrcTable->lNumFields == 0) {
         return;
     }
     // nothing to clone
     // the destination table
     $cdestTable->lTableID = $attr->lDestTableID;
     $cdestTable->strENPTableName = $cdestTable->strGenUF_TableName($attr->lDestTableID);
     $cdestTable->fields = array();
     $cdestTable->fields[0] = new stdClass();
     $destField =& $cdestTable->fields[0];
     foreach ($csrcTable->fields as $sfield) {
         $lSrcFieldID = $sfield->pff_lKeyID;
         $destField->enumFieldType = $enumFieldType = $sfield->enumFieldType;
         $destField->pff_strFieldNameUser = $sfield->pff_strFieldNameUser;
         $destField->pff_bCheckDef = $sfield->pff_bCheckDef;
         $destField->pff_curDef = $sfield->pff_curDef;
         $destField->pff_strTxtDef = $sfield->pff_strTxtDef;
         $destField->pff_lDef = $sfield->pff_lDef;
         $destField->pff_lCurrencyACO = $sfield->pff_lCurrencyACO;
         $destField->pff_bHidden = $sfield->pff_bHidden;
         $destField->pff_bRequired = $sfield->pff_bRequired;
         $destField->bPrefilled = $sfield->bPrefilled;
         $destField->strFieldNotes = $sfield->strFieldNotes;
         $lDestFieldID = $cdestTable->addNewField();
         // map the sort order
         $cdestTable->setUFieldSortIDX($lDestFieldID, $sfield->lSortIDX);
         // clone the drop-down list entries
         if ($enumFieldType == CS_FT_DDL || $enumFieldType == CS_FT_DDLMULTI) {
             $this->cloneUFDDL($lSrcFieldID, $lDestFieldID);
             $cdestTable->setDDL_asConfigured($lDestFieldID);
         }
     }
 }
 function addNewCProgram()
 {
     //---------------------------------------------------------------------
     //
     //---------------------------------------------------------------------
     global $glUserID;
     $cufc = new muser_fields_create();
     // first make the new entry in the client program table
     $sqlStr = 'INSERT INTO cprograms
       SET ' . $this->strSQLCommon() . ",\n             cp_bRetired = 0,\n             cp_lOriginID = {$glUserID},\n             cp_dteOrigin = NOW();";
     $query = $this->db->query($sqlStr);
     $this->cprogs[0]->lKeyID = $lKeyID = $this->db->insert_id();
     $strKeyID = str_pad($lKeyID, 5, '0', STR_PAD_LEFT);
     //---------------------------------
     // add the enrollment table
     //---------------------------------
     $cufc->bMultiEntry = true;
     $cufc->bReadOnly = $this->cprogs[0]->bETableReadOnly;
     $cufc->bCollapsibleHeadings = true;
     $cufc->bCollapseDefaultHide = true;
     $cufc->bAlertNoDataEntry = false;
     $cufc->strAlert = '';
     $cufc->strTableDescription = 'Auto-generated for Client Program ' . $strKeyID;
     $cufc->strUserTableName = 'Client Programs Enrollment ' . $strKeyID;
     $cufc->enumTType = CENUM_CONTEXT_CPROGENROLL;
     $lEnrollTableID = $cufc->lTableID = $cufc->lAddNewUFTable();
     $strEnrollTableName = $cufc->strENPTableName;
     $strFNPrefix = $cufc->strGenUF_KeyFieldPrefix($lEnrollTableID);
     $strClientIDFN = $strFNPrefix . '_lClientID';
     $strDateStartFN = $strFNPrefix . '_dteStart';
     $strDateEndFN = $strFNPrefix . '_dteEnd';
     $strBEnrolledFN = $strFNPrefix . '_bCurrentlyEnrolled';
     // note that the foreign ID in the personalized table is the client ID
     $sqlStr = "ALTER TABLE {$strEnrollTableName}\n               ADD {$strDateStartFN}  date NOT NULL,\n               ADD {$strDateEndFN}    date DEFAULT NULL,\n               ADD {$strBEnrolledFN}  tinyint(1) NOT NULL DEFAULT '0';";
     $query = $this->db->query($sqlStr);
     //---------------------------------
     // add the attendance table
     //---------------------------------
     $cufc->strUserTableName = 'Client Programs Attendance ' . $strKeyID;
     $cufc->enumTType = CENUM_CONTEXT_CPROGATTEND;
     $cufc->bReadOnly = $this->cprogs[0]->bATableReadOnly;
     $lAttendTableID = $cufc->lTableID = $cufc->lAddNewUFTable();
     $strAttendTableName = $cufc->strENPTableName;
     $strFNPrefix = $cufc->strGenUF_KeyFieldPrefix($lAttendTableID);
     $strEnrollIDFN = $strFNPrefix . '_lEnrollID';
     $strDateAttendFN = $strFNPrefix . '_dteAttendance';
     $strDurationFN = $strFNPrefix . '_dDuration';
     $strCaseNotesFN = $strFNPrefix . '_strCaseNotes';
     $sqlStr = "ALTER TABLE {$strAttendTableName}\n               ADD {$strEnrollIDFN}    INT NOT NULL COMMENT 'Foreign key to enrollment table',\n               ADD {$strDateAttendFN}  date NOT NULL,\n               ADD {$strDurationFN}    DECIMAL(10, 2) NOT NULL DEFAULT 0,\n               ADD {$strCaseNotesFN}   TEXT NOT NULL,\n            ADD INDEX({$strEnrollIDFN}),\n            ADD INDEX({$strDateAttendFN});";
     $query = $this->db->query($sqlStr);
     //------------------------------------------------------------------
     // add the activity DDL
     //------------------------------------------------------------------
     $cufc->lTableID = $lAttendTableID;
     $cufc->fields = array();
     $cufc->fields[0] = new stdClass();
     $ufield =& $cufc->fields[0];
     $ufield->enumFieldType = CS_FT_DDL;
     $ufield->pff_strFieldNameUser = '******';
     $ufield->pff_bCheckDef = false;
     $ufield->pff_curDef = 0.0;
     $ufield->pff_strTxtDef = '';
     $ufield->pff_lDef = 0;
     $ufield->pff_lCurrencyACO = 1;
     $ufield->pff_bHidden = false;
     $ufield->pff_bRequired = true;
     $cufc->addNewField();
     $lDDLActivityFID = $ufield->pff_lKeyID;
     // add default activity
     $cufc->setDDL_asConfigured($lDDLActivityFID);
     $lNewSortIDX = 1;
     $lDDLEntryID = $cufc->addUF_DDLEntry('(other)', $lDDLActivityFID, $lNewSortIDX);
     //------------------------------------------------------------------
     // update cprogram table with the enrollment/attendance table IDs
     //------------------------------------------------------------------
     $sqlStr = "UPDATE cprograms\n            SET\n               cp_lEnrollmentTableID = {$lEnrollTableID},\n               cp_lAttendanceTableID = {$lAttendTableID},\n               cp_lActivityFieldID   = {$lDDLActivityFID}\n            WHERE cp_lKeyID={$lKeyID};";
     $query = $this->db->query($sqlStr);
     return $lKeyID;
 }