Location: PHPKode > projects > The Referral Database > referralsdb/admin_fns.php
<?php
// This file contains functions used by the admin interface 
// for the Referrals database

function display_category_form($category = "")
// This displays the category form.
// This form can be used for inserting or editing categories.
// To insert, don't pass any parameters.  This will set $edit
// to false, and the form will go to insert_category.php.
// To update, pass an array containing a category.  The
// form will contain the old data and point to update_category.php.
// It will also add a "Delete category" button.
{
  // if passed an existing category, proceed in "edit mode"
  $edit = is_array($category);

  // most of the form is in plain HTML with some
  // optional PHP bits throughout
?>
  <form method=post 
      action="<?php echo $edit?"edit_category.php":"insert_category.php";?>"> 
  <table border=0>
  <tr>
    <td>Category Name:</td>
    <td><input type=text name=catname size=40 maxlength=40
          value="<?php echo $edit?$category["catname"]:""; ?>"></td>
   </tr>
  <tr>
    <td <?php if (!$edit) echo "colspan=2"; ?> align=center>
      <?php if ($edit) 
         echo "<input type=hidden name=catid 
                value=\"".$category["catid"]."\">";
         echo "<input type=hidden name=oldcatname 
                value=\"".$category["catname"]."\">";

      ?>
      <input type=submit 
       value="<?php echo $edit?"Rename":"Add"; ?> Category"></form>
     </td>
     <?php if ($edit)
       // allow deletion of existing categories 
       {
          echo "<td>";
          echo "<form method=post action=\"delete_category.php\">";
          echo "<input type=hidden name=catid value=\"".$category[catid]."\">";
          echo "<input type=submit value=\"Delete category\">";
          echo "</form></td>";
       }
     ?>
  </tr>
  </table>
<?php
}


function display_organization_form($organization = "")
// This displays the organization form.
// It is very similar to the category form.
// This form can be used for inserting or editing organizations.
// To insert, don't pass any parameters.  This will set $edit
// to false, and the form will go to insert_organization.php.
// To update, pass an array containing a organization.  The
// form will be displayed with the old data and point to update_organization.php.
// It will also add a "Delete organization" button.

{
  
  // if passed an existing organization, proceed in "edit mode"
  $edit = is_array($organization);
  if ($edit)
  {
    list($orgid, $orgcode, $orgtitle, $orgreferralnum, $orglastupdated, $orgdetails, $orghours, $orgbusinessphone, $orgaddress, $orgcity, $orgstate, $orgzip, $orgemail, $orgwebsite, $orgcseligibility, $orgfeesins, $orgdirector, $orgcontact, $orgspecialinst) = $organization;
  }

  // most of the form is in plain HTML with some
  // optional PHP bits throughout
?>
  <form method=post
        action="<?php echo $edit?"edit_organization.php":"insert_organization.php";?>">
  <table border=0>
  <?php 
  if ($edit) 
  {
   echo "<input type=hidden name=Org_ID value=$orgid";
  }
  ?>
  <tr>
    <font class="required">*</font> denotes a required field
    <td>Name:</td>
    <td class="required"><input type=text name=Org_Name size=30
         value="<?php echo $edit?$orgtitle:""; ?>"> *</td>
  </tr>
  <tr>
     <td>Referral Code:</td>
     <td class="required"><input type=text name=Org_Code size=6
         value="<?php echo $edit?$orgcode:""; ?>"> *</td>
  </tr>
  <tr>
    <td>Referral Number:</td>
    <td class="required"><input type=text size=10 maxlength=10 name=Org_Referral_Num 
         value="<?php echo $edit?$orgreferralnum:""; ?>"> *</td>
  </tr>
  <tr>
    <td>Services Offered:</td>
    <td class="required"><input type=text name=Org_Services_Offered size=50
         value="<?php echo $edit?$orgdetails:""; ?>"> *</td>
  </tr>
  <tr>
    <td>Last Updated:</td>
    <td><input type=text size=10 maxlength=10 name=Org_Last_Update 
         value="<?php echo $edit?$orglastupdated:""; ?>">  YYYY-MM-DD</td>
  </tr>
  <tr>
    <td>Hours of Operation:</td>
    <td><input type=text name=Org_Hours 
         value="<?php echo $edit?$orghours:""; ?>"></td>
  </tr>
  <tr>
    <td>Business Phone Number:</td>
    <td><input type=text size=10 maxlength=10 name=Org_Business_Phone 
         value="<?php echo $edit?$orgbusinessphone:""; ?>">  10 digits, no seperators (i.e. no "(" or "-")</td>
  </tr>
  <tr>
    <td>Address:</td>
    <td><input type=text name=Org_Address 
         value="<?php echo $edit?$orgaddress:""; ?>"></td>
  </tr>
  <tr>
    <td>City:</td>
    <td><input type=text name=Org_City 
         value="<?php echo $edit?$orgcity:""; ?>"></td>
  </tr>
  <tr>
    <td>State:</td>
    <td><input type=text name=Org_State 
         value="<?php echo $edit?$orgstate:""; ?>"></td>
  </tr>
  <tr>
    <td>Zip:</td>
    <td><input type=text name=Org_Zip 
         value="<?php echo $edit?$orgzip:""; ?>"></td>
  </tr>
  <tr>
    <td>Email Address:</td>
    <td><input type=text name=Org_Email 
         value="<?php echo $edit?$orgemail:""; ?>"></td>
  </tr>
  <tr>
    <td>Web Page:</td>
    <td><input type=text name=Org_Web_Page 
         value="<?php echo $edit?$orgwebsite:""; ?>"></td>
  </tr>
  <tr>
    <td>Clients Served/Eligibility::</td>
    <td><input type=text name=Org_Clients_Served_Eligibility 
         value="<?php echo $edit?$orgcseligibility:""; ?>"></td>
  </tr>
  <tr>
    <td>Fees for Service/Insurance:</td>
    <td><input type=text name=Org_Fees_Insurance 
         value="<?php echo $edit?$orgfeesins:""; ?>"></td>
  </tr>
  <tr>
    <td>Organization Director:</td>
    <td><input type=text name=Org_Director 
         value="<?php echo $edit?$orgdirector:""; ?>"></td>
  </tr>
  <tr>
    <td>Contact Person:</td>
    <td><input type=text name=Org_Contact 
         value="<?php echo $edit?$orgcontact:""; ?>"></td>
  </tr>
  <tr>
    <td>Special Instructions:</td>
    <td><input type=text name=Org_Special_Instructions
         value="<?php echo $edit?$orgspecialinst:""; ?>"></td>
  </tr>
  <tr>
    <td><B>Member of categories:</B></td>
  </tr>
      <?php
          $x = 1;
          // list of possible categories comes from database
          $cat_array = get_categories();
          // get the categories that this organization is a member of
          if ($edit)
          {
            $categories = get_organization_categories($orgid);
          }
          echo "<table cellspacing=0 cellpadding=0 border=0>";
          echo "<tr>";
          foreach ($cat_array as $thiscat)
          {
               /* centered label, checkbox with right center align */
               echo "<td valign=center width=120 align=center>$thiscat[1]</td><td valign=center align=left><input type=\"checkbox\" name=\"box";
               echo $thiscat[0];
               echo "\"";
               // if existing organization, put in current catgories
               if ($edit) {
                 if (is_array($categories)) {
                   foreach ($categories as $row)
                   {
                     $catid= $row[0];
                     if ($edit && $thiscat[0] == $catid) 
                     {
                         echo " checked"; 
                         echo ">"; 
                     }
                   }    
                 }
                 else // For some reason, this organization has no categories
                 {
                   echo ">";
                 }
               }
               echo "</td>";
               echo "<td><image src=images/pad.gif width=20></td>";
               if ($x == 4) 
               { 
                 $x = 0;
                 echo "</tr>";
                 echo "<tr>";
               }

               $x++;
          }
          echo "</table>";
          ?> 
    </tr>
    </table>
    <p>
    <input type=submit
     value="<?php echo $edit?"Update":"Add"; ?> Organization">
    <?php 

       if ($edit)
       {  
         echo "</form>";
         // this button gets it's own form b/c it submits to a diff place
         echo "<form method=post action=\"delete_organization.php\">";
         echo "<input type=hidden name=Org_ID value=\"$orgid\">";
         echo "<input type=submit 
                value=\"Delete organization\">";
         echo "</form>";
       }
       else 
       {
         echo "</form>";
       }
    ?>
<?php
}

