Categories > OpenTBS with ODT >

openTBS error: global variables not set

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Vincent
Date: 2011-08-08
Time: 09:04

openTBS error: global variables not set

Hello,

Using TBS and openTBS (formerly known as oo<something>) for several years now, and am very glad I can use this plugin.

Now I'm adding a new function to my site to be able to use an advanced document-templating system, but it won't work, although I have copy pasted both a piece of working PHP-code and my Openoffice/Libreoffice-template.

I've used [var], [onload] and [onshow], but I keep getting the error 'global variable not set', which is strange, because if I replace the TBS->show() with a function to view all variables, they are shown perfectly.

Here is my code (not very elegant, but made as bare as possible to check for errors):
    public function make( $POST ) {

        // Split off the array with cc-id's
        $cc        = array();
        $others    = array(
                    "newdoc_to"            => $POST["newdoc_to"],
                    "newdoc_subject"    => $POST["newdoc_subject"],
                    "newdoc_template"    => $POST["newdoc_template"],
                    "newdoc_sendby"        => $POST["newdoc_sendby"],
                    "dos_nr"            => $POST["dos_nr"],
                    "action"            => $POST["action"]
                    );
        $cc            = array_diff_assoc( $POST, $others );
        // Process other variables
        $to            = $POST["newdoc_to"];
        $subject    = $POST["newdoc_subject"];
        $template    = $POST["newdoc_template"];
        $sendby        = $POST["newdoc_sendby"];
        $dos_nr        = $POST["dos_nr"];

        // FETCH CONTENT OF LETTER
        $content2    = $this->get_tpl_detail( $template, "text" );

        // FETCH INFO ABOUT RECIPIENT OF LETTER
        $Pt        = new Party;
        $rec    = $Pt->get_parties_by_id( $to );
        $recinfo= $Pt->get_info_by_parties( $rec );

        // PROCESS VARIABLES
        $dosNr             = $dos_nr;
        $dosName        = $subject;
        $now            = time();
        $date             = trim( strftime ( "%e %B %Y", $now ) );
    //    $Kst    = new Kast;
    //    $dosLoc         = $Kst->getNr( $dosNr );
        $addr_title        = $recinfo[0]['title'];
        $addr_naam        = $recinfo[0]['givenname'] . " " . $recinfo[0]['sn'];
        $addr_straat    = $recinfo[0]['postaladdress'];
        $addr_postcode    = $recinfo[0]['postalcode'];
        $addr_plaats    = $recinfo[0]['l'];
        $addr_fax        = $recinfo[0]['facsimiletelephonenumber'];
        $addr_email        = $recinfo[0]['email'];
        $addr_referte    = $recinfo[0]['referte'];

        // FETCH VERZENDINGSWIJZE
        switch( $sendby ) {
            case 2:
                $howtosend    = "per email: ";
                $wheretosend= $addr_email;
                $sendabbr    = "m_";
            break;
            case 3:
                $howtosend    = "per fax: ";
                $wheretosend= $addr_fax;
                $sendabbr    = "f_";
            break;
            case 4:
                $howtosend    = "per aangetekende post en per gewone post";
                $wheretosend= "";
                $sendabbr    = "ab_";
            break;
            default:
                $howtosend    = "";
                $wheretosend= "";
                $sendabbr    = "b_";
            break;
        }

        //Variabelen mbt naam van de brief
        $addr_persona    = $recinfo[0]['persona'];

        $Pt1    = new Party;
        $prstri    = $Pt1->get_persona_detail( $recinfo[0]['persona'] );

        $fileYear    = substr( $dosNr, 0, 2 );
        $datetime    = date( "Ymd_His", $now );
        $filename    = Config::$docs_path . "20".$fileYear."/".$dosNr." - ".$dosName."/".$datetime."-".$sendabbr.$prstri.".odt";

        //Maak de brief
        $TBS = new clsTinyButStrong;
        $TBS->Plugin( TBS_INSTALL, OPENTBS_PLUGIN );
        $TBS->LoadTemplate( Config::$proi_path . "/briefhoofd/brief2.odt#" );
        $TBS->Show( OPENTBS_FILE, $filename );

        //Wijzig de eigenschappen van de brief
        chmod( $filename, 0660 );
        chgrp( $filename, "users" );

        //Verwijs de gebruiker terug naar de plaats waar die vandaan kwam
        header("location: index.php?p=dos&s=$dosNr");
    }
If I add ';noerr' to all variables inside the template, then an empty document is generated, so I'm really scratching my head over this one.

BTW: using TBS 3.7.0 and openTBS 1.6.1

THX!
By: Skrol29
Date: 2011-08-08
Time: 23:24

Re: openTBS error: global variables not set

Hello Vincent,

> I've used [var], [onload] and [onshow], but I keep getting the error 'global variable not set',

