Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ConfigWriter.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 
33 
34 
35  function setupDB(){
36  if ( !is_a($this, 'Dataface_ConfigTool') ){
37  throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR);
38  }
40  $config = file_get_contents(DATAFACE_PATH.'/install/dbconfig.sql');
41  foreach ( explode(';',$config) as $query){
42  if (!trim($query)) continue;
43  $res = mysql_query($query, $app->db());
44  if ( !$res ){
45  throw new Exception("Could not set up configuration database: ".mysql_error($app->db()), E_USER_ERROR);
46  }
47  }
48  return true;
49  }
50 
51  function writeConfig($storage=null){
52  if ( !is_a($this, 'Dataface_ConfigTool') ){
53  throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR);
54  }
55 
56  $this->loadAllConfig();
58 
59  if ( $storage === null ) $storage = $app->_conf['config_storage'];
60 
61  switch (strtolower($storage)){
62  case 'db':
63  case 'database':
64  case 'sql':
65  return $this->writeConfigToDB();
66  case 'ini':
67  return $this->writeConfigToINI();
68  }
69 
70  }
71 
72  function writeConfigToDB(){
73  import('Dataface/Table.php');
74  import('Dataface/Record.php');
75  import('Dataface/IO.php');
76  if ( !is_a($this, 'Dataface_ConfigTool') ){
77  throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR);
78  }
79  $this->loadAllConfig();
81  // first let's make copies of the current configuration.
82  $timestamp = time();
83  foreach ( $this->configTypes as $type ){
84  $res = mysql_query("CREATE TABLE `__".addslashes($type)."__".$timestamp."` SELECT * FROM `__".addslashes($type)."__`", $app->db());
85  if ( !$res ){
86  throw new Exception("Failed to make backup of table '__".$type."__'.". mysql_error($app->db()), E_USER_ERROR);
87  }
88  }
89 
90  $res = mysql_query("CREATE TABLE `__properties__".$timestamp."` SELECT * FROM `__properties__`", $app->db());
91  if ( !$res ){
92  throw new Exception("Failed to make backup of table '__properties__'.", $app->db());
93  }
94 
95  // Now that we have made our backups, we can continue to write the configuration to the database.
96  //print_r($this->config);
97  foreach ( $this->configTypes as $type ){
98 
99  $res = mysql_query("DELETE FROM `__".addslashes($type)."__`", $app->db());
100  if ( !$res ){
101  throw new Exception("Failed to delete all records from table '__".$type."__'", $app->db());
102  }
103 
104 
105  foreach ( $this->config[$type] as $tablename=>$tableConfig ){
106  foreach ( $tableConfig as $sectionname=>$section){
107  $tableObj =& Dataface_Table::loadTable('__'.$type.'__');
108  $record = new Dataface_Record('__'.$type.'__', array());
109  $record->useMetaData = false; // some of the field names begin with '__' which would conflict with dataface's handling of MetaData fields.
110 
111 
112  foreach ( array_keys($tableObj->fields()) as $fieldname ){
113  $record->setValue($fieldname, @$section[$fieldname]);
114  unset($section[$fieldname]);
115  }
116  $record->setValue('name',$sectionname);
117  $record->setValue('table', $tablename);
118  //echo nl2br("Section name: $sectionname\nTable: $tablename\n");
119  //print_r($record->strvals());
120 
121  echo nl2br("\nWriting section: $sectionname : ");
122  print_r($record->strvals());
123 
124  // now that we have created the record, we write the record
125  $io = new Dataface_IO('__'.$type.'__');
126  $res = $io->write($record);
127  if ( PEAR::isError($res) ){
128  throw new Exception($res->toString(), E_USER_ERROR);
129  } else if (!$res ){
130  throw new Exception("Failure to write to database for unknown reason.", E_USER_ERROR);
131  }
132 
133  // now for the rest of the properties.
134  foreach ( $section as $propertyName=>$propertyValue ){
135  $res = mysql_query("
136  INSERT INTO
137  `__properties__`
138  (`parent_id`,`parent_type`,`property_name`,`property_value`)
139  VALUES
140  ('".$record->val($type.'_id')."',
141  '".addslashes($type)."',
142  '".addslashes($propertyName)."',
143  '".addslashes($propertyValue)."')", $app->db());
144  if ( !$res ){
145  throw new Exception("Failed to add property '$propertyName' to table '__properties__' with value '$propertyValue'".mysql_error($app->db()), E_USER_ERROR);
146  }
147  }
148 
149  unset($tableObj);
150  unset($record);
151  unset($io);
152  }
153  }
154 
155  }
156 
157  }
158 
159 }