Menu

optional variable - quick patch

2012-04-04
2013-04-26
  • Greg Panula

    Greg Panula - 2012-04-04

    Here is a quick patch to odf.php that adds a new function(OptVar).  OptVar allows one to specify an optional variable in the template… i.e. maybe this variable is in the template, maybe it isn't.

    It is a quick & dirty hack but thought others might find it useful.

    --- a/library/odf.php   Tue Apr 03 14:48:22 2012 -0500
    +++ b/library/odf.php   Wed Apr 04 09:06:17 2012 -0500
    @@ -88,6 +88,29 @@
             return $this;
         }
         /**
    +     * Assing an optional template variable
    +     *
    +     * @param string $key name of the variable within the template
    +     * @param string $value replacement value
    +     * @param bool $encode if true, special XML characters are encoded
    +     * @return odf
    +     */
    +    public function OptVar($key, $value, $encode = true, $charset = 'ISO-8859')
    +    {
    +        if (strpos($this->contentXml, $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']) === false) {
    +   // variable was NOT found in the template
    +   $notme="<text:line-break/>";
    +   $this->$notme;
    +   }
    +   else {  // variable WAS found in the template
    +        // variable was found in the template, lets act on it
    +        $value = $encode ? htmlspecialchars($value) : $value;
    +        $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
    +        $this->vars[$this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']] = str_replace("\n", "<text:line-break/>", $value);        
    +   }
    +   return $this;
    +    }
    +    /**
          * Assign a template variable as a picture
          *
          * @param string $key name of the variable within the template
    
     
  • Greg Panula

    Greg Panula - 2012-04-04

    Ok, here's a better patch… fixes the Undefined error

    --- a/library/odf.php   Tue Apr 03 14:48:22 2012 -0500
    +++ b/library/odf.php   Wed Apr 04 09:32:54 2012 -0500
    @@ -32,6 +32,7 @@
         protected $images = array();
         protected $vars = array();
         protected $segments = array();
    +    protected $emptyvar;
         const PIXEL_TO_CM = 0.026458333;
         /**
          * Class constructor
    @@ -88,6 +89,29 @@
             return $this;
         }
         /**
    +     * Assing an optional template variable
    +     *
    +     * @param string $key name of the variable within the template
    +     * @param string $value replacement value
    +     * @param bool $encode if true, special XML characters are encoded
    +     * @return odf
    +     */
    +    public function OptVar($key, $value, $encode = true, $charset = 'ISO-8859')
    +    {
    +        if (strpos($this->contentXml, $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']) === false) {
    +   // variable was NOT found in the template
    +   $notme="emptyvar";
    +   $this->$notme;
    +   }
    +   else {  // variable WAS found in the template
    +        // variable was found in the template, lets act on it
    +        $value = $encode ? htmlspecialchars($value) : $value;
    +        $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
    +        $this->vars[$this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']] = str_replace("\n", "<text:line-break/>", $value);        
    +   }
    +   return $this;
    +    }
    +    /**
          * Assign a template variable as a picture
          *
          * @param string $key name of the variable within the template
    
     

Log in to post a comment.