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 $err = db_drop_db($db_connections[$id]);
63 unset($db_connections[$id]);
64 $conn = array_values($db_connections);
65 $db_connections = $conn;
66 //$$db_connections = array_values($db_connections);
69 //---------------------------------------------------------------------------------------------
71 function handle_submit()
73 global $db_connections, $def_coy, $tb_pref_counter, $db,
74 $comp_path, $comp_subdirs;
83 $db_connections[$id]['name'] = $_POST['name'];
84 $db_connections[$id]['host'] = $_POST['host'];
85 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
86 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
87 $db_connections[$id]['dbname'] = $_POST['dbname'];
88 if (isset($_GET['ul']) && $_GET['ul'] == 1)
90 if (is_numeric($_POST['tbpref']))
92 $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
93 $tb_pref_counter."_" : '';
96 else if ($_POST['tbpref'] != "")
97 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
99 $db_connections[$id]['tbpref'] = "";
101 if ((bool)$_POST['def'] == true)
103 if (isset($_GET['ul']) && $_GET['ul'] == 1)
105 $conn = $db_connections[$id];
106 if (($db = db_create_db($conn)) == 0)
108 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
109 remove_connection($id);
110 set_global_connection();
114 $filename = $_FILES['uploadfile']['tmp_name'];
115 if (is_uploaded_file ($filename))
117 db_import($filename, $conn, $id);
118 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
119 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
123 display_error(_("Error uploading Database Script, please upload it manually"));
124 set_global_connection();
127 set_global_connection();
129 $error = write_config_db($new);
131 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
132 else if ($error == -2)
133 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
134 else if ($error == -3)
135 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."));
140 $index = "<?php\nheader(\"Location: ../../index.php\");\n?>";
144 $cdir = $comp_path.'/'.$id;
146 save_to_file($cdir.'/'.'index.php', 0, $index);
148 foreach($comp_subdirs as $dir)
150 @mkdir($cdir.'/'.$dir);
151 save_to_file($cdir.'/'.$dir.'/'.'index.php', 0, $index);
157 //---------------------------------------------------------------------------------------------
159 function handle_delete()
161 global $comp_path, $def_coy, $db_connections, $comp_subdirs;
165 $cdir = $comp_path.'/'.$id;
169 display_error(_("Cannot remove company data directory ") . $cdir);
172 for($i = $id+1; $i < count($db_connections); $i++) {
173 if (!rename($comp_path.'/'.$i, $comp_path.'/'.($i-1))) {
174 display_error(_("Cannot rename company subdirectory"));
178 $err = remove_connection($id);
180 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
184 $error = write_config_db();
186 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
187 else if ($error == -2)
188 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
189 else if ($error == -3)
190 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."));
194 meta_forward($_SERVER['PHP_SELF']);
197 //---------------------------------------------------------------------------------------------
199 function display_companies()
201 global $table_style, $def_coy, $db_connections;
203 $coyno = $_SESSION["wa_current_user"]->company;
206 <script language='javascript'>
207 function deleteCompany(id) {
208 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
210 document.location.replace('create_coy.php?c=df&id='+id)
213 start_table($table_style);
215 $th = array(_("Company"), _("Database Host"), _("Database User"),
216 _("Database Name"), _("Table Pref"), _("Default"), "", "");
220 $conn = $db_connections;
222 for ($i = 0; $i < $n; $i++)
229 start_row("class='stockmankobg'");
231 alt_table_row_color($k);
233 label_cell($conn[$i]['name']);
234 label_cell($conn[$i]['host']);
235 label_cell($conn[$i]['dbuser']);
236 label_cell($conn[$i]['dbname']);
237 label_cell($conn[$i]['tbpref']);
239 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
240 label_cell( $i == $coyno ? '' :
241 "<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
246 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
249 //---------------------------------------------------------------------------------------------
251 function display_company_edit($selected_id)
253 global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
255 if ($selected_id != -1)
258 $n = count($db_connections);
260 start_form(true, true);
263 <script language='javascript'>
264 function updateCompany() {
265 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
266 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
269 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
271 document.forms[0].submit()
275 start_table($table_style2);
277 if ($selected_id != -1)
279 $conn = $db_connections[$selected_id];
280 $_POST['name'] = $conn['name'];
281 $_POST['host'] = $conn['host'];
282 $_POST['dbuser'] = $conn['dbuser'];
283 $_POST['dbpassword'] = $conn['dbpassword'];
284 $_POST['dbname'] = $conn['dbname'];
285 $_POST['tbpref'] = $conn['tbpref'];
286 if ($selected_id == $def_coy)
287 $_POST['def'] = true;
289 $_POST['def'] = false;
290 $_POST['dbcreate'] = false;
291 hidden('selected_id', $selected_id);
292 hidden('tbpref', $_POST['tbpref']);
293 hidden('dbpassword', $_POST['dbpassword']);
296 $_POST['tbpref'] = $tb_pref_counter."_";
297 text_row_ex(_("Company"), 'name', 30);
298 text_row_ex(_("Host"), 'host', 30);
299 text_row_ex(_("Database User"), 'dbuser', 30);
300 if ($selected_id == -1)
301 text_row_ex(_("Database Password"), 'dbpassword', 30);
302 text_row_ex(_("Database Name"), 'dbname', 30);
303 if ($selected_id == -1)
304 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
306 label_row(_("Table Pref"), $_POST['tbpref']);
307 yesno_list_row(_("Default"), 'def', null, "", "", false);
310 label_cell(_("Database Script"));
311 label_cell("<input name='uploadfile' type='file'>");
314 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
317 display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
318 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
325 //---------------------------------------------------------------------------------------------
327 if (isset($_GET['c']) && $_GET['c'] == 'df')
333 if (isset($_GET['c']) && $_GET['c'] == 'u')
337 meta_forward($_SERVER['PHP_SELF']);
342 //---------------------------------------------------------------------------------------------
346 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
348 display_company_edit($selected_id);
350 //---------------------------------------------------------------------------------------------