Solved

php code problem

Posted on 2011-03-05
4
300 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Insert values are dynamic 11 41
How would I do a website like this? 5 54
tutorial for ebay api 3 32
What does != "" mean in programming 6 22
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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 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

18 Experts available now in Live!

Get 1:1 Help Now