<?PHP if (!defined('PmWiki')) exit();

/*


   EditFunctionHelper allows a wiki administrator to easily
    place, substitute or remove a function in EditFunctions array

  *****
  *
  *  Copyright 2018 Carlos A. Bonamigo <cabsec.pmwiki@gmail.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *****

*/

$RecipeInfo['EditFunctionHelper']['Version']  =  '20180319';

# place/removes/substitute a edit function 
# f = function_name;
# p = place_to_inject (<before or >after) 
# or place_to_substitute function name (no <>);
# b = begin ; e = end;
# a = action, (p-place, r-remove, s-substitute)
function EditFunctionHelper($f,$p,$a='p'){ 
global $EditFunctions;
  if($f == '' || $p == '') return;
  if($p == "b" && $a == 'p'){array_unshift($EditFunctions,$f);return;}
  if($p == "e" && $a == 'p'){array_push($EditFunctions,$f);return;}
  if($p != "b" || $p != "e" && $a == 'p'){
    $NewEditFunctions = array();
    foreach($EditFunctions as $v){
      if($p != "<".$v && $p != ">".$v)
        $NewEditFunctions[] = $v;
      if($p == "<".$v){
        $NewEditFunctions[] = $f;   
        $NewEditFunctions[] = $v;
      }
      if($p == ">".$v){
        $NewEditFunctions[] = $v;
        $NewEditFunctions[] = $f;   
      }
    }
  }
  if($f != '' && $p != '' && $a == 's'){
    # f will be substitute by p
    foreach($EditFunctions as $v){
      if($v == $f) $NewEditFunctions[] = $p;
      else $NewEditFunctions[] = $v;
    }
  }
  if($f != '' && $p == '' & $a == 'r'){
    # f will be omitted in new array
    foreach($EditFunctions as $v){
      if($v != $f) $NewEditFunctions[] = $v
    }
  }
  $EditFunctions = $NewEditFunctions;
  return; 
}