Changed license type to GPLv3 in top of files
[fa-stable.git] / admin / inst_module.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 = 20;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Install/Update Modules"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/company_db.inc");
20 include_once($path_to_root . "/admin/db/maintenance_db.inc");
21 include_once($path_to_root . "/modules/installed_modules.php");
22 include_once($path_to_root . "/includes/ui.inc");
23
24 $tabs = array('orders', 'AP', 'stock', 'manuf', 'proj', 'GL', 'system');
25 $names = array(_("Sales"), _("Purchases"), _("Items and Inventory"), _("Manufacturing"),
26         _("Dimensions"), _("Banking and General Ledger"), _("Setup"));
27
28 //---------------------------------------------------------------------------------------------
29
30 if (isset($_GET['selected_id']))
31 {
32         $selected_id = $_GET['selected_id'];
33 }
34 elseif (isset($_POST['selected_id']))
35 {
36         $selected_id = $_POST['selected_id'];
37 }
38 else
39         $selected_id = -1;
40
41 //---------------------------------------------------------------------------------------------
42
43 function get_tab_title($tab)
44 {
45         global $tabs, $names;
46         for ($i = 0; $i < count($tabs); $i++)
47         {
48                 if ($tabs[$i] == $tab)
49                         return $names[$i];
50         }
51         return "";
52 }
53
54 function tab_list_row($label, $name, $selected)
55 {
56         global $tabs, $names;
57         echo "<tr>\n";
58         if ($label != null)
59                 echo "<td>$label</td>\n";
60         if ($selected == null)
61                 $selected = (!isset($_POST[$name]) ? "orders" : $_POST[$name]);
62         echo "<td><select name='$name'>";
63         for ($i = 0; $i < count($tabs); $i++)
64         {
65                 if ($selected == $tabs[$i])
66                         echo "<option selected value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
67                 else
68                         echo "<option value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
69         }
70         echo "</select></td>\n";
71         echo "</tr>\n";
72 }
73
74 //---------------------------------------------------------------------------------------------
75
76 function check_data()
77 {
78         if ($_POST['name'] == "" || $_POST['path'] == "")
79                 return false;
80         return true;
81 }
82
83 /**
84  * @return Returns the array sorted as required
85  * @param $aryData Array containing data to sort
86  * @param $strIndex name of column to use as an index
87  * @param $strSortBy Column to sort the array by
88  * @param $strSortType String containing either asc or desc [default to asc]
89  * @desc Naturally sorts an array using by the column $strSortBy
90  */
91 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
92 {
93    //    if the parameters are invalid
94    if (!is_array($aryData) || !$strIndex || !$strSortBy)
95        //    return the array
96        return $aryData;
97
98    //    create our temporary arrays
99    $arySort = $aryResult = array();
100
101    //    loop through the array
102    foreach ($aryData as $aryRow)
103        //    set up the value in the array
104        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
105
106    //    apply the natural sort
107    natsort($arySort);
108
109    //    if the sort type is descending
110    if ($strSortType=="desc")
111        //    reverse the array
112        arsort($arySort);
113
114    //    loop through the sorted and original data
115    foreach ($arySort as $arySortKey => $arySorted)
116        foreach ($aryData as $aryOriginal)
117            //    if the key matches
118            if ($aryOriginal[$strIndex]==$arySortKey)
119                //    add it to the output array
120                array_push($aryResult, $aryOriginal);
121
122    //    return the return
123    return $aryResult;
124 }
125
126 function write_modules()
127 {
128         global $path_to_root, $installed_modules;
129
130         $mods = array_natsort($installed_modules, 'tab', 'tab');
131         $installed_modules = $mods;
132         //reset($installed_languages);
133         $n = count($installed_modules);
134         $msg = "<?php\n\n";
135
136         $msg .= "/*****************************************************************\n";
137         $msg .= "External modules for FrontAccounting\n";
138         $msg .= "******************************************************************/\n";
139         $msg .= "\n\n";
140
141         $msg .= "\$installed_modules = array (\n";
142         if ($n > 0)
143             $msg .= "\t0 => ";
144         for ($i = 0; $i < $n; $i++)
145         {
146                 if ($i > 0)
147                         $msg .= "\t\tarray ";
148                 else
149                         $msg .= "array ";
150                 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
151                 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
152                 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
153                 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
154                 $msg .= "),\n";
155         }
156         $msg .= "\t);\n?>";
157
158         $filename = $path_to_root . "/modules/installed_modules.php";
159         // Check if the file exists and is writable first.
160         if (file_exists($filename) && is_writable($filename))
161         {
162                 if (!$zp = fopen($filename, 'w'))
163                 {
164                         display_error(_("Cannot open the modules file - ") . $filename);
165                         return false;
166                 }
167                 else
168                 {
169                         if (!fwrite($zp, $msg))
170                         {
171                                 display_error(_("Cannot write to the modules file - ") . $filename);
172                                 fclose($zp);
173                                 return false;
174                         }
175                         // Close file
176                         fclose($zp);
177                 }
178         }
179         else
180         {
181                 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
182                 return false;
183         }
184         return true;
185 }
186
187 //---------------------------------------------------------------------------------------------
188
189 function handle_submit()
190 {
191         global $path_to_root, $installed_modules, $db_connections;
192
193         if (!check_data())
194                 return false;
195
196         $id = $_GET['id'];
197
198         $installed_modules[$id]['tab'] = $_POST['tab'];
199         $installed_modules[$id]['name'] = $_POST['name'];
200         $installed_modules[$id]['path'] = $_POST['path'];
201         $directory = $path_to_root . "/modules/" . $_POST['path'];
202         if (!file_exists($directory))
203         {
204                 mkdir($directory);
205         }
206         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
207         {
208                 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
209                 $file1 = $_FILES['uploadfile']['tmp_name'];
210                 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
211                 if (file_exists($file2))
212                         unlink($file2);
213                 move_uploaded_file($file1, $file2);
214         }
215         else
216                 $installed_modules[$id]['filename'] = $_POST['filename'];
217         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
218         {
219                 $file1 = $_FILES['uploadfile2']['tmp_name'];
220                 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
221                 if (file_exists($file2))
222                         unlink($file2);
223                 move_uploaded_file($file1, $file2);
224                 $db_name = $_SESSION["wa_current_user"]->company;
225                 db_import($file2, $db_connections[$db_name]);
226         }
227         if (!write_modules())
228                 return false;
229         return true;
230 }
231
232 //---------------------------------------------------------------------------------------------
233
234 function handle_delete()
235 {
236         global  $path_to_root, $installed_modules;
237
238         $id = $_GET['id'];
239
240         $path = $installed_modules[$id]['path'];
241         $filename = "$path_to_root/modules/$path";
242         if ($h = opendir($filename))
243         {
244                 while (($file = readdir($h)) !== false)
245                 {
246                         if (is_file("$filename/$file"))
247                         unlink("$filename/$file");
248                 }
249                 closedir($h);
250         }
251         rmdir($filename);
252
253         unset($installed_modules[$id]);
254         $mods = array_values($installed_modules);
255         $installed_modules = $mods;
256
257         if (!write_modules())
258                 return;
259         meta_forward($_SERVER['PHP_SELF']);
260 }
261
262 //---------------------------------------------------------------------------------------------
263
264 function display_modules()
265 {
266         global $table_style, $installed_modules;
267
268         echo "
269                 <script language='javascript'>
270                 function deleteModule(id, name) {
271                         if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
272                                 return
273                         document.location.replace('inst_module.php?c=df&id='+id)
274                 }
275                 </script>";
276         start_table($table_style);
277         $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
278         table_header($th);
279
280         $k = 0;
281         $mods = $installed_modules;
282         $n = count($mods);
283         for ($i = 0; $i < $n; $i++)
284         {
285                 alt_table_row_color($k);
286
287                 label_cell(get_tab_title($mods[$i]['tab']));
288                 label_cell($mods[$i]['name']);
289                 label_cell($mods[$i]['path']);
290                 label_cell($mods[$i]['filename']);
291                 $edit = _("Edit");
292                 $delete = _("Delete");
293                 if (user_graphic_links())
294                 {
295                         $edit = set_icon(ICON_EDIT, $edit);
296                         $delete = set_icon(ICON_DELETE, $delete);
297                 }
298         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
299                 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
300                 end_row();
301         }
302
303         end_table();
304 }
305
306 //---------------------------------------------------------------------------------------------
307
308 function display_module_edit($selected_id)
309 {
310         global $installed_modules, $table_style2;
311
312         if ($selected_id != -1)
313                 $n = $selected_id;
314         else
315                 $n = count($installed_modules);
316
317         start_form(true, true);
318
319         echo "
320                 <script language='javascript'>
321                 function updateModule() {
322                         document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
323                         document.forms[0].submit()
324                 }
325                 </script>";
326
327         start_table($table_style2);
328
329         if ($selected_id != -1)
330         {
331                 $mod = $installed_modules[$selected_id];
332                 $_POST['tab']  = $mod['tab'];
333                 $_POST['name'] = $mod['name'];
334                 $_POST['path'] = $mod['path'];
335                 $_POST['filename'] = $mod['filename'];
336                 hidden('selected_id', $selected_id);
337                 hidden('filename', $_POST['filename']);
338         }
339         tab_list_row(_("Menu Tab"), 'tab', null);
340         text_row_ex(_("Name"), 'name', 30);
341         text_row_ex(_("Folder"), 'path', 20);
342
343         label_row(_("Module File"), "<input name='uploadfile' type='file'>");
344         label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
345
346         end_table(0);
347         display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
348         echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
349
350
351         end_form();
352 }
353
354
355 //---------------------------------------------------------------------------------------------
356
357 if (isset($_GET['c']))
358 {
359         if ($_GET['c'] == 'df')
360         {
361                 handle_delete();
362         }
363
364         if ($_GET['c'] == 'u')
365         {
366                 if (handle_submit())
367                 {
368                         //meta_forward($_SERVER['PHP_SELF']);
369                 }
370         }
371 }
372
373 //---------------------------------------------------------------------------------------------
374
375 display_modules();
376
377 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
378
379 display_module_edit($selected_id);
380
381 //---------------------------------------------------------------------------------------------
382
383 end_page();
384
385 ?>