5 include_once($path_to_root . "/includes/session.inc");
7 include_once($path_to_root . "/includes/date_functions.inc");
8 include_once($path_to_root . "/admin/db/company_db.inc");
9 include_once($path_to_root . "/admin/db/maintenance_db.inc");
10 include_once($path_to_root . "/includes/ui.inc");
12 page(_("Create/Update Company"));
14 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting');
16 //---------------------------------------------------------------------------------------------
17 if (isset($_GET['selected_id']))
19 $selected_id = $_GET['selected_id'];
21 elseif (isset($_POST['selected_id']))
23 $selected_id = $_POST['selected_id'];
28 //---------------------------------------------------------------------------------------------
32 global $db_connections, $tb_pref_counter, $selected_id;
34 if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
36 foreach($db_connections as $id=>$con)
38 if($id != $selected_id && $_POST['host'] == $con['host']
39 && $_POST['dbname'] == $con['dbname'])
41 if ($_POST['tbpref'] == $con['tbpref'])
43 display_error(_("This database settings are already used by another company."));
46 if ($_POST['tbpref'] == 0 || $con['tbpref'] == '')
48 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
56 //---------------------------------------------------------------------------------------------
58 function remove_connection($id) {
59 global $db_connections;
61 $dbase = $db_connections[$id]['dbname'];
62 $err = db_drop_db($db_connections[$id]);
64 unset($db_connections[$id]);
65 $conn = array_values($db_connections);
66 $db_connections = $conn;
67 //$$db_connections = array_values($db_connections);
70 //---------------------------------------------------------------------------------------------
72 function handle_submit()
74 global $db_connections, $def_coy, $tb_pref_counter, $db,
75 $comp_path, $comp_subdirs;
84 $db_connections[$id]['name'] = $_POST['name'];
85 $db_connections[$id]['host'] = $_POST['host'];
86 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
87 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
88 $db_connections[$id]['dbname'] = $_POST['dbname'];
89 if (isset($_GET['ul']) && $_GET['ul'] == 1)
91 if (is_numeric($_POST['tbpref']))
93 $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
94 $tb_pref_counter."_" : '';
97 else if ($_POST['tbpref'] != "")
98 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
100 $db_connections[$id]['tbpref'] = "";
102 if ((bool)$_POST['def'] == true)
104 if (isset($_GET['ul']) && $_GET['ul'] == 1)
106 $conn = $db_connections[$id];
107 if (($db = db_create_db($conn)) == 0)
109 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
110 remove_connection($id);
111 set_global_connection();
115 $filename = $_FILES['uploadfile']['tmp_name'];
116 if (is_uploaded_file ($filename))
118 db_import($filename, $conn, $id);
119 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
120 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
124 display_error(_("Error uploading Database Script, please upload it manually"));
125 set_global_connection();
128 set_global_connection();
130 $error = write_config_db($new);
132 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
133 else if ($error == -2)
134 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
135 else if ($error == -3)
136 display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
141 $index = "<?php\nheader(\"Location: ../../index.php\");\n?>";
145 $cdir = $comp_path.'/'.$id;
147 save_to_file($cdir.'/'.'index.php', 0, $index);
149 foreach($comp_subdirs as $dir)
151 @mkdir($cdir.'/'.$dir);
152 save_to_file($cdir.'/'.$dir.'/'.'index.php', 0, $index);
158 //---------------------------------------------------------------------------------------------
160 function handle_delete()
162 global $comp_path, $def_coy, $db_connections, $comp_subdirs;
166 $err = remove_connection($id);
168 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manuallly"));
172 $error = write_config_db();
174 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
175 else if ($error == -2)
176 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
177 else if ($error == -3)
178 display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
182 $cdir = $comp_path.'/'.$id;
186 display_error(_("Cannot remove company data directory ") . $cdir);
190 meta_forward($_SERVER['PHP_SELF']);
193 //---------------------------------------------------------------------------------------------
195 function display_companies()
197 global $table_style, $def_coy, $db_connections;
199 $coyno = $_SESSION["wa_current_user"]->company;
202 <script language='javascript'>
203 function deleteCompany(id) {
204 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
206 document.location.replace('create_coy.php?c=df&id='+id)
209 start_table($table_style);
211 $th = array(_("Company"), _("Database Host"), _("Database User"),
212 _("Database Name"), _("Table Pref"), _("Default"), "", "");
216 $conn = $db_connections;
218 for ($i = 0; $i < $n; $i++)
225 start_row("class='stockmankobg'");
227 alt_table_row_color($k);
229 label_cell($conn[$i]['name']);
230 label_cell($conn[$i]['host']);
231 label_cell($conn[$i]['dbuser']);
232 label_cell($conn[$i]['dbname']);
233 label_cell($conn[$i]['tbpref']);
235 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
236 label_cell( $i == $coyno ? '' :
237 "<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
242 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
245 //---------------------------------------------------------------------------------------------
247 function display_company_edit($selected_id)
249 global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
251 if ($selected_id != -1)
254 $n = count($db_connections);
256 start_form(true, true);
259 <script language='javascript'>
260 function updateCompany() {
261 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
262 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
265 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
267 document.forms[0].submit()
271 start_table($table_style2);
273 if ($selected_id != -1)
275 $conn = $db_connections[$selected_id];
276 $_POST['name'] = $conn['name'];
277 $_POST['host'] = $conn['host'];
278 $_POST['dbuser'] = $conn['dbuser'];
279 $_POST['dbpassword'] = $conn['dbpassword'];
280 $_POST['dbname'] = $conn['dbname'];
281 $_POST['tbpref'] = $conn['tbpref'];
282 if ($selected_id == $def_coy)
283 $_POST['def'] = true;
285 $_POST['def'] = false;
286 $_POST['dbcreate'] = false;
287 hidden('selected_id', $selected_id);
288 hidden('tbpref', $_POST['tbpref']);
289 hidden('dbpassword', $_POST['dbpassword']);
292 $_POST['tbpref'] = $tb_pref_counter."_";
293 text_row_ex(_("Company"), 'name', 30);
294 text_row_ex(_("Host"), 'host', 30);
295 text_row_ex(_("Database User"), 'dbuser', 30);
296 if ($selected_id == -1)
297 text_row_ex(_("Database Password"), 'dbpassword', 30);
298 text_row_ex(_("Database Name"), 'dbname', 30);
299 if ($selected_id == -1)
300 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
302 label_row(_("Table Pref"), $_POST['tbpref']);
303 yesno_list_row(_("Default"), 'def', null, "", "", false);
306 label_cell(_("Database Script"));
307 label_cell("<input name='uploadfile' type='file'>");
310 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
313 display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
314 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
321 //---------------------------------------------------------------------------------------------
323 if (isset($_GET['c']) && $_GET['c'] == 'df')
329 if (isset($_GET['c']) && $_GET['c'] == 'u')
333 meta_forward($_SERVER['PHP_SELF']);
338 //---------------------------------------------------------------------------------------------
342 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
344 display_company_edit($selected_id);
346 //---------------------------------------------------------------------------------------------