<?php

//register global/PHP functions to be used with your template files
//You can move this to common.conf.php   $config['TEMPLATE_GLOBAL_TAGS'] = array('isset', 'empty');
//Every public static methods in TemplateTag class (or tag classes from modules) are available in templates without the need to define in TEMPLATE_GLOBAL_TAGS
Doo::conf()->TEMPLATE_GLOBAL_TAGS = array('upper', 'tofloat', 'sample_with_args', 'debug', 'url', 'url2', 'function_deny', 'zhisset', 'empty', 'formatDate', 'ToChinaseNum');

/**
  Define as class (optional)

  class TemplateTag {
  public static test(){}
  public static test2(){}
  }
 * */
function upper($str) {
    return strtoupper($str);
}

function tofloat($str) {
    return sprintf("%.2f", $str);
}

function sample_with_args($str, $prefix) {
    return $str . ' with args: ' . $prefix;
}

function debug($var) {
    if (!empty($var)) {
	echo '<pre>';
	print_r($var);
	echo '</pre>';
    }
}

//This will be called when a function NOT Registered is used in IF or ElseIF statment
function function_deny($var = null) {
    echo '<span style="color:#ff0000;">Function denied in IF or ElseIF statement!</span>';
    exit;
}

function formatDate($date, $format = 'Y-m-d H:i:s') {
    return date($format, $date);
}

function zhisset($val) {
    return isset($val);
}

//Build URL based on route id
function url($id, $param = null, $addRootUrl = false) {
    Doo::loadHelper('DooUrlBuilder');
    // param pass in as string with format
    // 'param1=>this_is_my_value, param2=>something_here'

    if ($param != null) {
	$param = explode(', ', $param);
	$param2 = null;
	foreach ($param as $p) {
	    $splited = explode('=>', $p);
	    $param2[$splited[0]] = $splited[1];
	}
	return DooUrlBuilder::url($id, $param2, $addRootUrl);
    }

    return DooUrlBuilder::url($id, null, $addRootUrl);
}

//Build URL based on controller and method name
function url2($controller, $method, $param = null, $addRootUrl = false) {
    Doo::loadHelper('DooUrlBuilder');
    // param pass in as string with format
    // 'param1=>this_is_my_value, param2=>something_here'

    if ($param != null) {
	$param = explode(', ', $param);
	$param2 = null;
	foreach ($param as $p) {
	    $splited = explode('=>', $p);
	    $param2[$splited[0]] = $splited[1];
	}
	return DooUrlBuilder::url2($controller, $method, $param2, $addRootUrl);
    }

    return DooUrlBuilder::url2($controller, $method, null, $addRootUrl);
}

function ToChinaseNum($num) {
    $char = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
    $dw = array("", "十", "百", "千", "万", "亿", "兆");
    $retval = "";
    $proZero = false;
    for ($i = 0; $i < strlen($num); $i++) {
	if ($i > 0)
	    $temp = (int) (($num % pow(10, $i + 1)) / pow(10, $i));
	else
	    $temp = (int) ($num % pow(10, 1));

	if ($proZero == true && $temp == 0)
	    continue;

	if ($temp == 0)
	    $proZero = true;
	else
	    $proZero = false;

	if ($proZero) {
	    if ($retval == "")
		continue;
	    $retval = $char[$temp] . $retval;
	} else
	    $retval = $char[$temp] . $dw[$i] . $retval;
    }
    if ($retval == "一十")
	$retval = "十";
    return $retval;
}

?>