![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 /*------------------------------------------------------------------------------- 00003 * Xataface Web Application Framework 00004 * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *------------------------------------------------------------------------------- 00020 */ 00021 00032 class Dataface_ConfigWriter { 00033 00034 00035 function setupDB(){ 00036 if ( !is_a($this, 'Dataface_ConfigTool') ){ 00037 throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR); 00038 } 00039 $app =& Dataface_Application::getInstance(); 00040 $config = file_get_contents(DATAFACE_PATH.'/install/dbconfig.sql'); 00041 foreach ( explode(';',$config) as $query){ 00042 if (!trim($query)) continue; 00043 $res = mysql_query($query, $app->db()); 00044 if ( !$res ){ 00045 throw new Exception("Could not set up configuration database: ".mysql_error($app->db()), E_USER_ERROR); 00046 } 00047 } 00048 return true; 00049 } 00050 00051 function writeConfig($storage=null){ 00052 if ( !is_a($this, 'Dataface_ConfigTool') ){ 00053 throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR); 00054 } 00055 00056 $this->loadAllConfig(); 00057 $app =& Dataface_Application::getInstance(); 00058 00059 if ( $storage === null ) $storage = $app->_conf['config_storage']; 00060 00061 switch (strtolower($storage)){ 00062 case 'db': 00063 case 'database': 00064 case 'sql': 00065 return $this->writeConfigToDB(); 00066 case 'ini': 00067 return $this->writeConfigToINI(); 00068 } 00069 00070 } 00071 00072 function writeConfigToDB(){ 00073 import('Dataface/Table.php'); 00074 import('Dataface/Record.php'); 00075 import('Dataface/IO.php'); 00076 if ( !is_a($this, 'Dataface_ConfigTool') ){ 00077 throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR); 00078 } 00079 $this->loadAllConfig(); 00080 $app =& Dataface_Application::getInstance(); 00081 // first let's make copies of the current configuration. 00082 $timestamp = time(); 00083 foreach ( $this->configTypes as $type ){ 00084 $res = mysql_query("CREATE TABLE `__".addslashes($type)."__".$timestamp."` SELECT * FROM `__".addslashes($type)."__`", $app->db()); 00085 if ( !$res ){ 00086 throw new Exception("Failed to make backup of table '__".$type."__'.". mysql_error($app->db()), E_USER_ERROR); 00087 } 00088 } 00089 00090 $res = mysql_query("CREATE TABLE `__properties__".$timestamp."` SELECT * FROM `__properties__`", $app->db()); 00091 if ( !$res ){ 00092 throw new Exception("Failed to make backup of table '__properties__'.", $app->db()); 00093 } 00094 00095 // Now that we have made our backups, we can continue to write the configuration to the database. 00096 //print_r($this->config); 00097 foreach ( $this->configTypes as $type ){ 00098 00099 $res = mysql_query("DELETE FROM `__".addslashes($type)."__`", $app->db()); 00100 if ( !$res ){ 00101 throw new Exception("Failed to delete all records from table '__".$type."__'", $app->db()); 00102 } 00103 00104 00105 foreach ( $this->config[$type] as $tablename=>$tableConfig ){ 00106 foreach ( $tableConfig as $sectionname=>$section){ 00107 $tableObj =& Dataface_Table::loadTable('__'.$type.'__'); 00108 $record = new Dataface_Record('__'.$type.'__', array()); 00109 $record->useMetaData = false; // some of the field names begin with '__' which would conflict with dataface's handling of MetaData fields. 00110 00111 00112 foreach ( array_keys($tableObj->fields()) as $fieldname ){ 00113 $record->setValue($fieldname, @$section[$fieldname]); 00114 unset($section[$fieldname]); 00115 } 00116 $record->setValue('name',$sectionname); 00117 $record->setValue('table', $tablename); 00118 //echo nl2br("Section name: $sectionname\nTable: $tablename\n"); 00119 //print_r($record->strvals()); 00120 00121 echo nl2br("\nWriting section: $sectionname : "); 00122 print_r($record->strvals()); 00123 00124 // now that we have created the record, we write the record 00125 $io = new Dataface_IO('__'.$type.'__'); 00126 $res = $io->write($record); 00127 if ( PEAR::isError($res) ){ 00128 throw new Exception($res->toString(), E_USER_ERROR); 00129 } else if (!$res ){ 00130 throw new Exception("Failure to write to database for unknown reason.", E_USER_ERROR); 00131 } 00132 00133 // now for the rest of the properties. 00134 foreach ( $section as $propertyName=>$propertyValue ){ 00135 $res = mysql_query(" 00136 INSERT INTO 00137 `__properties__` 00138 (`parent_id`,`parent_type`,`property_name`,`property_value`) 00139 VALUES 00140 ('".$record->val($type.'_id')."', 00141 '".addslashes($type)."', 00142 '".addslashes($propertyName)."', 00143 '".addslashes($propertyValue)."')", $app->db()); 00144 if ( !$res ){ 00145 throw new Exception("Failed to add property '$propertyName' to table '__properties__' with value '$propertyValue'".mysql_error($app->db()), E_USER_ERROR); 00146 } 00147 } 00148 00149 unset($tableObj); 00150 unset($record); 00151 unset($io); 00152 } 00153 } 00154 00155 } 00156 00157 } 00158 00159 }