Location: PHPKode > projects > The Referral Database > referralsdb/organization.class.php
<?php
require_once('referrals_fns.php');

class Organization
{
  var $id;
  var $Org_ID;
  var $code;
  var $Org_Name;
  var $referralnum;
  var $lastupdated;
  var $details;
  var $hours;
  var $businessphone;
  var $address;
  var $city;
  var $state;
  var $zip;
  var $email;
  var $website;
  var $cseligibility;
  var $feesins;
  var $director;
  var $contact;
  var $specialinst;
  var $categories;

  var $columns;
  var $values;
  var $post_vars;

  function setOrg_ID($newOrg_ID)
  {
    $this->Org_ID = $newOrg_ID;
  }
  function getOrg_ID()
  {
    return $this->Org_ID;
  }

  function setOrg_Name($newOrg_Name)
  {
    $this->Org_Name = $newOrg_Name;
  }

   function getOrg_Name()
   {
      /***************************
      *
      * Query the database for the organization's name
      * Requires $this->Org_ID to be set
      *
      ***************************/

      // Get the organization details from the database
      $organization = get_organization_details($this->Org_ID);
      $this->setOrg_Name($organization[2]);
   }

  function gettitle()
  {
    return $this->title;
  }
  
  function setcategories()
  {
    $orgcategories = get_organization_categories($this->Org_ID);
    $this->categories = $orgcategories;
  }
  function getcategories()
  {
    return $this->categories;
  }

   function deletecategories()
   {
      /*********************************
      *
      * Deletes all data in Organization_Categoires table pertaining
      * to an organization identified by $this->Org_ID
      * deletecategories() is called when removing an organization,
      * before adding an organization, or before changing an organization
      * this way it is ensured that the table is "clean"
      *
      *********************************/

      // Connect to the database
      $conn = db_connect();
      if (!$conn)
      {
         // Database Error
         return 0;
      }

      $query = "delete from Organization_Categories 
                where Org_ID = $this->Org_ID";
      $result = mysql_query($query);

      if (!$result)
      {
         echo "<BR>Error erasing previous categores from the database:<BR>";
         echo_db_error();
         return false;
      }
   }
    

   function insertcategories($newcategories) 
   {
      /*********************************
      *
      * Insert categories of an organization into table Organization_Categories
      *
      * Called when adding a new organization or updating
      * a current organization
      *
      * $newcategories variable is passed as uni-enumerated array,
      * it contains the category numbers that the organization belongs to
      * This information is derived from POST variables, so it is current
      *
      *********************************/
      
      /* 
       * Parse category data passed to the function from the POST variables
       */
         // Initiate counter variable to the number of categories we
         // need to process.
         $iterations = count($newcategories);
         
         // Prepare the values for the database insert statement
         foreach($newcategories as $catid)
         {
            $x++;
            if ($x < $iterations) 
               { $seperator = ", "; } 
            else 
               { $seperator = ""; } 
            $inserts .= "('$this->Org_ID', '$catid', '')" . $seperator;
         }
      /*
       *
       */

      // First delete the previous categories that the organization belongs to
      $this->deletecategories();

      // Now, insert the current categories
      $query = "insert into Organization_Categories 
                values $inserts";
      $result = mysql_query($query);

      if (!$result)
      {
         // Database error
         echo "<BR>Error inserting categores into the database:<br>";
         echo_db_error();
         return 0;
      }

      else
      {
         return 1;
      }
   }

  function setpostvars($HTTP_POST_VARS)
  {
    $this->post_vars = $HTTP_POST_VARS;
    $this->Org_ID = $this->post_vars[Org_ID];
  }