Gan you give the detail of the TBS error you've got ?

> which is strange, because if I replace the TBS->show() with a function to view all variables, they are shown perfectly.

Which "all variables" are you talking about ?
By: Vincent
Date: 2011-08-09
Time: 08:47

Re: openTBS error: global variables not set

Hi Skrol,

Once again thx for your prompt replies!

This is the error I'm getting for *every* variable I'm using in the template:
TinyButStrong Error in field [var.dosName...]: the PHP global variable named 'dosName' does not exist or is not set yet. This message can be cancelled using parameter 'noerr'.

TinyButStrong Error Show() Method: The output is cancelled by the OpenTBS plugin because at least one error has occured.
(last line is only shown once of course.)

Extract from the template I'm using:
[onshow.addr_title;htmlconv=no] [onshow.addr_naam;htmlconv=no]
    
[onshow.addr_straat;htmlconv=no]
[onshow.addr_postcode;htmlconv=no] [onshow.addr_plaats;htmlconv=no]
[onshow.howtosend][onshow.wheretosend]
I have tried using onshow, onload and with var, and I have tried with $TBS->MergeField().

The reason I'm really stumped is because I also have a 'brief.odt'-template, which is almost completely the same, with the same variables and with the same PHP-backend (with fewer variables), and that one is working perfectly.
By: Skrol29
Date: 2011-08-10
Time: 10:43

Re: openTBS error: global variables not set

Hi Vincent,

Since your variables are defined in a function scope, they are not global anymore. TBS automatic fields [onload], [onshow] and [var] can merge only global variables.
By: Vincent
Date: 2011-08-12
Time: 09:39

Re: openTBS error: global variables not set

THX.

I guess this is where I can no longer hide I'm an amateur. I tend to forget that whenever I have added some functionality, pounding me on the chest :)

Can I ask you what the best solution is in this case? I have an array generated like this:
// FETCH RECIPIENT
$Pt    = new Party;
$recipient    = $Pt->get( $recip_id );
I have tried all kinds of variants of the MergeField()-method, but I don't seem to get them to merge in the template...
By: Skrol29
Date: 2011-08-13
Time: 01:23

Re: openTBS error: global variables not set

The best way is to gather needed variables into a PHP associative array, and them merge this array with MergeField().
Such variables can be arrays.

Example:
PHP
$content2 = $this->get_tpl_detail( $template, "text" );
...
$Pt = new Party;
$recipient = $Pt->get( $recip_id );
...
$TBS = new clsTinyButStrong;
$TBS->Plugin( TBS_INSTALL, OPENTBS_PLUGIN );
$TBS->LoadTemplate( Config::$proi_path . "/briefhoofd/brief2.odt#" );

$info = array(
  'content2' => $content2,
  'recipient' => $recipient,
  ...
);
$TBS->MergeField('info', $info);

$TBS->Show( OPENTBS_FILE, $filename );



Template
[info.content2]
...
[info.recipient.0.title]
[info.recipient.0.givenname]
...
By: Vincent
Date: 2011-08-13
Time: 12:24

Re: openTBS error: global variables not set

Great! Thank you for the great support - this is the kind of project I would happily donate to, but I don't see any donation-information on your site?

Your support has enabled me to make a (more or less) working interface to create a letter with a choice of default contents, but I would like to take it 2 steps further:

1. The default content (templates, but this word could be confusing) could contain variables that should be merged also. I guess this would mean merging the content first with 'onload' and the other variables with 'onshow' or even 'onformat'. Template side, I would do this:
[onload.D.get_tpl_detail( $template, "text" );htmlconv=no;noerr]
...but then I have to find a way to get $template merged before...
Other solution would be to do this inside the template:
[content;htmlconv=no;ondata=fct_convert_all_vars_to_data]
Which do you prefer?

2. Formatting the content of the letter is proving difficult. I have found the XML specs for odt-files, and I now know I have to place a paragraph between '<text:p></text:p>'. But if merge text with these tags, the tags are either printed inside the odt (htmlconv=yes), or the text is invisible (htmlconv=no)...

As I said earlier: if you would make donations possible, I will be among the first to donate  - until then I can only thank you time and time again :)
By: Skrol29
Date: 2011-08-14
Time: 15:37

Re: openTBS error: global variables not set

Hi,

> this is the kind of project I would happily donate to, but I don't see any donation-information on your site?

Thanks for your appreciation. There used to be a donation link, but it has been deleted.
I nice communication about this tool is better than donation for us.

1)

The second one is better.

2)

I you have to use XML tags in the contents, then you do need "htmlconv=no" in order to keep them as is.
If the text inside a <text:p> element is invisible it is probably because the TBS tag is itself inside an <text:p> element in the ODT template.
You probably need to inspect the XML content of the template in order to perform formating in your merged content.