Location: PHPKode > projects > Knowledge Box > knowledgebox/classes/substanceClass.php
<?php

  /*
   * Free IT Foundation
   * Free Technology Serving Knowledge
   * http://www.free-it-foundation.org
   * 
   * This file is part of Knowledge Box.
   * 
   * Knowledge Box is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   * 
   * Knowledge Box is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with Knowledge Box.  If not, see <http://www.gnu.org/licenses/>.
   */


  /*
   * class
   * KBModuleSubstance
   */
  class KBModuleSubstance extends CDpObject
  {
    var $entry_id;
    var $field_id;
    var $textvalue = "";
    var $binaryvalue = "";
    var $mime = "";
    var $information = "";


    /*
     * constructor
     */
    function KBModuleSubstance ()
    {
      // void
    }
    
    /*
     * getEntryId ()
     */
    function getEntryId ()
    {
      return $this->entry_id;
    }
    
    /*
     * setEntryId ()
     */
    function setEntryId ($entry_id)
    {
      $this->entry_id = (int) $entry_id;
    }

    /*
     * getFieldId ()
     */
    function getFieldId ()
    {
      return $this->field_id;
    }
    
    /*
     * setFieldId ()
     */
    function setFieldId ($field_id)
    {
      $this->field_id = (int) $field_id;
    }

    /*
     * getTextValue ()
     */
    function getTextValue ()
    {
      return $this->textvalue;
    }
    
    /*
     * setTextValue ()
     */
    function setTextValue ($textvalue)
    {
      $this->textvalue = $textvalue;
    }
    
    /*
     * getBinaryValue ()
     */
    function getBinaryValue ()
    {
      return $this->binaryvalue;
    }
    
    /*
     * setBinaryValue ()
     */
    function setBinaryValue ($binaryvalue)
    {
      $this->binaryvalue = $binaryvalue;
    }
    
    /*
     * getMimeType ()
     */
    function getMimeType ()
    {
      return $this->mime;
    }
    
    /*
     * setMimeType ()
     */
    function setMimeType ($mime)
    {
      $this->mime = $mime;
    }
    
    /*
     * getInformation ()
     */
    function getInformation ()
    {
      return $this->information;
    }
    
    /*
     * setInformation ()
     */
    function setInformation ($information)
    {
      $this->information = $information;
    }

    /*
     * store ()
     */
    function store ()
    {
      // free
      $this->delete ();

      // insert
      $sql  = "INSERT INTO `" . KB_TABLE_SUBSTANCES . "`";
      $sql .= " ( `entry_id` , `field_id` , `textvalue` , `binaryvalue` , `mime` , `information` )";
      $sql .= " VALUES (";
      $sql .= "  '" . $this->entry_id . "', '" . $this->field_id . "',";
      $sql .= "  '" . addslashes ($this->textvalue) . "', '" . $this->binaryvalue . "',";
      $sql .= "  '" . addslashes ($this->mime) . "',";
      $sql .= "  '" . addslashes ($this->information) . "'";
      $sql .= ")";

      // execute
      db_exec ($sql);
    }

    /*
     * delete ()
     */
    function delete ()
    {
      // sql
      $sql  = "DELETE FROM " . KB_TABLE_SUBSTANCES;
      $sql .= " WHERE entry_id = " . $this->entry_id;
      $sql .= " AND field_id = " . $this->field_id;

      // execute
      db_exec ($sql);
    }
    
    /*
     * getSubstancesByField ()
     */
    function getSubstancesByField ($field_id)
    {
      $r = array ();

      // sql (don't get the binary value. quite slow!)
      $sql  = "SELECT entry_id, field_id, textvalue, mime, information FROM " . KB_TABLE_SUBSTANCES;
      $sql .= " WHERE field_id = " . $field_id;

      // load
      $hash = db_loadList ($sql);

      // bind
      foreach ($hash as $row)
      {
        $obj = new KBModuleSubstance ();
        bindHashToObject ($row, $obj);
        
        $r [] = $obj;
      }

      // return
      return $r;
    }
    
    /*
     * getSubstancesByEntry ()
     */
    function getSubstancesByEntry ($entry_id)
    {
      $r = array ();

      // sql (don't get the binary value. quite slow!)
      $sql  = "SELECT entry_id, field_id, textvalue, mime, information FROM " . KB_TABLE_SUBSTANCES;
      $sql .= " WHERE entry_id = " . $entry_id;

      // load
      $hash = db_loadList ($sql);

      // bind
      foreach ($hash as $row)
      {
        $obj = new KBModuleSubstance ();
        bindHashToObject ($row, $obj);
        
        $r [] = $obj;
      }

      // return
      return $r;
    }
    
    /*
     * getSubstance ()
     */
    function getSubstance ($entry_id, $field_id)
    {
      // sql (don't get the binary value. quite slow!)
      $sql  = "SELECT entry_id, field_id, textvalue, mime, information FROM " . KB_TABLE_SUBSTANCES;
      $sql .= " WHERE entry_id = '" . (int) $entry_id . "'";
      $sql .= " AND field_id = '" . (int) $field_id . "'";

      // load
      $obj = new KBModuleSubstance ();

      // bind
      db_loadObject ($sql, $obj);

      // return
      return $obj;
    }
    
    /*
     * getBinaryBytes ()
     */
    function getBinaryBytes ()
    {
      global $dPconfig;
      
      // bypass the dotProject framework
      $db = mysql_connect ($dPconfig ['dbhost'], $dPconfig ['dbuser'], $dPconfig ['dbpass'])
            or die ('Couldn\'t connect to database server');

      // select db
      $dblink = mysql_select_db ($dPconfig ['dbname'], $db)
                or die ('Couldn\'t connect to database ' . $dPconfig ['dbname']);

      // query
      $sql  = "SELECT binaryvalue FROM " . KB_TABLE_SUBSTANCES;
      $sql .= " WHERE entry_id = " . $this->entry_id;
      $sql .= " AND field_id = " . $this->field_id;

      // record
      $record = mysql_query ($sql)
                or die ('Couldn\'t get record!');

      // empty record
      if (mysql_num_rows ($record) == 0)
      {
        print ('Couldn\'t get record!');
        exit ();
      }

      // get bytes
      $bytes = @mysql_result ($record, 0, 'binaryvalue');
      
      // return
      return $bytes;
    }

  }

?>
Return current item: Knowledge Box