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

// Has the form been submitted
if ($btnSubmit) {
  // are required fields were filled out
    if ($type_id) {
        $strSerial = validateText("Serial Number", $txtSerial, 2, 35, FALSE, FALSE);
        
        if (!$strError AND $software_id AND $strSerial) {
            $strSQLerr = "SELECT COUNT(*) FROM software WHERE type_id=$type_id AND
              accountID=$accountID AND serial='$strSerial' AND id!=$software_id";
        } elseif (!$strError AND $hardware_id AND $strSerial) {
            $strSQLerr = "SELECT COUNT(*) FROM software WHERE type_id=$type_id AND
              accountID=$accountID AND serial='$strSerial'";
        }
        If (!$strError AND $strSQLerr AND $strSerial) {
            $resulterr = dbquery($strSQLerr);
            $rowerr = mysql_fetch_row($resulterr);
            If ($rowerr[0] > 0) {
                $strError = "That serial number already exists.";
            }
        }
        
        if (!$strError) {
            // Pick the proper sql statement to query the database
            if ($software_id) {
                $strSQL = "UPDATE software SET serial='$strSerial' WHERE accountID=$accountID AND id=$software_id";
                $strError = "Record updated successfully.";
            } elseif ($hardware_id) {
                $strSQL = "INSERT INTO software (serial, hardware_id, type_id, accountID)
                  VALUES ('$strSerial', $hardware_id, $type_id, $accountID)";
                $strError = "Record created successfully.";
            }

            if ($strSQL) {
                $result = dbquery($strSQL);
                $noShow = 1;
            } else {
                $strError = "Error: Be sure you haven't altered the URL.";
            }
        }
    }
}

// If you're editing preload the vars
if ($software_id != "" AND !$delete) {
  // find the Software we're looking for
  $strSQL = "SELECT * FROM software as s, software_types as st WHERE s.type_id=st.id AND s.id=$software_id AND st.accountID=$accountID";
  $result = dbquery($strSQL);

  $row = mysql_fetch_array($result);
  $strSerial     = $row["serial"];
  $hardware_id   = $row["hardware_id"];
  $type_id       = $row["type_id"];
  $strName       = $row['Name'];
  $strMaker      = $row['Maker'];
  $strVersion    = $row['Version'];
  
  $actionWord = "Edit";
} Else {
  $actionWord = "Add";
}

if ($hardware_id == "" AND !$delete){
  $strError = "Error: Be sure you haven't altered the querystring.";
}

if (($numLicenses != "") AND ($numLicenses != "N/A") AND ($numLicenses < 1)) {
    fillError("Please be aware: you have no more licenses for this product.");
}

writeHeader("$actionWord Software");
declareError(TRUE);

if ($hardware_id != "" AND $type_id != "") {
  // if we're adding and we've already selected a Software type then
  // we need to get the serial number and external variables
  $strSQL = "SELECT * FROM software_types WHERE id=$type_id AND accountID=$accountID";
  $result = dbquery($strSQL);

  $row = mysql_fetch_array($result);
  $strName    = $row['Name'];
  $strMaker   = $row['Maker'];
  $strVersion = $row['Version'];
?>
  <FORM METHOD="post" ACTION="<? echo $PHP_SELF ?>">
  <INPUT TYPE="hidden" NAME="software_id" VALUE="<? echo $software_id; ?>">
  <INPUT TYPE="hidden" NAME="hardware_id" VALUE="<? echo $hardware_id; ?>">
  <INPUT TYPE="hidden" NAME="type_id" VALUE="<? echo $type_id; ?>">
  <INPUT TYPE="hidden" NAME="uid" VALUE="<? echo $uid; ?>">
  <table border='0' cellpadding='3' width='100%'>
    <tr>
      <td width='106'><u>Name</u>:
      </td>
      <td width='450'><? echo $strName; ?>
      </td>
    </tr>
    <tr>
      <td width='106'><u>Manufacturer</u>:
      </td>
      <td width='450'><? echo $strMaker; ?>
      </td>
    </tr>
    <tr>
      <td width='106'><u>Version</u>:
      </td>
      <td width='450'><? echo $strVersion; ?>
      </td>
    </tr>
    <tr>
      <td width='106'><u>Serial Number</u>:
      </td>
      <td width='450'><INPUT SIZE="30" MAXSIZE="255" TYPE="Text" NAME="txtSerial" VALUE="<? echo antiSlash($strSerial); ?>">
      </td>
    </tr>
    <tr>
      <td colspan='2'>&nbsp;
      </td>
    </tr>
     <tr>
      <td colspan='2'><? If (!$noShow) { ?><INPUT TYPE="submit" NAME="btnSubmit" VALUE="Enter Information"><? } ?>
      &nbsp;</td>
    </tr>
  </table>
  </FORM>
<?
  If ($uid) {
      buildlist($hardware_id, $uid, 1);
  }
}

