Solved

php code problem

Posted on 2011-03-05
4
297 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
Comment Utility
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
Comment Utility


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
Comment Utility
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
Comment Utility

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

14 Experts available now in Live!

Get 1:1 Help Now