function display_password_form()
{
// displays html change password form
?>
   <br>
   <form action="change_password.php" method=post>
   <table width=250 cellpadding=2 cellspacing=0 bgcolor=#cccccc>
   <tr><td>Old password:</td>
       <td><input type=password name=old_passwd size=16 maxlength=16></td>
   </tr>
   <tr><td>New password:</td>
       <td><input type=password name=new_passwd size=16 maxlength=16></td>
   </tr>
   <tr><td>Repeat new password:</td>
       <td><input type=password name=new_passwd2 size=16 maxlength=16></td>
   </tr>
   <tr><td colspan=2 align=center><input type=submit value="Change password">
   </td></tr>
   </table>
   <br>
<?php
};


function extract_categories($form_vars)
{
   /*********************************************
   *
   * Go through the HTML form's post variables and see what categories
   * the organization is a member of
   *
   *********************************************/

  foreach ($form_vars as $key => $value)
  {
    if (strstr($key, "box"))
      {
        $categories[] = substr($key, 3);
      }
  } 
  if (!is_array($categories))
  {
    echo "<b>Error updating organization.</b><p>";
    echo "You must select at least one category.";
    return false;
  }
  return $categories;
}

function update_category($catid, $catname)
// change the name of category with catid in the database
{
   $conn = db_connect();

   $query = "select * from Categories where Cat_Name='$catname'";
   $result = mysql_query($query);
   if (!$result) {
      return 0;
   }

   $numresults = mysql_num_rows($result);
   if (!"0" == $numresults) {
      return 2;
   } else {
      $query = "update Categories
                set Cat_Name='$catname'
                where Cat_ID='$catid'";
      $result = mysql_query($query);
      if (!$result) {
        return 0;
      } else {
        return 1; 
      }
   }
}

function delete_category($catid)
// Remove the category identified by catid from the db
// If there are organizations in the category, it will not
// be removed and the function will return false.
{
   $conn = db_connect();
   
   // check if there are any organizations in category 
   // to avoid deletion anomalies   
   $query = "select *
             from Organizations, Organization_Categories 
             where Organizations.Org_ID = Organization_Categories.Org_ID 
             and Organization_Categories.Cat_ID = '$catid' 
             order by Organizations.Org_Name";
   $result = @mysql_query($query);
   if (!$result || @mysql_num_rows($result)>0)
     return false;

   $query = "delete from Categories 
             where Cat_ID='$catid'";
   $result = @mysql_query($query);
   if (!$result)
     return false;
   else
     return true; 
}


function delete_organization($orgid)
// Deletes the organization identified by $orgid from the database.
{
   $conn = db_connect();

   $query = "delete from Organizations
             where Org_ID='$orgid'";
   $result = @mysql_query($query);
   if (!$result)
     return false;
   else
     return true;
}

?>
Return current item: The Referral Database