Location: PHPKode > scripts > phpMyAccess > phpmyaccess-1.5.3/phpmyaccess/pma-recordinc.php
<?php

function do_write_add_field($field) {
      // Field is not editable
      if ((isset($GLOBALS['fdd'][$field]['editable'])) and ($GLOBALS['fdd'][$field]['editable']==false)) {
        // nothing much to showon add for now
        echo '    <td> *-*-*-*-* </td>'."\n";
      // linked field, will be filled in on save/change commit path
      } elseif (isset($GLOBALS['fdd'][$field]['link']['linked'])) {
        // locked to another value, should be empty here
        echo '    <td> *linked* </td>'."\n";
      // linked against another table, value displayed from that table
      } elseif (isset($GLOBALS['opt']['ttable']) and isset($GLOBALS['opt']['tcolumn']) and isset($GLOBALS['opt']['tview'])) {
        echo DoLinkTableDisp($field, $GLOBALS['fdd'][$field]['default']);

      // linked against another table
      } elseif (isset($GLOBALS['opt']['ttable']) and isset($GLOBALS['opt']['tcolumn'])) {
        echo DoLinkTable($field, $GLOBALS['fdd'][$field]['default']);

      // Value list non-dynamic
      } elseif (isset($GLOBALS['fdd'][$field]['values'])) {
        echo DoStaticRangeEdit($field,$GLOBALS['fdd'][$field]['default']);

      // Big text block data entry
      } elseif (isset($GLOBALS['fdd'][$field]['textarea'])) {
        echo DoTextBlockEdit($field,$GLOBALS['fdd'][$field]['default']);

      // Boolean type input
      } elseif (isset($GLOBALS['fdd'][$field]['boolean'])) {
        echo DoBooleanEdit($field,$GLOBALS['fdd'][$field]['default']);
      // Password input
      } elseif (isset($GLOBALS['fdd'][$field]['password'])) {

        echo '    <td><input type="password" name="'.$field.'" value="" /></td>'."\n";
      // Auto-incrmenting number (use only for mysql auto-increment)
      } elseif (isset($GLOBALS['fdd'][$field]['auto'])) {

        echo '    <td>*auto-increment*</td>'."\n";
      // Dynamic range selection, for larger numbers of selections
      } elseif (isset($GLOBALS['fdd'][$field]['range'])) {
        echo DoRangeEdit($field,$GLOBALS['fdd'][$field]['default']);

      // Date values entry
      } elseif (isset($GLOBALS['fdd'][$field]['date'])) {
        echo DoDatefieldEdit ($field,$GLOBALS['fdd'][$field]['default']);

      // Distinct values entry
      } elseif (isset($GLOBALS['fdd'][$field]['distinct'])) {
        echo DoDistinctTableFieldSelect ($field,$GLOBALS['opt']['tb'],$GLOBALS['fdd'][$field]['default']);

      // Default standard stuff
      } else {
        // Simple edit box required, if length option is set it will be used
        if (isset($GLOBALS['fdd'][$field]['length'])) {
            $fwidth = $GLOBALS['fdd'][$field]['length'];
            $fdispwidth = $fwidth;
            if ($fdispwidth > $GLOBALS['opt']['maxdisp']) { $fdispwidth = $GLOBALS['opt']['maxdisp'];}
            echo '    <td><input type="text" size="' . $fdispwidth . '" maxlength="' . $fwidth .
                             '" name="'.$field.'" value="" /></td>'."\n";
        } else {
            echo '    <td><input type="text" name="'.$field.'" value="" /></td>'."\n";
        }
      }

}