if ($type_id == "") {
  // if this is the first time we've entered this page then get the user
  // to select a Software type

?><P>
<A HREF="admin_software_types.php">Add New Type</A>
</P><?

  $strSQL = "SELECT * FROM software_types WHERE accountID=$accountID
    ORDER BY name ASC";
  $result = dbquery($strSQL);
?>
   <table border='0' cellpadding='4' cellspacing='0'>
     <TR class='title'>
       <TD valign='bottom'><b>Name</b></TD>
       <TD valign='bottom'><b>Manufacturer</b></TD>
       <TD valign='bottom'><b>Version</b></TD>
       <TD valign='bottom'><b>&nbsp;Remaining&nbsp;<br>&nbsp;Licenses</b></TD>
       <TD valign='bottom'><b>Action</b></TD>
     </TR>
<?
  while ($row = mysql_fetch_array($result)) {
       $type_id      = $row['id'];
       $strName      = $row['Name'];
       $strMaker     = $row['Maker'];
       $strVersion   = $row['Version'];
       $numLicenses  = $row['numLicenses'];
       $licenseType  = $row['licenseType'];

       If ($licenseType == "peruser") {
          $strSQL2 = "SELECT t.id FROM software as s, tblSecurity as t, 
             hardware as h WHERE s.type_id=$type_id AND s.hardware_id=h.pk_asset AND 
             h.userID=t.id AND s.sparePart='0' AND h.sparePart='0' AND t.accountID=$accountID
             GROUP BY t.id";
          $result2            = dbquery($strSQL2);
          $usedLicenses       = mysql_num_rows($result2);
          $remainingLicenses  = $numLicenses - $usedLicenses;

       } ElseIf ($licenseType == "persystem") {
          $strSQL2 = "SELECT h.pk_asset FROM software as s, hardware as h WHERE s.type_id=$type_id 
             AND s.hardware_id=h.pk_asset AND s.sparePart='0' AND h.accountID=$accountID
             GROUP BY h.pk_asset";
          $result2            = dbquery($strSQL2);
          $usedLicenses       = mysql_num_rows($result2);
          $remainingLicenses  = $numLicenses - $usedLicenses;
       } Else {
          $remainingLicenses  = "N/A";
       }

?>
  <TR class='<?=alternateRowColor()?>'>
    <TD><? echo $strName ?> &nbsp;</TD>
    <TD><? echo $strMaker ?> &nbsp;</TD>
    <TD><? echo $strVersion ?> &nbsp;</TD>
    <TD>&nbsp;<? echo $remainingLicenses ?> &nbsp;</TD>
    <TD>
      <A HREF="admin_software.php?hardware_id=<?=$hardware_id;?>&type_id=<?=$type_id;?>&uid=<?=$uid;?>&numLicenses=<?=$remainingLicenses?>">Add</A>
    </TD>
  </TR>
<?
  }
?></table><?
}

writeFooter();
?>
Return current item: PhpMyInventory