From c70823611ef71079c28542f4810cc50c183a8cd5 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Mon, 28 Sep 2009 13:17:15 +0000 Subject: [PATCH] Rewritten extensions system to enable per company module/plugin activation. --- admin/create_coy.php | 2 + admin/db/maintenance_db.inc | 123 +++++++++++++ admin/inst_module.php | 321 +++++++++++++++++---------------- applications/customers.php | 10 +- applications/dimensions.php | 10 +- applications/generalledger.php | 10 +- applications/inventory.php | 10 +- applications/manufacturing.php | 10 +- applications/setup.php | 12 +- applications/suppliers.php | 10 +- frontaccounting.php | 20 +- includes/access_levels.inc | 8 +- includes/current_user.inc | 5 +- includes/main.inc | 1 - includes/session.inc | 4 +- includes/ui/ui_lists.inc | 18 ++ installed_extensions.php | 30 ++- sql/alter2.2.php | 56 ++++++ 18 files changed, 431 insertions(+), 229 deletions(-) diff --git a/admin/create_coy.php b/admin/create_coy.php index 684369c5..d1050f0e 100644 --- a/admin/create_coy.php +++ b/admin/create_coy.php @@ -151,6 +151,8 @@ function handle_submit() { create_comp_dirs("$comp_path/$id", $comp_subdirs); } + $exts = get_company_extensions(); + write_extensions($exts, $id); return true; } diff --git a/admin/db/maintenance_db.inc b/admin/db/maintenance_db.inc index e23595fc..18bcf3aa 100644 --- a/admin/db/maintenance_db.inc +++ b/admin/db/maintenance_db.inc @@ -10,6 +10,50 @@ See the License here . ***********************************************************************/ +/** + * @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_config_db($new = false) { global $path_to_root, $def_coy, $db_connections, $tb_pref_counter; @@ -76,6 +120,85 @@ function write_config_db($new = false) return 0; } +function write_extensions($extensions=null, $company = null) +{ + global $path_to_root, $installed_extensions; + + if (!isset($extensions)) { + $extensions = $installed_extensions; + } + + $exts = array_natsort($extensions, 'name', 'name'); + $extensions = $exts; + + $n = count($exts); + + $msg = " '); + + 'name' - name for identification purposes; + 'type' - type of extension: 'module' or 'plugin' + 'path' - FA root based installation path + 'filename' - name of module menu file, or plugin filename; related to path. + 'tab' - index of the module tab (new for module, or one of standard module names for plugin); + 'title' - is the menu text (for plugin) or new tab name + 'active' - current status of extension + 'acc_file' - (optional) file name with \$security_areas/\$security_sections extensions; + related to 'path'. +*/\n\n"; + else + $msg .= +"/* + Do not edit this file manually. This copy of global file is overwritten + by extensions editor. +*/\n\n"; + + $msg .= "\$installed_extensions = array (\n"; + if ($n > 0) + $msg .= "\t0 => "; + for ($i = 0; $i < $n; $i++) + { + if ($i > 0) + $msg .= "\t\t"; + $msg .= "array ( "; + $t = ''; + foreach($extensions[$i] as $key => $val) { + $msg .= $t."'$key' => '$val',\n"; + $t = "\t\t\t"; + } + $msg .= "\t\t),\n"; + } + $msg .= "\t);\n?>"; + + $filename = $path_to_root . (isset($company) ? '/company/'.$company : '') + .'/installed_extensions.php'; + + // Check if the file is writable first. + if (!$zp = @fopen($filename, 'w')) + { + display_error(sprintf(_("Cannot open the extension setup file '%s' for writing."), + $filename)); + return false; + } + else + { + if (!fwrite($zp, $msg)) + { + display_error(sprintf(_("Cannot write to the extensions setup file '%s'."), + $filename)); + fclose($zp); + return false; + } + // Close file + fclose($zp); + } + return true; +} + + function db_create_db($connection) { $db = mysql_connect($connection["host"] , diff --git a/admin/inst_module.php b/admin/inst_module.php index 92ea6324..11c30bfa 100644 --- a/admin/inst_module.php +++ b/admin/inst_module.php @@ -13,12 +13,11 @@ $page_security = 'SA_CREATEMODULES'; $path_to_root=".."; include_once($path_to_root . "/includes/session.inc"); -page(_("Install/Update Modules")); +page(_("Install/Activate extensions")); 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"); //--------------------------------------------------------------------------------------------- @@ -35,126 +34,43 @@ else $selected_id = -1; //--------------------------------------------------------------------------------------------- +function get_company_extensions($id = -1) { -function check_data($id) -{ - if ($_POST['name'] == "") { - display_error(_("Module name cannot be empty.")); - return false; - } - if ($_POST['path'] == "") { - display_error(_("Module folder name cannot be empty.")); - return false; - } - if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) { - display_error(_("You have to select module file to upload")); - return false; - } - return true; -} + global $path_to_root; -/** - * @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; + $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php'; + $installed_extensions = array(); + if (is_file($file)) { + include($file); + } + return $installed_extensions; } -function write_modules() +function check_data($id, $exts) { - global $path_to_root, $installed_modules; - - $mods = array_natsort($installed_modules, 'tab', 'tab'); - $installed_modules = $mods; - //reset($installed_languages); - $n = count($installed_modules); - $msg = " 0) - $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 .= "'acc_file' => '" . $installed_modules[$i]['acc_file'] . "'"; - $msg .= "),\n"; + if ($_POST['name'] == "") { + display_error(_("Extension name cannot be empty.")); + return false; } - $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); + foreach($exts as $n =>$ext) { + if ($_POST['name'] == $ext['name'] && $id != $n) { + display_error(_("Extension name have to be unique.")); 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 - { - display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation.")); + + if ($_POST['title'] == "") { + display_error(_("Extension title cannot be empty.")); + return false; + } + if ($_POST['path'] == "") { + display_error(_("Extension folder name cannot be empty.")); return false; } + if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) { + display_error(_("You have to select plugin file to upload")); + return false; + } return true; } @@ -162,16 +78,22 @@ function write_modules() function handle_submit() { - global $path_to_root, $installed_modules, $db_connections, $selected_id; + global $path_to_root, $db_connections, $selected_id; - if (!check_data($selected_id)) + $extensions = get_company_extensions(); + if (!check_data($selected_id), $extensions) return false; $id = $_GET['id']; - $installed_modules[$id]['tab'] = $_POST['tab']; - $installed_modules[$id]['name'] = $_POST['name']; - $installed_modules[$id]['path'] = $_POST['path']; + $extensions[$id]['tab'] = $_POST['tab']; + $extensions[$id]['name'] = $_POST['name']; + $extensions[$id]['path'] = $_POST['path']; + $extensions[$id]['title'] = $_POST['title']; + $extensions[$id]['active'] = $_POST['active']; + + // Currently we support only plugin extensions here. + $extensions[$id]['type'] = 'plugin'; $directory = $path_to_root . "/modules/" . $_POST['path']; if (!file_exists($directory)) { @@ -179,7 +101,7 @@ function handle_submit() } if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) { - $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name']; + $extensions[$id]['filename'] = $_FILES['uploadfile']['name']; $file1 = $_FILES['uploadfile']['tmp_name']; $file2 = $directory . "/".$_FILES['uploadfile']['name']; if (file_exists($file2)) @@ -187,7 +109,7 @@ function handle_submit() move_uploaded_file($file1, $file2); } else - $installed_modules[$id]['filename'] = get_post('filename'); + $extensions[$id]['filename'] = get_post('filename'); if (is_uploaded_file($_FILES['uploadfile2']['tmp_name'])) { $file1 = $_FILES['uploadfile2']['tmp_name']; @@ -201,7 +123,7 @@ function handle_submit() if (is_uploaded_file($_FILES['uploadfile3']['tmp_name'])) { - $installed_modules[$id]['acc_file'] = $_FILES['uploadfile3']['name']; + $extensions[$id]['acc_file'] = $_FILES['uploadfile3']['name']; $file1 = $_FILES['uploadfile3']['tmp_name']; $file2 = $directory . "/".$_FILES['uploadfile3']['name']; if (file_exists($file2)) @@ -209,9 +131,9 @@ function handle_submit() move_uploaded_file($file1, $file2); } else - $installed_modules[$id]['acc_file'] = get_post('acc_file'); + $extensions[$id]['acc_file'] = get_post('acc_file'); - if (!write_modules()) + if (!write_extensions($extensions)) return false; return true; } @@ -220,11 +142,19 @@ function handle_submit() function handle_delete() { - global $path_to_root, $installed_modules; + global $path_to_root; + + $extensions = get_company_extensions(); $id = $_GET['id']; - $path = $installed_modules[$id]['path']; + $path = $extensions[$id]['path']; + + if ($extensions[$id]['type'] != 'plugin') { + display_error(_('Module installation support is not implemented yet. You have to do it manually.')); + return; + } + $filename = "$path_to_root/modules/$path"; if ($h = opendir($filename)) { @@ -237,72 +167,126 @@ function handle_delete() } rmdir($filename); - unset($installed_modules[$id]); - $mods = array_values($installed_modules); - $installed_modules = $mods; + unset($extensions[$id]); + $mods = array_values($extensions); + $extensions = $mods; - if (!write_modules()) + if (!write_extensions($extensions)) return; + + // should we also delete module form per company extension files? + meta_forward($_SERVER['PHP_SELF']); } //--------------------------------------------------------------------------------------------- -function display_modules() +function display_extensions() { - global $table_style, $installed_modules, $tabs; + global $table_style, $tabs; echo " "; start_table($table_style); - $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), _("Access extensions"),"", ""); + $th = array(_("Name"),_("Tab"), _("Link text"), _("Folder"), _("Filename"), + _("Access extensions"),"", ""); table_header($th); $k = 0; - $mods = $installed_modules; + $mods = get_company_extensions(); $n = count($mods); for ($i = 0; $i < $n; $i++) { + $is_mod = $mods[$i]['type'] == 'module'; alt_table_row_color($k); - - label_cell($tabs[$mods[$i]['tab']]); label_cell($mods[$i]['name']); + label_cell( $is_mod ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]); + $ttl = access_string($mods[$i]['title']); + label_cell($ttl[0]); label_cell($mods[$i]['path']); label_cell($mods[$i]['filename']); label_cell(@$mods[$i]['acc_file']); $edit = _("Edit"); $delete = _("Delete"); - if (user_graphic_links()) + if ($is_mod) + { + label_cell(''); // not implemented (yet) + label_cell(''); + } + else { - $edit = set_icon(ICON_EDIT, $edit); - $delete = set_icon(ICON_DELETE, $delete); + if (user_graphic_links()) + { + $edit = set_icon(ICON_EDIT, $edit); + $delete = set_icon(ICON_DELETE, $delete); + } + label_cell("$edit"); + label_cell("$delete"); } - label_cell("$edit"); - label_cell("$delete"); end_row(); } end_table(); } +function company_extensions($id) +{ + global $table_style, $tabs; + + start_table($table_style); + + $th = array(_("Name"),_("Tab"), _("Link text"), _("Active")); + + // get all available extensions and display + // with current status stored in company directory. + + $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; + } + } + + table_header($th); + $k = 0; + $n = count($mods); + for ($i = 0; $i < $n; $i++) + { + alt_table_row_color($k); + label_cell($mods[$i]['name']); + label_cell($mods[$i]['type'] == 'module' ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]); + $ttl = access_string($mods[$i]['title']); + label_cell($ttl[0]); + check_cells(null, 'Active'.$i, @$mods[$i]['active'] ? 1:0, + false, false, "align='center'"); + end_row(); + } + + end_table(1); + submit_center('Update', _('Update'), true, false, 'default'); +} + //--------------------------------------------------------------------------------------------- -function display_module_edit($selected_id) +function display_ext_edit($selected_id) { - global $installed_modules, $table_style2; + global $table_style2; + $extensions = get_company_extensions(); if ($selected_id != -1) $n = $selected_id; else - $n = count($installed_modules); + $n = count($extensions); - start_form(true); echo "