function do_change_add_field($field, $value) {
          if ((isset($GLOBALS['fdd'][$field]['editable'])) and ($GLOBALS['fdd'][$field]['editable']==false)) {
             echo DoNotEditable($field,$value);
          // entry taken from another entry as index to some table
          } elseif (isset($GLOBALS['fdd'][$field]['link']['linked'])) {
             // locked to another value
             echo DoLinkedVal($field,$value);
          // indirect table select
          } elseif (isset($GLOBALS['opt']['ttable']) and isset($GLOBALS['opt']['tcolumn']) and isset($GLOBALS['opt']['tview'])) {
             echo DoTableSelectIndirect($field,$value);
          // direct table select
          } elseif (isset($GLOBALS['opt']['ttable']) and isset($GLOBALS['opt']['tcolumn'])) {
             echo DoTableSelectDirect($field,$value);
          // values list
          } elseif (isset($GLOBALS['fdd'][$field]['values'])) {
             echo DoStaticRangeEdit($field,$value);
          // textarea
          } elseif (isset($GLOBALS['fdd'][$field]['textarea'])) {
             echo DoTextBlockEdit($field,$value);
          // boolean
          } elseif (isset($GLOBALS['fdd'][$field]['boolean'])) {
             echo DoBooleanEdit($field,$value);
          // password
          } elseif (isset($GLOBALS['fdd'][$field]['password'])) {
               echo '    <td><input type="password" name="'.$field.'" value="' .nl2br(htmlentities($GLOBALS['opt']['pwchangedet'])). '" /></td>'."\n";
          // autonumber
          } elseif (isset($GLOBALS['fdd'][$field]['auto'])) {
               echo '  <input type="hidden" name="'.$field. '" value="'.nl2br(htmlentities($value)).'" />'."\n";
               echo '    <td>' . htmlentities($value) .'</td>'."\n";
          // range select
          } elseif (isset($GLOBALS['fdd'][$field]['range'])) {
               echo DoRangeEdit($field,$value);
          // data select
          } elseif (isset($GLOBALS['fdd'][$field]['date'])) {
               echo DoDatefieldEdit($field,$value);
          // Distinct values entry
          } elseif (isset($GLOBALS['fdd'][$field]['distinct'])) {
               echo DoDistinctTableFieldSelect ($field,$GLOBALS['opt']['tb'],$value);
          // default
          } else {
              if (isset($GLOBALS['fdd'][$field]['length'])) {
                 $fwidth = $GLOBALS['fdd'][$field]['length'];
                 $fdispwidth = $fwidth;
                 if ($fdispwidth > 60) { $fdispwidth = 60;}
                 echo '    <td><input type="text" size="' . $fdispwidth . '" maxlength="' . $fwidth .
                                   '" name="'.$field.'" value="'.htmlentities($value).'" /></td>'."\n";
              } else {
                 echo '    <td><input type="text" name="'.$field.'" value="'.htmlentities($value).'" /></td>'."\n";
              }
          } // else data type
}

function do_save_add_save() {
  echo "    <!--// do_save_add_save //--!>\n";
  unset($autofix);
  for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
    $fieldname = $GLOBALS['fds'][$k];
    if ($k==0) {
        // provisions for auto-increment fields
        if ( $GLOBALS['fdd'][$fieldname]['auto'] == true) {
           $qry = 'INSERT INTO '.$GLOBALS['opt']['tb'].' (';
           $qry1 = $qry1 . ') VALUES (';
           $autofix = true;
        } else {
           $qry = 'INSERT INTO '.$GLOBALS['opt']['tb'].' ('.$GLOBALS['fds'][$k];
           $qry1 = $qry1 . ") VALUES ('" .$GLOBALS['val'][$fieldname]."'";
        }
    } else {
       if ($autofix) {
         $qry = $qry .$fieldname;
         $qry1 = $qry1 .'\''.$GLOBALS['val'][$fieldname].'\'';
         unset($autofix);
       } elseif ( $GLOBALS['fdd'][$fieldname]['link']['linked'] == true) {
         $ltable = $GLOBALS['fdd'][$fieldname]['link']['table'];
         $lsel = $GLOBALS['fdd'][$fieldname]['link']['column'];
         $lkey = $GLOBALS['fdd'][$fieldname]['link']['rowindex'];
         // take care, next line points to the field value of the linked
         // field table's record index
         $lval = $$GLOBALS['fdd'][$fieldname]['link']['linkedto'];
         $linkval = get_linked_val($GLOBALS['opt']['db'],$ltable,$lsel,$lkey,$lval);
         $qry .= ','.$fieldname;
         $qry1 .= ",'" . $linkval .'\'';
       } elseif ($GLOBALS['fdd'][$fieldname]['values']['default']){
           if (!$GLOBALS['val'][$fieldname]) {
              $qry .= ','.$fieldname;
              $qry1 .= ",'" .$GLOBALS['fdd'][$fieldname]['values']['default'].'\'';
           }
       } elseif ($GLOBALS['fdd'][$fieldname]['distinct'] == true){
           if ($GLOBALS['val'][$fieldname."_new"]) {
              $qry .= ','.$fieldname;
              $qry1 .= ",'" .$GLOBALS['val'][$fieldname."_new"].'\'';
           } else {
              $qry  .= ','.$fieldname;
              $qry1 .= ",'".$GLOBALS['val'][$fieldname].'\'';
           }
       } else {
         $qry  .= ','.$fieldname;
         $qry1 .= ",'".$GLOBALS['val'][$fieldname].'\'';
       }
    }
  }

  $qry = $qry . $qry1 . ')';
  pma_debug ('Add query : '.$qry );
  $res = mysql_db_query($GLOBALS['opt']['db'],$qry);
  if ($res) {
    echo '<h5>'.mysql_affected_rows().' record added</h5>'."\n";
  } else {
       echo  "Add error : " . mysql_errno().": ".mysql_error()."<br/>";
  }
}

