COA selected form preiously uploaded charts.
[fa-stable.git] / admin / create_coy.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_CREATECOMPANY';
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(_($help_context = "Create/Update Company"));
22
23 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting', 'attachments');
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
46         foreach($db_connections as $id=>$con)
47         {
48          if($id != $selected_id && $_POST['host'] == $con['host'] 
49                 && $_POST['dbname'] == $con['dbname'])
50                 {
51                         if ($_POST['tbpref'] == $con['tbpref'])
52                         {
53                                 display_error(_("This database settings are already used by another company."));
54                                 return false;
55                         }
56                         if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
57                         {
58                                 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
59                                 return false;
60                         }
61                 }
62         }
63         return true;
64 }
65
66 //---------------------------------------------------------------------------------------------
67
68 function remove_connection($id) {
69         global $db_connections;
70
71         $err = db_drop_db($db_connections[$id]);
72
73         unset($db_connections[$id]);
74         $conn = array_values($db_connections);
75         $db_connections = $conn;
76         //$$db_connections = array_values($db_connections);
77     return $err;
78 }
79 //---------------------------------------------------------------------------------------------
80
81 function handle_submit()
82 {
83         global $db_connections, $def_coy, $tb_pref_counter, $db,
84             $comp_subdirs, $path_to_root, $selected_id;
85
86         $error = false;
87         if (!check_data())
88                 return false;
89
90         if ($selected_id==-1)
91                 $selected_id = count($db_connections);
92
93         $new = !isset($db_connections[$selected_id]);
94
95         $db_connections[$selected_id]['name'] = $_POST['name'];
96         $db_connections[$selected_id]['host'] = $_POST['host'];
97         $db_connections[$selected_id]['dbuser'] = $_POST['dbuser'];
98         $db_connections[$selected_id]['dbpassword'] = $_POST['dbpassword'];
99         $db_connections[$selected_id]['dbname'] = $_POST['dbname'];
100         if (is_numeric($_POST['tbpref']))
101         {
102                 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
103                   $tb_pref_counter."_" : '';
104         }
105         else if ($_POST['tbpref'] != "")
106                 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
107         else
108                 $db_connections[$selected_id]['tbpref'] = "";
109
110         if ((bool)$_POST['def'] == true)
111                 $def_coy = $selected_id;
112
113         $conn = $db_connections[$selected_id];
114         if (($db = db_create_db($conn)) == 0)
115         {
116                 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
117                 $error = true;
118         } else {
119                 if (!db_import($path_to_root.'/sql/'.get_post('coa'), $conn, $selected_id)) {
120                         display_error(_('Cannot create new company due to bugs in sql file.'));
121                         $error = true;
122                 } else
123                         if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
124                                 update_admin_password($conn, md5($_POST['admpassword']));
125         }
126         set_global_connection();
127         if ($error) {
128                 remove_connection($selected_id);
129                 return false;
130         }
131         $error = write_config_db($new);
132         if ($error == -1)
133                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
134         else if ($error == -2)
135                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
136         else if ($error == -3)
137                 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."));
138         if ($error != 0)
139         {
140                 return false;
141         }
142
143         if ($new)
144         {
145                 create_comp_dirs(company_path($selected_id), $comp_subdirs);
146         }
147         $exts = get_company_extensions();
148         write_extensions($exts, $selected_id);
149         display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
150         return true;
151 }
152
153 //---------------------------------------------------------------------------------------------
154
155 function handle_delete()
156 {
157         global $def_coy, $db_connections, $comp_subdirs, $path_to_root;
158
159         $id = $_GET['id'];
160
161         // First make sure all company directories from the one under removal are writable. 
162         // Without this after operation we end up with changed per-company owners!
163         for($i = $id; $i < count($db_connections); $i++) {
164                         $comp_path = company_path($i);
165                 if (!is_dir($comp_path) || !is_writable($comp_path)) {
166                         display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
167                         return;
168                 }
169         }
170         // make sure config file is writable
171         if (!is_writeable($path_to_root . "/config_db.php"))
172         {
173                 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."));
174                 return;
175         }
176         // rename directory to temporary name to ensure all
177         // other subdirectories will have right owners even after
178         // unsuccessfull removal.
179         $cdir = company_path($id);
180         $tmpname  = company_path('/old_'.$id);
181         if (!@rename($cdir, $tmpname)) {
182                 display_error(_('Cannot rename subdirectory to temporary name.'));
183                 return;
184         }
185         // 'shift' company directories names
186         for ($i = $id+1; $i < count($db_connections); $i++) {
187                 if (!rename(company_path($i), company_path($i-1))) {
188                         display_error(_("Cannot rename company subdirectory"));
189                         return;
190                 }
191         }
192         $err = remove_connection($id);
193         if ($err == 0)
194                 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
195
196         if ($def_coy == $id)
197                 $def_coy = 0;
198         $error = write_config_db();
199         if ($error == -1)
200                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
201         else if ($error == -2)
202                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
203         else if ($error == -3)
204                 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."));
205         if ($error != 0) {
206                 @rename($tmpname, $cdir);
207                 return;
208         }
209         // finally remove renamed company directory
210         @flush_dir($tmpname, true);
211         if (!@rmdir($tmpname))
212         {
213                 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
214                 return;
215         }
216         display_notification(_("Selected company has been deleted"));
217 }
218
219 //---------------------------------------------------------------------------------------------
220
221 function display_companies()
222 {
223         global $def_coy, $db_connections;
224
225         $coyno = $_SESSION["wa_current_user"]->company;
226
227         echo "
228                 <script language='javascript'>
229                 function deleteCompany(id) {
230                         if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
231                                 return
232                         document.location.replace('create_coy.php?c=df&id='+id)
233                 }
234                 </script>";
235         start_table(TABLESTYLE);
236
237         $th = array(_("Company"), _("Database Host"), _("Database User"),
238                 _("Database Name"), _("Table Pref"), _("Default"), "", "");
239         table_header($th);
240
241         $k=0;
242         $conn = $db_connections;
243         $n = count($conn);
244         for ($i = 0; $i < $n; $i++)
245         {
246                 if ($i == $def_coy)
247                         $what = _("Yes");
248                 else
249                         $what = _("No");
250                 if ($i == $coyno)
251                 start_row("class='stockmankobg'");
252         else
253                 alt_table_row_color($k);
254
255                 label_cell($conn[$i]['name']);
256                 label_cell($conn[$i]['host']);
257                 label_cell($conn[$i]['dbuser']);
258                 label_cell($conn[$i]['dbname']);
259                 label_cell($conn[$i]['tbpref']);
260                 label_cell($what);
261                 $edit = _("Edit");
262                 $delete = _("Delete");
263                 if (user_graphic_links())
264                 {
265                         $edit = set_icon(ICON_EDIT, $edit);
266                         $delete = set_icon(ICON_DELETE, $delete);
267                 }
268         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
269                 label_cell( $i == $coyno ? '' :
270                         "<a href='javascript:deleteCompany(" . $i . ")'>$delete</a>");
271                 end_row();
272         }
273
274         end_table();
275     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
276 }
277
278 //---------------------------------------------------------------------------------------------
279
280 function display_company_edit($selected_id)
281 {
282         global $def_coy, $db_connections, $tb_pref_counter;
283
284         if ($selected_id != -1)
285                 $n = $selected_id;
286         else
287                 $n = count($db_connections);
288
289         start_form();
290
291         start_table(TABLESTYLE2);
292
293         if ($selected_id != -1)
294         {
295                 $conn = $db_connections[$selected_id];
296                 $_POST['name'] = $conn['name'];
297                 $_POST['host']  = $conn['host'];
298                 $_POST['dbuser']  = $conn['dbuser'];
299                 $_POST['dbpassword']  = $conn['dbpassword'];
300                 $_POST['dbname']  = $conn['dbname'];
301                 $_POST['tbpref']  = $conn['tbpref'];
302                 if ($selected_id == $def_coy)
303                         $_POST['def'] = true;
304                 else
305                         $_POST['def'] = false;
306                 $_POST['dbcreate']  = false;
307                 hidden('selected_id', $selected_id);
308                 hidden('tbpref', $_POST['tbpref']);
309                 hidden('dbpassword', $_POST['dbpassword']);
310         }
311         else
312                 $_POST['tbpref'] = $tb_pref_counter."_";
313         text_row_ex(_("Company"), 'name', 30);
314         text_row_ex(_("Host"), 'host', 30);
315         text_row_ex(_("Database User"), 'dbuser', 30);
316         if ($selected_id == -1)
317                 text_row_ex(_("Database Password"), 'dbpassword', 30);
318         text_row_ex(_("Database Name"), 'dbname', 30);
319         if ($selected_id == -1)
320                 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
321         else
322                 label_row(_("Table Pref"), $_POST['tbpref']);
323         yesno_list_row(_("Default"), 'def', null, "", "", false);
324
325         coa_list_row(_("Database Script"), 'coa');
326
327         text_row_ex(_("New script Admin Password"), 'admpassword', 20);
328         end_table(1);
329
330         submit_center('save', _("Save"));
331 //      echo "<center><input type='button' style='width:150px' value='". _("Save"). "'></center>";
332
333
334         end_form();
335 }
336
337
338 //---------------------------------------------------------------------------------------------
339
340 if (isset($_GET['c']) && $_GET['c'] == 'df') {
341         handle_delete();
342         $selected_id = -1;
343 }
344
345 if (get_post('save')) {
346         if (handle_submit())
347                 $selected_id = -1;
348 }
349
350 //---------------------------------------------------------------------------------------------
351
352 display_companies();
353
354 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
355
356 display_company_edit($selected_id);
357
358 //---------------------------------------------------------------------------------------------
359 end_page();
360
361 ?>