  function setcolsvals($type)
  {
    foreach ($this->post_vars as $key => $value)
    {
      $key = addslashes($key);
      $value = addslashes($value);

      if (!strstr($key, "box"))
      {
        $seperator = ",";
        if ($type == "insert")
        {
          $this->columns .= "$key" . $seperator;
          $this->values .= "'$value'" . $seperator;
        }
        if ($type == "update")
        {
          if ($key == "Org_ID") // Org_ID is needed for updates
          {
             $this->Org_ID = $value;
          }
          else // don't save Org_ID for an update
          {
             $this->updates .= "$key = '$value',";
          }
        }
      }
    }
    if ($type == "insert")
    {
      $collength = strlen($this->columns);
      $vallength = strlen($this->values);
      $this->columns = substr($this->columns, 0, $collength-1);
      $this->values = substr($this->values, 0, $vallength-1);
      $this->columns .= ")";
      $this->values .= ")";
    }
    if ($type == "update")
    {
      $updlength = strlen($this->updates);
      $this->updates = substr($this->updates, 0, $updlength-1);
    }
  }

   function insertorg()
   {

      /*******************************************
      * Insert an organization into the database:
      * 
      * First we prepare the insert statements
      * Then insert the organization
      *
      * value of 0 means database error
      * value of 1 means the organization was inserted
      * value of 2 means no categories were set on HTML form
      * Make sure you handle error reporting when calling $this->insertorg()
      *
      * This function is only called when adding an organizaton -
      * when updating to an organization, use updateorg()
      *
      *******************************************/

      // Prepare the insert statements for insertion into the database
      $this->columns = "(Org_ID,";
      $this->values = "('Null',";
      
      // Tack on the data to the insert statements
      $this->setcolsvals('insert');
      
      // Set the categories that this organization is a member of
      $this->categories = extract_categories($this->post_vars);

      // No categories were set - stop & return 2
      if (!$this->categories)
      {
         return 2;
      }

      // Connect to the database
      $conn = db_connect();
      if (!$conn)
      {
         // Database Error
         return 0;
      }

      // Insert the organization
      $query = "insert into " . $GLOBALS["cfg"]["sqldatabase"] . 
               ".Organizations $this->columns values $this->values";
      $result = mysql_query($query);

      if (!$result)
      {
         // Database Error
         return 0;
      }

      else
      {
         // Get the Org_ID for the new organization
         $this->Org_ID = mysql_insert_id();
         
         $changestatus = $this->insertcategories($this->categories);
         if ($changestatus == 0)
         {
            return 0;
         }

         else
         {
            return 1;
         }
      }
  }

   function deleteorg()
   {
      /*******************************
      *
      * Deletes an organization from the database
      * Also deletes it's categories from Organization_Categories
      *
      * *** Prerequisits **************************************
      *    $this->Org_ID should be set through $this->setOrg_ID() before
      *    calling deleteorg()
      * 
      *    Check to see if organization_exists() before
      *    calling deleteorg()
      * *******************************************************
      *
      * Return values:
      * 0 means database error,
      * 1 means successful delete,
      *
      ********************************/

      // Delete the categories of the organization
      $this->deletecategories();

      // Now delete the organization
      $query = "delete from Organizations where Org_ID = $this->Org_ID";
      $result = mysql_query($query);

      if (!$result)
      {
         // Database error
         echo "<BR>Error deleting Organization:<BR>";
         echo_db_error();
         return 0;
      }
      
      else
      {
         // Successful delete
         return 1;
      }
   }

   function updateorg()
   {
      /*******************************
      *
      * Updates an existing organization in the database
      * Also updates it's categories in Organization_Categories
      *
      * *** Prerequisits **************************************
      * 
      *    Check to see if organization_exists() before
      *    calling updateorg()
      *
      * *******************************************************
      *
      * Return values:
      * 0 means database error,
      * 1 means successful update,
      *
      ********************************/

      // Set the categories that this organization is a member of
      $this->categories = extract_categories($this->post_vars);

      // If no categories were set, return false for user error reporting
      if (!$this->categories)
      {
         return false;
      }
      
      $this->insertcategories($this->categories);
      $this->setcolsvals('update'); 
      $query = "update " .$GLOBAL["cfg"]["sqldatabase"] . ".Organizations
                set $this->updates where Org_ID ='$this->Org_ID'";
      $conn = db_connect();
      $result = mysql_query($query);

      if (!$result)
      {
         // Database error
         echo "<BR>Error updating Organization:<BR>";
         echo_db_error();
         return false;
      }
      
      else
      {
         // Update successful
         return true;
      }
   }
  
}

// End Class Organization
?>
Return current item: The Referral Database