/**
  * Fetches a Result Field
  * 
  * Fetches the value in the first column of the first row and then frees the result set.
  * @access public
  * @param resource $rs A reference to a resource handle returned by executing a query.
  * @param mixed $field A reference to a variable that will contain the field value.
  * @return bool Returns true if the operation was successful, otherwise false.
  * @example fetch_result_field.php FetchResultField example.
  */
 function FetchResultField(&$rs, &$field)
 {
     $this->ClearError();
     if ($result = $this->IsResource($rs, "FetchResultField")) {
         $result = ($field = $this->_driver->FetchField($rs)) != false;
         $this->GetDriverError();
         $this->_driver->FreeResult($rs);
         $this->GetDriverError();
     }
     return $result;
 }
/**
 * Utility function to turn a result set into an associative array of records
 * This method turns a result set into a hash of records (keyed by the first
 * field in the result set)
 *
 * @param  object $rs An ADODB RecordSet object.
 * @return mixed  An array of objects, or false if the RecordSet was empty.
 * @throws SQLException
 * @access private
 */
function recordset_to_assoc($rs)
{
    if ($rs && $rs->RecordCount() > 0) {
        // First of all, we are going to get the name of the first column
        // to introduce it back after transforming the recordset to assoc array
        // See http://docs.moodle.org/en/XMLDB_Problems, fetch mode problem.
        $firstcolumn = $rs->FetchField(0);
        // Get the whole associative array
        if ($records = $rs->GetAssoc(true)) {
            foreach ($records as $key => $record) {
                $record[$firstcolumn->name] = $key;
                $objects[$key] = (object) $record;
            }
            return $objects;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Example #3
0
 /**
  *Creates a grid from a recordset
  *
  *This function perform all is needed to show the grid
  *
  *@param object $rs Recordset
  *@param integer $currentrecord First record of the recordset to show
  *@param boolean $fullrs If true, no pagination is generated
  */
 function grid($rs, $currentrecord = 0, $fullrs = TRUE, $filterrs = TRUE)
 {
     if (!$this->showgrid) {
         exit;
     }
     global $PHP_SELF;
     $fname = $this->forname . "grid";
     $fields = $rs->FieldCount();
     $fieldobjects = array();
     for ($i = 0; $i <= $fields - 1; $i++) {
         $field = $rs->FetchField($i);
         $fieldobjects[$field->name] = $field;
     }
     if (count($this->fieldorder) != 0) {
         $thefields = $this->fieldorder;
     } else {
         for ($i = 0; $i <= $fields - 1; $i++) {
             $field = $rs->FetchField($i);
             $thefields[$i] = $field->name;
         }
     }
     //If there must be a navigator, then, this is a form and must allow navigation
     if ($this->shownavigator) {
         echo "<form name=\"{$fname}\" method=\"post\" action=\"{$PHP_SELF}\">\n";
         echo "<input type=\"hidden\" name=\"currentrecord\" value=\"{$currentrecord}\">\n";
         global $where, $wherevalue;
         if (isset($where)) {
             $where = rawurlencode($where);
             $wherevalue = rawurlencode($wherevalue);
             echo "<input type=\"hidden\" name=\"where\" value=\"{$where}\">\n";
             echo "<input type=\"hidden\" name=\"wherevalue\" value=\"{$wherevalue}\">\n";
         }
         global $orderfield;
         global $ascdesc;
         if (isset($orderfield)) {
             echo "<input type=\"hidden\" name=\"orderfield\" value=\"{$orderfield}\">\n";
         }
         if (isset($ascdesc)) {
             echo "<input type=\"hidden\" name=\"ascdesc\" value=\"{$ascdesc}\">\n";
         }
     }
     $headercolor = $this->colors[1];
     $colspan = 1;
     reset($thefields);
     while (list($k, $v) = each($thefields)) {
         $afieldname = $v;
         $v = $this->getRealFieldName($afieldname);
         $field = $fieldobjects[$v];
         if (!array_key_exists($afieldname, $this->novisible)) {
             $colspan++;
         }
     }
     $colspan += count($this->calculatedfields);
     $w = '';
     if ($this->autosize) {
         $w = 'width="100%"';
     }
     $border = 'border="0"';
     echo "<table {$border} {$w} bgcolor=\"#ffffff\">\n";
     if ($this->formcaption != '') {
         echo "<tr bgcolor=\"{$headercolor}\">\n";
         if (!$this->showcheckboxes) {
             $colspan--;
         }
         echo "<td colspan=\"{$colspan}\"><font size=\"+1\"><b>{$this->formcaption}</b></font></td>";
         echo "</tr>\n";
     }
     //Shows the title row
     if ($this->showtitles) {
         echo "<tr bgcolor=\"{$headercolor}\">\n";
         if ($this->showcheckboxes) {
             echo "<td width=\"10px\">";
             echo "<input type=\"checkbox\" name=\"checkall\" onClick=\"CheckAllBoxes()\">";
             echo "</td>\n";
         }
         $rcolors = array();
         reset($thefields);
         while (list($k, $v) = each($thefields)) {
             $afieldname = $v;
             $v = $this->getRealFieldName($afieldname);
             $field = $fieldobjects[$v];
             $cellcolor = '';
             if (!array_key_exists($afieldname, $this->novisible)) {
                 $label = $this->getLabel($afieldname);
                 $olabel = $label;
                 global $orderfield, $ascdesc;
                 $ord = '&ascdesc=1';
                 if (isset($orderfield)) {
                     if ($orderfield == $field->name) {
                         $toadd = '&nbsp;(a-z)';
                         $ord = '&ascdesc=0';
                         if (isset($ascdesc)) {
                             if (!$ascdesc) {
                                 $toadd = '&nbsp;(z-a)';
                                 $ord = '&ascdesc=1';
                             }
                         }
                         $label .= $toadd;
                         $cs = $this->colors[2];
                         $cellcolor = " bgcolor=\"{$cs}\"";
                     }
                 }
                 global $where;
                 global $wherevalue;
                 if (isset($where)) {
                     $where = $this->xdecode($where);
                     $ord .= "&where={$where}";
                 }
                 if (isset($wherevalue)) {
                     if (trim($wherevalue) != '') {
                         $ord .= "&wherevalue={$wherevalue}";
                     }
                 }
                 if ($this->allowsort) {
                     echo "<td {$cellcolor}><b><A HREF=\"{$PHP_SELF}?orderfield={$field->name}{$ord}\" title=\"Ordenar por {$olabel}\">{$label}</A></b></td>\n";
                 } else {
                     echo "<td {$cellcolor}><b>{$label}</b></td>\n";
                 }
             }
         }
         reset($this->calculatedfields);
         while (list($fieldname, $displayfunction) = each($this->calculatedfields)) {
             echo "<td><b>{$fieldname}</b></td>\n";
         }
         echo "</tr>\n";
     }
     echo "<tr>\n";
     //Iterates through the records set and output the fields
     $r = 0;
     while (!$rs->EOF) {
         $row = array();
         $k = 0;
         reset($thefields);
         while (list($t, $v) = each($thefields)) {
             $afieldname = $v;
             $v = $this->getRealFieldName($afieldname);
             $field = $fieldobjects[$v];
             if (!array_key_exists($afieldname, $this->novisible)) {
                 $row[$k] = $this->getFieldValue($field, $afieldname, $rs->fields[$field->name], TRUE);
                 if (array_key_exists($afieldname, $this->linked)) {
                     $val = $row[$k];
                     $edrec = $rs->fields[$field->name];
                     $page = $this->linked[$afieldname][0] . "?sbedit_x=1&editrecord={$edrec}";
                     $hint = $this->linked[$afieldname][1];
                     $row[$k] = "<A HREF=\"{$page}\" alt=\"{$hint}\" title=\"{$hint}\">{$val}</A>";
                 }
                 if (isset($orderfield)) {
                     if ($orderfield == $field->name) {
                         $cs = $this->colors[2];
                         $rcolors[$k] = " bgcolor=\"{$cs}\"";
                     }
                 }
                 $k++;
             }
         }
         reset($this->calculatedfields);
         while (list($fieldname, $displayfunction) = each($this->calculatedfields)) {
             eval("\$value={$displayfunction}(\$rs);");
             $row[$k] = $value;
             $k++;
         }
         $this->gridRow($row, $r, $rcolors, $rs);
         $rs->MoveNext();
         $r++;
     }
     echo "</tr>\n";
     //If must show the status
     if ($this->showstatus) {
         echo "<tr bgcolor=\"{$headercolor}\">\n";
         if ($filterrs) {
             $sql = "select * from {$this->table}";
             $sql = $this->filterquery($sql);
             if ($fullrs) {
                 $rs = $this->ExecSQL($sql);
             }
         } else {
             $sql = $this->getlookupquery();
             $rs = $this->ExecSQL($sql);
         }
         $totalrecords = $rs->RowCount();
         $currentrecord++;
         if ($totalrecords == 0) {
             $currentrecord = 0;
         }
         $recs = "{$currentrecord} de {$totalrecords}";
         if (!$fullrs) {
             $recs = "{$totalrecords} {$this->units}";
         }
         if (!$this->showcheckboxes) {
             $colspan--;
         }
         echo "<td align=\"right\" colspan=\"{$colspan}\">{$recs}</td>\n";
         echo "</tr>\n";
     }
     //If must show the navigator
     if ($this->shownavigator) {
         echo "<tr bgcolor=\"{$headercolor}\">\n";
         if (!$this->showcheckboxes) {
             $colspan--;
         }
         echo "<td align=\"right\" colspan=\"{$colspan}\">\n";
         $this->printNavigator();
         echo "</td>\n";
         echo "</tr>\n";
     }
     //If must show the status text
     if ($this->showstatustext) {
         echo "<tr bgcolor=\"{$headercolor}\">\n";
         $this->updatestatustext($rs);
         global $stext;
         if ($stext != '') {
             $stext = urldecode($stext);
             $this->statustext = $stext;
         }
         $stat = $this->statustext;
         if (!$this->showcheckboxes) {
             $colspan--;
         }
         echo "<td align=\"right\" colspan=\"{$colspan}\">{$stat}</td>\n";
         echo "</tr>\n";
     }
     echo "</table>\n";
     //Closes the form if needed
     if ($this->shownavigator) {
         global $stext;
         if (isset($stext)) {
             $stext = rawurlencode($stext);
             echo "<input type=\"hidden\" name=\"stext\" value=\"{$stext}\">\n";
         }
         echo "</form>\n";
     }
 }
Example #4
0
 /**
  * Returns an SQL statement updating the fields $arrFields in the table
  * contained in $rs
  * 
  * @param object $rs a QuestDBrs object made of the relevant SELECT query
  * @param array $arrFields a set of name-value pairs containing field names and values
  * @param boolean $forceUpdate if set, will update fields even if the new value is the same as the current
  * @return string an UPDATE SQL statement
  */
 private function getUpdateSQL($rs, $arrFields, $forceUpdate = false)
 {
     if (!$rs) {
         $this->CatchError(7, "getUpdateSQL");
         return false;
     }
     $fieldUpdatedCount = 0;
     $arrFields = array_change_key_case($arrFields, CASE_UPPER);
     $hasnumeric = isset($rs->fields[0]);
     $setFields = '';
     // A big loop going through each field in the record set
     // and adding to update query if it has changed
     for ($i = 0, $max = $rs->FieldCount(); $i < $max; $i++) {
         // Get the field from the recordset
         $field = $rs->FetchField($i);
         // If the recordset field is one
         // of the fields passed in then process.
         $upperfname = strtoupper($field->name);
         if (array_key_exists($upperfname, $arrFields)) {
             // If the existing field value in the recordset
             // is different from the value passed in then
             // go ahead and append the field name and new value to
             // the update query.
             if ($hasnumeric) {
                 $val = $rs->fields[$i];
             } else {
                 if (isset($rs->fields[$upperfname])) {
                     $val = $rs->fields[$upperfname];
                 } else {
                     if (isset($rs->fields[$field->name])) {
                         $val = $rs->fields[$field->name];
                     } else {
                         if (isset($rs->fields[strtolower($upperfname)])) {
                             $val = $rs->fields[strtolower($upperfname)];
                         } else {
                             $val = '';
                         }
                     }
                 }
             }
             if ($forceUpdate || strcmp($val, $arrFields[$upperfname])) {
                 // Set the counter for the number of fields that will be updated.
                 $fieldUpdatedCount++;
                 if (strpos($upperfname, ' ') !== false) {
                     $fnameq = $this->nameQuote . $upperfname . $this->nameQuote;
                 } else {
                     $fnameq = $upperfname;
                 }
                 // is_null requires php 4.0.4
                 if (is_null($arrFields[$upperfname]) || empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0 || $arrFields[$upperfname] === $this->null2null) {
                     //Set the value that was given in array, so you can give both null and empty values
                     if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $this->null2null) {
                         $setFields .= $this->nameQuote . $field->name . $this->nameQuote . " = null, ";
                     } else {
                         $setFields .= $this->column_sql('U', $upperfname, $fnameq, $arrFields);
                     }
                 } else {
                     // A little thing to make cleaner SQL
                     $setFields .= $this->column_sql('U', $upperfname, $fnameq, $arrFields);
                 }
             }
         }
     }
     //for
     // If there were any modified fields then build the rest of the update query.
     if ($fieldUpdatedCount > 0 || $forceUpdate) {
         // Get the table name from the existing query.
         if (!empty($rs->tableName)) {
             $tableName = $rs->tableName;
         } else {
             preg_match("/FROM\\s+" . '([]0-9a-z_\\:\\"\\`\\.\\@\\[-]*)' . "/is", $rs->sql, $tableName);
             $tableName = $tableName[1];
         }
         // Get the full where clause excluding the word "WHERE" from
         // the existing query.
         preg_match('/\\sWHERE\\s(.*)/is', $rs->sql, $whereClause);
         $discard = false;
         // not a good hack, improvements?
         if ($whereClause) {
             if (preg_match('/\\s(ORDER\\s.*)/is', $whereClause[1], $discard)) {
             } else {
                 if (preg_match('/\\s(LIMIT\\s.*)/is', $whereClause[1], $discard)) {
                 } else {
                     if (preg_match('/\\s(FOR UPDATE.*)/is', $whereClause[1], $discard)) {
                     } else {
                         preg_match('/\\s.*(\\) WHERE .*)/is', $whereClause[1], $discard);
                     }
                 }
             }
             # see http://sourceforge.net/tracker/index.php?func=detail&aid=1379638&group_id=42718&atid=433976
         } else {
             $whereClause = array(false, false);
         }
         if ($discard) {
             $whereClause[1] = substr($whereClause[1], 0, strlen($whereClause[1]) - strlen($discard[1]));
         }
         $sql = 'UPDATE ' . $tableName . ' SET ' . substr($setFields, 0, -2);
         if (strlen($whereClause[1]) > 0) {
             $sql .= ' WHERE ' . $whereClause[1];
         }
         return $sql;
     } else {
         return false;
     }
 }