<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO


/*
    adi_calc - Calculator for TXP variables

    Written by Adi Gilbert

    Released under the GNU General Public License

    Version history:
    1.2        - TXP 4.6 tag registration
    1.1        - tidied up integer/floating point modes, default now strictly integer, floating point switched on with 'precision' (thanks Bloke)
            - 'div' can be specified as 'divide' (improve code readability)
    1.0        - new attribute: 'precision' to switch into floating point mode
            - new attribute: 'result' variable set to result of calculation, original variable left unchanged
            - new attribute: 'reset' to automatically reset value to zero
            - new attribute: 'reset_name' to count the number of resets
            - enhancement: container mode can be used to set the initial value
            - 'name' attribute no longer mandatory
            - no longer picky about lack of initial value - if all else fails use zero
    0.3        - fix: error if adi_calc is the first person to set a TXP variable (thanks maniqui)
            - new attribute: 'ceiling' to always round up the result of integer division (for aswihart)
    0.2        - new attribute: 'value' to explicitly set an initial value to variable (for maniqui)
            - new attribute: 'display' to display result of the calculation (for maniqui)
    0.1        - initial release

    INVESTIGATE: variable( array('name' => 'something', 'value' => 42) );

*/

// TXP 4.6 tag registration
if (class_exists('\Textpattern\Tag\Registry')) {
    
Txp::get('\Textpattern\Tag\Registry')
        ->
register('adi_calc')
    ;
}

function 
adi_calc($atts,$thing=NULL) {

    
extract(lAtts(array(
        
'name'            => '',    // TXP variable name
        
'value'            => '',    // set initial value of variable
        
'display'        => '0',    // display the variable's value (after the calculation)
        
'add'            => '',    // name = name + value
        
'subtract'        => '',    // name = name - value
        
'multiply'        => '',    // name = name * value
        
'div'            => '',    // name = name / value
        
'divide'        => '',    // name = name / value (equivalent to div)
        
'mod'            => '',    // name = name MOD value (remainder)
        
'ceiling'        => '0',    // used with integer division to always round up the result
        
'precision'        => '',    // number of decimal places (floating point mode)
        
'reset'            => '0'// the point at which the variable is reset to zero
        
'reset_name'    => '',    // additional TXP variable name (for counting resets)
        
'result'        => '',    // additional TXP variable to contain result, original variable (i.e. $name) unchanged

    
), $atts));

    
// tweak attributes
    
if (array_key_exists('divide'$atts))
        
$div $divide;

    
// check if any TXP variable has ever been set ever, ever, ever (else get error)
    
if (!isset($GLOBALS['variable']))
        
$GLOBALS['variable'] = array();

    
// get/set initial value
    
if ($thing === NULL) { // self closing mode
        
if (!array_key_exists('value'$atts)) // TXP variable 'value' attribute not supplied
            
if (array_key_exists($name,$GLOBALS['variable'])) // use TXP variable value from 'name' attribute
                
$value $GLOBALS['variable'][$name];
            else
                
$value 0// default to zero
    
}
    else { 
// container mode
        
$value trim(parse($thing));
        if (empty(
$value)) {
            if (
array_key_exists($name,$GLOBALS['variable'])) // use TXP variable value from 'name' attribute
                
$value $GLOBALS['variable'][$name];
            else
                
$value 0// default to zero
        
}
    }

    
// cope with a lack of 'name' attribute
    
if (!$name) { // name not supplied
        
$display '1'// force display mode
        
$name 'adi_calc_temp_name'// assign dumy name, else GLOBAL variables array gets an element called ''
    
}

    
// let's do it
    
if ($reset_name// set reset counter
        
if (array_key_exists($reset_name,$GLOBALS['variable'])) // use TXP variable value from 'reset_name' attribute
            
$reset_value $GLOBALS['variable'][$reset_name];
        else
            
$reset_value $GLOBALS['variable'][$reset_name] = 0;
    if (
$add)
        
$value $value $add;
    if (
$subtract)
        
$value $value $subtract;
    if (
$multiply)
        
$value $value $multiply;
    if (
$div)
        
$value $value $div;
    if (
$mod)
        
$value $value $mod;
    if (
$reset)
        if (
$value == $reset) {
            
$value 0;
            if (
$reset_name)
                
$GLOBALS['variable'][$reset_name] = $reset_value 1// increment reset counter
        
}

    
// massage the result
    
if (array_key_exists('precision'$atts)) { // precision attribute supplied, so avoid integer mode
        
if ($precision != '')
            
$value round($value$precision); // round up/down
    
}
    else 
// integer
        
if ($ceiling)
            
$value = (int)ceil($value);
        else
            
$value = (int)($value);

    
// ta-da!
    
if ($result)
        
$GLOBALS['variable'][$result] = $value// set TXP variable 'result' to new value
    
else
        
$GLOBALS['variable'][$name] = $value// set TXP variable 'name' to new value
    
if ($display)
        return 
$value;

}