Changed hook function name from TaxFunction to tax_report_done.
[fa-stable.git] / includes / packages.inc
index 300741387d754f294a3c5a30dbca847706d88c41..a98b8f897ff639628f389d99602fd110f8104cd0 100644 (file)
@@ -13,9 +13,9 @@ include_once($path_to_root. "/includes/archive.inc");
 
 define('PKG_CACHE_PATH', $path_to_root.'/modules/_cache');
 define('PUBKEY_PATH', $path_to_root);
-define('REPO_URL', "$repository/$FA_repo_version");
+define('REPO_URL', 'http://'.$repo_auth['login'].':'.$repo_auth['pass'].'@'.$repo_auth['host'].'/'.$repo_auth['branch']);
 //
-// FrontAccounting package class       
+// FrontAccounting package class
 //
 class package extends gzip_file {
        function package($filename, $basedir=null)
@@ -54,11 +54,6 @@ class package extends gzip_file {
                $cachepath = $this->options['basedir'];
                $ctrl = get_control_file("$cachepath/_init/config");
 
-//             $targetdir = $pkg_data_basedir[$ctrl['Type']];
-//             if ($targetdir)
-//                     $targetdir = $path_to_root.$targetdir.$ctrl['Package'];
-//             else
-//                     $targetdir = $path_to_root;
                $targetdir = $path_to_root.'/'.$ctrl['InstallPath'];
 
                if (!is_dir($targetdir))
@@ -92,11 +87,7 @@ class package extends gzip_file {
 
                $cachepath = $this->options['basedir'];
                $ctrl = get_control_file("$cachepath/_init/config");
-//             $targetdir = $pkg_data_basedir[$ctrl['Type']];
-//             if ($targetdir)
-//                     $targetdir = $path_to_root.$targetdir.$ctrl['Package'];
-//             else
-//                     $targetdir = $path_to_root;
+
                $targetdir = $path_to_root.'/'.$ctrl['InstallPath'];
 
                $dpackage = new package("$cachepath/_data", $targetdir);
@@ -127,10 +118,8 @@ class package extends gzip_file {
                        include("$cachepath/_init/init.php");
                        if (method_exists($installer, $name)) {
                                set_include_path("$cachepath/_init".PATH_SEPARATOR.get_include_path());
-//                             $old = getcwd();
-//                             chdir("$cachepath/_init");
+
                                $ret = $installer->$name($params);
-//                             chdir($old);
                                return $ret;
                        }
                }
@@ -190,7 +179,7 @@ function get_control_file($file, $index = false) {
        $line = '';
        do {
                $line = rtrim($line);
-               if (@ctype_space($line[0])) { // continuation of multiline property
+               if ($line && ctype_space($line[0])) { // continuation of multiline property
                        if (strlen(ltrim($line))) {
                                if ($value !== '' && !is_array($value))
                                        $value = array($value);
@@ -216,7 +205,6 @@ function get_control_file($file, $index = false) {
                        $pkg = array(); 
                        $key = null; $value = '';
                        continue;
-//             } elseif (preg_match('/([\w-]*):\s*(.*)/', $line, $m)) {
                } elseif (preg_match('/([^:]*):\s*(.*)/', $line, $m)) {
                        $key = $m[1]; $value = $m[2];
                        if (!strlen($key)) {
@@ -256,8 +244,7 @@ function save_control_file($fname, $list, $zip=false)
                                        }
                                }
                                // array elements on subsequent lines starting with white space
-                               $value = //(count($value) ? "\n " :'').
-                                       implode("\n ", $value);
+                               $value = implode("\n ", $value);
                        }
                        $zip ? gzwrite($file, "$key: $value\n") : fwrite($file, "$key: $value\n");
                }
@@ -290,14 +277,14 @@ function pkg_prop($pkg, $property, $lang=false)
 //
 function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey=null, $download=true) {
 
-       global $path_to_root, $repository, $FA_repo_version;
+       global $path_to_root, $repo_auth;
 
        // first download local copy of repo release file
        // and check remote signature with local copy of public key
        //
        $loclist = PKG_CACHE_PATH.'/Release.gz';
        
-       if ($type!=null && !is_array($type)) {
+       if (isset($type) && !is_array($type)) {
                $type = array($type);
        }
        $refresh = true;
@@ -321,7 +308,6 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey
        } while($refresh);
 
        $Release = get_control_file($loclist, 'Filename');
-
        // download and check all indexes containing given package types
        // then complete package list or seek for pkg
        $Packages = array();
@@ -329,7 +315,7 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey
                if ($type && !count(array_intersect(explode(' ', $parms['Type']), $type))) {
                        unset($Release[$fname]); continue; // no packages of selected type in this index
                }
-               if ($Release[$fname]['Version'] != $FA_repo_version) {
+               if ($Release[$fname]['Version'] != $repo_auth['branch']) {
                        display_warning(_('Repository version does not match application version.')); // ?
                }
                $remoteindex = REPO_URL.'/'.$fname;
@@ -356,7 +342,7 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey
                $pkglist = get_control_file($locindex, 'Package');
                foreach($pkglist as $name => $pkg) {
                        $pkgfullname = REPO_URL.'/'.$parms['Path']."/".$pkg['Filename'].'.pkg';
-                       if ($type==null || in_array($pkg['Type'], $type)) {
+                       if (!isset($type) || in_array($pkg['Type'], $type)) {
                                if (empty($filter))
                                        $p = $pkg;
                                else {
@@ -387,14 +373,13 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey
                        }
                }
        }
-       
+
        return $Packages;
 }
 
 function get_package($pkgname, $type = null)
 {
-       $all = get_pkg_or_list($type, $pkgname);
-       $pkg = array_search_value($all, $pkgname, 'Package');
+       return get_pkg_or_list($type, $pkgname);
 }
 /*
        Returns full name of installed package, or null if package is not installed.
@@ -471,12 +456,17 @@ function get_languages_list()
 }
 //---------------------------------------------------------------------------------------
 //
-//     Return merged list of available and installed extensions in inform of local 
+//     Return merged list of available and installed extensions as a local 
 // configuration array supplemented with installed versions information.
 //
-function get_extensions_list()
+function get_extensions_list($type = null)
 {
-       $pkgs = get_pkg_or_list('extension', null, array(
+
+       if (isset($type) && !is_array($type)) {
+               $type = array($type);
+       }
+       
+       $pkgs = get_pkg_or_list($type, null, array(
                                'Package' => 'package',
                                'Version' => 'available',
                                'Name' => 'name',
@@ -485,6 +475,7 @@ function get_extensions_list()
                                'DefaultStatus'=> 'active',
                                'MenuTabs' => 'tabs',
                                'MenuEntries' => 'entries',
+                               'Encoding' => 'encoding',
                                'AccessExtensions' => 'acc_file',
                                'InstallPath' => 'path'
                        ));
@@ -493,7 +484,7 @@ function get_extensions_list()
        // 
        $local = get_company_extensions();
        foreach($local as $extno => $ext) {
-               if ($ext['type'] == 'theme') continue;
+               if (!in_array($ext['type'], $type)) continue;
                $ext['local_id'] = $extno;
                if (!isset($pkgs[$ext['package']]) || $ext['package'] == '')
                        $pkgs[] = $ext;
@@ -504,7 +495,7 @@ function get_extensions_list()
        return $pkgs;
 }
 //
-// Return merged list of available and installed extensions in inform of local
+// Return merged list of available and installed extensions as a local
 // configuration array supplemented with installed versions information.
 //
 function get_themes_list()
@@ -531,6 +522,39 @@ function get_themes_list()
        ksort($pkgs);
        return $pkgs;
 }
+//---------------------------------------------------------------------------------------
+//
+//     Return merged list of available and installed COAs as a local 
+// configuration array supplemented with installed versions information.
+//
+function get_charts_list()
+{
+       $pkgs = get_pkg_or_list('chart', null, array(
+                               'Package' => 'package',
+                               'Version' => 'available',
+                               'Name' => 'name',
+                               'Description' => 'Descr',
+                               'Type' => 'type',
+                               'InstallPath' => 'path',
+                               'Encoding' => 'encoding',
+                               'SqlScript' => 'sql'
+                       ));
+
+       // add/update default charts
+       // 
+       $local = get_company_extensions();
+
+       foreach($local as $extno => $ext) {
+               if ($ext['type'] != 'chart') continue;
+               $ext['local_id'] = $extno;
+               if (!isset($pkgs[$ext['package']]) || $ext['package'] == '')
+                       $pkgs[] = $ext;
+               else
+                       $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
+       }
+       ksort($pkgs);
+       return $pkgs;
+}
 //---------------------------------------------------------------------------------------------
 //     Install/update package from repository
 //
@@ -578,7 +602,7 @@ function install_extension($pkg_name)
 {
        global $path_to_root, $next_extension_id, $Ajax;
        
-       $pkg = get_pkg_or_list(array('extension', 'theme'), $pkg_name);
+       $pkg = get_pkg_or_list(array('extension', 'theme', 'chart'), $pkg_name);
        if ($pkg) {
                $package = new package("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
                $local_exts = get_company_extensions();
@@ -593,9 +617,6 @@ function install_extension($pkg_name)
                                'type' => $pkg['Type'],
                                'active' => true,
                                'path' => $pkg['InstallPath'],
-//                             'tabs' => $pkg['MenuTabs'],
-//                             'entries' => $pkg['MenuEntries'],
-//                             'acc_file' => @$pkg['AccessExtensions'],
                        );
                        if (isset($pkg['MenuTabs']))
                                $ext['tabs'] = $pkg['MenuTabs'];
@@ -603,14 +624,19 @@ function install_extension($pkg_name)
                                $ext['entries'] = $pkg['MenuEntries'];
                        if (isset($pkg['AccessExtensions']))
                                $ext['acc_file'] = $pkg['AccessExtensions'];
+                       if (isset($pkg['SqlScript']))
+                               $ext['sql'] = $pkg['SqlScript'];
                        $local_exts[$ext_id] = $ext;
-                       update_extensions($local_exts);
+                       $ret = update_extensions($local_exts);
                        unlink("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
                        $Ajax->activate('ext_tbl');
+                       return $ret;
                } else {
                        display_error(implode('<br>', $package->error));
+                       return false;
                }
        }
+       return true;
 }
 /*
        Returns true if newer package version is available