function do_save_change($key,$rec) {
  echo "    <!--// do_save_change //--!>\n";
  for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
    $fieldname = $GLOBALS['fds'][$k];
    if ($k==0) {
      $qry = 'UPDATE '.$GLOBALS['opt']['tb'].' SET '.$fieldname."='".$GLOBALS['val'][$fieldname].'\'';
    } else {
       if ( $GLOBALS['fdd'][$fieldname]['link']['linked'] == true) {
         $ltable = $GLOBALS['fdd'][$fieldname]['link']['table'];
         $lsel = $GLOBALS['fdd'][$fieldname]['link']['column'];
         $lkey = $GLOBALS['fdd'][$fieldname]['link']['rowindex'];
         // take care, next line points to the field value of the linked
         // field table's record index
         $lval = $$GLOBALS['fdd'][$fieldname]['link']['linkedto'];
         $linkval = get_linked_val($GLOBALS['opt']['db'],$ltable,$lsel,$lkey,$lval);
         $qry = $qry.','.$fieldname."='".$linkval.'\'';
       } elseif ( $GLOBALS['fdd'][$fieldname]['password'] == true) {
         if ($$fieldname != $GLOBALS['opt']['pwchangedet']) {
           $qry = $qry.','.$GLOBALS['fds'][$k]."='".$GLOBALS['val'][$fieldname].'\'';
         }
       } elseif ($GLOBALS['fdd'][$fieldname]['distinct'] == true){
           if ($GLOBALS['val'][$fieldname."_new"]) {
              $qry = $qry .','. $fieldname."='".$GLOBALS['val'][$fieldname."_new"].'\'';
           } else {
              $qry = $qry.','.$fieldname."='".$GLOBALS['val'][$fieldname].'\'';
           }
       } elseif ($GLOBALS['fdd'][$fieldname]['values']['default']){
           if (!$$fieldname) {
              $qry = $qry.','.$fieldname."='".$GLOBALS['fdd'][$fieldname]['values']['default'].'\'';
           }
       } else {
         $qry = $qry.','.$fieldname."='".$GLOBALS['val'][$fieldname].'\'';
       }
    }
  }
  $qry = $qry.' WHERE ('.$key.' = '.$GLOBALS['opt']["key_delim"] . $rec . $GLOBALS['opt']["key_delim"].')';
  pma_debug (" Change query : " . $qry );
  $res = mysql_db_query($GLOBALS['opt']['db'],$qry);
  if ($res) {
    echo '<h5>'.mysql_affected_rows().' record changed</h5>'."\n";
  } else {
    echo  "Change error : " . mysql_errno().": ".mysql_error()."<br/>";
  }
}

