Location: PHPKode > projects > PhpMyInventory > pmi281/admin_hardware.php
<?
  Include("Includes/global.inc.php");
  checkPermissions(1, 1800);

if ($btnSubmit) { // the form has been submitted
    if (!$id) {
        $cboUser    = validateChoice("User", $cboUser);
    }
    $cboType        = validateChoice("System Type", $cboType);
    $strHW_Serial   = validateText("System Serial Number", $txtHW_Serial, 1, 255, FALSE, FALSE);
    $strIP          = validateIP("1", FALSE);
    $cboOS          = cleanFormInput($cboOS);
    $strOS_Serial   = validateText("OS Serial Number", $txtOS_Serial, 1, 255, FALSE, FALSE);
    If ($strOS_Serial AND !$cboOS) {
        fillError("Please select a primary operating system to go along with your OS serial number.");
    }
    
    if (!$strError AND !$id AND $strHW_Serial) {
        $strSQLerr = "SELECT COUNT(*) FROM hardware WHERE type=$cboType AND accountID=$accountID AND
          serial_num='$strHW_Serial'";
    } elseif (!$strError AND $id AND $strHW_Serial) {
        $strSQLerr = "SELECT COUNT(*) FROM hardware WHERE type=$cboType AND accountID=$accountID AND
          serial_num='$strHW_Serial' AND pk_asset!=$id";
    }
    If (!$strError AND $strHW_Serial) {
        $resulterr = dbquery($strSQLerr);
        $rowerr = mysql_fetch_row($resulterr);
        If ($rowerr[0] > 0) {
            $strError = "That serial number already exists.";
        }
    }
        
    if (!$strError) {
        If ($cboUser == "spare") {
            $cboUser  = "0";
            $spare    = "1";
        } ElseIf  ($cboUser == "independent") {
            $cboUser  = "0";
            $spare    = "2";
        } Else {
            $spare = "0";
        }
        
        if ($id) {
           $strSQL = "UPDATE hardware SET type=$cboType, serial_num='$strHW_Serial',
             ipAddress='$strIP' WHERE accountID=$accountID AND pk_asset=$id";
           $strNotify = "update";
        } else {
           $strSQL = "INSERT INTO hardware (type, serial_num, userID, ipAddress, sparePart, accountID)
             VALUES ($cboType, '$strHW_Serial', $cboUser, '$strIP', '$spare', $accountID)";
        }
        $result = dbquery($strSQL);
        
        if (!$id) {
            $hardwareID  = mysql_insert_id($db);
            $strNotify   = "insertSys&hardwareID=$hardwareID";
        }

        if ($id) {
           # $strSQL2 = "UPDATE software SET serial='$strOS_Serial', type_id=$cboOS WHERE accountID=$accountID AND id=$os_id";
        } elseif($cboOS) {
           $strSQL2 = "INSERT INTO software (serial, hardware_id, type_id, accountID) VALUES ('$strOS_Serial', $hardwareID, $cboOS, $accountID)";
             $strError = "Record created successfully.";

           $result2  = dbquery($strSQL2);
        }
        redirect("index.php", "notify=$strNotify&spare=$spare");
    } elseif (($spare === "0") AND $id) {
        $strSQL = "SELECT * FROM hardware as h, hw_types as t, tblSecurity as s WHERE
          s.id=h.userID AND h.type=t.type_pk AND h.pk_asset=$id AND t.accountID=$accountID";
        $result = dbquery($strSQL);
        While ($row = mysql_fetch_array($result)) {
            $strFirstName     = $row["firstName"];
            $strMiddleInit    = $row["middleInit"];
            $strLastName      = $row["lastName"];
        }
    }
} elseif ($id) {
    If ($spare === "0") {
       $strSQL = "SELECT * FROM hardware as h, hw_types as t, tblSecurity as s WHERE 
         s.id=h.userID AND h.type=t.type_pk AND h.pk_asset=$id AND t.accountID=$accountID";
    } Else {
       $strSQL = "SELECT * FROM hardware as h, hw_types as t WHERE 
         h.type=t.type_pk AND h.pk_asset=$id AND t.accountID=$accountID";
    }
    $result = dbquery($strSQL);

    While ($row = mysql_fetch_array($result)) {
        $cboType          = $row["type_pk"];
        $strHW_Serial     = $row["serial_num"];
        $spare            = $row["sparePart"];
        $strHwStatus      = $row["hardwareStatus"];
        $strIP            = $row["ipAddress"];
        If ($spare === "0") {
            $strFirstName     = $row["firstName"];
            $strMiddleInit    = $row["middleInit"];
            $strLastName      = $row["lastName"];
        }
    }

    $strSQL2 = "SELECT * FROM software WHERE accountID=$accountID AND hardware_id=$id";
    $result2 = dbquery($strSQL2);

    While ($row2 = mysql_fetch_array($result2)) {
        $cboOS         = $row2["type_id"];
        $strOS_Serial  = $row2["serial"];
        $os_id         = $row2["id"];
    }
}

