Code cleanup after db_pager optimization.
[fa-stable.git] / admin / inst_module.php
index 5f8e236361d36e44c3b4e8195750f6938cea03a6..00ab1adc90f5a08b4bc378b5c6e69c4125ffda8b 100644 (file)
@@ -1,6 +1,15 @@
 <?php
-
-$page_security = 15;
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+$page_security = 'SA_CREATEMODULES';
 $path_to_root="..";
 include_once($path_to_root . "/includes/session.inc");
 
@@ -8,19 +17,16 @@ page(_("Install/Update Modules"));
 
 include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/admin/db/company_db.inc");
+include_once($path_to_root . "/admin/db/maintenance_db.inc");
 include_once($path_to_root . "/modules/installed_modules.php");
 include_once($path_to_root . "/includes/ui.inc");
 
-$tabs = array('orders', 'AP', 'stock', 'manuf', 'proj', 'GL', 'system');
-$names = array(_("Sales"), _("Purchases"), _("Items and Inventory"), _("Manufacturing"), 
-       _("Dimensions"), _("Banking and General Ledger"), _("Setup"));
-       
 //---------------------------------------------------------------------------------------------
 
 if (isset($_GET['selected_id']))
 {
        $selected_id = $_GET['selected_id'];
-} 
+}
 elseif (isset($_POST['selected_id']))
 {
        $selected_id = $_POST['selected_id'];
@@ -30,41 +36,10 @@ else
 
 //---------------------------------------------------------------------------------------------
 
-function get_tab_title($tab)
-{
-       global $tabs, $names;
-       for ($i = 0; $i < count($tabs); $i++)
-       {
-               if ($tabs[$i] == $tab)
-                       return $names[$i];
-       }
-       return "";
-}
-
-function tab_list_row($label, $name, $selected)
-{
-       global $tabs, $names;
-       echo "<tr>\n";
-       if ($label != null)
-               echo "<td>$label</td>\n";
-       if ($selected == null)
-               $selected = (!isset($_POST[$name]) ? "orders" : $_POST[$name]);
-       echo "<td><select name='$name'>";
-       for ($i = 0; $i < count($tabs); $i++)
-       {
-               if ($selected == $tabs[$i])
-                       echo "<option selected value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
-               else    
-                       echo "<option value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
-       }               
-       echo "</select></td>\n";
-       echo "</tr>\n";
-}
-
-//---------------------------------------------------------------------------------------------
-
 function check_data()
 {
+       if ($_POST['name'] == "" || $_POST['path'] == "")
+               return false;
        return true;
 }
 
@@ -82,15 +57,15 @@ function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
    if (!is_array($aryData) || !$strIndex || !$strSortBy)
        //    return the array
        return $aryData;
-       
+
    //    create our temporary arrays
    $arySort = $aryResult = array();
-   
+
    //    loop through the array
    foreach ($aryData as $aryRow)
        //    set up the value in the array
        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
-       
+
    //    apply the natural sort
    natsort($arySort);
 
@@ -98,7 +73,7 @@ function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
    if ($strSortType=="desc")
        //    reverse the array
        arsort($arySort);
-       
+
    //    loop through the sorted and original data
    foreach ($arySort as $arySortKey => $arySorted)
        foreach ($aryData as $aryOriginal)
@@ -109,7 +84,7 @@ function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
 
    //    return the return
    return $aryResult;
-} 
+}
 
 function write_modules()
 {
@@ -118,16 +93,17 @@ function write_modules()
        $mods = array_natsort($installed_modules, 'tab', 'tab');
        $installed_modules = $mods;
        //reset($installed_languages);
-       $n = count($installed_modules); 
+       $n = count($installed_modules);
        $msg = "<?php\n\n";
-       
+
        $msg .= "/*****************************************************************\n";
        $msg .= "External modules for FrontAccounting\n";
        $msg .= "******************************************************************/\n";
        $msg .= "\n\n";
 
        $msg .= "\$installed_modules = array (\n";
-       $msg .= "\t0 => ";
+       if ($n > 0)
+           $msg .= "\t0 => ";
        for ($i = 0; $i < $n; $i++)
        {
                if ($i > 0)
@@ -144,16 +120,16 @@ function write_modules()
 
        $filename = $path_to_root . "/modules/installed_modules.php";
        // Check if the file exists and is writable first.
-       if (file_exists($filename) && is_writable($filename)) 
+       if (file_exists($filename) && is_writable($filename))
        {
-               if (!$zp = fopen($filename, 'w')) 
+               if (!$zp = fopen($filename, 'w'))
                {
                        display_error(_("Cannot open the modules file - ") . $filename);
                        return false;
-               } 
-               else 
+               }
+               else
                {
-                       if (!fwrite($zp, $msg)) 
+                       if (!fwrite($zp, $msg))
                        {
                                display_error(_("Cannot write to the modules file - ") . $filename);
                                fclose($zp);
@@ -162,8 +138,8 @@ function write_modules()
                        // Close file
                        fclose($zp);
                }
-       } 
-       else 
+       }
+       else
        {
                display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
                return false;
@@ -175,7 +151,7 @@ function write_modules()
 
 function handle_submit()
 {
-       global $path_to_root, $installed_modules;
+       global $path_to_root, $installed_modules, $db_connections;
 
        if (!check_data())
                return false;
@@ -185,11 +161,11 @@ function handle_submit()
        $installed_modules[$id]['tab'] = $_POST['tab'];
        $installed_modules[$id]['name'] = $_POST['name'];
        $installed_modules[$id]['path'] = $_POST['path'];
-       $directory = $path_to_root . "/modules/" . $_POST['path'];      
+       $directory = $path_to_root . "/modules/" . $_POST['path'];
        if (!file_exists($directory))
        {
                mkdir($directory);
-       }       
+       }
        if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
        {
                $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
@@ -201,6 +177,16 @@ function handle_submit()
        }
        else
                $installed_modules[$id]['filename'] = $_POST['filename'];
+       if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
+       {
+               $file1 = $_FILES['uploadfile2']['tmp_name'];
+               $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
+               if (file_exists($file2))
+                       unlink($file2);
+               move_uploaded_file($file1, $file2);
+               $db_name = $_SESSION["wa_current_user"]->company;
+               db_import($file2, $db_connections[$db_name]);
+       }
        if (!write_modules())
                return false;
        return true;
@@ -212,13 +198,13 @@ function handle_delete()
 {
        global  $path_to_root, $installed_modules;
 
-       $id = $_GET['id'];      
+       $id = $_GET['id'];
 
        $path = $installed_modules[$id]['path'];
-       $filename = "$path_to_root/modules/$path";      
-       if ($h = opendir($filename)) 
+       $filename = "$path_to_root/modules/$path";
+       if ($h = opendir($filename))
        {
-               while (($file = readdir($h)) !== false) 
+               while (($file = readdir($h)) !== false)
                {
                        if (is_file("$filename/$file"))
                        unlink("$filename/$file");
@@ -240,7 +226,7 @@ function handle_delete()
 
 function display_modules()
 {
-       global $table_style, $installed_modules;
+       global $table_style, $installed_modules, $tabs;
 
        echo "
                <script language='javascript'>
@@ -261,12 +247,19 @@ function display_modules()
        {
                alt_table_row_color($k);
 
-               label_cell(get_tab_title($mods[$i]['tab']));
+               label_cell($tabs[$mods[$i]['tab']]);
                label_cell($mods[$i]['name']);
                label_cell($mods[$i]['path']);
                label_cell($mods[$i]['filename']);
-               edit_link_cell("selected_id=" . $i);
-               label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>" . _("Delete") . "</a>");
+               $edit = _("Edit");
+               $delete = _("Delete");
+               if (user_graphic_links())
+               {
+                       $edit = set_icon(ICON_EDIT, $edit);
+                       $delete = set_icon(ICON_DELETE, $delete);
+               }
+       label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
+               label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
                end_row();
        }
 
@@ -283,8 +276,8 @@ function display_module_edit($selected_id)
                $n = $selected_id;
        else
                $n = count($installed_modules);
-       
-       start_form(true, true);
+
+       start_form(true);
 
        echo "
                <script language='javascript'>
@@ -293,10 +286,10 @@ function display_module_edit($selected_id)
                        document.forms[0].submit()
                }
                </script>";
-       
+
        start_table($table_style2);
 
-       if ($selected_id != -1) 
+       if ($selected_id != -1)
        {
                $mod = $installed_modules[$selected_id];
                $_POST['tab']  = $mod['tab'];
@@ -305,16 +298,17 @@ function display_module_edit($selected_id)
                $_POST['filename'] = $mod['filename'];
                hidden('selected_id', $selected_id);
                hidden('filename', $_POST['filename']);
-       } 
+       }
        tab_list_row(_("Menu Tab"), 'tab', null);
        text_row_ex(_("Name"), 'name', 30);
        text_row_ex(_("Folder"), 'path', 20);
 
        label_row(_("Module File"), "<input name='uploadfile' type='file'>");
+       label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
 
        end_table(0);
        display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
-       echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150' value='". _("Save"). "'>";
+       echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
 
 
        end_form();
@@ -325,18 +319,18 @@ function display_module_edit($selected_id)
 
 if (isset($_GET['c']))
 {
-       if ($_GET['c'] == 'df') 
+       if ($_GET['c'] == 'df')
        {
                handle_delete();
        }
 
-       if ($_GET['c'] == 'u') 
+       if ($_GET['c'] == 'u')
        {
-               if (handle_submit()) 
+               if (handle_submit())
                {
                        //meta_forward($_SERVER['PHP_SELF']);
                }
-       }       
+       }
 }
 
 //---------------------------------------------------------------------------------------------