Categories > TinyButStrong general >

Multiple plugin operations?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: sheepy
Date: 2006-08-01
Time: 10:40

Multiple plugin operations?

I noticed that as of v3.1.1, only the last 'opt' parameter of a series is recognised.  Is there some other way to allow for multiple plugin operations?  This will allow simple solution of complicated operations that would otherwise has to be processed with php.
By: sheepy
Date: 2006-08-01
Time: 10:42

Re: Multiple plugin operations?

I mean, aside from resorting to using new parameters (instead of operations) and trapping the expensive OnFormat
By: Skrol29
Date: 2006-08-01
Time: 14:39

Re: Multiple plugin operations?

Hi,

Thta's true, oly the last parameter "ope" is taken into acount into a same TBS field. I didn't think it could be a problem. I think this parameter can be enhanced in a futur version in order to accept several values.

Can you give me an example of several "ope" usage you'd like to perform ?
By: sheepy
Date: 2006-08-04
Time: 12:32

Re: Multiple plugin operations?

Here is a simplified example of what I'm really using:

[item.price;add=[tax];multi=[currency_rate];zeromagnet;magnet=_;frm=[currency_format]]
OR
[item.price;+=[tax];*=[currency_rate];zeromagnet;magnet=_;frm=[currency_format]]

What the above block does, when merged in the correct order, is take an item's price, add some tax, multiply the sum by some currency rate, and then trigger magnet if the result is zero (by setting the value to ''), and finally apply formatting if it's not magneted.

It is not terriably efficient, I know, but it saves me time.  The syntax is simple and flexible, safe to be copied around, no need to write custom function, and let me merge database result directly.

This also let me easily and safely change how prices are displayed by a multi-file replace, since I don't have to look for prices in code and pick out the one that are shown from the one being processed.

Other functions are possible, I've got substring, lower case, upper case, random number, negation, floor, round, boolean expression, etc.  I am not using all of them but I think they may be useful someday.
By: sheepy
Date: 2006-08-04
Time: 12:37

Re: Multiple plugin operations?

Actually, except for the performance concern that every parameter of every field has to go through a long list of elseif, I'm quite happy with how it works, both the syntax and the result.

The plugin goes like this (I haven't tested all operations)

define('TBS_PLUS','clsTbsPlugInPlus');
$GLOBALS['_TBS_AutoInstallPlugIns'][] = TBS_PLUS; // Auto-install
class clsTbsPlugInPlus {
    function OnInstall() { return array('OnFormat'); }
    function OnFormat($FieldName,&$Value,&$PrmLst,&$TBS) {
      foreach ($PrmLst as $key => $param) {
        $key = strtolower($key);
          // Mathematical operations
        if ($key=='+') $Value += $param;     // Multiplication
        elseif ($key=='-') $Value -= $param;     // Multiplication
        elseif ($key=='*') $Value *= $param;     // Multiplication
        elseif ($key=='/') $Value /= $param;     // Multiplication
        elseif ($key=='add') $Value += $param;         // Addition
        elseif ($key=='dec') $Value -= $param;         // Addition
        elseif ($key=='multi') $Value *= $param;     // Multiplication
        elseif ($key=='div') $Value /= $param;       // Division
      elseif ($key=='inv') $Value = $param/$Value; // Inversion
      elseif ($key=='mod') $Value = $Value % $param;
      elseif ($key=='power') $Value = pow($Value, $param); // Power to value
        elseif ($key=='random') $Value = rand(1, $Value); // Random number of 1..value
        elseif ($key=='sqrt') $Value = sqrt($Value);      // Square root
        elseif ($key=='floor') $Value = floor($Value);    // Floor number
        elseif ($key=='round') $Value = round($Value);    // Floor number
      elseif ($key=='neg') $Value = -$Value; // Negation
          // String operations
        elseif ($key=='sub') {  // Substring                        
              $param = explode(',', $param, 2);
              if (sizeof($param)==1) $Value = substr($Value, $param[0]);
              else $Value = substr($Value, $param[0],$param[1]); 
        }
        elseif ($key=='lowercase') $Value = strtolower($Value);                                
        elseif ($key=='uppercase') $Value = strtoupper($Value);           
          // Boolean operators
        elseif ($key=='not') $Value = !$Value;
        // Magnet functions
        elseif ($key=='zeromagnet' && is_numeric($Value) && !(float)$Value) $Value='';
        elseif ($key=='emptymagnet' && empty($Value)) $Value='';
      }
    }
}