*** empty log message ***
[fa-stable.git] / admin / create_coy.php
1 <?php
2
3 $page_security = 15;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/session.inc");
6
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");
11
12 page(_("Create/Update Company"));
13
14
15 //---------------------------------------------------------------------------------------------
16
17 if (isset($_GET['selected_id']))
18 {
19         $selected_id = $_GET['selected_id'];
20
21 elseif (isset($_POST['selected_id']))
22 {
23         $selected_id = $_POST['selected_id'];
24 }
25 else
26         $selected_id = -1;
27
28 //---------------------------------------------------------------------------------------------
29
30 function check_data()
31 {
32         return true;
33 }
34
35 //---------------------------------------------------------------------------------------------
36
37 function handle_submit()
38 {
39         global $db_connections, $def_coy, $tb_pref_counter, $db;
40
41         $new = false;
42         
43         if (!check_data())
44                 return false;
45
46         $id = $_GET['id'];
47
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)
54         {
55                 if ($_POST['tbpref'] == 1)
56                 {
57                         $db_connections[$id]['tbpref'] = $tb_pref_counter."_";
58                         $new = true;
59                 }
60                 else if ($_POST['tbpref'] != "")
61                         $db_connections[$id]['tbpref'] = $_POST['tbpref'];
62                 else
63                         $db_connections[$id]['tbpref'] = "";
64         }               
65         if ((bool)$_POST['def'] == true)
66                 $def_coy = $id;
67         $error = write_config_db($new);
68         if ($error == -1)
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."));
74         if ($error != 0)
75                 return false;
76         if (isset($_GET['ul']) && $_GET['ul'] == 1)
77         {
78                 $conn = $db_connections[$id];
79                 if (($db = db_create_db($conn)) == 0)
80                 {
81                         display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
82                         set_global_connection();
83                         return false;
84                 }
85         
86                 $filename = $_FILES['uploadfile']['tmp_name'];
87                 if (is_uploaded_file ($filename)) 
88                 {
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'");
92                 } 
93                 else 
94                 {
95                         display_error(_("Error uploading Database Script, please upload it manually"));
96                         set_global_connection();
97                         return false;
98                 }
99                 set_global_connection();
100         }
101         return true;
102 }
103
104 //---------------------------------------------------------------------------------------------
105
106 function handle_delete()
107 {
108         global $def_coy, $db_connections;
109
110         $id = $_GET['id'];      
111
112         $dbase = $db_connections[$id]['dbname'];
113         $err = db_drop_db($db_connections[$id]);
114
115         unset($db_connections[$id]);
116         $conn = array_values($db_connections);
117         $db_connections = $conn;
118
119         //$$db_connections = array_values($db_connections);
120         if ($def_coy == $id)
121                 $def_coy = 0;
122         $error = write_config_db();
123         if ($error == -1)
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."));
129         if ($error != 0)
130                 return;
131         if ($err == 0)
132                 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manuallly"));
133         else            
134                 meta_forward($_SERVER['PHP_SELF']);
135 }
136
137 //---------------------------------------------------------------------------------------------
138
139 function display_companies()
140 {
141         global $table_style, $def_coy, $db_connections;
142
143         $coyno = $_SESSION["wa_current_user"]->company;
144         
145         echo "
146                 <script language='javascript'>
147                 function deleteCompany(id) {
148                         if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
149                                 return
150                         document.location.replace('create_coy.php?c=df&id='+id)
151                 }
152                 </script>";
153         start_table($table_style);
154         
155         $th = array(_("Company"), _("Database Host"), _("Database User"), 
156                 _("Database Name"), _("Table Pref"), _("Default"), "", "");
157         table_header($th);      
158
159         $k=0;
160         $conn = $db_connections;
161         $n = count($conn);
162         for ($i = 0; $i < $n; $i++)
163         {
164                 if ($i == $def_coy)
165                         $what = _("Yes");
166                 else
167                         $what = _("No");
168                 if ($i == $coyno)
169                 start_row("class='stockmankobg'");
170         else
171                 alt_table_row_color($k);
172
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']);
178                 label_cell($what);
179                 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
180                 if ($i != $coyno)
181                         label_cell("<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
182                 end_row();
183         }
184
185         end_table();
186     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
187 }
188
189 //---------------------------------------------------------------------------------------------
190
191 function display_company_edit($selected_id)
192 {
193         global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
194
195         if ($selected_id != -1)
196                 $n = $selected_id;
197         else
198                 $n = count($db_connections);
199         
200         start_form(true, true);
201
202         echo "
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
207                         }
208                         else {
209                                 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
210                         }       
211                         document.forms[0].submit()
212                 }
213                 </script>";
214         
215         start_table($table_style2);
216
217         if ($selected_id != -1) 
218         {
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; 
228                 else    
229                         $_POST['def'] = false; 
230                 $_POST['dbcreate']  = false;
231                 hidden('selected_id', $selected_id);
232                 hidden('tbpref', $_POST['tbpref']);
233                 hidden('dbpassword', $_POST['dbpassword']);
234         }
235         else
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);
245         else
246                 label_row(_("Table Pref"), $_POST['tbpref']);
247         yesno_list_row(_("Default"), 'def', null, "", "", false);
248         
249         start_row();
250         label_cell(_("Database Script"));
251         label_cell("<input name='uploadfile' type='file'>");
252         end_row();
253
254         text_row_ex(_("New script Admin Password"), 'admpassword', 20);
255
256         end_table();
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"). "'>";
259
260
261         end_form();
262 }
263
264
265 //---------------------------------------------------------------------------------------------
266
267 if (isset($_GET['c']) && $_GET['c'] == 'df') 
268 {
269
270         handle_delete();
271 }
272
273 if (isset($_GET['c']) && $_GET['c'] == 'u') 
274 {
275         if (handle_submit()) 
276         {
277                 meta_forward($_SERVER['PHP_SELF']);
278         }
279 }       
280
281
282 //---------------------------------------------------------------------------------------------
283
284 display_companies();
285
286 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
287
288 display_company_edit($selected_id);
289
290 //---------------------------------------------------------------------------------------------
291
292 end_page();
293
294 ?>