function do_form_button_display() {
  echo "    <!--// do_form_button_display //--!>\n";
  echo "<div class=\"nav\"><table border=\"2\"><tr>\n";
  if ($GLOBALS['opt']['add'] == 'Add') {
     echo '<td><input type="submit" name="saveadd" value="Save" />'."</td>\n";
     echo '<td><input type="button" name="cancel" value="Cancel" onClick="javascript:history.go(-1);" />'."</td>\n";
  } elseif ($GLOBALS['opt']['change'] == 'Change') {
     echo '<td><input type="submit" name="savechange" value="Save" />'."</td>\n";
     echo '<td><input type="button" name="cancel" value="Cancel" onClick="javascript:history.go(-1);" />'."</td>\n";
  } elseif ($GLOBALS['opt']['delete'] == 'Delete') {
     echo '<td><input type="submit" name="savedelete" value="Delete" />'."</td>\n";
     echo '<td><input type="submit" name="cancel" value="Cancel" />'."</td>\n";
  } elseif ($GLOBALS['opt']['zoom'] == 'Zoom') {
     echo '<td><input type="submit" name="cancel" value="Cancel" />'."</td>\n";
  }
  echo "</tr></table></div>\n";
}

function do_zoom_delete_form() {
  echo "    <!--// do_zoom_delete_form //--!>\n";
  $qry = create_column_list($GLOBALS['opt']['tb'],$GLOBALS['fdd']);
  if (!in_array($key,$GLOBALS['fds'])) {
     $qry = $qry.','.fully_qualified_name($GLOBALS['opt']['tb'],$GLOBALS['fdd'],$GLOBALS['opt']['key'],"view");
  }
  $qry = $qry.create_from_clause($GLOBALS['opt']['tb'],$GLOBALS['fdd']);
  $qry_select = create_where_clause($GLOBALS['opt']['tb'],$GLOBALS['fdd']);
  if ($qry_select=='') {
     $kwd = ' WHERE ';
  } else {
     $kwd = ' AND ';
  }
  $qry = $qry.$qry_select.$kwd.'('.fully_qualified_name($GLOBALS['opt']['tb'],$GLOBALS['fdd'],$GLOBALS['opt']['key'],"view").' = '.$GLOBALS['opt']['key_delim'].$GLOBALS['opt']['rec'].$GLOBALS['opt']['key_delim'].')';
  pma_debug (" Zoom/Delete query : $qry ");
  $res = mysql_db_query($GLOBALS['opt']['db'],$qry);
  if ($res) {
    if ($row = mysql_fetch_array($res)) {
      for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
        echo '  <tr>'."\n";
        echo '    <td>'.$GLOBALS['fdd'][$GLOBALS['fds'][$k]]['name'].'</td>'."\n";
        if (isset($GLOBALS['fdd'][$GLOBALS['fds'][$k]]['boolean'])) {
            if ($row[$k]) {
                 echo '      <td>' . $GLOBALS['opt']['booltrue'] . '</td>' . "\n";
            } else {
                 echo '      <td>' . $GLOBALS['opt']['boolfalse'] . '</td>' . "\n";
            }
        } elseif (isset($GLOBALS['fdd'][$GLOBALS['fds'][$k]]['password'])) {
            echo '    <td>*********</td>'."\n";
        } else {
            // default action
            echo '    <td>' . htmlentities($row[$k]) . '</td>' . "\n";
            //echo '    <td>' . $row[$k] . '</td>' . "\n";
        }
      }
      echo '  </tr>'."\n";
    }
  } else {
     echo  "Zoom/Delete error : " . mysql_errno().": ".mysql_error()."<br/>";
  }
}

function make_javascript_confirm() {
  // echo "    <!--// make_javascript_confirm //--!>\n";
  echo '<script type="text/javascript">'."\n";
  echo "  function form_control(theForm)  {\n";
  for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
    $fieldname=$GLOBALS['fds'][$k];
    if ($GLOBALS['fdd'][$fieldname]['required']
        and !isset($GLOBALS['fdd'][$fieldname]['values'])
        and !isset($GLOBALS['fdd'][$fieldname]['auto'])
        and !isset($GLOBALS['fdd'][$fieldname]['date'])
        and !isset($GLOBALS['fdd'][$fieldname]['boolean'])) {
      echo "
      if ( theForm.".$fieldname.".value.length == 0 ) {
        alert( 'Please enter ".$GLOBALS['fdd'][$fieldname]['name']." .' );
        theForm.".$fieldname.".focus();
        return false;
      }\n";
    }
  }
  echo "    theForm.submit();\n";
  echo "    return true;\n";
  echo "  }\n";
  echo "</script>\n";
}

