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"));
15 //---------------------------------------------------------------------------------------------
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 //---------------------------------------------------------------------------------------------
35 //---------------------------------------------------------------------------------------------
37 function handle_submit()
39 global $db_connections, $def_coy, $tb_pref_counter, $db;
48 $db_connections[$id]['name'] = $_POST['name'];
49 $db_connections[$id]['host'] = $_POST['host'];
50 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
51 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
52 $db_connections[$id]['dbname'] = $_POST['dbname'];
53 if (isset($_GET['ul']) && $_GET['ul'] == 1)
55 if ($_POST['tbpref'] == 1)
57 $db_connections[$id]['tbpref'] = $tb_pref_counter."_";
60 else if ($_POST['tbpref'] != "")
61 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
63 $db_connections[$id]['tbpref'] = "";
65 if ((bool)$_POST['def'] == true)
67 $error = write_config_db($new);
69 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
70 else if ($error == -2)
71 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
72 else if ($error == -3)
73 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."));
76 if (isset($_GET['ul']) && $_GET['ul'] == 1)
78 $conn = $db_connections[$id];
79 if (($db = db_create_db($conn)) == 0)
81 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
82 set_global_connection();
86 $filename = $_FILES['uploadfile']['tmp_name'];
87 if (is_uploaded_file ($filename))
89 db_import($filename, $conn, $id);
90 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
91 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
95 display_error(_("Error uploading Database Script, please upload it manually"));
96 set_global_connection();
99 set_global_connection();
104 //---------------------------------------------------------------------------------------------
106 function handle_delete()
108 global $def_coy, $db_connections;
112 $dbase = $db_connections[$id]['dbname'];
113 $err = db_drop_db($db_connections[$id]);
115 unset($db_connections[$id]);
116 $conn = array_values($db_connections);
117 $db_connections = $conn;
119 //$$db_connections = array_values($db_connections);
122 $error = write_config_db();
124 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
125 else if ($error == -2)
126 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
127 else if ($error == -3)
128 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."));
132 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manuallly"));
134 meta_forward($_SERVER['PHP_SELF']);
137 //---------------------------------------------------------------------------------------------
139 function display_companies()
141 global $table_style, $def_coy, $db_connections;
143 $coyno = $_SESSION["wa_current_user"]->company;
146 <script language='javascript'>
147 function deleteCompany(id) {
148 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
150 document.location.replace('create_coy.php?c=df&id='+id)
153 start_table($table_style);
155 $th = array(_("Company"), _("Database Host"), _("Database User"),
156 _("Database Name"), _("Table Pref"), _("Default"), "", "");
160 $conn = $db_connections;
162 for ($i = 0; $i < $n; $i++)
169 start_row("class='stockmankobg'");
171 alt_table_row_color($k);
173 label_cell($conn[$i]['name']);
174 label_cell($conn[$i]['host']);
175 label_cell($conn[$i]['dbuser']);
176 label_cell($conn[$i]['dbname']);
177 label_cell($conn[$i]['tbpref']);
179 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
181 label_cell("<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
186 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
189 //---------------------------------------------------------------------------------------------
191 function display_company_edit($selected_id)
193 global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
195 if ($selected_id != -1)
198 $n = count($db_connections);
200 start_form(true, true);
203 <script language='javascript'>
204 function updateCompany() {
205 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
206 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
209 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
211 document.forms[0].submit()
215 start_table($table_style2);
217 if ($selected_id != -1)
219 $conn = $db_connections[$selected_id];
220 $_POST['name'] = $conn['name'];
221 $_POST['host'] = $conn['host'];
222 $_POST['dbuser'] = $conn['dbuser'];
223 $_POST['dbpassword'] = $conn['dbpassword'];
224 $_POST['dbname'] = $conn['dbname'];
225 $_POST['tbpref'] = $conn['tbpref'];
226 if ($selected_id == $def_coy)
227 $_POST['def'] = true;
229 $_POST['def'] = false;
230 $_POST['dbcreate'] = false;
231 hidden('selected_id', $selected_id);
232 hidden('tbpref', $_POST['tbpref']);
233 hidden('dbpassword', $_POST['dbpassword']);
236 $_POST['tbpref'] = $tb_pref_counter."_";
237 text_row_ex(_("Company"), 'name', 20);
238 text_row_ex(_("Host"), 'host', 20);
239 text_row_ex(_("Database User"), 'dbuser', 20);
240 if ($selected_id == -1)
241 text_row_ex(_("Database Password"), 'dbpassword', 20);
242 text_row_ex(_("Database Name"), 'dbname', 20);
243 if ($selected_id == -1)
244 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
246 label_row(_("Table Pref"), $_POST['tbpref']);
247 yesno_list_row(_("Default"), 'def', null, "", "", false);
250 label_cell(_("Database Script"));
251 label_cell("<input name='uploadfile' type='file'>");
254 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
257 display_note(_("Choose from Database scripts in SQL folder. No Datase is created without a script."), 0, 1);
258 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150' value='". _("Save"). "'>";
265 //---------------------------------------------------------------------------------------------
267 if (isset($_GET['c']) && $_GET['c'] == 'df')
273 if (isset($_GET['c']) && $_GET['c'] == 'u')
277 meta_forward($_SERVER['PHP_SELF']);
282 //---------------------------------------------------------------------------------------------
286 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
288 display_company_edit($selected_id);
290 //---------------------------------------------------------------------------------------------