• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilElement.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00025 
00036 class ilElement {
00037 
00043     var $dbHandle;
00044 
00050     var $dbTable;
00051 
00057     var $data = array();
00058 
00063     function Element() {
00064                 // empty
00065     }
00066 
00073     function set($data) {
00074         // Has to be overwritten by object class
00075                 //$this->data = $data;
00076     }
00077 
00083     function get() {
00084         // Has to be overwritten by object class        
00085         //return $this->data ;
00086     }
00087 
00094     function setDbTable($AdbTable) {
00095         if ($AdbTable == "") {
00096             die("ilElement::setDbTable(): No database table given.");
00097         } else {
00098             $this->dbTable = $AdbTable;
00099         }
00100     }
00101 
00108     function getDbTable() {
00109         return $this->dbTable;
00110     }
00111 
00118     function setDbHandle($AdbHandle) {
00119         if ($AdbHandle == "") {
00120                         die("<b>Error: No Database handle given!</b><br>class: ".get_class($this)."<br>Script: ".__FILE__."<br>Line: ".__LINE__);
00121         } else {
00122             $this->dbHandle = $AdbHandle;
00123         }
00124     }
00125 
00132     function getDbHandle() {
00133         return $this->dbHandle;
00134     }
00135 
00142     function queryDb($query) {
00143         $this->checkDb("ilElement::queryDb()");
00144         //query is empty?
00145         if ($query == "") {
00146             die("ilElement::queryDb(): No query given.");
00147         }
00148         //evaluate the result
00149         $result = $this->dbHandle->query($query);
00150         if (DB::isError($result)) {
00151             die("ilElement::queryDb(): ".$result->getMessage());
00152         }
00153 
00154         return true;
00155     }
00156 
00164     function updateDb($unique, $value) {
00165         $this->checkDb("ilElement::updateDb()");
00166         //check unique
00167         if ($unique == "") {
00168             die("ilElement::updateDB(): No unique database field given.");
00169         }
00170         //check value
00171         if ($value == "") {
00172             die("ilElement::updateDB(): No value given.");
00173         }
00174         //check the private data-array
00175         if (!is_array($this->data)) {
00176             die("ilElement::updateDB(): No data given.");
00177         }
00178         //build query
00179         $subq = "";
00180         while (list($key, $val) = each($this->data)) {
00181             if (substr($val, 0, 9) == "password(") {
00182                 $val = trim($val);
00183                 $val= substr($val, 10);
00184                 $val = substr($val, 0, strlen($val) - 2);
00185                 $subq .= $key. " = password(" . $this->dbHandle->quote($val . "") . "), ";
00186             } else {
00187                 $subq .= $key . " = ". $this->dbHandle->quote($val) . ", ";
00188             }
00189 
00190         }
00191         /* alt
00192         while (list($key, $val) = each($this->data)) {
00193             $subq .= $key . " = " . $this->dbHandle->quote($val) . ", ";
00194         }
00195         */
00196         //query is empty
00197         if ($subq == "") {
00198             die("ilElement::updateDB(): No data given.");
00199         }
00200         //truncate subq (there is a comma...)
00201         $subq = substr($subq, 0, strlen($subq)-2);
00202         //set query
00203         $q = "UPDATE " . $this->dbTable . " SET " . $subq . " WHERE " . $unique . " = " . $this->dbHandle->quote($value);
00204         $this->dbHandle->query($q);
00205         //evaluate result
00206         if (DB::isError($result)) {
00207             die("ilElement::updateDb(): ".$result->getMessage()." : $q");
00208         }
00209     } //end function updateDb
00210 
00218     function insertDb() {
00219         $this->checkDb("ilElement::insertDb()");
00220         //check data
00221         if (!is_array($this->data)) {
00222             die("ilElement::insertDB(): No data given.");
00223         }
00224         //build query
00225         $fields = "";
00226         $values = "";
00227         while (list($key, $val) = each($this->data)) {
00228             $fields .= $key . ", ";
00229             if (substr($val, 0, 9) == "password(") {
00230                 $val = trim($val);
00231                 $val= substr($val, 10);
00232                 $val = substr($val, 0, strlen($val) - 2);
00233                 $values .= "password(" . $this->dbHandle->quote($val . "") . "), ";
00234             } else {
00235                 $values .= $this->dbHandle->quote($val . "") . ", ";
00236             }
00237         }
00238         //check fields string
00239         if ($fields == "") {
00240             die("ilElement::insertDB(): No fields given.");
00241         }
00242         //check values-string
00243         if ($values == "") {
00244             die("ilElement::insertDB(): No values given.");
00245         }
00246         //truncate fields (there is a comma at the end...)
00247         $fields = substr($fields, 0, strlen($fields)-2);
00248         $values = substr($values, 0, strlen($values)-2);
00249         $q = "INSERT INTO " . $this->dbTable . " (" . $fields . ") VALUES (" . $values . ")";
00250 
00251         //evaluate result
00252         $result = $this->dbHandle->query($q);
00253         if (DB::isError($result)) {
00254             die("ilElement::insertDb(): ".$result->getMessage()." : $q");
00255         }
00256         //query the unique-key of inserted dataset
00257         $q = "SELECT LAST_INSERT_ID()";
00258         $this->result = $this->dbHandle->query($q);
00259         if (DB::isError($result)) {
00260             die("ilElement::insertDb()-Last_ID: ".$result->getMessage());
00261         }
00262         //query the result
00263         if ($data = $this->result->fetchRow()) {
00264             return $data[0];
00265         } else {
00266             return(0);
00267         }
00268     } //end function insertDb
00269 
00277     function getDbData($unique, $value) {
00278         $this->checkDb("ilElement::getDbData()");
00279         //check unique-key
00280         if ($unique == "") {
00281             die("ilElement::getDbData(): No unique database field given.");
00282         }
00283         //check value
00284         if ($value == "") {
00285             die("ilElement::getDbData(): No value given.");
00286         }
00287         //build query
00288         $q = "SELECT * FROM " . $this->getDbTable() . " WHERE " . $unique . " = " . $this->dbHandle->quote($value);
00289         $result = $this->dbHandle->query($q);
00290         //check result
00291         if (DB::isError($result)) {
00292             die("ilElement::getDbData(): ".$result->getMessage());
00293         }
00294         //return an associative array from query or false
00295         if ($result->numRows() > 0) {
00296             return $result->fetchRow(DB_FETCHMODE_ASSOC);
00297         } else {
00298             return false;
00299         }
00300     } //end function getDbData
00301 
00308     function getDbDataByQuery($query) {
00309         //check database handle
00310         if ($this->dbHandle == "") {
00311             die("ilElement::getDbDataByQuery(): No database handle given.");
00312         }
00313         //check query
00314         if ($query == "") {
00315             die("ilElement::getDbDataByQuery(): No query given.");
00316         }
00317         //send query
00318         $result = $this->dbHandle->query($query);
00319         //analyze resultset
00320         if (DB::isError($result)) {
00321             die("ilElement::getDbDataByQuery(): ".$result->getMessage());
00322         }
00323         //return associative array or false
00324         if ($result->numRows() > 0) {
00325             return $result->fetchRow(DB_FETCHMODE_ASSOC);
00326         } else {
00327             return false;
00328         }
00329     } //end function getDbDataByQuery
00330 
00335     function getDbValueByQuery($query, $field) {
00336         //check database handle
00337         if ($this->dbHandle == "") {
00338             die("ilElement::getDbDataByQuery(): No database handle given.");
00339         }
00340         //check query
00341         if ($query == "") {
00342             die("ilElement::getDbDataByQuery(): No query given.");
00343         }
00344         $result = $this->dbHandle->query($query);
00345         //analyze resultset
00346         if (DB::isError($result)) {
00347             die("ilElement::getDbValueByQuery(): ".$result->getMessage());
00348         }
00349         //return associative array or false
00350         if ($result->numRows() > 0) {
00351             $R = $result->fetchRow(DB_FETCHMODE_ASSOC);
00352             return( $R[$field] );
00353         } else {
00354             return false;
00355         }
00356         
00357     }
00358     
00359 } // end class Element
00360 
00361 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1