<?php  if (!defined('BASEPATH')) { exit('No direct script access allowed'); }
/**
 * CodeIgniter helper
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @copyright 2009 untermStrich
 * @package helpers
 * @version $Id$
 */

if ( ! function_exists('validate_minversion')) {
    /**
     * Validates the minversion
     *
     * @param   string	$minver         The minversion to compare
     * @param   string  &$us_version    Optional Reference Return us_version
     * @return  boolean
     */
    function validate_minversion($minver, &$us_version=false)
    {
        $CI = CI_Controller::get_instance();
        
        $us_version = $CI->config->item('us_version');

        if (preg_match_all('#\d+#', $minver, $matches, PREG_SET_ORDER)) {
            $x1 = array_shift($matches);
            $x2 = array_pop($matches);
            $minver_main = intval(array_pop($x1));
            $minver_id = intval(array_pop($x2));
        }

        if (preg_match_all('#\d+#', $us_version, $matches, PREG_SET_ORDER)) {
            $x1 = array_shift($matches);
            $x2 = array_pop($matches);
            $version_main = intval(array_pop($x1));
            $version_id = intval(array_pop($x2));
        }
        
        if (empty($minver_main) || empty($minver_id)) {
            return false;
        }
        
        if ($version_main >= $minver_main) {
            if ($version_id >= $minver_id) {
                return true;
            }
        }
        
        return false;
    }
}

if ( ! function_exists('validate_maxversion')) {
    /**
     * Validates the maxversion
     *
     * @param   string	$maxver         The maxversion to compare
     * @param   string  &$us_version    Optional Reference Return us_version
     * @return  boolean
     */
    function validate_maxversion($maxver, &$us_version=false)
    {
        $CI = CI_Controller::get_instance();
        
        $us_version = $CI->config->item('us_version');

        if (preg_match_all('#\d+#', $maxver, $matches, PREG_SET_ORDER)) {
            $x1 = array_shift($matches);
            $x2 = array_pop($matches);
            $maxver_main = intval(array_pop($x1));
            $maxver_id = intval(array_pop($x2));
        }

        if (preg_match_all('#\d+#', $us_version, $matches, PREG_SET_ORDER)) {
            $x1 = array_shift($matches);
            $x2 = array_pop($matches);
            $version_main = intval(array_pop($x1));
            $version_id = intval(array_pop($x2));
        }
        
        if (empty($maxver_main) || empty($maxver_id)) {
            return false;
        }
        
        if ($version_main <= $maxver_main) {
            if ($version_id <= $maxver_id) {
                return true;
            }
        }
        
        return false;
    }
}


if ( ! function_exists('get_maxversion_for_butler')) {
    /**
     * 
     *
     * @param   string	$maxver  The maxversion to compare
     * @param   string  &$us_version    Reference Return us_version
     * @return  boolean
     */
    
    /**
     * Get the maxversion for a specific butler and butlerversion
     * 
     * @param   string  $portlet_class      Portlet Class
     * @param   string  $butlerversion      Butler version
     * @param   string  $maxver             Current defined max version
     * @return  string  Max Version
     */
    function get_maxversion_for_butler($portlet_class, $butlerversion, $maxver)
    {
        switch ($portlet_class) {
            case 'Com_untermstrich_budget_liqui':
            case 'Com_untermstrich_cost_overview':
            case 'Com_untermstrich_employee_hourly_rate':
            case 'Com_untermstrich_hoai':
            case 'Com_untermstrich_liqui':
                if (empty($butlerversion) || $butlerversion<2) {
                    $maxver = '11.99.99.99999';
                }
                break;
        }
        
        return $maxver;
    }
}



/* End of file us_portlet_helper.php */
