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');
16 //---------------------------------------------------------------------------------------------
18 if (isset($_GET['selected_id']))
20 $selected_id = $_GET['selected_id'];
22 elseif (isset($_POST['selected_id']))
24 $selected_id = $_POST['selected_id'];
29 //---------------------------------------------------------------------------------------------
36 //---------------------------------------------------------------------------------------------
38 function remove_connection($id) {
39 global $db_connections;
41 $dbase = $db_connections[$id]['dbname'];
42 $err = db_drop_db($db_connections[$id]);
44 unset($db_connections[$id]);
45 $conn = array_values($db_connections);
46 $db_connections = $conn;
47 //$$db_connections = array_values($db_connections);
50 //---------------------------------------------------------------------------------------------
52 function handle_submit()
54 global $db_connections, $def_coy, $tb_pref_counter, $db,
55 $comp_path, $comp_subdirs;
64 $db_connections[$id]['name'] = $_POST['name'];
65 $db_connections[$id]['host'] = $_POST['host'];
66 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
67 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
68 $db_connections[$id]['dbname'] = $_POST['dbname'];
69 if (isset($_GET['ul']) && $_GET['ul'] == 1)
71 if (is_numeric($_POST['tbpref']) && $_POST['tbpref'] == 1)
73 $db_connections[$id]['tbpref'] = $tb_pref_counter."_";
76 else if ($_POST['tbpref'] != "")
77 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
79 $db_connections[$id]['tbpref'] = "";
81 if ((bool)$_POST['def'] == true)
83 if (isset($_GET['ul']) && $_GET['ul'] == 1)
85 $conn = $db_connections[$id];
86 if (($db = db_create_db($conn)) == 0)
88 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
89 remove_connection($id);
90 set_global_connection();
94 $filename = $_FILES['uploadfile']['tmp_name'];
95 if (is_uploaded_file ($filename))
97 db_import($filename, $conn, $id);
98 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
99 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
103 display_error(_("Error uploading Database Script, please upload it manually"));
104 set_global_connection();
107 set_global_connection();
109 $error = write_config_db($new);
111 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
112 else if ($error == -2)
113 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
114 else if ($error == -3)
115 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."));
119 $index = "<?php\nheader(\"Location: ../../index.php\");\n?>";
122 $cdir = $comp_path.'/'.($tb_pref_counter-1);
124 save_to_file($cdir.'/'.'index.php', 0, $index);
126 foreach($comp_subdirs as $dir) {
127 @mkdir($cdir.'/'.$dir);
128 save_to_file($cdir.'/'.$dir.'/'.'index.php', 0, $index);
134 //---------------------------------------------------------------------------------------------
136 function handle_delete()
138 global $comp_path, $def_coy, $db_connections, $comp_subdirs;
142 $pref = $db_connections[$id]['tbpref'];
144 $pref = substr($pref, 0, strlen($pref)-1);
146 $err = remove_connection($id);
148 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manuallly"));
152 $error = write_config_db();
154 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
155 else if ($error == -2)
156 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
157 else if ($error == -3)
158 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."));
162 $cdir = $comp_path.'/'.$pref;
165 display_error(_("Cannot remove company data directory ") . $cdir);
169 meta_forward($_SERVER['PHP_SELF']);
172 //---------------------------------------------------------------------------------------------
174 function display_companies()
176 global $table_style, $def_coy, $db_connections;
178 $coyno = $_SESSION["wa_current_user"]->company;
181 <script language='javascript'>
182 function deleteCompany(id) {
183 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
185 document.location.replace('create_coy.php?c=df&id='+id)
188 start_table($table_style);
190 $th = array(_("Company"), _("Database Host"), _("Database User"),
191 _("Database Name"), _("Table Pref"), _("Default"), "", "");
195 $conn = $db_connections;
197 for ($i = 0; $i < $n; $i++)
204 start_row("class='stockmankobg'");
206 alt_table_row_color($k);
208 label_cell($conn[$i]['name']);
209 label_cell($conn[$i]['host']);
210 label_cell($conn[$i]['dbuser']);
211 label_cell($conn[$i]['dbname']);
212 label_cell($conn[$i]['tbpref']);
214 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
216 label_cell("<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
221 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
224 //---------------------------------------------------------------------------------------------
226 function display_company_edit($selected_id)
228 global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
230 if ($selected_id != -1)
233 $n = count($db_connections);
235 start_form(true, true);
238 <script language='javascript'>
239 function updateCompany() {
240 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
241 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
244 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
246 document.forms[0].submit()
250 start_table($table_style2);
252 if ($selected_id != -1)
254 $conn = $db_connections[$selected_id];
255 $_POST['name'] = $conn['name'];
256 $_POST['host'] = $conn['host'];
257 $_POST['dbuser'] = $conn['dbuser'];
258 $_POST['dbpassword'] = $conn['dbpassword'];
259 $_POST['dbname'] = $conn['dbname'];
260 $_POST['tbpref'] = $conn['tbpref'];
261 if ($selected_id == $def_coy)
262 $_POST['def'] = true;
264 $_POST['def'] = false;
265 $_POST['dbcreate'] = false;
266 hidden('selected_id', $selected_id);
267 hidden('tbpref', $_POST['tbpref']);
268 hidden('dbpassword', $_POST['dbpassword']);
271 $_POST['tbpref'] = $tb_pref_counter."_";
272 text_row_ex(_("Company"), 'name', 30);
273 text_row_ex(_("Host"), 'host', 30);
274 text_row_ex(_("Database User"), 'dbuser', 30);
275 if ($selected_id == -1)
276 text_row_ex(_("Database Password"), 'dbpassword', 30);
277 text_row_ex(_("Database Name"), 'dbname', 30);
278 if ($selected_id == -1)
279 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
281 label_row(_("Table Pref"), $_POST['tbpref']);
282 yesno_list_row(_("Default"), 'def', null, "", "", false);
285 label_cell(_("Database Script"));
286 label_cell("<input name='uploadfile' type='file'>");
289 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
292 display_note(_("Choose from Database scripts in SQL folder. No Datase is created without a script."), 0, 1);
293 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150' value='". _("Save"). "'>";
300 //---------------------------------------------------------------------------------------------
302 if (isset($_GET['c']) && $_GET['c'] == 'df')
308 if (isset($_GET['c']) && $_GET['c'] == 'u')
312 meta_forward($_SERVER['PHP_SELF']);
317 //---------------------------------------------------------------------------------------------
321 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
323 display_company_edit($selected_id);
325 //---------------------------------------------------------------------------------------------