cdcb971ec698b6a5b1fb48630b185cf281a9d52d
[fa-stable.git] / admin / create_coy.php
1 <?php
2 /**********************************************************************
3     Copyright (C) 2005-2008  FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 20;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/admin/db/company_db.inc");
18 include_once($path_to_root . "/admin/db/maintenance_db.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20
21 page(_("Create/Update Company"));
22
23 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting');
24
25 //---------------------------------------------------------------------------------------------
26 if (isset($_GET['selected_id']))
27 {
28         $selected_id = $_GET['selected_id'];
29 }
30 elseif (isset($_POST['selected_id']))
31 {
32         $selected_id = $_POST['selected_id'];
33 }
34 else
35         $selected_id = -1;
36
37 //---------------------------------------------------------------------------------------------
38
39 function check_data()
40 {
41         global $db_connections, $tb_pref_counter, $selected_id;
42
43         if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
44                 return false;
45         foreach($db_connections as $id=>$con)
46         {
47          if($id != $selected_id && $_POST['host'] == $con['host'] 
48                 && $_POST['dbname'] == $con['dbname'])
49                 {
50                         if ($_POST['tbpref'] == $con['tbpref'])
51                         {
52                                 display_error(_("This database settings are already used by another company."));
53                                 return false;
54                         }
55                         if ($_POST['tbpref'] == 0 || $con['tbpref'] == '')
56                         {
57                                 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
58                                 return false;
59                         }
60                 }
61         }
62         return true;
63 }
64
65 //---------------------------------------------------------------------------------------------
66
67 function remove_connection($id) {
68         global $db_connections;
69
70         $err = db_drop_db($db_connections[$id]);
71
72         unset($db_connections[$id]);
73         $conn = array_values($db_connections);
74         $db_connections = $conn;
75         //$$db_connections = array_values($db_connections);
76     return $err;
77 }
78 //---------------------------------------------------------------------------------------------
79
80 function handle_submit()
81 {
82         global $db_connections, $def_coy, $tb_pref_counter, $db,
83             $comp_path, $comp_subdirs;
84
85         $new = false;
86
87         if (!check_data())
88                 return false;
89
90         $id = $_GET['id'];
91
92         $db_connections[$id]['name'] = $_POST['name'];
93         $db_connections[$id]['host'] = $_POST['host'];
94         $db_connections[$id]['dbuser'] = $_POST['dbuser'];
95         $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
96         $db_connections[$id]['dbname'] = $_POST['dbname'];
97         if (isset($_GET['ul']) && $_GET['ul'] == 1)
98         {
99                 if (is_numeric($_POST['tbpref']))
100                 {
101                         $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
102                           $tb_pref_counter."_" : '';
103                         $new = true;
104                 }
105                 else if ($_POST['tbpref'] != "")
106                         $db_connections[$id]['tbpref'] = $_POST['tbpref'];
107                 else
108                         $db_connections[$id]['tbpref'] = "";
109         }
110         if ((bool)$_POST['def'] == true)
111                 $def_coy = $id;
112         if (isset($_GET['ul']) && $_GET['ul'] == 1)
113         {
114                 $conn = $db_connections[$id];
115                 if (($db = db_create_db($conn)) == 0)
116                 {
117                         display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
118                         remove_connection($id);
119                         set_global_connection();
120                         return false;
121                 }
122
123                 $filename = $_FILES['uploadfile']['tmp_name'];
124                 if (is_uploaded_file ($filename))
125                 {
126                         db_import($filename, $conn, $id);
127                         if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
128                                 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
129                 }
130                 else
131                 {
132                         display_error(_("Error uploading Database Script, please upload it manually"));
133                         set_global_connection();
134                         return false;
135                 }
136                 set_global_connection();
137         }
138         $error = write_config_db($new);
139         if ($error == -1)
140                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
141         else if ($error == -2)
142                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
143         else if ($error == -3)
144                 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."));
145         if ($error != 0)
146         {
147                 return false;
148         }
149         $index = "<?php\nheader(\"Location: ../../index.php\");\n?>";
150
151         if ($new)
152         {
153             $cdir = $comp_path.'/'.$id;
154             @mkdir($cdir);
155             save_to_file($cdir.'/'.'index.php', 0, $index);
156
157             foreach($comp_subdirs as $dir)
158             {
159                         @mkdir($cdir.'/'.$dir);
160                         save_to_file($cdir.'/'.$dir.'/'.'index.php', 0, $index);
161             }
162         }
163         return true;
164 }
165
166 //---------------------------------------------------------------------------------------------
167
168 function handle_delete()
169 {
170         global $comp_path, $def_coy, $db_connections, $comp_subdirs;
171
172         $id = $_GET['id'];
173
174         $cdir = $comp_path.'/'.$id;
175         @flush_dir($cdir);
176         if (!rmdir($cdir))
177         {
178                 display_error(_("Cannot remove company data directory ") . $cdir);
179                 return;
180         }
181         for($i = $id+1; $i < count($db_connections); $i++) {
182                 if (!rename($comp_path.'/'.$i, $comp_path.'/'.($i-1))) {
183                         display_error(_("Cannot rename company subdirectory"));
184                         return;
185                 }       
186         }
187         $err = remove_connection($id);
188         if ($err == 0)
189                 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
190
191         if ($def_coy == $id)
192                 $def_coy = 0;
193         $error = write_config_db();
194         if ($error == -1)
195                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
196         else if ($error == -2)
197                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
198         else if ($error == -3)
199                 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."));
200         if ($error != 0)
201                 return;
202
203         meta_forward($_SERVER['PHP_SELF']);
204 }
205
206 //---------------------------------------------------------------------------------------------
207
208 function display_companies()
209 {
210         global $table_style, $def_coy, $db_connections;
211
212         $coyno = $_SESSION["wa_current_user"]->company;
213
214         echo "
215                 <script language='javascript'>
216                 function deleteCompany(id) {
217                         if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
218                                 return
219                         document.location.replace('create_coy.php?c=df&id='+id)
220                 }
221                 </script>";
222         start_table($table_style);
223
224         $th = array(_("Company"), _("Database Host"), _("Database User"),
225                 _("Database Name"), _("Table Pref"), _("Default"), "", "");
226         table_header($th);
227
228         $k=0;
229         $conn = $db_connections;
230         $n = count($conn);
231         for ($i = 0; $i < $n; $i++)
232         {
233                 if ($i == $def_coy)
234                         $what = _("Yes");
235                 else
236                         $what = _("No");
237                 if ($i == $coyno)
238                 start_row("class='stockmankobg'");
239         else
240                 alt_table_row_color($k);
241
242                 label_cell($conn[$i]['name']);
243                 label_cell($conn[$i]['host']);
244                 label_cell($conn[$i]['dbuser']);
245                 label_cell($conn[$i]['dbname']);
246                 label_cell($conn[$i]['tbpref']);
247                 label_cell($what);
248                 $edit = _("Edit");
249                 $delete = _("Delete");
250                 if (user_graphic_links())
251                 {
252                         global $path_to_root;
253                         $edit = "<img src='$path_to_root/themes/".user_theme()."/images/".ICON_EDIT."' width='14' height='14' border='0' title='$edit' />\n";
254                         $delete = "<img src='$path_to_root/themes/".user_theme()."/images/".ICON_DELETE."' width='14' height='14' border='0' title='$delete' />\n";
255                 }
256         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
257                 label_cell( $i == $coyno ? '' :
258                         "<a href='javascript:deleteCompany(" . $i . ")'>$delete</a>");
259                 end_row();
260         }
261
262         end_table();
263     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
264 }
265
266 //---------------------------------------------------------------------------------------------
267
268 function display_company_edit($selected_id)
269 {
270         global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
271
272         if ($selected_id != -1)
273                 $n = $selected_id;
274         else
275                 $n = count($db_connections);
276
277         start_form(true, true);
278
279         echo "
280                 <script language='javascript'>
281                 function updateCompany() {
282                         if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
283                                 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
284                         }
285                         else {
286                                 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
287                         }
288                         document.forms[0].submit()
289                 }
290                 </script>";
291
292         start_table($table_style2);
293
294         if ($selected_id != -1)
295         {
296                 $conn = $db_connections[$selected_id];
297                 $_POST['name'] = $conn['name'];
298                 $_POST['host']  = $conn['host'];
299                 $_POST['dbuser']  = $conn['dbuser'];
300                 $_POST['dbpassword']  = $conn['dbpassword'];
301                 $_POST['dbname']  = $conn['dbname'];
302                 $_POST['tbpref']  = $conn['tbpref'];
303                 if ($selected_id == $def_coy)
304                         $_POST['def'] = true;
305                 else
306                         $_POST['def'] = false;
307                 $_POST['dbcreate']  = false;
308                 hidden('selected_id', $selected_id);
309                 hidden('tbpref', $_POST['tbpref']);
310                 hidden('dbpassword', $_POST['dbpassword']);
311         }
312         else
313                 $_POST['tbpref'] = $tb_pref_counter."_";
314         text_row_ex(_("Company"), 'name', 30);
315         text_row_ex(_("Host"), 'host', 30);
316         text_row_ex(_("Database User"), 'dbuser', 30);
317         if ($selected_id == -1)
318                 text_row_ex(_("Database Password"), 'dbpassword', 30);
319         text_row_ex(_("Database Name"), 'dbname', 30);
320         if ($selected_id == -1)
321                 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
322         else
323                 label_row(_("Table Pref"), $_POST['tbpref']);
324         yesno_list_row(_("Default"), 'def', null, "", "", false);
325
326         start_row();
327         label_cell(_("Database Script"));
328         label_cell("<input name='uploadfile' type='file'>");
329         end_row();
330
331         text_row_ex(_("New script Admin Password"), 'admpassword', 20);
332
333         end_table();
334         display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
335         echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
336
337
338         end_form();
339 }
340
341
342 //---------------------------------------------------------------------------------------------
343
344 if (isset($_GET['c']) && $_GET['c'] == 'df')
345 {
346
347         handle_delete();
348 }
349
350 if (isset($_GET['c']) && $_GET['c'] == 'u')
351 {
352         if (handle_submit())
353         {
354                 meta_forward($_SERVER['PHP_SELF']);
355         }
356 }
357
358
359 //---------------------------------------------------------------------------------------------
360
361 display_companies();
362
363 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
364
365 display_company_edit($selected_id);
366
367 //---------------------------------------------------------------------------------------------
368 end_page();
369
370 ?>