database.php

Go to the documentation of this file.
00001 <?php
00032 class Database {
00033     private $USERINFOID = 0;            
00035     private $db;                        
00036     private $connected = FALSE;         
00037     private $dberrormsg;                
00044     function __construct() {
00045         /* try to connect to the database using defined parameters */
00046         $this->db = @mysql_connect(DB_HOST, DB_USER, DB_PWD);
00047 
00048         /* select database and save connection status */
00049         $this->connected = ($this->db && @mysql_select_db(DB_NAME));
00050 
00051         /* if not connected, save error message */
00052         if (!$this->connected) { $this->dberrormsg = mysql_error(); }
00053     }
00054 
00066     private function createExtensionTable($id_type) {
00067         /* get the field list */
00068         $sql =  "SELECT c.field_name AS field_name, f.type_specs AS type_specs " .
00069                 "FROM contenttype_defs AS c " .
00070                 "LEFT JOIN fieldtypes AS f ON c.field_type = f.id " .
00071                 "WHERE c.id = " . $id_type . " " .
00072                 "ORDER BY c.id_field";
00073         $res = mysql_query($sql);
00074 
00075         if ($res === FALSE) { return FALSE; }
00076 
00077         $sql = "";
00078         while ($line = mysql_fetch_assoc($res)) {
00079             $sql .= "`" . $line['field_name'] . "` " . $line['type_specs'] . ", ";
00080         }
00081         mysql_free_result($res);
00082 
00083         $sql =  "CREATE TABLE IF NOT EXISTS `_cnt_" . $id_type .
00084                 "` (`id` bigint unsigned NOT NULL, " . $sql .
00085                 "CONSTRAINT fk_cnt_" . $id_type . " FOREIGN KEY (`id`) REFERENCES content (`id`) ON DELETE CASCADE) " .
00086                 "TYPE=InnoDB DEFAULT CHARSET=utf8";
00087         return mysql_query($sql);   
00088     }
00089 
00098     private function getExtendedFieldList($id_type) {
00099         $sql =  "SELECT field_name FROM contenttype_defs " .
00100                 "WHERE id = " . $id_type . " " .
00101                 "ORDER BY id_field";
00102         $res = mysql_query($sql);
00103 
00104         if ($res === FALSE) { return ""; }      // not critical, go on
00105 
00106         while ($line = mysql_fetch_assoc($res)) {
00107             $fieldlist .= ", cnt." . $line['field_name'] . " AS " . $line['field_name'];
00108         }
00109         mysql_free_result($res);
00110         return $fieldlist;      
00111     }
00112 
00121     private function createContentView($id_type) {
00122         $viewname = (($id_type > 0) ? "_cntview_" . $id_type : "_usrview");
00123         $sql =  "CREATE OR REPLACE VIEW `" . $viewname . "` AS " .
00124                 "SELECT c.id AS id, " .
00125                 "c.id_area AS id_area, " .
00126                 "c.id_parent AS id_parent, " .
00127                 "c.id_type AS id_type, " .
00128                 "c.id_owner AS id_owner, " .
00129                 "u.userid as userid, " .
00130                 "c.visible AS visible, " .
00131                 "c.position AS position, " .
00132                 "c.readperm AS readperm, " .
00133                 "c.writeperm AS writeperm, " .
00134                 "c.group_readperm AS group_readperm, " .
00135                 "c.group_writeperm AS group_writeperm, " .
00136                 "c.counter AS counter, " .
00137                 "c.date_created AS date_created, " .
00138                 "c.date_lastmodified AS date_lastmodified, " .
00139                 "c.date_expiration AS date_expiration, " .
00140                 "((date_expiration > 0) AND (UNIX_TIMESTAMP(NOW()) > date_expiration)) AS expired" .
00141                 $this->getExtendedFieldList($id_type) .
00142                 " FROM content AS c " .
00143                 "JOIN _cnt_" . $id_type . " AS cnt ON c.id = cnt.id " .
00144                 "LEFT JOIN users AS u ON c.id_owner = u.id";
00145         return mysql_query($sql);
00146     }
00147 
00153     private function createUserInfoView() {
00154         $sql =  "CREATE OR REPLACE VIEW `_usrview` AS " .
00155                 "SELECT u.id AS id, " .
00156                 "u.id_area AS id_area, " .
00157                 "u.pin AS pin, " .
00158                 "u.userid AS userid, " .
00159                 "u.pwd AS pwd, " .
00160                 "u.email AS email, " .
00161                 "u.admin AS admin, " .
00162                 "u.enabled AS enabled, " .
00163                 "u.groups AS groups, " .
00164                 "u.datecreated AS datecreated" .
00165                 $this->getExtendedFieldList($this->USERINFOID) .
00166                 " FROM users AS u " .
00167                 "LEFT JOIN _cnt_" . $this->USERINFOID . " AS cnt " .
00168                 "ON u.id = cnt.id";
00169         return mysql_query($sql);
00170     }
00171 
00175     public function disconnect() {
00176         @mysql_close($this->db);
00177         $this->connected = FALSE;
00178     }
00179 
00185     public function isConnected() {
00186         return $this->connected;
00187     }
00188 
00194     public function errorMsg() {
00195         return $this->dberrormsg;
00196     }
00197 
00206     public function checkTables() {
00207         mysql_query("BEGIN");
00208         $ok = (($res = mysql_query("SELECT id FROM contenttypes")) !== FALSE);
00209         while ($ok && ($line = mysql_fetch_assoc($res))) {
00210             if ($ok = $this->createExtensionTable($line['id'])) {
00211                 if ($line['id'] > 0) {
00212                     $ok = $this->createContentView($line['id']);
00213                 } else {
00214                     $ok = $this->createUserInfoView();
00215                 }
00216             }
00217         }
00218         if ($ok) {
00219             mysql_query("COMMIT");
00220         } else {
00221             mysql_query("ROLLBACK");
00222         }
00223         mysql_free_result($res);
00224         return $ok;
00225     }
00226 }
00227 ?>

Generated on Fri Feb 22 11:19:48 2008 for CMSkey by  doxygen 1.5.3