Solved

php code problem

Posted on 2011-03-05
4
299 Views
Last Modified: 2012-05-11
I know nothing about php and have inheritted some code that is not working.  See attachment.

There are three functions in the code.  Basically what it is trying to do is set a value to $pn in the function onPostParse.  This value $pn is then used in the other two functions.

Can anyone see anything that is wrong.  The value of $pn is always an empty string. I want to use it in when i create the files in the other two functions.

<?php

 
class MyHandlers {

        private $pn = "Unknown";


        function addEventHandlers (RequestContext $ctx) {
                $handler = $this;


                $ctx->addEventHandler (FileGeneratedEvent::PDF, $handler, "onGeneratedPDF");

                $ctx->addEventHandler (FileGeneratedEvent::XML, $handler, "onGeneratedXML");
                $ctx->addEventHandler (ParseFieldEvent::POST_PARSE, $handler, "onPostParse");
       }
 

        function onPostParse( ParseFieldEvent $event)
        {
          $field = $event->field;
          if($field->key == "personnummer"){
            if ($field->value == ""){
              $this->pn = "Unknown" ;
            }
            else {
              $this->pn = $field->value;
            }
          }
          else { $this->pn = "NotFound" ; }
         
         }


        function onGeneratedPDF (FileGeneratedEvent $event)
        {

                $docDir = "/smart/export/journal";
                $docId = $event->document->id;
                $retval = 0; 
                $lines = array();
                $fileName = $docDir . "/" . basename($event->file). $this->pn . ".jpg";
                exec( "/usr/bin/convert $event->file $fileName", $lines, $retval );
                   if ($retval != 0)
                       throw new Exception ("Could not create jpg. $event->file to $fileName");

        }

        function onGeneratedXML (FileGeneratedEvent $event)
        {
                $docDir = "/smart/export/journal";
                $docId = $event->document->id;
                $fileName = $docDir . "/" . basename($event->file). $this->pn .  ".xml" ;
                if(!copy($event->file, $fileName))
                        throw new Exception ("Could not copy XML file $event->file to $fileName");
        }


}
?>

Open in new window

0
Comment
Question by:soozh
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35044362
Can you post the code which call the class function ?
I assume you did initialize the object of the class
$obj = new MyHandlers();
// and call the function with proper parameter
$obj->onPostParse($SOMEEVENT);

where $SOMEEVENT variable needs to have a $field inside.
0
 

Author Comment

by:soozh
ID: 35044378


Sorry but I do not have the rest of the code.  But you can assume that everything "external" works correctly.  

The purpose of the MyHandler code is to extend an existing system.  I know that the call works, files are saved etc.  Its just the $pn field is empty.

Perhaps I should somehow log the call to onPostParse so I know that it is setting up a value.
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35044400
Ok then put this into the function

        function onPostParse( ParseFieldEvent $event)
        {

          var_dump($event);

          $field = $event->field;
          if($field->key == "personnummer"){
            if ($field->value == ""){
              $this->pn = "Unknown" ;
            }
            else {
              $this->pn = $field->value;
            }
          }
          else { $this->pn = "NotFound" ; }
         
         }
0
 

Author Comment

by:soozh
ID: 35044425

ok and what shall i look for? Where is the dump.  Sorry i know nothing about php or linux.

Is the $pn variable declared correctly?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
wamp versus xampp 4 46
Site hacked - decoding the PHP? 15 66
Scope of $_SESSION 17 30
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now