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 //---------------------------------------------------------------------------------------------
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 //---------------------------------------------------------------------------------------------
33 global $db_connections, $tb_pref_counter;
35 foreach($db_connections as $id=>$con) {
36 if ($_POST['host'] == $con['host'] && $_POST['dbname'] == $con['dbname']) {
37 if ($_POST['tbpref'] == $con['tbpref']) {
38 display_error(_("This database settings are already used by another company."));
41 if ($_POST['tbpref'] == 0 || $con['tbpref'] == '') {
42 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
50 //---------------------------------------------------------------------------------------------
52 function remove_connection($id) {
53 global $db_connections;
55 $dbase = $db_connections[$id]['dbname'];
56 $err = db_drop_db($db_connections[$id]);
58 unset($db_connections[$id]);
59 $conn = array_values($db_connections);
60 $db_connections = $conn;
61 //$$db_connections = array_values($db_connections);
64 //---------------------------------------------------------------------------------------------
66 function handle_submit()
68 global $db_connections, $def_coy, $tb_pref_counter, $db,
69 $comp_path, $comp_subdirs;
78 $db_connections[$id]['name'] = $_POST['name'];
79 $db_connections[$id]['host'] = $_POST['host'];
80 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
81 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
82 $db_connections[$id]['dbname'] = $_POST['dbname'];
83 if (isset($_GET['ul']) && $_GET['ul'] == 1)
85 if (is_numeric($_POST['tbpref']))
87 $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
88 $tb_pref_counter."_" : '';
91 else if ($_POST['tbpref'] != "")
92 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
94 $db_connections[$id]['tbpref'] = "";
96 if ((bool)$_POST['def'] == true)
98 if (isset($_GET['ul']) && $_GET['ul'] == 1)
100 $conn = $db_connections[$id];
101 if (($db = db_create_db($conn)) == 0)
103 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
104 remove_connection($id);
105 set_global_connection();
109 $filename = $_FILES['uploadfile']['tmp_name'];
110 if (is_uploaded_file ($filename))
112 db_import($filename, $conn, $id);
113 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
114 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
118 display_error(_("Error uploading Database Script, please upload it manually"));
119 set_global_connection();
122 set_global_connection();
124 $error = write_config_db($new);
126 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
127 else if ($error == -2)
128 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
129 else if ($error == -3)
130 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."));
135 $index = "<?php\nheader(\"Location: ../../index.php\");\n?>";
139 $cdir = $comp_path.'/'.$id;
141 save_to_file($cdir.'/'.'index.php', 0, $index);
143 foreach($comp_subdirs as $dir)
145 @mkdir($cdir.'/'.$dir);
146 save_to_file($cdir.'/'.$dir.'/'.'index.php', 0, $index);
152 //---------------------------------------------------------------------------------------------
154 function handle_delete()
156 global $comp_path, $def_coy, $db_connections, $comp_subdirs;
160 $err = remove_connection($id);
162 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manuallly"));
166 $error = write_config_db();
168 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
169 else if ($error == -2)
170 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
171 else if ($error == -3)
172 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."));
176 $cdir = $comp_path.'/'.$id;
180 display_error(_("Cannot remove company data directory ") . $cdir);
184 meta_forward($_SERVER['PHP_SELF']);
187 //---------------------------------------------------------------------------------------------
189 function display_companies()
191 global $table_style, $def_coy, $db_connections;
193 $coyno = $_SESSION["wa_current_user"]->company;
196 <script language='javascript'>
197 function deleteCompany(id) {
198 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
200 document.location.replace('create_coy.php?c=df&id='+id)
203 start_table($table_style);
205 $th = array(_("Company"), _("Database Host"), _("Database User"),
206 _("Database Name"), _("Table Pref"), _("Default"), "", "");
210 $conn = $db_connections;
212 for ($i = 0; $i < $n; $i++)
219 start_row("class='stockmankobg'");
221 alt_table_row_color($k);
223 label_cell($conn[$i]['name']);
224 label_cell($conn[$i]['host']);
225 label_cell($conn[$i]['dbuser']);
226 label_cell($conn[$i]['dbname']);
227 label_cell($conn[$i]['tbpref']);
229 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
231 label_cell("<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
236 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
239 //---------------------------------------------------------------------------------------------
241 function display_company_edit($selected_id)
243 global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
245 if ($selected_id != -1)
248 $n = count($db_connections);
250 start_form(true, true);
253 <script language='javascript'>
254 function updateCompany() {
255 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
256 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
259 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
261 document.forms[0].submit()
265 start_table($table_style2);
267 if ($selected_id != -1)
269 $conn = $db_connections[$selected_id];
270 $_POST['name'] = $conn['name'];
271 $_POST['host'] = $conn['host'];
272 $_POST['dbuser'] = $conn['dbuser'];
273 $_POST['dbpassword'] = $conn['dbpassword'];
274 $_POST['dbname'] = $conn['dbname'];
275 $_POST['tbpref'] = $conn['tbpref'];
276 if ($selected_id == $def_coy)
277 $_POST['def'] = true;
279 $_POST['def'] = false;
280 $_POST['dbcreate'] = false;
281 hidden('selected_id', $selected_id);
282 hidden('tbpref', $_POST['tbpref']);
283 hidden('dbpassword', $_POST['dbpassword']);
286 $_POST['tbpref'] = $tb_pref_counter."_";
287 text_row_ex(_("Company"), 'name', 30);
288 text_row_ex(_("Host"), 'host', 30);
289 text_row_ex(_("Database User"), 'dbuser', 30);
290 if ($selected_id == -1)
291 text_row_ex(_("Database Password"), 'dbpassword', 30);
292 text_row_ex(_("Database Name"), 'dbname', 30);
293 if ($selected_id == -1)
294 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
296 label_row(_("Table Pref"), $_POST['tbpref']);
297 yesno_list_row(_("Default"), 'def', null, "", "", false);
300 label_cell(_("Database Script"));
301 label_cell("<input name='uploadfile' type='file'>");
304 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
307 display_note(_("Choose from Database scripts in SQL folder. No Datase is created without a script."), 0, 1);
308 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150' value='". _("Save"). "'>";
315 //---------------------------------------------------------------------------------------------
317 if (isset($_GET['c']) && $_GET['c'] == 'df')
323 if (isset($_GET['c']) && $_GET['c'] == 'u')
327 meta_forward($_SERVER['PHP_SELF']);
332 //---------------------------------------------------------------------------------------------
336 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
338 display_company_edit($selected_id);
340 //---------------------------------------------------------------------------------------------