function do_add_record_form() {
  echo "    <!--// do_add_record_form //--!>\n";
  for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
    echo '  <tr>'."\n";
    echo '    <td>'.$GLOBALS['fdd'][$GLOBALS['fds'][$k]]['name'].'</td>'."\n";
    SetLinkOpt($GLOBALS['fds'][$k]);
    do_write_add_field($GLOBALS['fds'][$k]);
    echo '  </tr>'."\n";
  }
}

function do_change_record_form () {
  echo "    <!--// do_change_record_form //--!>\n";
  // for change: SQL SELECT to retrieve the selected record
  $qry = create_column_list_change($GLOBALS['opt']['tb'],$GLOBALS['fdd']);
  if (!in_array($GLOBALS['opt']['key'],$GLOBALS['fds'])) {
     $qry = $qry.','.fully_qualified_name($GLOBALS['opt']['tb'],$GLOBALS['fdd'],$GLOBALS['opt']['key'],"change");
  }
  $qry = $qry.create_from_clause($GLOBALS['opt']['tb'],$GLOBALS['fdd']);
  $qry_select = create_where_clause($GLOBALS['opt']['tb'],$GLOBALS['fdd']);
  if ($qry_select=='') {
    $kwd = ' WHERE ';
  } else {
    $kwd = ' AND ';
  }
  $qry = $qry.$qry_select.$kwd.'('.fully_qualified_name($GLOBALS['opt']['tb'],$GLOBALS['fdd'],$GLOBALS['opt']['key'], "change").' = '.$GLOBALS['opt']['key_delim'].$GLOBALS['opt']['rec'].$GLOBALS['opt']['key_delim'].')';
  pma_debug ('Change query '.$qry );
  $res = mysql_db_query($GLOBALS['opt']['db'],$qry);
  if ($res) {
    if ($row = mysql_fetch_array($res)){
      for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
        echo '  <tr>'."\n";
        echo '    <td>'.$GLOBALS['fdd'][$GLOBALS['fds'][$k]]['name'].'</td>'."\n";
        // table select vars setup
        SetLinkOpt($GLOBALS['fds'][$k]);
        // Field is not editable
        do_change_add_field($GLOBALS['fds'][$k], $row[$k]);
      } // for loop fields
      echo '  </tr>'."\n";
    }
  } else {
    echo  "change error : " . mysql_errno().": ".mysql_error()."<br/>";
  }
}

function do_record_delete($savedelete, $key, $rec) {
  // Deleting records
  $qry = 'DELETE FROM '.$GLOBALS['opt']['tb'].' WHERE ('.$key.' = '.$GLOBALS['opt']['key_delim'].$rec.$GLOBALS['opt']['key_delim'].')';
  $res = mysql_db_query($GLOBALS['opt']['db'],$qry);
  if ($res) {
   echo '<h5>'.mysql_affected_rows().' record deleted</h5>'."\n";
  } else {
    echo  "Delete error : " . mysql_errno().": ".mysql_error()."<br/>";
  }
}

// this is where you should add input verification
function make_valarr_from_postvar() {
  echo "    <!--// make_valarr_from_postvar //--!>\n";
  for ($k=0 ; $k<$GLOBALS['opt']['num_fds']; $k++) {
    $fieldname = $GLOBALS['fds'][$k];
    // if not editable, the date is already in the right format
    if (isset($GLOBALS['fdd'][$fieldname]['editable']) && ($GLOBALS['fdd'][$fieldname]['editable']==false)) {
        // do nothing as yet, needs cleaning up
    } else if ($GLOBALS['fdd'][$fieldname]['distinct'] == true){
         $GLOBALS['val'][$fieldname. "_new"] = $_POST[$fieldname."_new"];
         $GLOBALS['val'][$fieldname] = $_POST[$fieldname];
    } else {
      // fix date fields
      if (isset($GLOBALS['fdd'][$fieldname]['date'])) {
         $tmp1 = $fieldname . "year";
         $tmp2 = $fieldname . "month";
         $tmp3 = $fieldname . "day";
         $tmp = $_POST[$tmp1] . '-' . $_POST[$tmp2] . '-' .  $_POST[$tmp3];
         // echo "date input was " . $tmp;
         $GLOBALS['val'][$fieldname] = $tmp;
      } else {
         $GLOBALS['val'][$fieldname] = $_POST[$fieldname];
      }
    }
  }
}

?>
Return current item: phpMyAccess