Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / admin / inst_module.php
index 5f8e236361d36e44c3b4e8195750f6938cea03a6..6c974ddb4bda02234902550237a7e75365b122e1 100644 (file)
 <?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");
+include_once($path_to_root."/includes/packages.inc");
 
-page(_("Install/Update Modules"));
+if ($SysPrefs->use_popup_windows) {
+       $js = get_js_open_window(900, 500);
+}
+page(_($help_context = "Install/Activate extensions"), false, false, "", $js);
 
 include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/admin/db/company_db.inc");
-include_once($path_to_root . "/modules/installed_modules.php");
+include_once($path_to_root . "/admin/db/maintenance_db.inc");
 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'];
-}
-else
-       $selected_id = -1;
+simple_page_mode(true);
 
 //---------------------------------------------------------------------------------------------
-
-function get_tab_title($tab)
+function local_extension($id)
 {
-       global $tabs, $names;
-       for ($i = 0; $i < count($tabs); $i++)
-       {
-               if ($tabs[$i] == $tab)
-                       return $names[$i];
+       global $next_extension_id, $Ajax, $path_to_root;
+
+       $exts = get_company_extensions();
+       $exts[$next_extension_id++] = array(
+                       'package' => $id,
+                       'name' => $id,
+                       'version' => '-',
+                       'available' => '',
+                       'type' => 'extension',
+                       'path' => 'modules/'.$id,
+                       'active' => false
+       );
+
+       $local_module_path = $path_to_root.'/modules/'.clean_file_name($id);
+       $local_config_file = $local_module_path.'/_init/config';
+       $local_hook_file = $local_module_path.'/hooks.php';
+
+       if (file_exists($local_config_file)) {
+               $ctrl = get_control_file($local_config_file);
+               if (key_exists('Name', $ctrl)) $exts[$next_extension_id-1]['name'] = $ctrl['Name'];
+               if (key_exists('Version', $ctrl)) $exts[$next_extension_id-1]['version'] = $ctrl['Version'];
        }
-       return "";
-}
+       if (file_exists($local_hook_file)) {
+               include_once($local_hook_file);
 
-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()
-{
+       }
+       $hooks_class = 'hooks_'.$id;
+       if (class_exists($hooks_class, false)) {
+               $hooks = new $hooks_class;
+               $hooks->install_extension(false);
+       }
+       $Ajax->activate('ext_tbl'); // refresh settings display
+       if (!update_extensions($exts))
+               return false;
        return true;
 }
 
-/**
- * @return Returns the array sorted as required
- * @param $aryData Array containing data to sort
- * @param $strIndex name of column to use as an index
- * @param $strSortBy Column to sort the array by
- * @param $strSortType String containing either asc or desc [default to asc]
- * @desc Naturally sorts an array using by the column $strSortBy
- */
-function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
-{
-   //    if the parameters are invalid
-   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);
-
-   //    if the sort type is descending
-   if ($strSortType=="desc")
-       //    reverse the array
-       arsort($arySort);
-       
-   //    loop through the sorted and original data
-   foreach ($arySort as $arySortKey => $arySorted)
-       foreach ($aryData as $aryOriginal)
-           //    if the key matches
-           if ($aryOriginal[$strIndex]==$arySortKey)
-               //    add it to the output array
-               array_push($aryResult, $aryOriginal);
-
-   //    return the return
-   return $aryResult;
-} 
-
-function write_modules()
+function handle_delete($id)
 {
-       global $path_to_root, $installed_modules;
-
-       $mods = array_natsort($installed_modules, 'tab', 'tab');
-       $installed_modules = $mods;
-       //reset($installed_languages);
-       $n = count($installed_modules); 
-       $msg = "<?php\n\n";
+       global $path_to_root;
        
-       $msg .= "/*****************************************************************\n";
-       $msg .= "External modules for FrontAccounting\n";
-       $msg .= "******************************************************************/\n";
-       $msg .= "\n\n";
-
-       $msg .= "\$installed_modules = array (\n";
-       $msg .= "\t0 => ";
-       for ($i = 0; $i < $n; $i++)
-       {
-               if ($i > 0)
-                       $msg .= "\t\tarray ";
-               else
-                       $msg .= "array ";
-               $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
-               $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
-               $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
-               $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
-               $msg .= "),\n";
-       }
-       $msg .= "\t);\n?>";
-
-       $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 (!$zp = fopen($filename, 'w')) 
-               {
-                       display_error(_("Cannot open the modules file - ") . $filename);
+       $extensions = get_company_extensions();
+       $ext = $extensions[$id];
+       if ($ext['version'] != '-') {
+               if (!uninstall_package($ext['package']))
                        return false;
-               } 
-               else 
-               {
-                       if (!fwrite($zp, $msg)) 
-                       {
-                               display_error(_("Cannot write to the modules file - ") . $filename);
-                               fclose($zp);
-                               return false;
-                       }
-                       // Close file
-                       fclose($zp);
+       } else {
+               @include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
+               $hooks_class = 'hooks_'.$ext['package'];
+               if (class_exists($hooks_class)) {
+                       $hooks = new $hooks_class;
+                       $hooks->uninstall_extension(false);
                }
-       } 
-       else 
-       {
-               display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
-               return false;
+       }
+       unset($extensions[$id]);
+       if (update_extensions($extensions)) {
+               display_notification(_("Selected extension has been successfully deleted"));
        }
        return true;
 }
-
+//
+// Helper for formating menu tabs/entries to be displayed in extension table
+//
+function fmt_titles($defs)
+{
+               if (!$defs) return '';
+               foreach($defs as $def) {
+                       $str[] = access_string($def['title'], true);
+               }
+               return implode('<br>', array_values($str));
+}
 //---------------------------------------------------------------------------------------------
-
-function handle_submit()
+//
+// Display list of all extensions - installed and available from repository
+//
+function display_extensions($mods)
 {
-       global $path_to_root, $installed_modules;
+       global $installed_extensions;
+       
+       div_start('ext_tbl');
+       start_table(TABLESTYLE);
 
-       if (!check_data())
-               return false;
+       $th = array(_("Extension"),_("Modules provided"), _("Options provided"),
+                _("Installed"), _("Available"),  "", "");
+       table_header($th);
 
-       $id = $_GET['id'];
+       $k = 0;
 
-       $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'];      
-       if (!file_exists($directory))
-       {
-               mkdir($directory);
-       }       
-       if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
+       foreach($mods as $pkg_name => $ext)
        {
-               $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
-               $file1 = $_FILES['uploadfile']['tmp_name'];
-               $file2 = $directory . "/".$_FILES['uploadfile']['name'];
-               if (file_exists($file2))
-                       unlink($file2);
-               move_uploaded_file($file1, $file2);
-       }
-       else
-               $installed_modules[$id]['filename'] = $_POST['filename'];
-       if (!write_modules())
-               return false;
-       return true;
-}
-
-//---------------------------------------------------------------------------------------------
-
-function handle_delete()
-{
-       global  $path_to_root, $installed_modules;
+               $available = @$ext['available'];
+               $installed = @$ext['version'];
+               $id = @$ext['local_id'];
+
+               $entries = fmt_titles(@$ext['entries']);
+               $tabs = fmt_titles(@$ext['tabs']);
+
+               alt_table_row_color($k);
+
+               label_cell($available ? get_package_view_str($pkg_name, $ext['name']) : $ext['name']);
+               label_cell($tabs);
+               label_cell($entries);
+
+               label_cell($id === null ? _("None") :
+                       (($installed && ($installed != '-' || $installed != '')) ? $installed : _("Unknown")));
+               label_cell($available ? $available : _("Unknown"));
+
+               if (!$available && $ext['type'] == 'extension') {// third-party plugin
+                       if (!$installed)
+                               button_cell('Local'.$ext['package'], _("Install"), _('Install third-party extension.'), 
+                                       ICON_DOWN);
+                       else
+                               label_cell('');
+               } elseif (check_pkg_upgrade($installed, $available)) // outdated or not installed extension in repo
+                       button_cell('Update'.$pkg_name, $installed ? _("Update") : _("Install"),
+                               _('Upload and install latest extension package'), ICON_DOWN);
+               else
+                       label_cell('');
 
-       $id = $_GET['id'];      
+               if ($id !== null) {
+                       delete_button_cell('Delete'.$id, _('Delete'));
+                       submit_js_confirm('Delete'.$id, 
+                               sprintf(_("You are about to remove package \'%s\'.\nDo you want to continue ?"), 
+                                       $ext['name']));
+               } else
+                       label_cell('');
 
-       $path = $installed_modules[$id]['path'];
-       $filename = "$path_to_root/modules/$path";      
-       if ($h = opendir($filename)) 
-       {
-               while (($file = readdir($h)) !== false) 
-               {
-                       if (is_file("$filename/$file"))
-                       unlink("$filename/$file");
-               }
-               closedir($h);
+               end_row();
        }
-       rmdir($filename);
 
-       unset($installed_modules[$id]);
-       $mods = array_values($installed_modules);
-       $installed_modules = $mods;
+       end_table(1);
 
-       if (!write_modules())
-               return;
-       meta_forward($_SERVER['PHP_SELF']);
-}
-
-//---------------------------------------------------------------------------------------------
+       submit_center_first('Refresh', _("Update"), '', null);
 
-function display_modules()
+       div_end();
+}
+//---------------------------------------------------------------------------------
+//
+// Get all installed extensions and display
+// with current status stored in company directory.
+//
+function company_extensions($id)
 {
-       global $table_style, $installed_modules;
-
-       echo "
-               <script language='javascript'>
-               function deleteModule(id, name) {
-                       if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
-                               return
-                       document.location.replace('inst_module.php?c=df&id='+id)
-               }
-               </script>";
-       start_table($table_style);
-       $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
+       start_table(TABLESTYLE);
+       
+       $th = array(_("Extension"),_("Modules provided"), _("Options provided"), _("Active"));
+       
+       $mods = get_company_extensions();
+       $exts = get_company_extensions($id);
+       foreach($mods as $key => $ins) {
+               foreach($exts as $ext)
+                       if ($ext['name'] == $ins['name']) {
+                               $mods[$key]['active'] = @$ext['active'];
+                               continue 2;
+                       }
+       }
+       $mods = array_natsort($mods, null, 'name');
        table_header($th);
-
        $k = 0;
-       $mods = $installed_modules;
-       $n = count($mods);
-       for ($i = 0; $i < $n; $i++)
+       foreach($mods as $i => $mod)
        {
+               if ($mod['type'] != 'extension') continue;
                alt_table_row_color($k);
-
-               label_cell(get_tab_title($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>");
+               label_cell($mod['name']);
+               $entries = fmt_titles(@$mod['entries']);
+               $tabs = fmt_titles(@$mod['tabs']);
+               label_cell($tabs);
+               label_cell($entries);
+
+               check_cells(null, 'Active'.$i, @$mod['active'] ? 1:0, 
+                       false, false, "align='center'");
                end_row();
        }
 
-       end_table();
+       end_table(1);
+       submit_center('Refresh', _('Update'), true, false, 'default');
 }
 
 //---------------------------------------------------------------------------------------------
-
-function display_module_edit($selected_id)
+if ($Mode == 'Delete')
 {
-       global $installed_modules, $table_style2;
+       handle_delete($selected_id);
+       $Mode = 'RESET';
+}
 
-       if ($selected_id != -1)
-               $n = $selected_id;
-       else
-               $n = count($installed_modules);
-       
-       start_form(true, true);
+if (get_post('Refresh')) {
+       $comp = get_post('extset');
+       $exts = get_company_extensions($comp);
 
-       echo "
-               <script language='javascript'>
-               function updateModule() {
-                       document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
-                       document.forms[0].submit()
+       $result = true;
+       foreach($exts as $i => $ext) {
+               if ($ext['package'] && ($ext['active'] ^ check_value('Active'.$i))) 
+               {
+                       if (check_value('Active'.$i) && !check_src_ext_version($ext['version']))
+                       {
+                               display_warning(sprintf(_("Package '%s' is incompatible with current application version and cannot be activated.\n")
+                                       . _("Check Install/Activate page for newer package version."), $ext['name']));
+                               continue;
+                       }
+                       $activated = activate_hooks($ext['package'], $comp, !$ext['active']);   // change active state
+
+                       if ($activated !== null)
+                               $result &= $activated;
+                       if ($activated || ($activated === null))
+                               $exts[$i]['active'] = check_value('Active'.$i);
                }
-               </script>";
+       }
+       write_extensions($exts, get_post('extset'));
+       if (get_post('extset') == user_company())
+               $installed_extensions = $exts;
        
-       start_table($table_style2);
-
-       if ($selected_id != -1) 
-       {
-               $mod = $installed_modules[$selected_id];
-               $_POST['tab']  = $mod['tab'];
-               $_POST['name'] = $mod['name'];
-               $_POST['path'] = $mod['path'];
-               $_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'>");
-
-       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"). "'>";
-
-
-       end_form();
+       if(!$result) {
+               display_error(_('Status change for some extensions failed.'));
+               $Ajax->activate('ext_tbl'); // refresh settings display
+       }else
+               display_notification(_('Current active extensions set has been saved.'));
 }
 
+if ($id = find_submit('Update', false))
+       install_extension($id);
 
-//---------------------------------------------------------------------------------------------
+if ($id = find_submit('Local', false))
+       local_extension($id);
 
-if (isset($_GET['c']))
+if ($Mode == 'RESET')
 {
-       if ($_GET['c'] == 'df') 
-       {
-               handle_delete();
-       }
-
-       if ($_GET['c'] == 'u') 
-       {
-               if (handle_submit()) 
-               {
-                       //meta_forward($_SERVER['PHP_SELF']);
-               }
-       }       
+       $selected_id = -1;
+       unset($_POST);
 }
 
 //---------------------------------------------------------------------------------------------
+start_form(true);
+if (list_updated('extset'))
+       $Ajax->activate('_page_body');
 
-display_modules();
+$set = get_post('extset', -1);
 
-hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
+echo "<center>" . _('Extensions:') . "&nbsp;&nbsp;";
+echo extset_list('extset', null, true);
+echo "</center><br>";
 
-display_module_edit($selected_id);
+if ($set == -1) 
+{
+       $mods = get_extensions_list('extension');
+       if (!$mods)
+               display_note(_("No optional extension module is currently available."));
+       else
+               display_extensions($mods);
+} else 
+       company_extensions($set);
 
 //---------------------------------------------------------------------------------------------
+end_form();
 
 end_page();
-
-?>
\ No newline at end of file