<?php

if ($report->lNumFields == 0) {
    echoT('<i>There are no fields associated with this report.</i>');
    return;
}
// It's the little things that make a house a home.
$bSingular = $report->lNumFields == 1;
echoT('There ' . ($bSingular ? 'is' : 'are') . ' ' . $report->lNumFields . ' field' . ($bSingular ? '' : 's') . ' displayed in this report.<br><br>');
$clsUpDown = new up_down_top_bottom();
$clsUpDown->lMax = $report->lNumFields;
echoT('<table border="0" cellspacing="5">' . "\n");
$iFldCnt = 1;
$idx = 0;
foreach ($report->fields as $field) {
    $lFieldID = $field->lKeyID;
    $clsUpDown->strLinkBase = '<a href="' . base_url() . "index.php/creports/display_order/moveSort/{$lReportID}/{$lFieldID}/";
    $clsUpDown->upDownLinks($idx);
    echoT('<tr>');
    if ($field->lTableID <= 0) {
        $strTable = $field->strUserTableName;
    } else {
        $strTable = '<b>[' . $field->strAttachLabel . ']</b> ' . htmlspecialchars($field->strUserTableName);
    }
    echoT('<td style="width: 20pt;">&nbsp;</td>
              <td class="enpRpt" style="font-size: 7pt;" nowrap>' . $clsUpDown->strUp . $clsUpDown->strDown . '&nbsp;&nbsp;&nbsp;&nbsp;' . $clsUpDown->strTop . $clsUpDown->strBottom . '
              </td>
              <td class="enpRpt" style="vertical-align: middle;">' . $strTable . '
              </td>
              <td class="enpRpt" style="vertical-align: middle;">' . htmlspecialchars($field->strUserFN) . '
              </td>
 function addNewQuestion($lPPTestID)
 {
     //---------------------------------------------------------------------
     //
     //---------------------------------------------------------------------
     global $glUserID;
     $quest =& $this->questions[0];
     $udtb = new up_down_top_bottom();
     $udtb->strTable = 'cpp_questions';
     $udtb->strKeyField = 'cpq_lKeyID';
     $udtb->strSortField = 'cpq_lSortIDX';
     $opts = array();
     $opts[0] = new stdClass();
     $opts[0]->strFN = 'cpq_lPrePostID';
     $opts[0]->lQualValue = $lPPTestID;
     $sqlStr = 'INSERT INTO cpp_questions
       SET ' . $this->sqlCommonAddEditQuest() . ',
          cpq_lSortIDX   = ' . ($udtb->lMaxSortIDX($opts) + 1) . ",\n             cpq_lPrePostID = {$lPPTestID},\n             cpq_lOriginID  = {$glUserID},\n             cpq_dteOrigin  = NOW(),\n             cpq_bRetired   = 0;";
     $query = $this->db->query($sqlStr);
     $quest->lKeyID = $lKeyID = $this->db->insert_id();
     return $lKeyID;
 }
 function strFormattedSearchExpression($lReportID, $att, &$bBalanced)
 {
     //---------------------------------------------------------------------
     //
     //---------------------------------------------------------------------
     $this->setDefaultAttributes($att);
     $strOut = '';
     $this->loadSearchTermViaReportID($att->lReportID);
     $bBalanced = $this->bAreParensBalanced();
     if ($this->lNumTerms == 0) {
         return '<i>There are no search expressions associated with this report.</i>';
     }
     // It's the little things that make a house a home.
     $bSingular = $this->lNumTerms == 1;
     if ($att->showSearchCnt) {
         $strOut .= 'There ' . ($bSingular ? 'is' : 'are') . ' ' . $this->lNumTerms . ' search expression' . ($bSingular ? '' : 's') . ' associated with this report.<br><br>';
     }
     if ($att->bShowUpDownLink) {
         $clsUpDown = new up_down_top_bottom();
         $clsUpDown->lMax = $this->lNumTerms;
     }
     if ($att->warnUnbalancedParen) {
         if (!$bBalanced) {
             $strOut .= '
            <div class="formError">Warning: the parentheses in this search
               expresssion are not balanced. You will not be able to run
               this report until the parenthese are balanced.</div>';
         }
     }
     if ($att->bShowSortLink) {
         if ($this->lNumTerms > 1) {
             $strOut .= strLinkEdit_CRptSearchFieldOrder($lReportID, 'Search order', false) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
             //                       strLinkEdit_CRptSearchFieldOrder($lReportID, 'Search order', true).'&nbsp;'
             //                      .strLinkEdit_CRptSearchFieldOrder($lReportID, 'Search order', false).'<br>';
         }
     }
     if ($att->bShowParenEditLink) {
         $strOut .= strLinkEdit_CRptSearchFieldParens($lReportID, 'Parentheses', false) . '<br>';
         //         $strOut .= strLinkEdit_CRptSearchFieldParens($lReportID, 'Parentheses', true).'&nbsp;'
         //                   .strLinkEdit_CRptSearchFieldParens($lReportID, 'Parentheses', false).'<br>';
     }
     $lMaxP = $this->lMaxParentheses();
     $strOut .= '<table border="0" cellspacing="5">' . "\n";
     $iFldCnt = 1;
     $idx = 0;
     $lMaxNumParen = 6;
     foreach ($this->terms as $term) {
         $lFieldID = $term->lFieldID;
         $lTermID = $term->lKeyID;
         $strOut .= '<tr>';
         if ($att->showEditDelete) {
             $strOut .= '<td>' . strLinkEdit_CReportSearchTerm($lReportID, $term->strFieldID, $lTermID, 'Edit search term', true) . '&nbsp;&nbsp;&nbsp;&nbsp;' . strLinkRem_CReportSearchTerm($lReportID, $lTermID, 'Remove search term', true, true) . '
              </td>';
         }
         // field count
         if ($att->showFieldIDX) {
             $strOut .= '
            <td style="text-align: center;">(' . $iFldCnt . ')&nbsp;
            </td>';
         }
         // up/down links
         if ($att->bShowUpDownLink) {
             $clsUpDown->strLinkBase = '<a href="' . base_url() . "index.php/creports/search_order/move/{$lReportID}/{$lTermID}/";
             $clsUpDown->upDownLinks($idx);
             $strOut .= '<td style="width: 20pt;">&nbsp;</td>
                 <td class="enpRpt" style="font-size: 7pt;" nowrap>' . $clsUpDown->strUp . $clsUpDown->strDown . '&nbsp;&nbsp;&nbsp;&nbsp;' . $clsUpDown->strTop . $clsUpDown->strBottom . '
                 </td>';
         }
         // opening parentheses
         $strOut .= '
            <td style="font-family: courier;">' . "\n";
         if ($att->bParenAsTextInput) {
             $strOut .= $this->inputTextParens($lTermID, true, $term->lNumLParen, $lMaxNumParen, $idx);
         } else {
             for ($jidx = 0; $jidx < $term->lNumLParen; ++$jidx) {
                 $strOut .= '( ';
             }
         }
         $strOut .= '&nbsp;</td>';
         // table and field name
         $strOut .= '<td style="font-weight: bold; font-size: 8pt;">[' . htmlspecialchars($term->strUserTableName) . ']
           </td>
           <td>' . htmlspecialchars($term->strFieldNameUser) . '
           </td>';
         // comparison operator
         $strOut .= '<td style="font-weight: bold;">' . crptLabelSearchType($term->lCompareOpt) . '
           </td>';
         // comparitor
         $strOut .= '<td>' . $this->strComparitor($term) . '</td>';
         // closing parentheses
         $strOut .= '<td style="font-family: courier;">&nbsp;';
         if ($att->bParenAsTextInput) {
             $strOut .= $this->inputTextParens($lTermID, false, $term->lNumRParen, $lMaxNumParen, $idx);
         } else {
             for ($jidx = 0; $jidx < $term->lNumRParen; ++$jidx) {
                 $strOut .= ' )';
             }
         }
         $strOut .= '</td>';
         // spacer
         $strOut .= '<td style="width: 20pt;">&nbsp;</td>';
         // boolean with next term
         if ($iFldCnt == $this->lNumTerms) {
             $strOut .= '<td>&nbsp;</td>';
         } else {
             if ($att->bParenAsTextInput) {
                 $strOut .= '<td><i>' . $this->strAndOrDDL($lTermID, $term->bNextTermBoolAND, $idx) . '</i></td>';
             } else {
                 $strOut .= '<td>' . ($term->bNextTermBoolAND ? 'AND' : 'OR') . '</td>';
             }
         }
         $strOut .= '</tr>';
         ++$iFldCnt;
         ++$idx;
     }
     $strOut .= '</table>';
     return $strOut;
 }