if ($id) {
    $pageTitle = "Edit a System";
} else {
    $pageTitle = "Add a System";
}

$strSQLz = "SELECT * FROM hw_types WHERE accountID=$accountID ORDER BY type_desc ASC";
$resultz = dbquery($strSQLz);
if (mysql_num_rows($resultz) == 0) {
    fillError("You must create a <a href='admin_hw_types.php'>system type</a> before you can 
      add a system.");
    
    $noSystemTypes = TRUE;
}

$strSQLx = "SELECT * FROM software_types WHERE accountID=$accountID AND operatingSystem='1'
   ORDER BY name ASC";
$resultx = dbquery($strSQLx);
if (mysql_num_rows($resultx) == 0) {
    fillError("If you would like to associate an operating system (OS) with this new 
      hardware system, you should <a href='admin_software_types.php?bolOS=1'>define one</a>, first.");
}

writeHeader($pageTitle);
declareError(TRUE);

If (!$noSystemTypes) {
?>
<font color='ff0000'>*</font> Indicates a required field.<p>

<FORM METHOD="post" ACTION="<? echo $PHP_SELF?>">
<TABLE border='0' width='100%' cellpadding='4' cellspacing='0'>
   <TR>
      <TD width='110'><font color='ff0000'>*</font> User:</TD>
      <TD><?
  if ($id AND ($spare === "0")) {
      echo buildName($strFirstName, $strMiddleInit, $strLastName, 1);
  } elseif ($id AND ($spare === "1")) {
      echo "<i>Spare</i>";
  } elseif ($id AND ($spare === "2")) {
      echo "<i>Independent</i>";
  } else {
      echo buildUserSelect($cboUser, TRUE, "", TRUE);
  }
    ?></TD>
   </TR>
   <TR>
      <TD width='110'><font color='ff0000'>*</font> System Type:</TD>
      <TD>
         <SELECT SIZE="1" NAME="cboType" >
            <OPTION VALUE=''>&nbsp;</OPTION>
             <?
             // Get all hardware types for the drop down menu
             while ($rowz = mysql_fetch_array($resultz)) {
                  echo "   <OPTION VALUE=\"" . $rowz['type_pk'] . "\"";
                  if ($cboType == $rowz['type_pk']) {
                     echo " SELECTED";
                  }
                  echo ">".$rowz['type_desc']."</OPTION>\n";
             }
             ?>
         </SELECT> &nbsp;<a href='admin_hw_types.php'>Add System Type</a>
      </TD>
   </TR>
   <TR>
      <TD width='110'>Serial Number:</TD>
      <TD><INPUT SIZE="30" MAXSIZE="255" TYPE="Text" NAME="txtHW_Serial" VALUE="<? echo antiSlash($strHW_Serial); ?>"></TD>
   </TR>
   <TR>
      <TD width='110'>IP Address:</TD>
      <TD><? buildIP($strIP, "1"); ?></TD>
   </TR>

   <TR><TD colspan='2'>&nbsp;</TD></TR>

             <?
               If (!$id) {
             ?>
   <TR>
      <TD width='110'>Primary OS:</TD>
      <TD>
         <SELECT SIZE="1" NAME="cboOS" >
            <OPTION VALUE=''>&nbsp;</OPTION>
             <?
                 // Get all operating systems for the drop down menu
                 while ($rowx = mysql_fetch_array($resultx)) {
                     echo "   <OPTION VALUE=\"" . $rowx['id'] . "\"";
                     if ($cboOS == $rowx['id']) {
                         echo " SELECTED";
                     }
                     echo ">".$rowx['Name']." ".$rowx['Version']."</OPTION>\n";
                 }
             ?>
         </SELECT> &nbsp;<a href='admin_software_types.php?bolOS=1'>Add OS Type</a>
      </TD>
   </TR>
   <TR>
      <TD width='110'>OS Serial Number:</TD>
      <TD><INPUT SIZE="30" MAXSIZE="255" TYPE="Text" NAME="txtOS_Serial" VALUE="<? echo antiSlash($strOS_Serial); ?>"></TD>
   </TR>

   <TR><TD colspan='2'>&nbsp;</TD></TR>
             <?
               }
             ?>
   <TR>
      <TD colspan='2'><INPUT TYPE="submit" NAME="btnSubmit" VALUE="Enter Information"></TD>
   </TR>
  </TABLE>

  <input type='hidden' name='spare' value='<?=$spare;?>'>
  <input type="hidden" name="id" value="<?=$id;?>">
  <input type="hidden" name="os_id" value="<?=$os_id;?>">
</FORM>

<?
}
writeFooter();
?>
Return current item: PhpMyInventory